blob: 8f61ef70a2976860a07ca4fa0f265397fedcf758 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -050065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
Jiri Olsae2484912012-02-15 15:51:48 +010066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
68#define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70#else
71#define INIT_REGEX_LOCK(opsname)
72#endif
73
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040074static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040076 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/* ftrace_enabled is a method to turn ftrace on or off */
80int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020081static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050083/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084int function_trace_stop __read_mostly;
85
86/* Current function tracing op */
87struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050088/* What to set function_trace_op to */
89static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050090
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040091/* List for set_ftrace_pid's pids. */
92LIST_HEAD(ftrace_pids);
93struct ftrace_pid {
94 struct list_head list;
95 struct pid *pid;
96};
97
Steven Rostedt4eebcc82008-05-12 21:20:48 +020098/*
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
101 */
102static int ftrace_disabled __read_mostly;
103
Steven Rostedt52baf112009-02-14 01:15:39 -0500104static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200105
Jiri Olsae2484912012-02-15 15:51:48 +0100106static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400107static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200108ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500109ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400110static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100111static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200112
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400113#if ARCH_SUPPORTS_FTRACE_OPS
114static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400115 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400116#else
117/* See comment below, where ftrace_ops_list_func is defined */
118static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
119#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
120#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400121
Steven Rostedt0a016402012-11-02 17:03:03 -0400122/*
123 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400124 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400125 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400126 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400127 * concurrent insertions into the ftrace_global_list.
128 *
129 * Silly Alpha and silly pointer-speculation compiler optimizations!
130 */
131#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400132 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400133 do
134
135/*
136 * Optimized for just a single item in the list (as that is the normal case).
137 */
138#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400139 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400140 unlikely((op) != &ftrace_list_end))
141
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900142static inline void ftrace_ops_init(struct ftrace_ops *ops)
143{
144#ifdef CONFIG_DYNAMIC_FTRACE
145 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
146 mutex_init(&ops->regex_lock);
147 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
148 }
149#endif
150}
151
Steven Rostedtea701f12012-07-20 13:08:05 -0400152/**
153 * ftrace_nr_registered_ops - return number of ops registered
154 *
155 * Returns the number of ftrace_ops registered and tracing functions
156 */
157int ftrace_nr_registered_ops(void)
158{
159 struct ftrace_ops *ops;
160 int cnt = 0;
161
162 mutex_lock(&ftrace_lock);
163
164 for (ops = ftrace_ops_list;
165 ops != &ftrace_list_end; ops = ops->next)
166 cnt++;
167
168 mutex_unlock(&ftrace_lock);
169
170 return cnt;
171}
172
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400173static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400174 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500175{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500176 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500177 return;
178
Steven Rostedta1e2e312011-08-09 12:50:46 -0400179 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500180}
181
182static void set_ftrace_pid_function(ftrace_func_t func)
183{
184 /* do not set ftrace_pid_function to itself! */
185 if (func != ftrace_pid_func)
186 ftrace_pid_function = func;
187}
188
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200189/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200190 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200192 * This NULLs the ftrace function and in essence stops
193 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200194 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200195void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196{
Steven Rostedt3d083392008-05-12 21:20:42 +0200197 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500198 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200199}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200200
Jiri Olsae2484912012-02-15 15:51:48 +0100201static void control_ops_disable_all(struct ftrace_ops *ops)
202{
203 int cpu;
204
205 for_each_possible_cpu(cpu)
206 *per_cpu_ptr(ops->disabled, cpu) = 1;
207}
208
209static int control_ops_alloc(struct ftrace_ops *ops)
210{
211 int __percpu *disabled;
212
213 disabled = alloc_percpu(int);
214 if (!disabled)
215 return -ENOMEM;
216
217 ops->disabled = disabled;
218 control_ops_disable_all(ops);
219 return 0;
220}
221
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500222static void ftrace_sync(struct work_struct *work)
223{
224 /*
225 * This function is just a stub to implement a hard force
226 * of synchronize_sched(). This requires synchronizing
227 * tasks even in userspace and idle.
228 *
229 * Yes, function tracing is rude.
230 */
231}
232
233static void ftrace_sync_ipi(void *data)
234{
235 /* Probably not needed, but do it anyway */
236 smp_rmb();
237}
238
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500239#ifdef CONFIG_FUNCTION_GRAPH_TRACER
240static void update_function_graph_func(void);
241#else
242static inline void update_function_graph_func(void) { }
243#endif
244
Steven Rostedt2b499382011-05-03 22:49:52 -0400245static void update_ftrace_function(void)
246{
247 ftrace_func_t func;
248
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400249 /*
250 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400251 * recursion safe and not dynamic and the arch supports passing ops,
252 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400253 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400254 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400255 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400256 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400257 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400258 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400259 /* Set the ftrace_ops that the arch callback uses */
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500260 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400261 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400262 } else {
263 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500264 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400265 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400266 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400267
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500268 /* If there's no change, then do nothing more here */
269 if (ftrace_trace_function == func)
270 return;
271
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500272 update_function_graph_func();
273
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500274 /*
275 * If we are using the list function, it doesn't care
276 * about the function_trace_ops.
277 */
278 if (func == ftrace_ops_list_func) {
279 ftrace_trace_function = func;
280 /*
281 * Don't even bother setting function_trace_ops,
282 * it would be racy to do so anyway.
283 */
284 return;
285 }
286
287#ifndef CONFIG_DYNAMIC_FTRACE
288 /*
289 * For static tracing, we need to be a bit more careful.
290 * The function change takes affect immediately. Thus,
291 * we need to coorditate the setting of the function_trace_ops
292 * with the setting of the ftrace_trace_function.
293 *
294 * Set the function to the list ops, which will call the
295 * function we want, albeit indirectly, but it handles the
296 * ftrace_ops and doesn't depend on function_trace_op.
297 */
298 ftrace_trace_function = ftrace_ops_list_func;
299 /*
300 * Make sure all CPUs see this. Yes this is slow, but static
301 * tracing is slow and nasty to have enabled.
302 */
303 schedule_on_each_cpu(ftrace_sync);
304 /* Now all cpus are using the list ops. */
305 function_trace_op = set_function_trace_op;
306 /* Make sure the function_trace_op is visible on all CPUs */
307 smp_wmb();
308 /* Nasty way to force a rmb on all cpus */
309 smp_call_function(ftrace_sync_ipi, NULL, 1);
310 /* OK, we are all set to update the ftrace_trace_function now! */
311#endif /* !CONFIG_DYNAMIC_FTRACE */
312
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400313 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400314}
315
Steven Rostedt2b499382011-05-03 22:49:52 -0400316static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200317{
Steven Rostedt2b499382011-05-03 22:49:52 -0400318 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200319 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400320 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200321 * CPU might be walking that list. We need to make sure
322 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400323 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200324 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400325 rcu_assign_pointer(*list, ops);
326}
Steven Rostedt3d083392008-05-12 21:20:42 +0200327
Steven Rostedt2b499382011-05-03 22:49:52 -0400328static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
329{
330 struct ftrace_ops **p;
331
332 /*
333 * If we are removing the last function, then simply point
334 * to the ftrace_stub.
335 */
336 if (*list == ops && ops->next == &ftrace_list_end) {
337 *list = &ftrace_list_end;
338 return 0;
339 }
340
341 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
342 if (*p == ops)
343 break;
344
345 if (*p != ops)
346 return -1;
347
348 *p = (*p)->next;
349 return 0;
350}
351
Jiri Olsae2484912012-02-15 15:51:48 +0100352static void add_ftrace_list_ops(struct ftrace_ops **list,
353 struct ftrace_ops *main_ops,
354 struct ftrace_ops *ops)
355{
356 int first = *list == &ftrace_list_end;
357 add_ftrace_ops(list, ops);
358 if (first)
359 add_ftrace_ops(&ftrace_ops_list, main_ops);
360}
361
362static int remove_ftrace_list_ops(struct ftrace_ops **list,
363 struct ftrace_ops *main_ops,
364 struct ftrace_ops *ops)
365{
366 int ret = remove_ftrace_ops(list, ops);
367 if (!ret && *list == &ftrace_list_end)
368 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
369 return ret;
370}
371
Steven Rostedt2b499382011-05-03 22:49:52 -0400372static int __register_ftrace_function(struct ftrace_ops *ops)
373{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500374 if (ops->flags & FTRACE_OPS_FL_DELETED)
375 return -EINVAL;
376
Steven Rostedtb8489142011-05-04 09:27:52 -0400377 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
378 return -EBUSY;
379
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900380#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400381 /*
382 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
383 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
384 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
385 */
386 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
387 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
388 return -EINVAL;
389
390 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
391 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
392#endif
393
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400394 if (!core_kernel_data((unsigned long)ops))
395 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
396
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500397 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100398 if (control_ops_alloc(ops))
399 return -ENOMEM;
400 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400401 } else
402 add_ftrace_ops(&ftrace_ops_list, ops);
403
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400404 if (ftrace_enabled)
405 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200406
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200407 return 0;
408}
409
Ingo Molnare309b412008-05-12 21:20:51 +0200410static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200411{
Steven Rostedt2b499382011-05-03 22:49:52 -0400412 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200413
Steven Rostedtb8489142011-05-04 09:27:52 -0400414 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
415 return -EBUSY;
416
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500417 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100418 ret = remove_ftrace_list_ops(&ftrace_control_list,
419 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400420 } else
421 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
422
Steven Rostedt2b499382011-05-03 22:49:52 -0400423 if (ret < 0)
424 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400425
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400426 if (ftrace_enabled)
427 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200428
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500429 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200430}
431
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500432static void ftrace_update_pid_func(void)
433{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400434 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500435 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900436 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500437
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400438 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500439}
440
Steven Rostedt493762f2009-03-23 17:12:36 -0400441#ifdef CONFIG_FUNCTION_PROFILER
442struct ftrace_profile {
443 struct hlist_node node;
444 unsigned long ip;
445 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400446#ifdef CONFIG_FUNCTION_GRAPH_TRACER
447 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400448 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400449#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400450};
451
452struct ftrace_profile_page {
453 struct ftrace_profile_page *next;
454 unsigned long index;
455 struct ftrace_profile records[];
456};
457
Steven Rostedtcafb1682009-03-24 20:50:39 -0400458struct ftrace_profile_stat {
459 atomic_t disabled;
460 struct hlist_head *hash;
461 struct ftrace_profile_page *pages;
462 struct ftrace_profile_page *start;
463 struct tracer_stat stat;
464};
465
Steven Rostedt493762f2009-03-23 17:12:36 -0400466#define PROFILE_RECORDS_SIZE \
467 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
468
469#define PROFILES_PER_PAGE \
470 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
471
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400472static int ftrace_profile_enabled __read_mostly;
473
474/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400475static DEFINE_MUTEX(ftrace_profile_lock);
476
Steven Rostedtcafb1682009-03-24 20:50:39 -0400477static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400478
Namhyung Kim20079eb2013-04-10 08:55:50 +0900479#define FTRACE_PROFILE_HASH_BITS 10
480#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400481
Steven Rostedt493762f2009-03-23 17:12:36 -0400482static void *
483function_stat_next(void *v, int idx)
484{
485 struct ftrace_profile *rec = v;
486 struct ftrace_profile_page *pg;
487
488 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
489
490 again:
Li Zefan0296e422009-06-26 11:15:37 +0800491 if (idx != 0)
492 rec++;
493
Steven Rostedt493762f2009-03-23 17:12:36 -0400494 if ((void *)rec >= (void *)&pg->records[pg->index]) {
495 pg = pg->next;
496 if (!pg)
497 return NULL;
498 rec = &pg->records[0];
499 if (!rec->counter)
500 goto again;
501 }
502
503 return rec;
504}
505
506static void *function_stat_start(struct tracer_stat *trace)
507{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400508 struct ftrace_profile_stat *stat =
509 container_of(trace, struct ftrace_profile_stat, stat);
510
511 if (!stat || !stat->start)
512 return NULL;
513
514 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400515}
516
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400517#ifdef CONFIG_FUNCTION_GRAPH_TRACER
518/* function graph compares on total time */
519static int function_stat_cmp(void *p1, void *p2)
520{
521 struct ftrace_profile *a = p1;
522 struct ftrace_profile *b = p2;
523
524 if (a->time < b->time)
525 return -1;
526 if (a->time > b->time)
527 return 1;
528 else
529 return 0;
530}
531#else
532/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400533static int function_stat_cmp(void *p1, void *p2)
534{
535 struct ftrace_profile *a = p1;
536 struct ftrace_profile *b = p2;
537
538 if (a->counter < b->counter)
539 return -1;
540 if (a->counter > b->counter)
541 return 1;
542 else
543 return 0;
544}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400545#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400546
547static int function_stat_headers(struct seq_file *m)
548{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400549#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400550 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400551 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400552 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400553 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400554#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400555 seq_printf(m, " Function Hit\n"
556 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400557#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400558 return 0;
559}
560
561static int function_stat_show(struct seq_file *m, void *v)
562{
563 struct ftrace_profile *rec = v;
564 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800565 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400566#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400567 static struct trace_seq s;
568 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400569 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400570#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800571 mutex_lock(&ftrace_profile_lock);
572
573 /* we raced with function_profile_reset() */
574 if (unlikely(rec->counter == 0)) {
575 ret = -EBUSY;
576 goto out;
577 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400578
579 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400580 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400581
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400582#ifdef CONFIG_FUNCTION_GRAPH_TRACER
583 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400584 avg = rec->time;
585 do_div(avg, rec->counter);
586
Chase Douglase330b3b2010-04-26 14:02:05 -0400587 /* Sample standard deviation (s^2) */
588 if (rec->counter <= 1)
589 stddev = 0;
590 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200591 /*
592 * Apply Welford's method:
593 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
594 */
595 stddev = rec->counter * rec->time_squared -
596 rec->time * rec->time;
597
Chase Douglase330b3b2010-04-26 14:02:05 -0400598 /*
599 * Divide only 1000 for ns^2 -> us^2 conversion.
600 * trace_print_graph_duration will divide 1000 again.
601 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200602 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400603 }
604
Steven Rostedt34886c82009-03-25 21:00:47 -0400605 trace_seq_init(&s);
606 trace_print_graph_duration(rec->time, &s);
607 trace_seq_puts(&s, " ");
608 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400609 trace_seq_puts(&s, " ");
610 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400611 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400612#endif
613 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800614out:
615 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400616
Li Zefan3aaba202010-08-23 16:50:12 +0800617 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400618}
619
Steven Rostedtcafb1682009-03-24 20:50:39 -0400620static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400621{
622 struct ftrace_profile_page *pg;
623
Steven Rostedtcafb1682009-03-24 20:50:39 -0400624 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400625
626 while (pg) {
627 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
628 pg->index = 0;
629 pg = pg->next;
630 }
631
Steven Rostedtcafb1682009-03-24 20:50:39 -0400632 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400633 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
634}
635
Steven Rostedtcafb1682009-03-24 20:50:39 -0400636int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400637{
638 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400639 int functions;
640 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400641 int i;
642
643 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400644 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400645 return 0;
646
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
648 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400649 return -ENOMEM;
650
Steven Rostedt318e0a72009-03-25 20:06:34 -0400651#ifdef CONFIG_DYNAMIC_FTRACE
652 functions = ftrace_update_tot_cnt;
653#else
654 /*
655 * We do not know the number of functions that exist because
656 * dynamic tracing is what counts them. With past experience
657 * we have around 20K functions. That should be more than enough.
658 * It is highly unlikely we will execute every function in
659 * the kernel.
660 */
661 functions = 20000;
662#endif
663
Steven Rostedtcafb1682009-03-24 20:50:39 -0400664 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400665
Steven Rostedt318e0a72009-03-25 20:06:34 -0400666 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
667
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900668 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400669 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400670 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400671 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400672 pg = pg->next;
673 }
674
675 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400676
677 out_free:
678 pg = stat->start;
679 while (pg) {
680 unsigned long tmp = (unsigned long)pg;
681
682 pg = pg->next;
683 free_page(tmp);
684 }
685
Steven Rostedt318e0a72009-03-25 20:06:34 -0400686 stat->pages = NULL;
687 stat->start = NULL;
688
689 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400690}
691
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400693{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400694 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400695 int size;
696
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 stat = &per_cpu(ftrace_profile_stats, cpu);
698
699 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400700 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400702 return 0;
703 }
704
705 /*
706 * We are profiling all functions, but usually only a few thousand
707 * functions are hit. We'll make a hash of 1024 items.
708 */
709 size = FTRACE_PROFILE_HASH_SIZE;
710
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400712
Steven Rostedtcafb1682009-03-24 20:50:39 -0400713 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400714 return -ENOMEM;
715
Steven Rostedt318e0a72009-03-25 20:06:34 -0400716 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400717 if (ftrace_profile_pages_init(stat) < 0) {
718 kfree(stat->hash);
719 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400720 return -ENOMEM;
721 }
722
723 return 0;
724}
725
Steven Rostedtcafb1682009-03-24 20:50:39 -0400726static int ftrace_profile_init(void)
727{
728 int cpu;
729 int ret = 0;
730
Miao Xiec4602c12013-12-16 15:20:01 +0800731 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400732 ret = ftrace_profile_init_cpu(cpu);
733 if (ret)
734 break;
735 }
736
737 return ret;
738}
739
Steven Rostedt493762f2009-03-23 17:12:36 -0400740/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741static struct ftrace_profile *
742ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400743{
744 struct ftrace_profile *rec;
745 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400746 unsigned long key;
747
Namhyung Kim20079eb2013-04-10 08:55:50 +0900748 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400749 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400750
751 if (hlist_empty(hhd))
752 return NULL;
753
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400754 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 if (rec->ip == ip)
756 return rec;
757 }
758
759 return NULL;
760}
761
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762static void ftrace_add_profile(struct ftrace_profile_stat *stat,
763 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400764{
765 unsigned long key;
766
Namhyung Kim20079eb2013-04-10 08:55:50 +0900767 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400768 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400769}
770
Steven Rostedt318e0a72009-03-25 20:06:34 -0400771/*
772 * The memory is already allocated, this simply finds a new record to use.
773 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400774static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400775ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400776{
777 struct ftrace_profile *rec = NULL;
778
Steven Rostedt318e0a72009-03-25 20:06:34 -0400779 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400780 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400781 goto out;
782
Steven Rostedt493762f2009-03-23 17:12:36 -0400783 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400784 * Try to find the function again since an NMI
785 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400786 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400787 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400788 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400789 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400790
Steven Rostedtcafb1682009-03-24 20:50:39 -0400791 if (stat->pages->index == PROFILES_PER_PAGE) {
792 if (!stat->pages->next)
793 goto out;
794 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400795 }
796
Steven Rostedtcafb1682009-03-24 20:50:39 -0400797 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400798 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400799 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400800
Steven Rostedt493762f2009-03-23 17:12:36 -0400801 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400803
804 return rec;
805}
806
Steven Rostedt493762f2009-03-23 17:12:36 -0400807static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400808function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400809 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400810{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400812 struct ftrace_profile *rec;
813 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400814
815 if (!ftrace_profile_enabled)
816 return;
817
Steven Rostedt493762f2009-03-23 17:12:36 -0400818 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819
820 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400821 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 goto out;
823
824 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400825 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400826 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400827 if (!rec)
828 goto out;
829 }
830
831 rec->counter++;
832 out:
833 local_irq_restore(flags);
834}
835
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400836#ifdef CONFIG_FUNCTION_GRAPH_TRACER
837static int profile_graph_entry(struct ftrace_graph_ent *trace)
838{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400839 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400840 return 1;
841}
842
843static void profile_graph_return(struct ftrace_graph_ret *trace)
844{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400845 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400846 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400847 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400849
850 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400851 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400852 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400853 goto out;
854
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400855 /* If the calltime was zero'd ignore it */
856 if (!trace->calltime)
857 goto out;
858
Steven Rostedta2a16d62009-03-24 23:17:58 -0400859 calltime = trace->rettime - trace->calltime;
860
861 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
862 int index;
863
864 index = trace->depth;
865
866 /* Append this call time to the parent time to subtract */
867 if (index)
868 current->ret_stack[index - 1].subtime += calltime;
869
870 if (current->ret_stack[index].subtime < calltime)
871 calltime -= current->ret_stack[index].subtime;
872 else
873 calltime = 0;
874 }
875
Steven Rostedtcafb1682009-03-24 20:50:39 -0400876 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400877 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400878 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400879 rec->time_squared += calltime * calltime;
880 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400881
Steven Rostedtcafb1682009-03-24 20:50:39 -0400882 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400883 local_irq_restore(flags);
884}
885
886static int register_ftrace_profiler(void)
887{
888 return register_ftrace_graph(&profile_graph_return,
889 &profile_graph_entry);
890}
891
892static void unregister_ftrace_profiler(void)
893{
894 unregister_ftrace_graph();
895}
896#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100897static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400898 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900899 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
900 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400901};
902
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400903static int register_ftrace_profiler(void)
904{
905 return register_ftrace_function(&ftrace_profile_ops);
906}
907
908static void unregister_ftrace_profiler(void)
909{
910 unregister_ftrace_function(&ftrace_profile_ops);
911}
912#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
913
Steven Rostedt493762f2009-03-23 17:12:36 -0400914static ssize_t
915ftrace_profile_write(struct file *filp, const char __user *ubuf,
916 size_t cnt, loff_t *ppos)
917{
918 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400919 int ret;
920
Peter Huewe22fe9b52011-06-07 21:58:27 +0200921 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
922 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400923 return ret;
924
925 val = !!val;
926
927 mutex_lock(&ftrace_profile_lock);
928 if (ftrace_profile_enabled ^ val) {
929 if (val) {
930 ret = ftrace_profile_init();
931 if (ret < 0) {
932 cnt = ret;
933 goto out;
934 }
935
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400936 ret = register_ftrace_profiler();
937 if (ret < 0) {
938 cnt = ret;
939 goto out;
940 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400941 ftrace_profile_enabled = 1;
942 } else {
943 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400944 /*
945 * unregister_ftrace_profiler calls stop_machine
946 * so this acts like an synchronize_sched.
947 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400948 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400949 }
950 }
951 out:
952 mutex_unlock(&ftrace_profile_lock);
953
Jiri Olsacf8517c2009-10-23 19:36:16 -0400954 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400955
956 return cnt;
957}
958
959static ssize_t
960ftrace_profile_read(struct file *filp, char __user *ubuf,
961 size_t cnt, loff_t *ppos)
962{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400963 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400964 int r;
965
966 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
967 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
968}
969
970static const struct file_operations ftrace_profile_fops = {
971 .open = tracing_open_generic,
972 .read = ftrace_profile_read,
973 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200974 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400975};
976
Steven Rostedtcafb1682009-03-24 20:50:39 -0400977/* used to initialize the real stat files */
978static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400979 .name = "functions",
980 .stat_start = function_stat_start,
981 .stat_next = function_stat_next,
982 .stat_cmp = function_stat_cmp,
983 .stat_headers = function_stat_headers,
984 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400985};
986
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400987static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400988{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400989 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400990 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400991 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400992 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400993 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400994
Steven Rostedtcafb1682009-03-24 20:50:39 -0400995 for_each_possible_cpu(cpu) {
996 stat = &per_cpu(ftrace_profile_stats, cpu);
997
998 /* allocate enough for function name + cpu number */
999 name = kmalloc(32, GFP_KERNEL);
1000 if (!name) {
1001 /*
1002 * The files created are permanent, if something happens
1003 * we still do not free memory.
1004 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001005 WARN(1,
1006 "Could not allocate stat file for cpu %d\n",
1007 cpu);
1008 return;
1009 }
1010 stat->stat = function_stats;
1011 snprintf(name, 32, "function%d", cpu);
1012 stat->stat.name = name;
1013 ret = register_stat_tracer(&stat->stat);
1014 if (ret) {
1015 WARN(1,
1016 "Could not register function stat for cpu %d\n",
1017 cpu);
1018 kfree(name);
1019 return;
1020 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001021 }
1022
1023 entry = debugfs_create_file("function_profile_enabled", 0644,
1024 d_tracer, NULL, &ftrace_profile_fops);
1025 if (!entry)
1026 pr_warning("Could not create debugfs "
1027 "'function_profile_enabled' entry\n");
1028}
1029
1030#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001031static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001032{
1033}
1034#endif /* CONFIG_FUNCTION_PROFILER */
1035
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001036static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1037
Steven Rostedt3d083392008-05-12 21:20:42 +02001038#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001039
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001040#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001041# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001042#endif
1043
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001044static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1045
Steven Rostedtb6887d72009-02-17 12:32:04 -05001046struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001047 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001048 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001049 unsigned long flags;
1050 unsigned long ip;
1051 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001052 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001053};
1054
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001055struct ftrace_func_entry {
1056 struct hlist_node hlist;
1057 unsigned long ip;
1058};
1059
1060struct ftrace_hash {
1061 unsigned long size_bits;
1062 struct hlist_head *buckets;
1063 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001064 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001065};
1066
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001067/*
1068 * We make these constant because no one should touch them,
1069 * but they are used as the default "empty hash", to avoid allocating
1070 * it all the time. These are in a read only section such that if
1071 * anyone does try to modify it, it will cause an exception.
1072 */
1073static const struct hlist_head empty_buckets[1];
1074static const struct ftrace_hash empty_hash = {
1075 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001076};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001077#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001078
Steven Rostedt2b499382011-05-03 22:49:52 -04001079static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001080 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001081 .notrace_hash = EMPTY_HASH,
1082 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001083 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1084 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001085};
1086
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001087struct ftrace_page {
1088 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001089 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001090 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001091 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001092};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001093
Steven Rostedta7900872011-12-16 16:23:44 -05001094#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1095#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001096
1097/* estimate from running different kernels */
1098#define NR_TO_INIT 10000
1099
1100static struct ftrace_page *ftrace_pages_start;
1101static struct ftrace_page *ftrace_pages;
1102
Steven Rostedt06a51d92011-12-19 19:07:36 -05001103static bool ftrace_hash_empty(struct ftrace_hash *hash)
1104{
1105 return !hash || !hash->count;
1106}
1107
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001108static struct ftrace_func_entry *
1109ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1110{
1111 unsigned long key;
1112 struct ftrace_func_entry *entry;
1113 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001114
Steven Rostedt06a51d92011-12-19 19:07:36 -05001115 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001116 return NULL;
1117
1118 if (hash->size_bits > 0)
1119 key = hash_long(ip, hash->size_bits);
1120 else
1121 key = 0;
1122
1123 hhd = &hash->buckets[key];
1124
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001125 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001126 if (entry->ip == ip)
1127 return entry;
1128 }
1129 return NULL;
1130}
1131
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001132static void __add_hash_entry(struct ftrace_hash *hash,
1133 struct ftrace_func_entry *entry)
1134{
1135 struct hlist_head *hhd;
1136 unsigned long key;
1137
1138 if (hash->size_bits)
1139 key = hash_long(entry->ip, hash->size_bits);
1140 else
1141 key = 0;
1142
1143 hhd = &hash->buckets[key];
1144 hlist_add_head(&entry->hlist, hhd);
1145 hash->count++;
1146}
1147
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001148static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1149{
1150 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001151
1152 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1153 if (!entry)
1154 return -ENOMEM;
1155
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001156 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001157 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001158
1159 return 0;
1160}
1161
1162static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001163free_hash_entry(struct ftrace_hash *hash,
1164 struct ftrace_func_entry *entry)
1165{
1166 hlist_del(&entry->hlist);
1167 kfree(entry);
1168 hash->count--;
1169}
1170
1171static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001172remove_hash_entry(struct ftrace_hash *hash,
1173 struct ftrace_func_entry *entry)
1174{
1175 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001176 hash->count--;
1177}
1178
1179static void ftrace_hash_clear(struct ftrace_hash *hash)
1180{
1181 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001182 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001183 struct ftrace_func_entry *entry;
1184 int size = 1 << hash->size_bits;
1185 int i;
1186
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001187 if (!hash->count)
1188 return;
1189
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001190 for (i = 0; i < size; i++) {
1191 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001192 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001193 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001194 }
1195 FTRACE_WARN_ON(hash->count);
1196}
1197
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001198static void free_ftrace_hash(struct ftrace_hash *hash)
1199{
1200 if (!hash || hash == EMPTY_HASH)
1201 return;
1202 ftrace_hash_clear(hash);
1203 kfree(hash->buckets);
1204 kfree(hash);
1205}
1206
Steven Rostedt07fd5512011-05-05 18:03:47 -04001207static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1208{
1209 struct ftrace_hash *hash;
1210
1211 hash = container_of(rcu, struct ftrace_hash, rcu);
1212 free_ftrace_hash(hash);
1213}
1214
1215static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1216{
1217 if (!hash || hash == EMPTY_HASH)
1218 return;
1219 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1220}
1221
Jiri Olsa5500fa52012-02-15 15:51:54 +01001222void ftrace_free_filter(struct ftrace_ops *ops)
1223{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001224 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001225 free_ftrace_hash(ops->filter_hash);
1226 free_ftrace_hash(ops->notrace_hash);
1227}
1228
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001229static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1230{
1231 struct ftrace_hash *hash;
1232 int size;
1233
1234 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1235 if (!hash)
1236 return NULL;
1237
1238 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001239 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001240
1241 if (!hash->buckets) {
1242 kfree(hash);
1243 return NULL;
1244 }
1245
1246 hash->size_bits = size_bits;
1247
1248 return hash;
1249}
1250
1251static struct ftrace_hash *
1252alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1253{
1254 struct ftrace_func_entry *entry;
1255 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001256 int size;
1257 int ret;
1258 int i;
1259
1260 new_hash = alloc_ftrace_hash(size_bits);
1261 if (!new_hash)
1262 return NULL;
1263
1264 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001265 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001266 return new_hash;
1267
1268 size = 1 << hash->size_bits;
1269 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001270 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001271 ret = add_hash_entry(new_hash, entry->ip);
1272 if (ret < 0)
1273 goto free_hash;
1274 }
1275 }
1276
1277 FTRACE_WARN_ON(new_hash->count != hash->count);
1278
1279 return new_hash;
1280
1281 free_hash:
1282 free_ftrace_hash(new_hash);
1283 return NULL;
1284}
1285
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001286static void
1287ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1288static void
1289ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1290
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001291static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001292ftrace_hash_move(struct ftrace_ops *ops, int enable,
1293 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001294{
1295 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001296 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001297 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001298 struct ftrace_hash *old_hash;
1299 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001300 int size = src->count;
1301 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001302 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001303 int i;
1304
1305 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001306 * Remove the current set, update the hash and add
1307 * them back.
1308 */
1309 ftrace_hash_rec_disable(ops, enable);
1310
1311 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001312 * If the new source is empty, just free dst and assign it
1313 * the empty_hash.
1314 */
1315 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001316 free_ftrace_hash_rcu(*dst);
1317 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001318 /* still need to update the function records */
1319 ret = 0;
1320 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001321 }
1322
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001323 /*
1324 * Make the hash size about 1/2 the # found
1325 */
1326 for (size /= 2; size; size >>= 1)
1327 bits++;
1328
1329 /* Don't allocate too much */
1330 if (bits > FTRACE_HASH_MAX_BITS)
1331 bits = FTRACE_HASH_MAX_BITS;
1332
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001333 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001334 new_hash = alloc_ftrace_hash(bits);
1335 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001336 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337
1338 size = 1 << src->size_bits;
1339 for (i = 0; i < size; i++) {
1340 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001341 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001343 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001344 }
1345 }
1346
Steven Rostedt07fd5512011-05-05 18:03:47 -04001347 old_hash = *dst;
1348 rcu_assign_pointer(*dst, new_hash);
1349 free_ftrace_hash_rcu(old_hash);
1350
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001351 ret = 0;
1352 out:
1353 /*
1354 * Enable regardless of ret:
1355 * On success, we enable the new hash.
1356 * On failure, we re-enable the original hash.
1357 */
1358 ftrace_hash_rec_enable(ops, enable);
1359
1360 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001361}
1362
Steven Rostedt265c8312009-02-13 12:43:56 -05001363/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001364 * Test the hashes for this ops to see if we want to call
1365 * the ops->func or not.
1366 *
1367 * It's a match if the ip is in the ops->filter_hash or
1368 * the filter_hash does not exist or is empty,
1369 * AND
1370 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001371 *
1372 * This needs to be called with preemption disabled as
1373 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001374 */
1375static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001376ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001377{
1378 struct ftrace_hash *filter_hash;
1379 struct ftrace_hash *notrace_hash;
1380 int ret;
1381
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001382#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1383 /*
1384 * There's a small race when adding ops that the ftrace handler
1385 * that wants regs, may be called without them. We can not
1386 * allow that handler to be called if regs is NULL.
1387 */
1388 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1389 return 0;
1390#endif
1391
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001392 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1393 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001394
Steven Rostedt06a51d92011-12-19 19:07:36 -05001395 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001396 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001397 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001398 !ftrace_lookup_ip(notrace_hash, ip)))
1399 ret = 1;
1400 else
1401 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001402
1403 return ret;
1404}
1405
1406/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001407 * This is a double for. Do not use 'break' to break out of the loop,
1408 * you must use a goto.
1409 */
1410#define do_for_each_ftrace_rec(pg, rec) \
1411 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1412 int _____i; \
1413 for (_____i = 0; _____i < pg->index; _____i++) { \
1414 rec = &pg->records[_____i];
1415
1416#define while_for_each_ftrace_rec() \
1417 } \
1418 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301419
Steven Rostedt5855fea2011-12-16 19:27:42 -05001420
1421static int ftrace_cmp_recs(const void *a, const void *b)
1422{
Steven Rostedta650e022012-04-25 13:48:13 -04001423 const struct dyn_ftrace *key = a;
1424 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001425
Steven Rostedta650e022012-04-25 13:48:13 -04001426 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001427 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001428 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1429 return 1;
1430 return 0;
1431}
1432
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001433static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001434{
1435 struct ftrace_page *pg;
1436 struct dyn_ftrace *rec;
1437 struct dyn_ftrace key;
1438
1439 key.ip = start;
1440 key.flags = end; /* overload flags, as it is unsigned long */
1441
1442 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1443 if (end < pg->records[0].ip ||
1444 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1445 continue;
1446 rec = bsearch(&key, pg->records, pg->index,
1447 sizeof(struct dyn_ftrace),
1448 ftrace_cmp_recs);
1449 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001450 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001451 }
1452
Steven Rostedt5855fea2011-12-16 19:27:42 -05001453 return 0;
1454}
1455
Steven Rostedtc88fd862011-08-16 09:53:39 -04001456/**
1457 * ftrace_location - return true if the ip giving is a traced location
1458 * @ip: the instruction pointer to check
1459 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001460 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001461 * That is, the instruction that is either a NOP or call to
1462 * the function tracer. It checks the ftrace internal tables to
1463 * determine if the address belongs or not.
1464 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001465unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001466{
Steven Rostedta650e022012-04-25 13:48:13 -04001467 return ftrace_location_range(ip, ip);
1468}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001469
Steven Rostedta650e022012-04-25 13:48:13 -04001470/**
1471 * ftrace_text_reserved - return true if range contains an ftrace location
1472 * @start: start of range to search
1473 * @end: end of range to search (inclusive). @end points to the last byte to check.
1474 *
1475 * Returns 1 if @start and @end contains a ftrace location.
1476 * That is, the instruction that is either a NOP or call to
1477 * the function tracer. It checks the ftrace internal tables to
1478 * determine if the address belongs or not.
1479 */
Sasha Levind88471c2013-01-09 18:09:20 -05001480int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001481{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001482 unsigned long ret;
1483
1484 ret = ftrace_location_range((unsigned long)start,
1485 (unsigned long)end);
1486
1487 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001488}
1489
Steven Rostedted926f92011-05-03 13:25:24 -04001490static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1491 int filter_hash,
1492 bool inc)
1493{
1494 struct ftrace_hash *hash;
1495 struct ftrace_hash *other_hash;
1496 struct ftrace_page *pg;
1497 struct dyn_ftrace *rec;
1498 int count = 0;
1499 int all = 0;
1500
1501 /* Only update if the ops has been registered */
1502 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1503 return;
1504
1505 /*
1506 * In the filter_hash case:
1507 * If the count is zero, we update all records.
1508 * Otherwise we just update the items in the hash.
1509 *
1510 * In the notrace_hash case:
1511 * We enable the update in the hash.
1512 * As disabling notrace means enabling the tracing,
1513 * and enabling notrace means disabling, the inc variable
1514 * gets inversed.
1515 */
1516 if (filter_hash) {
1517 hash = ops->filter_hash;
1518 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001519 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001520 all = 1;
1521 } else {
1522 inc = !inc;
1523 hash = ops->notrace_hash;
1524 other_hash = ops->filter_hash;
1525 /*
1526 * If the notrace hash has no items,
1527 * then there's nothing to do.
1528 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001529 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001530 return;
1531 }
1532
1533 do_for_each_ftrace_rec(pg, rec) {
1534 int in_other_hash = 0;
1535 int in_hash = 0;
1536 int match = 0;
1537
1538 if (all) {
1539 /*
1540 * Only the filter_hash affects all records.
1541 * Update if the record is not in the notrace hash.
1542 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001543 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001544 match = 1;
1545 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001546 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1547 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001548
1549 /*
1550 *
1551 */
1552 if (filter_hash && in_hash && !in_other_hash)
1553 match = 1;
1554 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001555 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001556 match = 1;
1557 }
1558 if (!match)
1559 continue;
1560
1561 if (inc) {
1562 rec->flags++;
1563 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1564 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001565 /*
1566 * If any ops wants regs saved for this function
1567 * then all ops will get saved regs.
1568 */
1569 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1570 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001571 } else {
1572 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1573 return;
1574 rec->flags--;
1575 }
1576 count++;
1577 /* Shortcut, if we handled all records, we are done. */
1578 if (!all && count == hash->count)
1579 return;
1580 } while_for_each_ftrace_rec();
1581}
1582
1583static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1584 int filter_hash)
1585{
1586 __ftrace_hash_rec_update(ops, filter_hash, 0);
1587}
1588
1589static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1590 int filter_hash)
1591{
1592 __ftrace_hash_rec_update(ops, filter_hash, 1);
1593}
1594
Steven Rostedt05736a42008-09-22 14:55:47 -07001595static void print_ip_ins(const char *fmt, unsigned char *p)
1596{
1597 int i;
1598
1599 printk(KERN_CONT "%s", fmt);
1600
1601 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1602 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1603}
1604
Steven Rostedtc88fd862011-08-16 09:53:39 -04001605/**
1606 * ftrace_bug - report and shutdown function tracer
1607 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1608 * @ip: The address that failed
1609 *
1610 * The arch code that enables or disables the function tracing
1611 * can call ftrace_bug() when it has detected a problem in
1612 * modifying the code. @failed should be one of either:
1613 * EFAULT - if the problem happens on reading the @ip address
1614 * EINVAL - if what is read at @ip is not what was expected
1615 * EPERM - if the problem happens on writting to the @ip address
1616 */
1617void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001618{
1619 switch (failed) {
1620 case -EFAULT:
1621 FTRACE_WARN_ON_ONCE(1);
1622 pr_info("ftrace faulted on modifying ");
1623 print_ip_sym(ip);
1624 break;
1625 case -EINVAL:
1626 FTRACE_WARN_ON_ONCE(1);
1627 pr_info("ftrace failed to modify ");
1628 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001629 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001630 printk(KERN_CONT "\n");
1631 break;
1632 case -EPERM:
1633 FTRACE_WARN_ON_ONCE(1);
1634 pr_info("ftrace faulted on writing ");
1635 print_ip_sym(ip);
1636 break;
1637 default:
1638 FTRACE_WARN_ON_ONCE(1);
1639 pr_info("ftrace faulted on unknown error ");
1640 print_ip_sym(ip);
1641 }
1642}
1643
Steven Rostedtc88fd862011-08-16 09:53:39 -04001644static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001645{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001646 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001647
Steven Rostedt982c3502008-11-15 16:31:41 -05001648 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001649 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001650 *
Steven Rostedted926f92011-05-03 13:25:24 -04001651 * If the record has a ref count, then we need to enable it
1652 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001653 *
Steven Rostedted926f92011-05-03 13:25:24 -04001654 * Otherwise we make sure its disabled.
1655 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001656 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001657 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001658 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001659 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001660 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001661
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001662 /*
1663 * If enabling and the REGS flag does not match the REGS_EN, then
1664 * do not ignore this record. Set flags to fail the compare against
1665 * ENABLED.
1666 */
1667 if (flag &&
1668 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1669 flag |= FTRACE_FL_REGS;
1670
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001671 /* If the state of this record hasn't changed, then do nothing */
1672 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001673 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001674
1675 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001676 /* Save off if rec is being enabled (for return value) */
1677 flag ^= rec->flags & FTRACE_FL_ENABLED;
1678
1679 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001680 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001681 if (flag & FTRACE_FL_REGS) {
1682 if (rec->flags & FTRACE_FL_REGS)
1683 rec->flags |= FTRACE_FL_REGS_EN;
1684 else
1685 rec->flags &= ~FTRACE_FL_REGS_EN;
1686 }
1687 }
1688
1689 /*
1690 * If this record is being updated from a nop, then
1691 * return UPDATE_MAKE_CALL.
1692 * Otherwise, if the EN flag is set, then return
1693 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1694 * from the non-save regs, to a save regs function.
1695 * Otherwise,
1696 * return UPDATE_MODIFY_CALL to tell the caller to convert
1697 * from the save regs, to a non-save regs function.
1698 */
1699 if (flag & FTRACE_FL_ENABLED)
1700 return FTRACE_UPDATE_MAKE_CALL;
1701 else if (rec->flags & FTRACE_FL_REGS_EN)
1702 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1703 else
1704 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001705 }
1706
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001707 if (update) {
1708 /* If there's no more users, clear all flags */
1709 if (!(rec->flags & ~FTRACE_FL_MASK))
1710 rec->flags = 0;
1711 else
1712 /* Just disable the record (keep REGS state) */
1713 rec->flags &= ~FTRACE_FL_ENABLED;
1714 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001715
1716 return FTRACE_UPDATE_MAKE_NOP;
1717}
1718
1719/**
1720 * ftrace_update_record, set a record that now is tracing or not
1721 * @rec: the record to update
1722 * @enable: set to 1 if the record is tracing, zero to force disable
1723 *
1724 * The records that represent all functions that can be traced need
1725 * to be updated when tracing has been enabled.
1726 */
1727int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1728{
1729 return ftrace_check_record(rec, enable, 1);
1730}
1731
1732/**
1733 * ftrace_test_record, check if the record has been enabled or not
1734 * @rec: the record to test
1735 * @enable: set to 1 to check if enabled, 0 if it is disabled
1736 *
1737 * The arch code may need to test if a record is already set to
1738 * tracing to determine how to modify the function code that it
1739 * represents.
1740 */
1741int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1742{
1743 return ftrace_check_record(rec, enable, 0);
1744}
1745
1746static int
1747__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1748{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001749 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001750 unsigned long ftrace_addr;
1751 int ret;
1752
Steven Rostedtc88fd862011-08-16 09:53:39 -04001753 ret = ftrace_update_record(rec, enable);
1754
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001755 if (rec->flags & FTRACE_FL_REGS)
1756 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1757 else
1758 ftrace_addr = (unsigned long)FTRACE_ADDR;
1759
Steven Rostedtc88fd862011-08-16 09:53:39 -04001760 switch (ret) {
1761 case FTRACE_UPDATE_IGNORE:
1762 return 0;
1763
1764 case FTRACE_UPDATE_MAKE_CALL:
1765 return ftrace_make_call(rec, ftrace_addr);
1766
1767 case FTRACE_UPDATE_MAKE_NOP:
1768 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001769
1770 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1771 case FTRACE_UPDATE_MODIFY_CALL:
1772 if (rec->flags & FTRACE_FL_REGS)
1773 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1774 else
1775 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1776
1777 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001778 }
1779
1780 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001781}
1782
Steven Rostedte4f5d542012-04-27 09:13:18 -04001783void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001784{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001785 struct dyn_ftrace *rec;
1786 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001787 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001788
Steven Rostedt45a4a232011-04-21 23:16:46 -04001789 if (unlikely(ftrace_disabled))
1790 return;
1791
Steven Rostedt265c8312009-02-13 12:43:56 -05001792 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001793 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001794 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001795 ftrace_bug(failed, rec->ip);
1796 /* Stop processing */
1797 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001798 }
1799 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001800}
1801
Steven Rostedtc88fd862011-08-16 09:53:39 -04001802struct ftrace_rec_iter {
1803 struct ftrace_page *pg;
1804 int index;
1805};
1806
1807/**
1808 * ftrace_rec_iter_start, start up iterating over traced functions
1809 *
1810 * Returns an iterator handle that is used to iterate over all
1811 * the records that represent address locations where functions
1812 * are traced.
1813 *
1814 * May return NULL if no records are available.
1815 */
1816struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1817{
1818 /*
1819 * We only use a single iterator.
1820 * Protected by the ftrace_lock mutex.
1821 */
1822 static struct ftrace_rec_iter ftrace_rec_iter;
1823 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1824
1825 iter->pg = ftrace_pages_start;
1826 iter->index = 0;
1827
1828 /* Could have empty pages */
1829 while (iter->pg && !iter->pg->index)
1830 iter->pg = iter->pg->next;
1831
1832 if (!iter->pg)
1833 return NULL;
1834
1835 return iter;
1836}
1837
1838/**
1839 * ftrace_rec_iter_next, get the next record to process.
1840 * @iter: The handle to the iterator.
1841 *
1842 * Returns the next iterator after the given iterator @iter.
1843 */
1844struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1845{
1846 iter->index++;
1847
1848 if (iter->index >= iter->pg->index) {
1849 iter->pg = iter->pg->next;
1850 iter->index = 0;
1851
1852 /* Could have empty pages */
1853 while (iter->pg && !iter->pg->index)
1854 iter->pg = iter->pg->next;
1855 }
1856
1857 if (!iter->pg)
1858 return NULL;
1859
1860 return iter;
1861}
1862
1863/**
1864 * ftrace_rec_iter_record, get the record at the iterator location
1865 * @iter: The current iterator location
1866 *
1867 * Returns the record that the current @iter is at.
1868 */
1869struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1870{
1871 return &iter->pg->records[iter->index];
1872}
1873
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301874static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001875ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001876{
1877 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001878 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001879
1880 ip = rec->ip;
1881
Steven Rostedt45a4a232011-04-21 23:16:46 -04001882 if (unlikely(ftrace_disabled))
1883 return 0;
1884
Shaohua Li25aac9d2009-01-09 11:29:40 +08001885 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001886 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001887 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301888 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001889 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301890 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001891}
1892
Steven Rostedt000ab692009-02-17 13:35:06 -05001893/*
1894 * archs can override this function if they must do something
1895 * before the modifying code is performed.
1896 */
1897int __weak ftrace_arch_code_modify_prepare(void)
1898{
1899 return 0;
1900}
1901
1902/*
1903 * archs can override this function if they must do something
1904 * after the modifying code is performed.
1905 */
1906int __weak ftrace_arch_code_modify_post_process(void)
1907{
1908 return 0;
1909}
1910
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001911void ftrace_modify_all_code(int command)
1912{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001913 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01001914 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001915
1916 /*
1917 * If the ftrace_caller calls a ftrace_ops func directly,
1918 * we need to make sure that it only traces functions it
1919 * expects to trace. When doing the switch of functions,
1920 * we need to update to the ftrace_ops_list_func first
1921 * before the transition between old and new calls are set,
1922 * as the ftrace_ops_list_func will check the ops hashes
1923 * to make sure the ops are having the right functions
1924 * traced.
1925 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01001926 if (update) {
1927 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
1928 if (FTRACE_WARN_ON(err))
1929 return;
1930 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001931
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001932 if (command & FTRACE_UPDATE_CALLS)
1933 ftrace_replace_code(1);
1934 else if (command & FTRACE_DISABLE_CALLS)
1935 ftrace_replace_code(0);
1936
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05001937 if (update && ftrace_trace_function != ftrace_ops_list_func) {
1938 function_trace_op = set_function_trace_op;
1939 smp_wmb();
1940 /* If irqs are disabled, we are in stop machine */
1941 if (!irqs_disabled())
1942 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01001943 err = ftrace_update_ftrace_func(ftrace_trace_function);
1944 if (FTRACE_WARN_ON(err))
1945 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05001946 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001947
1948 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01001949 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001950 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01001951 err = ftrace_disable_ftrace_graph_caller();
1952 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001953}
1954
Ingo Molnare309b412008-05-12 21:20:51 +02001955static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001956{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001957 int *command = data;
1958
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001959 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001960
Steven Rostedtc88fd862011-08-16 09:53:39 -04001961 return 0;
1962}
1963
1964/**
1965 * ftrace_run_stop_machine, go back to the stop machine method
1966 * @command: The command to tell ftrace what to do
1967 *
1968 * If an arch needs to fall back to the stop machine method, the
1969 * it can call this function.
1970 */
1971void ftrace_run_stop_machine(int command)
1972{
1973 stop_machine(__ftrace_modify_code, &command, NULL);
1974}
1975
1976/**
1977 * arch_ftrace_update_code, modify the code to trace or not trace
1978 * @command: The command that needs to be done
1979 *
1980 * Archs can override this function if it does not need to
1981 * run stop_machine() to modify code.
1982 */
1983void __weak arch_ftrace_update_code(int command)
1984{
1985 ftrace_run_stop_machine(command);
1986}
1987
1988static void ftrace_run_update_code(int command)
1989{
1990 int ret;
1991
1992 ret = ftrace_arch_code_modify_prepare();
1993 FTRACE_WARN_ON(ret);
1994 if (ret)
1995 return;
1996 /*
1997 * Do not call function tracer while we update the code.
1998 * We are in stop machine.
1999 */
2000 function_trace_stop++;
2001
2002 /*
2003 * By default we use stop_machine() to modify the code.
2004 * But archs can do what ever they want as long as it
2005 * is safe. The stop_machine() is the safest, but also
2006 * produces the most overhead.
2007 */
2008 arch_ftrace_update_code(command);
2009
Steven Rostedt6331c282011-07-13 15:11:02 -04002010 function_trace_stop--;
2011
Steven Rostedt000ab692009-02-17 13:35:06 -05002012 ret = ftrace_arch_code_modify_post_process();
2013 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002014}
2015
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002016static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002017static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002018static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002019
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002020static void control_ops_free(struct ftrace_ops *ops)
2021{
2022 free_percpu(ops->disabled);
2023}
2024
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002025static void ftrace_startup_enable(int command)
2026{
2027 if (saved_ftrace_func != ftrace_trace_function) {
2028 saved_ftrace_func = ftrace_trace_function;
2029 command |= FTRACE_UPDATE_TRACE_FUNC;
2030 }
2031
2032 if (!command || !ftrace_enabled)
2033 return;
2034
2035 ftrace_run_update_code(command);
2036}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002037
Steven Rostedta1cd6172011-05-23 15:24:25 -04002038static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002039{
Steven Rostedtb8489142011-05-04 09:27:52 -04002040 bool hash_enable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002041 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002042
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002043 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002044 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002045
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002046 ret = __register_ftrace_function(ops);
2047 if (ret)
2048 return ret;
2049
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002050 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002051 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002052
Steven Rostedted926f92011-05-03 13:25:24 -04002053 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002054 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002055 ftrace_hash_rec_enable(ops, 1);
2056
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002057 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002058
2059 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002060}
2061
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002062static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002063{
Steven Rostedtb8489142011-05-04 09:27:52 -04002064 bool hash_disable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002065 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002066
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002067 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002068 return -ENODEV;
2069
2070 ret = __unregister_ftrace_function(ops);
2071 if (ret)
2072 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002073
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002074 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002075 /*
2076 * Just warn in case of unbalance, no need to kill ftrace, it's not
2077 * critical but the ftrace_call callers may be never nopped again after
2078 * further ftrace uses.
2079 */
2080 WARN_ON_ONCE(ftrace_start_up < 0);
2081
Steven Rostedtb8489142011-05-04 09:27:52 -04002082 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002083 ftrace_hash_rec_disable(ops, 1);
2084
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05002085 if (!global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002086 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002087
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002088 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002089
2090 if (saved_ftrace_func != ftrace_trace_function) {
2091 saved_ftrace_func = ftrace_trace_function;
2092 command |= FTRACE_UPDATE_TRACE_FUNC;
2093 }
2094
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002095 if (!command || !ftrace_enabled) {
2096 /*
2097 * If these are control ops, they still need their
2098 * per_cpu field freed. Since, function tracing is
2099 * not currently active, we can just free them
2100 * without synchronizing all CPUs.
2101 */
2102 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2103 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002104 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002105 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002106
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002107 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002108
2109 /*
2110 * Dynamic ops may be freed, we must make sure that all
2111 * callers are done before leaving this function.
2112 * The same goes for freeing the per_cpu data of the control
2113 * ops.
2114 *
2115 * Again, normal synchronize_sched() is not good enough.
2116 * We need to do a hard force of sched synchronization.
2117 * This is because we use preempt_disable() to do RCU, but
2118 * the function tracers can be called where RCU is not watching
2119 * (like before user_exit()). We can not rely on the RCU
2120 * infrastructure to do the synchronization, thus we must do it
2121 * ourselves.
2122 */
2123 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2124 schedule_on_each_cpu(ftrace_sync);
2125
2126 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2127 control_ops_free(ops);
2128 }
2129
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002130 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002131}
2132
Ingo Molnare309b412008-05-12 21:20:51 +02002133static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002134{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002135 if (unlikely(ftrace_disabled))
2136 return;
2137
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002138 /* Force update next time */
2139 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002140 /* ftrace_start_up is true if we want ftrace running */
2141 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002142 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002143}
2144
Ingo Molnare309b412008-05-12 21:20:51 +02002145static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002146{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002147 if (unlikely(ftrace_disabled))
2148 return;
2149
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002150 /* ftrace_start_up is true if ftrace is running */
2151 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002152 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002153}
2154
Steven Rostedt3d083392008-05-12 21:20:42 +02002155static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002156unsigned long ftrace_update_tot_cnt;
2157
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002158static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002159{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002160 /*
2161 * Filter_hash being empty will default to trace module.
2162 * But notrace hash requires a test of individual module functions.
2163 */
2164 return ftrace_hash_empty(ops->filter_hash) &&
2165 ftrace_hash_empty(ops->notrace_hash);
2166}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002167
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002168/*
2169 * Check if the current ops references the record.
2170 *
2171 * If the ops traces all functions, then it was already accounted for.
2172 * If the ops does not trace the current record function, skip it.
2173 * If the ops ignores the function via notrace filter, skip it.
2174 */
2175static inline bool
2176ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2177{
2178 /* If ops isn't enabled, ignore it */
2179 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2180 return 0;
2181
2182 /* If ops traces all mods, we already accounted for it */
2183 if (ops_traces_mod(ops))
2184 return 0;
2185
2186 /* The function must be in the filter */
2187 if (!ftrace_hash_empty(ops->filter_hash) &&
2188 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2189 return 0;
2190
2191 /* If in notrace hash, we ignore it too */
2192 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2193 return 0;
2194
2195 return 1;
2196}
2197
2198static int referenced_filters(struct dyn_ftrace *rec)
2199{
2200 struct ftrace_ops *ops;
2201 int cnt = 0;
2202
2203 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2204 if (ops_references_rec(ops, rec))
2205 cnt++;
2206 }
2207
2208 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002209}
2210
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002211static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002212{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002213 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002214 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302215 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002216 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002217 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002218 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002219 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002220
2221 /*
2222 * When adding a module, we need to check if tracers are
2223 * currently enabled and if they are set to trace all functions.
2224 * If they are, we need to enable the module functions as well
2225 * as update the reference counts for those function records.
2226 */
2227 if (mod) {
2228 struct ftrace_ops *ops;
2229
2230 for (ops = ftrace_ops_list;
2231 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002232 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2233 if (ops_traces_mod(ops))
2234 ref++;
2235 else
2236 test = true;
2237 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002238 }
2239 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002240
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002241 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002242
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002243 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302244
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002245 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002246 int cnt = ref;
2247
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002248 /* If something went wrong, bail without enabling anything */
2249 if (unlikely(ftrace_disabled))
2250 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002251
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002252 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002253 if (test)
2254 cnt += referenced_filters(p);
2255 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302256
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002257 /*
2258 * Do the initial record conversion from mcount jump
2259 * to the NOP instructions.
2260 */
2261 if (!ftrace_code_disable(mod, p))
2262 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002263
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002264 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002265
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002266 /*
2267 * If the tracing is enabled, go ahead and enable the record.
2268 *
2269 * The reason not to enable the record immediatelly is the
2270 * inherent check of ftrace_make_nop/ftrace_make_call for
2271 * correct previous instructions. Making first the NOP
2272 * conversion puts the module to the correct state, thus
2273 * passing the ftrace_make_call check.
2274 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002275 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002276 int failed = __ftrace_replace_code(p, 1);
2277 if (failed)
2278 ftrace_bug(failed, p->ip);
2279 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002280 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002281 }
2282
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002283 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002284 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002285 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002286
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002287 return 0;
2288}
2289
Steven Rostedta7900872011-12-16 16:23:44 -05002290static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002291{
Steven Rostedta7900872011-12-16 16:23:44 -05002292 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002293 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002294
Steven Rostedta7900872011-12-16 16:23:44 -05002295 if (WARN_ON(!count))
2296 return -EINVAL;
2297
2298 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002299
2300 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002301 * We want to fill as much as possible. No more than a page
2302 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002303 */
Steven Rostedta7900872011-12-16 16:23:44 -05002304 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2305 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002306
Steven Rostedta7900872011-12-16 16:23:44 -05002307 again:
2308 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2309
2310 if (!pg->records) {
2311 /* if we can't allocate this size, try something smaller */
2312 if (!order)
2313 return -ENOMEM;
2314 order >>= 1;
2315 goto again;
2316 }
2317
2318 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2319 pg->size = cnt;
2320
2321 if (cnt > count)
2322 cnt = count;
2323
2324 return cnt;
2325}
2326
2327static struct ftrace_page *
2328ftrace_allocate_pages(unsigned long num_to_init)
2329{
2330 struct ftrace_page *start_pg;
2331 struct ftrace_page *pg;
2332 int order;
2333 int cnt;
2334
2335 if (!num_to_init)
2336 return 0;
2337
2338 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2339 if (!pg)
2340 return NULL;
2341
2342 /*
2343 * Try to allocate as much as possible in one continues
2344 * location that fills in all of the space. We want to
2345 * waste as little space as possible.
2346 */
2347 for (;;) {
2348 cnt = ftrace_allocate_records(pg, num_to_init);
2349 if (cnt < 0)
2350 goto free_pages;
2351
2352 num_to_init -= cnt;
2353 if (!num_to_init)
2354 break;
2355
2356 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2357 if (!pg->next)
2358 goto free_pages;
2359
2360 pg = pg->next;
2361 }
2362
2363 return start_pg;
2364
2365 free_pages:
2366 while (start_pg) {
2367 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2368 free_pages((unsigned long)pg->records, order);
2369 start_pg = pg->next;
2370 kfree(pg);
2371 pg = start_pg;
2372 }
2373 pr_info("ftrace: FAILED to allocate memory for functions\n");
2374 return NULL;
2375}
2376
Steven Rostedt5072c592008-05-12 21:20:43 +02002377#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2378
2379struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002380 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002381 loff_t func_pos;
2382 struct ftrace_page *pg;
2383 struct dyn_ftrace *func;
2384 struct ftrace_func_probe *probe;
2385 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002386 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002387 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002388 int hidx;
2389 int idx;
2390 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002391};
2392
Ingo Molnare309b412008-05-12 21:20:51 +02002393static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002394t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002395{
2396 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002397 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002398 struct hlist_head *hhd;
2399
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002400 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002401 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002402
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002403 if (iter->probe)
2404 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002405 retry:
2406 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2407 return NULL;
2408
2409 hhd = &ftrace_func_hash[iter->hidx];
2410
2411 if (hlist_empty(hhd)) {
2412 iter->hidx++;
2413 hnd = NULL;
2414 goto retry;
2415 }
2416
2417 if (!hnd)
2418 hnd = hhd->first;
2419 else {
2420 hnd = hnd->next;
2421 if (!hnd) {
2422 iter->hidx++;
2423 goto retry;
2424 }
2425 }
2426
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002427 if (WARN_ON_ONCE(!hnd))
2428 return NULL;
2429
2430 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2431
2432 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002433}
2434
2435static void *t_hash_start(struct seq_file *m, loff_t *pos)
2436{
2437 struct ftrace_iterator *iter = m->private;
2438 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002439 loff_t l;
2440
Steven Rostedt69a30832011-12-19 15:21:16 -05002441 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2442 return NULL;
2443
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002444 if (iter->func_pos > *pos)
2445 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002446
Li Zefand82d6242009-06-24 09:54:54 +08002447 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002448 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002449 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002450 if (!p)
2451 break;
2452 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002453 if (!p)
2454 return NULL;
2455
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002456 /* Only set this if we have an item */
2457 iter->flags |= FTRACE_ITER_HASH;
2458
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002459 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002460}
2461
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002462static int
2463t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002464{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002465 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002466
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002467 rec = iter->probe;
2468 if (WARN_ON_ONCE(!rec))
2469 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002470
Steven Rostedt809dcf22009-02-16 23:06:01 -05002471 if (rec->ops->print)
2472 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2473
Steven Rostedtb375a112009-09-17 00:05:58 -04002474 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002475
2476 if (rec->data)
2477 seq_printf(m, ":%p", rec->data);
2478 seq_putc(m, '\n');
2479
2480 return 0;
2481}
2482
2483static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002484t_next(struct seq_file *m, void *v, loff_t *pos)
2485{
2486 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002487 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002488 struct dyn_ftrace *rec = NULL;
2489
Steven Rostedt45a4a232011-04-21 23:16:46 -04002490 if (unlikely(ftrace_disabled))
2491 return NULL;
2492
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002493 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002494 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002495
Steven Rostedt5072c592008-05-12 21:20:43 +02002496 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002497 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002498
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002499 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002500 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002501
Steven Rostedt5072c592008-05-12 21:20:43 +02002502 retry:
2503 if (iter->idx >= iter->pg->index) {
2504 if (iter->pg->next) {
2505 iter->pg = iter->pg->next;
2506 iter->idx = 0;
2507 goto retry;
2508 }
2509 } else {
2510 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002511 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002512 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002513
Steven Rostedt41c52c02008-05-22 11:46:33 -04002514 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002515 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2516
2517 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002518 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002519
Steven Rostedt5072c592008-05-12 21:20:43 +02002520 rec = NULL;
2521 goto retry;
2522 }
2523 }
2524
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002525 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002526 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002527
2528 iter->func = rec;
2529
2530 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002531}
2532
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002533static void reset_iter_read(struct ftrace_iterator *iter)
2534{
2535 iter->pos = 0;
2536 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03002537 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002538}
2539
2540static void *t_start(struct seq_file *m, loff_t *pos)
2541{
2542 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002543 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002544 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002545 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002546
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002547 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002548
2549 if (unlikely(ftrace_disabled))
2550 return NULL;
2551
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002552 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002553 * If an lseek was done, then reset and start from beginning.
2554 */
2555 if (*pos < iter->pos)
2556 reset_iter_read(iter);
2557
2558 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002559 * For set_ftrace_filter reading, if we have the filter
2560 * off, we can short cut and just print out that all
2561 * functions are enabled.
2562 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002563 if (iter->flags & FTRACE_ITER_FILTER &&
2564 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002565 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002566 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002567 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002568 /* reset in case of seek/pread */
2569 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002570 return iter;
2571 }
2572
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002573 if (iter->flags & FTRACE_ITER_HASH)
2574 return t_hash_start(m, pos);
2575
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002576 /*
2577 * Unfortunately, we need to restart at ftrace_pages_start
2578 * every time we let go of the ftrace_mutex. This is because
2579 * those pointers can change without the lock.
2580 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002581 iter->pg = ftrace_pages_start;
2582 iter->idx = 0;
2583 for (l = 0; l <= *pos; ) {
2584 p = t_next(m, p, &l);
2585 if (!p)
2586 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002587 }
walimis5821e1b2008-11-15 15:19:06 +08002588
Steven Rostedt69a30832011-12-19 15:21:16 -05002589 if (!p)
2590 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002591
2592 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002593}
2594
2595static void t_stop(struct seq_file *m, void *p)
2596{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002597 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002598}
2599
2600static int t_show(struct seq_file *m, void *v)
2601{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002602 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002603 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002604
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002605 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002606 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002607
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002608 if (iter->flags & FTRACE_ITER_PRINTALL) {
2609 seq_printf(m, "#### all functions enabled ####\n");
2610 return 0;
2611 }
2612
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002613 rec = iter->func;
2614
Steven Rostedt5072c592008-05-12 21:20:43 +02002615 if (!rec)
2616 return 0;
2617
Steven Rostedt647bcd02011-05-03 14:39:21 -04002618 seq_printf(m, "%ps", (void *)rec->ip);
2619 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002620 seq_printf(m, " (%ld)%s",
2621 rec->flags & ~FTRACE_FL_MASK,
2622 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002623 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002624
2625 return 0;
2626}
2627
James Morris88e9d342009-09-22 16:43:43 -07002628static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002629 .start = t_start,
2630 .next = t_next,
2631 .stop = t_stop,
2632 .show = t_show,
2633};
2634
Ingo Molnare309b412008-05-12 21:20:51 +02002635static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002636ftrace_avail_open(struct inode *inode, struct file *file)
2637{
2638 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002639
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002640 if (unlikely(ftrace_disabled))
2641 return -ENODEV;
2642
Jiri Olsa50e18b92012-04-25 10:23:39 +02002643 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2644 if (iter) {
2645 iter->pg = ftrace_pages_start;
2646 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002647 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002648
Jiri Olsa50e18b92012-04-25 10:23:39 +02002649 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002650}
2651
Steven Rostedt647bcd02011-05-03 14:39:21 -04002652static int
2653ftrace_enabled_open(struct inode *inode, struct file *file)
2654{
2655 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002656
2657 if (unlikely(ftrace_disabled))
2658 return -ENODEV;
2659
Jiri Olsa50e18b92012-04-25 10:23:39 +02002660 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2661 if (iter) {
2662 iter->pg = ftrace_pages_start;
2663 iter->flags = FTRACE_ITER_ENABLED;
2664 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002665 }
2666
Jiri Olsa50e18b92012-04-25 10:23:39 +02002667 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002668}
2669
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002670static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002671{
Steven Rostedt52baf112009-02-14 01:15:39 -05002672 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002673 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002674 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002675}
2676
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002677/**
2678 * ftrace_regex_open - initialize function tracer filter files
2679 * @ops: The ftrace_ops that hold the hash filters
2680 * @flag: The type of filter to process
2681 * @inode: The inode, usually passed in to your open routine
2682 * @file: The file, usually passed in to your open routine
2683 *
2684 * ftrace_regex_open() initializes the filter files for the
2685 * @ops. Depending on @flag it may process the filter hash or
2686 * the notrace hash of @ops. With this called from the open
2687 * routine, you can use ftrace_filter_write() for the write
2688 * routine if @flag has FTRACE_ITER_FILTER set, or
2689 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002690 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002691 * release must call ftrace_regex_release().
2692 */
2693int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002694ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002695 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002696{
2697 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002698 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002699 int ret = 0;
2700
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002701 ftrace_ops_init(ops);
2702
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002703 if (unlikely(ftrace_disabled))
2704 return -ENODEV;
2705
Steven Rostedt5072c592008-05-12 21:20:43 +02002706 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2707 if (!iter)
2708 return -ENOMEM;
2709
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002710 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2711 kfree(iter);
2712 return -ENOMEM;
2713 }
2714
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002715 iter->ops = ops;
2716 iter->flags = flag;
2717
2718 mutex_lock(&ops->regex_lock);
2719
Steven Rostedtf45948e2011-05-02 12:29:25 -04002720 if (flag & FTRACE_ITER_NOTRACE)
2721 hash = ops->notrace_hash;
2722 else
2723 hash = ops->filter_hash;
2724
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002725 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002726 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002727 if (!iter->hash) {
2728 trace_parser_put(&iter->parser);
2729 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002730 ret = -ENOMEM;
2731 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002732 }
2733 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002734
Steven Rostedt5072c592008-05-12 21:20:43 +02002735 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002736 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002737 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002738
2739 if (file->f_mode & FMODE_READ) {
2740 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002741
2742 ret = seq_open(file, &show_ftrace_seq_ops);
2743 if (!ret) {
2744 struct seq_file *m = file->private_data;
2745 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002746 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002747 /* Failed */
2748 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002749 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002750 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002751 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002752 } else
2753 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002754
2755 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002756 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002757
2758 return ret;
2759}
2760
Steven Rostedt41c52c02008-05-22 11:46:33 -04002761static int
2762ftrace_filter_open(struct inode *inode, struct file *file)
2763{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002764 struct ftrace_ops *ops = inode->i_private;
2765
2766 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05002767 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2768 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002769}
2770
2771static int
2772ftrace_notrace_open(struct inode *inode, struct file *file)
2773{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002774 struct ftrace_ops *ops = inode->i_private;
2775
2776 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002777 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002778}
2779
Steven Rostedt64e7c442009-02-13 17:08:48 -05002780static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002781{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002782 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002783 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002784
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002785 switch (type) {
2786 case MATCH_FULL:
2787 if (strcmp(str, regex) == 0)
2788 matched = 1;
2789 break;
2790 case MATCH_FRONT_ONLY:
2791 if (strncmp(str, regex, len) == 0)
2792 matched = 1;
2793 break;
2794 case MATCH_MIDDLE_ONLY:
2795 if (strstr(str, regex))
2796 matched = 1;
2797 break;
2798 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002799 slen = strlen(str);
2800 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002801 matched = 1;
2802 break;
2803 }
2804
2805 return matched;
2806}
2807
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002808static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002809enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002810{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002811 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002812 int ret = 0;
2813
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002814 entry = ftrace_lookup_ip(hash, rec->ip);
2815 if (not) {
2816 /* Do nothing if it doesn't exist */
2817 if (!entry)
2818 return 0;
2819
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002820 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002821 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002822 /* Do nothing if it exists */
2823 if (entry)
2824 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002825
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002826 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002827 }
2828 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002829}
2830
Steven Rostedt64e7c442009-02-13 17:08:48 -05002831static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002832ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2833 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002834{
2835 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002836 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002837
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002838 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2839
2840 if (mod) {
2841 /* module lookup requires matching the module */
2842 if (!modname || strcmp(modname, mod))
2843 return 0;
2844
2845 /* blank search means to match all funcs in the mod */
2846 if (!len)
2847 return 1;
2848 }
2849
Steven Rostedt64e7c442009-02-13 17:08:48 -05002850 return ftrace_match(str, regex, len, type);
2851}
2852
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002853static int
2854match_records(struct ftrace_hash *hash, char *buff,
2855 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002856{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002857 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002858 struct ftrace_page *pg;
2859 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002860 int type = MATCH_FULL;
2861 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002862 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002863 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002864
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002865 if (len) {
2866 type = filter_parse_regex(buff, len, &search, &not);
2867 search_len = strlen(search);
2868 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002869
Steven Rostedt52baf112009-02-14 01:15:39 -05002870 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002871
2872 if (unlikely(ftrace_disabled))
2873 goto out_unlock;
2874
Steven Rostedt265c8312009-02-13 12:43:56 -05002875 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002876 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002877 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002878 if (ret < 0) {
2879 found = ret;
2880 goto out_unlock;
2881 }
Li Zefan311d16d2009-12-08 11:15:11 +08002882 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002883 }
2884 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002885 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002886 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002887
2888 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002889}
2890
Steven Rostedt64e7c442009-02-13 17:08:48 -05002891static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002892ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002893{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002894 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002895}
2896
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002897static int
2898ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002899{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002900 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002901
Steven Rostedt64e7c442009-02-13 17:08:48 -05002902 /* blank or '*' mean the same */
2903 if (strcmp(buff, "*") == 0)
2904 buff[0] = 0;
2905
2906 /* handle the case of 'dont filter this module' */
2907 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2908 buff[0] = 0;
2909 not = 1;
2910 }
2911
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002912 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002913}
2914
Steven Rostedtf6180772009-02-14 00:40:25 -05002915/*
2916 * We register the module command as a template to show others how
2917 * to register the a command as well.
2918 */
2919
2920static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002921ftrace_mod_callback(struct ftrace_hash *hash,
2922 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002923{
2924 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002925 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002926
2927 /*
2928 * cmd == 'mod' because we only registered this func
2929 * for the 'mod' ftrace_func_command.
2930 * But if you register one func with multiple commands,
2931 * you can tell which command was used by the cmd
2932 * parameter.
2933 */
2934
2935 /* we must have a module name */
2936 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002937 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002938
2939 mod = strsep(&param, ":");
2940 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002941 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002942
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002943 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002944 if (!ret)
2945 ret = -EINVAL;
2946 if (ret < 0)
2947 return ret;
2948
2949 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002950}
2951
2952static struct ftrace_func_command ftrace_mod_cmd = {
2953 .name = "mod",
2954 .func = ftrace_mod_callback,
2955};
2956
2957static int __init ftrace_mod_cmd_init(void)
2958{
2959 return register_ftrace_command(&ftrace_mod_cmd);
2960}
Steven Rostedt6f415672012-10-05 12:13:07 -04002961core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002962
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002963static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002964 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002965{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002966 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002967 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002968 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002969
2970 key = hash_long(ip, FTRACE_HASH_BITS);
2971
2972 hhd = &ftrace_func_hash[key];
2973
2974 if (hlist_empty(hhd))
2975 return;
2976
2977 /*
2978 * Disable preemption for these calls to prevent a RCU grace
2979 * period. This syncs the hash iteration and freeing of items
2980 * on the hash. rcu_read_lock is too dangerous here.
2981 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002982 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04002983 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002984 if (entry->ip == ip)
2985 entry->ops->func(ip, parent_ip, &entry->data);
2986 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002987 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002988}
2989
Steven Rostedtb6887d72009-02-17 12:32:04 -05002990static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002991{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002992 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002993 .flags = FTRACE_OPS_FL_INITIALIZED,
2994 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002995};
2996
Steven Rostedtb6887d72009-02-17 12:32:04 -05002997static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002998
Steven Rostedtb6887d72009-02-17 12:32:04 -05002999static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003000{
Steven Rostedtb8489142011-05-04 09:27:52 -04003001 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003002 int i;
3003
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003004 if (ftrace_probe_registered) {
3005 /* still need to update the function call sites */
3006 if (ftrace_enabled)
3007 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003008 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003009 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003010
3011 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3012 struct hlist_head *hhd = &ftrace_func_hash[i];
3013 if (hhd->first)
3014 break;
3015 }
3016 /* Nothing registered? */
3017 if (i == FTRACE_FUNC_HASHSIZE)
3018 return;
3019
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003020 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003021
Steven Rostedtb6887d72009-02-17 12:32:04 -05003022 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003023}
3024
Steven Rostedtb6887d72009-02-17 12:32:04 -05003025static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003026{
3027 int i;
3028
Steven Rostedtb6887d72009-02-17 12:32:04 -05003029 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003030 return;
3031
3032 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3033 struct hlist_head *hhd = &ftrace_func_hash[i];
3034 if (hhd->first)
3035 return;
3036 }
3037
3038 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003039 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003040
Steven Rostedtb6887d72009-02-17 12:32:04 -05003041 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003042}
3043
3044
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003045static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003046{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003047 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003048 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003049 kfree(entry);
3050}
3051
Steven Rostedt59df055f2009-02-14 15:29:06 -05003052int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003053register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003054 void *data)
3055{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003056 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003057 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3058 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003059 struct ftrace_page *pg;
3060 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003061 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003062 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003063 int count = 0;
3064 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003065 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003066
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003067 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003068 len = strlen(search);
3069
Steven Rostedtb6887d72009-02-17 12:32:04 -05003070 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071 if (WARN_ON(not))
3072 return -EINVAL;
3073
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003074 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003075
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003076 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3077 if (!hash) {
3078 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003079 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003080 }
3081
3082 if (unlikely(ftrace_disabled)) {
3083 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003084 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003085 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003086
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003087 mutex_lock(&ftrace_lock);
3088
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089 do_for_each_ftrace_rec(pg, rec) {
3090
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003091 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003092 continue;
3093
3094 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3095 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003096 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003097 if (!count)
3098 count = -ENOMEM;
3099 goto out_unlock;
3100 }
3101
3102 count++;
3103
3104 entry->data = data;
3105
3106 /*
3107 * The caller might want to do something special
3108 * for each function we find. We call the callback
3109 * to give the caller an opportunity to do so.
3110 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003111 if (ops->init) {
3112 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003113 /* caller does not like this func */
3114 kfree(entry);
3115 continue;
3116 }
3117 }
3118
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003119 ret = enter_record(hash, rec, 0);
3120 if (ret < 0) {
3121 kfree(entry);
3122 count = ret;
3123 goto out_unlock;
3124 }
3125
Steven Rostedt59df055f2009-02-14 15:29:06 -05003126 entry->ops = ops;
3127 entry->ip = rec->ip;
3128
3129 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3130 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3131
3132 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003133
3134 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3135 if (ret < 0)
3136 count = ret;
3137
Steven Rostedtb6887d72009-02-17 12:32:04 -05003138 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003139
3140 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003141 mutex_unlock(&ftrace_lock);
3142 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003143 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003144 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003145
3146 return count;
3147}
3148
3149enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003150 PROBE_TEST_FUNC = 1,
3151 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003152};
3153
3154static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003155__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003156 void *data, int flags)
3157{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003158 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003159 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003160 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003161 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003162 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003163 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003164 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003165 char str[KSYM_SYMBOL_LEN];
3166 int type = MATCH_FULL;
3167 int i, len = 0;
3168 char *search;
3169
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003170 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003171 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003172 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003173 int not;
3174
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003175 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003176 len = strlen(search);
3177
Steven Rostedtb6887d72009-02-17 12:32:04 -05003178 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003179 if (WARN_ON(not))
3180 return;
3181 }
3182
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003183 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003184
3185 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3186 if (!hash)
3187 /* Hmm, should report this somehow */
3188 goto out_unlock;
3189
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003190 INIT_LIST_HEAD(&free_list);
3191
Steven Rostedt59df055f2009-02-14 15:29:06 -05003192 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3193 struct hlist_head *hhd = &ftrace_func_hash[i];
3194
Sasha Levinb67bfe02013-02-27 17:06:00 -08003195 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003196
3197 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003198 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003199 continue;
3200
Steven Rostedtb6887d72009-02-17 12:32:04 -05003201 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003202 continue;
3203
3204 /* do this last, since it is the most expensive */
3205 if (glob) {
3206 kallsyms_lookup(entry->ip, NULL, NULL,
3207 NULL, str);
3208 if (!ftrace_match(str, glob, len, type))
3209 continue;
3210 }
3211
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003212 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3213 /* It is possible more than one entry had this ip */
3214 if (rec_entry)
3215 free_hash_entry(hash, rec_entry);
3216
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003217 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003218 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003219 }
3220 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003221 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003222 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003223 /*
3224 * Remove after the disable is called. Otherwise, if the last
3225 * probe is removed, a null hash means *all enabled*.
3226 */
3227 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003228 synchronize_sched();
3229 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3230 list_del(&entry->free_list);
3231 ftrace_free_entry(entry);
3232 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003233 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003234
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003235 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003236 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003237 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003238}
3239
3240void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003241unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003242 void *data)
3243{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003244 __unregister_ftrace_function_probe(glob, ops, data,
3245 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003246}
3247
3248void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003249unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003250{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003251 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003252}
3253
Steven Rostedtb6887d72009-02-17 12:32:04 -05003254void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003255{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003256 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003257}
3258
Steven Rostedtf6180772009-02-14 00:40:25 -05003259static LIST_HEAD(ftrace_commands);
3260static DEFINE_MUTEX(ftrace_cmd_mutex);
3261
Tom Zanussi38de93a2013-10-24 08:34:18 -05003262/*
3263 * Currently we only register ftrace commands from __init, so mark this
3264 * __init too.
3265 */
3266__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003267{
3268 struct ftrace_func_command *p;
3269 int ret = 0;
3270
3271 mutex_lock(&ftrace_cmd_mutex);
3272 list_for_each_entry(p, &ftrace_commands, list) {
3273 if (strcmp(cmd->name, p->name) == 0) {
3274 ret = -EBUSY;
3275 goto out_unlock;
3276 }
3277 }
3278 list_add(&cmd->list, &ftrace_commands);
3279 out_unlock:
3280 mutex_unlock(&ftrace_cmd_mutex);
3281
3282 return ret;
3283}
3284
Tom Zanussi38de93a2013-10-24 08:34:18 -05003285/*
3286 * Currently we only unregister ftrace commands from __init, so mark
3287 * this __init too.
3288 */
3289__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003290{
3291 struct ftrace_func_command *p, *n;
3292 int ret = -ENODEV;
3293
3294 mutex_lock(&ftrace_cmd_mutex);
3295 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3296 if (strcmp(cmd->name, p->name) == 0) {
3297 ret = 0;
3298 list_del_init(&p->list);
3299 goto out_unlock;
3300 }
3301 }
3302 out_unlock:
3303 mutex_unlock(&ftrace_cmd_mutex);
3304
3305 return ret;
3306}
3307
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003308static int ftrace_process_regex(struct ftrace_hash *hash,
3309 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003310{
Steven Rostedtf6180772009-02-14 00:40:25 -05003311 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003312 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003313 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003314
3315 func = strsep(&next, ":");
3316
3317 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003318 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003319 if (!ret)
3320 ret = -EINVAL;
3321 if (ret < 0)
3322 return ret;
3323 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003324 }
3325
Steven Rostedtf6180772009-02-14 00:40:25 -05003326 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003327
3328 command = strsep(&next, ":");
3329
Steven Rostedtf6180772009-02-14 00:40:25 -05003330 mutex_lock(&ftrace_cmd_mutex);
3331 list_for_each_entry(p, &ftrace_commands, list) {
3332 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003333 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003334 goto out_unlock;
3335 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003336 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003337 out_unlock:
3338 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003339
Steven Rostedtf6180772009-02-14 00:40:25 -05003340 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003341}
3342
Ingo Molnare309b412008-05-12 21:20:51 +02003343static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003344ftrace_regex_write(struct file *file, const char __user *ubuf,
3345 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003346{
3347 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003348 struct trace_parser *parser;
3349 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003350
Li Zefan4ba79782009-09-22 13:52:20 +08003351 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003352 return 0;
3353
Steven Rostedt5072c592008-05-12 21:20:43 +02003354 if (file->f_mode & FMODE_READ) {
3355 struct seq_file *m = file->private_data;
3356 iter = m->private;
3357 } else
3358 iter = file->private_data;
3359
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003360 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003361 return -ENODEV;
3362
3363 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003364
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003365 parser = &iter->parser;
3366 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003367
Li Zefan4ba79782009-09-22 13:52:20 +08003368 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003369 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003370 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003371 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003372 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003373 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003374 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003375 }
3376
Steven Rostedt5072c592008-05-12 21:20:43 +02003377 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003378 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003379 return ret;
3380}
3381
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003382ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003383ftrace_filter_write(struct file *file, const char __user *ubuf,
3384 size_t cnt, loff_t *ppos)
3385{
3386 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3387}
3388
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003389ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003390ftrace_notrace_write(struct file *file, const char __user *ubuf,
3391 size_t cnt, loff_t *ppos)
3392{
3393 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3394}
3395
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003396static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003397ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3398{
3399 struct ftrace_func_entry *entry;
3400
3401 if (!ftrace_location(ip))
3402 return -EINVAL;
3403
3404 if (remove) {
3405 entry = ftrace_lookup_ip(hash, ip);
3406 if (!entry)
3407 return -ENOENT;
3408 free_hash_entry(hash, entry);
3409 return 0;
3410 }
3411
3412 return add_hash_entry(hash, ip);
3413}
3414
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003415static void ftrace_ops_update_code(struct ftrace_ops *ops)
3416{
3417 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3418 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3419}
3420
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003421static int
3422ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3423 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003424{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003425 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003426 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003427 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003428
Steven Rostedt41c52c02008-05-22 11:46:33 -04003429 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003430 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003431
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003432 mutex_lock(&ops->regex_lock);
3433
Steven Rostedtf45948e2011-05-02 12:29:25 -04003434 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003435 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003436 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003437 orig_hash = &ops->notrace_hash;
3438
3439 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003440 if (!hash) {
3441 ret = -ENOMEM;
3442 goto out_regex_unlock;
3443 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003444
Steven Rostedt41c52c02008-05-22 11:46:33 -04003445 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003446 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003447 if (buf && !ftrace_match_records(hash, buf, len)) {
3448 ret = -EINVAL;
3449 goto out_regex_unlock;
3450 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003451 if (ip) {
3452 ret = ftrace_match_addr(hash, ip, remove);
3453 if (ret < 0)
3454 goto out_regex_unlock;
3455 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003456
3457 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003458 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003459 if (!ret)
3460 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003461
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003462 mutex_unlock(&ftrace_lock);
3463
Jiri Olsaac483c42012-01-02 10:04:14 +01003464 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003465 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003466
3467 free_ftrace_hash(hash);
3468 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003469}
3470
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003471static int
3472ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3473 int reset, int enable)
3474{
3475 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3476}
3477
3478/**
3479 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3480 * @ops - the ops to set the filter with
3481 * @ip - the address to add to or remove from the filter.
3482 * @remove - non zero to remove the ip from the filter
3483 * @reset - non zero to reset all filters before applying this filter.
3484 *
3485 * Filters denote which functions should be enabled when tracing is enabled
3486 * If @ip is NULL, it failes to update filter.
3487 */
3488int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3489 int remove, int reset)
3490{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003491 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003492 return ftrace_set_addr(ops, ip, remove, reset, 1);
3493}
3494EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3495
3496static int
3497ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3498 int reset, int enable)
3499{
3500 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3501}
3502
Steven Rostedt77a2b372008-05-12 21:20:45 +02003503/**
3504 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003505 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003506 * @buf - the string that holds the function filter text.
3507 * @len - the length of the string.
3508 * @reset - non zero to reset all filters before applying this filter.
3509 *
3510 * Filters denote which functions should be enabled when tracing is enabled.
3511 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3512 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003513int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003514 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003515{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003516 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003517 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003518}
Steven Rostedt936e0742011-05-05 22:54:01 -04003519EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003520
Steven Rostedt41c52c02008-05-22 11:46:33 -04003521/**
3522 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003523 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003524 * @buf - the string that holds the function notrace text.
3525 * @len - the length of the string.
3526 * @reset - non zero to reset all filters before applying this filter.
3527 *
3528 * Notrace Filters denote which functions should not be enabled when tracing
3529 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3530 * for tracing.
3531 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003532int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003533 int len, int reset)
3534{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003535 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003536 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003537}
3538EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3539/**
3540 * ftrace_set_filter - set a function to filter on in ftrace
3541 * @ops - the ops to set the filter with
3542 * @buf - the string that holds the function filter text.
3543 * @len - the length of the string.
3544 * @reset - non zero to reset all filters before applying this filter.
3545 *
3546 * Filters denote which functions should be enabled when tracing is enabled.
3547 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3548 */
3549void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3550{
3551 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3552}
3553EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3554
3555/**
3556 * ftrace_set_notrace - set a function to not trace in ftrace
3557 * @ops - the ops to set the notrace filter with
3558 * @buf - the string that holds the function notrace text.
3559 * @len - the length of the string.
3560 * @reset - non zero to reset all filters before applying this filter.
3561 *
3562 * Notrace Filters denote which functions should not be enabled when tracing
3563 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3564 * for tracing.
3565 */
3566void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003567{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003568 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003569}
Steven Rostedt936e0742011-05-05 22:54:01 -04003570EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003571
Steven Rostedt2af15d62009-05-28 13:37:24 -04003572/*
3573 * command line interface to allow users to set filters on boot up.
3574 */
3575#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3576static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3577static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3578
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003579/* Used by function selftest to not test if filter is set */
3580bool ftrace_filter_param __initdata;
3581
Steven Rostedt2af15d62009-05-28 13:37:24 -04003582static int __init set_ftrace_notrace(char *str)
3583{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003584 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003585 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003586 return 1;
3587}
3588__setup("ftrace_notrace=", set_ftrace_notrace);
3589
3590static int __init set_ftrace_filter(char *str)
3591{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003592 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003593 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003594 return 1;
3595}
3596__setup("ftrace_filter=", set_ftrace_filter);
3597
Stefan Assmann369bc182009-10-12 22:17:21 +02003598#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003599static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003600static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003601
Stefan Assmann369bc182009-10-12 22:17:21 +02003602static int __init set_graph_function(char *str)
3603{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003604 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003605 return 1;
3606}
3607__setup("ftrace_graph_filter=", set_graph_function);
3608
3609static void __init set_ftrace_early_graph(char *buf)
3610{
3611 int ret;
3612 char *func;
3613
3614 while (buf) {
3615 func = strsep(&buf, ",");
3616 /* we allow only one expression at a time */
3617 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003618 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003619 if (ret)
3620 printk(KERN_DEBUG "ftrace: function %s not "
3621 "traceable\n", func);
3622 }
3623}
3624#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3625
Steven Rostedt2a85a372011-12-19 21:57:44 -05003626void __init
3627ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003628{
3629 char *func;
3630
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003631 ftrace_ops_init(ops);
3632
Steven Rostedt2af15d62009-05-28 13:37:24 -04003633 while (buf) {
3634 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003635 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003636 }
3637}
3638
3639static void __init set_ftrace_early_filters(void)
3640{
3641 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003642 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003643 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003644 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003645#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3646 if (ftrace_graph_buf[0])
3647 set_ftrace_early_graph(ftrace_graph_buf);
3648#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003649}
3650
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003651int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003652{
3653 struct seq_file *m = (struct seq_file *)file->private_data;
3654 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003655 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003656 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003657 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003658 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003659
Steven Rostedt5072c592008-05-12 21:20:43 +02003660 if (file->f_mode & FMODE_READ) {
3661 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003662 seq_release(inode, file);
3663 } else
3664 iter = file->private_data;
3665
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003666 parser = &iter->parser;
3667 if (trace_parser_loaded(parser)) {
3668 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003669 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003670 }
3671
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003672 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003673
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003674 mutex_lock(&iter->ops->regex_lock);
3675
Steven Rostedt058e2972011-04-29 22:35:33 -04003676 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003677 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3678
3679 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003680 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003681 else
3682 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003683
Steven Rostedt058e2972011-04-29 22:35:33 -04003684 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003685 ret = ftrace_hash_move(iter->ops, filter_hash,
3686 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003687 if (!ret)
3688 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003689
Steven Rostedt058e2972011-04-29 22:35:33 -04003690 mutex_unlock(&ftrace_lock);
3691 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003692
3693 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003694 free_ftrace_hash(iter->hash);
3695 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003696
Steven Rostedt5072c592008-05-12 21:20:43 +02003697 return 0;
3698}
3699
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003700static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003701 .open = ftrace_avail_open,
3702 .read = seq_read,
3703 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003704 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003705};
3706
Steven Rostedt647bcd02011-05-03 14:39:21 -04003707static const struct file_operations ftrace_enabled_fops = {
3708 .open = ftrace_enabled_open,
3709 .read = seq_read,
3710 .llseek = seq_lseek,
3711 .release = seq_release_private,
3712};
3713
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003714static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003715 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003716 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003717 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003718 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003719 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003720};
3721
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003722static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003723 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003724 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003725 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003726 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003727 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003728};
3729
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003730#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3731
3732static DEFINE_MUTEX(graph_lock);
3733
3734int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003735int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003736unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003737unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003738
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003739struct ftrace_graph_data {
3740 unsigned long *table;
3741 size_t size;
3742 int *count;
3743 const struct seq_operations *seq_ops;
3744};
3745
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003746static void *
Li Zefan85951842009-06-24 09:54:00 +08003747__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003748{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003749 struct ftrace_graph_data *fgd = m->private;
3750
3751 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003752 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003753 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003754}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003755
Li Zefan85951842009-06-24 09:54:00 +08003756static void *
3757g_next(struct seq_file *m, void *v, loff_t *pos)
3758{
3759 (*pos)++;
3760 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003761}
3762
3763static void *g_start(struct seq_file *m, loff_t *pos)
3764{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003765 struct ftrace_graph_data *fgd = m->private;
3766
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003767 mutex_lock(&graph_lock);
3768
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003769 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003770 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003771 return (void *)1;
3772
Li Zefan85951842009-06-24 09:54:00 +08003773 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003774}
3775
3776static void g_stop(struct seq_file *m, void *p)
3777{
3778 mutex_unlock(&graph_lock);
3779}
3780
3781static int g_show(struct seq_file *m, void *v)
3782{
3783 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003784
3785 if (!ptr)
3786 return 0;
3787
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003788 if (ptr == (unsigned long *)1) {
3789 seq_printf(m, "#### all functions enabled ####\n");
3790 return 0;
3791 }
3792
Steven Rostedtb375a112009-09-17 00:05:58 -04003793 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003794
3795 return 0;
3796}
3797
James Morris88e9d342009-09-22 16:43:43 -07003798static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003799 .start = g_start,
3800 .next = g_next,
3801 .stop = g_stop,
3802 .show = g_show,
3803};
3804
3805static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003806__ftrace_graph_open(struct inode *inode, struct file *file,
3807 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003808{
3809 int ret = 0;
3810
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003811 mutex_lock(&graph_lock);
3812 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003813 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003814 *fgd->count = 0;
3815 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003816 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003817 mutex_unlock(&graph_lock);
3818
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003819 if (file->f_mode & FMODE_READ) {
3820 ret = seq_open(file, fgd->seq_ops);
3821 if (!ret) {
3822 struct seq_file *m = file->private_data;
3823 m->private = fgd;
3824 }
3825 } else
3826 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003827
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003828 return ret;
3829}
3830
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003831static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003832ftrace_graph_open(struct inode *inode, struct file *file)
3833{
3834 struct ftrace_graph_data *fgd;
3835
3836 if (unlikely(ftrace_disabled))
3837 return -ENODEV;
3838
3839 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3840 if (fgd == NULL)
3841 return -ENOMEM;
3842
3843 fgd->table = ftrace_graph_funcs;
3844 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3845 fgd->count = &ftrace_graph_count;
3846 fgd->seq_ops = &ftrace_graph_seq_ops;
3847
3848 return __ftrace_graph_open(inode, file, fgd);
3849}
3850
3851static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003852ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3853{
3854 struct ftrace_graph_data *fgd;
3855
3856 if (unlikely(ftrace_disabled))
3857 return -ENODEV;
3858
3859 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3860 if (fgd == NULL)
3861 return -ENOMEM;
3862
3863 fgd->table = ftrace_graph_notrace_funcs;
3864 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3865 fgd->count = &ftrace_graph_notrace_count;
3866 fgd->seq_ops = &ftrace_graph_seq_ops;
3867
3868 return __ftrace_graph_open(inode, file, fgd);
3869}
3870
3871static int
Li Zefan87827112009-07-23 11:29:11 +08003872ftrace_graph_release(struct inode *inode, struct file *file)
3873{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003874 if (file->f_mode & FMODE_READ) {
3875 struct seq_file *m = file->private_data;
3876
3877 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003878 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003879 } else {
3880 kfree(file->private_data);
3881 }
3882
Li Zefan87827112009-07-23 11:29:11 +08003883 return 0;
3884}
3885
3886static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003887ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003888{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003889 struct dyn_ftrace *rec;
3890 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003891 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003892 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003893 int type, not;
3894 char *search;
3895 bool exists;
3896 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003897
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003898 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003899 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003900 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003901 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003902
3903 search_len = strlen(search);
3904
Steven Rostedt52baf112009-02-14 01:15:39 -05003905 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003906
3907 if (unlikely(ftrace_disabled)) {
3908 mutex_unlock(&ftrace_lock);
3909 return -ENODEV;
3910 }
3911
Steven Rostedt265c8312009-02-13 12:43:56 -05003912 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003913
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003914 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003915 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003916 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003917 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003918 if (array[i] == rec->ip) {
3919 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003920 break;
3921 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003922 }
3923
3924 if (!not) {
3925 fail = 0;
3926 if (!exists) {
3927 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003928 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003929 goto out;
3930 }
3931 } else {
3932 if (exists) {
3933 array[i] = array[--(*idx)];
3934 array[*idx] = 0;
3935 fail = 0;
3936 }
3937 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003938 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003939 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003940out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003941 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003942
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003943 if (fail)
3944 return -EINVAL;
3945
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003946 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003947}
3948
3949static ssize_t
3950ftrace_graph_write(struct file *file, const char __user *ubuf,
3951 size_t cnt, loff_t *ppos)
3952{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003953 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09003954 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003955 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003956
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003957 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003958 return 0;
3959
Namhyung Kim6a101082013-10-14 17:24:25 +09003960 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
3961 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003962
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003963 read = trace_get_user(&parser, ubuf, cnt, ppos);
3964
Li Zefan4ba79782009-09-22 13:52:20 +08003965 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003966 parser.buffer[parser.idx] = 0;
3967
Namhyung Kim6a101082013-10-14 17:24:25 +09003968 mutex_lock(&graph_lock);
3969
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003970 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003971 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
3972 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09003973
3974 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003975 }
3976
Namhyung Kim6a101082013-10-14 17:24:25 +09003977 if (!ret)
3978 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003979
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003980 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003981
3982 return ret;
3983}
3984
3985static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003986 .open = ftrace_graph_open,
3987 .read = seq_read,
3988 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003989 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003990 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003991};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003992
3993static const struct file_operations ftrace_graph_notrace_fops = {
3994 .open = ftrace_graph_notrace_open,
3995 .read = seq_read,
3996 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003997 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003998 .release = ftrace_graph_release,
3999};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004000#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4001
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004002void ftrace_create_filter_files(struct ftrace_ops *ops,
4003 struct dentry *parent)
4004{
4005
4006 trace_create_file("set_ftrace_filter", 0644, parent,
4007 ops, &ftrace_filter_fops);
4008
4009 trace_create_file("set_ftrace_notrace", 0644, parent,
4010 ops, &ftrace_notrace_fops);
4011}
4012
4013/*
4014 * The name "destroy_filter_files" is really a misnomer. Although
4015 * in the future, it may actualy delete the files, but this is
4016 * really intended to make sure the ops passed in are disabled
4017 * and that when this function returns, the caller is free to
4018 * free the ops.
4019 *
4020 * The "destroy" name is only to match the "create" name that this
4021 * should be paired with.
4022 */
4023void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4024{
4025 mutex_lock(&ftrace_lock);
4026 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4027 ftrace_shutdown(ops, 0);
4028 ops->flags |= FTRACE_OPS_FL_DELETED;
4029 mutex_unlock(&ftrace_lock);
4030}
4031
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004032static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004033{
Steven Rostedt5072c592008-05-12 21:20:43 +02004034
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004035 trace_create_file("available_filter_functions", 0444,
4036 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004037
Steven Rostedt647bcd02011-05-03 14:39:21 -04004038 trace_create_file("enabled_functions", 0444,
4039 d_tracer, NULL, &ftrace_enabled_fops);
4040
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004041 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004042
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004043#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004044 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004045 NULL,
4046 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004047 trace_create_file("set_graph_notrace", 0444, d_tracer,
4048 NULL,
4049 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004050#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4051
Steven Rostedt5072c592008-05-12 21:20:43 +02004052 return 0;
4053}
4054
Steven Rostedt9fd49322012-04-24 22:32:06 -04004055static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004056{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004057 const unsigned long *ipa = a;
4058 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004059
Steven Rostedt9fd49322012-04-24 22:32:06 -04004060 if (*ipa > *ipb)
4061 return 1;
4062 if (*ipa < *ipb)
4063 return -1;
4064 return 0;
4065}
4066
4067static void ftrace_swap_ips(void *a, void *b, int size)
4068{
4069 unsigned long *ipa = a;
4070 unsigned long *ipb = b;
4071 unsigned long t;
4072
4073 t = *ipa;
4074 *ipa = *ipb;
4075 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004076}
4077
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004078static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004079 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004080 unsigned long *end)
4081{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004082 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004083 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004084 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004085 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004086 unsigned long *p;
4087 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004088 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004089 int ret = -ENOMEM;
4090
4091 count = end - start;
4092
4093 if (!count)
4094 return 0;
4095
Steven Rostedt9fd49322012-04-24 22:32:06 -04004096 sort(start, count, sizeof(*start),
4097 ftrace_cmp_ips, ftrace_swap_ips);
4098
Steven Rostedt706c81f2012-04-24 23:45:26 -04004099 start_pg = ftrace_allocate_pages(count);
4100 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004101 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004102
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004103 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004104
Steven Rostedt320823092011-12-16 14:42:37 -05004105 /*
4106 * Core and each module needs their own pages, as
4107 * modules will free them when they are removed.
4108 * Force a new page to be allocated for modules.
4109 */
Steven Rostedta7900872011-12-16 16:23:44 -05004110 if (!mod) {
4111 WARN_ON(ftrace_pages || ftrace_pages_start);
4112 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004113 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004114 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004115 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004116 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004117
Steven Rostedta7900872011-12-16 16:23:44 -05004118 if (WARN_ON(ftrace_pages->next)) {
4119 /* Hmm, we have free pages? */
4120 while (ftrace_pages->next)
4121 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004122 }
Steven Rostedta7900872011-12-16 16:23:44 -05004123
Steven Rostedt706c81f2012-04-24 23:45:26 -04004124 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004125 }
4126
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004127 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004128 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004129 while (p < end) {
4130 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004131 /*
4132 * Some architecture linkers will pad between
4133 * the different mcount_loc sections of different
4134 * object files to satisfy alignments.
4135 * Skip any NULL pointers.
4136 */
4137 if (!addr)
4138 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004139
4140 if (pg->index == pg->size) {
4141 /* We should have allocated enough */
4142 if (WARN_ON(!pg->next))
4143 break;
4144 pg = pg->next;
4145 }
4146
4147 rec = &pg->records[pg->index++];
4148 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004149 }
4150
Steven Rostedt706c81f2012-04-24 23:45:26 -04004151 /* We should have used all pages */
4152 WARN_ON(pg->next);
4153
4154 /* Assign the last page to ftrace_pages */
4155 ftrace_pages = pg;
4156
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004157 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004158 * We only need to disable interrupts on start up
4159 * because we are modifying code that an interrupt
4160 * may execute, and the modification is not atomic.
4161 * But for modules, nothing runs the code we modify
4162 * until we are finished with it, and there's no
4163 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004164 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004165 if (!mod)
4166 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004167 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004168 if (!mod)
4169 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004170 ret = 0;
4171 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004172 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004173
Steven Rostedta7900872011-12-16 16:23:44 -05004174 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004175}
4176
Steven Rostedt93eb6772009-04-15 13:24:06 -04004177#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004178
4179#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4180
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004181void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004182{
4183 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004184 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004185 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004186 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004187
Steven Rostedt93eb6772009-04-15 13:24:06 -04004188 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004189
4190 if (ftrace_disabled)
4191 goto out_unlock;
4192
Steven Rostedt320823092011-12-16 14:42:37 -05004193 /*
4194 * Each module has its own ftrace_pages, remove
4195 * them from the list.
4196 */
4197 last_pg = &ftrace_pages_start;
4198 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4199 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004200 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004201 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004202 * As core pages are first, the first
4203 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004204 */
Steven Rostedt320823092011-12-16 14:42:37 -05004205 if (WARN_ON(pg == ftrace_pages_start))
4206 goto out_unlock;
4207
4208 /* Check if we are deleting the last page */
4209 if (pg == ftrace_pages)
4210 ftrace_pages = next_to_ftrace_page(last_pg);
4211
4212 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004213 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4214 free_pages((unsigned long)pg->records, order);
4215 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004216 } else
4217 last_pg = &pg->next;
4218 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004219 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004220 mutex_unlock(&ftrace_lock);
4221}
4222
4223static void ftrace_init_module(struct module *mod,
4224 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004225{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004226 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004227 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004228 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004229}
4230
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004231static int ftrace_module_notify_enter(struct notifier_block *self,
4232 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004233{
4234 struct module *mod = data;
4235
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004236 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004237 ftrace_init_module(mod, mod->ftrace_callsites,
4238 mod->ftrace_callsites +
4239 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004240 return 0;
4241}
4242
4243static int ftrace_module_notify_exit(struct notifier_block *self,
4244 unsigned long val, void *data)
4245{
4246 struct module *mod = data;
4247
4248 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004249 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004250
4251 return 0;
4252}
4253#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004254static int ftrace_module_notify_enter(struct notifier_block *self,
4255 unsigned long val, void *data)
4256{
4257 return 0;
4258}
4259static int ftrace_module_notify_exit(struct notifier_block *self,
4260 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004261{
4262 return 0;
4263}
4264#endif /* CONFIG_MODULES */
4265
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004266struct notifier_block ftrace_module_enter_nb = {
4267 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004268 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004269};
4270
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004271struct notifier_block ftrace_module_exit_nb = {
4272 .notifier_call = ftrace_module_notify_exit,
4273 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4274};
4275
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004276void __init ftrace_init(void)
4277{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004278 extern unsigned long __start_mcount_loc[];
4279 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004280 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004281 int ret;
4282
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004283 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004284 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004285 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004286 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004287 goto failed;
4288
4289 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004290 if (!count) {
4291 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004292 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004293 }
4294
4295 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4296 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004297
4298 last_ftrace_enabled = ftrace_enabled = 1;
4299
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004300 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004301 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004302 __stop_mcount_loc);
4303
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004304 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004305 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004306 pr_warning("Failed to register trace ftrace module enter notifier\n");
4307
4308 ret = register_module_notifier(&ftrace_module_exit_nb);
4309 if (ret)
4310 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004311
Steven Rostedt2af15d62009-05-28 13:37:24 -04004312 set_ftrace_early_filters();
4313
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004314 return;
4315 failed:
4316 ftrace_disabled = 1;
4317}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004318
Steven Rostedt3d083392008-05-12 21:20:42 +02004319#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004320
Steven Rostedt2b499382011-05-03 22:49:52 -04004321static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004322 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004323 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4324 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004325};
4326
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004327static int __init ftrace_nodyn_init(void)
4328{
4329 ftrace_enabled = 1;
4330 return 0;
4331}
Steven Rostedt6f415672012-10-05 12:13:07 -04004332core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004333
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004334static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4335static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004336/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004337# define ftrace_startup(ops, command) \
4338 ({ \
4339 int ___ret = __register_ftrace_function(ops); \
4340 if (!___ret) \
4341 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4342 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004343 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004344# define ftrace_shutdown(ops, command) \
4345 ({ \
4346 int ___ret = __unregister_ftrace_function(ops); \
4347 if (!___ret) \
4348 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4349 ___ret; \
4350 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004351
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004352# define ftrace_startup_sysctl() do { } while (0)
4353# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004354
4355static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004356ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004357{
4358 return 1;
4359}
4360
Steven Rostedt3d083392008-05-12 21:20:42 +02004361#endif /* CONFIG_DYNAMIC_FTRACE */
4362
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004363__init void ftrace_init_global_array_ops(struct trace_array *tr)
4364{
4365 tr->ops = &global_ops;
4366 tr->ops->private = tr;
4367}
4368
4369void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4370{
4371 /* If we filter on pids, update to use the pid function */
4372 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4373 if (WARN_ON(tr->ops->func != ftrace_stub))
4374 printk("ftrace ops had %pS for function\n",
4375 tr->ops->func);
4376 /* Only the top level instance does pid tracing */
4377 if (!list_empty(&ftrace_pids)) {
4378 set_ftrace_pid_function(func);
4379 func = ftrace_pid_func;
4380 }
4381 }
4382 tr->ops->func = func;
4383 tr->ops->private = tr;
4384}
4385
4386void ftrace_reset_array_ops(struct trace_array *tr)
4387{
4388 tr->ops->func = ftrace_stub;
4389}
4390
Steven Rostedtb8489142011-05-04 09:27:52 -04004391static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004392ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004393 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004394{
Jiri Olsae2484912012-02-15 15:51:48 +01004395 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4396 return;
4397
4398 /*
4399 * Some of the ops may be dynamically allocated,
4400 * they must be freed after a synchronize_sched().
4401 */
4402 preempt_disable_notrace();
4403 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004404
4405 /*
4406 * Control funcs (perf) uses RCU. Only trace if
4407 * RCU is currently active.
4408 */
4409 if (!rcu_is_watching())
4410 goto out;
4411
Steven Rostedt0a016402012-11-02 17:03:03 -04004412 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004413 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4414 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004415 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004416 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004417 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004418 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004419 trace_recursion_clear(TRACE_CONTROL_BIT);
4420 preempt_enable_notrace();
4421}
4422
4423static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004424 .func = ftrace_ops_control_func,
4425 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4426 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004427};
4428
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004429static inline void
4430__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004431 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004432{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004433 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004434 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004435
Steven Rostedtccf36722012-06-05 09:44:25 -04004436 if (function_trace_stop)
4437 return;
4438
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004439 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4440 if (bit < 0)
4441 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004442
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004443 /*
4444 * Some of the ops may be dynamically allocated,
4445 * they must be freed after a synchronize_sched().
4446 */
4447 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004448 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004449 if (ftrace_ops_test(op, ip, regs)) {
4450 if (WARN_ON(!op->func)) {
4451 function_trace_stop = 1;
4452 printk("op=%p %pS\n", op, op);
4453 goto out;
4454 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004455 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004456 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004457 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004458out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004459 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004460 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004461}
4462
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004463/*
4464 * Some archs only support passing ip and parent_ip. Even though
4465 * the list function ignores the op parameter, we do not want any
4466 * C side effects, where a function is called without the caller
4467 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004468 * Archs are to support both the regs and ftrace_ops at the same time.
4469 * If they support ftrace_ops, it is assumed they support regs.
4470 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004471 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4472 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004473 * An architecture can pass partial regs with ftrace_ops and still
4474 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004475 */
4476#if ARCH_SUPPORTS_FTRACE_OPS
4477static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004478 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004479{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004480 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004481}
4482#else
4483static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4484{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004485 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004486}
4487#endif
4488
Steven Rostedte32d8952008-12-04 00:26:41 -05004489static void clear_ftrace_swapper(void)
4490{
4491 struct task_struct *p;
4492 int cpu;
4493
4494 get_online_cpus();
4495 for_each_online_cpu(cpu) {
4496 p = idle_task(cpu);
4497 clear_tsk_trace_trace(p);
4498 }
4499 put_online_cpus();
4500}
4501
4502static void set_ftrace_swapper(void)
4503{
4504 struct task_struct *p;
4505 int cpu;
4506
4507 get_online_cpus();
4508 for_each_online_cpu(cpu) {
4509 p = idle_task(cpu);
4510 set_tsk_trace_trace(p);
4511 }
4512 put_online_cpus();
4513}
4514
4515static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004516{
4517 struct task_struct *p;
4518
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004519 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004520 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004521 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004522 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004523 rcu_read_unlock();
4524
Steven Rostedte32d8952008-12-04 00:26:41 -05004525 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004526}
4527
Steven Rostedte32d8952008-12-04 00:26:41 -05004528static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004529{
4530 struct task_struct *p;
4531
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004532 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004533 do_each_pid_task(pid, PIDTYPE_PID, p) {
4534 set_tsk_trace_trace(p);
4535 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004536 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004537}
4538
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004539static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004540{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004541 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004542 clear_ftrace_swapper();
4543 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004544 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004545}
4546
4547static void set_ftrace_pid_task(struct pid *pid)
4548{
4549 if (pid == ftrace_swapper_pid)
4550 set_ftrace_swapper();
4551 else
4552 set_ftrace_pid(pid);
4553}
4554
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004555static int ftrace_pid_add(int p)
4556{
4557 struct pid *pid;
4558 struct ftrace_pid *fpid;
4559 int ret = -EINVAL;
4560
4561 mutex_lock(&ftrace_lock);
4562
4563 if (!p)
4564 pid = ftrace_swapper_pid;
4565 else
4566 pid = find_get_pid(p);
4567
4568 if (!pid)
4569 goto out;
4570
4571 ret = 0;
4572
4573 list_for_each_entry(fpid, &ftrace_pids, list)
4574 if (fpid->pid == pid)
4575 goto out_put;
4576
4577 ret = -ENOMEM;
4578
4579 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4580 if (!fpid)
4581 goto out_put;
4582
4583 list_add(&fpid->list, &ftrace_pids);
4584 fpid->pid = pid;
4585
4586 set_ftrace_pid_task(pid);
4587
4588 ftrace_update_pid_func();
4589 ftrace_startup_enable(0);
4590
4591 mutex_unlock(&ftrace_lock);
4592 return 0;
4593
4594out_put:
4595 if (pid != ftrace_swapper_pid)
4596 put_pid(pid);
4597
4598out:
4599 mutex_unlock(&ftrace_lock);
4600 return ret;
4601}
4602
4603static void ftrace_pid_reset(void)
4604{
4605 struct ftrace_pid *fpid, *safe;
4606
4607 mutex_lock(&ftrace_lock);
4608 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4609 struct pid *pid = fpid->pid;
4610
4611 clear_ftrace_pid_task(pid);
4612
4613 list_del(&fpid->list);
4614 kfree(fpid);
4615 }
4616
4617 ftrace_update_pid_func();
4618 ftrace_startup_enable(0);
4619
4620 mutex_unlock(&ftrace_lock);
4621}
4622
4623static void *fpid_start(struct seq_file *m, loff_t *pos)
4624{
4625 mutex_lock(&ftrace_lock);
4626
4627 if (list_empty(&ftrace_pids) && (!*pos))
4628 return (void *) 1;
4629
4630 return seq_list_start(&ftrace_pids, *pos);
4631}
4632
4633static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4634{
4635 if (v == (void *)1)
4636 return NULL;
4637
4638 return seq_list_next(v, &ftrace_pids, pos);
4639}
4640
4641static void fpid_stop(struct seq_file *m, void *p)
4642{
4643 mutex_unlock(&ftrace_lock);
4644}
4645
4646static int fpid_show(struct seq_file *m, void *v)
4647{
4648 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4649
4650 if (v == (void *)1) {
4651 seq_printf(m, "no pid\n");
4652 return 0;
4653 }
4654
4655 if (fpid->pid == ftrace_swapper_pid)
4656 seq_printf(m, "swapper tasks\n");
4657 else
4658 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4659
4660 return 0;
4661}
4662
4663static const struct seq_operations ftrace_pid_sops = {
4664 .start = fpid_start,
4665 .next = fpid_next,
4666 .stop = fpid_stop,
4667 .show = fpid_show,
4668};
4669
4670static int
4671ftrace_pid_open(struct inode *inode, struct file *file)
4672{
4673 int ret = 0;
4674
4675 if ((file->f_mode & FMODE_WRITE) &&
4676 (file->f_flags & O_TRUNC))
4677 ftrace_pid_reset();
4678
4679 if (file->f_mode & FMODE_READ)
4680 ret = seq_open(file, &ftrace_pid_sops);
4681
4682 return ret;
4683}
4684
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004685static ssize_t
4686ftrace_pid_write(struct file *filp, const char __user *ubuf,
4687 size_t cnt, loff_t *ppos)
4688{
Ingo Molnar457dc922009-11-23 11:03:28 +01004689 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004690 long val;
4691 int ret;
4692
4693 if (cnt >= sizeof(buf))
4694 return -EINVAL;
4695
4696 if (copy_from_user(&buf, ubuf, cnt))
4697 return -EFAULT;
4698
4699 buf[cnt] = 0;
4700
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004701 /*
4702 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4703 * to clean the filter quietly.
4704 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004705 tmp = strstrip(buf);
4706 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004707 return 1;
4708
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004709 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004710 if (ret < 0)
4711 return ret;
4712
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004713 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004714
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004715 return ret ? ret : cnt;
4716}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004717
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004718static int
4719ftrace_pid_release(struct inode *inode, struct file *file)
4720{
4721 if (file->f_mode & FMODE_READ)
4722 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004723
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004724 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004725}
4726
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004727static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004728 .open = ftrace_pid_open,
4729 .write = ftrace_pid_write,
4730 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004731 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004732 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004733};
4734
4735static __init int ftrace_init_debugfs(void)
4736{
4737 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004738
4739 d_tracer = tracing_init_dentry();
4740 if (!d_tracer)
4741 return 0;
4742
4743 ftrace_init_dyn_debugfs(d_tracer);
4744
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004745 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4746 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004747
4748 ftrace_profile_debugfs(d_tracer);
4749
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004750 return 0;
4751}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004752fs_initcall(ftrace_init_debugfs);
4753
Steven Rostedt3d083392008-05-12 21:20:42 +02004754/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004755 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004756 *
4757 * This function should be used by panic code. It stops ftrace
4758 * but in a not so nice way. If you need to simply kill ftrace
4759 * from a non-atomic section, use ftrace_kill.
4760 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004761void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004762{
4763 ftrace_disabled = 1;
4764 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004765 clear_ftrace_function();
4766}
4767
4768/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004769 * Test if ftrace is dead or not.
4770 */
4771int ftrace_is_dead(void)
4772{
4773 return ftrace_disabled;
4774}
4775
4776/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004777 * register_ftrace_function - register a function for profiling
4778 * @ops - ops structure that holds the function for profiling.
4779 *
4780 * Register a function to be called by all functions in the
4781 * kernel.
4782 *
4783 * Note: @ops->func and all the functions it calls must be labeled
4784 * with "notrace", otherwise it will go into a
4785 * recursive loop.
4786 */
4787int register_ftrace_function(struct ftrace_ops *ops)
4788{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004789 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004790
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004791 ftrace_ops_init(ops);
4792
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004793 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004794
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004795 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004796
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004797 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004798
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004799 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004800}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004801EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004802
4803/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004804 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004805 * @ops - ops structure that holds the function to unregister
4806 *
4807 * Unregister a function that was added to be called by ftrace profiling.
4808 */
4809int unregister_ftrace_function(struct ftrace_ops *ops)
4810{
4811 int ret;
4812
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004813 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004814 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004815 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004816
4817 return ret;
4818}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004819EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004820
Ingo Molnare309b412008-05-12 21:20:51 +02004821int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004822ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004823 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004824 loff_t *ppos)
4825{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004826 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004827
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004828 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004829
Steven Rostedt45a4a232011-04-21 23:16:46 -04004830 if (unlikely(ftrace_disabled))
4831 goto out;
4832
4833 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004834
Li Zefana32c7762009-06-26 16:55:51 +08004835 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004836 goto out;
4837
Li Zefana32c7762009-06-26 16:55:51 +08004838 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004839
4840 if (ftrace_enabled) {
4841
4842 ftrace_startup_sysctl();
4843
4844 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004845 if (ftrace_ops_list != &ftrace_list_end)
4846 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004847
4848 } else {
4849 /* stopping ftrace calls (just send to ftrace_stub) */
4850 ftrace_trace_function = ftrace_stub;
4851
4852 ftrace_shutdown_sysctl();
4853 }
4854
4855 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004856 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004857 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004858}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004859
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004860#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004861
Steven Rostedt597af812009-04-03 15:24:12 -04004862static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004863static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004864
Steven Rostedte49dc192008-12-02 23:50:05 -05004865int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4866{
4867 return 0;
4868}
4869
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004870/* The callbacks that hook a function */
4871trace_func_graph_ret_t ftrace_graph_return =
4872 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004873trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05004874static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004875
4876/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4877static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4878{
4879 int i;
4880 int ret = 0;
4881 unsigned long flags;
4882 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4883 struct task_struct *g, *t;
4884
4885 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4886 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4887 * sizeof(struct ftrace_ret_stack),
4888 GFP_KERNEL);
4889 if (!ret_stack_list[i]) {
4890 start = 0;
4891 end = i;
4892 ret = -ENOMEM;
4893 goto free;
4894 }
4895 }
4896
4897 read_lock_irqsave(&tasklist_lock, flags);
4898 do_each_thread(g, t) {
4899 if (start == end) {
4900 ret = -EAGAIN;
4901 goto unlock;
4902 }
4903
4904 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004905 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004906 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004907 t->curr_ret_stack = -1;
4908 /* Make sure the tasks see the -1 first: */
4909 smp_wmb();
4910 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004911 }
4912 } while_each_thread(g, t);
4913
4914unlock:
4915 read_unlock_irqrestore(&tasklist_lock, flags);
4916free:
4917 for (i = start; i < end; i++)
4918 kfree(ret_stack_list[i]);
4919 return ret;
4920}
4921
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004922static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004923ftrace_graph_probe_sched_switch(void *ignore,
4924 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004925{
4926 unsigned long long timestamp;
4927 int index;
4928
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004929 /*
4930 * Does the user want to count the time a function was asleep.
4931 * If so, do not update the time stamps.
4932 */
4933 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4934 return;
4935
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004936 timestamp = trace_clock_local();
4937
4938 prev->ftrace_timestamp = timestamp;
4939
4940 /* only process tasks that we timestamped */
4941 if (!next->ftrace_timestamp)
4942 return;
4943
4944 /*
4945 * Update all the counters in next to make up for the
4946 * time next was sleeping.
4947 */
4948 timestamp -= next->ftrace_timestamp;
4949
4950 for (index = next->curr_ret_stack; index >= 0; index--)
4951 next->ret_stack[index].calltime += timestamp;
4952}
4953
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004954/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004955static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004956{
4957 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004958 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004959
4960 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4961 sizeof(struct ftrace_ret_stack *),
4962 GFP_KERNEL);
4963
4964 if (!ret_stack_list)
4965 return -ENOMEM;
4966
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004967 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004968 for_each_online_cpu(cpu) {
4969 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004970 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004971 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004972
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004973 do {
4974 ret = alloc_retstack_tasklist(ret_stack_list);
4975 } while (ret == -EAGAIN);
4976
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004977 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004978 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004979 if (ret)
4980 pr_info("ftrace_graph: Couldn't activate tracepoint"
4981 " probe to kernel_sched_switch\n");
4982 }
4983
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004984 kfree(ret_stack_list);
4985 return ret;
4986}
4987
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004988/*
4989 * Hibernation protection.
4990 * The state of the current task is too much unstable during
4991 * suspend/restore to disk. We want to protect against that.
4992 */
4993static int
4994ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4995 void *unused)
4996{
4997 switch (state) {
4998 case PM_HIBERNATION_PREPARE:
4999 pause_graph_tracing();
5000 break;
5001
5002 case PM_POST_HIBERNATION:
5003 unpause_graph_tracing();
5004 break;
5005 }
5006 return NOTIFY_DONE;
5007}
5008
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005009/* Just a place holder for function graph */
5010static struct ftrace_ops fgraph_ops __read_mostly = {
5011 .func = ftrace_stub,
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005012 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005013};
5014
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005015static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5016{
5017 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5018 return 0;
5019 return __ftrace_graph_entry(trace);
5020}
5021
5022/*
5023 * The function graph tracer should only trace the functions defined
5024 * by set_ftrace_filter and set_ftrace_notrace. If another function
5025 * tracer ops is registered, the graph tracer requires testing the
5026 * function against the global ops, and not just trace any function
5027 * that any ftrace_ops registered.
5028 */
5029static void update_function_graph_func(void)
5030{
5031 if (ftrace_ops_list == &ftrace_list_end ||
5032 (ftrace_ops_list == &global_ops &&
5033 global_ops.next == &ftrace_list_end))
5034 ftrace_graph_entry = __ftrace_graph_entry;
5035 else
5036 ftrace_graph_entry = ftrace_graph_entry_test;
5037}
5038
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005039int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5040 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005041{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005042 int ret = 0;
5043
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005044 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005045
Steven Rostedt05ce5812009-03-24 00:18:31 -04005046 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005047 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005048 ret = -EBUSY;
5049 goto out;
5050 }
5051
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005052 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5053 register_pm_notifier(&ftrace_suspend_notifier);
5054
Steven Rostedt597af812009-04-03 15:24:12 -04005055 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005056 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005057 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005058 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005059 goto out;
5060 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005061
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005062 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005063
5064 /*
5065 * Update the indirect function to the entryfunc, and the
5066 * function that gets called to the entry_test first. Then
5067 * call the update fgraph entry function to determine if
5068 * the entryfunc should be called directly or not.
5069 */
5070 __ftrace_graph_entry = entryfunc;
5071 ftrace_graph_entry = ftrace_graph_entry_test;
5072 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005073
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005074 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005075
5076out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005077 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005078 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005079}
5080
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005081void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005082{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005083 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005084
Steven Rostedt597af812009-04-03 15:24:12 -04005085 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005086 goto out;
5087
Steven Rostedt597af812009-04-03 15:24:12 -04005088 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005089 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005090 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005091 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005092 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005093 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005094 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005095
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005096 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005097 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005098}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005099
Steven Rostedt868baf02011-02-10 21:26:13 -05005100static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5101
5102static void
5103graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5104{
5105 atomic_set(&t->tracing_graph_pause, 0);
5106 atomic_set(&t->trace_overrun, 0);
5107 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005108 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005109 smp_wmb();
5110 t->ret_stack = ret_stack;
5111}
5112
5113/*
5114 * Allocate a return stack for the idle task. May be the first
5115 * time through, or it may be done by CPU hotplug online.
5116 */
5117void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5118{
5119 t->curr_ret_stack = -1;
5120 /*
5121 * The idle task has no parent, it either has its own
5122 * stack or no stack at all.
5123 */
5124 if (t->ret_stack)
5125 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5126
5127 if (ftrace_graph_active) {
5128 struct ftrace_ret_stack *ret_stack;
5129
5130 ret_stack = per_cpu(idle_ret_stack, cpu);
5131 if (!ret_stack) {
5132 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5133 * sizeof(struct ftrace_ret_stack),
5134 GFP_KERNEL);
5135 if (!ret_stack)
5136 return;
5137 per_cpu(idle_ret_stack, cpu) = ret_stack;
5138 }
5139 graph_init_task(t, ret_stack);
5140 }
5141}
5142
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005143/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005144void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005145{
Steven Rostedt84047e32009-06-02 16:51:55 -04005146 /* Make sure we do not use the parent ret_stack */
5147 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005148 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005149
Steven Rostedt597af812009-04-03 15:24:12 -04005150 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005151 struct ftrace_ret_stack *ret_stack;
5152
5153 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005154 * sizeof(struct ftrace_ret_stack),
5155 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005156 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005157 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005158 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005159 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005160}
5161
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005162void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005163{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005164 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5165
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005166 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005167 /* NULL must become visible to IRQs before we free it: */
5168 barrier();
5169
5170 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005171}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005172
5173void ftrace_graph_stop(void)
5174{
5175 ftrace_stop();
5176}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005177#endif