blob: 483b8c1b1de0589b0ac2ea2646b1782a4e0ddf00 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
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 Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001060/*
1061 * Set when doing a global update, like enabling all recs or disabling them.
1062 * It is not set when just updating a single ftrace_ops.
1063 */
1064static bool update_all_ops;
1065
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001066#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001067# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001068#endif
1069
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001070static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1071
Steven Rostedtb6887d72009-02-17 12:32:04 -05001072struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001073 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001074 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001075 unsigned long flags;
1076 unsigned long ip;
1077 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001078 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001079};
1080
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001081struct ftrace_func_entry {
1082 struct hlist_node hlist;
1083 unsigned long ip;
1084};
1085
1086struct ftrace_hash {
1087 unsigned long size_bits;
1088 struct hlist_head *buckets;
1089 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001090 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001091};
1092
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001093/*
1094 * We make these constant because no one should touch them,
1095 * but they are used as the default "empty hash", to avoid allocating
1096 * it all the time. These are in a read only section such that if
1097 * anyone does try to modify it, it will cause an exception.
1098 */
1099static const struct hlist_head empty_buckets[1];
1100static const struct ftrace_hash empty_hash = {
1101 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001102};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001103#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001104
Steven Rostedt2b499382011-05-03 22:49:52 -04001105static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001106 .func = ftrace_stub,
1107 .local_hash.notrace_hash = EMPTY_HASH,
1108 .local_hash.filter_hash = EMPTY_HASH,
1109 INIT_OPS_HASH(global_ops)
1110 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1111 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001112};
1113
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001114struct ftrace_page {
1115 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001116 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001117 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001118 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001119};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001120
Steven Rostedta7900872011-12-16 16:23:44 -05001121#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1122#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001123
1124/* estimate from running different kernels */
1125#define NR_TO_INIT 10000
1126
1127static struct ftrace_page *ftrace_pages_start;
1128static struct ftrace_page *ftrace_pages;
1129
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001130static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001131{
1132 return !hash || !hash->count;
1133}
1134
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001135static struct ftrace_func_entry *
1136ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1137{
1138 unsigned long key;
1139 struct ftrace_func_entry *entry;
1140 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001141
Steven Rostedt06a51d92011-12-19 19:07:36 -05001142 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001143 return NULL;
1144
1145 if (hash->size_bits > 0)
1146 key = hash_long(ip, hash->size_bits);
1147 else
1148 key = 0;
1149
1150 hhd = &hash->buckets[key];
1151
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001152 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001153 if (entry->ip == ip)
1154 return entry;
1155 }
1156 return NULL;
1157}
1158
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001159static void __add_hash_entry(struct ftrace_hash *hash,
1160 struct ftrace_func_entry *entry)
1161{
1162 struct hlist_head *hhd;
1163 unsigned long key;
1164
1165 if (hash->size_bits)
1166 key = hash_long(entry->ip, hash->size_bits);
1167 else
1168 key = 0;
1169
1170 hhd = &hash->buckets[key];
1171 hlist_add_head(&entry->hlist, hhd);
1172 hash->count++;
1173}
1174
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001175static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1176{
1177 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178
1179 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1180 if (!entry)
1181 return -ENOMEM;
1182
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001183 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001184 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001185
1186 return 0;
1187}
1188
1189static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001190free_hash_entry(struct ftrace_hash *hash,
1191 struct ftrace_func_entry *entry)
1192{
1193 hlist_del(&entry->hlist);
1194 kfree(entry);
1195 hash->count--;
1196}
1197
1198static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199remove_hash_entry(struct ftrace_hash *hash,
1200 struct ftrace_func_entry *entry)
1201{
1202 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001203 hash->count--;
1204}
1205
1206static void ftrace_hash_clear(struct ftrace_hash *hash)
1207{
1208 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210 struct ftrace_func_entry *entry;
1211 int size = 1 << hash->size_bits;
1212 int i;
1213
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001214 if (!hash->count)
1215 return;
1216
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001217 for (i = 0; i < size; i++) {
1218 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001219 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001220 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001221 }
1222 FTRACE_WARN_ON(hash->count);
1223}
1224
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001225static void free_ftrace_hash(struct ftrace_hash *hash)
1226{
1227 if (!hash || hash == EMPTY_HASH)
1228 return;
1229 ftrace_hash_clear(hash);
1230 kfree(hash->buckets);
1231 kfree(hash);
1232}
1233
Steven Rostedt07fd5512011-05-05 18:03:47 -04001234static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1235{
1236 struct ftrace_hash *hash;
1237
1238 hash = container_of(rcu, struct ftrace_hash, rcu);
1239 free_ftrace_hash(hash);
1240}
1241
1242static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1243{
1244 if (!hash || hash == EMPTY_HASH)
1245 return;
1246 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1247}
1248
Jiri Olsa5500fa52012-02-15 15:51:54 +01001249void ftrace_free_filter(struct ftrace_ops *ops)
1250{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001251 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001252 free_ftrace_hash(ops->func_hash->filter_hash);
1253 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001254}
1255
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001256static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1257{
1258 struct ftrace_hash *hash;
1259 int size;
1260
1261 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1262 if (!hash)
1263 return NULL;
1264
1265 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001266 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001267
1268 if (!hash->buckets) {
1269 kfree(hash);
1270 return NULL;
1271 }
1272
1273 hash->size_bits = size_bits;
1274
1275 return hash;
1276}
1277
1278static struct ftrace_hash *
1279alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1280{
1281 struct ftrace_func_entry *entry;
1282 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001283 int size;
1284 int ret;
1285 int i;
1286
1287 new_hash = alloc_ftrace_hash(size_bits);
1288 if (!new_hash)
1289 return NULL;
1290
1291 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001292 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001293 return new_hash;
1294
1295 size = 1 << hash->size_bits;
1296 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001297 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001298 ret = add_hash_entry(new_hash, entry->ip);
1299 if (ret < 0)
1300 goto free_hash;
1301 }
1302 }
1303
1304 FTRACE_WARN_ON(new_hash->count != hash->count);
1305
1306 return new_hash;
1307
1308 free_hash:
1309 free_ftrace_hash(new_hash);
1310 return NULL;
1311}
1312
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001313static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001314ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001315static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001316ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001317
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001318static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001319ftrace_hash_move(struct ftrace_ops *ops, int enable,
1320 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001321{
1322 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001323 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001324 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001325 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326 int size = src->count;
1327 int bits = 0;
1328 int i;
1329
1330 /*
1331 * If the new source is empty, just free dst and assign it
1332 * the empty_hash.
1333 */
1334 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001335 new_hash = EMPTY_HASH;
1336 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 }
1338
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001339 /*
1340 * Make the hash size about 1/2 the # found
1341 */
1342 for (size /= 2; size; size >>= 1)
1343 bits++;
1344
1345 /* Don't allocate too much */
1346 if (bits > FTRACE_HASH_MAX_BITS)
1347 bits = FTRACE_HASH_MAX_BITS;
1348
Steven Rostedt07fd5512011-05-05 18:03:47 -04001349 new_hash = alloc_ftrace_hash(bits);
1350 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001351 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001352
1353 size = 1 << src->size_bits;
1354 for (i = 0; i < size; i++) {
1355 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001356 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001357 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001358 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001359 }
1360 }
1361
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001362update:
1363 /*
1364 * Remove the current set, update the hash and add
1365 * them back.
1366 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001367 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001368
Steven Rostedt07fd5512011-05-05 18:03:47 -04001369 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001370
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001371 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001372
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001373 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001374}
1375
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001376static bool hash_contains_ip(unsigned long ip,
1377 struct ftrace_ops_hash *hash)
1378{
1379 /*
1380 * The function record is a match if it exists in the filter
1381 * hash and not in the notrace hash. Note, an emty hash is
1382 * considered a match for the filter hash, but an empty
1383 * notrace hash is considered not in the notrace hash.
1384 */
1385 return (ftrace_hash_empty(hash->filter_hash) ||
1386 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1387 (ftrace_hash_empty(hash->notrace_hash) ||
1388 !ftrace_lookup_ip(hash->notrace_hash, ip));
1389}
1390
Steven Rostedt265c8312009-02-13 12:43:56 -05001391/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001392 * Test the hashes for this ops to see if we want to call
1393 * the ops->func or not.
1394 *
1395 * It's a match if the ip is in the ops->filter_hash or
1396 * the filter_hash does not exist or is empty,
1397 * AND
1398 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001399 *
1400 * This needs to be called with preemption disabled as
1401 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001402 */
1403static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001404ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001405{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001406 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001407 int ret;
1408
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001409#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1410 /*
1411 * There's a small race when adding ops that the ftrace handler
1412 * that wants regs, may be called without them. We can not
1413 * allow that handler to be called if regs is NULL.
1414 */
1415 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1416 return 0;
1417#endif
1418
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001419 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1420 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001421
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001422 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001423 ret = 1;
1424 else
1425 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001426
1427 return ret;
1428}
1429
1430/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001431 * This is a double for. Do not use 'break' to break out of the loop,
1432 * you must use a goto.
1433 */
1434#define do_for_each_ftrace_rec(pg, rec) \
1435 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1436 int _____i; \
1437 for (_____i = 0; _____i < pg->index; _____i++) { \
1438 rec = &pg->records[_____i];
1439
1440#define while_for_each_ftrace_rec() \
1441 } \
1442 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301443
Steven Rostedt5855fea2011-12-16 19:27:42 -05001444
1445static int ftrace_cmp_recs(const void *a, const void *b)
1446{
Steven Rostedta650e022012-04-25 13:48:13 -04001447 const struct dyn_ftrace *key = a;
1448 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001449
Steven Rostedta650e022012-04-25 13:48:13 -04001450 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001451 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001452 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1453 return 1;
1454 return 0;
1455}
1456
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001457static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001458{
1459 struct ftrace_page *pg;
1460 struct dyn_ftrace *rec;
1461 struct dyn_ftrace key;
1462
1463 key.ip = start;
1464 key.flags = end; /* overload flags, as it is unsigned long */
1465
1466 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1467 if (end < pg->records[0].ip ||
1468 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1469 continue;
1470 rec = bsearch(&key, pg->records, pg->index,
1471 sizeof(struct dyn_ftrace),
1472 ftrace_cmp_recs);
1473 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001474 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001475 }
1476
Steven Rostedt5855fea2011-12-16 19:27:42 -05001477 return 0;
1478}
1479
Steven Rostedtc88fd862011-08-16 09:53:39 -04001480/**
1481 * ftrace_location - return true if the ip giving is a traced location
1482 * @ip: the instruction pointer to check
1483 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001484 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001485 * That is, the instruction that is either a NOP or call to
1486 * the function tracer. It checks the ftrace internal tables to
1487 * determine if the address belongs or not.
1488 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001489unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001490{
Steven Rostedta650e022012-04-25 13:48:13 -04001491 return ftrace_location_range(ip, ip);
1492}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001493
Steven Rostedta650e022012-04-25 13:48:13 -04001494/**
1495 * ftrace_text_reserved - return true if range contains an ftrace location
1496 * @start: start of range to search
1497 * @end: end of range to search (inclusive). @end points to the last byte to check.
1498 *
1499 * Returns 1 if @start and @end contains a ftrace location.
1500 * That is, the instruction that is either a NOP or call to
1501 * the function tracer. It checks the ftrace internal tables to
1502 * determine if the address belongs or not.
1503 */
Sasha Levind88471c2013-01-09 18:09:20 -05001504int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001505{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001506 unsigned long ret;
1507
1508 ret = ftrace_location_range((unsigned long)start,
1509 (unsigned long)end);
1510
1511 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001512}
1513
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001514/* Test if ops registered to this rec needs regs */
1515static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1516{
1517 struct ftrace_ops *ops;
1518 bool keep_regs = false;
1519
1520 for (ops = ftrace_ops_list;
1521 ops != &ftrace_list_end; ops = ops->next) {
1522 /* pass rec in as regs to have non-NULL val */
1523 if (ftrace_ops_test(ops, rec->ip, rec)) {
1524 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1525 keep_regs = true;
1526 break;
1527 }
1528 }
1529 }
1530
1531 return keep_regs;
1532}
1533
Steven Rostedted926f92011-05-03 13:25:24 -04001534static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1535 int filter_hash,
1536 bool inc)
1537{
1538 struct ftrace_hash *hash;
1539 struct ftrace_hash *other_hash;
1540 struct ftrace_page *pg;
1541 struct dyn_ftrace *rec;
1542 int count = 0;
1543 int all = 0;
1544
1545 /* Only update if the ops has been registered */
1546 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1547 return;
1548
1549 /*
1550 * In the filter_hash case:
1551 * If the count is zero, we update all records.
1552 * Otherwise we just update the items in the hash.
1553 *
1554 * In the notrace_hash case:
1555 * We enable the update in the hash.
1556 * As disabling notrace means enabling the tracing,
1557 * and enabling notrace means disabling, the inc variable
1558 * gets inversed.
1559 */
1560 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001561 hash = ops->func_hash->filter_hash;
1562 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001563 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001564 all = 1;
1565 } else {
1566 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001567 hash = ops->func_hash->notrace_hash;
1568 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001569 /*
1570 * If the notrace hash has no items,
1571 * then there's nothing to do.
1572 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001573 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001574 return;
1575 }
1576
1577 do_for_each_ftrace_rec(pg, rec) {
1578 int in_other_hash = 0;
1579 int in_hash = 0;
1580 int match = 0;
1581
1582 if (all) {
1583 /*
1584 * Only the filter_hash affects all records.
1585 * Update if the record is not in the notrace hash.
1586 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001587 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001588 match = 1;
1589 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001590 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1591 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001592
1593 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001594 * If filter_hash is set, we want to match all functions
1595 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001596 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001597 * If filter_hash is not set, then we are decrementing.
1598 * That means we match anything that is in the hash
1599 * and also in the other_hash. That is, we need to turn
1600 * off functions in the other hash because they are disabled
1601 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001602 */
1603 if (filter_hash && in_hash && !in_other_hash)
1604 match = 1;
1605 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001606 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001607 match = 1;
1608 }
1609 if (!match)
1610 continue;
1611
1612 if (inc) {
1613 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001614 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001615 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001616
1617 /*
1618 * If there's only a single callback registered to a
1619 * function, and the ops has a trampoline registered
1620 * for it, then we can call it directly.
1621 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001622 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001623 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001624 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001625 /*
1626 * If we are adding another function callback
1627 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001628 * custom trampoline in use, then we need to go
1629 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001630 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001631 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001632
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001633 /*
1634 * If any ops wants regs saved for this function
1635 * then all ops will get saved regs.
1636 */
1637 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1638 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001639 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001640 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001641 return;
1642 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001643
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001644 /*
1645 * If the rec had REGS enabled and the ops that is
1646 * being removed had REGS set, then see if there is
1647 * still any ops for this record that wants regs.
1648 * If not, we can stop recording them.
1649 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001650 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001651 rec->flags & FTRACE_FL_REGS &&
1652 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1653 if (!test_rec_ops_needs_regs(rec))
1654 rec->flags &= ~FTRACE_FL_REGS;
1655 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001656
1657 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001658 * If the rec had TRAMP enabled, then it needs to
1659 * be cleared. As TRAMP can only be enabled iff
1660 * there is only a single ops attached to it.
1661 * In otherwords, always disable it on decrementing.
1662 * In the future, we may set it if rec count is
1663 * decremented to one, and the ops that is left
1664 * has a trampoline.
1665 */
1666 rec->flags &= ~FTRACE_FL_TRAMP;
1667
1668 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001669 * flags will be cleared in ftrace_check_record()
1670 * if rec count is zero.
1671 */
Steven Rostedted926f92011-05-03 13:25:24 -04001672 }
1673 count++;
1674 /* Shortcut, if we handled all records, we are done. */
1675 if (!all && count == hash->count)
1676 return;
1677 } while_for_each_ftrace_rec();
1678}
1679
1680static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1681 int filter_hash)
1682{
1683 __ftrace_hash_rec_update(ops, filter_hash, 0);
1684}
1685
1686static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1687 int filter_hash)
1688{
1689 __ftrace_hash_rec_update(ops, filter_hash, 1);
1690}
1691
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001692static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1693 int filter_hash, int inc)
1694{
1695 struct ftrace_ops *op;
1696
1697 __ftrace_hash_rec_update(ops, filter_hash, inc);
1698
1699 if (ops->func_hash != &global_ops.local_hash)
1700 return;
1701
1702 /*
1703 * If the ops shares the global_ops hash, then we need to update
1704 * all ops that are enabled and use this hash.
1705 */
1706 do_for_each_ftrace_op(op, ftrace_ops_list) {
1707 /* Already done */
1708 if (op == ops)
1709 continue;
1710 if (op->func_hash == &global_ops.local_hash)
1711 __ftrace_hash_rec_update(op, filter_hash, inc);
1712 } while_for_each_ftrace_op(op);
1713}
1714
1715static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1716 int filter_hash)
1717{
1718 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1719}
1720
1721static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1722 int filter_hash)
1723{
1724 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1725}
1726
Steven Rostedt05736a42008-09-22 14:55:47 -07001727static void print_ip_ins(const char *fmt, unsigned char *p)
1728{
1729 int i;
1730
1731 printk(KERN_CONT "%s", fmt);
1732
1733 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1734 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1735}
1736
Steven Rostedtc88fd862011-08-16 09:53:39 -04001737/**
1738 * ftrace_bug - report and shutdown function tracer
1739 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1740 * @ip: The address that failed
1741 *
1742 * The arch code that enables or disables the function tracing
1743 * can call ftrace_bug() when it has detected a problem in
1744 * modifying the code. @failed should be one of either:
1745 * EFAULT - if the problem happens on reading the @ip address
1746 * EINVAL - if what is read at @ip is not what was expected
1747 * EPERM - if the problem happens on writting to the @ip address
1748 */
1749void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001750{
1751 switch (failed) {
1752 case -EFAULT:
1753 FTRACE_WARN_ON_ONCE(1);
1754 pr_info("ftrace faulted on modifying ");
1755 print_ip_sym(ip);
1756 break;
1757 case -EINVAL:
1758 FTRACE_WARN_ON_ONCE(1);
1759 pr_info("ftrace failed to modify ");
1760 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001761 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001762 printk(KERN_CONT "\n");
1763 break;
1764 case -EPERM:
1765 FTRACE_WARN_ON_ONCE(1);
1766 pr_info("ftrace faulted on writing ");
1767 print_ip_sym(ip);
1768 break;
1769 default:
1770 FTRACE_WARN_ON_ONCE(1);
1771 pr_info("ftrace faulted on unknown error ");
1772 print_ip_sym(ip);
1773 }
1774}
1775
Steven Rostedtc88fd862011-08-16 09:53:39 -04001776static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001777{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001778 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001779
Steven Rostedt982c3502008-11-15 16:31:41 -05001780 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001781 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001782 *
Steven Rostedted926f92011-05-03 13:25:24 -04001783 * If the record has a ref count, then we need to enable it
1784 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001785 *
Steven Rostedted926f92011-05-03 13:25:24 -04001786 * Otherwise we make sure its disabled.
1787 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001788 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001789 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001790 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001791 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001792 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001793
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001794 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001795 * If enabling and the REGS flag does not match the REGS_EN, or
1796 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1797 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001798 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001799 if (flag) {
1800 if (!(rec->flags & FTRACE_FL_REGS) !=
1801 !(rec->flags & FTRACE_FL_REGS_EN))
1802 flag |= FTRACE_FL_REGS;
1803
1804 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1805 !(rec->flags & FTRACE_FL_TRAMP_EN))
1806 flag |= FTRACE_FL_TRAMP;
1807 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001808
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001809 /* If the state of this record hasn't changed, then do nothing */
1810 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001811 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001812
1813 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001814 /* Save off if rec is being enabled (for return value) */
1815 flag ^= rec->flags & FTRACE_FL_ENABLED;
1816
1817 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001818 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001819 if (flag & FTRACE_FL_REGS) {
1820 if (rec->flags & FTRACE_FL_REGS)
1821 rec->flags |= FTRACE_FL_REGS_EN;
1822 else
1823 rec->flags &= ~FTRACE_FL_REGS_EN;
1824 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001825 if (flag & FTRACE_FL_TRAMP) {
1826 if (rec->flags & FTRACE_FL_TRAMP)
1827 rec->flags |= FTRACE_FL_TRAMP_EN;
1828 else
1829 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1830 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001831 }
1832
1833 /*
1834 * If this record is being updated from a nop, then
1835 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001836 * Otherwise,
1837 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001838 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001839 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001840 */
1841 if (flag & FTRACE_FL_ENABLED)
1842 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001843
1844 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001845 }
1846
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001847 if (update) {
1848 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001849 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001850 rec->flags = 0;
1851 else
1852 /* Just disable the record (keep REGS state) */
1853 rec->flags &= ~FTRACE_FL_ENABLED;
1854 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001855
1856 return FTRACE_UPDATE_MAKE_NOP;
1857}
1858
1859/**
1860 * ftrace_update_record, set a record that now is tracing or not
1861 * @rec: the record to update
1862 * @enable: set to 1 if the record is tracing, zero to force disable
1863 *
1864 * The records that represent all functions that can be traced need
1865 * to be updated when tracing has been enabled.
1866 */
1867int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1868{
1869 return ftrace_check_record(rec, enable, 1);
1870}
1871
1872/**
1873 * ftrace_test_record, check if the record has been enabled or not
1874 * @rec: the record to test
1875 * @enable: set to 1 to check if enabled, 0 if it is disabled
1876 *
1877 * The arch code may need to test if a record is already set to
1878 * tracing to determine how to modify the function code that it
1879 * represents.
1880 */
1881int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1882{
1883 return ftrace_check_record(rec, enable, 0);
1884}
1885
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001886static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001887ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
1888{
1889 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001890 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001891
1892 do_for_each_ftrace_op(op, ftrace_ops_list) {
1893
1894 if (!op->trampoline)
1895 continue;
1896
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001897 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001898 return op;
1899 } while_for_each_ftrace_op(op);
1900
1901 return NULL;
1902}
1903
1904static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001905ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1906{
1907 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001908 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001909
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001910 /*
1911 * Need to check removed ops first.
1912 * If they are being removed, and this rec has a tramp,
1913 * and this rec is in the ops list, then it would be the
1914 * one with the tramp.
1915 */
1916 if (removed_ops) {
1917 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001918 return removed_ops;
1919 }
1920
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001921 /*
1922 * Need to find the current trampoline for a rec.
1923 * Now, a trampoline is only attached to a rec if there
1924 * was a single 'ops' attached to it. But this can be called
1925 * when we are adding another op to the rec or removing the
1926 * current one. Thus, if the op is being added, we can
1927 * ignore it because it hasn't attached itself to the rec
1928 * yet. That means we just need to find the op that has a
1929 * trampoline and is not beeing added.
1930 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001931 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001932
1933 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001934 continue;
1935
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001936 /*
1937 * If the ops is being added, it hasn't gotten to
1938 * the point to be removed from this tree yet.
1939 */
1940 if (op->flags & FTRACE_OPS_FL_ADDING)
1941 continue;
1942
1943 /*
1944 * If the ops is not being added and has a trampoline,
1945 * then it must be the one that we want!
1946 */
1947 if (hash_contains_ip(ip, op->func_hash))
1948 return op;
1949
1950 /* If the ops is being modified, it may be in the old hash. */
1951 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
1952 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001953 return op;
1954
1955 } while_for_each_ftrace_op(op);
1956
1957 return NULL;
1958}
1959
1960static struct ftrace_ops *
1961ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1962{
1963 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001964 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001965
1966 do_for_each_ftrace_op(op, ftrace_ops_list) {
1967 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001968 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001969 return op;
1970 } while_for_each_ftrace_op(op);
1971
1972 return NULL;
1973}
1974
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001975/**
1976 * ftrace_get_addr_new - Get the call address to set to
1977 * @rec: The ftrace record descriptor
1978 *
1979 * If the record has the FTRACE_FL_REGS set, that means that it
1980 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1981 * is not not set, then it wants to convert to the normal callback.
1982 *
1983 * Returns the address of the trampoline to set to
1984 */
1985unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1986{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001987 struct ftrace_ops *ops;
1988
1989 /* Trampolines take precedence over regs */
1990 if (rec->flags & FTRACE_FL_TRAMP) {
1991 ops = ftrace_find_tramp_ops_new(rec);
1992 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001993 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
1994 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001995 /* Ftrace is shutting down, return anything */
1996 return (unsigned long)FTRACE_ADDR;
1997 }
1998 return ops->trampoline;
1999 }
2000
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002001 if (rec->flags & FTRACE_FL_REGS)
2002 return (unsigned long)FTRACE_REGS_ADDR;
2003 else
2004 return (unsigned long)FTRACE_ADDR;
2005}
2006
2007/**
2008 * ftrace_get_addr_curr - Get the call address that is already there
2009 * @rec: The ftrace record descriptor
2010 *
2011 * The FTRACE_FL_REGS_EN is set when the record already points to
2012 * a function that saves all the regs. Basically the '_EN' version
2013 * represents the current state of the function.
2014 *
2015 * Returns the address of the trampoline that is currently being called
2016 */
2017unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2018{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002019 struct ftrace_ops *ops;
2020
2021 /* Trampolines take precedence over regs */
2022 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2023 ops = ftrace_find_tramp_ops_curr(rec);
2024 if (FTRACE_WARN_ON(!ops)) {
2025 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2026 (void *)rec->ip, (void *)rec->ip);
2027 /* Ftrace is shutting down, return anything */
2028 return (unsigned long)FTRACE_ADDR;
2029 }
2030 return ops->trampoline;
2031 }
2032
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002033 if (rec->flags & FTRACE_FL_REGS_EN)
2034 return (unsigned long)FTRACE_REGS_ADDR;
2035 else
2036 return (unsigned long)FTRACE_ADDR;
2037}
2038
Steven Rostedtc88fd862011-08-16 09:53:39 -04002039static int
2040__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2041{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002042 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002043 unsigned long ftrace_addr;
2044 int ret;
2045
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002046 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002047
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002048 /* This needs to be done before we call ftrace_update_record */
2049 ftrace_old_addr = ftrace_get_addr_curr(rec);
2050
2051 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002052
Steven Rostedtc88fd862011-08-16 09:53:39 -04002053 switch (ret) {
2054 case FTRACE_UPDATE_IGNORE:
2055 return 0;
2056
2057 case FTRACE_UPDATE_MAKE_CALL:
2058 return ftrace_make_call(rec, ftrace_addr);
2059
2060 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002061 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002062
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002063 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002064 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002065 }
2066
2067 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002068}
2069
Steven Rostedte4f5d542012-04-27 09:13:18 -04002070void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002071{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002072 struct dyn_ftrace *rec;
2073 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002074 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002075
Steven Rostedt45a4a232011-04-21 23:16:46 -04002076 if (unlikely(ftrace_disabled))
2077 return;
2078
Steven Rostedt265c8312009-02-13 12:43:56 -05002079 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002080 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002081 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04002082 ftrace_bug(failed, rec->ip);
2083 /* Stop processing */
2084 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002085 }
2086 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002087}
2088
Steven Rostedtc88fd862011-08-16 09:53:39 -04002089struct ftrace_rec_iter {
2090 struct ftrace_page *pg;
2091 int index;
2092};
2093
2094/**
2095 * ftrace_rec_iter_start, start up iterating over traced functions
2096 *
2097 * Returns an iterator handle that is used to iterate over all
2098 * the records that represent address locations where functions
2099 * are traced.
2100 *
2101 * May return NULL if no records are available.
2102 */
2103struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2104{
2105 /*
2106 * We only use a single iterator.
2107 * Protected by the ftrace_lock mutex.
2108 */
2109 static struct ftrace_rec_iter ftrace_rec_iter;
2110 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2111
2112 iter->pg = ftrace_pages_start;
2113 iter->index = 0;
2114
2115 /* Could have empty pages */
2116 while (iter->pg && !iter->pg->index)
2117 iter->pg = iter->pg->next;
2118
2119 if (!iter->pg)
2120 return NULL;
2121
2122 return iter;
2123}
2124
2125/**
2126 * ftrace_rec_iter_next, get the next record to process.
2127 * @iter: The handle to the iterator.
2128 *
2129 * Returns the next iterator after the given iterator @iter.
2130 */
2131struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2132{
2133 iter->index++;
2134
2135 if (iter->index >= iter->pg->index) {
2136 iter->pg = iter->pg->next;
2137 iter->index = 0;
2138
2139 /* Could have empty pages */
2140 while (iter->pg && !iter->pg->index)
2141 iter->pg = iter->pg->next;
2142 }
2143
2144 if (!iter->pg)
2145 return NULL;
2146
2147 return iter;
2148}
2149
2150/**
2151 * ftrace_rec_iter_record, get the record at the iterator location
2152 * @iter: The current iterator location
2153 *
2154 * Returns the record that the current @iter is at.
2155 */
2156struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2157{
2158 return &iter->pg->records[iter->index];
2159}
2160
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302161static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002162ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002163{
2164 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002165 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002166
2167 ip = rec->ip;
2168
Steven Rostedt45a4a232011-04-21 23:16:46 -04002169 if (unlikely(ftrace_disabled))
2170 return 0;
2171
Shaohua Li25aac9d2009-01-09 11:29:40 +08002172 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002173 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002174 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302175 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002176 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302177 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002178}
2179
Steven Rostedt000ab692009-02-17 13:35:06 -05002180/*
2181 * archs can override this function if they must do something
2182 * before the modifying code is performed.
2183 */
2184int __weak ftrace_arch_code_modify_prepare(void)
2185{
2186 return 0;
2187}
2188
2189/*
2190 * archs can override this function if they must do something
2191 * after the modifying code is performed.
2192 */
2193int __weak ftrace_arch_code_modify_post_process(void)
2194{
2195 return 0;
2196}
2197
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002198void ftrace_modify_all_code(int command)
2199{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002200 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002201 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002202
2203 /*
2204 * If the ftrace_caller calls a ftrace_ops func directly,
2205 * we need to make sure that it only traces functions it
2206 * expects to trace. When doing the switch of functions,
2207 * we need to update to the ftrace_ops_list_func first
2208 * before the transition between old and new calls are set,
2209 * as the ftrace_ops_list_func will check the ops hashes
2210 * to make sure the ops are having the right functions
2211 * traced.
2212 */
Petr Mladekcd210672014-02-24 17:12:21 +01002213 if (update) {
2214 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2215 if (FTRACE_WARN_ON(err))
2216 return;
2217 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002218
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002219 if (command & FTRACE_UPDATE_CALLS)
2220 ftrace_replace_code(1);
2221 else if (command & FTRACE_DISABLE_CALLS)
2222 ftrace_replace_code(0);
2223
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002224 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2225 function_trace_op = set_function_trace_op;
2226 smp_wmb();
2227 /* If irqs are disabled, we are in stop machine */
2228 if (!irqs_disabled())
2229 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002230 err = ftrace_update_ftrace_func(ftrace_trace_function);
2231 if (FTRACE_WARN_ON(err))
2232 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002233 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002234
2235 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002236 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002237 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002238 err = ftrace_disable_ftrace_graph_caller();
2239 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002240}
2241
Ingo Molnare309b412008-05-12 21:20:51 +02002242static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002243{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002244 int *command = data;
2245
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002246 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002247
Steven Rostedtc88fd862011-08-16 09:53:39 -04002248 return 0;
2249}
2250
2251/**
2252 * ftrace_run_stop_machine, go back to the stop machine method
2253 * @command: The command to tell ftrace what to do
2254 *
2255 * If an arch needs to fall back to the stop machine method, the
2256 * it can call this function.
2257 */
2258void ftrace_run_stop_machine(int command)
2259{
2260 stop_machine(__ftrace_modify_code, &command, NULL);
2261}
2262
2263/**
2264 * arch_ftrace_update_code, modify the code to trace or not trace
2265 * @command: The command that needs to be done
2266 *
2267 * Archs can override this function if it does not need to
2268 * run stop_machine() to modify code.
2269 */
2270void __weak arch_ftrace_update_code(int command)
2271{
2272 ftrace_run_stop_machine(command);
2273}
2274
2275static void ftrace_run_update_code(int command)
2276{
2277 int ret;
2278
2279 ret = ftrace_arch_code_modify_prepare();
2280 FTRACE_WARN_ON(ret);
2281 if (ret)
2282 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002283
2284 /*
2285 * By default we use stop_machine() to modify the code.
2286 * But archs can do what ever they want as long as it
2287 * is safe. The stop_machine() is the safest, but also
2288 * produces the most overhead.
2289 */
2290 arch_ftrace_update_code(command);
2291
Steven Rostedt000ab692009-02-17 13:35:06 -05002292 ret = ftrace_arch_code_modify_post_process();
2293 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002294}
2295
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002296static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2297 struct ftrace_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002298{
2299 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002300 ops->old_hash.filter_hash = old_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002301 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002302 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002303 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2304}
2305
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002306static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002307static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002308
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002309static void control_ops_free(struct ftrace_ops *ops)
2310{
2311 free_percpu(ops->disabled);
2312}
2313
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002314static void ftrace_startup_enable(int command)
2315{
2316 if (saved_ftrace_func != ftrace_trace_function) {
2317 saved_ftrace_func = ftrace_trace_function;
2318 command |= FTRACE_UPDATE_TRACE_FUNC;
2319 }
2320
2321 if (!command || !ftrace_enabled)
2322 return;
2323
2324 ftrace_run_update_code(command);
2325}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002326
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002327static void ftrace_startup_all(int command)
2328{
2329 update_all_ops = true;
2330 ftrace_startup_enable(command);
2331 update_all_ops = false;
2332}
2333
Steven Rostedta1cd6172011-05-23 15:24:25 -04002334static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002335{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002336 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002337
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002338 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002339 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002340
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002341 ret = __register_ftrace_function(ops);
2342 if (ret)
2343 return ret;
2344
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002345 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002346 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002347
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002348 /*
2349 * Note that ftrace probes uses this to start up
2350 * and modify functions it will probe. But we still
2351 * set the ADDING flag for modification, as probes
2352 * do not have trampolines. If they add them in the
2353 * future, then the probes will need to distinguish
2354 * between adding and updating probes.
2355 */
2356 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002357
2358 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002359
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002360 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002361
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002362 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2363
Steven Rostedta1cd6172011-05-23 15:24:25 -04002364 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002365}
2366
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002367static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002368{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002369 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002370
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002371 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002372 return -ENODEV;
2373
2374 ret = __unregister_ftrace_function(ops);
2375 if (ret)
2376 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002377
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002378 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002379 /*
2380 * Just warn in case of unbalance, no need to kill ftrace, it's not
2381 * critical but the ftrace_call callers may be never nopped again after
2382 * further ftrace uses.
2383 */
2384 WARN_ON_ONCE(ftrace_start_up < 0);
2385
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002386 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002387
Namhyung Kima737e6d2014-06-12 23:56:12 +09002388 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002389
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002390 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002391
2392 if (saved_ftrace_func != ftrace_trace_function) {
2393 saved_ftrace_func = ftrace_trace_function;
2394 command |= FTRACE_UPDATE_TRACE_FUNC;
2395 }
2396
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002397 if (!command || !ftrace_enabled) {
2398 /*
2399 * If these are control ops, they still need their
2400 * per_cpu field freed. Since, function tracing is
2401 * not currently active, we can just free them
2402 * without synchronizing all CPUs.
2403 */
2404 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2405 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002406 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002407 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002408
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002409 /*
2410 * If the ops uses a trampoline, then it needs to be
2411 * tested first on update.
2412 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002413 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002414 removed_ops = ops;
2415
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002416 /* The trampoline logic checks the old hashes */
2417 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2418 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2419
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002420 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002421
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002422 /*
2423 * If there's no more ops registered with ftrace, run a
2424 * sanity check to make sure all rec flags are cleared.
2425 */
2426 if (ftrace_ops_list == &ftrace_list_end) {
2427 struct ftrace_page *pg;
2428 struct dyn_ftrace *rec;
2429
2430 do_for_each_ftrace_rec(pg, rec) {
2431 if (FTRACE_WARN_ON_ONCE(rec->flags))
2432 pr_warn(" %pS flags:%lx\n",
2433 (void *)rec->ip, rec->flags);
2434 } while_for_each_ftrace_rec();
2435 }
2436
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002437 ops->old_hash.filter_hash = NULL;
2438 ops->old_hash.notrace_hash = NULL;
2439
2440 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002441 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002442
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002443 /*
2444 * Dynamic ops may be freed, we must make sure that all
2445 * callers are done before leaving this function.
2446 * The same goes for freeing the per_cpu data of the control
2447 * ops.
2448 *
2449 * Again, normal synchronize_sched() is not good enough.
2450 * We need to do a hard force of sched synchronization.
2451 * This is because we use preempt_disable() to do RCU, but
2452 * the function tracers can be called where RCU is not watching
2453 * (like before user_exit()). We can not rely on the RCU
2454 * infrastructure to do the synchronization, thus we must do it
2455 * ourselves.
2456 */
2457 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2458 schedule_on_each_cpu(ftrace_sync);
2459
2460 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2461 control_ops_free(ops);
2462 }
2463
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002464 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002465}
2466
Ingo Molnare309b412008-05-12 21:20:51 +02002467static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002468{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002469 if (unlikely(ftrace_disabled))
2470 return;
2471
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002472 /* Force update next time */
2473 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002474 /* ftrace_start_up is true if we want ftrace running */
2475 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002476 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002477}
2478
Ingo Molnare309b412008-05-12 21:20:51 +02002479static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002480{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002481 if (unlikely(ftrace_disabled))
2482 return;
2483
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002484 /* ftrace_start_up is true if ftrace is running */
2485 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002486 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002487}
2488
Steven Rostedt3d083392008-05-12 21:20:42 +02002489static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002490unsigned long ftrace_update_tot_cnt;
2491
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002492static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002493{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002494 /*
2495 * Filter_hash being empty will default to trace module.
2496 * But notrace hash requires a test of individual module functions.
2497 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002498 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2499 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002500}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002501
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002502/*
2503 * Check if the current ops references the record.
2504 *
2505 * If the ops traces all functions, then it was already accounted for.
2506 * If the ops does not trace the current record function, skip it.
2507 * If the ops ignores the function via notrace filter, skip it.
2508 */
2509static inline bool
2510ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2511{
2512 /* If ops isn't enabled, ignore it */
2513 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2514 return 0;
2515
2516 /* If ops traces all mods, we already accounted for it */
2517 if (ops_traces_mod(ops))
2518 return 0;
2519
2520 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002521 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2522 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002523 return 0;
2524
2525 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002526 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002527 return 0;
2528
2529 return 1;
2530}
2531
2532static int referenced_filters(struct dyn_ftrace *rec)
2533{
2534 struct ftrace_ops *ops;
2535 int cnt = 0;
2536
2537 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2538 if (ops_references_rec(ops, rec))
2539 cnt++;
2540 }
2541
2542 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002543}
2544
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002545static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002546{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002547 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002548 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302549 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002550 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002551 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002552 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002553 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002554
2555 /*
2556 * When adding a module, we need to check if tracers are
2557 * currently enabled and if they are set to trace all functions.
2558 * If they are, we need to enable the module functions as well
2559 * as update the reference counts for those function records.
2560 */
2561 if (mod) {
2562 struct ftrace_ops *ops;
2563
2564 for (ops = ftrace_ops_list;
2565 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002566 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2567 if (ops_traces_mod(ops))
2568 ref++;
2569 else
2570 test = true;
2571 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002572 }
2573 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002574
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002575 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002576
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002577 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302578
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002579 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002580 int cnt = ref;
2581
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002582 /* If something went wrong, bail without enabling anything */
2583 if (unlikely(ftrace_disabled))
2584 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002585
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002586 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002587 if (test)
2588 cnt += referenced_filters(p);
2589 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302590
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002591 /*
2592 * Do the initial record conversion from mcount jump
2593 * to the NOP instructions.
2594 */
2595 if (!ftrace_code_disable(mod, p))
2596 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002597
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002598 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002599
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002600 /*
2601 * If the tracing is enabled, go ahead and enable the record.
2602 *
2603 * The reason not to enable the record immediatelly is the
2604 * inherent check of ftrace_make_nop/ftrace_make_call for
2605 * correct previous instructions. Making first the NOP
2606 * conversion puts the module to the correct state, thus
2607 * passing the ftrace_make_call check.
2608 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002609 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002610 int failed = __ftrace_replace_code(p, 1);
2611 if (failed)
2612 ftrace_bug(failed, p->ip);
2613 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002614 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002615 }
2616
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002617 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002618 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002619 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002620
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002621 return 0;
2622}
2623
Steven Rostedta7900872011-12-16 16:23:44 -05002624static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002625{
Steven Rostedta7900872011-12-16 16:23:44 -05002626 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002627 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002628
Steven Rostedta7900872011-12-16 16:23:44 -05002629 if (WARN_ON(!count))
2630 return -EINVAL;
2631
2632 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002633
2634 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002635 * We want to fill as much as possible. No more than a page
2636 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002637 */
Steven Rostedta7900872011-12-16 16:23:44 -05002638 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2639 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002640
Steven Rostedta7900872011-12-16 16:23:44 -05002641 again:
2642 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2643
2644 if (!pg->records) {
2645 /* if we can't allocate this size, try something smaller */
2646 if (!order)
2647 return -ENOMEM;
2648 order >>= 1;
2649 goto again;
2650 }
2651
2652 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2653 pg->size = cnt;
2654
2655 if (cnt > count)
2656 cnt = count;
2657
2658 return cnt;
2659}
2660
2661static struct ftrace_page *
2662ftrace_allocate_pages(unsigned long num_to_init)
2663{
2664 struct ftrace_page *start_pg;
2665 struct ftrace_page *pg;
2666 int order;
2667 int cnt;
2668
2669 if (!num_to_init)
2670 return 0;
2671
2672 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2673 if (!pg)
2674 return NULL;
2675
2676 /*
2677 * Try to allocate as much as possible in one continues
2678 * location that fills in all of the space. We want to
2679 * waste as little space as possible.
2680 */
2681 for (;;) {
2682 cnt = ftrace_allocate_records(pg, num_to_init);
2683 if (cnt < 0)
2684 goto free_pages;
2685
2686 num_to_init -= cnt;
2687 if (!num_to_init)
2688 break;
2689
2690 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2691 if (!pg->next)
2692 goto free_pages;
2693
2694 pg = pg->next;
2695 }
2696
2697 return start_pg;
2698
2699 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002700 pg = start_pg;
2701 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002702 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2703 free_pages((unsigned long)pg->records, order);
2704 start_pg = pg->next;
2705 kfree(pg);
2706 pg = start_pg;
2707 }
2708 pr_info("ftrace: FAILED to allocate memory for functions\n");
2709 return NULL;
2710}
2711
Steven Rostedt5072c592008-05-12 21:20:43 +02002712#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2713
2714struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002715 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002716 loff_t func_pos;
2717 struct ftrace_page *pg;
2718 struct dyn_ftrace *func;
2719 struct ftrace_func_probe *probe;
2720 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002721 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002722 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002723 int hidx;
2724 int idx;
2725 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002726};
2727
Ingo Molnare309b412008-05-12 21:20:51 +02002728static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002729t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002730{
2731 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002732 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002733 struct hlist_head *hhd;
2734
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002735 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002736 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002737
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002738 if (iter->probe)
2739 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002740 retry:
2741 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2742 return NULL;
2743
2744 hhd = &ftrace_func_hash[iter->hidx];
2745
2746 if (hlist_empty(hhd)) {
2747 iter->hidx++;
2748 hnd = NULL;
2749 goto retry;
2750 }
2751
2752 if (!hnd)
2753 hnd = hhd->first;
2754 else {
2755 hnd = hnd->next;
2756 if (!hnd) {
2757 iter->hidx++;
2758 goto retry;
2759 }
2760 }
2761
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002762 if (WARN_ON_ONCE(!hnd))
2763 return NULL;
2764
2765 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2766
2767 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002768}
2769
2770static void *t_hash_start(struct seq_file *m, loff_t *pos)
2771{
2772 struct ftrace_iterator *iter = m->private;
2773 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002774 loff_t l;
2775
Steven Rostedt69a30832011-12-19 15:21:16 -05002776 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2777 return NULL;
2778
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002779 if (iter->func_pos > *pos)
2780 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002781
Li Zefand82d6242009-06-24 09:54:54 +08002782 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002783 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002784 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002785 if (!p)
2786 break;
2787 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002788 if (!p)
2789 return NULL;
2790
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002791 /* Only set this if we have an item */
2792 iter->flags |= FTRACE_ITER_HASH;
2793
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002794 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002795}
2796
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002797static int
2798t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002799{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002800 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002801
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002802 rec = iter->probe;
2803 if (WARN_ON_ONCE(!rec))
2804 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002805
Steven Rostedt809dcf22009-02-16 23:06:01 -05002806 if (rec->ops->print)
2807 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2808
Steven Rostedtb375a112009-09-17 00:05:58 -04002809 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002810
2811 if (rec->data)
2812 seq_printf(m, ":%p", rec->data);
2813 seq_putc(m, '\n');
2814
2815 return 0;
2816}
2817
2818static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002819t_next(struct seq_file *m, void *v, loff_t *pos)
2820{
2821 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002822 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002823 struct dyn_ftrace *rec = NULL;
2824
Steven Rostedt45a4a232011-04-21 23:16:46 -04002825 if (unlikely(ftrace_disabled))
2826 return NULL;
2827
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002828 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002829 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002830
Steven Rostedt5072c592008-05-12 21:20:43 +02002831 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002832 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002833
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002834 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002835 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002836
Steven Rostedt5072c592008-05-12 21:20:43 +02002837 retry:
2838 if (iter->idx >= iter->pg->index) {
2839 if (iter->pg->next) {
2840 iter->pg = iter->pg->next;
2841 iter->idx = 0;
2842 goto retry;
2843 }
2844 } else {
2845 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002846 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002847 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002848
Steven Rostedt41c52c02008-05-22 11:46:33 -04002849 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002850 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04002851
2852 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002853 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002854
Steven Rostedt5072c592008-05-12 21:20:43 +02002855 rec = NULL;
2856 goto retry;
2857 }
2858 }
2859
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002860 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002861 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002862
2863 iter->func = rec;
2864
2865 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002866}
2867
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002868static void reset_iter_read(struct ftrace_iterator *iter)
2869{
2870 iter->pos = 0;
2871 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002872 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002873}
2874
2875static void *t_start(struct seq_file *m, loff_t *pos)
2876{
2877 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002878 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002879 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002880 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002881
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002882 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002883
2884 if (unlikely(ftrace_disabled))
2885 return NULL;
2886
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002887 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002888 * If an lseek was done, then reset and start from beginning.
2889 */
2890 if (*pos < iter->pos)
2891 reset_iter_read(iter);
2892
2893 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002894 * For set_ftrace_filter reading, if we have the filter
2895 * off, we can short cut and just print out that all
2896 * functions are enabled.
2897 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002898 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002899 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002900 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002901 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002902 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002903 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002904 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002905 /* reset in case of seek/pread */
2906 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002907 return iter;
2908 }
2909
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002910 if (iter->flags & FTRACE_ITER_HASH)
2911 return t_hash_start(m, pos);
2912
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002913 /*
2914 * Unfortunately, we need to restart at ftrace_pages_start
2915 * every time we let go of the ftrace_mutex. This is because
2916 * those pointers can change without the lock.
2917 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002918 iter->pg = ftrace_pages_start;
2919 iter->idx = 0;
2920 for (l = 0; l <= *pos; ) {
2921 p = t_next(m, p, &l);
2922 if (!p)
2923 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002924 }
walimis5821e1b2008-11-15 15:19:06 +08002925
Steven Rostedt69a30832011-12-19 15:21:16 -05002926 if (!p)
2927 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002928
2929 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002930}
2931
2932static void t_stop(struct seq_file *m, void *p)
2933{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002934 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002935}
2936
2937static int t_show(struct seq_file *m, void *v)
2938{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002939 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002940 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002941
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002942 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002943 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002944
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002945 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002946 if (iter->flags & FTRACE_ITER_NOTRACE)
2947 seq_printf(m, "#### no functions disabled ####\n");
2948 else
2949 seq_printf(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002950 return 0;
2951 }
2952
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002953 rec = iter->func;
2954
Steven Rostedt5072c592008-05-12 21:20:43 +02002955 if (!rec)
2956 return 0;
2957
Steven Rostedt647bcd02011-05-03 14:39:21 -04002958 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002959 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002960 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002961 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002962 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2963 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2964 struct ftrace_ops *ops;
2965
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002966 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002967 if (ops)
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002968 seq_printf(m, "\ttramp: %pS",
2969 (void *)ops->trampoline);
2970 else
2971 seq_printf(m, "\ttramp: ERROR!");
2972 }
2973 }
2974
Steven Rostedt647bcd02011-05-03 14:39:21 -04002975 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002976
2977 return 0;
2978}
2979
James Morris88e9d342009-09-22 16:43:43 -07002980static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002981 .start = t_start,
2982 .next = t_next,
2983 .stop = t_stop,
2984 .show = t_show,
2985};
2986
Ingo Molnare309b412008-05-12 21:20:51 +02002987static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002988ftrace_avail_open(struct inode *inode, struct file *file)
2989{
2990 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002991
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002992 if (unlikely(ftrace_disabled))
2993 return -ENODEV;
2994
Jiri Olsa50e18b92012-04-25 10:23:39 +02002995 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2996 if (iter) {
2997 iter->pg = ftrace_pages_start;
2998 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002999 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003000
Jiri Olsa50e18b92012-04-25 10:23:39 +02003001 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003002}
3003
Steven Rostedt647bcd02011-05-03 14:39:21 -04003004static int
3005ftrace_enabled_open(struct inode *inode, struct file *file)
3006{
3007 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003008
3009 if (unlikely(ftrace_disabled))
3010 return -ENODEV;
3011
Jiri Olsa50e18b92012-04-25 10:23:39 +02003012 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3013 if (iter) {
3014 iter->pg = ftrace_pages_start;
3015 iter->flags = FTRACE_ITER_ENABLED;
3016 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003017 }
3018
Jiri Olsa50e18b92012-04-25 10:23:39 +02003019 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003020}
3021
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003022/**
3023 * ftrace_regex_open - initialize function tracer filter files
3024 * @ops: The ftrace_ops that hold the hash filters
3025 * @flag: The type of filter to process
3026 * @inode: The inode, usually passed in to your open routine
3027 * @file: The file, usually passed in to your open routine
3028 *
3029 * ftrace_regex_open() initializes the filter files for the
3030 * @ops. Depending on @flag it may process the filter hash or
3031 * the notrace hash of @ops. With this called from the open
3032 * routine, you can use ftrace_filter_write() for the write
3033 * routine if @flag has FTRACE_ITER_FILTER set, or
3034 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003035 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003036 * release must call ftrace_regex_release().
3037 */
3038int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003039ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003040 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003041{
3042 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003043 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003044 int ret = 0;
3045
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003046 ftrace_ops_init(ops);
3047
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003048 if (unlikely(ftrace_disabled))
3049 return -ENODEV;
3050
Steven Rostedt5072c592008-05-12 21:20:43 +02003051 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3052 if (!iter)
3053 return -ENOMEM;
3054
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003055 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3056 kfree(iter);
3057 return -ENOMEM;
3058 }
3059
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003060 iter->ops = ops;
3061 iter->flags = flag;
3062
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003063 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003064
Steven Rostedtf45948e2011-05-02 12:29:25 -04003065 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003066 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003067 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003068 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003069
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003070 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003071 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3072
3073 if (file->f_flags & O_TRUNC)
3074 iter->hash = alloc_ftrace_hash(size_bits);
3075 else
3076 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3077
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003078 if (!iter->hash) {
3079 trace_parser_put(&iter->parser);
3080 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003081 ret = -ENOMEM;
3082 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003083 }
3084 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003085
Steven Rostedt5072c592008-05-12 21:20:43 +02003086 if (file->f_mode & FMODE_READ) {
3087 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003088
3089 ret = seq_open(file, &show_ftrace_seq_ops);
3090 if (!ret) {
3091 struct seq_file *m = file->private_data;
3092 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003093 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003094 /* Failed */
3095 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003096 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003097 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003098 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003099 } else
3100 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003101
3102 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003103 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003104
3105 return ret;
3106}
3107
Steven Rostedt41c52c02008-05-22 11:46:33 -04003108static int
3109ftrace_filter_open(struct inode *inode, struct file *file)
3110{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003111 struct ftrace_ops *ops = inode->i_private;
3112
3113 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003114 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3115 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003116}
3117
3118static int
3119ftrace_notrace_open(struct inode *inode, struct file *file)
3120{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003121 struct ftrace_ops *ops = inode->i_private;
3122
3123 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003124 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003125}
3126
Steven Rostedt64e7c442009-02-13 17:08:48 -05003127static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003128{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003129 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003130 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003131
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003132 switch (type) {
3133 case MATCH_FULL:
3134 if (strcmp(str, regex) == 0)
3135 matched = 1;
3136 break;
3137 case MATCH_FRONT_ONLY:
3138 if (strncmp(str, regex, len) == 0)
3139 matched = 1;
3140 break;
3141 case MATCH_MIDDLE_ONLY:
3142 if (strstr(str, regex))
3143 matched = 1;
3144 break;
3145 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003146 slen = strlen(str);
3147 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003148 matched = 1;
3149 break;
3150 }
3151
3152 return matched;
3153}
3154
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003155static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003156enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003157{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003158 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003159 int ret = 0;
3160
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003161 entry = ftrace_lookup_ip(hash, rec->ip);
3162 if (not) {
3163 /* Do nothing if it doesn't exist */
3164 if (!entry)
3165 return 0;
3166
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003167 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003168 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003169 /* Do nothing if it exists */
3170 if (entry)
3171 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003172
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003173 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003174 }
3175 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003176}
3177
Steven Rostedt64e7c442009-02-13 17:08:48 -05003178static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003179ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3180 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003181{
3182 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003183 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003184
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003185 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3186
3187 if (mod) {
3188 /* module lookup requires matching the module */
3189 if (!modname || strcmp(modname, mod))
3190 return 0;
3191
3192 /* blank search means to match all funcs in the mod */
3193 if (!len)
3194 return 1;
3195 }
3196
Steven Rostedt64e7c442009-02-13 17:08:48 -05003197 return ftrace_match(str, regex, len, type);
3198}
3199
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003200static int
3201match_records(struct ftrace_hash *hash, char *buff,
3202 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003203{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003204 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003205 struct ftrace_page *pg;
3206 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003207 int type = MATCH_FULL;
3208 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003209 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003210 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003211
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003212 if (len) {
3213 type = filter_parse_regex(buff, len, &search, &not);
3214 search_len = strlen(search);
3215 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003216
Steven Rostedt52baf112009-02-14 01:15:39 -05003217 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003218
3219 if (unlikely(ftrace_disabled))
3220 goto out_unlock;
3221
Steven Rostedt265c8312009-02-13 12:43:56 -05003222 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003223 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003224 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003225 if (ret < 0) {
3226 found = ret;
3227 goto out_unlock;
3228 }
Li Zefan311d16d2009-12-08 11:15:11 +08003229 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003230 }
3231 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003232 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003233 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003234
3235 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003236}
3237
Steven Rostedt64e7c442009-02-13 17:08:48 -05003238static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003239ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003240{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003241 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003242}
3243
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003244static int
3245ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003246{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003247 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003248
Steven Rostedt64e7c442009-02-13 17:08:48 -05003249 /* blank or '*' mean the same */
3250 if (strcmp(buff, "*") == 0)
3251 buff[0] = 0;
3252
3253 /* handle the case of 'dont filter this module' */
3254 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3255 buff[0] = 0;
3256 not = 1;
3257 }
3258
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003259 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003260}
3261
Steven Rostedtf6180772009-02-14 00:40:25 -05003262/*
3263 * We register the module command as a template to show others how
3264 * to register the a command as well.
3265 */
3266
3267static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003268ftrace_mod_callback(struct ftrace_hash *hash,
3269 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003270{
3271 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003272 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003273
3274 /*
3275 * cmd == 'mod' because we only registered this func
3276 * for the 'mod' ftrace_func_command.
3277 * But if you register one func with multiple commands,
3278 * you can tell which command was used by the cmd
3279 * parameter.
3280 */
3281
3282 /* we must have a module name */
3283 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003284 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003285
3286 mod = strsep(&param, ":");
3287 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003288 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003289
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003290 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003291 if (!ret)
3292 ret = -EINVAL;
3293 if (ret < 0)
3294 return ret;
3295
3296 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003297}
3298
3299static struct ftrace_func_command ftrace_mod_cmd = {
3300 .name = "mod",
3301 .func = ftrace_mod_callback,
3302};
3303
3304static int __init ftrace_mod_cmd_init(void)
3305{
3306 return register_ftrace_command(&ftrace_mod_cmd);
3307}
Steven Rostedt6f415672012-10-05 12:13:07 -04003308core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003309
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003310static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003311 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003312{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003313 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003314 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003315 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003316
3317 key = hash_long(ip, FTRACE_HASH_BITS);
3318
3319 hhd = &ftrace_func_hash[key];
3320
3321 if (hlist_empty(hhd))
3322 return;
3323
3324 /*
3325 * Disable preemption for these calls to prevent a RCU grace
3326 * period. This syncs the hash iteration and freeing of items
3327 * on the hash. rcu_read_lock is too dangerous here.
3328 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003329 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003330 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003331 if (entry->ip == ip)
3332 entry->ops->func(ip, parent_ip, &entry->data);
3333 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003334 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003335}
3336
Steven Rostedtb6887d72009-02-17 12:32:04 -05003337static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003338{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003339 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003340 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003341 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003342};
3343
Steven Rostedtb6887d72009-02-17 12:32:04 -05003344static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003345
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003346static void __enable_ftrace_function_probe(struct ftrace_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003347{
Steven Rostedtb8489142011-05-04 09:27:52 -04003348 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003349 int i;
3350
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003351 if (ftrace_probe_registered) {
3352 /* still need to update the function call sites */
3353 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003354 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3355 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003356 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003357 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003358
3359 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3360 struct hlist_head *hhd = &ftrace_func_hash[i];
3361 if (hhd->first)
3362 break;
3363 }
3364 /* Nothing registered? */
3365 if (i == FTRACE_FUNC_HASHSIZE)
3366 return;
3367
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003368 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003369
Steven Rostedtb6887d72009-02-17 12:32:04 -05003370 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003371}
3372
Steven Rostedtb6887d72009-02-17 12:32:04 -05003373static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003374{
3375 int i;
3376
Steven Rostedtb6887d72009-02-17 12:32:04 -05003377 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003378 return;
3379
3380 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3381 struct hlist_head *hhd = &ftrace_func_hash[i];
3382 if (hhd->first)
3383 return;
3384 }
3385
3386 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003387 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003388
Steven Rostedtb6887d72009-02-17 12:32:04 -05003389 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003390}
3391
3392
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003393static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003394{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003395 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003396 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003397 kfree(entry);
3398}
3399
Steven Rostedt59df055f2009-02-14 15:29:06 -05003400int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003401register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003402 void *data)
3403{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003404 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003405 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003406 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003407 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003408 struct ftrace_page *pg;
3409 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003410 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003411 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003412 int count = 0;
3413 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003414 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003415
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003416 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003417 len = strlen(search);
3418
Steven Rostedtb6887d72009-02-17 12:32:04 -05003419 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003420 if (WARN_ON(not))
3421 return -EINVAL;
3422
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003423 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003424
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003425 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003426 if (!hash) {
3427 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003428 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003429 }
3430
3431 if (unlikely(ftrace_disabled)) {
3432 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003433 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003434 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003435
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003436 mutex_lock(&ftrace_lock);
3437
Steven Rostedt59df055f2009-02-14 15:29:06 -05003438 do_for_each_ftrace_rec(pg, rec) {
3439
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003440 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003441 continue;
3442
3443 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3444 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003445 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003446 if (!count)
3447 count = -ENOMEM;
3448 goto out_unlock;
3449 }
3450
3451 count++;
3452
3453 entry->data = data;
3454
3455 /*
3456 * The caller might want to do something special
3457 * for each function we find. We call the callback
3458 * to give the caller an opportunity to do so.
3459 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003460 if (ops->init) {
3461 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003462 /* caller does not like this func */
3463 kfree(entry);
3464 continue;
3465 }
3466 }
3467
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003468 ret = enter_record(hash, rec, 0);
3469 if (ret < 0) {
3470 kfree(entry);
3471 count = ret;
3472 goto out_unlock;
3473 }
3474
Steven Rostedt59df055f2009-02-14 15:29:06 -05003475 entry->ops = ops;
3476 entry->ip = rec->ip;
3477
3478 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3479 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3480
3481 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003482
3483 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003484
3485 __enable_ftrace_function_probe(old_hash);
3486
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003487 if (!ret)
3488 free_ftrace_hash_rcu(old_hash);
3489 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003490 count = ret;
3491
Steven Rostedt59df055f2009-02-14 15:29:06 -05003492 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003493 mutex_unlock(&ftrace_lock);
3494 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003495 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003496 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003497
3498 return count;
3499}
3500
3501enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003502 PROBE_TEST_FUNC = 1,
3503 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003504};
3505
3506static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003507__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003508 void *data, int flags)
3509{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003510 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003511 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003512 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003513 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003514 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003515 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003516 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003517 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003518 char str[KSYM_SYMBOL_LEN];
3519 int type = MATCH_FULL;
3520 int i, len = 0;
3521 char *search;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003522 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003523
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003524 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003525 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003526 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003527 int not;
3528
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003529 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003530 len = strlen(search);
3531
Steven Rostedtb6887d72009-02-17 12:32:04 -05003532 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003533 if (WARN_ON(not))
3534 return;
3535 }
3536
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003537 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003538
3539 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3540 if (!hash)
3541 /* Hmm, should report this somehow */
3542 goto out_unlock;
3543
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003544 INIT_LIST_HEAD(&free_list);
3545
Steven Rostedt59df055f2009-02-14 15:29:06 -05003546 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3547 struct hlist_head *hhd = &ftrace_func_hash[i];
3548
Sasha Levinb67bfe02013-02-27 17:06:00 -08003549 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003550
3551 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003552 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003553 continue;
3554
Steven Rostedtb6887d72009-02-17 12:32:04 -05003555 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003556 continue;
3557
3558 /* do this last, since it is the most expensive */
3559 if (glob) {
3560 kallsyms_lookup(entry->ip, NULL, NULL,
3561 NULL, str);
3562 if (!ftrace_match(str, glob, len, type))
3563 continue;
3564 }
3565
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003566 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3567 /* It is possible more than one entry had this ip */
3568 if (rec_entry)
3569 free_hash_entry(hash, rec_entry);
3570
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003571 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003572 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003573 }
3574 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003575 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003576 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003577 /*
3578 * Remove after the disable is called. Otherwise, if the last
3579 * probe is removed, a null hash means *all enabled*.
3580 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003581 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003582 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003583 if (!ret)
3584 free_ftrace_hash_rcu(old_hash);
3585
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003586 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3587 list_del(&entry->free_list);
3588 ftrace_free_entry(entry);
3589 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003590 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003591
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003592 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003593 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003594 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003595}
3596
3597void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003598unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003599 void *data)
3600{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003601 __unregister_ftrace_function_probe(glob, ops, data,
3602 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003603}
3604
3605void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003606unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003607{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003608 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003609}
3610
Steven Rostedtb6887d72009-02-17 12:32:04 -05003611void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003612{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003613 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003614}
3615
Steven Rostedtf6180772009-02-14 00:40:25 -05003616static LIST_HEAD(ftrace_commands);
3617static DEFINE_MUTEX(ftrace_cmd_mutex);
3618
Tom Zanussi38de93a2013-10-24 08:34:18 -05003619/*
3620 * Currently we only register ftrace commands from __init, so mark this
3621 * __init too.
3622 */
3623__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003624{
3625 struct ftrace_func_command *p;
3626 int ret = 0;
3627
3628 mutex_lock(&ftrace_cmd_mutex);
3629 list_for_each_entry(p, &ftrace_commands, list) {
3630 if (strcmp(cmd->name, p->name) == 0) {
3631 ret = -EBUSY;
3632 goto out_unlock;
3633 }
3634 }
3635 list_add(&cmd->list, &ftrace_commands);
3636 out_unlock:
3637 mutex_unlock(&ftrace_cmd_mutex);
3638
3639 return ret;
3640}
3641
Tom Zanussi38de93a2013-10-24 08:34:18 -05003642/*
3643 * Currently we only unregister ftrace commands from __init, so mark
3644 * this __init too.
3645 */
3646__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003647{
3648 struct ftrace_func_command *p, *n;
3649 int ret = -ENODEV;
3650
3651 mutex_lock(&ftrace_cmd_mutex);
3652 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3653 if (strcmp(cmd->name, p->name) == 0) {
3654 ret = 0;
3655 list_del_init(&p->list);
3656 goto out_unlock;
3657 }
3658 }
3659 out_unlock:
3660 mutex_unlock(&ftrace_cmd_mutex);
3661
3662 return ret;
3663}
3664
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003665static int ftrace_process_regex(struct ftrace_hash *hash,
3666 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003667{
Steven Rostedtf6180772009-02-14 00:40:25 -05003668 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003669 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003670 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003671
3672 func = strsep(&next, ":");
3673
3674 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003675 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003676 if (!ret)
3677 ret = -EINVAL;
3678 if (ret < 0)
3679 return ret;
3680 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003681 }
3682
Steven Rostedtf6180772009-02-14 00:40:25 -05003683 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003684
3685 command = strsep(&next, ":");
3686
Steven Rostedtf6180772009-02-14 00:40:25 -05003687 mutex_lock(&ftrace_cmd_mutex);
3688 list_for_each_entry(p, &ftrace_commands, list) {
3689 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003690 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003691 goto out_unlock;
3692 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003693 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003694 out_unlock:
3695 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003696
Steven Rostedtf6180772009-02-14 00:40:25 -05003697 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003698}
3699
Ingo Molnare309b412008-05-12 21:20:51 +02003700static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003701ftrace_regex_write(struct file *file, const char __user *ubuf,
3702 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003703{
3704 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003705 struct trace_parser *parser;
3706 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003707
Li Zefan4ba79782009-09-22 13:52:20 +08003708 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003709 return 0;
3710
Steven Rostedt5072c592008-05-12 21:20:43 +02003711 if (file->f_mode & FMODE_READ) {
3712 struct seq_file *m = file->private_data;
3713 iter = m->private;
3714 } else
3715 iter = file->private_data;
3716
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003717 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003718 return -ENODEV;
3719
3720 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003721
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003722 parser = &iter->parser;
3723 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003724
Li Zefan4ba79782009-09-22 13:52:20 +08003725 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003726 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003727 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003728 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003729 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003730 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003731 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003732 }
3733
Steven Rostedt5072c592008-05-12 21:20:43 +02003734 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003735 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003736 return ret;
3737}
3738
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003739ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003740ftrace_filter_write(struct file *file, const char __user *ubuf,
3741 size_t cnt, loff_t *ppos)
3742{
3743 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3744}
3745
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003746ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003747ftrace_notrace_write(struct file *file, const char __user *ubuf,
3748 size_t cnt, loff_t *ppos)
3749{
3750 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3751}
3752
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003753static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003754ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3755{
3756 struct ftrace_func_entry *entry;
3757
3758 if (!ftrace_location(ip))
3759 return -EINVAL;
3760
3761 if (remove) {
3762 entry = ftrace_lookup_ip(hash, ip);
3763 if (!entry)
3764 return -ENOENT;
3765 free_hash_entry(hash, entry);
3766 return 0;
3767 }
3768
3769 return add_hash_entry(hash, ip);
3770}
3771
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003772static void ftrace_ops_update_code(struct ftrace_ops *ops,
3773 struct ftrace_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003774{
3775 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003776 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003777}
3778
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003779static int
3780ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3781 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003782{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003783 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003784 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003785 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003786 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003787
Steven Rostedt41c52c02008-05-22 11:46:33 -04003788 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003789 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003790
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003791 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003792
Steven Rostedtf45948e2011-05-02 12:29:25 -04003793 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003794 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003795 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003796 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003797
Wang Nanb972cc52014-07-15 08:40:20 +08003798 if (reset)
3799 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3800 else
3801 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3802
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003803 if (!hash) {
3804 ret = -ENOMEM;
3805 goto out_regex_unlock;
3806 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003807
Jiri Olsaac483c42012-01-02 10:04:14 +01003808 if (buf && !ftrace_match_records(hash, buf, len)) {
3809 ret = -EINVAL;
3810 goto out_regex_unlock;
3811 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003812 if (ip) {
3813 ret = ftrace_match_addr(hash, ip, remove);
3814 if (ret < 0)
3815 goto out_regex_unlock;
3816 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003817
3818 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003819 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003820 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003821 if (!ret) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003822 ftrace_ops_update_code(ops, old_hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003823 free_ftrace_hash_rcu(old_hash);
3824 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003825 mutex_unlock(&ftrace_lock);
3826
Jiri Olsaac483c42012-01-02 10:04:14 +01003827 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003828 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003829
3830 free_ftrace_hash(hash);
3831 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003832}
3833
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003834static int
3835ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3836 int reset, int enable)
3837{
3838 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3839}
3840
3841/**
3842 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3843 * @ops - the ops to set the filter with
3844 * @ip - the address to add to or remove from the filter.
3845 * @remove - non zero to remove the ip from the filter
3846 * @reset - non zero to reset all filters before applying this filter.
3847 *
3848 * Filters denote which functions should be enabled when tracing is enabled
3849 * If @ip is NULL, it failes to update filter.
3850 */
3851int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3852 int remove, int reset)
3853{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003854 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003855 return ftrace_set_addr(ops, ip, remove, reset, 1);
3856}
3857EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3858
3859static int
3860ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3861 int reset, int enable)
3862{
3863 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3864}
3865
Steven Rostedt77a2b372008-05-12 21:20:45 +02003866/**
3867 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003868 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003869 * @buf - the string that holds the function filter text.
3870 * @len - the length of the string.
3871 * @reset - non zero to reset all filters before applying this filter.
3872 *
3873 * Filters denote which functions should be enabled when tracing is enabled.
3874 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3875 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003876int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003877 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003878{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003879 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003880 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003881}
Steven Rostedt936e0742011-05-05 22:54:01 -04003882EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003883
Steven Rostedt41c52c02008-05-22 11:46:33 -04003884/**
3885 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003886 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003887 * @buf - the string that holds the function notrace text.
3888 * @len - the length of the string.
3889 * @reset - non zero to reset all filters before applying this filter.
3890 *
3891 * Notrace Filters denote which functions should not be enabled when tracing
3892 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3893 * for tracing.
3894 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003895int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003896 int len, int reset)
3897{
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, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003900}
3901EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3902/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003903 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003904 * @buf - the string that holds the function filter text.
3905 * @len - the length of the string.
3906 * @reset - non zero to reset all filters before applying this filter.
3907 *
3908 * Filters denote which functions should be enabled when tracing is enabled.
3909 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3910 */
3911void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3912{
3913 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3914}
3915EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3916
3917/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003918 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003919 * @buf - the string that holds the function notrace text.
3920 * @len - the length of the string.
3921 * @reset - non zero to reset all filters before applying this filter.
3922 *
3923 * Notrace Filters denote which functions should not be enabled when tracing
3924 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3925 * for tracing.
3926 */
3927void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003928{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003929 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003930}
Steven Rostedt936e0742011-05-05 22:54:01 -04003931EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003932
Steven Rostedt2af15d62009-05-28 13:37:24 -04003933/*
3934 * command line interface to allow users to set filters on boot up.
3935 */
3936#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3937static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3938static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3939
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003940/* Used by function selftest to not test if filter is set */
3941bool ftrace_filter_param __initdata;
3942
Steven Rostedt2af15d62009-05-28 13:37:24 -04003943static int __init set_ftrace_notrace(char *str)
3944{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003945 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003946 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003947 return 1;
3948}
3949__setup("ftrace_notrace=", set_ftrace_notrace);
3950
3951static int __init set_ftrace_filter(char *str)
3952{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003953 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003954 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003955 return 1;
3956}
3957__setup("ftrace_filter=", set_ftrace_filter);
3958
Stefan Assmann369bc182009-10-12 22:17:21 +02003959#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003960static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003961static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003962static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003963
Stefan Assmann369bc182009-10-12 22:17:21 +02003964static int __init set_graph_function(char *str)
3965{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003966 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003967 return 1;
3968}
3969__setup("ftrace_graph_filter=", set_graph_function);
3970
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003971static int __init set_graph_notrace_function(char *str)
3972{
3973 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3974 return 1;
3975}
3976__setup("ftrace_graph_notrace=", set_graph_notrace_function);
3977
3978static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02003979{
3980 int ret;
3981 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003982 unsigned long *table = ftrace_graph_funcs;
3983 int *count = &ftrace_graph_count;
3984
3985 if (!enable) {
3986 table = ftrace_graph_notrace_funcs;
3987 count = &ftrace_graph_notrace_count;
3988 }
Stefan Assmann369bc182009-10-12 22:17:21 +02003989
3990 while (buf) {
3991 func = strsep(&buf, ",");
3992 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003993 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003994 if (ret)
3995 printk(KERN_DEBUG "ftrace: function %s not "
3996 "traceable\n", func);
3997 }
3998}
3999#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4000
Steven Rostedt2a85a372011-12-19 21:57:44 -05004001void __init
4002ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004003{
4004 char *func;
4005
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004006 ftrace_ops_init(ops);
4007
Steven Rostedt2af15d62009-05-28 13:37:24 -04004008 while (buf) {
4009 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004010 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004011 }
4012}
4013
4014static void __init set_ftrace_early_filters(void)
4015{
4016 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004017 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004018 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004019 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004020#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4021 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004022 set_ftrace_early_graph(ftrace_graph_buf, 1);
4023 if (ftrace_graph_notrace_buf[0])
4024 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004025#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004026}
4027
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004028int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004029{
4030 struct seq_file *m = (struct seq_file *)file->private_data;
4031 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004032 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004033 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004034 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004035 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004036 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004037
Steven Rostedt5072c592008-05-12 21:20:43 +02004038 if (file->f_mode & FMODE_READ) {
4039 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004040 seq_release(inode, file);
4041 } else
4042 iter = file->private_data;
4043
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004044 parser = &iter->parser;
4045 if (trace_parser_loaded(parser)) {
4046 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004047 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004048 }
4049
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004050 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004051
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004052 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004053
Steven Rostedt058e2972011-04-29 22:35:33 -04004054 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004055 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4056
4057 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004058 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004059 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004060 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004061
Steven Rostedt058e2972011-04-29 22:35:33 -04004062 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004063 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004064 ret = ftrace_hash_move(iter->ops, filter_hash,
4065 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004066 if (!ret) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004067 ftrace_ops_update_code(iter->ops, old_hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004068 free_ftrace_hash_rcu(old_hash);
4069 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004070 mutex_unlock(&ftrace_lock);
4071 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004072
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004073 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004074 free_ftrace_hash(iter->hash);
4075 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004076
Steven Rostedt5072c592008-05-12 21:20:43 +02004077 return 0;
4078}
4079
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004080static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004081 .open = ftrace_avail_open,
4082 .read = seq_read,
4083 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004084 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004085};
4086
Steven Rostedt647bcd02011-05-03 14:39:21 -04004087static const struct file_operations ftrace_enabled_fops = {
4088 .open = ftrace_enabled_open,
4089 .read = seq_read,
4090 .llseek = seq_lseek,
4091 .release = seq_release_private,
4092};
4093
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004094static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004095 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004096 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004097 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004098 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004099 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004100};
4101
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004102static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004103 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004104 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004105 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004106 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004107 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004108};
4109
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004110#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4111
4112static DEFINE_MUTEX(graph_lock);
4113
4114int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004115int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004116unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004117unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004118
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004119struct ftrace_graph_data {
4120 unsigned long *table;
4121 size_t size;
4122 int *count;
4123 const struct seq_operations *seq_ops;
4124};
4125
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004126static void *
Li Zefan85951842009-06-24 09:54:00 +08004127__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004128{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004129 struct ftrace_graph_data *fgd = m->private;
4130
4131 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004132 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004133 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004134}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004135
Li Zefan85951842009-06-24 09:54:00 +08004136static void *
4137g_next(struct seq_file *m, void *v, loff_t *pos)
4138{
4139 (*pos)++;
4140 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004141}
4142
4143static void *g_start(struct seq_file *m, loff_t *pos)
4144{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004145 struct ftrace_graph_data *fgd = m->private;
4146
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004147 mutex_lock(&graph_lock);
4148
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004149 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004150 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004151 return (void *)1;
4152
Li Zefan85951842009-06-24 09:54:00 +08004153 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004154}
4155
4156static void g_stop(struct seq_file *m, void *p)
4157{
4158 mutex_unlock(&graph_lock);
4159}
4160
4161static int g_show(struct seq_file *m, void *v)
4162{
4163 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004164
4165 if (!ptr)
4166 return 0;
4167
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004168 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004169 struct ftrace_graph_data *fgd = m->private;
4170
4171 if (fgd->table == ftrace_graph_funcs)
4172 seq_printf(m, "#### all functions enabled ####\n");
4173 else
4174 seq_printf(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004175 return 0;
4176 }
4177
Steven Rostedtb375a112009-09-17 00:05:58 -04004178 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004179
4180 return 0;
4181}
4182
James Morris88e9d342009-09-22 16:43:43 -07004183static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004184 .start = g_start,
4185 .next = g_next,
4186 .stop = g_stop,
4187 .show = g_show,
4188};
4189
4190static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004191__ftrace_graph_open(struct inode *inode, struct file *file,
4192 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004193{
4194 int ret = 0;
4195
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004196 mutex_lock(&graph_lock);
4197 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004198 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004199 *fgd->count = 0;
4200 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004201 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004202 mutex_unlock(&graph_lock);
4203
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004204 if (file->f_mode & FMODE_READ) {
4205 ret = seq_open(file, fgd->seq_ops);
4206 if (!ret) {
4207 struct seq_file *m = file->private_data;
4208 m->private = fgd;
4209 }
4210 } else
4211 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004212
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004213 return ret;
4214}
4215
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004216static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004217ftrace_graph_open(struct inode *inode, struct file *file)
4218{
4219 struct ftrace_graph_data *fgd;
4220
4221 if (unlikely(ftrace_disabled))
4222 return -ENODEV;
4223
4224 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4225 if (fgd == NULL)
4226 return -ENOMEM;
4227
4228 fgd->table = ftrace_graph_funcs;
4229 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4230 fgd->count = &ftrace_graph_count;
4231 fgd->seq_ops = &ftrace_graph_seq_ops;
4232
4233 return __ftrace_graph_open(inode, file, fgd);
4234}
4235
4236static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004237ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4238{
4239 struct ftrace_graph_data *fgd;
4240
4241 if (unlikely(ftrace_disabled))
4242 return -ENODEV;
4243
4244 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4245 if (fgd == NULL)
4246 return -ENOMEM;
4247
4248 fgd->table = ftrace_graph_notrace_funcs;
4249 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4250 fgd->count = &ftrace_graph_notrace_count;
4251 fgd->seq_ops = &ftrace_graph_seq_ops;
4252
4253 return __ftrace_graph_open(inode, file, fgd);
4254}
4255
4256static int
Li Zefan87827112009-07-23 11:29:11 +08004257ftrace_graph_release(struct inode *inode, struct file *file)
4258{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004259 if (file->f_mode & FMODE_READ) {
4260 struct seq_file *m = file->private_data;
4261
4262 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004263 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004264 } else {
4265 kfree(file->private_data);
4266 }
4267
Li Zefan87827112009-07-23 11:29:11 +08004268 return 0;
4269}
4270
4271static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004272ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004273{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004274 struct dyn_ftrace *rec;
4275 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004276 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004277 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004278 int type, not;
4279 char *search;
4280 bool exists;
4281 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004282
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004283 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004284 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004285 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004286 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004287
4288 search_len = strlen(search);
4289
Steven Rostedt52baf112009-02-14 01:15:39 -05004290 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004291
4292 if (unlikely(ftrace_disabled)) {
4293 mutex_unlock(&ftrace_lock);
4294 return -ENODEV;
4295 }
4296
Steven Rostedt265c8312009-02-13 12:43:56 -05004297 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004298
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004299 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004300 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004301 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004302 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004303 if (array[i] == rec->ip) {
4304 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004305 break;
4306 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004307 }
4308
4309 if (!not) {
4310 fail = 0;
4311 if (!exists) {
4312 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004313 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004314 goto out;
4315 }
4316 } else {
4317 if (exists) {
4318 array[i] = array[--(*idx)];
4319 array[*idx] = 0;
4320 fail = 0;
4321 }
4322 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004323 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004324 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004325out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004326 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004327
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004328 if (fail)
4329 return -EINVAL;
4330
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004331 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004332}
4333
4334static ssize_t
4335ftrace_graph_write(struct file *file, const char __user *ubuf,
4336 size_t cnt, loff_t *ppos)
4337{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004338 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004339 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004340 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004341
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004342 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004343 return 0;
4344
Namhyung Kim6a101082013-10-14 17:24:25 +09004345 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4346 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004347
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004348 read = trace_get_user(&parser, ubuf, cnt, ppos);
4349
Li Zefan4ba79782009-09-22 13:52:20 +08004350 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004351 parser.buffer[parser.idx] = 0;
4352
Namhyung Kim6a101082013-10-14 17:24:25 +09004353 mutex_lock(&graph_lock);
4354
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004355 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004356 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4357 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004358
4359 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004360 }
4361
Namhyung Kim6a101082013-10-14 17:24:25 +09004362 if (!ret)
4363 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004364
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004365 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004366
4367 return ret;
4368}
4369
4370static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004371 .open = ftrace_graph_open,
4372 .read = seq_read,
4373 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004374 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004375 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004376};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004377
4378static const struct file_operations ftrace_graph_notrace_fops = {
4379 .open = ftrace_graph_notrace_open,
4380 .read = seq_read,
4381 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004382 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004383 .release = ftrace_graph_release,
4384};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004385#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4386
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004387void ftrace_create_filter_files(struct ftrace_ops *ops,
4388 struct dentry *parent)
4389{
4390
4391 trace_create_file("set_ftrace_filter", 0644, parent,
4392 ops, &ftrace_filter_fops);
4393
4394 trace_create_file("set_ftrace_notrace", 0644, parent,
4395 ops, &ftrace_notrace_fops);
4396}
4397
4398/*
4399 * The name "destroy_filter_files" is really a misnomer. Although
4400 * in the future, it may actualy delete the files, but this is
4401 * really intended to make sure the ops passed in are disabled
4402 * and that when this function returns, the caller is free to
4403 * free the ops.
4404 *
4405 * The "destroy" name is only to match the "create" name that this
4406 * should be paired with.
4407 */
4408void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4409{
4410 mutex_lock(&ftrace_lock);
4411 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4412 ftrace_shutdown(ops, 0);
4413 ops->flags |= FTRACE_OPS_FL_DELETED;
4414 mutex_unlock(&ftrace_lock);
4415}
4416
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004417static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004418{
Steven Rostedt5072c592008-05-12 21:20:43 +02004419
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004420 trace_create_file("available_filter_functions", 0444,
4421 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004422
Steven Rostedt647bcd02011-05-03 14:39:21 -04004423 trace_create_file("enabled_functions", 0444,
4424 d_tracer, NULL, &ftrace_enabled_fops);
4425
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004426 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004427
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004428#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004429 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004430 NULL,
4431 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004432 trace_create_file("set_graph_notrace", 0444, d_tracer,
4433 NULL,
4434 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004435#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4436
Steven Rostedt5072c592008-05-12 21:20:43 +02004437 return 0;
4438}
4439
Steven Rostedt9fd49322012-04-24 22:32:06 -04004440static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004441{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004442 const unsigned long *ipa = a;
4443 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004444
Steven Rostedt9fd49322012-04-24 22:32:06 -04004445 if (*ipa > *ipb)
4446 return 1;
4447 if (*ipa < *ipb)
4448 return -1;
4449 return 0;
4450}
4451
4452static void ftrace_swap_ips(void *a, void *b, int size)
4453{
4454 unsigned long *ipa = a;
4455 unsigned long *ipb = b;
4456 unsigned long t;
4457
4458 t = *ipa;
4459 *ipa = *ipb;
4460 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004461}
4462
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004463static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004464 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004465 unsigned long *end)
4466{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004467 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004468 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004469 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004470 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004471 unsigned long *p;
4472 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004473 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004474 int ret = -ENOMEM;
4475
4476 count = end - start;
4477
4478 if (!count)
4479 return 0;
4480
Steven Rostedt9fd49322012-04-24 22:32:06 -04004481 sort(start, count, sizeof(*start),
4482 ftrace_cmp_ips, ftrace_swap_ips);
4483
Steven Rostedt706c81f2012-04-24 23:45:26 -04004484 start_pg = ftrace_allocate_pages(count);
4485 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004486 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004487
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004488 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004489
Steven Rostedt32082302011-12-16 14:42:37 -05004490 /*
4491 * Core and each module needs their own pages, as
4492 * modules will free them when they are removed.
4493 * Force a new page to be allocated for modules.
4494 */
Steven Rostedta7900872011-12-16 16:23:44 -05004495 if (!mod) {
4496 WARN_ON(ftrace_pages || ftrace_pages_start);
4497 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004498 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004499 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004500 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004501 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004502
Steven Rostedta7900872011-12-16 16:23:44 -05004503 if (WARN_ON(ftrace_pages->next)) {
4504 /* Hmm, we have free pages? */
4505 while (ftrace_pages->next)
4506 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004507 }
Steven Rostedta7900872011-12-16 16:23:44 -05004508
Steven Rostedt706c81f2012-04-24 23:45:26 -04004509 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004510 }
4511
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004512 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004513 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004514 while (p < end) {
4515 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004516 /*
4517 * Some architecture linkers will pad between
4518 * the different mcount_loc sections of different
4519 * object files to satisfy alignments.
4520 * Skip any NULL pointers.
4521 */
4522 if (!addr)
4523 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004524
4525 if (pg->index == pg->size) {
4526 /* We should have allocated enough */
4527 if (WARN_ON(!pg->next))
4528 break;
4529 pg = pg->next;
4530 }
4531
4532 rec = &pg->records[pg->index++];
4533 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004534 }
4535
Steven Rostedt706c81f2012-04-24 23:45:26 -04004536 /* We should have used all pages */
4537 WARN_ON(pg->next);
4538
4539 /* Assign the last page to ftrace_pages */
4540 ftrace_pages = pg;
4541
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004542 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004543 * We only need to disable interrupts on start up
4544 * because we are modifying code that an interrupt
4545 * may execute, and the modification is not atomic.
4546 * But for modules, nothing runs the code we modify
4547 * until we are finished with it, and there's no
4548 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004549 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004550 if (!mod)
4551 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004552 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004553 if (!mod)
4554 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004555 ret = 0;
4556 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004557 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004558
Steven Rostedta7900872011-12-16 16:23:44 -05004559 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004560}
4561
Steven Rostedt93eb6772009-04-15 13:24:06 -04004562#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004563
4564#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4565
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004566void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004567{
4568 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004569 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004570 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004571 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004572
Steven Rostedt93eb6772009-04-15 13:24:06 -04004573 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004574
4575 if (ftrace_disabled)
4576 goto out_unlock;
4577
Steven Rostedt32082302011-12-16 14:42:37 -05004578 /*
4579 * Each module has its own ftrace_pages, remove
4580 * them from the list.
4581 */
4582 last_pg = &ftrace_pages_start;
4583 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4584 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004585 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004586 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004587 * As core pages are first, the first
4588 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004589 */
Steven Rostedt32082302011-12-16 14:42:37 -05004590 if (WARN_ON(pg == ftrace_pages_start))
4591 goto out_unlock;
4592
4593 /* Check if we are deleting the last page */
4594 if (pg == ftrace_pages)
4595 ftrace_pages = next_to_ftrace_page(last_pg);
4596
4597 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004598 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4599 free_pages((unsigned long)pg->records, order);
4600 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004601 } else
4602 last_pg = &pg->next;
4603 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004604 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004605 mutex_unlock(&ftrace_lock);
4606}
4607
4608static void ftrace_init_module(struct module *mod,
4609 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004610{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004611 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004612 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004613 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004614}
4615
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004616void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004617{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004618 ftrace_init_module(mod, mod->ftrace_callsites,
4619 mod->ftrace_callsites +
4620 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004621}
4622
4623static int ftrace_module_notify_exit(struct notifier_block *self,
4624 unsigned long val, void *data)
4625{
4626 struct module *mod = data;
4627
4628 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004629 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004630
4631 return 0;
4632}
4633#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004634static int ftrace_module_notify_exit(struct notifier_block *self,
4635 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004636{
4637 return 0;
4638}
4639#endif /* CONFIG_MODULES */
4640
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004641struct notifier_block ftrace_module_exit_nb = {
4642 .notifier_call = ftrace_module_notify_exit,
4643 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4644};
4645
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004646void __init ftrace_init(void)
4647{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004648 extern unsigned long __start_mcount_loc[];
4649 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004650 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004651 int ret;
4652
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004653 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004654 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004655 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004656 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004657 goto failed;
4658
4659 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004660 if (!count) {
4661 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004662 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004663 }
4664
4665 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4666 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004667
4668 last_ftrace_enabled = ftrace_enabled = 1;
4669
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004670 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004671 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004672 __stop_mcount_loc);
4673
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004674 ret = register_module_notifier(&ftrace_module_exit_nb);
4675 if (ret)
4676 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004677
Steven Rostedt2af15d62009-05-28 13:37:24 -04004678 set_ftrace_early_filters();
4679
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004680 return;
4681 failed:
4682 ftrace_disabled = 1;
4683}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004684
Steven Rostedt3d083392008-05-12 21:20:42 +02004685#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004686
Steven Rostedt2b499382011-05-03 22:49:52 -04004687static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004688 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004689 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004690};
4691
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004692static int __init ftrace_nodyn_init(void)
4693{
4694 ftrace_enabled = 1;
4695 return 0;
4696}
Steven Rostedt6f415672012-10-05 12:13:07 -04004697core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004698
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004699static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4700static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04004701static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004702/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004703# define ftrace_startup(ops, command) \
4704 ({ \
4705 int ___ret = __register_ftrace_function(ops); \
4706 if (!___ret) \
4707 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4708 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004709 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004710# define ftrace_shutdown(ops, command) \
4711 ({ \
4712 int ___ret = __unregister_ftrace_function(ops); \
4713 if (!___ret) \
4714 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4715 ___ret; \
4716 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004717
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004718# define ftrace_startup_sysctl() do { } while (0)
4719# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004720
4721static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004722ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004723{
4724 return 1;
4725}
4726
Steven Rostedt3d083392008-05-12 21:20:42 +02004727#endif /* CONFIG_DYNAMIC_FTRACE */
4728
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004729__init void ftrace_init_global_array_ops(struct trace_array *tr)
4730{
4731 tr->ops = &global_ops;
4732 tr->ops->private = tr;
4733}
4734
4735void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4736{
4737 /* If we filter on pids, update to use the pid function */
4738 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4739 if (WARN_ON(tr->ops->func != ftrace_stub))
4740 printk("ftrace ops had %pS for function\n",
4741 tr->ops->func);
4742 /* Only the top level instance does pid tracing */
4743 if (!list_empty(&ftrace_pids)) {
4744 set_ftrace_pid_function(func);
4745 func = ftrace_pid_func;
4746 }
4747 }
4748 tr->ops->func = func;
4749 tr->ops->private = tr;
4750}
4751
4752void ftrace_reset_array_ops(struct trace_array *tr)
4753{
4754 tr->ops->func = ftrace_stub;
4755}
4756
Steven Rostedtb8489142011-05-04 09:27:52 -04004757static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004758ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004759 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004760{
Jiri Olsae2484912012-02-15 15:51:48 +01004761 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4762 return;
4763
4764 /*
4765 * Some of the ops may be dynamically allocated,
4766 * they must be freed after a synchronize_sched().
4767 */
4768 preempt_disable_notrace();
4769 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004770
4771 /*
4772 * Control funcs (perf) uses RCU. Only trace if
4773 * RCU is currently active.
4774 */
4775 if (!rcu_is_watching())
4776 goto out;
4777
Steven Rostedt0a016402012-11-02 17:03:03 -04004778 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004779 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4780 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004781 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004782 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004783 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004784 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004785 trace_recursion_clear(TRACE_CONTROL_BIT);
4786 preempt_enable_notrace();
4787}
4788
4789static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004790 .func = ftrace_ops_control_func,
4791 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004792 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004793};
4794
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004795static inline void
4796__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004797 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004798{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004799 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004800 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004801
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004802 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4803 if (bit < 0)
4804 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004805
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004806 /*
4807 * Some of the ops may be dynamically allocated,
4808 * they must be freed after a synchronize_sched().
4809 */
4810 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004811 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004812 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004813 if (FTRACE_WARN_ON(!op->func)) {
4814 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004815 goto out;
4816 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004817 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004818 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004819 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004820out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004821 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004822 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004823}
4824
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004825/*
4826 * Some archs only support passing ip and parent_ip. Even though
4827 * the list function ignores the op parameter, we do not want any
4828 * C side effects, where a function is called without the caller
4829 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004830 * Archs are to support both the regs and ftrace_ops at the same time.
4831 * If they support ftrace_ops, it is assumed they support regs.
4832 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004833 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4834 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004835 * An architecture can pass partial regs with ftrace_ops and still
4836 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004837 */
4838#if ARCH_SUPPORTS_FTRACE_OPS
4839static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004840 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004841{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004842 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004843}
4844#else
4845static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4846{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004847 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004848}
4849#endif
4850
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04004851/*
4852 * If there's only one function registered but it does not support
4853 * recursion, this function will be called by the mcount trampoline.
4854 * This function will handle recursion protection.
4855 */
4856static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
4857 struct ftrace_ops *op, struct pt_regs *regs)
4858{
4859 int bit;
4860
4861 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4862 if (bit < 0)
4863 return;
4864
4865 op->func(ip, parent_ip, op, regs);
4866
4867 trace_clear_recursion(bit);
4868}
4869
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04004870/**
4871 * ftrace_ops_get_func - get the function a trampoline should call
4872 * @ops: the ops to get the function for
4873 *
4874 * Normally the mcount trampoline will call the ops->func, but there
4875 * are times that it should not. For example, if the ops does not
4876 * have its own recursion protection, then it should call the
4877 * ftrace_ops_recurs_func() instead.
4878 *
4879 * Returns the function that the trampoline should call for @ops.
4880 */
4881ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
4882{
4883 /*
4884 * If this is a dynamic ops or we force list func,
4885 * then it needs to call the list anyway.
4886 */
4887 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
4888 return ftrace_ops_list_func;
4889
4890 /*
4891 * If the func handles its own recursion, call it directly.
4892 * Otherwise call the recursion protected function that
4893 * will call the ftrace ops function.
4894 */
4895 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
4896 return ftrace_ops_recurs_func;
4897
4898 return ops->func;
4899}
4900
Steven Rostedte32d8952008-12-04 00:26:41 -05004901static void clear_ftrace_swapper(void)
4902{
4903 struct task_struct *p;
4904 int cpu;
4905
4906 get_online_cpus();
4907 for_each_online_cpu(cpu) {
4908 p = idle_task(cpu);
4909 clear_tsk_trace_trace(p);
4910 }
4911 put_online_cpus();
4912}
4913
4914static void set_ftrace_swapper(void)
4915{
4916 struct task_struct *p;
4917 int cpu;
4918
4919 get_online_cpus();
4920 for_each_online_cpu(cpu) {
4921 p = idle_task(cpu);
4922 set_tsk_trace_trace(p);
4923 }
4924 put_online_cpus();
4925}
4926
4927static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004928{
4929 struct task_struct *p;
4930
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004931 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004932 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004933 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004934 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004935 rcu_read_unlock();
4936
Steven Rostedte32d8952008-12-04 00:26:41 -05004937 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004938}
4939
Steven Rostedte32d8952008-12-04 00:26:41 -05004940static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004941{
4942 struct task_struct *p;
4943
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004944 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004945 do_each_pid_task(pid, PIDTYPE_PID, p) {
4946 set_tsk_trace_trace(p);
4947 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004948 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004949}
4950
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004951static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004952{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004953 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004954 clear_ftrace_swapper();
4955 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004956 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004957}
4958
4959static void set_ftrace_pid_task(struct pid *pid)
4960{
4961 if (pid == ftrace_swapper_pid)
4962 set_ftrace_swapper();
4963 else
4964 set_ftrace_pid(pid);
4965}
4966
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004967static int ftrace_pid_add(int p)
4968{
4969 struct pid *pid;
4970 struct ftrace_pid *fpid;
4971 int ret = -EINVAL;
4972
4973 mutex_lock(&ftrace_lock);
4974
4975 if (!p)
4976 pid = ftrace_swapper_pid;
4977 else
4978 pid = find_get_pid(p);
4979
4980 if (!pid)
4981 goto out;
4982
4983 ret = 0;
4984
4985 list_for_each_entry(fpid, &ftrace_pids, list)
4986 if (fpid->pid == pid)
4987 goto out_put;
4988
4989 ret = -ENOMEM;
4990
4991 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4992 if (!fpid)
4993 goto out_put;
4994
4995 list_add(&fpid->list, &ftrace_pids);
4996 fpid->pid = pid;
4997
4998 set_ftrace_pid_task(pid);
4999
5000 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005001
5002 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005003
5004 mutex_unlock(&ftrace_lock);
5005 return 0;
5006
5007out_put:
5008 if (pid != ftrace_swapper_pid)
5009 put_pid(pid);
5010
5011out:
5012 mutex_unlock(&ftrace_lock);
5013 return ret;
5014}
5015
5016static void ftrace_pid_reset(void)
5017{
5018 struct ftrace_pid *fpid, *safe;
5019
5020 mutex_lock(&ftrace_lock);
5021 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5022 struct pid *pid = fpid->pid;
5023
5024 clear_ftrace_pid_task(pid);
5025
5026 list_del(&fpid->list);
5027 kfree(fpid);
5028 }
5029
5030 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005031 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005032
5033 mutex_unlock(&ftrace_lock);
5034}
5035
5036static void *fpid_start(struct seq_file *m, loff_t *pos)
5037{
5038 mutex_lock(&ftrace_lock);
5039
5040 if (list_empty(&ftrace_pids) && (!*pos))
5041 return (void *) 1;
5042
5043 return seq_list_start(&ftrace_pids, *pos);
5044}
5045
5046static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5047{
5048 if (v == (void *)1)
5049 return NULL;
5050
5051 return seq_list_next(v, &ftrace_pids, pos);
5052}
5053
5054static void fpid_stop(struct seq_file *m, void *p)
5055{
5056 mutex_unlock(&ftrace_lock);
5057}
5058
5059static int fpid_show(struct seq_file *m, void *v)
5060{
5061 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5062
5063 if (v == (void *)1) {
5064 seq_printf(m, "no pid\n");
5065 return 0;
5066 }
5067
5068 if (fpid->pid == ftrace_swapper_pid)
5069 seq_printf(m, "swapper tasks\n");
5070 else
5071 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5072
5073 return 0;
5074}
5075
5076static const struct seq_operations ftrace_pid_sops = {
5077 .start = fpid_start,
5078 .next = fpid_next,
5079 .stop = fpid_stop,
5080 .show = fpid_show,
5081};
5082
5083static int
5084ftrace_pid_open(struct inode *inode, struct file *file)
5085{
5086 int ret = 0;
5087
5088 if ((file->f_mode & FMODE_WRITE) &&
5089 (file->f_flags & O_TRUNC))
5090 ftrace_pid_reset();
5091
5092 if (file->f_mode & FMODE_READ)
5093 ret = seq_open(file, &ftrace_pid_sops);
5094
5095 return ret;
5096}
5097
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005098static ssize_t
5099ftrace_pid_write(struct file *filp, const char __user *ubuf,
5100 size_t cnt, loff_t *ppos)
5101{
Ingo Molnar457dc922009-11-23 11:03:28 +01005102 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005103 long val;
5104 int ret;
5105
5106 if (cnt >= sizeof(buf))
5107 return -EINVAL;
5108
5109 if (copy_from_user(&buf, ubuf, cnt))
5110 return -EFAULT;
5111
5112 buf[cnt] = 0;
5113
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005114 /*
5115 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5116 * to clean the filter quietly.
5117 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005118 tmp = strstrip(buf);
5119 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005120 return 1;
5121
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005122 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005123 if (ret < 0)
5124 return ret;
5125
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005126 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005127
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005128 return ret ? ret : cnt;
5129}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005130
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005131static int
5132ftrace_pid_release(struct inode *inode, struct file *file)
5133{
5134 if (file->f_mode & FMODE_READ)
5135 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005136
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005137 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005138}
5139
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005140static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005141 .open = ftrace_pid_open,
5142 .write = ftrace_pid_write,
5143 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005144 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005145 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005146};
5147
5148static __init int ftrace_init_debugfs(void)
5149{
5150 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005151
5152 d_tracer = tracing_init_dentry();
5153 if (!d_tracer)
5154 return 0;
5155
5156 ftrace_init_dyn_debugfs(d_tracer);
5157
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005158 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5159 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005160
5161 ftrace_profile_debugfs(d_tracer);
5162
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005163 return 0;
5164}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005165fs_initcall(ftrace_init_debugfs);
5166
Steven Rostedt3d083392008-05-12 21:20:42 +02005167/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005168 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005169 *
5170 * This function should be used by panic code. It stops ftrace
5171 * but in a not so nice way. If you need to simply kill ftrace
5172 * from a non-atomic section, use ftrace_kill.
5173 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005174void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005175{
5176 ftrace_disabled = 1;
5177 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005178 clear_ftrace_function();
5179}
5180
5181/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005182 * Test if ftrace is dead or not.
5183 */
5184int ftrace_is_dead(void)
5185{
5186 return ftrace_disabled;
5187}
5188
5189/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005190 * register_ftrace_function - register a function for profiling
5191 * @ops - ops structure that holds the function for profiling.
5192 *
5193 * Register a function to be called by all functions in the
5194 * kernel.
5195 *
5196 * Note: @ops->func and all the functions it calls must be labeled
5197 * with "notrace", otherwise it will go into a
5198 * recursive loop.
5199 */
5200int register_ftrace_function(struct ftrace_ops *ops)
5201{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005202 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005203
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005204 ftrace_ops_init(ops);
5205
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005206 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005207
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005208 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005209
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005210 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005211
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005212 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005213}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005214EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005215
5216/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005217 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005218 * @ops - ops structure that holds the function to unregister
5219 *
5220 * Unregister a function that was added to be called by ftrace profiling.
5221 */
5222int unregister_ftrace_function(struct ftrace_ops *ops)
5223{
5224 int ret;
5225
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005226 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005227 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005228 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005229
5230 return ret;
5231}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005232EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005233
Ingo Molnare309b412008-05-12 21:20:51 +02005234int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005235ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005236 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005237 loff_t *ppos)
5238{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005239 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005240
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005241 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005242
Steven Rostedt45a4a232011-04-21 23:16:46 -04005243 if (unlikely(ftrace_disabled))
5244 goto out;
5245
5246 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005247
Li Zefana32c7762009-06-26 16:55:51 +08005248 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005249 goto out;
5250
Li Zefana32c7762009-06-26 16:55:51 +08005251 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005252
5253 if (ftrace_enabled) {
5254
5255 ftrace_startup_sysctl();
5256
5257 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005258 if (ftrace_ops_list != &ftrace_list_end)
5259 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005260
5261 } else {
5262 /* stopping ftrace calls (just send to ftrace_stub) */
5263 ftrace_trace_function = ftrace_stub;
5264
5265 ftrace_shutdown_sysctl();
5266 }
5267
5268 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005269 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005270 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005271}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005272
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005273#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005274
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005275static struct ftrace_ops graph_ops = {
5276 .func = ftrace_stub,
5277 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5278 FTRACE_OPS_FL_INITIALIZED |
5279 FTRACE_OPS_FL_STUB,
5280#ifdef FTRACE_GRAPH_TRAMP_ADDR
5281 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5282#endif
5283 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5284};
5285
Steven Rostedt597af812009-04-03 15:24:12 -04005286static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005287
Steven Rostedte49dc192008-12-02 23:50:05 -05005288int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5289{
5290 return 0;
5291}
5292
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005293/* The callbacks that hook a function */
5294trace_func_graph_ret_t ftrace_graph_return =
5295 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005296trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005297static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005298
5299/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5300static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5301{
5302 int i;
5303 int ret = 0;
5304 unsigned long flags;
5305 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5306 struct task_struct *g, *t;
5307
5308 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5309 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5310 * sizeof(struct ftrace_ret_stack),
5311 GFP_KERNEL);
5312 if (!ret_stack_list[i]) {
5313 start = 0;
5314 end = i;
5315 ret = -ENOMEM;
5316 goto free;
5317 }
5318 }
5319
5320 read_lock_irqsave(&tasklist_lock, flags);
5321 do_each_thread(g, t) {
5322 if (start == end) {
5323 ret = -EAGAIN;
5324 goto unlock;
5325 }
5326
5327 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005328 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005329 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005330 t->curr_ret_stack = -1;
5331 /* Make sure the tasks see the -1 first: */
5332 smp_wmb();
5333 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005334 }
5335 } while_each_thread(g, t);
5336
5337unlock:
5338 read_unlock_irqrestore(&tasklist_lock, flags);
5339free:
5340 for (i = start; i < end; i++)
5341 kfree(ret_stack_list[i]);
5342 return ret;
5343}
5344
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005345static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005346ftrace_graph_probe_sched_switch(void *ignore,
5347 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005348{
5349 unsigned long long timestamp;
5350 int index;
5351
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005352 /*
5353 * Does the user want to count the time a function was asleep.
5354 * If so, do not update the time stamps.
5355 */
5356 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5357 return;
5358
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005359 timestamp = trace_clock_local();
5360
5361 prev->ftrace_timestamp = timestamp;
5362
5363 /* only process tasks that we timestamped */
5364 if (!next->ftrace_timestamp)
5365 return;
5366
5367 /*
5368 * Update all the counters in next to make up for the
5369 * time next was sleeping.
5370 */
5371 timestamp -= next->ftrace_timestamp;
5372
5373 for (index = next->curr_ret_stack; index >= 0; index--)
5374 next->ret_stack[index].calltime += timestamp;
5375}
5376
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005377/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005378static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005379{
5380 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005381 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005382
5383 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5384 sizeof(struct ftrace_ret_stack *),
5385 GFP_KERNEL);
5386
5387 if (!ret_stack_list)
5388 return -ENOMEM;
5389
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005390 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005391 for_each_online_cpu(cpu) {
5392 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005393 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005394 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005395
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005396 do {
5397 ret = alloc_retstack_tasklist(ret_stack_list);
5398 } while (ret == -EAGAIN);
5399
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005400 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005401 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005402 if (ret)
5403 pr_info("ftrace_graph: Couldn't activate tracepoint"
5404 " probe to kernel_sched_switch\n");
5405 }
5406
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005407 kfree(ret_stack_list);
5408 return ret;
5409}
5410
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005411/*
5412 * Hibernation protection.
5413 * The state of the current task is too much unstable during
5414 * suspend/restore to disk. We want to protect against that.
5415 */
5416static int
5417ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5418 void *unused)
5419{
5420 switch (state) {
5421 case PM_HIBERNATION_PREPARE:
5422 pause_graph_tracing();
5423 break;
5424
5425 case PM_POST_HIBERNATION:
5426 unpause_graph_tracing();
5427 break;
5428 }
5429 return NOTIFY_DONE;
5430}
5431
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005432static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5433{
5434 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5435 return 0;
5436 return __ftrace_graph_entry(trace);
5437}
5438
5439/*
5440 * The function graph tracer should only trace the functions defined
5441 * by set_ftrace_filter and set_ftrace_notrace. If another function
5442 * tracer ops is registered, the graph tracer requires testing the
5443 * function against the global ops, and not just trace any function
5444 * that any ftrace_ops registered.
5445 */
5446static void update_function_graph_func(void)
5447{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005448 struct ftrace_ops *op;
5449 bool do_test = false;
5450
5451 /*
5452 * The graph and global ops share the same set of functions
5453 * to test. If any other ops is on the list, then
5454 * the graph tracing needs to test if its the function
5455 * it should call.
5456 */
5457 do_for_each_ftrace_op(op, ftrace_ops_list) {
5458 if (op != &global_ops && op != &graph_ops &&
5459 op != &ftrace_list_end) {
5460 do_test = true;
5461 /* in double loop, break out with goto */
5462 goto out;
5463 }
5464 } while_for_each_ftrace_op(op);
5465 out:
5466 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005467 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005468 else
5469 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005470}
5471
Mathias Krause8275f692014-03-30 15:31:50 +02005472static struct notifier_block ftrace_suspend_notifier = {
5473 .notifier_call = ftrace_suspend_notifier_call,
5474};
5475
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005476int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5477 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005478{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005479 int ret = 0;
5480
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005481 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005482
Steven Rostedt05ce5812009-03-24 00:18:31 -04005483 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005484 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005485 ret = -EBUSY;
5486 goto out;
5487 }
5488
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005489 register_pm_notifier(&ftrace_suspend_notifier);
5490
Steven Rostedt597af812009-04-03 15:24:12 -04005491 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005492 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005493 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005494 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005495 goto out;
5496 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005497
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005498 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005499
5500 /*
5501 * Update the indirect function to the entryfunc, and the
5502 * function that gets called to the entry_test first. Then
5503 * call the update fgraph entry function to determine if
5504 * the entryfunc should be called directly or not.
5505 */
5506 __ftrace_graph_entry = entryfunc;
5507 ftrace_graph_entry = ftrace_graph_entry_test;
5508 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005509
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005510 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005511
5512out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005513 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005514 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005515}
5516
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005517void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005518{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005519 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005520
Steven Rostedt597af812009-04-03 15:24:12 -04005521 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005522 goto out;
5523
Steven Rostedt597af812009-04-03 15:24:12 -04005524 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005525 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005526 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005527 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005528 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005529 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005530 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005531
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005532 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005533 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005534}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005535
Steven Rostedt868baf02011-02-10 21:26:13 -05005536static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5537
5538static void
5539graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5540{
5541 atomic_set(&t->tracing_graph_pause, 0);
5542 atomic_set(&t->trace_overrun, 0);
5543 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005544 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005545 smp_wmb();
5546 t->ret_stack = ret_stack;
5547}
5548
5549/*
5550 * Allocate a return stack for the idle task. May be the first
5551 * time through, or it may be done by CPU hotplug online.
5552 */
5553void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5554{
5555 t->curr_ret_stack = -1;
5556 /*
5557 * The idle task has no parent, it either has its own
5558 * stack or no stack at all.
5559 */
5560 if (t->ret_stack)
5561 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5562
5563 if (ftrace_graph_active) {
5564 struct ftrace_ret_stack *ret_stack;
5565
5566 ret_stack = per_cpu(idle_ret_stack, cpu);
5567 if (!ret_stack) {
5568 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5569 * sizeof(struct ftrace_ret_stack),
5570 GFP_KERNEL);
5571 if (!ret_stack)
5572 return;
5573 per_cpu(idle_ret_stack, cpu) = ret_stack;
5574 }
5575 graph_init_task(t, ret_stack);
5576 }
5577}
5578
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005579/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005580void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005581{
Steven Rostedt84047e32009-06-02 16:51:55 -04005582 /* Make sure we do not use the parent ret_stack */
5583 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005584 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005585
Steven Rostedt597af812009-04-03 15:24:12 -04005586 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005587 struct ftrace_ret_stack *ret_stack;
5588
5589 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005590 * sizeof(struct ftrace_ret_stack),
5591 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005592 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005593 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005594 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005595 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005596}
5597
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005598void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005599{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005600 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5601
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005602 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005603 /* NULL must become visible to IRQs before we free it: */
5604 barrier();
5605
5606 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005607}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005608#endif