blob: 3cfeb66bda6703a22dd78c6def2d4685344b7c76 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -050065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
Jiri Olsae2484912012-02-15 15:51:48 +010066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
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
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800316int using_ftrace_ops_list_func(void)
317{
318 return ftrace_trace_function == ftrace_ops_list_func;
319}
320
Steven Rostedt2b499382011-05-03 22:49:52 -0400321static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200322{
Steven Rostedt2b499382011-05-03 22:49:52 -0400323 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200324 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400325 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200326 * CPU might be walking that list. We need to make sure
327 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400328 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200329 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400330 rcu_assign_pointer(*list, ops);
331}
Steven Rostedt3d083392008-05-12 21:20:42 +0200332
Steven Rostedt2b499382011-05-03 22:49:52 -0400333static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
334{
335 struct ftrace_ops **p;
336
337 /*
338 * If we are removing the last function, then simply point
339 * to the ftrace_stub.
340 */
341 if (*list == ops && ops->next == &ftrace_list_end) {
342 *list = &ftrace_list_end;
343 return 0;
344 }
345
346 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
347 if (*p == ops)
348 break;
349
350 if (*p != ops)
351 return -1;
352
353 *p = (*p)->next;
354 return 0;
355}
356
Jiri Olsae2484912012-02-15 15:51:48 +0100357static void add_ftrace_list_ops(struct ftrace_ops **list,
358 struct ftrace_ops *main_ops,
359 struct ftrace_ops *ops)
360{
361 int first = *list == &ftrace_list_end;
362 add_ftrace_ops(list, ops);
363 if (first)
364 add_ftrace_ops(&ftrace_ops_list, main_ops);
365}
366
367static int remove_ftrace_list_ops(struct ftrace_ops **list,
368 struct ftrace_ops *main_ops,
369 struct ftrace_ops *ops)
370{
371 int ret = remove_ftrace_ops(list, ops);
372 if (!ret && *list == &ftrace_list_end)
373 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
374 return ret;
375}
376
Steven Rostedt2b499382011-05-03 22:49:52 -0400377static int __register_ftrace_function(struct ftrace_ops *ops)
378{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500379 if (ops->flags & FTRACE_OPS_FL_DELETED)
380 return -EINVAL;
381
Steven Rostedtb8489142011-05-04 09:27:52 -0400382 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
383 return -EBUSY;
384
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900385#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400386 /*
387 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
388 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
389 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
390 */
391 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
392 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
393 return -EINVAL;
394
395 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
396 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
397#endif
398
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400399 if (!core_kernel_data((unsigned long)ops))
400 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
401
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500402 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100403 if (control_ops_alloc(ops))
404 return -ENOMEM;
405 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400406 } else
407 add_ftrace_ops(&ftrace_ops_list, ops);
408
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400409 if (ftrace_enabled)
410 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200411
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200412 return 0;
413}
414
Ingo Molnare309b412008-05-12 21:20:51 +0200415static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200416{
Steven Rostedt2b499382011-05-03 22:49:52 -0400417 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200418
Steven Rostedtb8489142011-05-04 09:27:52 -0400419 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
420 return -EBUSY;
421
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500422 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100423 ret = remove_ftrace_list_ops(&ftrace_control_list,
424 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400425 } else
426 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
427
Steven Rostedt2b499382011-05-03 22:49:52 -0400428 if (ret < 0)
429 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400430
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400431 if (ftrace_enabled)
432 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200433
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500434 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200435}
436
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500437static void ftrace_update_pid_func(void)
438{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400439 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500440 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900441 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500442
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400443 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500444}
445
Steven Rostedt493762f2009-03-23 17:12:36 -0400446#ifdef CONFIG_FUNCTION_PROFILER
447struct ftrace_profile {
448 struct hlist_node node;
449 unsigned long ip;
450 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400451#ifdef CONFIG_FUNCTION_GRAPH_TRACER
452 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400453 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400454#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400455};
456
457struct ftrace_profile_page {
458 struct ftrace_profile_page *next;
459 unsigned long index;
460 struct ftrace_profile records[];
461};
462
Steven Rostedtcafb1682009-03-24 20:50:39 -0400463struct ftrace_profile_stat {
464 atomic_t disabled;
465 struct hlist_head *hash;
466 struct ftrace_profile_page *pages;
467 struct ftrace_profile_page *start;
468 struct tracer_stat stat;
469};
470
Steven Rostedt493762f2009-03-23 17:12:36 -0400471#define PROFILE_RECORDS_SIZE \
472 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
473
474#define PROFILES_PER_PAGE \
475 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
476
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400477static int ftrace_profile_enabled __read_mostly;
478
479/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400480static DEFINE_MUTEX(ftrace_profile_lock);
481
Steven Rostedtcafb1682009-03-24 20:50:39 -0400482static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400483
Namhyung Kim20079eb2013-04-10 08:55:50 +0900484#define FTRACE_PROFILE_HASH_BITS 10
485#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400486
Steven Rostedt493762f2009-03-23 17:12:36 -0400487static void *
488function_stat_next(void *v, int idx)
489{
490 struct ftrace_profile *rec = v;
491 struct ftrace_profile_page *pg;
492
493 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
494
495 again:
Li Zefan0296e422009-06-26 11:15:37 +0800496 if (idx != 0)
497 rec++;
498
Steven Rostedt493762f2009-03-23 17:12:36 -0400499 if ((void *)rec >= (void *)&pg->records[pg->index]) {
500 pg = pg->next;
501 if (!pg)
502 return NULL;
503 rec = &pg->records[0];
504 if (!rec->counter)
505 goto again;
506 }
507
508 return rec;
509}
510
511static void *function_stat_start(struct tracer_stat *trace)
512{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400513 struct ftrace_profile_stat *stat =
514 container_of(trace, struct ftrace_profile_stat, stat);
515
516 if (!stat || !stat->start)
517 return NULL;
518
519 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400520}
521
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400522#ifdef CONFIG_FUNCTION_GRAPH_TRACER
523/* function graph compares on total time */
524static int function_stat_cmp(void *p1, void *p2)
525{
526 struct ftrace_profile *a = p1;
527 struct ftrace_profile *b = p2;
528
529 if (a->time < b->time)
530 return -1;
531 if (a->time > b->time)
532 return 1;
533 else
534 return 0;
535}
536#else
537/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400538static int function_stat_cmp(void *p1, void *p2)
539{
540 struct ftrace_profile *a = p1;
541 struct ftrace_profile *b = p2;
542
543 if (a->counter < b->counter)
544 return -1;
545 if (a->counter > b->counter)
546 return 1;
547 else
548 return 0;
549}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400550#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400551
552static int function_stat_headers(struct seq_file *m)
553{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400554#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400555 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400556 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400557 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400558 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400559#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400560 seq_printf(m, " Function Hit\n"
561 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400562#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400563 return 0;
564}
565
566static int function_stat_show(struct seq_file *m, void *v)
567{
568 struct ftrace_profile *rec = v;
569 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800570 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400572 static struct trace_seq s;
573 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400574 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400575#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800576 mutex_lock(&ftrace_profile_lock);
577
578 /* we raced with function_profile_reset() */
579 if (unlikely(rec->counter == 0)) {
580 ret = -EBUSY;
581 goto out;
582 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400583
584 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400585 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400586
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400587#ifdef CONFIG_FUNCTION_GRAPH_TRACER
588 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400589 avg = rec->time;
590 do_div(avg, rec->counter);
591
Chase Douglase330b3b2010-04-26 14:02:05 -0400592 /* Sample standard deviation (s^2) */
593 if (rec->counter <= 1)
594 stddev = 0;
595 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200596 /*
597 * Apply Welford's method:
598 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
599 */
600 stddev = rec->counter * rec->time_squared -
601 rec->time * rec->time;
602
Chase Douglase330b3b2010-04-26 14:02:05 -0400603 /*
604 * Divide only 1000 for ns^2 -> us^2 conversion.
605 * trace_print_graph_duration will divide 1000 again.
606 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200607 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400608 }
609
Steven Rostedt34886c82009-03-25 21:00:47 -0400610 trace_seq_init(&s);
611 trace_print_graph_duration(rec->time, &s);
612 trace_seq_puts(&s, " ");
613 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400614 trace_seq_puts(&s, " ");
615 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400616 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400617#endif
618 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800619out:
620 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400621
Li Zefan3aaba202010-08-23 16:50:12 +0800622 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400623}
624
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400626{
627 struct ftrace_profile_page *pg;
628
Steven Rostedtcafb1682009-03-24 20:50:39 -0400629 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400630
631 while (pg) {
632 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
633 pg->index = 0;
634 pg = pg->next;
635 }
636
Steven Rostedtcafb1682009-03-24 20:50:39 -0400637 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400638 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
639}
640
Steven Rostedtcafb1682009-03-24 20:50:39 -0400641int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400642{
643 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400644 int functions;
645 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400646 int i;
647
648 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400650 return 0;
651
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
653 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400654 return -ENOMEM;
655
Steven Rostedt318e0a72009-03-25 20:06:34 -0400656#ifdef CONFIG_DYNAMIC_FTRACE
657 functions = ftrace_update_tot_cnt;
658#else
659 /*
660 * We do not know the number of functions that exist because
661 * dynamic tracing is what counts them. With past experience
662 * we have around 20K functions. That should be more than enough.
663 * It is highly unlikely we will execute every function in
664 * the kernel.
665 */
666 functions = 20000;
667#endif
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400670
Steven Rostedt318e0a72009-03-25 20:06:34 -0400671 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
672
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900673 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400674 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400675 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400676 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400677 pg = pg->next;
678 }
679
680 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400681
682 out_free:
683 pg = stat->start;
684 while (pg) {
685 unsigned long tmp = (unsigned long)pg;
686
687 pg = pg->next;
688 free_page(tmp);
689 }
690
Steven Rostedt318e0a72009-03-25 20:06:34 -0400691 stat->pages = NULL;
692 stat->start = NULL;
693
694 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400695}
696
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400698{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400699 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400700 int size;
701
Steven Rostedtcafb1682009-03-24 20:50:39 -0400702 stat = &per_cpu(ftrace_profile_stats, cpu);
703
704 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400705 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400706 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400707 return 0;
708 }
709
710 /*
711 * We are profiling all functions, but usually only a few thousand
712 * functions are hit. We'll make a hash of 1024 items.
713 */
714 size = FTRACE_PROFILE_HASH_SIZE;
715
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400717
Steven Rostedtcafb1682009-03-24 20:50:39 -0400718 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400719 return -ENOMEM;
720
Steven Rostedt318e0a72009-03-25 20:06:34 -0400721 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 if (ftrace_profile_pages_init(stat) < 0) {
723 kfree(stat->hash);
724 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400725 return -ENOMEM;
726 }
727
728 return 0;
729}
730
Steven Rostedtcafb1682009-03-24 20:50:39 -0400731static int ftrace_profile_init(void)
732{
733 int cpu;
734 int ret = 0;
735
Miao Xiec4602c12013-12-16 15:20:01 +0800736 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400737 ret = ftrace_profile_init_cpu(cpu);
738 if (ret)
739 break;
740 }
741
742 return ret;
743}
744
Steven Rostedt493762f2009-03-23 17:12:36 -0400745/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400746static struct ftrace_profile *
747ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400748{
749 struct ftrace_profile *rec;
750 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400751 unsigned long key;
752
Namhyung Kim20079eb2013-04-10 08:55:50 +0900753 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400755
756 if (hlist_empty(hhd))
757 return NULL;
758
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400759 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400760 if (rec->ip == ip)
761 return rec;
762 }
763
764 return NULL;
765}
766
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767static void ftrace_add_profile(struct ftrace_profile_stat *stat,
768 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400769{
770 unsigned long key;
771
Namhyung Kim20079eb2013-04-10 08:55:50 +0900772 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400774}
775
Steven Rostedt318e0a72009-03-25 20:06:34 -0400776/*
777 * The memory is already allocated, this simply finds a new record to use.
778 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400779static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400780ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400781{
782 struct ftrace_profile *rec = NULL;
783
Steven Rostedt318e0a72009-03-25 20:06:34 -0400784 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400785 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400786 goto out;
787
Steven Rostedt493762f2009-03-23 17:12:36 -0400788 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400789 * Try to find the function again since an NMI
790 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400791 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400794 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400795
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 if (stat->pages->index == PROFILES_PER_PAGE) {
797 if (!stat->pages->next)
798 goto out;
799 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400800 }
801
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400805
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400808
809 return rec;
810}
811
Steven Rostedt493762f2009-03-23 17:12:36 -0400812static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400813function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400814 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400815{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400817 struct ftrace_profile *rec;
818 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400819
820 if (!ftrace_profile_enabled)
821 return;
822
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824
825 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400826 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 goto out;
828
829 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400831 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400832 if (!rec)
833 goto out;
834 }
835
836 rec->counter++;
837 out:
838 local_irq_restore(flags);
839}
840
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400841#ifdef CONFIG_FUNCTION_GRAPH_TRACER
842static int profile_graph_entry(struct ftrace_graph_ent *trace)
843{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400844 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400845 return 1;
846}
847
848static void profile_graph_return(struct ftrace_graph_ret *trace)
849{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400850 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400851 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400852 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400853 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400854
855 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400856 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400857 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400858 goto out;
859
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400860 /* If the calltime was zero'd ignore it */
861 if (!trace->calltime)
862 goto out;
863
Steven Rostedta2a16d62009-03-24 23:17:58 -0400864 calltime = trace->rettime - trace->calltime;
865
866 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
867 int index;
868
869 index = trace->depth;
870
871 /* Append this call time to the parent time to subtract */
872 if (index)
873 current->ret_stack[index - 1].subtime += calltime;
874
875 if (current->ret_stack[index].subtime < calltime)
876 calltime -= current->ret_stack[index].subtime;
877 else
878 calltime = 0;
879 }
880
Steven Rostedtcafb1682009-03-24 20:50:39 -0400881 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400882 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400883 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400884 rec->time_squared += calltime * calltime;
885 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400886
Steven Rostedtcafb1682009-03-24 20:50:39 -0400887 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400888 local_irq_restore(flags);
889}
890
891static int register_ftrace_profiler(void)
892{
893 return register_ftrace_graph(&profile_graph_return,
894 &profile_graph_entry);
895}
896
897static void unregister_ftrace_profiler(void)
898{
899 unregister_ftrace_graph();
900}
901#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100902static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400903 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900904 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
905 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400906};
907
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400908static int register_ftrace_profiler(void)
909{
910 return register_ftrace_function(&ftrace_profile_ops);
911}
912
913static void unregister_ftrace_profiler(void)
914{
915 unregister_ftrace_function(&ftrace_profile_ops);
916}
917#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
918
Steven Rostedt493762f2009-03-23 17:12:36 -0400919static ssize_t
920ftrace_profile_write(struct file *filp, const char __user *ubuf,
921 size_t cnt, loff_t *ppos)
922{
923 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400924 int ret;
925
Peter Huewe22fe9b52011-06-07 21:58:27 +0200926 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
927 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400928 return ret;
929
930 val = !!val;
931
932 mutex_lock(&ftrace_profile_lock);
933 if (ftrace_profile_enabled ^ val) {
934 if (val) {
935 ret = ftrace_profile_init();
936 if (ret < 0) {
937 cnt = ret;
938 goto out;
939 }
940
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400941 ret = register_ftrace_profiler();
942 if (ret < 0) {
943 cnt = ret;
944 goto out;
945 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400946 ftrace_profile_enabled = 1;
947 } else {
948 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400949 /*
950 * unregister_ftrace_profiler calls stop_machine
951 * so this acts like an synchronize_sched.
952 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400953 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400954 }
955 }
956 out:
957 mutex_unlock(&ftrace_profile_lock);
958
Jiri Olsacf8517c2009-10-23 19:36:16 -0400959 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400960
961 return cnt;
962}
963
964static ssize_t
965ftrace_profile_read(struct file *filp, char __user *ubuf,
966 size_t cnt, loff_t *ppos)
967{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400968 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400969 int r;
970
971 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
972 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
973}
974
975static const struct file_operations ftrace_profile_fops = {
976 .open = tracing_open_generic,
977 .read = ftrace_profile_read,
978 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200979 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400980};
981
Steven Rostedtcafb1682009-03-24 20:50:39 -0400982/* used to initialize the real stat files */
983static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400984 .name = "functions",
985 .stat_start = function_stat_start,
986 .stat_next = function_stat_next,
987 .stat_cmp = function_stat_cmp,
988 .stat_headers = function_stat_headers,
989 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400990};
991
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400992static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400993{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400994 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400995 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400996 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400997 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400998 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400999
Steven Rostedtcafb1682009-03-24 20:50:39 -04001000 for_each_possible_cpu(cpu) {
1001 stat = &per_cpu(ftrace_profile_stats, cpu);
1002
1003 /* allocate enough for function name + cpu number */
1004 name = kmalloc(32, GFP_KERNEL);
1005 if (!name) {
1006 /*
1007 * The files created are permanent, if something happens
1008 * we still do not free memory.
1009 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001010 WARN(1,
1011 "Could not allocate stat file for cpu %d\n",
1012 cpu);
1013 return;
1014 }
1015 stat->stat = function_stats;
1016 snprintf(name, 32, "function%d", cpu);
1017 stat->stat.name = name;
1018 ret = register_stat_tracer(&stat->stat);
1019 if (ret) {
1020 WARN(1,
1021 "Could not register function stat for cpu %d\n",
1022 cpu);
1023 kfree(name);
1024 return;
1025 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001026 }
1027
1028 entry = debugfs_create_file("function_profile_enabled", 0644,
1029 d_tracer, NULL, &ftrace_profile_fops);
1030 if (!entry)
1031 pr_warning("Could not create debugfs "
1032 "'function_profile_enabled' entry\n");
1033}
1034
1035#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001036static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001037{
1038}
1039#endif /* CONFIG_FUNCTION_PROFILER */
1040
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001041static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1042
Steven Rostedt3d083392008-05-12 21:20:42 +02001043#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001044
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001045#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001046# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001047#endif
1048
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001049static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1050
Steven Rostedtb6887d72009-02-17 12:32:04 -05001051struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001052 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001053 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001054 unsigned long flags;
1055 unsigned long ip;
1056 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001057 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001058};
1059
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001060struct ftrace_func_entry {
1061 struct hlist_node hlist;
1062 unsigned long ip;
1063};
1064
1065struct ftrace_hash {
1066 unsigned long size_bits;
1067 struct hlist_head *buckets;
1068 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001069 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001070};
1071
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001072/*
1073 * We make these constant because no one should touch them,
1074 * but they are used as the default "empty hash", to avoid allocating
1075 * it all the time. These are in a read only section such that if
1076 * anyone does try to modify it, it will cause an exception.
1077 */
1078static const struct hlist_head empty_buckets[1];
1079static const struct ftrace_hash empty_hash = {
1080 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001081};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001082#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001083
Steven Rostedt2b499382011-05-03 22:49:52 -04001084static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001085 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001086 .notrace_hash = EMPTY_HASH,
1087 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001088 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1089 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001090};
1091
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001092struct ftrace_page {
1093 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001094 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001095 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001096 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001097};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001098
Steven Rostedta7900872011-12-16 16:23:44 -05001099#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1100#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101
1102/* estimate from running different kernels */
1103#define NR_TO_INIT 10000
1104
1105static struct ftrace_page *ftrace_pages_start;
1106static struct ftrace_page *ftrace_pages;
1107
Steven Rostedt06a51d92011-12-19 19:07:36 -05001108static bool ftrace_hash_empty(struct ftrace_hash *hash)
1109{
1110 return !hash || !hash->count;
1111}
1112
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001113static struct ftrace_func_entry *
1114ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1115{
1116 unsigned long key;
1117 struct ftrace_func_entry *entry;
1118 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001119
Steven Rostedt06a51d92011-12-19 19:07:36 -05001120 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001121 return NULL;
1122
1123 if (hash->size_bits > 0)
1124 key = hash_long(ip, hash->size_bits);
1125 else
1126 key = 0;
1127
1128 hhd = &hash->buckets[key];
1129
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001130 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001131 if (entry->ip == ip)
1132 return entry;
1133 }
1134 return NULL;
1135}
1136
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001137static void __add_hash_entry(struct ftrace_hash *hash,
1138 struct ftrace_func_entry *entry)
1139{
1140 struct hlist_head *hhd;
1141 unsigned long key;
1142
1143 if (hash->size_bits)
1144 key = hash_long(entry->ip, hash->size_bits);
1145 else
1146 key = 0;
1147
1148 hhd = &hash->buckets[key];
1149 hlist_add_head(&entry->hlist, hhd);
1150 hash->count++;
1151}
1152
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001153static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1154{
1155 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001156
1157 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1158 if (!entry)
1159 return -ENOMEM;
1160
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001161 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001162 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001163
1164 return 0;
1165}
1166
1167static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001168free_hash_entry(struct ftrace_hash *hash,
1169 struct ftrace_func_entry *entry)
1170{
1171 hlist_del(&entry->hlist);
1172 kfree(entry);
1173 hash->count--;
1174}
1175
1176static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001177remove_hash_entry(struct ftrace_hash *hash,
1178 struct ftrace_func_entry *entry)
1179{
1180 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001181 hash->count--;
1182}
1183
1184static void ftrace_hash_clear(struct ftrace_hash *hash)
1185{
1186 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001187 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001188 struct ftrace_func_entry *entry;
1189 int size = 1 << hash->size_bits;
1190 int i;
1191
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001192 if (!hash->count)
1193 return;
1194
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001195 for (i = 0; i < size; i++) {
1196 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001197 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001198 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199 }
1200 FTRACE_WARN_ON(hash->count);
1201}
1202
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001203static void free_ftrace_hash(struct ftrace_hash *hash)
1204{
1205 if (!hash || hash == EMPTY_HASH)
1206 return;
1207 ftrace_hash_clear(hash);
1208 kfree(hash->buckets);
1209 kfree(hash);
1210}
1211
Steven Rostedt07fd5512011-05-05 18:03:47 -04001212static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1213{
1214 struct ftrace_hash *hash;
1215
1216 hash = container_of(rcu, struct ftrace_hash, rcu);
1217 free_ftrace_hash(hash);
1218}
1219
1220static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1221{
1222 if (!hash || hash == EMPTY_HASH)
1223 return;
1224 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1225}
1226
Jiri Olsa5500fa52012-02-15 15:51:54 +01001227void ftrace_free_filter(struct ftrace_ops *ops)
1228{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001229 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001230 free_ftrace_hash(ops->filter_hash);
1231 free_ftrace_hash(ops->notrace_hash);
1232}
1233
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001234static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1235{
1236 struct ftrace_hash *hash;
1237 int size;
1238
1239 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1240 if (!hash)
1241 return NULL;
1242
1243 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001244 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001245
1246 if (!hash->buckets) {
1247 kfree(hash);
1248 return NULL;
1249 }
1250
1251 hash->size_bits = size_bits;
1252
1253 return hash;
1254}
1255
1256static struct ftrace_hash *
1257alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1258{
1259 struct ftrace_func_entry *entry;
1260 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001261 int size;
1262 int ret;
1263 int i;
1264
1265 new_hash = alloc_ftrace_hash(size_bits);
1266 if (!new_hash)
1267 return NULL;
1268
1269 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001270 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001271 return new_hash;
1272
1273 size = 1 << hash->size_bits;
1274 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001275 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276 ret = add_hash_entry(new_hash, entry->ip);
1277 if (ret < 0)
1278 goto free_hash;
1279 }
1280 }
1281
1282 FTRACE_WARN_ON(new_hash->count != hash->count);
1283
1284 return new_hash;
1285
1286 free_hash:
1287 free_ftrace_hash(new_hash);
1288 return NULL;
1289}
1290
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001291static void
1292ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1293static void
1294ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1295
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001296static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001297ftrace_hash_move(struct ftrace_ops *ops, int enable,
1298 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001299{
1300 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001301 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001303 struct ftrace_hash *old_hash;
1304 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001305 int size = src->count;
1306 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001307 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001308 int i;
1309
1310 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001311 * Remove the current set, update the hash and add
1312 * them back.
1313 */
1314 ftrace_hash_rec_disable(ops, enable);
1315
1316 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001317 * If the new source is empty, just free dst and assign it
1318 * the empty_hash.
1319 */
1320 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001321 free_ftrace_hash_rcu(*dst);
1322 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001323 /* still need to update the function records */
1324 ret = 0;
1325 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326 }
1327
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 /*
1329 * Make the hash size about 1/2 the # found
1330 */
1331 for (size /= 2; size; size >>= 1)
1332 bits++;
1333
1334 /* Don't allocate too much */
1335 if (bits > FTRACE_HASH_MAX_BITS)
1336 bits = FTRACE_HASH_MAX_BITS;
1337
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001338 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001339 new_hash = alloc_ftrace_hash(bits);
1340 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001341 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342
1343 size = 1 << src->size_bits;
1344 for (i = 0; i < size; i++) {
1345 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001346 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001347 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001348 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001349 }
1350 }
1351
Steven Rostedt07fd5512011-05-05 18:03:47 -04001352 old_hash = *dst;
1353 rcu_assign_pointer(*dst, new_hash);
1354 free_ftrace_hash_rcu(old_hash);
1355
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001356 ret = 0;
1357 out:
1358 /*
1359 * Enable regardless of ret:
1360 * On success, we enable the new hash.
1361 * On failure, we re-enable the original hash.
1362 */
1363 ftrace_hash_rec_enable(ops, enable);
1364
1365 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001366}
1367
Steven Rostedt265c8312009-02-13 12:43:56 -05001368/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001369 * Test the hashes for this ops to see if we want to call
1370 * the ops->func or not.
1371 *
1372 * It's a match if the ip is in the ops->filter_hash or
1373 * the filter_hash does not exist or is empty,
1374 * AND
1375 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001376 *
1377 * This needs to be called with preemption disabled as
1378 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001379 */
1380static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001381ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001382{
1383 struct ftrace_hash *filter_hash;
1384 struct ftrace_hash *notrace_hash;
1385 int ret;
1386
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001387#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1388 /*
1389 * There's a small race when adding ops that the ftrace handler
1390 * that wants regs, may be called without them. We can not
1391 * allow that handler to be called if regs is NULL.
1392 */
1393 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1394 return 0;
1395#endif
1396
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001397 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1398 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001399
Steven Rostedt06a51d92011-12-19 19:07:36 -05001400 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001401 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001402 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001403 !ftrace_lookup_ip(notrace_hash, ip)))
1404 ret = 1;
1405 else
1406 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001407
1408 return ret;
1409}
1410
1411/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001412 * This is a double for. Do not use 'break' to break out of the loop,
1413 * you must use a goto.
1414 */
1415#define do_for_each_ftrace_rec(pg, rec) \
1416 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1417 int _____i; \
1418 for (_____i = 0; _____i < pg->index; _____i++) { \
1419 rec = &pg->records[_____i];
1420
1421#define while_for_each_ftrace_rec() \
1422 } \
1423 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301424
Steven Rostedt5855fea2011-12-16 19:27:42 -05001425
1426static int ftrace_cmp_recs(const void *a, const void *b)
1427{
Steven Rostedta650e022012-04-25 13:48:13 -04001428 const struct dyn_ftrace *key = a;
1429 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001430
Steven Rostedta650e022012-04-25 13:48:13 -04001431 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001432 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001433 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1434 return 1;
1435 return 0;
1436}
1437
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001438static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001439{
1440 struct ftrace_page *pg;
1441 struct dyn_ftrace *rec;
1442 struct dyn_ftrace key;
1443
1444 key.ip = start;
1445 key.flags = end; /* overload flags, as it is unsigned long */
1446
1447 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1448 if (end < pg->records[0].ip ||
1449 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1450 continue;
1451 rec = bsearch(&key, pg->records, pg->index,
1452 sizeof(struct dyn_ftrace),
1453 ftrace_cmp_recs);
1454 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001455 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001456 }
1457
Steven Rostedt5855fea2011-12-16 19:27:42 -05001458 return 0;
1459}
1460
Steven Rostedtc88fd862011-08-16 09:53:39 -04001461/**
1462 * ftrace_location - return true if the ip giving is a traced location
1463 * @ip: the instruction pointer to check
1464 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001465 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001466 * That is, the instruction that is either a NOP or call to
1467 * the function tracer. It checks the ftrace internal tables to
1468 * determine if the address belongs or not.
1469 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001470unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001471{
Steven Rostedta650e022012-04-25 13:48:13 -04001472 return ftrace_location_range(ip, ip);
1473}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001474
Steven Rostedta650e022012-04-25 13:48:13 -04001475/**
1476 * ftrace_text_reserved - return true if range contains an ftrace location
1477 * @start: start of range to search
1478 * @end: end of range to search (inclusive). @end points to the last byte to check.
1479 *
1480 * Returns 1 if @start and @end contains a ftrace location.
1481 * That is, the instruction that is either a NOP or call to
1482 * the function tracer. It checks the ftrace internal tables to
1483 * determine if the address belongs or not.
1484 */
Sasha Levind88471c2013-01-09 18:09:20 -05001485int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001486{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001487 unsigned long ret;
1488
1489 ret = ftrace_location_range((unsigned long)start,
1490 (unsigned long)end);
1491
1492 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001493}
1494
Steven Rostedted926f92011-05-03 13:25:24 -04001495static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1496 int filter_hash,
1497 bool inc)
1498{
1499 struct ftrace_hash *hash;
1500 struct ftrace_hash *other_hash;
1501 struct ftrace_page *pg;
1502 struct dyn_ftrace *rec;
1503 int count = 0;
1504 int all = 0;
1505
1506 /* Only update if the ops has been registered */
1507 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1508 return;
1509
1510 /*
1511 * In the filter_hash case:
1512 * If the count is zero, we update all records.
1513 * Otherwise we just update the items in the hash.
1514 *
1515 * In the notrace_hash case:
1516 * We enable the update in the hash.
1517 * As disabling notrace means enabling the tracing,
1518 * and enabling notrace means disabling, the inc variable
1519 * gets inversed.
1520 */
1521 if (filter_hash) {
1522 hash = ops->filter_hash;
1523 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001524 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001525 all = 1;
1526 } else {
1527 inc = !inc;
1528 hash = ops->notrace_hash;
1529 other_hash = ops->filter_hash;
1530 /*
1531 * If the notrace hash has no items,
1532 * then there's nothing to do.
1533 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001534 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001535 return;
1536 }
1537
1538 do_for_each_ftrace_rec(pg, rec) {
1539 int in_other_hash = 0;
1540 int in_hash = 0;
1541 int match = 0;
1542
1543 if (all) {
1544 /*
1545 * Only the filter_hash affects all records.
1546 * Update if the record is not in the notrace hash.
1547 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001548 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001549 match = 1;
1550 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001551 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1552 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001553
1554 /*
1555 *
1556 */
1557 if (filter_hash && in_hash && !in_other_hash)
1558 match = 1;
1559 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001560 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001561 match = 1;
1562 }
1563 if (!match)
1564 continue;
1565
1566 if (inc) {
1567 rec->flags++;
1568 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1569 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001570 /*
1571 * If any ops wants regs saved for this function
1572 * then all ops will get saved regs.
1573 */
1574 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1575 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001576 } else {
1577 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1578 return;
1579 rec->flags--;
1580 }
1581 count++;
1582 /* Shortcut, if we handled all records, we are done. */
1583 if (!all && count == hash->count)
1584 return;
1585 } while_for_each_ftrace_rec();
1586}
1587
1588static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1589 int filter_hash)
1590{
1591 __ftrace_hash_rec_update(ops, filter_hash, 0);
1592}
1593
1594static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1595 int filter_hash)
1596{
1597 __ftrace_hash_rec_update(ops, filter_hash, 1);
1598}
1599
Steven Rostedt05736a42008-09-22 14:55:47 -07001600static void print_ip_ins(const char *fmt, unsigned char *p)
1601{
1602 int i;
1603
1604 printk(KERN_CONT "%s", fmt);
1605
1606 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1607 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1608}
1609
Steven Rostedtc88fd862011-08-16 09:53:39 -04001610/**
1611 * ftrace_bug - report and shutdown function tracer
1612 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1613 * @ip: The address that failed
1614 *
1615 * The arch code that enables or disables the function tracing
1616 * can call ftrace_bug() when it has detected a problem in
1617 * modifying the code. @failed should be one of either:
1618 * EFAULT - if the problem happens on reading the @ip address
1619 * EINVAL - if what is read at @ip is not what was expected
1620 * EPERM - if the problem happens on writting to the @ip address
1621 */
1622void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001623{
1624 switch (failed) {
1625 case -EFAULT:
1626 FTRACE_WARN_ON_ONCE(1);
1627 pr_info("ftrace faulted on modifying ");
1628 print_ip_sym(ip);
1629 break;
1630 case -EINVAL:
1631 FTRACE_WARN_ON_ONCE(1);
1632 pr_info("ftrace failed to modify ");
1633 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001634 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001635 printk(KERN_CONT "\n");
1636 break;
1637 case -EPERM:
1638 FTRACE_WARN_ON_ONCE(1);
1639 pr_info("ftrace faulted on writing ");
1640 print_ip_sym(ip);
1641 break;
1642 default:
1643 FTRACE_WARN_ON_ONCE(1);
1644 pr_info("ftrace faulted on unknown error ");
1645 print_ip_sym(ip);
1646 }
1647}
1648
Steven Rostedtc88fd862011-08-16 09:53:39 -04001649static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001650{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001651 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001652
Steven Rostedt982c3502008-11-15 16:31:41 -05001653 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001654 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001655 *
Steven Rostedted926f92011-05-03 13:25:24 -04001656 * If the record has a ref count, then we need to enable it
1657 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001658 *
Steven Rostedted926f92011-05-03 13:25:24 -04001659 * Otherwise we make sure its disabled.
1660 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001661 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001662 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001663 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001664 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001665 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001666
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001667 /*
1668 * If enabling and the REGS flag does not match the REGS_EN, then
1669 * do not ignore this record. Set flags to fail the compare against
1670 * ENABLED.
1671 */
1672 if (flag &&
1673 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1674 flag |= FTRACE_FL_REGS;
1675
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001676 /* If the state of this record hasn't changed, then do nothing */
1677 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001678 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001679
1680 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001681 /* Save off if rec is being enabled (for return value) */
1682 flag ^= rec->flags & FTRACE_FL_ENABLED;
1683
1684 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001685 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001686 if (flag & FTRACE_FL_REGS) {
1687 if (rec->flags & FTRACE_FL_REGS)
1688 rec->flags |= FTRACE_FL_REGS_EN;
1689 else
1690 rec->flags &= ~FTRACE_FL_REGS_EN;
1691 }
1692 }
1693
1694 /*
1695 * If this record is being updated from a nop, then
1696 * return UPDATE_MAKE_CALL.
1697 * Otherwise, if the EN flag is set, then return
1698 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1699 * from the non-save regs, to a save regs function.
1700 * Otherwise,
1701 * return UPDATE_MODIFY_CALL to tell the caller to convert
1702 * from the save regs, to a non-save regs function.
1703 */
1704 if (flag & FTRACE_FL_ENABLED)
1705 return FTRACE_UPDATE_MAKE_CALL;
1706 else if (rec->flags & FTRACE_FL_REGS_EN)
1707 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1708 else
1709 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001710 }
1711
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001712 if (update) {
1713 /* If there's no more users, clear all flags */
1714 if (!(rec->flags & ~FTRACE_FL_MASK))
1715 rec->flags = 0;
1716 else
1717 /* Just disable the record (keep REGS state) */
1718 rec->flags &= ~FTRACE_FL_ENABLED;
1719 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001720
1721 return FTRACE_UPDATE_MAKE_NOP;
1722}
1723
1724/**
1725 * ftrace_update_record, set a record that now is tracing or not
1726 * @rec: the record to update
1727 * @enable: set to 1 if the record is tracing, zero to force disable
1728 *
1729 * The records that represent all functions that can be traced need
1730 * to be updated when tracing has been enabled.
1731 */
1732int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1733{
1734 return ftrace_check_record(rec, enable, 1);
1735}
1736
1737/**
1738 * ftrace_test_record, check if the record has been enabled or not
1739 * @rec: the record to test
1740 * @enable: set to 1 to check if enabled, 0 if it is disabled
1741 *
1742 * The arch code may need to test if a record is already set to
1743 * tracing to determine how to modify the function code that it
1744 * represents.
1745 */
1746int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1747{
1748 return ftrace_check_record(rec, enable, 0);
1749}
1750
1751static int
1752__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1753{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001754 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001755 unsigned long ftrace_addr;
1756 int ret;
1757
Steven Rostedtc88fd862011-08-16 09:53:39 -04001758 ret = ftrace_update_record(rec, enable);
1759
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001760 if (rec->flags & FTRACE_FL_REGS)
1761 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1762 else
1763 ftrace_addr = (unsigned long)FTRACE_ADDR;
1764
Steven Rostedtc88fd862011-08-16 09:53:39 -04001765 switch (ret) {
1766 case FTRACE_UPDATE_IGNORE:
1767 return 0;
1768
1769 case FTRACE_UPDATE_MAKE_CALL:
1770 return ftrace_make_call(rec, ftrace_addr);
1771
1772 case FTRACE_UPDATE_MAKE_NOP:
1773 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001774
1775 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1776 case FTRACE_UPDATE_MODIFY_CALL:
1777 if (rec->flags & FTRACE_FL_REGS)
1778 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1779 else
1780 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1781
1782 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001783 }
1784
1785 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001786}
1787
Steven Rostedte4f5d542012-04-27 09:13:18 -04001788void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001789{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001790 struct dyn_ftrace *rec;
1791 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001792 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001793
Steven Rostedt45a4a232011-04-21 23:16:46 -04001794 if (unlikely(ftrace_disabled))
1795 return;
1796
Steven Rostedt265c8312009-02-13 12:43:56 -05001797 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001798 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001799 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001800 ftrace_bug(failed, rec->ip);
1801 /* Stop processing */
1802 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001803 }
1804 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001805}
1806
Steven Rostedtc88fd862011-08-16 09:53:39 -04001807struct ftrace_rec_iter {
1808 struct ftrace_page *pg;
1809 int index;
1810};
1811
1812/**
1813 * ftrace_rec_iter_start, start up iterating over traced functions
1814 *
1815 * Returns an iterator handle that is used to iterate over all
1816 * the records that represent address locations where functions
1817 * are traced.
1818 *
1819 * May return NULL if no records are available.
1820 */
1821struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1822{
1823 /*
1824 * We only use a single iterator.
1825 * Protected by the ftrace_lock mutex.
1826 */
1827 static struct ftrace_rec_iter ftrace_rec_iter;
1828 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1829
1830 iter->pg = ftrace_pages_start;
1831 iter->index = 0;
1832
1833 /* Could have empty pages */
1834 while (iter->pg && !iter->pg->index)
1835 iter->pg = iter->pg->next;
1836
1837 if (!iter->pg)
1838 return NULL;
1839
1840 return iter;
1841}
1842
1843/**
1844 * ftrace_rec_iter_next, get the next record to process.
1845 * @iter: The handle to the iterator.
1846 *
1847 * Returns the next iterator after the given iterator @iter.
1848 */
1849struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1850{
1851 iter->index++;
1852
1853 if (iter->index >= iter->pg->index) {
1854 iter->pg = iter->pg->next;
1855 iter->index = 0;
1856
1857 /* Could have empty pages */
1858 while (iter->pg && !iter->pg->index)
1859 iter->pg = iter->pg->next;
1860 }
1861
1862 if (!iter->pg)
1863 return NULL;
1864
1865 return iter;
1866}
1867
1868/**
1869 * ftrace_rec_iter_record, get the record at the iterator location
1870 * @iter: The current iterator location
1871 *
1872 * Returns the record that the current @iter is at.
1873 */
1874struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1875{
1876 return &iter->pg->records[iter->index];
1877}
1878
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301879static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001880ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001881{
1882 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001883 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001884
1885 ip = rec->ip;
1886
Steven Rostedt45a4a232011-04-21 23:16:46 -04001887 if (unlikely(ftrace_disabled))
1888 return 0;
1889
Shaohua Li25aac9d2009-01-09 11:29:40 +08001890 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001891 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001892 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301893 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001894 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301895 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001896}
1897
Steven Rostedt000ab692009-02-17 13:35:06 -05001898/*
1899 * archs can override this function if they must do something
1900 * before the modifying code is performed.
1901 */
1902int __weak ftrace_arch_code_modify_prepare(void)
1903{
1904 return 0;
1905}
1906
1907/*
1908 * archs can override this function if they must do something
1909 * after the modifying code is performed.
1910 */
1911int __weak ftrace_arch_code_modify_post_process(void)
1912{
1913 return 0;
1914}
1915
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001916void ftrace_modify_all_code(int command)
1917{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001918 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01001919 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001920
1921 /*
1922 * If the ftrace_caller calls a ftrace_ops func directly,
1923 * we need to make sure that it only traces functions it
1924 * expects to trace. When doing the switch of functions,
1925 * we need to update to the ftrace_ops_list_func first
1926 * before the transition between old and new calls are set,
1927 * as the ftrace_ops_list_func will check the ops hashes
1928 * to make sure the ops are having the right functions
1929 * traced.
1930 */
Petr Mladekcd210672014-02-24 17:12:21 +01001931 if (update) {
1932 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
1933 if (FTRACE_WARN_ON(err))
1934 return;
1935 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001936
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001937 if (command & FTRACE_UPDATE_CALLS)
1938 ftrace_replace_code(1);
1939 else if (command & FTRACE_DISABLE_CALLS)
1940 ftrace_replace_code(0);
1941
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05001942 if (update && ftrace_trace_function != ftrace_ops_list_func) {
1943 function_trace_op = set_function_trace_op;
1944 smp_wmb();
1945 /* If irqs are disabled, we are in stop machine */
1946 if (!irqs_disabled())
1947 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01001948 err = ftrace_update_ftrace_func(ftrace_trace_function);
1949 if (FTRACE_WARN_ON(err))
1950 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05001951 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001952
1953 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01001954 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001955 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01001956 err = ftrace_disable_ftrace_graph_caller();
1957 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001958}
1959
Ingo Molnare309b412008-05-12 21:20:51 +02001960static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001961{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001962 int *command = data;
1963
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001964 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001965
Steven Rostedtc88fd862011-08-16 09:53:39 -04001966 return 0;
1967}
1968
1969/**
1970 * ftrace_run_stop_machine, go back to the stop machine method
1971 * @command: The command to tell ftrace what to do
1972 *
1973 * If an arch needs to fall back to the stop machine method, the
1974 * it can call this function.
1975 */
1976void ftrace_run_stop_machine(int command)
1977{
1978 stop_machine(__ftrace_modify_code, &command, NULL);
1979}
1980
1981/**
1982 * arch_ftrace_update_code, modify the code to trace or not trace
1983 * @command: The command that needs to be done
1984 *
1985 * Archs can override this function if it does not need to
1986 * run stop_machine() to modify code.
1987 */
1988void __weak arch_ftrace_update_code(int command)
1989{
1990 ftrace_run_stop_machine(command);
1991}
1992
1993static void ftrace_run_update_code(int command)
1994{
1995 int ret;
1996
1997 ret = ftrace_arch_code_modify_prepare();
1998 FTRACE_WARN_ON(ret);
1999 if (ret)
2000 return;
2001 /*
2002 * Do not call function tracer while we update the code.
2003 * We are in stop machine.
2004 */
2005 function_trace_stop++;
2006
2007 /*
2008 * By default we use stop_machine() to modify the code.
2009 * But archs can do what ever they want as long as it
2010 * is safe. The stop_machine() is the safest, but also
2011 * produces the most overhead.
2012 */
2013 arch_ftrace_update_code(command);
2014
Steven Rostedt6331c282011-07-13 15:11:02 -04002015 function_trace_stop--;
2016
Steven Rostedt000ab692009-02-17 13:35:06 -05002017 ret = ftrace_arch_code_modify_post_process();
2018 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002019}
2020
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002021static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002022static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002023static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002024
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002025static void control_ops_free(struct ftrace_ops *ops)
2026{
2027 free_percpu(ops->disabled);
2028}
2029
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002030static void ftrace_startup_enable(int command)
2031{
2032 if (saved_ftrace_func != ftrace_trace_function) {
2033 saved_ftrace_func = ftrace_trace_function;
2034 command |= FTRACE_UPDATE_TRACE_FUNC;
2035 }
2036
2037 if (!command || !ftrace_enabled)
2038 return;
2039
2040 ftrace_run_update_code(command);
2041}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002042
Steven Rostedta1cd6172011-05-23 15:24:25 -04002043static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002044{
Steven Rostedtb8489142011-05-04 09:27:52 -04002045 bool hash_enable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002046 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002047
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002048 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002049 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002050
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002051 ret = __register_ftrace_function(ops);
2052 if (ret)
2053 return ret;
2054
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002055 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002056 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002057
Steven Rostedted926f92011-05-03 13:25:24 -04002058 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002059 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002060 ftrace_hash_rec_enable(ops, 1);
2061
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002062 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002063
2064 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002065}
2066
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002067static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002068{
Steven Rostedtb8489142011-05-04 09:27:52 -04002069 bool hash_disable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002070 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002071
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002072 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002073 return -ENODEV;
2074
2075 ret = __unregister_ftrace_function(ops);
2076 if (ret)
2077 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002078
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002079 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002080 /*
2081 * Just warn in case of unbalance, no need to kill ftrace, it's not
2082 * critical but the ftrace_call callers may be never nopped again after
2083 * further ftrace uses.
2084 */
2085 WARN_ON_ONCE(ftrace_start_up < 0);
2086
Steven Rostedtb8489142011-05-04 09:27:52 -04002087 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002088 ftrace_hash_rec_disable(ops, 1);
2089
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05002090 if (!global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002091 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002092
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002093 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002094
2095 if (saved_ftrace_func != ftrace_trace_function) {
2096 saved_ftrace_func = ftrace_trace_function;
2097 command |= FTRACE_UPDATE_TRACE_FUNC;
2098 }
2099
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002100 if (!command || !ftrace_enabled) {
2101 /*
2102 * If these are control ops, they still need their
2103 * per_cpu field freed. Since, function tracing is
2104 * not currently active, we can just free them
2105 * without synchronizing all CPUs.
2106 */
2107 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2108 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002109 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002110 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002111
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002112 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002113
2114 /*
2115 * Dynamic ops may be freed, we must make sure that all
2116 * callers are done before leaving this function.
2117 * The same goes for freeing the per_cpu data of the control
2118 * ops.
2119 *
2120 * Again, normal synchronize_sched() is not good enough.
2121 * We need to do a hard force of sched synchronization.
2122 * This is because we use preempt_disable() to do RCU, but
2123 * the function tracers can be called where RCU is not watching
2124 * (like before user_exit()). We can not rely on the RCU
2125 * infrastructure to do the synchronization, thus we must do it
2126 * ourselves.
2127 */
2128 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2129 schedule_on_each_cpu(ftrace_sync);
2130
2131 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2132 control_ops_free(ops);
2133 }
2134
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002135 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002136}
2137
Ingo Molnare309b412008-05-12 21:20:51 +02002138static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002139{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002140 if (unlikely(ftrace_disabled))
2141 return;
2142
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002143 /* Force update next time */
2144 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002145 /* ftrace_start_up is true if we want ftrace running */
2146 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002147 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002148}
2149
Ingo Molnare309b412008-05-12 21:20:51 +02002150static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002151{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002152 if (unlikely(ftrace_disabled))
2153 return;
2154
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002155 /* ftrace_start_up is true if ftrace is running */
2156 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002157 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002158}
2159
Steven Rostedt3d083392008-05-12 21:20:42 +02002160static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002161unsigned long ftrace_update_tot_cnt;
2162
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002163static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002164{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002165 /*
2166 * Filter_hash being empty will default to trace module.
2167 * But notrace hash requires a test of individual module functions.
2168 */
2169 return ftrace_hash_empty(ops->filter_hash) &&
2170 ftrace_hash_empty(ops->notrace_hash);
2171}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002172
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002173/*
2174 * Check if the current ops references the record.
2175 *
2176 * If the ops traces all functions, then it was already accounted for.
2177 * If the ops does not trace the current record function, skip it.
2178 * If the ops ignores the function via notrace filter, skip it.
2179 */
2180static inline bool
2181ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2182{
2183 /* If ops isn't enabled, ignore it */
2184 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2185 return 0;
2186
2187 /* If ops traces all mods, we already accounted for it */
2188 if (ops_traces_mod(ops))
2189 return 0;
2190
2191 /* The function must be in the filter */
2192 if (!ftrace_hash_empty(ops->filter_hash) &&
2193 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2194 return 0;
2195
2196 /* If in notrace hash, we ignore it too */
2197 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2198 return 0;
2199
2200 return 1;
2201}
2202
2203static int referenced_filters(struct dyn_ftrace *rec)
2204{
2205 struct ftrace_ops *ops;
2206 int cnt = 0;
2207
2208 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2209 if (ops_references_rec(ops, rec))
2210 cnt++;
2211 }
2212
2213 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002214}
2215
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002216static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002217{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002218 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002219 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302220 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002221 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002222 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002223 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002224 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002225
2226 /*
2227 * When adding a module, we need to check if tracers are
2228 * currently enabled and if they are set to trace all functions.
2229 * If they are, we need to enable the module functions as well
2230 * as update the reference counts for those function records.
2231 */
2232 if (mod) {
2233 struct ftrace_ops *ops;
2234
2235 for (ops = ftrace_ops_list;
2236 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002237 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2238 if (ops_traces_mod(ops))
2239 ref++;
2240 else
2241 test = true;
2242 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002243 }
2244 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002245
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002246 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002247
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002248 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302249
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002250 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002251 int cnt = ref;
2252
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002253 /* If something went wrong, bail without enabling anything */
2254 if (unlikely(ftrace_disabled))
2255 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002256
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002257 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002258 if (test)
2259 cnt += referenced_filters(p);
2260 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302261
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002262 /*
2263 * Do the initial record conversion from mcount jump
2264 * to the NOP instructions.
2265 */
2266 if (!ftrace_code_disable(mod, p))
2267 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002268
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002269 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002270
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002271 /*
2272 * If the tracing is enabled, go ahead and enable the record.
2273 *
2274 * The reason not to enable the record immediatelly is the
2275 * inherent check of ftrace_make_nop/ftrace_make_call for
2276 * correct previous instructions. Making first the NOP
2277 * conversion puts the module to the correct state, thus
2278 * passing the ftrace_make_call check.
2279 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002280 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002281 int failed = __ftrace_replace_code(p, 1);
2282 if (failed)
2283 ftrace_bug(failed, p->ip);
2284 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002285 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002286 }
2287
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002288 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002289 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002290 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002291
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002292 return 0;
2293}
2294
Steven Rostedta7900872011-12-16 16:23:44 -05002295static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002296{
Steven Rostedta7900872011-12-16 16:23:44 -05002297 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002298 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002299
Steven Rostedta7900872011-12-16 16:23:44 -05002300 if (WARN_ON(!count))
2301 return -EINVAL;
2302
2303 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002304
2305 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002306 * We want to fill as much as possible. No more than a page
2307 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002308 */
Steven Rostedta7900872011-12-16 16:23:44 -05002309 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2310 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002311
Steven Rostedta7900872011-12-16 16:23:44 -05002312 again:
2313 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2314
2315 if (!pg->records) {
2316 /* if we can't allocate this size, try something smaller */
2317 if (!order)
2318 return -ENOMEM;
2319 order >>= 1;
2320 goto again;
2321 }
2322
2323 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2324 pg->size = cnt;
2325
2326 if (cnt > count)
2327 cnt = count;
2328
2329 return cnt;
2330}
2331
2332static struct ftrace_page *
2333ftrace_allocate_pages(unsigned long num_to_init)
2334{
2335 struct ftrace_page *start_pg;
2336 struct ftrace_page *pg;
2337 int order;
2338 int cnt;
2339
2340 if (!num_to_init)
2341 return 0;
2342
2343 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2344 if (!pg)
2345 return NULL;
2346
2347 /*
2348 * Try to allocate as much as possible in one continues
2349 * location that fills in all of the space. We want to
2350 * waste as little space as possible.
2351 */
2352 for (;;) {
2353 cnt = ftrace_allocate_records(pg, num_to_init);
2354 if (cnt < 0)
2355 goto free_pages;
2356
2357 num_to_init -= cnt;
2358 if (!num_to_init)
2359 break;
2360
2361 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2362 if (!pg->next)
2363 goto free_pages;
2364
2365 pg = pg->next;
2366 }
2367
2368 return start_pg;
2369
2370 free_pages:
2371 while (start_pg) {
2372 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2373 free_pages((unsigned long)pg->records, order);
2374 start_pg = pg->next;
2375 kfree(pg);
2376 pg = start_pg;
2377 }
2378 pr_info("ftrace: FAILED to allocate memory for functions\n");
2379 return NULL;
2380}
2381
Steven Rostedt5072c592008-05-12 21:20:43 +02002382#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2383
2384struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002385 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002386 loff_t func_pos;
2387 struct ftrace_page *pg;
2388 struct dyn_ftrace *func;
2389 struct ftrace_func_probe *probe;
2390 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002391 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002392 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002393 int hidx;
2394 int idx;
2395 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002396};
2397
Ingo Molnare309b412008-05-12 21:20:51 +02002398static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002399t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002400{
2401 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002402 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002403 struct hlist_head *hhd;
2404
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002405 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002406 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002407
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002408 if (iter->probe)
2409 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002410 retry:
2411 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2412 return NULL;
2413
2414 hhd = &ftrace_func_hash[iter->hidx];
2415
2416 if (hlist_empty(hhd)) {
2417 iter->hidx++;
2418 hnd = NULL;
2419 goto retry;
2420 }
2421
2422 if (!hnd)
2423 hnd = hhd->first;
2424 else {
2425 hnd = hnd->next;
2426 if (!hnd) {
2427 iter->hidx++;
2428 goto retry;
2429 }
2430 }
2431
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002432 if (WARN_ON_ONCE(!hnd))
2433 return NULL;
2434
2435 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2436
2437 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002438}
2439
2440static void *t_hash_start(struct seq_file *m, loff_t *pos)
2441{
2442 struct ftrace_iterator *iter = m->private;
2443 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002444 loff_t l;
2445
Steven Rostedt69a30832011-12-19 15:21:16 -05002446 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2447 return NULL;
2448
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002449 if (iter->func_pos > *pos)
2450 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002451
Li Zefand82d6242009-06-24 09:54:54 +08002452 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002453 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002454 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002455 if (!p)
2456 break;
2457 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002458 if (!p)
2459 return NULL;
2460
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002461 /* Only set this if we have an item */
2462 iter->flags |= FTRACE_ITER_HASH;
2463
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002464 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002465}
2466
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002467static int
2468t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002469{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002470 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002471
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002472 rec = iter->probe;
2473 if (WARN_ON_ONCE(!rec))
2474 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002475
Steven Rostedt809dcf22009-02-16 23:06:01 -05002476 if (rec->ops->print)
2477 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2478
Steven Rostedtb375a112009-09-17 00:05:58 -04002479 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002480
2481 if (rec->data)
2482 seq_printf(m, ":%p", rec->data);
2483 seq_putc(m, '\n');
2484
2485 return 0;
2486}
2487
2488static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002489t_next(struct seq_file *m, void *v, loff_t *pos)
2490{
2491 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002492 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002493 struct dyn_ftrace *rec = NULL;
2494
Steven Rostedt45a4a232011-04-21 23:16:46 -04002495 if (unlikely(ftrace_disabled))
2496 return NULL;
2497
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002498 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002499 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002500
Steven Rostedt5072c592008-05-12 21:20:43 +02002501 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002502 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002503
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002504 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002505 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002506
Steven Rostedt5072c592008-05-12 21:20:43 +02002507 retry:
2508 if (iter->idx >= iter->pg->index) {
2509 if (iter->pg->next) {
2510 iter->pg = iter->pg->next;
2511 iter->idx = 0;
2512 goto retry;
2513 }
2514 } else {
2515 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002516 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002517 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002518
Steven Rostedt41c52c02008-05-22 11:46:33 -04002519 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002520 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2521
2522 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002523 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002524
Steven Rostedt5072c592008-05-12 21:20:43 +02002525 rec = NULL;
2526 goto retry;
2527 }
2528 }
2529
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002530 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002531 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002532
2533 iter->func = rec;
2534
2535 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002536}
2537
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002538static void reset_iter_read(struct ftrace_iterator *iter)
2539{
2540 iter->pos = 0;
2541 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002542 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002543}
2544
2545static void *t_start(struct seq_file *m, loff_t *pos)
2546{
2547 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002548 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002549 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002550 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002551
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002552 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002553
2554 if (unlikely(ftrace_disabled))
2555 return NULL;
2556
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002557 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002558 * If an lseek was done, then reset and start from beginning.
2559 */
2560 if (*pos < iter->pos)
2561 reset_iter_read(iter);
2562
2563 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002564 * For set_ftrace_filter reading, if we have the filter
2565 * off, we can short cut and just print out that all
2566 * functions are enabled.
2567 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002568 if (iter->flags & FTRACE_ITER_FILTER &&
2569 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002570 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002571 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002572 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002573 /* reset in case of seek/pread */
2574 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002575 return iter;
2576 }
2577
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002578 if (iter->flags & FTRACE_ITER_HASH)
2579 return t_hash_start(m, pos);
2580
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002581 /*
2582 * Unfortunately, we need to restart at ftrace_pages_start
2583 * every time we let go of the ftrace_mutex. This is because
2584 * those pointers can change without the lock.
2585 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002586 iter->pg = ftrace_pages_start;
2587 iter->idx = 0;
2588 for (l = 0; l <= *pos; ) {
2589 p = t_next(m, p, &l);
2590 if (!p)
2591 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002592 }
walimis5821e1b2008-11-15 15:19:06 +08002593
Steven Rostedt69a30832011-12-19 15:21:16 -05002594 if (!p)
2595 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002596
2597 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002598}
2599
2600static void t_stop(struct seq_file *m, void *p)
2601{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002602 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002603}
2604
2605static int t_show(struct seq_file *m, void *v)
2606{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002607 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002608 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002609
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002610 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002611 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002612
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002613 if (iter->flags & FTRACE_ITER_PRINTALL) {
2614 seq_printf(m, "#### all functions enabled ####\n");
2615 return 0;
2616 }
2617
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002618 rec = iter->func;
2619
Steven Rostedt5072c592008-05-12 21:20:43 +02002620 if (!rec)
2621 return 0;
2622
Steven Rostedt647bcd02011-05-03 14:39:21 -04002623 seq_printf(m, "%ps", (void *)rec->ip);
2624 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002625 seq_printf(m, " (%ld)%s",
2626 rec->flags & ~FTRACE_FL_MASK,
2627 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002628 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002629
2630 return 0;
2631}
2632
James Morris88e9d342009-09-22 16:43:43 -07002633static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002634 .start = t_start,
2635 .next = t_next,
2636 .stop = t_stop,
2637 .show = t_show,
2638};
2639
Ingo Molnare309b412008-05-12 21:20:51 +02002640static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002641ftrace_avail_open(struct inode *inode, struct file *file)
2642{
2643 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002644
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002645 if (unlikely(ftrace_disabled))
2646 return -ENODEV;
2647
Jiri Olsa50e18b92012-04-25 10:23:39 +02002648 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2649 if (iter) {
2650 iter->pg = ftrace_pages_start;
2651 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002652 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002653
Jiri Olsa50e18b92012-04-25 10:23:39 +02002654 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002655}
2656
Steven Rostedt647bcd02011-05-03 14:39:21 -04002657static int
2658ftrace_enabled_open(struct inode *inode, struct file *file)
2659{
2660 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002661
2662 if (unlikely(ftrace_disabled))
2663 return -ENODEV;
2664
Jiri Olsa50e18b92012-04-25 10:23:39 +02002665 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2666 if (iter) {
2667 iter->pg = ftrace_pages_start;
2668 iter->flags = FTRACE_ITER_ENABLED;
2669 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002670 }
2671
Jiri Olsa50e18b92012-04-25 10:23:39 +02002672 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002673}
2674
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002675static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002676{
Steven Rostedt52baf112009-02-14 01:15:39 -05002677 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002678 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002679 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002680}
2681
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002682/**
2683 * ftrace_regex_open - initialize function tracer filter files
2684 * @ops: The ftrace_ops that hold the hash filters
2685 * @flag: The type of filter to process
2686 * @inode: The inode, usually passed in to your open routine
2687 * @file: The file, usually passed in to your open routine
2688 *
2689 * ftrace_regex_open() initializes the filter files for the
2690 * @ops. Depending on @flag it may process the filter hash or
2691 * the notrace hash of @ops. With this called from the open
2692 * routine, you can use ftrace_filter_write() for the write
2693 * routine if @flag has FTRACE_ITER_FILTER set, or
2694 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002695 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002696 * release must call ftrace_regex_release().
2697 */
2698int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002699ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002700 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002701{
2702 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002703 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002704 int ret = 0;
2705
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002706 ftrace_ops_init(ops);
2707
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002708 if (unlikely(ftrace_disabled))
2709 return -ENODEV;
2710
Steven Rostedt5072c592008-05-12 21:20:43 +02002711 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2712 if (!iter)
2713 return -ENOMEM;
2714
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002715 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2716 kfree(iter);
2717 return -ENOMEM;
2718 }
2719
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002720 iter->ops = ops;
2721 iter->flags = flag;
2722
2723 mutex_lock(&ops->regex_lock);
2724
Steven Rostedtf45948e2011-05-02 12:29:25 -04002725 if (flag & FTRACE_ITER_NOTRACE)
2726 hash = ops->notrace_hash;
2727 else
2728 hash = ops->filter_hash;
2729
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002730 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002731 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002732 if (!iter->hash) {
2733 trace_parser_put(&iter->parser);
2734 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002735 ret = -ENOMEM;
2736 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002737 }
2738 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002739
Steven Rostedt5072c592008-05-12 21:20:43 +02002740 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002741 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002742 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002743
2744 if (file->f_mode & FMODE_READ) {
2745 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002746
2747 ret = seq_open(file, &show_ftrace_seq_ops);
2748 if (!ret) {
2749 struct seq_file *m = file->private_data;
2750 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002751 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002752 /* Failed */
2753 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002754 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002755 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002756 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002757 } else
2758 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002759
2760 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002761 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002762
2763 return ret;
2764}
2765
Steven Rostedt41c52c02008-05-22 11:46:33 -04002766static int
2767ftrace_filter_open(struct inode *inode, struct file *file)
2768{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002769 struct ftrace_ops *ops = inode->i_private;
2770
2771 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05002772 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2773 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002774}
2775
2776static int
2777ftrace_notrace_open(struct inode *inode, struct file *file)
2778{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002779 struct ftrace_ops *ops = inode->i_private;
2780
2781 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002782 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002783}
2784
Steven Rostedt64e7c442009-02-13 17:08:48 -05002785static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002786{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002787 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002788 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002789
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002790 switch (type) {
2791 case MATCH_FULL:
2792 if (strcmp(str, regex) == 0)
2793 matched = 1;
2794 break;
2795 case MATCH_FRONT_ONLY:
2796 if (strncmp(str, regex, len) == 0)
2797 matched = 1;
2798 break;
2799 case MATCH_MIDDLE_ONLY:
2800 if (strstr(str, regex))
2801 matched = 1;
2802 break;
2803 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002804 slen = strlen(str);
2805 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002806 matched = 1;
2807 break;
2808 }
2809
2810 return matched;
2811}
2812
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002813static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002814enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002815{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002816 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002817 int ret = 0;
2818
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002819 entry = ftrace_lookup_ip(hash, rec->ip);
2820 if (not) {
2821 /* Do nothing if it doesn't exist */
2822 if (!entry)
2823 return 0;
2824
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002825 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002826 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002827 /* Do nothing if it exists */
2828 if (entry)
2829 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002830
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002831 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002832 }
2833 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002834}
2835
Steven Rostedt64e7c442009-02-13 17:08:48 -05002836static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002837ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2838 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002839{
2840 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002841 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002842
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002843 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2844
2845 if (mod) {
2846 /* module lookup requires matching the module */
2847 if (!modname || strcmp(modname, mod))
2848 return 0;
2849
2850 /* blank search means to match all funcs in the mod */
2851 if (!len)
2852 return 1;
2853 }
2854
Steven Rostedt64e7c442009-02-13 17:08:48 -05002855 return ftrace_match(str, regex, len, type);
2856}
2857
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002858static int
2859match_records(struct ftrace_hash *hash, char *buff,
2860 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002861{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002862 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002863 struct ftrace_page *pg;
2864 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002865 int type = MATCH_FULL;
2866 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002867 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002868 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002869
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002870 if (len) {
2871 type = filter_parse_regex(buff, len, &search, &not);
2872 search_len = strlen(search);
2873 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002874
Steven Rostedt52baf112009-02-14 01:15:39 -05002875 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002876
2877 if (unlikely(ftrace_disabled))
2878 goto out_unlock;
2879
Steven Rostedt265c8312009-02-13 12:43:56 -05002880 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002881 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002882 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002883 if (ret < 0) {
2884 found = ret;
2885 goto out_unlock;
2886 }
Li Zefan311d16d2009-12-08 11:15:11 +08002887 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002888 }
2889 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002890 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002891 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002892
2893 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002894}
2895
Steven Rostedt64e7c442009-02-13 17:08:48 -05002896static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002897ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002898{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002899 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002900}
2901
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002902static int
2903ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002904{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002905 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002906
Steven Rostedt64e7c442009-02-13 17:08:48 -05002907 /* blank or '*' mean the same */
2908 if (strcmp(buff, "*") == 0)
2909 buff[0] = 0;
2910
2911 /* handle the case of 'dont filter this module' */
2912 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2913 buff[0] = 0;
2914 not = 1;
2915 }
2916
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002917 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002918}
2919
Steven Rostedtf6180772009-02-14 00:40:25 -05002920/*
2921 * We register the module command as a template to show others how
2922 * to register the a command as well.
2923 */
2924
2925static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002926ftrace_mod_callback(struct ftrace_hash *hash,
2927 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002928{
2929 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002930 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002931
2932 /*
2933 * cmd == 'mod' because we only registered this func
2934 * for the 'mod' ftrace_func_command.
2935 * But if you register one func with multiple commands,
2936 * you can tell which command was used by the cmd
2937 * parameter.
2938 */
2939
2940 /* we must have a module name */
2941 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002942 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002943
2944 mod = strsep(&param, ":");
2945 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002946 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002947
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002948 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002949 if (!ret)
2950 ret = -EINVAL;
2951 if (ret < 0)
2952 return ret;
2953
2954 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002955}
2956
2957static struct ftrace_func_command ftrace_mod_cmd = {
2958 .name = "mod",
2959 .func = ftrace_mod_callback,
2960};
2961
2962static int __init ftrace_mod_cmd_init(void)
2963{
2964 return register_ftrace_command(&ftrace_mod_cmd);
2965}
Steven Rostedt6f415672012-10-05 12:13:07 -04002966core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002967
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002968static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002969 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002970{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002971 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002972 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002974
2975 key = hash_long(ip, FTRACE_HASH_BITS);
2976
2977 hhd = &ftrace_func_hash[key];
2978
2979 if (hlist_empty(hhd))
2980 return;
2981
2982 /*
2983 * Disable preemption for these calls to prevent a RCU grace
2984 * period. This syncs the hash iteration and freeing of items
2985 * on the hash. rcu_read_lock is too dangerous here.
2986 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002987 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04002988 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002989 if (entry->ip == ip)
2990 entry->ops->func(ip, parent_ip, &entry->data);
2991 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002992 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002993}
2994
Steven Rostedtb6887d72009-02-17 12:32:04 -05002995static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002996{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002997 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002998 .flags = FTRACE_OPS_FL_INITIALIZED,
2999 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003000};
3001
Steven Rostedtb6887d72009-02-17 12:32:04 -05003002static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003003
Steven Rostedtb6887d72009-02-17 12:32:04 -05003004static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003005{
Steven Rostedtb8489142011-05-04 09:27:52 -04003006 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003007 int i;
3008
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003009 if (ftrace_probe_registered) {
3010 /* still need to update the function call sites */
3011 if (ftrace_enabled)
3012 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003013 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003014 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003015
3016 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3017 struct hlist_head *hhd = &ftrace_func_hash[i];
3018 if (hhd->first)
3019 break;
3020 }
3021 /* Nothing registered? */
3022 if (i == FTRACE_FUNC_HASHSIZE)
3023 return;
3024
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003025 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003026
Steven Rostedtb6887d72009-02-17 12:32:04 -05003027 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003028}
3029
Steven Rostedtb6887d72009-02-17 12:32:04 -05003030static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003031{
3032 int i;
3033
Steven Rostedtb6887d72009-02-17 12:32:04 -05003034 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003035 return;
3036
3037 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3038 struct hlist_head *hhd = &ftrace_func_hash[i];
3039 if (hhd->first)
3040 return;
3041 }
3042
3043 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003044 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003045
Steven Rostedtb6887d72009-02-17 12:32:04 -05003046 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003047}
3048
3049
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003050static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003051{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003052 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003053 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003054 kfree(entry);
3055}
3056
Steven Rostedt59df055f2009-02-14 15:29:06 -05003057int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003058register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003059 void *data)
3060{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003061 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003062 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3063 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003064 struct ftrace_page *pg;
3065 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003066 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003067 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003068 int count = 0;
3069 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003070 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003072 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003073 len = strlen(search);
3074
Steven Rostedtb6887d72009-02-17 12:32:04 -05003075 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003076 if (WARN_ON(not))
3077 return -EINVAL;
3078
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003079 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003080
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003081 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3082 if (!hash) {
3083 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003084 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003085 }
3086
3087 if (unlikely(ftrace_disabled)) {
3088 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003089 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003090 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003091
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003092 mutex_lock(&ftrace_lock);
3093
Steven Rostedt59df055f2009-02-14 15:29:06 -05003094 do_for_each_ftrace_rec(pg, rec) {
3095
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003096 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003097 continue;
3098
3099 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3100 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003101 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003102 if (!count)
3103 count = -ENOMEM;
3104 goto out_unlock;
3105 }
3106
3107 count++;
3108
3109 entry->data = data;
3110
3111 /*
3112 * The caller might want to do something special
3113 * for each function we find. We call the callback
3114 * to give the caller an opportunity to do so.
3115 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003116 if (ops->init) {
3117 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003118 /* caller does not like this func */
3119 kfree(entry);
3120 continue;
3121 }
3122 }
3123
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003124 ret = enter_record(hash, rec, 0);
3125 if (ret < 0) {
3126 kfree(entry);
3127 count = ret;
3128 goto out_unlock;
3129 }
3130
Steven Rostedt59df055f2009-02-14 15:29:06 -05003131 entry->ops = ops;
3132 entry->ip = rec->ip;
3133
3134 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3135 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3136
3137 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003138
3139 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3140 if (ret < 0)
3141 count = ret;
3142
Steven Rostedtb6887d72009-02-17 12:32:04 -05003143 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003144
3145 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003146 mutex_unlock(&ftrace_lock);
3147 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003148 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003149 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003150
3151 return count;
3152}
3153
3154enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003155 PROBE_TEST_FUNC = 1,
3156 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003157};
3158
3159static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003160__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003161 void *data, int flags)
3162{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003163 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003164 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003165 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003166 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003167 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003168 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003169 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003170 char str[KSYM_SYMBOL_LEN];
3171 int type = MATCH_FULL;
3172 int i, len = 0;
3173 char *search;
3174
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003175 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003176 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003177 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003178 int not;
3179
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003180 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003181 len = strlen(search);
3182
Steven Rostedtb6887d72009-02-17 12:32:04 -05003183 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003184 if (WARN_ON(not))
3185 return;
3186 }
3187
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003188 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003189
3190 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3191 if (!hash)
3192 /* Hmm, should report this somehow */
3193 goto out_unlock;
3194
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003195 INIT_LIST_HEAD(&free_list);
3196
Steven Rostedt59df055f2009-02-14 15:29:06 -05003197 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3198 struct hlist_head *hhd = &ftrace_func_hash[i];
3199
Sasha Levinb67bfe02013-02-27 17:06:00 -08003200 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003201
3202 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003203 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003204 continue;
3205
Steven Rostedtb6887d72009-02-17 12:32:04 -05003206 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003207 continue;
3208
3209 /* do this last, since it is the most expensive */
3210 if (glob) {
3211 kallsyms_lookup(entry->ip, NULL, NULL,
3212 NULL, str);
3213 if (!ftrace_match(str, glob, len, type))
3214 continue;
3215 }
3216
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003217 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3218 /* It is possible more than one entry had this ip */
3219 if (rec_entry)
3220 free_hash_entry(hash, rec_entry);
3221
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003222 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003223 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003224 }
3225 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003226 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003227 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003228 /*
3229 * Remove after the disable is called. Otherwise, if the last
3230 * probe is removed, a null hash means *all enabled*.
3231 */
3232 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003233 synchronize_sched();
3234 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3235 list_del(&entry->free_list);
3236 ftrace_free_entry(entry);
3237 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003238 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003239
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003240 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003241 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003242 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003243}
3244
3245void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003246unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003247 void *data)
3248{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003249 __unregister_ftrace_function_probe(glob, ops, data,
3250 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003251}
3252
3253void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003254unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003255{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003256 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003257}
3258
Steven Rostedtb6887d72009-02-17 12:32:04 -05003259void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003260{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003261 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003262}
3263
Steven Rostedtf6180772009-02-14 00:40:25 -05003264static LIST_HEAD(ftrace_commands);
3265static DEFINE_MUTEX(ftrace_cmd_mutex);
3266
Tom Zanussi38de93a2013-10-24 08:34:18 -05003267/*
3268 * Currently we only register ftrace commands from __init, so mark this
3269 * __init too.
3270 */
3271__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003272{
3273 struct ftrace_func_command *p;
3274 int ret = 0;
3275
3276 mutex_lock(&ftrace_cmd_mutex);
3277 list_for_each_entry(p, &ftrace_commands, list) {
3278 if (strcmp(cmd->name, p->name) == 0) {
3279 ret = -EBUSY;
3280 goto out_unlock;
3281 }
3282 }
3283 list_add(&cmd->list, &ftrace_commands);
3284 out_unlock:
3285 mutex_unlock(&ftrace_cmd_mutex);
3286
3287 return ret;
3288}
3289
Tom Zanussi38de93a2013-10-24 08:34:18 -05003290/*
3291 * Currently we only unregister ftrace commands from __init, so mark
3292 * this __init too.
3293 */
3294__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003295{
3296 struct ftrace_func_command *p, *n;
3297 int ret = -ENODEV;
3298
3299 mutex_lock(&ftrace_cmd_mutex);
3300 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3301 if (strcmp(cmd->name, p->name) == 0) {
3302 ret = 0;
3303 list_del_init(&p->list);
3304 goto out_unlock;
3305 }
3306 }
3307 out_unlock:
3308 mutex_unlock(&ftrace_cmd_mutex);
3309
3310 return ret;
3311}
3312
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003313static int ftrace_process_regex(struct ftrace_hash *hash,
3314 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003315{
Steven Rostedtf6180772009-02-14 00:40:25 -05003316 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003317 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003318 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003319
3320 func = strsep(&next, ":");
3321
3322 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003323 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003324 if (!ret)
3325 ret = -EINVAL;
3326 if (ret < 0)
3327 return ret;
3328 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003329 }
3330
Steven Rostedtf6180772009-02-14 00:40:25 -05003331 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003332
3333 command = strsep(&next, ":");
3334
Steven Rostedtf6180772009-02-14 00:40:25 -05003335 mutex_lock(&ftrace_cmd_mutex);
3336 list_for_each_entry(p, &ftrace_commands, list) {
3337 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003338 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003339 goto out_unlock;
3340 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003341 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003342 out_unlock:
3343 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003344
Steven Rostedtf6180772009-02-14 00:40:25 -05003345 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003346}
3347
Ingo Molnare309b412008-05-12 21:20:51 +02003348static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003349ftrace_regex_write(struct file *file, const char __user *ubuf,
3350 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003351{
3352 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003353 struct trace_parser *parser;
3354 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003355
Li Zefan4ba79782009-09-22 13:52:20 +08003356 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003357 return 0;
3358
Steven Rostedt5072c592008-05-12 21:20:43 +02003359 if (file->f_mode & FMODE_READ) {
3360 struct seq_file *m = file->private_data;
3361 iter = m->private;
3362 } else
3363 iter = file->private_data;
3364
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003365 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003366 return -ENODEV;
3367
3368 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003369
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003370 parser = &iter->parser;
3371 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003372
Li Zefan4ba79782009-09-22 13:52:20 +08003373 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003374 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003375 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003376 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003377 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003378 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003379 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003380 }
3381
Steven Rostedt5072c592008-05-12 21:20:43 +02003382 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003383 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003384 return ret;
3385}
3386
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003387ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003388ftrace_filter_write(struct file *file, const char __user *ubuf,
3389 size_t cnt, loff_t *ppos)
3390{
3391 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3392}
3393
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003394ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003395ftrace_notrace_write(struct file *file, const char __user *ubuf,
3396 size_t cnt, loff_t *ppos)
3397{
3398 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3399}
3400
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003401static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003402ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3403{
3404 struct ftrace_func_entry *entry;
3405
3406 if (!ftrace_location(ip))
3407 return -EINVAL;
3408
3409 if (remove) {
3410 entry = ftrace_lookup_ip(hash, ip);
3411 if (!entry)
3412 return -ENOENT;
3413 free_hash_entry(hash, entry);
3414 return 0;
3415 }
3416
3417 return add_hash_entry(hash, ip);
3418}
3419
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003420static void ftrace_ops_update_code(struct ftrace_ops *ops)
3421{
3422 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3423 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3424}
3425
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003426static int
3427ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3428 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003429{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003430 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003431 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003432 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003433
Steven Rostedt41c52c02008-05-22 11:46:33 -04003434 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003435 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003436
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003437 mutex_lock(&ops->regex_lock);
3438
Steven Rostedtf45948e2011-05-02 12:29:25 -04003439 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003440 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003441 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003442 orig_hash = &ops->notrace_hash;
3443
3444 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003445 if (!hash) {
3446 ret = -ENOMEM;
3447 goto out_regex_unlock;
3448 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003449
Steven Rostedt41c52c02008-05-22 11:46:33 -04003450 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003451 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003452 if (buf && !ftrace_match_records(hash, buf, len)) {
3453 ret = -EINVAL;
3454 goto out_regex_unlock;
3455 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003456 if (ip) {
3457 ret = ftrace_match_addr(hash, ip, remove);
3458 if (ret < 0)
3459 goto out_regex_unlock;
3460 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003461
3462 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003463 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003464 if (!ret)
3465 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003466
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003467 mutex_unlock(&ftrace_lock);
3468
Jiri Olsaac483c42012-01-02 10:04:14 +01003469 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003470 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003471
3472 free_ftrace_hash(hash);
3473 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003474}
3475
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003476static int
3477ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3478 int reset, int enable)
3479{
3480 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3481}
3482
3483/**
3484 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3485 * @ops - the ops to set the filter with
3486 * @ip - the address to add to or remove from the filter.
3487 * @remove - non zero to remove the ip from the filter
3488 * @reset - non zero to reset all filters before applying this filter.
3489 *
3490 * Filters denote which functions should be enabled when tracing is enabled
3491 * If @ip is NULL, it failes to update filter.
3492 */
3493int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3494 int remove, int reset)
3495{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003496 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003497 return ftrace_set_addr(ops, ip, remove, reset, 1);
3498}
3499EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3500
3501static int
3502ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3503 int reset, int enable)
3504{
3505 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3506}
3507
Steven Rostedt77a2b372008-05-12 21:20:45 +02003508/**
3509 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003510 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003511 * @buf - the string that holds the function filter text.
3512 * @len - the length of the string.
3513 * @reset - non zero to reset all filters before applying this filter.
3514 *
3515 * Filters denote which functions should be enabled when tracing is enabled.
3516 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3517 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003518int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003519 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003520{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003521 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003522 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003523}
Steven Rostedt936e0742011-05-05 22:54:01 -04003524EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003525
Steven Rostedt41c52c02008-05-22 11:46:33 -04003526/**
3527 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003528 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003529 * @buf - the string that holds the function notrace text.
3530 * @len - the length of the string.
3531 * @reset - non zero to reset all filters before applying this filter.
3532 *
3533 * Notrace Filters denote which functions should not be enabled when tracing
3534 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3535 * for tracing.
3536 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003537int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003538 int len, int reset)
3539{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003540 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003541 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003542}
3543EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3544/**
3545 * ftrace_set_filter - set a function to filter on in ftrace
3546 * @ops - the ops to set the filter with
3547 * @buf - the string that holds the function filter text.
3548 * @len - the length of the string.
3549 * @reset - non zero to reset all filters before applying this filter.
3550 *
3551 * Filters denote which functions should be enabled when tracing is enabled.
3552 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3553 */
3554void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3555{
3556 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3557}
3558EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3559
3560/**
3561 * ftrace_set_notrace - set a function to not trace in ftrace
3562 * @ops - the ops to set the notrace filter with
3563 * @buf - the string that holds the function notrace text.
3564 * @len - the length of the string.
3565 * @reset - non zero to reset all filters before applying this filter.
3566 *
3567 * Notrace Filters denote which functions should not be enabled when tracing
3568 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3569 * for tracing.
3570 */
3571void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003572{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003573 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003574}
Steven Rostedt936e0742011-05-05 22:54:01 -04003575EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003576
Steven Rostedt2af15d62009-05-28 13:37:24 -04003577/*
3578 * command line interface to allow users to set filters on boot up.
3579 */
3580#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3581static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3582static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3583
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003584/* Used by function selftest to not test if filter is set */
3585bool ftrace_filter_param __initdata;
3586
Steven Rostedt2af15d62009-05-28 13:37:24 -04003587static int __init set_ftrace_notrace(char *str)
3588{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003589 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003590 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003591 return 1;
3592}
3593__setup("ftrace_notrace=", set_ftrace_notrace);
3594
3595static int __init set_ftrace_filter(char *str)
3596{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003597 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003598 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003599 return 1;
3600}
3601__setup("ftrace_filter=", set_ftrace_filter);
3602
Stefan Assmann369bc182009-10-12 22:17:21 +02003603#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003604static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003605static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003606
Stefan Assmann369bc182009-10-12 22:17:21 +02003607static int __init set_graph_function(char *str)
3608{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003609 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003610 return 1;
3611}
3612__setup("ftrace_graph_filter=", set_graph_function);
3613
3614static void __init set_ftrace_early_graph(char *buf)
3615{
3616 int ret;
3617 char *func;
3618
3619 while (buf) {
3620 func = strsep(&buf, ",");
3621 /* we allow only one expression at a time */
3622 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003623 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003624 if (ret)
3625 printk(KERN_DEBUG "ftrace: function %s not "
3626 "traceable\n", func);
3627 }
3628}
3629#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3630
Steven Rostedt2a85a372011-12-19 21:57:44 -05003631void __init
3632ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003633{
3634 char *func;
3635
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003636 ftrace_ops_init(ops);
3637
Steven Rostedt2af15d62009-05-28 13:37:24 -04003638 while (buf) {
3639 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003640 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003641 }
3642}
3643
3644static void __init set_ftrace_early_filters(void)
3645{
3646 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003647 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003648 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003649 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003650#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3651 if (ftrace_graph_buf[0])
3652 set_ftrace_early_graph(ftrace_graph_buf);
3653#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003654}
3655
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003656int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003657{
3658 struct seq_file *m = (struct seq_file *)file->private_data;
3659 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003660 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003661 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003662 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003663 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003664
Steven Rostedt5072c592008-05-12 21:20:43 +02003665 if (file->f_mode & FMODE_READ) {
3666 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003667 seq_release(inode, file);
3668 } else
3669 iter = file->private_data;
3670
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003671 parser = &iter->parser;
3672 if (trace_parser_loaded(parser)) {
3673 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003674 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003675 }
3676
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003677 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003678
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003679 mutex_lock(&iter->ops->regex_lock);
3680
Steven Rostedt058e2972011-04-29 22:35:33 -04003681 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003682 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3683
3684 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003685 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003686 else
3687 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003688
Steven Rostedt058e2972011-04-29 22:35:33 -04003689 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003690 ret = ftrace_hash_move(iter->ops, filter_hash,
3691 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003692 if (!ret)
3693 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003694
Steven Rostedt058e2972011-04-29 22:35:33 -04003695 mutex_unlock(&ftrace_lock);
3696 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003697
3698 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003699 free_ftrace_hash(iter->hash);
3700 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003701
Steven Rostedt5072c592008-05-12 21:20:43 +02003702 return 0;
3703}
3704
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003705static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003706 .open = ftrace_avail_open,
3707 .read = seq_read,
3708 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003709 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003710};
3711
Steven Rostedt647bcd02011-05-03 14:39:21 -04003712static const struct file_operations ftrace_enabled_fops = {
3713 .open = ftrace_enabled_open,
3714 .read = seq_read,
3715 .llseek = seq_lseek,
3716 .release = seq_release_private,
3717};
3718
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003719static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003720 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003721 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003722 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003723 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003724 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003725};
3726
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003727static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003728 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003729 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003730 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003731 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003732 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003733};
3734
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003735#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3736
3737static DEFINE_MUTEX(graph_lock);
3738
3739int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003740int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003741unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003742unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003743
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003744struct ftrace_graph_data {
3745 unsigned long *table;
3746 size_t size;
3747 int *count;
3748 const struct seq_operations *seq_ops;
3749};
3750
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003751static void *
Li Zefan85951842009-06-24 09:54:00 +08003752__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003753{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003754 struct ftrace_graph_data *fgd = m->private;
3755
3756 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003757 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003758 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003759}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003760
Li Zefan85951842009-06-24 09:54:00 +08003761static void *
3762g_next(struct seq_file *m, void *v, loff_t *pos)
3763{
3764 (*pos)++;
3765 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003766}
3767
3768static void *g_start(struct seq_file *m, loff_t *pos)
3769{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003770 struct ftrace_graph_data *fgd = m->private;
3771
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003772 mutex_lock(&graph_lock);
3773
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003774 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003775 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003776 return (void *)1;
3777
Li Zefan85951842009-06-24 09:54:00 +08003778 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003779}
3780
3781static void g_stop(struct seq_file *m, void *p)
3782{
3783 mutex_unlock(&graph_lock);
3784}
3785
3786static int g_show(struct seq_file *m, void *v)
3787{
3788 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003789
3790 if (!ptr)
3791 return 0;
3792
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003793 if (ptr == (unsigned long *)1) {
3794 seq_printf(m, "#### all functions enabled ####\n");
3795 return 0;
3796 }
3797
Steven Rostedtb375a112009-09-17 00:05:58 -04003798 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003799
3800 return 0;
3801}
3802
James Morris88e9d342009-09-22 16:43:43 -07003803static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003804 .start = g_start,
3805 .next = g_next,
3806 .stop = g_stop,
3807 .show = g_show,
3808};
3809
3810static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003811__ftrace_graph_open(struct inode *inode, struct file *file,
3812 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003813{
3814 int ret = 0;
3815
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003816 mutex_lock(&graph_lock);
3817 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003818 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003819 *fgd->count = 0;
3820 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003821 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003822 mutex_unlock(&graph_lock);
3823
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003824 if (file->f_mode & FMODE_READ) {
3825 ret = seq_open(file, fgd->seq_ops);
3826 if (!ret) {
3827 struct seq_file *m = file->private_data;
3828 m->private = fgd;
3829 }
3830 } else
3831 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003832
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003833 return ret;
3834}
3835
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003836static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003837ftrace_graph_open(struct inode *inode, struct file *file)
3838{
3839 struct ftrace_graph_data *fgd;
3840
3841 if (unlikely(ftrace_disabled))
3842 return -ENODEV;
3843
3844 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3845 if (fgd == NULL)
3846 return -ENOMEM;
3847
3848 fgd->table = ftrace_graph_funcs;
3849 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3850 fgd->count = &ftrace_graph_count;
3851 fgd->seq_ops = &ftrace_graph_seq_ops;
3852
3853 return __ftrace_graph_open(inode, file, fgd);
3854}
3855
3856static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003857ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3858{
3859 struct ftrace_graph_data *fgd;
3860
3861 if (unlikely(ftrace_disabled))
3862 return -ENODEV;
3863
3864 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3865 if (fgd == NULL)
3866 return -ENOMEM;
3867
3868 fgd->table = ftrace_graph_notrace_funcs;
3869 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3870 fgd->count = &ftrace_graph_notrace_count;
3871 fgd->seq_ops = &ftrace_graph_seq_ops;
3872
3873 return __ftrace_graph_open(inode, file, fgd);
3874}
3875
3876static int
Li Zefan87827112009-07-23 11:29:11 +08003877ftrace_graph_release(struct inode *inode, struct file *file)
3878{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003879 if (file->f_mode & FMODE_READ) {
3880 struct seq_file *m = file->private_data;
3881
3882 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003883 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003884 } else {
3885 kfree(file->private_data);
3886 }
3887
Li Zefan87827112009-07-23 11:29:11 +08003888 return 0;
3889}
3890
3891static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003892ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003893{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003894 struct dyn_ftrace *rec;
3895 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003896 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003897 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003898 int type, not;
3899 char *search;
3900 bool exists;
3901 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003902
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003903 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003904 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003905 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003906 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003907
3908 search_len = strlen(search);
3909
Steven Rostedt52baf112009-02-14 01:15:39 -05003910 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003911
3912 if (unlikely(ftrace_disabled)) {
3913 mutex_unlock(&ftrace_lock);
3914 return -ENODEV;
3915 }
3916
Steven Rostedt265c8312009-02-13 12:43:56 -05003917 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003918
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003919 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003920 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003921 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003922 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003923 if (array[i] == rec->ip) {
3924 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003925 break;
3926 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003927 }
3928
3929 if (!not) {
3930 fail = 0;
3931 if (!exists) {
3932 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003933 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003934 goto out;
3935 }
3936 } else {
3937 if (exists) {
3938 array[i] = array[--(*idx)];
3939 array[*idx] = 0;
3940 fail = 0;
3941 }
3942 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003943 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003944 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003945out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003946 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003947
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003948 if (fail)
3949 return -EINVAL;
3950
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003951 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003952}
3953
3954static ssize_t
3955ftrace_graph_write(struct file *file, const char __user *ubuf,
3956 size_t cnt, loff_t *ppos)
3957{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003958 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09003959 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003960 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003961
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003962 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003963 return 0;
3964
Namhyung Kim6a101082013-10-14 17:24:25 +09003965 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
3966 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003967
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003968 read = trace_get_user(&parser, ubuf, cnt, ppos);
3969
Li Zefan4ba79782009-09-22 13:52:20 +08003970 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003971 parser.buffer[parser.idx] = 0;
3972
Namhyung Kim6a101082013-10-14 17:24:25 +09003973 mutex_lock(&graph_lock);
3974
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003975 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003976 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
3977 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09003978
3979 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003980 }
3981
Namhyung Kim6a101082013-10-14 17:24:25 +09003982 if (!ret)
3983 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003984
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003985 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003986
3987 return ret;
3988}
3989
3990static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003991 .open = ftrace_graph_open,
3992 .read = seq_read,
3993 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003994 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003995 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003996};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003997
3998static const struct file_operations ftrace_graph_notrace_fops = {
3999 .open = ftrace_graph_notrace_open,
4000 .read = seq_read,
4001 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004002 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004003 .release = ftrace_graph_release,
4004};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004005#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4006
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004007void ftrace_create_filter_files(struct ftrace_ops *ops,
4008 struct dentry *parent)
4009{
4010
4011 trace_create_file("set_ftrace_filter", 0644, parent,
4012 ops, &ftrace_filter_fops);
4013
4014 trace_create_file("set_ftrace_notrace", 0644, parent,
4015 ops, &ftrace_notrace_fops);
4016}
4017
4018/*
4019 * The name "destroy_filter_files" is really a misnomer. Although
4020 * in the future, it may actualy delete the files, but this is
4021 * really intended to make sure the ops passed in are disabled
4022 * and that when this function returns, the caller is free to
4023 * free the ops.
4024 *
4025 * The "destroy" name is only to match the "create" name that this
4026 * should be paired with.
4027 */
4028void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4029{
4030 mutex_lock(&ftrace_lock);
4031 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4032 ftrace_shutdown(ops, 0);
4033 ops->flags |= FTRACE_OPS_FL_DELETED;
4034 mutex_unlock(&ftrace_lock);
4035}
4036
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004037static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004038{
Steven Rostedt5072c592008-05-12 21:20:43 +02004039
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004040 trace_create_file("available_filter_functions", 0444,
4041 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004042
Steven Rostedt647bcd02011-05-03 14:39:21 -04004043 trace_create_file("enabled_functions", 0444,
4044 d_tracer, NULL, &ftrace_enabled_fops);
4045
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004046 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004047
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004048#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004049 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004050 NULL,
4051 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004052 trace_create_file("set_graph_notrace", 0444, d_tracer,
4053 NULL,
4054 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004055#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4056
Steven Rostedt5072c592008-05-12 21:20:43 +02004057 return 0;
4058}
4059
Steven Rostedt9fd49322012-04-24 22:32:06 -04004060static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004061{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004062 const unsigned long *ipa = a;
4063 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004064
Steven Rostedt9fd49322012-04-24 22:32:06 -04004065 if (*ipa > *ipb)
4066 return 1;
4067 if (*ipa < *ipb)
4068 return -1;
4069 return 0;
4070}
4071
4072static void ftrace_swap_ips(void *a, void *b, int size)
4073{
4074 unsigned long *ipa = a;
4075 unsigned long *ipb = b;
4076 unsigned long t;
4077
4078 t = *ipa;
4079 *ipa = *ipb;
4080 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004081}
4082
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004083static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004084 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004085 unsigned long *end)
4086{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004087 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004088 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004089 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004090 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004091 unsigned long *p;
4092 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004093 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004094 int ret = -ENOMEM;
4095
4096 count = end - start;
4097
4098 if (!count)
4099 return 0;
4100
Steven Rostedt9fd49322012-04-24 22:32:06 -04004101 sort(start, count, sizeof(*start),
4102 ftrace_cmp_ips, ftrace_swap_ips);
4103
Steven Rostedt706c81f2012-04-24 23:45:26 -04004104 start_pg = ftrace_allocate_pages(count);
4105 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004106 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004107
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004108 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004109
Steven Rostedt32082302011-12-16 14:42:37 -05004110 /*
4111 * Core and each module needs their own pages, as
4112 * modules will free them when they are removed.
4113 * Force a new page to be allocated for modules.
4114 */
Steven Rostedta7900872011-12-16 16:23:44 -05004115 if (!mod) {
4116 WARN_ON(ftrace_pages || ftrace_pages_start);
4117 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004118 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004119 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004120 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004121 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004122
Steven Rostedta7900872011-12-16 16:23:44 -05004123 if (WARN_ON(ftrace_pages->next)) {
4124 /* Hmm, we have free pages? */
4125 while (ftrace_pages->next)
4126 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004127 }
Steven Rostedta7900872011-12-16 16:23:44 -05004128
Steven Rostedt706c81f2012-04-24 23:45:26 -04004129 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004130 }
4131
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004132 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004133 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004134 while (p < end) {
4135 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004136 /*
4137 * Some architecture linkers will pad between
4138 * the different mcount_loc sections of different
4139 * object files to satisfy alignments.
4140 * Skip any NULL pointers.
4141 */
4142 if (!addr)
4143 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004144
4145 if (pg->index == pg->size) {
4146 /* We should have allocated enough */
4147 if (WARN_ON(!pg->next))
4148 break;
4149 pg = pg->next;
4150 }
4151
4152 rec = &pg->records[pg->index++];
4153 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004154 }
4155
Steven Rostedt706c81f2012-04-24 23:45:26 -04004156 /* We should have used all pages */
4157 WARN_ON(pg->next);
4158
4159 /* Assign the last page to ftrace_pages */
4160 ftrace_pages = pg;
4161
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004162 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004163 * We only need to disable interrupts on start up
4164 * because we are modifying code that an interrupt
4165 * may execute, and the modification is not atomic.
4166 * But for modules, nothing runs the code we modify
4167 * until we are finished with it, and there's no
4168 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004169 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004170 if (!mod)
4171 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004172 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004173 if (!mod)
4174 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004175 ret = 0;
4176 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004177 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004178
Steven Rostedta7900872011-12-16 16:23:44 -05004179 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004180}
4181
Steven Rostedt93eb6772009-04-15 13:24:06 -04004182#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004183
4184#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4185
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004186void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004187{
4188 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004189 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004190 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004191 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004192
Steven Rostedt93eb6772009-04-15 13:24:06 -04004193 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004194
4195 if (ftrace_disabled)
4196 goto out_unlock;
4197
Steven Rostedt32082302011-12-16 14:42:37 -05004198 /*
4199 * Each module has its own ftrace_pages, remove
4200 * them from the list.
4201 */
4202 last_pg = &ftrace_pages_start;
4203 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4204 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004205 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004206 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004207 * As core pages are first, the first
4208 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004209 */
Steven Rostedt32082302011-12-16 14:42:37 -05004210 if (WARN_ON(pg == ftrace_pages_start))
4211 goto out_unlock;
4212
4213 /* Check if we are deleting the last page */
4214 if (pg == ftrace_pages)
4215 ftrace_pages = next_to_ftrace_page(last_pg);
4216
4217 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004218 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4219 free_pages((unsigned long)pg->records, order);
4220 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004221 } else
4222 last_pg = &pg->next;
4223 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004224 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004225 mutex_unlock(&ftrace_lock);
4226}
4227
4228static void ftrace_init_module(struct module *mod,
4229 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004230{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004231 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004232 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004233 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004234}
4235
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004236static int ftrace_module_notify_enter(struct notifier_block *self,
4237 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004238{
4239 struct module *mod = data;
4240
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004241 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004242 ftrace_init_module(mod, mod->ftrace_callsites,
4243 mod->ftrace_callsites +
4244 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004245 return 0;
4246}
4247
4248static int ftrace_module_notify_exit(struct notifier_block *self,
4249 unsigned long val, void *data)
4250{
4251 struct module *mod = data;
4252
4253 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004254 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004255
4256 return 0;
4257}
4258#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004259static int ftrace_module_notify_enter(struct notifier_block *self,
4260 unsigned long val, void *data)
4261{
4262 return 0;
4263}
4264static int ftrace_module_notify_exit(struct notifier_block *self,
4265 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004266{
4267 return 0;
4268}
4269#endif /* CONFIG_MODULES */
4270
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004271struct notifier_block ftrace_module_enter_nb = {
4272 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004273 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004274};
4275
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004276struct notifier_block ftrace_module_exit_nb = {
4277 .notifier_call = ftrace_module_notify_exit,
4278 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4279};
4280
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004281void __init ftrace_init(void)
4282{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004283 extern unsigned long __start_mcount_loc[];
4284 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004285 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004286 int ret;
4287
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004288 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004289 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004290 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004291 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004292 goto failed;
4293
4294 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004295 if (!count) {
4296 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004297 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004298 }
4299
4300 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4301 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004302
4303 last_ftrace_enabled = ftrace_enabled = 1;
4304
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004305 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004306 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004307 __stop_mcount_loc);
4308
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004309 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004310 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004311 pr_warning("Failed to register trace ftrace module enter notifier\n");
4312
4313 ret = register_module_notifier(&ftrace_module_exit_nb);
4314 if (ret)
4315 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004316
Steven Rostedt2af15d62009-05-28 13:37:24 -04004317 set_ftrace_early_filters();
4318
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004319 return;
4320 failed:
4321 ftrace_disabled = 1;
4322}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004323
Steven Rostedt3d083392008-05-12 21:20:42 +02004324#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004325
Steven Rostedt2b499382011-05-03 22:49:52 -04004326static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004327 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004328 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4329 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004330};
4331
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004332static int __init ftrace_nodyn_init(void)
4333{
4334 ftrace_enabled = 1;
4335 return 0;
4336}
Steven Rostedt6f415672012-10-05 12:13:07 -04004337core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004338
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004339static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4340static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004341/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004342# define ftrace_startup(ops, command) \
4343 ({ \
4344 int ___ret = __register_ftrace_function(ops); \
4345 if (!___ret) \
4346 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4347 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004348 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004349# define ftrace_shutdown(ops, command) \
4350 ({ \
4351 int ___ret = __unregister_ftrace_function(ops); \
4352 if (!___ret) \
4353 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4354 ___ret; \
4355 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004356
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004357# define ftrace_startup_sysctl() do { } while (0)
4358# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004359
4360static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004361ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004362{
4363 return 1;
4364}
4365
Steven Rostedt3d083392008-05-12 21:20:42 +02004366#endif /* CONFIG_DYNAMIC_FTRACE */
4367
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004368__init void ftrace_init_global_array_ops(struct trace_array *tr)
4369{
4370 tr->ops = &global_ops;
4371 tr->ops->private = tr;
4372}
4373
4374void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4375{
4376 /* If we filter on pids, update to use the pid function */
4377 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4378 if (WARN_ON(tr->ops->func != ftrace_stub))
4379 printk("ftrace ops had %pS for function\n",
4380 tr->ops->func);
4381 /* Only the top level instance does pid tracing */
4382 if (!list_empty(&ftrace_pids)) {
4383 set_ftrace_pid_function(func);
4384 func = ftrace_pid_func;
4385 }
4386 }
4387 tr->ops->func = func;
4388 tr->ops->private = tr;
4389}
4390
4391void ftrace_reset_array_ops(struct trace_array *tr)
4392{
4393 tr->ops->func = ftrace_stub;
4394}
4395
Steven Rostedtb8489142011-05-04 09:27:52 -04004396static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004397ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004398 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004399{
Jiri Olsae2484912012-02-15 15:51:48 +01004400 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4401 return;
4402
4403 /*
4404 * Some of the ops may be dynamically allocated,
4405 * they must be freed after a synchronize_sched().
4406 */
4407 preempt_disable_notrace();
4408 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004409
4410 /*
4411 * Control funcs (perf) uses RCU. Only trace if
4412 * RCU is currently active.
4413 */
4414 if (!rcu_is_watching())
4415 goto out;
4416
Steven Rostedt0a016402012-11-02 17:03:03 -04004417 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004418 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4419 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004420 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004421 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004422 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004423 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004424 trace_recursion_clear(TRACE_CONTROL_BIT);
4425 preempt_enable_notrace();
4426}
4427
4428static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004429 .func = ftrace_ops_control_func,
4430 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4431 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004432};
4433
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004434static inline void
4435__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004436 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004437{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004438 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004439 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004440
Steven Rostedtccf36722012-06-05 09:44:25 -04004441 if (function_trace_stop)
4442 return;
4443
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004444 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4445 if (bit < 0)
4446 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004447
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004448 /*
4449 * Some of the ops may be dynamically allocated,
4450 * they must be freed after a synchronize_sched().
4451 */
4452 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004453 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004454 if (ftrace_ops_test(op, ip, regs)) {
4455 if (WARN_ON(!op->func)) {
4456 function_trace_stop = 1;
4457 printk("op=%p %pS\n", op, op);
4458 goto out;
4459 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004460 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004461 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004462 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004463out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004464 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004465 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004466}
4467
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004468/*
4469 * Some archs only support passing ip and parent_ip. Even though
4470 * the list function ignores the op parameter, we do not want any
4471 * C side effects, where a function is called without the caller
4472 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004473 * Archs are to support both the regs and ftrace_ops at the same time.
4474 * If they support ftrace_ops, it is assumed they support regs.
4475 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004476 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4477 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004478 * An architecture can pass partial regs with ftrace_ops and still
4479 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004480 */
4481#if ARCH_SUPPORTS_FTRACE_OPS
4482static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004483 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004484{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004485 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004486}
4487#else
4488static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4489{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004490 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004491}
4492#endif
4493
Steven Rostedte32d8952008-12-04 00:26:41 -05004494static void clear_ftrace_swapper(void)
4495{
4496 struct task_struct *p;
4497 int cpu;
4498
4499 get_online_cpus();
4500 for_each_online_cpu(cpu) {
4501 p = idle_task(cpu);
4502 clear_tsk_trace_trace(p);
4503 }
4504 put_online_cpus();
4505}
4506
4507static void set_ftrace_swapper(void)
4508{
4509 struct task_struct *p;
4510 int cpu;
4511
4512 get_online_cpus();
4513 for_each_online_cpu(cpu) {
4514 p = idle_task(cpu);
4515 set_tsk_trace_trace(p);
4516 }
4517 put_online_cpus();
4518}
4519
4520static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004521{
4522 struct task_struct *p;
4523
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004524 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004525 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004526 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004527 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004528 rcu_read_unlock();
4529
Steven Rostedte32d8952008-12-04 00:26:41 -05004530 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004531}
4532
Steven Rostedte32d8952008-12-04 00:26:41 -05004533static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004534{
4535 struct task_struct *p;
4536
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004537 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004538 do_each_pid_task(pid, PIDTYPE_PID, p) {
4539 set_tsk_trace_trace(p);
4540 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004541 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004542}
4543
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004544static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004545{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004546 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004547 clear_ftrace_swapper();
4548 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004549 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004550}
4551
4552static void set_ftrace_pid_task(struct pid *pid)
4553{
4554 if (pid == ftrace_swapper_pid)
4555 set_ftrace_swapper();
4556 else
4557 set_ftrace_pid(pid);
4558}
4559
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004560static int ftrace_pid_add(int p)
4561{
4562 struct pid *pid;
4563 struct ftrace_pid *fpid;
4564 int ret = -EINVAL;
4565
4566 mutex_lock(&ftrace_lock);
4567
4568 if (!p)
4569 pid = ftrace_swapper_pid;
4570 else
4571 pid = find_get_pid(p);
4572
4573 if (!pid)
4574 goto out;
4575
4576 ret = 0;
4577
4578 list_for_each_entry(fpid, &ftrace_pids, list)
4579 if (fpid->pid == pid)
4580 goto out_put;
4581
4582 ret = -ENOMEM;
4583
4584 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4585 if (!fpid)
4586 goto out_put;
4587
4588 list_add(&fpid->list, &ftrace_pids);
4589 fpid->pid = pid;
4590
4591 set_ftrace_pid_task(pid);
4592
4593 ftrace_update_pid_func();
4594 ftrace_startup_enable(0);
4595
4596 mutex_unlock(&ftrace_lock);
4597 return 0;
4598
4599out_put:
4600 if (pid != ftrace_swapper_pid)
4601 put_pid(pid);
4602
4603out:
4604 mutex_unlock(&ftrace_lock);
4605 return ret;
4606}
4607
4608static void ftrace_pid_reset(void)
4609{
4610 struct ftrace_pid *fpid, *safe;
4611
4612 mutex_lock(&ftrace_lock);
4613 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4614 struct pid *pid = fpid->pid;
4615
4616 clear_ftrace_pid_task(pid);
4617
4618 list_del(&fpid->list);
4619 kfree(fpid);
4620 }
4621
4622 ftrace_update_pid_func();
4623 ftrace_startup_enable(0);
4624
4625 mutex_unlock(&ftrace_lock);
4626}
4627
4628static void *fpid_start(struct seq_file *m, loff_t *pos)
4629{
4630 mutex_lock(&ftrace_lock);
4631
4632 if (list_empty(&ftrace_pids) && (!*pos))
4633 return (void *) 1;
4634
4635 return seq_list_start(&ftrace_pids, *pos);
4636}
4637
4638static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4639{
4640 if (v == (void *)1)
4641 return NULL;
4642
4643 return seq_list_next(v, &ftrace_pids, pos);
4644}
4645
4646static void fpid_stop(struct seq_file *m, void *p)
4647{
4648 mutex_unlock(&ftrace_lock);
4649}
4650
4651static int fpid_show(struct seq_file *m, void *v)
4652{
4653 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4654
4655 if (v == (void *)1) {
4656 seq_printf(m, "no pid\n");
4657 return 0;
4658 }
4659
4660 if (fpid->pid == ftrace_swapper_pid)
4661 seq_printf(m, "swapper tasks\n");
4662 else
4663 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4664
4665 return 0;
4666}
4667
4668static const struct seq_operations ftrace_pid_sops = {
4669 .start = fpid_start,
4670 .next = fpid_next,
4671 .stop = fpid_stop,
4672 .show = fpid_show,
4673};
4674
4675static int
4676ftrace_pid_open(struct inode *inode, struct file *file)
4677{
4678 int ret = 0;
4679
4680 if ((file->f_mode & FMODE_WRITE) &&
4681 (file->f_flags & O_TRUNC))
4682 ftrace_pid_reset();
4683
4684 if (file->f_mode & FMODE_READ)
4685 ret = seq_open(file, &ftrace_pid_sops);
4686
4687 return ret;
4688}
4689
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004690static ssize_t
4691ftrace_pid_write(struct file *filp, const char __user *ubuf,
4692 size_t cnt, loff_t *ppos)
4693{
Ingo Molnar457dc922009-11-23 11:03:28 +01004694 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004695 long val;
4696 int ret;
4697
4698 if (cnt >= sizeof(buf))
4699 return -EINVAL;
4700
4701 if (copy_from_user(&buf, ubuf, cnt))
4702 return -EFAULT;
4703
4704 buf[cnt] = 0;
4705
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004706 /*
4707 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4708 * to clean the filter quietly.
4709 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004710 tmp = strstrip(buf);
4711 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004712 return 1;
4713
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004714 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004715 if (ret < 0)
4716 return ret;
4717
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004718 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004719
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004720 return ret ? ret : cnt;
4721}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004722
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004723static int
4724ftrace_pid_release(struct inode *inode, struct file *file)
4725{
4726 if (file->f_mode & FMODE_READ)
4727 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004728
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004729 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004730}
4731
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004732static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004733 .open = ftrace_pid_open,
4734 .write = ftrace_pid_write,
4735 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004736 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004737 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004738};
4739
4740static __init int ftrace_init_debugfs(void)
4741{
4742 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004743
4744 d_tracer = tracing_init_dentry();
4745 if (!d_tracer)
4746 return 0;
4747
4748 ftrace_init_dyn_debugfs(d_tracer);
4749
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004750 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4751 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004752
4753 ftrace_profile_debugfs(d_tracer);
4754
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004755 return 0;
4756}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004757fs_initcall(ftrace_init_debugfs);
4758
Steven Rostedt3d083392008-05-12 21:20:42 +02004759/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004760 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004761 *
4762 * This function should be used by panic code. It stops ftrace
4763 * but in a not so nice way. If you need to simply kill ftrace
4764 * from a non-atomic section, use ftrace_kill.
4765 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004766void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004767{
4768 ftrace_disabled = 1;
4769 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004770 clear_ftrace_function();
4771}
4772
4773/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004774 * Test if ftrace is dead or not.
4775 */
4776int ftrace_is_dead(void)
4777{
4778 return ftrace_disabled;
4779}
4780
4781/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004782 * register_ftrace_function - register a function for profiling
4783 * @ops - ops structure that holds the function for profiling.
4784 *
4785 * Register a function to be called by all functions in the
4786 * kernel.
4787 *
4788 * Note: @ops->func and all the functions it calls must be labeled
4789 * with "notrace", otherwise it will go into a
4790 * recursive loop.
4791 */
4792int register_ftrace_function(struct ftrace_ops *ops)
4793{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004794 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004795
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004796 ftrace_ops_init(ops);
4797
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004798 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004799
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004800 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004801
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004802 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004803
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004804 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004805}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004806EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004807
4808/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004809 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004810 * @ops - ops structure that holds the function to unregister
4811 *
4812 * Unregister a function that was added to be called by ftrace profiling.
4813 */
4814int unregister_ftrace_function(struct ftrace_ops *ops)
4815{
4816 int ret;
4817
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004818 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004819 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004820 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004821
4822 return ret;
4823}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004824EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004825
Ingo Molnare309b412008-05-12 21:20:51 +02004826int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004827ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004828 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004829 loff_t *ppos)
4830{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004831 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004832
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004833 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004834
Steven Rostedt45a4a232011-04-21 23:16:46 -04004835 if (unlikely(ftrace_disabled))
4836 goto out;
4837
4838 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004839
Li Zefana32c7762009-06-26 16:55:51 +08004840 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004841 goto out;
4842
Li Zefana32c7762009-06-26 16:55:51 +08004843 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004844
4845 if (ftrace_enabled) {
4846
4847 ftrace_startup_sysctl();
4848
4849 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004850 if (ftrace_ops_list != &ftrace_list_end)
4851 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004852
4853 } else {
4854 /* stopping ftrace calls (just send to ftrace_stub) */
4855 ftrace_trace_function = ftrace_stub;
4856
4857 ftrace_shutdown_sysctl();
4858 }
4859
4860 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004861 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004862 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004863}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004864
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004865#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004866
Steven Rostedt597af812009-04-03 15:24:12 -04004867static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004868
Steven Rostedte49dc192008-12-02 23:50:05 -05004869int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4870{
4871 return 0;
4872}
4873
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004874/* The callbacks that hook a function */
4875trace_func_graph_ret_t ftrace_graph_return =
4876 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004877trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05004878static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004879
4880/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4881static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4882{
4883 int i;
4884 int ret = 0;
4885 unsigned long flags;
4886 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4887 struct task_struct *g, *t;
4888
4889 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4890 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4891 * sizeof(struct ftrace_ret_stack),
4892 GFP_KERNEL);
4893 if (!ret_stack_list[i]) {
4894 start = 0;
4895 end = i;
4896 ret = -ENOMEM;
4897 goto free;
4898 }
4899 }
4900
4901 read_lock_irqsave(&tasklist_lock, flags);
4902 do_each_thread(g, t) {
4903 if (start == end) {
4904 ret = -EAGAIN;
4905 goto unlock;
4906 }
4907
4908 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004909 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004910 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004911 t->curr_ret_stack = -1;
4912 /* Make sure the tasks see the -1 first: */
4913 smp_wmb();
4914 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004915 }
4916 } while_each_thread(g, t);
4917
4918unlock:
4919 read_unlock_irqrestore(&tasklist_lock, flags);
4920free:
4921 for (i = start; i < end; i++)
4922 kfree(ret_stack_list[i]);
4923 return ret;
4924}
4925
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004926static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004927ftrace_graph_probe_sched_switch(void *ignore,
4928 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004929{
4930 unsigned long long timestamp;
4931 int index;
4932
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004933 /*
4934 * Does the user want to count the time a function was asleep.
4935 * If so, do not update the time stamps.
4936 */
4937 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4938 return;
4939
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004940 timestamp = trace_clock_local();
4941
4942 prev->ftrace_timestamp = timestamp;
4943
4944 /* only process tasks that we timestamped */
4945 if (!next->ftrace_timestamp)
4946 return;
4947
4948 /*
4949 * Update all the counters in next to make up for the
4950 * time next was sleeping.
4951 */
4952 timestamp -= next->ftrace_timestamp;
4953
4954 for (index = next->curr_ret_stack; index >= 0; index--)
4955 next->ret_stack[index].calltime += timestamp;
4956}
4957
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004958/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004959static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004960{
4961 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004962 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004963
4964 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4965 sizeof(struct ftrace_ret_stack *),
4966 GFP_KERNEL);
4967
4968 if (!ret_stack_list)
4969 return -ENOMEM;
4970
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004971 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004972 for_each_online_cpu(cpu) {
4973 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004974 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004975 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004976
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004977 do {
4978 ret = alloc_retstack_tasklist(ret_stack_list);
4979 } while (ret == -EAGAIN);
4980
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004981 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004982 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004983 if (ret)
4984 pr_info("ftrace_graph: Couldn't activate tracepoint"
4985 " probe to kernel_sched_switch\n");
4986 }
4987
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004988 kfree(ret_stack_list);
4989 return ret;
4990}
4991
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004992/*
4993 * Hibernation protection.
4994 * The state of the current task is too much unstable during
4995 * suspend/restore to disk. We want to protect against that.
4996 */
4997static int
4998ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4999 void *unused)
5000{
5001 switch (state) {
5002 case PM_HIBERNATION_PREPARE:
5003 pause_graph_tracing();
5004 break;
5005
5006 case PM_POST_HIBERNATION:
5007 unpause_graph_tracing();
5008 break;
5009 }
5010 return NOTIFY_DONE;
5011}
5012
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005013/* Just a place holder for function graph */
5014static struct ftrace_ops fgraph_ops __read_mostly = {
5015 .func = ftrace_stub,
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005016 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005017};
5018
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005019static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5020{
5021 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5022 return 0;
5023 return __ftrace_graph_entry(trace);
5024}
5025
5026/*
5027 * The function graph tracer should only trace the functions defined
5028 * by set_ftrace_filter and set_ftrace_notrace. If another function
5029 * tracer ops is registered, the graph tracer requires testing the
5030 * function against the global ops, and not just trace any function
5031 * that any ftrace_ops registered.
5032 */
5033static void update_function_graph_func(void)
5034{
5035 if (ftrace_ops_list == &ftrace_list_end ||
5036 (ftrace_ops_list == &global_ops &&
5037 global_ops.next == &ftrace_list_end))
5038 ftrace_graph_entry = __ftrace_graph_entry;
5039 else
5040 ftrace_graph_entry = ftrace_graph_entry_test;
5041}
5042
Mathias Krause8275f692014-03-30 15:31:50 +02005043static struct notifier_block ftrace_suspend_notifier = {
5044 .notifier_call = ftrace_suspend_notifier_call,
5045};
5046
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005047int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5048 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005049{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005050 int ret = 0;
5051
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005052 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005053
Steven Rostedt05ce5812009-03-24 00:18:31 -04005054 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005055 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005056 ret = -EBUSY;
5057 goto out;
5058 }
5059
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005060 register_pm_notifier(&ftrace_suspend_notifier);
5061
Steven Rostedt597af812009-04-03 15:24:12 -04005062 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005063 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005064 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005065 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005066 goto out;
5067 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005068
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005069 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005070
5071 /*
5072 * Update the indirect function to the entryfunc, and the
5073 * function that gets called to the entry_test first. Then
5074 * call the update fgraph entry function to determine if
5075 * the entryfunc should be called directly or not.
5076 */
5077 __ftrace_graph_entry = entryfunc;
5078 ftrace_graph_entry = ftrace_graph_entry_test;
5079 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005080
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005081 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005082
5083out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005084 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005085 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005086}
5087
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005088void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005089{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005090 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005091
Steven Rostedt597af812009-04-03 15:24:12 -04005092 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005093 goto out;
5094
Steven Rostedt597af812009-04-03 15:24:12 -04005095 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005096 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005097 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005098 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005099 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005100 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005101 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005102
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005103 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005104 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005105}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005106
Steven Rostedt868baf02011-02-10 21:26:13 -05005107static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5108
5109static void
5110graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5111{
5112 atomic_set(&t->tracing_graph_pause, 0);
5113 atomic_set(&t->trace_overrun, 0);
5114 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005115 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005116 smp_wmb();
5117 t->ret_stack = ret_stack;
5118}
5119
5120/*
5121 * Allocate a return stack for the idle task. May be the first
5122 * time through, or it may be done by CPU hotplug online.
5123 */
5124void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5125{
5126 t->curr_ret_stack = -1;
5127 /*
5128 * The idle task has no parent, it either has its own
5129 * stack or no stack at all.
5130 */
5131 if (t->ret_stack)
5132 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5133
5134 if (ftrace_graph_active) {
5135 struct ftrace_ret_stack *ret_stack;
5136
5137 ret_stack = per_cpu(idle_ret_stack, cpu);
5138 if (!ret_stack) {
5139 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5140 * sizeof(struct ftrace_ret_stack),
5141 GFP_KERNEL);
5142 if (!ret_stack)
5143 return;
5144 per_cpu(idle_ret_stack, cpu) = ret_stack;
5145 }
5146 graph_init_task(t, ret_stack);
5147 }
5148}
5149
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005150/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005151void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005152{
Steven Rostedt84047e32009-06-02 16:51:55 -04005153 /* Make sure we do not use the parent ret_stack */
5154 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005155 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005156
Steven Rostedt597af812009-04-03 15:24:12 -04005157 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005158 struct ftrace_ret_stack *ret_stack;
5159
5160 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005161 * sizeof(struct ftrace_ret_stack),
5162 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005163 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005164 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005165 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005166 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005167}
5168
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005169void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005170{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005171 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5172
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005173 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005174 /* NULL must become visible to IRQs before we free it: */
5175 barrier();
5176
5177 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005178}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005179
5180void ftrace_graph_stop(void)
5181{
5182 ftrace_stop();
5183}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005184#endif