blob: 37f9e90d241c64906c5aaf7bf4c494d592e3e518 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -050065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
Jiri Olsae2484912012-02-15 15:51:48 +010066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040068#define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090071#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040072#define INIT_OPS_HASH(opsname)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090073#endif
74
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040075static struct ftrace_ops ftrace_list_end __read_mostly = {
76 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040077 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040078 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040079};
80
Steven Rostedt4eebcc82008-05-12 21:20:48 +020081/* ftrace_enabled is a method to turn ftrace on or off */
82int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020083static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020084
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040085/* Current function tracing op */
86struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050087/* What to set function_trace_op to */
88static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050089
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040090/* List for set_ftrace_pid's pids. */
91LIST_HEAD(ftrace_pids);
92struct ftrace_pid {
93 struct list_head list;
94 struct pid *pid;
95};
96
Steven Rostedt4eebcc82008-05-12 21:20:48 +020097/*
98 * ftrace_disabled is set when an anomaly is discovered.
99 * ftrace_disabled is much stronger than ftrace_enabled.
100 */
101static int ftrace_disabled __read_mostly;
102
Steven Rostedt52baf112009-02-14 01:15:39 -0500103static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200104
Jiri Olsae2484912012-02-15 15:51:48 +0100105static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400106static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200107ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500108ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400109static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100110static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400112#if ARCH_SUPPORTS_FTRACE_OPS
113static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400114 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400115#else
116/* See comment below, where ftrace_ops_list_func is defined */
117static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
118#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
119#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400120
Steven Rostedt0a016402012-11-02 17:03:03 -0400121/*
122 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400123 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400124 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400125 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400126 * concurrent insertions into the ftrace_global_list.
127 *
128 * Silly Alpha and silly pointer-speculation compiler optimizations!
129 */
130#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400131 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400132 do
133
134/*
135 * Optimized for just a single item in the list (as that is the normal case).
136 */
137#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 unlikely((op) != &ftrace_list_end))
140
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900141static inline void ftrace_ops_init(struct ftrace_ops *ops)
142{
143#ifdef CONFIG_DYNAMIC_FTRACE
144 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400145 mutex_init(&ops->local_hash.regex_lock);
146 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900147 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)5f8bf2d22014-07-15 11:05:12 -0400268 update_function_graph_func();
269
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500270 /* If there's no change, then do nothing more here */
271 if (ftrace_trace_function == func)
272 return;
273
274 /*
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
Christoph Lameterbdffd892014-04-29 14:17:40 -0500825 stat = this_cpu_ptr(&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);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500856 stat = this_cpu_ptr(&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,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400905 INIT_OPS_HASH(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 Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001045static struct ftrace_ops *removed_ops;
1046
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001047#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001048# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001049#endif
1050
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001051static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1052
Steven Rostedtb6887d72009-02-17 12:32:04 -05001053struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001054 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001055 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001056 unsigned long flags;
1057 unsigned long ip;
1058 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001059 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001060};
1061
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001062struct ftrace_func_entry {
1063 struct hlist_node hlist;
1064 unsigned long ip;
1065};
1066
1067struct ftrace_hash {
1068 unsigned long size_bits;
1069 struct hlist_head *buckets;
1070 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001071 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001072};
1073
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001074/*
1075 * We make these constant because no one should touch them,
1076 * but they are used as the default "empty hash", to avoid allocating
1077 * it all the time. These are in a read only section such that if
1078 * anyone does try to modify it, it will cause an exception.
1079 */
1080static const struct hlist_head empty_buckets[1];
1081static const struct ftrace_hash empty_hash = {
1082 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001083};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001084#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001085
Steven Rostedt2b499382011-05-03 22:49:52 -04001086static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001087 .func = ftrace_stub,
1088 .local_hash.notrace_hash = EMPTY_HASH,
1089 .local_hash.filter_hash = EMPTY_HASH,
1090 INIT_OPS_HASH(global_ops)
1091 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1092 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001093};
1094
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001095struct ftrace_page {
1096 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001097 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001098 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001099 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001100};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101
Steven Rostedta7900872011-12-16 16:23:44 -05001102#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1103#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001104
1105/* estimate from running different kernels */
1106#define NR_TO_INIT 10000
1107
1108static struct ftrace_page *ftrace_pages_start;
1109static struct ftrace_page *ftrace_pages;
1110
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001111static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001112{
1113 return !hash || !hash->count;
1114}
1115
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001116static struct ftrace_func_entry *
1117ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1118{
1119 unsigned long key;
1120 struct ftrace_func_entry *entry;
1121 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001122
Steven Rostedt06a51d92011-12-19 19:07:36 -05001123 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001124 return NULL;
1125
1126 if (hash->size_bits > 0)
1127 key = hash_long(ip, hash->size_bits);
1128 else
1129 key = 0;
1130
1131 hhd = &hash->buckets[key];
1132
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001133 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001134 if (entry->ip == ip)
1135 return entry;
1136 }
1137 return NULL;
1138}
1139
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001140static void __add_hash_entry(struct ftrace_hash *hash,
1141 struct ftrace_func_entry *entry)
1142{
1143 struct hlist_head *hhd;
1144 unsigned long key;
1145
1146 if (hash->size_bits)
1147 key = hash_long(entry->ip, hash->size_bits);
1148 else
1149 key = 0;
1150
1151 hhd = &hash->buckets[key];
1152 hlist_add_head(&entry->hlist, hhd);
1153 hash->count++;
1154}
1155
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001156static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1157{
1158 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001159
1160 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1161 if (!entry)
1162 return -ENOMEM;
1163
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001164 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001165 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001166
1167 return 0;
1168}
1169
1170static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001171free_hash_entry(struct ftrace_hash *hash,
1172 struct ftrace_func_entry *entry)
1173{
1174 hlist_del(&entry->hlist);
1175 kfree(entry);
1176 hash->count--;
1177}
1178
1179static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001180remove_hash_entry(struct ftrace_hash *hash,
1181 struct ftrace_func_entry *entry)
1182{
1183 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184 hash->count--;
1185}
1186
1187static void ftrace_hash_clear(struct ftrace_hash *hash)
1188{
1189 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001190 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001191 struct ftrace_func_entry *entry;
1192 int size = 1 << hash->size_bits;
1193 int i;
1194
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001195 if (!hash->count)
1196 return;
1197
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001198 for (i = 0; i < size; i++) {
1199 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001200 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001201 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001202 }
1203 FTRACE_WARN_ON(hash->count);
1204}
1205
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001206static void free_ftrace_hash(struct ftrace_hash *hash)
1207{
1208 if (!hash || hash == EMPTY_HASH)
1209 return;
1210 ftrace_hash_clear(hash);
1211 kfree(hash->buckets);
1212 kfree(hash);
1213}
1214
Steven Rostedt07fd5512011-05-05 18:03:47 -04001215static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1216{
1217 struct ftrace_hash *hash;
1218
1219 hash = container_of(rcu, struct ftrace_hash, rcu);
1220 free_ftrace_hash(hash);
1221}
1222
1223static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1224{
1225 if (!hash || hash == EMPTY_HASH)
1226 return;
1227 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1228}
1229
Jiri Olsa5500fa52012-02-15 15:51:54 +01001230void ftrace_free_filter(struct ftrace_ops *ops)
1231{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001232 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001233 free_ftrace_hash(ops->func_hash->filter_hash);
1234 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001235}
1236
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001237static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1238{
1239 struct ftrace_hash *hash;
1240 int size;
1241
1242 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1243 if (!hash)
1244 return NULL;
1245
1246 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001247 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001248
1249 if (!hash->buckets) {
1250 kfree(hash);
1251 return NULL;
1252 }
1253
1254 hash->size_bits = size_bits;
1255
1256 return hash;
1257}
1258
1259static struct ftrace_hash *
1260alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1261{
1262 struct ftrace_func_entry *entry;
1263 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001264 int size;
1265 int ret;
1266 int i;
1267
1268 new_hash = alloc_ftrace_hash(size_bits);
1269 if (!new_hash)
1270 return NULL;
1271
1272 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001273 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001274 return new_hash;
1275
1276 size = 1 << hash->size_bits;
1277 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001278 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001279 ret = add_hash_entry(new_hash, entry->ip);
1280 if (ret < 0)
1281 goto free_hash;
1282 }
1283 }
1284
1285 FTRACE_WARN_ON(new_hash->count != hash->count);
1286
1287 return new_hash;
1288
1289 free_hash:
1290 free_ftrace_hash(new_hash);
1291 return NULL;
1292}
1293
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001294static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001295ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001296static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001297ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001298
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001299static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001300ftrace_hash_move(struct ftrace_ops *ops, int enable,
1301 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302{
1303 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001304 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001305 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001306 struct ftrace_hash *old_hash;
1307 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001308 int size = src->count;
1309 int bits = 0;
1310 int i;
1311
1312 /*
1313 * If the new source is empty, just free dst and assign it
1314 * the empty_hash.
1315 */
1316 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001317 new_hash = EMPTY_HASH;
1318 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319 }
1320
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001321 /*
1322 * Make the hash size about 1/2 the # found
1323 */
1324 for (size /= 2; size; size >>= 1)
1325 bits++;
1326
1327 /* Don't allocate too much */
1328 if (bits > FTRACE_HASH_MAX_BITS)
1329 bits = FTRACE_HASH_MAX_BITS;
1330
Steven Rostedt07fd5512011-05-05 18:03:47 -04001331 new_hash = alloc_ftrace_hash(bits);
1332 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001333 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001334
1335 size = 1 << src->size_bits;
1336 for (i = 0; i < size; i++) {
1337 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001338 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001339 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001340 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001341 }
1342 }
1343
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001344update:
1345 /*
1346 * Remove the current set, update the hash and add
1347 * them back.
1348 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001349 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001350
Steven Rostedt07fd5512011-05-05 18:03:47 -04001351 old_hash = *dst;
1352 rcu_assign_pointer(*dst, new_hash);
1353 free_ftrace_hash_rcu(old_hash);
1354
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001355 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001356
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001357 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001358}
1359
Steven Rostedt265c8312009-02-13 12:43:56 -05001360/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001361 * Test the hashes for this ops to see if we want to call
1362 * the ops->func or not.
1363 *
1364 * It's a match if the ip is in the ops->filter_hash or
1365 * the filter_hash does not exist or is empty,
1366 * AND
1367 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001368 *
1369 * This needs to be called with preemption disabled as
1370 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001371 */
1372static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001373ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001374{
1375 struct ftrace_hash *filter_hash;
1376 struct ftrace_hash *notrace_hash;
1377 int ret;
1378
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001379#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1380 /*
1381 * There's a small race when adding ops that the ftrace handler
1382 * that wants regs, may be called without them. We can not
1383 * allow that handler to be called if regs is NULL.
1384 */
1385 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1386 return 0;
1387#endif
1388
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001389 filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1390 notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001391
Steven Rostedt06a51d92011-12-19 19:07:36 -05001392 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001393 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001394 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001395 !ftrace_lookup_ip(notrace_hash, ip)))
1396 ret = 1;
1397 else
1398 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001399
1400 return ret;
1401}
1402
1403/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001404 * This is a double for. Do not use 'break' to break out of the loop,
1405 * you must use a goto.
1406 */
1407#define do_for_each_ftrace_rec(pg, rec) \
1408 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1409 int _____i; \
1410 for (_____i = 0; _____i < pg->index; _____i++) { \
1411 rec = &pg->records[_____i];
1412
1413#define while_for_each_ftrace_rec() \
1414 } \
1415 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301416
Steven Rostedt5855fea2011-12-16 19:27:42 -05001417
1418static int ftrace_cmp_recs(const void *a, const void *b)
1419{
Steven Rostedta650e022012-04-25 13:48:13 -04001420 const struct dyn_ftrace *key = a;
1421 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001422
Steven Rostedta650e022012-04-25 13:48:13 -04001423 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001424 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001425 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1426 return 1;
1427 return 0;
1428}
1429
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001430static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001431{
1432 struct ftrace_page *pg;
1433 struct dyn_ftrace *rec;
1434 struct dyn_ftrace key;
1435
1436 key.ip = start;
1437 key.flags = end; /* overload flags, as it is unsigned long */
1438
1439 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1440 if (end < pg->records[0].ip ||
1441 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1442 continue;
1443 rec = bsearch(&key, pg->records, pg->index,
1444 sizeof(struct dyn_ftrace),
1445 ftrace_cmp_recs);
1446 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001447 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001448 }
1449
Steven Rostedt5855fea2011-12-16 19:27:42 -05001450 return 0;
1451}
1452
Steven Rostedtc88fd862011-08-16 09:53:39 -04001453/**
1454 * ftrace_location - return true if the ip giving is a traced location
1455 * @ip: the instruction pointer to check
1456 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001457 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001458 * That is, the instruction that is either a NOP or call to
1459 * the function tracer. It checks the ftrace internal tables to
1460 * determine if the address belongs or not.
1461 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001462unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001463{
Steven Rostedta650e022012-04-25 13:48:13 -04001464 return ftrace_location_range(ip, ip);
1465}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001466
Steven Rostedta650e022012-04-25 13:48:13 -04001467/**
1468 * ftrace_text_reserved - return true if range contains an ftrace location
1469 * @start: start of range to search
1470 * @end: end of range to search (inclusive). @end points to the last byte to check.
1471 *
1472 * Returns 1 if @start and @end contains a ftrace location.
1473 * That is, the instruction that is either a NOP or call to
1474 * the function tracer. It checks the ftrace internal tables to
1475 * determine if the address belongs or not.
1476 */
Sasha Levind88471c2013-01-09 18:09:20 -05001477int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001478{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001479 unsigned long ret;
1480
1481 ret = ftrace_location_range((unsigned long)start,
1482 (unsigned long)end);
1483
1484 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001485}
1486
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001487/* Test if ops registered to this rec needs regs */
1488static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1489{
1490 struct ftrace_ops *ops;
1491 bool keep_regs = false;
1492
1493 for (ops = ftrace_ops_list;
1494 ops != &ftrace_list_end; ops = ops->next) {
1495 /* pass rec in as regs to have non-NULL val */
1496 if (ftrace_ops_test(ops, rec->ip, rec)) {
1497 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1498 keep_regs = true;
1499 break;
1500 }
1501 }
1502 }
1503
1504 return keep_regs;
1505}
1506
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001507static void ftrace_remove_tramp(struct ftrace_ops *ops,
1508 struct dyn_ftrace *rec)
1509{
1510 struct ftrace_func_entry *entry;
1511
1512 entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1513 if (!entry)
1514 return;
1515
1516 /*
1517 * The tramp_hash entry will be removed at time
1518 * of update.
1519 */
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001520 ops->nr_trampolines--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001521 rec->flags &= ~FTRACE_FL_TRAMP;
1522}
1523
1524static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1525{
1526 struct ftrace_ops *op;
1527
1528 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001529 if (op->nr_trampolines)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001530 ftrace_remove_tramp(op, rec);
1531 } while_for_each_ftrace_op(op);
1532}
1533
Steven Rostedted926f92011-05-03 13:25:24 -04001534static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1535 int filter_hash,
1536 bool inc)
1537{
1538 struct ftrace_hash *hash;
1539 struct ftrace_hash *other_hash;
1540 struct ftrace_page *pg;
1541 struct dyn_ftrace *rec;
1542 int count = 0;
1543 int all = 0;
1544
1545 /* Only update if the ops has been registered */
1546 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1547 return;
1548
1549 /*
1550 * In the filter_hash case:
1551 * If the count is zero, we update all records.
1552 * Otherwise we just update the items in the hash.
1553 *
1554 * In the notrace_hash case:
1555 * We enable the update in the hash.
1556 * As disabling notrace means enabling the tracing,
1557 * and enabling notrace means disabling, the inc variable
1558 * gets inversed.
1559 */
1560 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001561 hash = ops->func_hash->filter_hash;
1562 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001563 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001564 all = 1;
1565 } else {
1566 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001567 hash = ops->func_hash->notrace_hash;
1568 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001569 /*
1570 * If the notrace hash has no items,
1571 * then there's nothing to do.
1572 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001573 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001574 return;
1575 }
1576
1577 do_for_each_ftrace_rec(pg, rec) {
1578 int in_other_hash = 0;
1579 int in_hash = 0;
1580 int match = 0;
1581
1582 if (all) {
1583 /*
1584 * Only the filter_hash affects all records.
1585 * Update if the record is not in the notrace hash.
1586 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001587 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001588 match = 1;
1589 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001590 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1591 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001592
1593 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001594 * If filter_hash is set, we want to match all functions
1595 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001596 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001597 * If filter_hash is not set, then we are decrementing.
1598 * That means we match anything that is in the hash
1599 * and also in the other_hash. That is, we need to turn
1600 * off functions in the other hash because they are disabled
1601 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001602 */
1603 if (filter_hash && in_hash && !in_other_hash)
1604 match = 1;
1605 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001606 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001607 match = 1;
1608 }
1609 if (!match)
1610 continue;
1611
1612 if (inc) {
1613 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001614 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001615 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001616
1617 /*
1618 * If there's only a single callback registered to a
1619 * function, and the ops has a trampoline registered
1620 * for it, then we can call it directly.
1621 */
1622 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1623 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001624 ops->nr_trampolines++;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001625 } else {
1626 /*
1627 * If we are adding another function callback
1628 * to this function, and the previous had a
1629 * trampoline used, then we need to go back to
1630 * the default trampoline.
1631 */
1632 rec->flags &= ~FTRACE_FL_TRAMP;
1633
1634 /* remove trampolines from any ops for this rec */
1635 ftrace_clear_tramps(rec);
1636 }
1637
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001638 /*
1639 * If any ops wants regs saved for this function
1640 * then all ops will get saved regs.
1641 */
1642 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1643 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001644 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001645 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001646 return;
1647 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001648
1649 if (ops->trampoline && !ftrace_rec_count(rec))
1650 ftrace_remove_tramp(ops, rec);
1651
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001652 /*
1653 * If the rec had REGS enabled and the ops that is
1654 * being removed had REGS set, then see if there is
1655 * still any ops for this record that wants regs.
1656 * If not, we can stop recording them.
1657 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001658 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001659 rec->flags & FTRACE_FL_REGS &&
1660 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1661 if (!test_rec_ops_needs_regs(rec))
1662 rec->flags &= ~FTRACE_FL_REGS;
1663 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001664
1665 /*
1666 * flags will be cleared in ftrace_check_record()
1667 * if rec count is zero.
1668 */
Steven Rostedted926f92011-05-03 13:25:24 -04001669 }
1670 count++;
1671 /* Shortcut, if we handled all records, we are done. */
1672 if (!all && count == hash->count)
1673 return;
1674 } while_for_each_ftrace_rec();
1675}
1676
1677static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1678 int filter_hash)
1679{
1680 __ftrace_hash_rec_update(ops, filter_hash, 0);
1681}
1682
1683static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1684 int filter_hash)
1685{
1686 __ftrace_hash_rec_update(ops, filter_hash, 1);
1687}
1688
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001689static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1690 int filter_hash, int inc)
1691{
1692 struct ftrace_ops *op;
1693
1694 __ftrace_hash_rec_update(ops, filter_hash, inc);
1695
1696 if (ops->func_hash != &global_ops.local_hash)
1697 return;
1698
1699 /*
1700 * If the ops shares the global_ops hash, then we need to update
1701 * all ops that are enabled and use this hash.
1702 */
1703 do_for_each_ftrace_op(op, ftrace_ops_list) {
1704 /* Already done */
1705 if (op == ops)
1706 continue;
1707 if (op->func_hash == &global_ops.local_hash)
1708 __ftrace_hash_rec_update(op, filter_hash, inc);
1709 } while_for_each_ftrace_op(op);
1710}
1711
1712static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1713 int filter_hash)
1714{
1715 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1716}
1717
1718static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1719 int filter_hash)
1720{
1721 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1722}
1723
Steven Rostedt05736a42008-09-22 14:55:47 -07001724static void print_ip_ins(const char *fmt, unsigned char *p)
1725{
1726 int i;
1727
1728 printk(KERN_CONT "%s", fmt);
1729
1730 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1731 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1732}
1733
Steven Rostedtc88fd862011-08-16 09:53:39 -04001734/**
1735 * ftrace_bug - report and shutdown function tracer
1736 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1737 * @ip: The address that failed
1738 *
1739 * The arch code that enables or disables the function tracing
1740 * can call ftrace_bug() when it has detected a problem in
1741 * modifying the code. @failed should be one of either:
1742 * EFAULT - if the problem happens on reading the @ip address
1743 * EINVAL - if what is read at @ip is not what was expected
1744 * EPERM - if the problem happens on writting to the @ip address
1745 */
1746void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001747{
1748 switch (failed) {
1749 case -EFAULT:
1750 FTRACE_WARN_ON_ONCE(1);
1751 pr_info("ftrace faulted on modifying ");
1752 print_ip_sym(ip);
1753 break;
1754 case -EINVAL:
1755 FTRACE_WARN_ON_ONCE(1);
1756 pr_info("ftrace failed to modify ");
1757 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001758 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001759 printk(KERN_CONT "\n");
1760 break;
1761 case -EPERM:
1762 FTRACE_WARN_ON_ONCE(1);
1763 pr_info("ftrace faulted on writing ");
1764 print_ip_sym(ip);
1765 break;
1766 default:
1767 FTRACE_WARN_ON_ONCE(1);
1768 pr_info("ftrace faulted on unknown error ");
1769 print_ip_sym(ip);
1770 }
1771}
1772
Steven Rostedtc88fd862011-08-16 09:53:39 -04001773static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001774{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001775 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001776
Steven Rostedt982c3502008-11-15 16:31:41 -05001777 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001778 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001779 *
Steven Rostedted926f92011-05-03 13:25:24 -04001780 * If the record has a ref count, then we need to enable it
1781 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001782 *
Steven Rostedted926f92011-05-03 13:25:24 -04001783 * Otherwise we make sure its disabled.
1784 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001785 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001786 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001787 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001788 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001789 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001790
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001791 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001792 * If enabling and the REGS flag does not match the REGS_EN, or
1793 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1794 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001795 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001796 if (flag) {
1797 if (!(rec->flags & FTRACE_FL_REGS) !=
1798 !(rec->flags & FTRACE_FL_REGS_EN))
1799 flag |= FTRACE_FL_REGS;
1800
1801 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1802 !(rec->flags & FTRACE_FL_TRAMP_EN))
1803 flag |= FTRACE_FL_TRAMP;
1804 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001805
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001806 /* If the state of this record hasn't changed, then do nothing */
1807 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001808 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001809
1810 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001811 /* Save off if rec is being enabled (for return value) */
1812 flag ^= rec->flags & FTRACE_FL_ENABLED;
1813
1814 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001815 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001816 if (flag & FTRACE_FL_REGS) {
1817 if (rec->flags & FTRACE_FL_REGS)
1818 rec->flags |= FTRACE_FL_REGS_EN;
1819 else
1820 rec->flags &= ~FTRACE_FL_REGS_EN;
1821 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001822 if (flag & FTRACE_FL_TRAMP) {
1823 if (rec->flags & FTRACE_FL_TRAMP)
1824 rec->flags |= FTRACE_FL_TRAMP_EN;
1825 else
1826 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1827 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001828 }
1829
1830 /*
1831 * If this record is being updated from a nop, then
1832 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001833 * Otherwise,
1834 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001835 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001836 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001837 */
1838 if (flag & FTRACE_FL_ENABLED)
1839 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001840
1841 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001842 }
1843
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001844 if (update) {
1845 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001846 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001847 rec->flags = 0;
1848 else
1849 /* Just disable the record (keep REGS state) */
1850 rec->flags &= ~FTRACE_FL_ENABLED;
1851 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001852
1853 return FTRACE_UPDATE_MAKE_NOP;
1854}
1855
1856/**
1857 * ftrace_update_record, set a record that now is tracing or not
1858 * @rec: the record to update
1859 * @enable: set to 1 if the record is tracing, zero to force disable
1860 *
1861 * The records that represent all functions that can be traced need
1862 * to be updated when tracing has been enabled.
1863 */
1864int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1865{
1866 return ftrace_check_record(rec, enable, 1);
1867}
1868
1869/**
1870 * ftrace_test_record, check if the record has been enabled or not
1871 * @rec: the record to test
1872 * @enable: set to 1 to check if enabled, 0 if it is disabled
1873 *
1874 * The arch code may need to test if a record is already set to
1875 * tracing to determine how to modify the function code that it
1876 * represents.
1877 */
1878int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1879{
1880 return ftrace_check_record(rec, enable, 0);
1881}
1882
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001883static struct ftrace_ops *
1884ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1885{
1886 struct ftrace_ops *op;
1887
1888 /* Removed ops need to be tested first */
1889 if (removed_ops && removed_ops->tramp_hash) {
1890 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1891 return removed_ops;
1892 }
1893
1894 do_for_each_ftrace_op(op, ftrace_ops_list) {
1895 if (!op->tramp_hash)
1896 continue;
1897
1898 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1899 return op;
1900
1901 } while_for_each_ftrace_op(op);
1902
1903 return NULL;
1904}
1905
1906static struct ftrace_ops *
1907ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1908{
1909 struct ftrace_ops *op;
1910
1911 do_for_each_ftrace_op(op, ftrace_ops_list) {
1912 /* pass rec in as regs to have non-NULL val */
1913 if (ftrace_ops_test(op, rec->ip, rec))
1914 return op;
1915 } while_for_each_ftrace_op(op);
1916
1917 return NULL;
1918}
1919
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001920/**
1921 * ftrace_get_addr_new - Get the call address to set to
1922 * @rec: The ftrace record descriptor
1923 *
1924 * If the record has the FTRACE_FL_REGS set, that means that it
1925 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1926 * is not not set, then it wants to convert to the normal callback.
1927 *
1928 * Returns the address of the trampoline to set to
1929 */
1930unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1931{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001932 struct ftrace_ops *ops;
1933
1934 /* Trampolines take precedence over regs */
1935 if (rec->flags & FTRACE_FL_TRAMP) {
1936 ops = ftrace_find_tramp_ops_new(rec);
1937 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1938 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1939 (void *)rec->ip, (void *)rec->ip);
1940 /* Ftrace is shutting down, return anything */
1941 return (unsigned long)FTRACE_ADDR;
1942 }
1943 return ops->trampoline;
1944 }
1945
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001946 if (rec->flags & FTRACE_FL_REGS)
1947 return (unsigned long)FTRACE_REGS_ADDR;
1948 else
1949 return (unsigned long)FTRACE_ADDR;
1950}
1951
1952/**
1953 * ftrace_get_addr_curr - Get the call address that is already there
1954 * @rec: The ftrace record descriptor
1955 *
1956 * The FTRACE_FL_REGS_EN is set when the record already points to
1957 * a function that saves all the regs. Basically the '_EN' version
1958 * represents the current state of the function.
1959 *
1960 * Returns the address of the trampoline that is currently being called
1961 */
1962unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1963{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001964 struct ftrace_ops *ops;
1965
1966 /* Trampolines take precedence over regs */
1967 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1968 ops = ftrace_find_tramp_ops_curr(rec);
1969 if (FTRACE_WARN_ON(!ops)) {
1970 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1971 (void *)rec->ip, (void *)rec->ip);
1972 /* Ftrace is shutting down, return anything */
1973 return (unsigned long)FTRACE_ADDR;
1974 }
1975 return ops->trampoline;
1976 }
1977
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001978 if (rec->flags & FTRACE_FL_REGS_EN)
1979 return (unsigned long)FTRACE_REGS_ADDR;
1980 else
1981 return (unsigned long)FTRACE_ADDR;
1982}
1983
Steven Rostedtc88fd862011-08-16 09:53:39 -04001984static int
1985__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1986{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001987 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001988 unsigned long ftrace_addr;
1989 int ret;
1990
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001991 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001992
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001993 /* This needs to be done before we call ftrace_update_record */
1994 ftrace_old_addr = ftrace_get_addr_curr(rec);
1995
1996 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001997
Steven Rostedtc88fd862011-08-16 09:53:39 -04001998 switch (ret) {
1999 case FTRACE_UPDATE_IGNORE:
2000 return 0;
2001
2002 case FTRACE_UPDATE_MAKE_CALL:
2003 return ftrace_make_call(rec, ftrace_addr);
2004
2005 case FTRACE_UPDATE_MAKE_NOP:
2006 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002007
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002008 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002009 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002010 }
2011
2012 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002013}
2014
Steven Rostedte4f5d542012-04-27 09:13:18 -04002015void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002016{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002017 struct dyn_ftrace *rec;
2018 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002019 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002020
Steven Rostedt45a4a232011-04-21 23:16:46 -04002021 if (unlikely(ftrace_disabled))
2022 return;
2023
Steven Rostedt265c8312009-02-13 12:43:56 -05002024 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002025 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002026 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04002027 ftrace_bug(failed, rec->ip);
2028 /* Stop processing */
2029 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002030 }
2031 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002032}
2033
Steven Rostedtc88fd862011-08-16 09:53:39 -04002034struct ftrace_rec_iter {
2035 struct ftrace_page *pg;
2036 int index;
2037};
2038
2039/**
2040 * ftrace_rec_iter_start, start up iterating over traced functions
2041 *
2042 * Returns an iterator handle that is used to iterate over all
2043 * the records that represent address locations where functions
2044 * are traced.
2045 *
2046 * May return NULL if no records are available.
2047 */
2048struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2049{
2050 /*
2051 * We only use a single iterator.
2052 * Protected by the ftrace_lock mutex.
2053 */
2054 static struct ftrace_rec_iter ftrace_rec_iter;
2055 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2056
2057 iter->pg = ftrace_pages_start;
2058 iter->index = 0;
2059
2060 /* Could have empty pages */
2061 while (iter->pg && !iter->pg->index)
2062 iter->pg = iter->pg->next;
2063
2064 if (!iter->pg)
2065 return NULL;
2066
2067 return iter;
2068}
2069
2070/**
2071 * ftrace_rec_iter_next, get the next record to process.
2072 * @iter: The handle to the iterator.
2073 *
2074 * Returns the next iterator after the given iterator @iter.
2075 */
2076struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2077{
2078 iter->index++;
2079
2080 if (iter->index >= iter->pg->index) {
2081 iter->pg = iter->pg->next;
2082 iter->index = 0;
2083
2084 /* Could have empty pages */
2085 while (iter->pg && !iter->pg->index)
2086 iter->pg = iter->pg->next;
2087 }
2088
2089 if (!iter->pg)
2090 return NULL;
2091
2092 return iter;
2093}
2094
2095/**
2096 * ftrace_rec_iter_record, get the record at the iterator location
2097 * @iter: The current iterator location
2098 *
2099 * Returns the record that the current @iter is at.
2100 */
2101struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2102{
2103 return &iter->pg->records[iter->index];
2104}
2105
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302106static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002107ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002108{
2109 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002110 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002111
2112 ip = rec->ip;
2113
Steven Rostedt45a4a232011-04-21 23:16:46 -04002114 if (unlikely(ftrace_disabled))
2115 return 0;
2116
Shaohua Li25aac9d2009-01-09 11:29:40 +08002117 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002118 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002119 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302120 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002121 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302122 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002123}
2124
Steven Rostedt000ab692009-02-17 13:35:06 -05002125/*
2126 * archs can override this function if they must do something
2127 * before the modifying code is performed.
2128 */
2129int __weak ftrace_arch_code_modify_prepare(void)
2130{
2131 return 0;
2132}
2133
2134/*
2135 * archs can override this function if they must do something
2136 * after the modifying code is performed.
2137 */
2138int __weak ftrace_arch_code_modify_post_process(void)
2139{
2140 return 0;
2141}
2142
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002143void ftrace_modify_all_code(int command)
2144{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002145 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002146 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002147
2148 /*
2149 * If the ftrace_caller calls a ftrace_ops func directly,
2150 * we need to make sure that it only traces functions it
2151 * expects to trace. When doing the switch of functions,
2152 * we need to update to the ftrace_ops_list_func first
2153 * before the transition between old and new calls are set,
2154 * as the ftrace_ops_list_func will check the ops hashes
2155 * to make sure the ops are having the right functions
2156 * traced.
2157 */
Petr Mladekcd210672014-02-24 17:12:21 +01002158 if (update) {
2159 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2160 if (FTRACE_WARN_ON(err))
2161 return;
2162 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002163
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002164 if (command & FTRACE_UPDATE_CALLS)
2165 ftrace_replace_code(1);
2166 else if (command & FTRACE_DISABLE_CALLS)
2167 ftrace_replace_code(0);
2168
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002169 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2170 function_trace_op = set_function_trace_op;
2171 smp_wmb();
2172 /* If irqs are disabled, we are in stop machine */
2173 if (!irqs_disabled())
2174 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002175 err = ftrace_update_ftrace_func(ftrace_trace_function);
2176 if (FTRACE_WARN_ON(err))
2177 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002178 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002179
2180 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002181 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002182 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002183 err = ftrace_disable_ftrace_graph_caller();
2184 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002185}
2186
Ingo Molnare309b412008-05-12 21:20:51 +02002187static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002188{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002189 int *command = data;
2190
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002191 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002192
Steven Rostedtc88fd862011-08-16 09:53:39 -04002193 return 0;
2194}
2195
2196/**
2197 * ftrace_run_stop_machine, go back to the stop machine method
2198 * @command: The command to tell ftrace what to do
2199 *
2200 * If an arch needs to fall back to the stop machine method, the
2201 * it can call this function.
2202 */
2203void ftrace_run_stop_machine(int command)
2204{
2205 stop_machine(__ftrace_modify_code, &command, NULL);
2206}
2207
2208/**
2209 * arch_ftrace_update_code, modify the code to trace or not trace
2210 * @command: The command that needs to be done
2211 *
2212 * Archs can override this function if it does not need to
2213 * run stop_machine() to modify code.
2214 */
2215void __weak arch_ftrace_update_code(int command)
2216{
2217 ftrace_run_stop_machine(command);
2218}
2219
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002220static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2221{
2222 struct ftrace_page *pg;
2223 struct dyn_ftrace *rec;
2224 int size, bits;
2225 int ret;
2226
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002227 size = ops->nr_trampolines;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002228 bits = 0;
2229 /*
2230 * Make the hash size about 1/2 the # found
2231 */
2232 for (size /= 2; size; size >>= 1)
2233 bits++;
2234
2235 ops->tramp_hash = alloc_ftrace_hash(bits);
2236 /*
2237 * TODO: a failed allocation is going to screw up
2238 * the accounting of what needs to be modified
2239 * and not. For now, we kill ftrace if we fail
2240 * to allocate here. But there are ways around this,
2241 * but that will take a little more work.
2242 */
2243 if (!ops->tramp_hash)
2244 return -ENOMEM;
2245
2246 do_for_each_ftrace_rec(pg, rec) {
2247 if (ftrace_rec_count(rec) == 1 &&
2248 ftrace_ops_test(ops, rec->ip, rec)) {
2249
Steven Rostedt (Red Hat)2a0343b2014-07-24 09:56:27 -04002250 /*
2251 * If another ops adds to a rec, the rec will
2252 * lose its trampoline and never get it back
2253 * until all ops are off of it.
2254 */
2255 if (!(rec->flags & FTRACE_FL_TRAMP))
2256 continue;
2257
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002258 /* This record had better have a trampoline */
2259 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2260 return -1;
2261
2262 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2263 if (ret < 0)
2264 return ret;
2265 }
2266 } while_for_each_ftrace_rec();
2267
Steven Rostedt (Red Hat)dc6f03f2014-07-24 11:26:11 -04002268 /* The number of recs in the hash must match nr_trampolines */
2269 FTRACE_WARN_ON(ops->tramp_hash->count != ops->nr_trampolines);
2270
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002271 return 0;
2272}
2273
2274static int ftrace_save_tramp_hashes(void)
2275{
2276 struct ftrace_ops *op;
2277 int ret;
2278
2279 /*
2280 * Now that any trampoline is being used, we need to save the
2281 * hashes for the ops that have them. This allows the mapping
2282 * back from the record to the ops that has the trampoline to
2283 * know what code is being replaced. Modifying code must always
2284 * verify what it is changing.
2285 */
2286 do_for_each_ftrace_op(op, ftrace_ops_list) {
2287
2288 /* The tramp_hash is recreated each time. */
2289 free_ftrace_hash(op->tramp_hash);
2290 op->tramp_hash = NULL;
2291
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002292 if (op->nr_trampolines) {
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002293 ret = ftrace_save_ops_tramp_hash(op);
2294 if (ret)
2295 return ret;
2296 }
2297
2298 } while_for_each_ftrace_op(op);
2299
2300 return 0;
2301}
2302
Steven Rostedtc88fd862011-08-16 09:53:39 -04002303static void ftrace_run_update_code(int command)
2304{
2305 int ret;
2306
2307 ret = ftrace_arch_code_modify_prepare();
2308 FTRACE_WARN_ON(ret);
2309 if (ret)
2310 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002311
2312 /*
2313 * By default we use stop_machine() to modify the code.
2314 * But archs can do what ever they want as long as it
2315 * is safe. The stop_machine() is the safest, but also
2316 * produces the most overhead.
2317 */
2318 arch_ftrace_update_code(command);
2319
Steven Rostedt000ab692009-02-17 13:35:06 -05002320 ret = ftrace_arch_code_modify_post_process();
2321 FTRACE_WARN_ON(ret);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002322
2323 ret = ftrace_save_tramp_hashes();
2324 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002325}
2326
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002327static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002328static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002329
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002330static void control_ops_free(struct ftrace_ops *ops)
2331{
2332 free_percpu(ops->disabled);
2333}
2334
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002335static void ftrace_startup_enable(int command)
2336{
2337 if (saved_ftrace_func != ftrace_trace_function) {
2338 saved_ftrace_func = ftrace_trace_function;
2339 command |= FTRACE_UPDATE_TRACE_FUNC;
2340 }
2341
2342 if (!command || !ftrace_enabled)
2343 return;
2344
2345 ftrace_run_update_code(command);
2346}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002347
Steven Rostedta1cd6172011-05-23 15:24:25 -04002348static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002349{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002350 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002351
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002352 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002353 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002354
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002355 ret = __register_ftrace_function(ops);
2356 if (ret)
2357 return ret;
2358
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002359 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002360 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002361
Steven Rostedted926f92011-05-03 13:25:24 -04002362 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002363
2364 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002365
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002366 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002367
2368 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002369}
2370
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002371static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002372{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002373 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002374
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002375 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002376 return -ENODEV;
2377
2378 ret = __unregister_ftrace_function(ops);
2379 if (ret)
2380 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002381
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002382 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002383 /*
2384 * Just warn in case of unbalance, no need to kill ftrace, it's not
2385 * critical but the ftrace_call callers may be never nopped again after
2386 * further ftrace uses.
2387 */
2388 WARN_ON_ONCE(ftrace_start_up < 0);
2389
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002390 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002391
Namhyung Kima737e6d2014-06-12 23:56:12 +09002392 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002393
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002394 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002395
2396 if (saved_ftrace_func != ftrace_trace_function) {
2397 saved_ftrace_func = ftrace_trace_function;
2398 command |= FTRACE_UPDATE_TRACE_FUNC;
2399 }
2400
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002401 if (!command || !ftrace_enabled) {
2402 /*
2403 * If these are control ops, they still need their
2404 * per_cpu field freed. Since, function tracing is
2405 * not currently active, we can just free them
2406 * without synchronizing all CPUs.
2407 */
2408 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2409 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002410 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002411 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002412
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002413 /*
2414 * If the ops uses a trampoline, then it needs to be
2415 * tested first on update.
2416 */
2417 removed_ops = ops;
2418
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002419 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002420
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002421 removed_ops = NULL;
2422
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002423 /*
2424 * Dynamic ops may be freed, we must make sure that all
2425 * callers are done before leaving this function.
2426 * The same goes for freeing the per_cpu data of the control
2427 * ops.
2428 *
2429 * Again, normal synchronize_sched() is not good enough.
2430 * We need to do a hard force of sched synchronization.
2431 * This is because we use preempt_disable() to do RCU, but
2432 * the function tracers can be called where RCU is not watching
2433 * (like before user_exit()). We can not rely on the RCU
2434 * infrastructure to do the synchronization, thus we must do it
2435 * ourselves.
2436 */
2437 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2438 schedule_on_each_cpu(ftrace_sync);
2439
2440 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2441 control_ops_free(ops);
2442 }
2443
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002444 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002445}
2446
Ingo Molnare309b412008-05-12 21:20:51 +02002447static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002448{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002449 if (unlikely(ftrace_disabled))
2450 return;
2451
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002452 /* Force update next time */
2453 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002454 /* ftrace_start_up is true if we want ftrace running */
2455 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002456 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002457}
2458
Ingo Molnare309b412008-05-12 21:20:51 +02002459static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002460{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002461 if (unlikely(ftrace_disabled))
2462 return;
2463
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002464 /* ftrace_start_up is true if ftrace is running */
2465 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002466 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002467}
2468
Steven Rostedt3d083392008-05-12 21:20:42 +02002469static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002470unsigned long ftrace_update_tot_cnt;
2471
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002472static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002473{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002474 /*
2475 * Filter_hash being empty will default to trace module.
2476 * But notrace hash requires a test of individual module functions.
2477 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002478 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2479 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002480}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002481
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002482/*
2483 * Check if the current ops references the record.
2484 *
2485 * If the ops traces all functions, then it was already accounted for.
2486 * If the ops does not trace the current record function, skip it.
2487 * If the ops ignores the function via notrace filter, skip it.
2488 */
2489static inline bool
2490ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2491{
2492 /* If ops isn't enabled, ignore it */
2493 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2494 return 0;
2495
2496 /* If ops traces all mods, we already accounted for it */
2497 if (ops_traces_mod(ops))
2498 return 0;
2499
2500 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002501 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2502 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002503 return 0;
2504
2505 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002506 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002507 return 0;
2508
2509 return 1;
2510}
2511
2512static int referenced_filters(struct dyn_ftrace *rec)
2513{
2514 struct ftrace_ops *ops;
2515 int cnt = 0;
2516
2517 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2518 if (ops_references_rec(ops, rec))
2519 cnt++;
2520 }
2521
2522 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002523}
2524
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002525static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002526{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002527 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002528 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302529 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002530 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002531 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002532 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002533 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002534
2535 /*
2536 * When adding a module, we need to check if tracers are
2537 * currently enabled and if they are set to trace all functions.
2538 * If they are, we need to enable the module functions as well
2539 * as update the reference counts for those function records.
2540 */
2541 if (mod) {
2542 struct ftrace_ops *ops;
2543
2544 for (ops = ftrace_ops_list;
2545 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002546 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2547 if (ops_traces_mod(ops))
2548 ref++;
2549 else
2550 test = true;
2551 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002552 }
2553 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002554
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002555 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002556
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002557 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302558
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002559 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002560 int cnt = ref;
2561
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002562 /* If something went wrong, bail without enabling anything */
2563 if (unlikely(ftrace_disabled))
2564 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002565
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002566 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002567 if (test)
2568 cnt += referenced_filters(p);
2569 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302570
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002571 /*
2572 * Do the initial record conversion from mcount jump
2573 * to the NOP instructions.
2574 */
2575 if (!ftrace_code_disable(mod, p))
2576 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002577
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002578 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002579
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002580 /*
2581 * If the tracing is enabled, go ahead and enable the record.
2582 *
2583 * The reason not to enable the record immediatelly is the
2584 * inherent check of ftrace_make_nop/ftrace_make_call for
2585 * correct previous instructions. Making first the NOP
2586 * conversion puts the module to the correct state, thus
2587 * passing the ftrace_make_call check.
2588 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002589 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002590 int failed = __ftrace_replace_code(p, 1);
2591 if (failed)
2592 ftrace_bug(failed, p->ip);
2593 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002594 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002595 }
2596
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002597 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002598 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002599 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002600
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002601 return 0;
2602}
2603
Steven Rostedta7900872011-12-16 16:23:44 -05002604static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002605{
Steven Rostedta7900872011-12-16 16:23:44 -05002606 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002607 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002608
Steven Rostedta7900872011-12-16 16:23:44 -05002609 if (WARN_ON(!count))
2610 return -EINVAL;
2611
2612 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002613
2614 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002615 * We want to fill as much as possible. No more than a page
2616 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002617 */
Steven Rostedta7900872011-12-16 16:23:44 -05002618 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2619 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002620
Steven Rostedta7900872011-12-16 16:23:44 -05002621 again:
2622 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2623
2624 if (!pg->records) {
2625 /* if we can't allocate this size, try something smaller */
2626 if (!order)
2627 return -ENOMEM;
2628 order >>= 1;
2629 goto again;
2630 }
2631
2632 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2633 pg->size = cnt;
2634
2635 if (cnt > count)
2636 cnt = count;
2637
2638 return cnt;
2639}
2640
2641static struct ftrace_page *
2642ftrace_allocate_pages(unsigned long num_to_init)
2643{
2644 struct ftrace_page *start_pg;
2645 struct ftrace_page *pg;
2646 int order;
2647 int cnt;
2648
2649 if (!num_to_init)
2650 return 0;
2651
2652 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2653 if (!pg)
2654 return NULL;
2655
2656 /*
2657 * Try to allocate as much as possible in one continues
2658 * location that fills in all of the space. We want to
2659 * waste as little space as possible.
2660 */
2661 for (;;) {
2662 cnt = ftrace_allocate_records(pg, num_to_init);
2663 if (cnt < 0)
2664 goto free_pages;
2665
2666 num_to_init -= cnt;
2667 if (!num_to_init)
2668 break;
2669
2670 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2671 if (!pg->next)
2672 goto free_pages;
2673
2674 pg = pg->next;
2675 }
2676
2677 return start_pg;
2678
2679 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002680 pg = start_pg;
2681 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002682 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2683 free_pages((unsigned long)pg->records, order);
2684 start_pg = pg->next;
2685 kfree(pg);
2686 pg = start_pg;
2687 }
2688 pr_info("ftrace: FAILED to allocate memory for functions\n");
2689 return NULL;
2690}
2691
Steven Rostedt5072c592008-05-12 21:20:43 +02002692#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2693
2694struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002695 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002696 loff_t func_pos;
2697 struct ftrace_page *pg;
2698 struct dyn_ftrace *func;
2699 struct ftrace_func_probe *probe;
2700 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002701 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002702 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002703 int hidx;
2704 int idx;
2705 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002706};
2707
Ingo Molnare309b412008-05-12 21:20:51 +02002708static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002709t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002710{
2711 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002712 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002713 struct hlist_head *hhd;
2714
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002715 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002716 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002717
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002718 if (iter->probe)
2719 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002720 retry:
2721 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2722 return NULL;
2723
2724 hhd = &ftrace_func_hash[iter->hidx];
2725
2726 if (hlist_empty(hhd)) {
2727 iter->hidx++;
2728 hnd = NULL;
2729 goto retry;
2730 }
2731
2732 if (!hnd)
2733 hnd = hhd->first;
2734 else {
2735 hnd = hnd->next;
2736 if (!hnd) {
2737 iter->hidx++;
2738 goto retry;
2739 }
2740 }
2741
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002742 if (WARN_ON_ONCE(!hnd))
2743 return NULL;
2744
2745 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2746
2747 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002748}
2749
2750static void *t_hash_start(struct seq_file *m, loff_t *pos)
2751{
2752 struct ftrace_iterator *iter = m->private;
2753 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002754 loff_t l;
2755
Steven Rostedt69a30832011-12-19 15:21:16 -05002756 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2757 return NULL;
2758
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002759 if (iter->func_pos > *pos)
2760 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002761
Li Zefand82d6242009-06-24 09:54:54 +08002762 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002763 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002764 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002765 if (!p)
2766 break;
2767 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002768 if (!p)
2769 return NULL;
2770
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002771 /* Only set this if we have an item */
2772 iter->flags |= FTRACE_ITER_HASH;
2773
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002774 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002775}
2776
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002777static int
2778t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002779{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002780 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002781
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002782 rec = iter->probe;
2783 if (WARN_ON_ONCE(!rec))
2784 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002785
Steven Rostedt809dcf22009-02-16 23:06:01 -05002786 if (rec->ops->print)
2787 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2788
Steven Rostedtb375a112009-09-17 00:05:58 -04002789 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002790
2791 if (rec->data)
2792 seq_printf(m, ":%p", rec->data);
2793 seq_putc(m, '\n');
2794
2795 return 0;
2796}
2797
2798static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002799t_next(struct seq_file *m, void *v, loff_t *pos)
2800{
2801 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002802 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002803 struct dyn_ftrace *rec = NULL;
2804
Steven Rostedt45a4a232011-04-21 23:16:46 -04002805 if (unlikely(ftrace_disabled))
2806 return NULL;
2807
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002808 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002809 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002810
Steven Rostedt5072c592008-05-12 21:20:43 +02002811 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002812 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002813
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002814 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002815 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002816
Steven Rostedt5072c592008-05-12 21:20:43 +02002817 retry:
2818 if (iter->idx >= iter->pg->index) {
2819 if (iter->pg->next) {
2820 iter->pg = iter->pg->next;
2821 iter->idx = 0;
2822 goto retry;
2823 }
2824 } else {
2825 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002826 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002827 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002828
Steven Rostedt41c52c02008-05-22 11:46:33 -04002829 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002830 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04002831
2832 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002833 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002834
Steven Rostedt5072c592008-05-12 21:20:43 +02002835 rec = NULL;
2836 goto retry;
2837 }
2838 }
2839
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002840 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002841 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002842
2843 iter->func = rec;
2844
2845 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002846}
2847
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002848static void reset_iter_read(struct ftrace_iterator *iter)
2849{
2850 iter->pos = 0;
2851 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002852 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002853}
2854
2855static void *t_start(struct seq_file *m, loff_t *pos)
2856{
2857 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002858 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002859 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002860 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002861
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002862 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002863
2864 if (unlikely(ftrace_disabled))
2865 return NULL;
2866
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002867 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002868 * If an lseek was done, then reset and start from beginning.
2869 */
2870 if (*pos < iter->pos)
2871 reset_iter_read(iter);
2872
2873 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002874 * For set_ftrace_filter reading, if we have the filter
2875 * off, we can short cut and just print out that all
2876 * functions are enabled.
2877 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002878 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002879 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002880 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002881 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002882 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002883 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002884 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002885 /* reset in case of seek/pread */
2886 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002887 return iter;
2888 }
2889
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002890 if (iter->flags & FTRACE_ITER_HASH)
2891 return t_hash_start(m, pos);
2892
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002893 /*
2894 * Unfortunately, we need to restart at ftrace_pages_start
2895 * every time we let go of the ftrace_mutex. This is because
2896 * those pointers can change without the lock.
2897 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002898 iter->pg = ftrace_pages_start;
2899 iter->idx = 0;
2900 for (l = 0; l <= *pos; ) {
2901 p = t_next(m, p, &l);
2902 if (!p)
2903 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002904 }
walimis5821e1b2008-11-15 15:19:06 +08002905
Steven Rostedt69a30832011-12-19 15:21:16 -05002906 if (!p)
2907 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002908
2909 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002910}
2911
2912static void t_stop(struct seq_file *m, void *p)
2913{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002914 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002915}
2916
2917static int t_show(struct seq_file *m, void *v)
2918{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002919 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002920 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002921
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002922 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002923 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002924
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002925 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002926 if (iter->flags & FTRACE_ITER_NOTRACE)
2927 seq_printf(m, "#### no functions disabled ####\n");
2928 else
2929 seq_printf(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002930 return 0;
2931 }
2932
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002933 rec = iter->func;
2934
Steven Rostedt5072c592008-05-12 21:20:43 +02002935 if (!rec)
2936 return 0;
2937
Steven Rostedt647bcd02011-05-03 14:39:21 -04002938 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002939 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002940 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002941 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002942 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2943 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2944 struct ftrace_ops *ops;
2945
2946 ops = ftrace_find_tramp_ops_curr(rec);
2947 if (ops && ops->trampoline)
2948 seq_printf(m, "\ttramp: %pS",
2949 (void *)ops->trampoline);
2950 else
2951 seq_printf(m, "\ttramp: ERROR!");
2952 }
2953 }
2954
Steven Rostedt647bcd02011-05-03 14:39:21 -04002955 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002956
2957 return 0;
2958}
2959
James Morris88e9d342009-09-22 16:43:43 -07002960static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002961 .start = t_start,
2962 .next = t_next,
2963 .stop = t_stop,
2964 .show = t_show,
2965};
2966
Ingo Molnare309b412008-05-12 21:20:51 +02002967static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002968ftrace_avail_open(struct inode *inode, struct file *file)
2969{
2970 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002971
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002972 if (unlikely(ftrace_disabled))
2973 return -ENODEV;
2974
Jiri Olsa50e18b92012-04-25 10:23:39 +02002975 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2976 if (iter) {
2977 iter->pg = ftrace_pages_start;
2978 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002979 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002980
Jiri Olsa50e18b92012-04-25 10:23:39 +02002981 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002982}
2983
Steven Rostedt647bcd02011-05-03 14:39:21 -04002984static int
2985ftrace_enabled_open(struct inode *inode, struct file *file)
2986{
2987 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002988
2989 if (unlikely(ftrace_disabled))
2990 return -ENODEV;
2991
Jiri Olsa50e18b92012-04-25 10:23:39 +02002992 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2993 if (iter) {
2994 iter->pg = ftrace_pages_start;
2995 iter->flags = FTRACE_ITER_ENABLED;
2996 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002997 }
2998
Jiri Olsa50e18b92012-04-25 10:23:39 +02002999 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003000}
3001
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003002/**
3003 * ftrace_regex_open - initialize function tracer filter files
3004 * @ops: The ftrace_ops that hold the hash filters
3005 * @flag: The type of filter to process
3006 * @inode: The inode, usually passed in to your open routine
3007 * @file: The file, usually passed in to your open routine
3008 *
3009 * ftrace_regex_open() initializes the filter files for the
3010 * @ops. Depending on @flag it may process the filter hash or
3011 * the notrace hash of @ops. With this called from the open
3012 * routine, you can use ftrace_filter_write() for the write
3013 * routine if @flag has FTRACE_ITER_FILTER set, or
3014 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003015 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003016 * release must call ftrace_regex_release().
3017 */
3018int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003019ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003020 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003021{
3022 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003023 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003024 int ret = 0;
3025
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003026 ftrace_ops_init(ops);
3027
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003028 if (unlikely(ftrace_disabled))
3029 return -ENODEV;
3030
Steven Rostedt5072c592008-05-12 21:20:43 +02003031 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3032 if (!iter)
3033 return -ENOMEM;
3034
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003035 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3036 kfree(iter);
3037 return -ENOMEM;
3038 }
3039
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003040 iter->ops = ops;
3041 iter->flags = flag;
3042
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003043 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003044
Steven Rostedtf45948e2011-05-02 12:29:25 -04003045 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003046 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003047 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003048 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003049
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003050 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003051 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3052
3053 if (file->f_flags & O_TRUNC)
3054 iter->hash = alloc_ftrace_hash(size_bits);
3055 else
3056 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3057
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003058 if (!iter->hash) {
3059 trace_parser_put(&iter->parser);
3060 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003061 ret = -ENOMEM;
3062 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003063 }
3064 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003065
Steven Rostedt5072c592008-05-12 21:20:43 +02003066 if (file->f_mode & FMODE_READ) {
3067 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003068
3069 ret = seq_open(file, &show_ftrace_seq_ops);
3070 if (!ret) {
3071 struct seq_file *m = file->private_data;
3072 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003073 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003074 /* Failed */
3075 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003076 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003077 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003078 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003079 } else
3080 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003081
3082 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003083 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003084
3085 return ret;
3086}
3087
Steven Rostedt41c52c02008-05-22 11:46:33 -04003088static int
3089ftrace_filter_open(struct inode *inode, struct file *file)
3090{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003091 struct ftrace_ops *ops = inode->i_private;
3092
3093 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003094 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3095 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003096}
3097
3098static int
3099ftrace_notrace_open(struct inode *inode, struct file *file)
3100{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003101 struct ftrace_ops *ops = inode->i_private;
3102
3103 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003104 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003105}
3106
Steven Rostedt64e7c442009-02-13 17:08:48 -05003107static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003108{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003109 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003110 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003111
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003112 switch (type) {
3113 case MATCH_FULL:
3114 if (strcmp(str, regex) == 0)
3115 matched = 1;
3116 break;
3117 case MATCH_FRONT_ONLY:
3118 if (strncmp(str, regex, len) == 0)
3119 matched = 1;
3120 break;
3121 case MATCH_MIDDLE_ONLY:
3122 if (strstr(str, regex))
3123 matched = 1;
3124 break;
3125 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003126 slen = strlen(str);
3127 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003128 matched = 1;
3129 break;
3130 }
3131
3132 return matched;
3133}
3134
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003135static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003136enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003137{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003138 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003139 int ret = 0;
3140
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003141 entry = ftrace_lookup_ip(hash, rec->ip);
3142 if (not) {
3143 /* Do nothing if it doesn't exist */
3144 if (!entry)
3145 return 0;
3146
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003147 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003148 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003149 /* Do nothing if it exists */
3150 if (entry)
3151 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003152
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003153 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003154 }
3155 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003156}
3157
Steven Rostedt64e7c442009-02-13 17:08:48 -05003158static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003159ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3160 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003161{
3162 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003163 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003164
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003165 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3166
3167 if (mod) {
3168 /* module lookup requires matching the module */
3169 if (!modname || strcmp(modname, mod))
3170 return 0;
3171
3172 /* blank search means to match all funcs in the mod */
3173 if (!len)
3174 return 1;
3175 }
3176
Steven Rostedt64e7c442009-02-13 17:08:48 -05003177 return ftrace_match(str, regex, len, type);
3178}
3179
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003180static int
3181match_records(struct ftrace_hash *hash, char *buff,
3182 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003183{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003184 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003185 struct ftrace_page *pg;
3186 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003187 int type = MATCH_FULL;
3188 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003189 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003190 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003191
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003192 if (len) {
3193 type = filter_parse_regex(buff, len, &search, &not);
3194 search_len = strlen(search);
3195 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003196
Steven Rostedt52baf112009-02-14 01:15:39 -05003197 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003198
3199 if (unlikely(ftrace_disabled))
3200 goto out_unlock;
3201
Steven Rostedt265c8312009-02-13 12:43:56 -05003202 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003203 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003204 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003205 if (ret < 0) {
3206 found = ret;
3207 goto out_unlock;
3208 }
Li Zefan311d16d2009-12-08 11:15:11 +08003209 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003210 }
3211 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003212 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003213 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003214
3215 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003216}
3217
Steven Rostedt64e7c442009-02-13 17:08:48 -05003218static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003219ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003220{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003221 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003222}
3223
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003224static int
3225ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003226{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003227 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003228
Steven Rostedt64e7c442009-02-13 17:08:48 -05003229 /* blank or '*' mean the same */
3230 if (strcmp(buff, "*") == 0)
3231 buff[0] = 0;
3232
3233 /* handle the case of 'dont filter this module' */
3234 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3235 buff[0] = 0;
3236 not = 1;
3237 }
3238
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003239 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003240}
3241
Steven Rostedtf6180772009-02-14 00:40:25 -05003242/*
3243 * We register the module command as a template to show others how
3244 * to register the a command as well.
3245 */
3246
3247static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003248ftrace_mod_callback(struct ftrace_hash *hash,
3249 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003250{
3251 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003252 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003253
3254 /*
3255 * cmd == 'mod' because we only registered this func
3256 * for the 'mod' ftrace_func_command.
3257 * But if you register one func with multiple commands,
3258 * you can tell which command was used by the cmd
3259 * parameter.
3260 */
3261
3262 /* we must have a module name */
3263 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003264 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003265
3266 mod = strsep(&param, ":");
3267 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003268 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003269
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003270 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003271 if (!ret)
3272 ret = -EINVAL;
3273 if (ret < 0)
3274 return ret;
3275
3276 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003277}
3278
3279static struct ftrace_func_command ftrace_mod_cmd = {
3280 .name = "mod",
3281 .func = ftrace_mod_callback,
3282};
3283
3284static int __init ftrace_mod_cmd_init(void)
3285{
3286 return register_ftrace_command(&ftrace_mod_cmd);
3287}
Steven Rostedt6f415672012-10-05 12:13:07 -04003288core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003289
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003290static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003291 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003292{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003293 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003294 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003295 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003296
3297 key = hash_long(ip, FTRACE_HASH_BITS);
3298
3299 hhd = &ftrace_func_hash[key];
3300
3301 if (hlist_empty(hhd))
3302 return;
3303
3304 /*
3305 * Disable preemption for these calls to prevent a RCU grace
3306 * period. This syncs the hash iteration and freeing of items
3307 * on the hash. rcu_read_lock is too dangerous here.
3308 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003309 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003310 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003311 if (entry->ip == ip)
3312 entry->ops->func(ip, parent_ip, &entry->data);
3313 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003314 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003315}
3316
Steven Rostedtb6887d72009-02-17 12:32:04 -05003317static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003318{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003319 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003320 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003321 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003322};
3323
Steven Rostedtb6887d72009-02-17 12:32:04 -05003324static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003325
Steven Rostedtb6887d72009-02-17 12:32:04 -05003326static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003327{
Steven Rostedtb8489142011-05-04 09:27:52 -04003328 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003329 int i;
3330
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003331 if (ftrace_probe_registered) {
3332 /* still need to update the function call sites */
3333 if (ftrace_enabled)
3334 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003335 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003336 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003337
3338 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3339 struct hlist_head *hhd = &ftrace_func_hash[i];
3340 if (hhd->first)
3341 break;
3342 }
3343 /* Nothing registered? */
3344 if (i == FTRACE_FUNC_HASHSIZE)
3345 return;
3346
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003347 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003348
Steven Rostedtb6887d72009-02-17 12:32:04 -05003349 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003350}
3351
Steven Rostedtb6887d72009-02-17 12:32:04 -05003352static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003353{
3354 int i;
3355
Steven Rostedtb6887d72009-02-17 12:32:04 -05003356 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003357 return;
3358
3359 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3360 struct hlist_head *hhd = &ftrace_func_hash[i];
3361 if (hhd->first)
3362 return;
3363 }
3364
3365 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003366 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003367
Steven Rostedtb6887d72009-02-17 12:32:04 -05003368 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003369}
3370
3371
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003372static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003373{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003374 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003375 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003376 kfree(entry);
3377}
3378
Steven Rostedt59df055f2009-02-14 15:29:06 -05003379int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003380register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003381 void *data)
3382{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003383 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003384 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003385 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003386 struct ftrace_page *pg;
3387 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003388 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003389 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003390 int count = 0;
3391 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003392 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003393
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003394 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003395 len = strlen(search);
3396
Steven Rostedtb6887d72009-02-17 12:32:04 -05003397 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003398 if (WARN_ON(not))
3399 return -EINVAL;
3400
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003401 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003402
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003403 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3404 if (!hash) {
3405 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003406 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003407 }
3408
3409 if (unlikely(ftrace_disabled)) {
3410 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003411 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003412 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003413
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003414 mutex_lock(&ftrace_lock);
3415
Steven Rostedt59df055f2009-02-14 15:29:06 -05003416 do_for_each_ftrace_rec(pg, rec) {
3417
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003418 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003419 continue;
3420
3421 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3422 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003423 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003424 if (!count)
3425 count = -ENOMEM;
3426 goto out_unlock;
3427 }
3428
3429 count++;
3430
3431 entry->data = data;
3432
3433 /*
3434 * The caller might want to do something special
3435 * for each function we find. We call the callback
3436 * to give the caller an opportunity to do so.
3437 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003438 if (ops->init) {
3439 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003440 /* caller does not like this func */
3441 kfree(entry);
3442 continue;
3443 }
3444 }
3445
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003446 ret = enter_record(hash, rec, 0);
3447 if (ret < 0) {
3448 kfree(entry);
3449 count = ret;
3450 goto out_unlock;
3451 }
3452
Steven Rostedt59df055f2009-02-14 15:29:06 -05003453 entry->ops = ops;
3454 entry->ip = rec->ip;
3455
3456 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3457 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3458
3459 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003460
3461 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3462 if (ret < 0)
3463 count = ret;
3464
Steven Rostedtb6887d72009-02-17 12:32:04 -05003465 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003466
3467 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003468 mutex_unlock(&ftrace_lock);
3469 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003470 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003471 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003472
3473 return count;
3474}
3475
3476enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003477 PROBE_TEST_FUNC = 1,
3478 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003479};
3480
3481static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003482__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003483 void *data, int flags)
3484{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003485 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003486 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003487 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003488 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003489 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003490 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003491 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003492 char str[KSYM_SYMBOL_LEN];
3493 int type = MATCH_FULL;
3494 int i, len = 0;
3495 char *search;
3496
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003497 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003498 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003499 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003500 int not;
3501
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003502 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003503 len = strlen(search);
3504
Steven Rostedtb6887d72009-02-17 12:32:04 -05003505 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003506 if (WARN_ON(not))
3507 return;
3508 }
3509
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003510 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003511
3512 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3513 if (!hash)
3514 /* Hmm, should report this somehow */
3515 goto out_unlock;
3516
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003517 INIT_LIST_HEAD(&free_list);
3518
Steven Rostedt59df055f2009-02-14 15:29:06 -05003519 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3520 struct hlist_head *hhd = &ftrace_func_hash[i];
3521
Sasha Levinb67bfe02013-02-27 17:06:00 -08003522 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003523
3524 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003525 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003526 continue;
3527
Steven Rostedtb6887d72009-02-17 12:32:04 -05003528 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003529 continue;
3530
3531 /* do this last, since it is the most expensive */
3532 if (glob) {
3533 kallsyms_lookup(entry->ip, NULL, NULL,
3534 NULL, str);
3535 if (!ftrace_match(str, glob, len, type))
3536 continue;
3537 }
3538
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003539 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3540 /* It is possible more than one entry had this ip */
3541 if (rec_entry)
3542 free_hash_entry(hash, rec_entry);
3543
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003544 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003545 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003546 }
3547 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003548 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003549 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003550 /*
3551 * Remove after the disable is called. Otherwise, if the last
3552 * probe is removed, a null hash means *all enabled*.
3553 */
3554 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003555 synchronize_sched();
3556 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3557 list_del(&entry->free_list);
3558 ftrace_free_entry(entry);
3559 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003560 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003561
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003562 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003563 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003564 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003565}
3566
3567void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003568unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003569 void *data)
3570{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003571 __unregister_ftrace_function_probe(glob, ops, data,
3572 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003573}
3574
3575void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003576unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003577{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003578 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003579}
3580
Steven Rostedtb6887d72009-02-17 12:32:04 -05003581void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003582{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003583 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003584}
3585
Steven Rostedtf6180772009-02-14 00:40:25 -05003586static LIST_HEAD(ftrace_commands);
3587static DEFINE_MUTEX(ftrace_cmd_mutex);
3588
Tom Zanussi38de93a2013-10-24 08:34:18 -05003589/*
3590 * Currently we only register ftrace commands from __init, so mark this
3591 * __init too.
3592 */
3593__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003594{
3595 struct ftrace_func_command *p;
3596 int ret = 0;
3597
3598 mutex_lock(&ftrace_cmd_mutex);
3599 list_for_each_entry(p, &ftrace_commands, list) {
3600 if (strcmp(cmd->name, p->name) == 0) {
3601 ret = -EBUSY;
3602 goto out_unlock;
3603 }
3604 }
3605 list_add(&cmd->list, &ftrace_commands);
3606 out_unlock:
3607 mutex_unlock(&ftrace_cmd_mutex);
3608
3609 return ret;
3610}
3611
Tom Zanussi38de93a2013-10-24 08:34:18 -05003612/*
3613 * Currently we only unregister ftrace commands from __init, so mark
3614 * this __init too.
3615 */
3616__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003617{
3618 struct ftrace_func_command *p, *n;
3619 int ret = -ENODEV;
3620
3621 mutex_lock(&ftrace_cmd_mutex);
3622 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3623 if (strcmp(cmd->name, p->name) == 0) {
3624 ret = 0;
3625 list_del_init(&p->list);
3626 goto out_unlock;
3627 }
3628 }
3629 out_unlock:
3630 mutex_unlock(&ftrace_cmd_mutex);
3631
3632 return ret;
3633}
3634
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003635static int ftrace_process_regex(struct ftrace_hash *hash,
3636 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003637{
Steven Rostedtf6180772009-02-14 00:40:25 -05003638 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003639 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003640 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003641
3642 func = strsep(&next, ":");
3643
3644 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003645 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003646 if (!ret)
3647 ret = -EINVAL;
3648 if (ret < 0)
3649 return ret;
3650 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003651 }
3652
Steven Rostedtf6180772009-02-14 00:40:25 -05003653 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003654
3655 command = strsep(&next, ":");
3656
Steven Rostedtf6180772009-02-14 00:40:25 -05003657 mutex_lock(&ftrace_cmd_mutex);
3658 list_for_each_entry(p, &ftrace_commands, list) {
3659 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003660 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003661 goto out_unlock;
3662 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003663 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003664 out_unlock:
3665 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003666
Steven Rostedtf6180772009-02-14 00:40:25 -05003667 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003668}
3669
Ingo Molnare309b412008-05-12 21:20:51 +02003670static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003671ftrace_regex_write(struct file *file, const char __user *ubuf,
3672 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003673{
3674 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003675 struct trace_parser *parser;
3676 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003677
Li Zefan4ba79782009-09-22 13:52:20 +08003678 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003679 return 0;
3680
Steven Rostedt5072c592008-05-12 21:20:43 +02003681 if (file->f_mode & FMODE_READ) {
3682 struct seq_file *m = file->private_data;
3683 iter = m->private;
3684 } else
3685 iter = file->private_data;
3686
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003687 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003688 return -ENODEV;
3689
3690 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003691
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003692 parser = &iter->parser;
3693 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003694
Li Zefan4ba79782009-09-22 13:52:20 +08003695 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003696 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003697 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003698 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003699 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003700 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003701 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003702 }
3703
Steven Rostedt5072c592008-05-12 21:20:43 +02003704 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003705 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003706 return ret;
3707}
3708
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003709ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003710ftrace_filter_write(struct file *file, const char __user *ubuf,
3711 size_t cnt, loff_t *ppos)
3712{
3713 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3714}
3715
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003716ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003717ftrace_notrace_write(struct file *file, const char __user *ubuf,
3718 size_t cnt, loff_t *ppos)
3719{
3720 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3721}
3722
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003723static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003724ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3725{
3726 struct ftrace_func_entry *entry;
3727
3728 if (!ftrace_location(ip))
3729 return -EINVAL;
3730
3731 if (remove) {
3732 entry = ftrace_lookup_ip(hash, ip);
3733 if (!entry)
3734 return -ENOENT;
3735 free_hash_entry(hash, entry);
3736 return 0;
3737 }
3738
3739 return add_hash_entry(hash, ip);
3740}
3741
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003742static void ftrace_ops_update_code(struct ftrace_ops *ops)
3743{
3744 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3745 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3746}
3747
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003748static int
3749ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3750 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003751{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003752 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003753 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003754 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003755
Steven Rostedt41c52c02008-05-22 11:46:33 -04003756 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003757 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003758
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003759 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003760
Steven Rostedtf45948e2011-05-02 12:29:25 -04003761 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003762 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003763 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003764 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003765
Wang Nanb972cc52014-07-15 08:40:20 +08003766 if (reset)
3767 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3768 else
3769 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3770
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003771 if (!hash) {
3772 ret = -ENOMEM;
3773 goto out_regex_unlock;
3774 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003775
Jiri Olsaac483c42012-01-02 10:04:14 +01003776 if (buf && !ftrace_match_records(hash, buf, len)) {
3777 ret = -EINVAL;
3778 goto out_regex_unlock;
3779 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003780 if (ip) {
3781 ret = ftrace_match_addr(hash, ip, remove);
3782 if (ret < 0)
3783 goto out_regex_unlock;
3784 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003785
3786 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003787 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003788 if (!ret)
3789 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003790
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003791 mutex_unlock(&ftrace_lock);
3792
Jiri Olsaac483c42012-01-02 10:04:14 +01003793 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003794 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003795
3796 free_ftrace_hash(hash);
3797 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003798}
3799
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003800static int
3801ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3802 int reset, int enable)
3803{
3804 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3805}
3806
3807/**
3808 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3809 * @ops - the ops to set the filter with
3810 * @ip - the address to add to or remove from the filter.
3811 * @remove - non zero to remove the ip from the filter
3812 * @reset - non zero to reset all filters before applying this filter.
3813 *
3814 * Filters denote which functions should be enabled when tracing is enabled
3815 * If @ip is NULL, it failes to update filter.
3816 */
3817int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3818 int remove, int reset)
3819{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003820 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003821 return ftrace_set_addr(ops, ip, remove, reset, 1);
3822}
3823EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3824
3825static int
3826ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3827 int reset, int enable)
3828{
3829 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3830}
3831
Steven Rostedt77a2b372008-05-12 21:20:45 +02003832/**
3833 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003834 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003835 * @buf - the string that holds the function filter text.
3836 * @len - the length of the string.
3837 * @reset - non zero to reset all filters before applying this filter.
3838 *
3839 * Filters denote which functions should be enabled when tracing is enabled.
3840 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3841 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003842int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003843 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003844{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003845 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003846 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003847}
Steven Rostedt936e0742011-05-05 22:54:01 -04003848EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003849
Steven Rostedt41c52c02008-05-22 11:46:33 -04003850/**
3851 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003852 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003853 * @buf - the string that holds the function notrace text.
3854 * @len - the length of the string.
3855 * @reset - non zero to reset all filters before applying this filter.
3856 *
3857 * Notrace Filters denote which functions should not be enabled when tracing
3858 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3859 * for tracing.
3860 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003861int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003862 int len, int reset)
3863{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003864 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003865 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003866}
3867EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3868/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003869 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003870 * @buf - the string that holds the function filter text.
3871 * @len - the length of the string.
3872 * @reset - non zero to reset all filters before applying this filter.
3873 *
3874 * Filters denote which functions should be enabled when tracing is enabled.
3875 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3876 */
3877void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3878{
3879 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3880}
3881EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3882
3883/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003884 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003885 * @buf - the string that holds the function notrace text.
3886 * @len - the length of the string.
3887 * @reset - non zero to reset all filters before applying this filter.
3888 *
3889 * Notrace Filters denote which functions should not be enabled when tracing
3890 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3891 * for tracing.
3892 */
3893void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003894{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003895 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003896}
Steven Rostedt936e0742011-05-05 22:54:01 -04003897EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003898
Steven Rostedt2af15d62009-05-28 13:37:24 -04003899/*
3900 * command line interface to allow users to set filters on boot up.
3901 */
3902#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3903static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3904static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3905
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003906/* Used by function selftest to not test if filter is set */
3907bool ftrace_filter_param __initdata;
3908
Steven Rostedt2af15d62009-05-28 13:37:24 -04003909static int __init set_ftrace_notrace(char *str)
3910{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003911 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003912 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003913 return 1;
3914}
3915__setup("ftrace_notrace=", set_ftrace_notrace);
3916
3917static int __init set_ftrace_filter(char *str)
3918{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003919 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003920 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003921 return 1;
3922}
3923__setup("ftrace_filter=", set_ftrace_filter);
3924
Stefan Assmann369bc182009-10-12 22:17:21 +02003925#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003926static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003927static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003928static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003929
Stefan Assmann369bc182009-10-12 22:17:21 +02003930static int __init set_graph_function(char *str)
3931{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003932 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003933 return 1;
3934}
3935__setup("ftrace_graph_filter=", set_graph_function);
3936
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003937static int __init set_graph_notrace_function(char *str)
3938{
3939 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3940 return 1;
3941}
3942__setup("ftrace_graph_notrace=", set_graph_notrace_function);
3943
3944static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02003945{
3946 int ret;
3947 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003948 unsigned long *table = ftrace_graph_funcs;
3949 int *count = &ftrace_graph_count;
3950
3951 if (!enable) {
3952 table = ftrace_graph_notrace_funcs;
3953 count = &ftrace_graph_notrace_count;
3954 }
Stefan Assmann369bc182009-10-12 22:17:21 +02003955
3956 while (buf) {
3957 func = strsep(&buf, ",");
3958 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003959 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003960 if (ret)
3961 printk(KERN_DEBUG "ftrace: function %s not "
3962 "traceable\n", func);
3963 }
3964}
3965#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3966
Steven Rostedt2a85a372011-12-19 21:57:44 -05003967void __init
3968ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003969{
3970 char *func;
3971
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003972 ftrace_ops_init(ops);
3973
Steven Rostedt2af15d62009-05-28 13:37:24 -04003974 while (buf) {
3975 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003976 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003977 }
3978}
3979
3980static void __init set_ftrace_early_filters(void)
3981{
3982 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003983 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003984 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003985 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003986#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3987 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003988 set_ftrace_early_graph(ftrace_graph_buf, 1);
3989 if (ftrace_graph_notrace_buf[0])
3990 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003991#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003992}
3993
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003994int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003995{
3996 struct seq_file *m = (struct seq_file *)file->private_data;
3997 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003998 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003999 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004000 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004001 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004002
Steven Rostedt5072c592008-05-12 21:20:43 +02004003 if (file->f_mode & FMODE_READ) {
4004 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004005 seq_release(inode, file);
4006 } else
4007 iter = file->private_data;
4008
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004009 parser = &iter->parser;
4010 if (trace_parser_loaded(parser)) {
4011 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004012 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004013 }
4014
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004015 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004016
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004017 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004018
Steven Rostedt058e2972011-04-29 22:35:33 -04004019 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004020 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4021
4022 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004023 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004024 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004025 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004026
Steven Rostedt058e2972011-04-29 22:35:33 -04004027 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004028 ret = ftrace_hash_move(iter->ops, filter_hash,
4029 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004030 if (!ret)
4031 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004032
Steven Rostedt058e2972011-04-29 22:35:33 -04004033 mutex_unlock(&ftrace_lock);
4034 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004035
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004036 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004037 free_ftrace_hash(iter->hash);
4038 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004039
Steven Rostedt5072c592008-05-12 21:20:43 +02004040 return 0;
4041}
4042
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004043static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004044 .open = ftrace_avail_open,
4045 .read = seq_read,
4046 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004047 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004048};
4049
Steven Rostedt647bcd02011-05-03 14:39:21 -04004050static const struct file_operations ftrace_enabled_fops = {
4051 .open = ftrace_enabled_open,
4052 .read = seq_read,
4053 .llseek = seq_lseek,
4054 .release = seq_release_private,
4055};
4056
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004057static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004058 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004059 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004060 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004061 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004062 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004063};
4064
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004065static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004066 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004067 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004068 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004069 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004070 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004071};
4072
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004073#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4074
4075static DEFINE_MUTEX(graph_lock);
4076
4077int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004078int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004079unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004080unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004081
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004082struct ftrace_graph_data {
4083 unsigned long *table;
4084 size_t size;
4085 int *count;
4086 const struct seq_operations *seq_ops;
4087};
4088
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004089static void *
Li Zefan85951842009-06-24 09:54:00 +08004090__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004091{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004092 struct ftrace_graph_data *fgd = m->private;
4093
4094 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004095 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004096 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004097}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004098
Li Zefan85951842009-06-24 09:54:00 +08004099static void *
4100g_next(struct seq_file *m, void *v, loff_t *pos)
4101{
4102 (*pos)++;
4103 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004104}
4105
4106static void *g_start(struct seq_file *m, loff_t *pos)
4107{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004108 struct ftrace_graph_data *fgd = m->private;
4109
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004110 mutex_lock(&graph_lock);
4111
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004112 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004113 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004114 return (void *)1;
4115
Li Zefan85951842009-06-24 09:54:00 +08004116 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004117}
4118
4119static void g_stop(struct seq_file *m, void *p)
4120{
4121 mutex_unlock(&graph_lock);
4122}
4123
4124static int g_show(struct seq_file *m, void *v)
4125{
4126 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004127
4128 if (!ptr)
4129 return 0;
4130
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004131 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004132 struct ftrace_graph_data *fgd = m->private;
4133
4134 if (fgd->table == ftrace_graph_funcs)
4135 seq_printf(m, "#### all functions enabled ####\n");
4136 else
4137 seq_printf(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004138 return 0;
4139 }
4140
Steven Rostedtb375a112009-09-17 00:05:58 -04004141 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004142
4143 return 0;
4144}
4145
James Morris88e9d342009-09-22 16:43:43 -07004146static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004147 .start = g_start,
4148 .next = g_next,
4149 .stop = g_stop,
4150 .show = g_show,
4151};
4152
4153static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004154__ftrace_graph_open(struct inode *inode, struct file *file,
4155 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004156{
4157 int ret = 0;
4158
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004159 mutex_lock(&graph_lock);
4160 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004161 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004162 *fgd->count = 0;
4163 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004164 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004165 mutex_unlock(&graph_lock);
4166
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004167 if (file->f_mode & FMODE_READ) {
4168 ret = seq_open(file, fgd->seq_ops);
4169 if (!ret) {
4170 struct seq_file *m = file->private_data;
4171 m->private = fgd;
4172 }
4173 } else
4174 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004175
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004176 return ret;
4177}
4178
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004179static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004180ftrace_graph_open(struct inode *inode, struct file *file)
4181{
4182 struct ftrace_graph_data *fgd;
4183
4184 if (unlikely(ftrace_disabled))
4185 return -ENODEV;
4186
4187 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4188 if (fgd == NULL)
4189 return -ENOMEM;
4190
4191 fgd->table = ftrace_graph_funcs;
4192 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4193 fgd->count = &ftrace_graph_count;
4194 fgd->seq_ops = &ftrace_graph_seq_ops;
4195
4196 return __ftrace_graph_open(inode, file, fgd);
4197}
4198
4199static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004200ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4201{
4202 struct ftrace_graph_data *fgd;
4203
4204 if (unlikely(ftrace_disabled))
4205 return -ENODEV;
4206
4207 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4208 if (fgd == NULL)
4209 return -ENOMEM;
4210
4211 fgd->table = ftrace_graph_notrace_funcs;
4212 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4213 fgd->count = &ftrace_graph_notrace_count;
4214 fgd->seq_ops = &ftrace_graph_seq_ops;
4215
4216 return __ftrace_graph_open(inode, file, fgd);
4217}
4218
4219static int
Li Zefan87827112009-07-23 11:29:11 +08004220ftrace_graph_release(struct inode *inode, struct file *file)
4221{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004222 if (file->f_mode & FMODE_READ) {
4223 struct seq_file *m = file->private_data;
4224
4225 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004226 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004227 } else {
4228 kfree(file->private_data);
4229 }
4230
Li Zefan87827112009-07-23 11:29:11 +08004231 return 0;
4232}
4233
4234static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004235ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004236{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004237 struct dyn_ftrace *rec;
4238 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004239 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004240 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004241 int type, not;
4242 char *search;
4243 bool exists;
4244 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004245
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004246 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004247 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004248 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004249 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004250
4251 search_len = strlen(search);
4252
Steven Rostedt52baf112009-02-14 01:15:39 -05004253 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004254
4255 if (unlikely(ftrace_disabled)) {
4256 mutex_unlock(&ftrace_lock);
4257 return -ENODEV;
4258 }
4259
Steven Rostedt265c8312009-02-13 12:43:56 -05004260 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004261
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004262 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004263 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004264 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004265 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004266 if (array[i] == rec->ip) {
4267 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004268 break;
4269 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004270 }
4271
4272 if (!not) {
4273 fail = 0;
4274 if (!exists) {
4275 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004276 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004277 goto out;
4278 }
4279 } else {
4280 if (exists) {
4281 array[i] = array[--(*idx)];
4282 array[*idx] = 0;
4283 fail = 0;
4284 }
4285 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004286 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004287 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004288out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004289 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004290
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004291 if (fail)
4292 return -EINVAL;
4293
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004294 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004295}
4296
4297static ssize_t
4298ftrace_graph_write(struct file *file, const char __user *ubuf,
4299 size_t cnt, loff_t *ppos)
4300{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004301 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004302 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004303 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004304
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004305 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004306 return 0;
4307
Namhyung Kim6a101082013-10-14 17:24:25 +09004308 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4309 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004310
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004311 read = trace_get_user(&parser, ubuf, cnt, ppos);
4312
Li Zefan4ba79782009-09-22 13:52:20 +08004313 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004314 parser.buffer[parser.idx] = 0;
4315
Namhyung Kim6a101082013-10-14 17:24:25 +09004316 mutex_lock(&graph_lock);
4317
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004318 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004319 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4320 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004321
4322 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004323 }
4324
Namhyung Kim6a101082013-10-14 17:24:25 +09004325 if (!ret)
4326 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004327
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004328 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004329
4330 return ret;
4331}
4332
4333static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004334 .open = ftrace_graph_open,
4335 .read = seq_read,
4336 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004337 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004338 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004339};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004340
4341static const struct file_operations ftrace_graph_notrace_fops = {
4342 .open = ftrace_graph_notrace_open,
4343 .read = seq_read,
4344 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004345 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004346 .release = ftrace_graph_release,
4347};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004348#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4349
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004350void ftrace_create_filter_files(struct ftrace_ops *ops,
4351 struct dentry *parent)
4352{
4353
4354 trace_create_file("set_ftrace_filter", 0644, parent,
4355 ops, &ftrace_filter_fops);
4356
4357 trace_create_file("set_ftrace_notrace", 0644, parent,
4358 ops, &ftrace_notrace_fops);
4359}
4360
4361/*
4362 * The name "destroy_filter_files" is really a misnomer. Although
4363 * in the future, it may actualy delete the files, but this is
4364 * really intended to make sure the ops passed in are disabled
4365 * and that when this function returns, the caller is free to
4366 * free the ops.
4367 *
4368 * The "destroy" name is only to match the "create" name that this
4369 * should be paired with.
4370 */
4371void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4372{
4373 mutex_lock(&ftrace_lock);
4374 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4375 ftrace_shutdown(ops, 0);
4376 ops->flags |= FTRACE_OPS_FL_DELETED;
4377 mutex_unlock(&ftrace_lock);
4378}
4379
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004380static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004381{
Steven Rostedt5072c592008-05-12 21:20:43 +02004382
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004383 trace_create_file("available_filter_functions", 0444,
4384 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004385
Steven Rostedt647bcd02011-05-03 14:39:21 -04004386 trace_create_file("enabled_functions", 0444,
4387 d_tracer, NULL, &ftrace_enabled_fops);
4388
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004389 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004390
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004391#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004392 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004393 NULL,
4394 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004395 trace_create_file("set_graph_notrace", 0444, d_tracer,
4396 NULL,
4397 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004398#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4399
Steven Rostedt5072c592008-05-12 21:20:43 +02004400 return 0;
4401}
4402
Steven Rostedt9fd49322012-04-24 22:32:06 -04004403static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004404{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004405 const unsigned long *ipa = a;
4406 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004407
Steven Rostedt9fd49322012-04-24 22:32:06 -04004408 if (*ipa > *ipb)
4409 return 1;
4410 if (*ipa < *ipb)
4411 return -1;
4412 return 0;
4413}
4414
4415static void ftrace_swap_ips(void *a, void *b, int size)
4416{
4417 unsigned long *ipa = a;
4418 unsigned long *ipb = b;
4419 unsigned long t;
4420
4421 t = *ipa;
4422 *ipa = *ipb;
4423 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004424}
4425
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004426static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004427 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004428 unsigned long *end)
4429{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004430 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004431 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004432 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004433 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004434 unsigned long *p;
4435 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004436 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004437 int ret = -ENOMEM;
4438
4439 count = end - start;
4440
4441 if (!count)
4442 return 0;
4443
Steven Rostedt9fd49322012-04-24 22:32:06 -04004444 sort(start, count, sizeof(*start),
4445 ftrace_cmp_ips, ftrace_swap_ips);
4446
Steven Rostedt706c81f2012-04-24 23:45:26 -04004447 start_pg = ftrace_allocate_pages(count);
4448 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004449 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004450
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004451 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004452
Steven Rostedt32082302011-12-16 14:42:37 -05004453 /*
4454 * Core and each module needs their own pages, as
4455 * modules will free them when they are removed.
4456 * Force a new page to be allocated for modules.
4457 */
Steven Rostedta7900872011-12-16 16:23:44 -05004458 if (!mod) {
4459 WARN_ON(ftrace_pages || ftrace_pages_start);
4460 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004461 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004462 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004463 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004464 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004465
Steven Rostedta7900872011-12-16 16:23:44 -05004466 if (WARN_ON(ftrace_pages->next)) {
4467 /* Hmm, we have free pages? */
4468 while (ftrace_pages->next)
4469 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004470 }
Steven Rostedta7900872011-12-16 16:23:44 -05004471
Steven Rostedt706c81f2012-04-24 23:45:26 -04004472 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004473 }
4474
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004475 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004476 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004477 while (p < end) {
4478 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004479 /*
4480 * Some architecture linkers will pad between
4481 * the different mcount_loc sections of different
4482 * object files to satisfy alignments.
4483 * Skip any NULL pointers.
4484 */
4485 if (!addr)
4486 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004487
4488 if (pg->index == pg->size) {
4489 /* We should have allocated enough */
4490 if (WARN_ON(!pg->next))
4491 break;
4492 pg = pg->next;
4493 }
4494
4495 rec = &pg->records[pg->index++];
4496 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004497 }
4498
Steven Rostedt706c81f2012-04-24 23:45:26 -04004499 /* We should have used all pages */
4500 WARN_ON(pg->next);
4501
4502 /* Assign the last page to ftrace_pages */
4503 ftrace_pages = pg;
4504
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004505 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004506 * We only need to disable interrupts on start up
4507 * because we are modifying code that an interrupt
4508 * may execute, and the modification is not atomic.
4509 * But for modules, nothing runs the code we modify
4510 * until we are finished with it, and there's no
4511 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004512 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004513 if (!mod)
4514 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004515 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004516 if (!mod)
4517 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004518 ret = 0;
4519 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004520 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004521
Steven Rostedta7900872011-12-16 16:23:44 -05004522 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004523}
4524
Steven Rostedt93eb6772009-04-15 13:24:06 -04004525#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004526
4527#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4528
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004529void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004530{
4531 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004532 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004533 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004534 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004535
Steven Rostedt93eb6772009-04-15 13:24:06 -04004536 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004537
4538 if (ftrace_disabled)
4539 goto out_unlock;
4540
Steven Rostedt32082302011-12-16 14:42:37 -05004541 /*
4542 * Each module has its own ftrace_pages, remove
4543 * them from the list.
4544 */
4545 last_pg = &ftrace_pages_start;
4546 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4547 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004548 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004549 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004550 * As core pages are first, the first
4551 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004552 */
Steven Rostedt32082302011-12-16 14:42:37 -05004553 if (WARN_ON(pg == ftrace_pages_start))
4554 goto out_unlock;
4555
4556 /* Check if we are deleting the last page */
4557 if (pg == ftrace_pages)
4558 ftrace_pages = next_to_ftrace_page(last_pg);
4559
4560 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004561 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4562 free_pages((unsigned long)pg->records, order);
4563 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004564 } else
4565 last_pg = &pg->next;
4566 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004567 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004568 mutex_unlock(&ftrace_lock);
4569}
4570
4571static void ftrace_init_module(struct module *mod,
4572 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004573{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004574 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004575 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004576 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004577}
4578
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004579void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004580{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004581 ftrace_init_module(mod, mod->ftrace_callsites,
4582 mod->ftrace_callsites +
4583 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004584}
4585
4586static int ftrace_module_notify_exit(struct notifier_block *self,
4587 unsigned long val, void *data)
4588{
4589 struct module *mod = data;
4590
4591 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004592 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004593
4594 return 0;
4595}
4596#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004597static int ftrace_module_notify_exit(struct notifier_block *self,
4598 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004599{
4600 return 0;
4601}
4602#endif /* CONFIG_MODULES */
4603
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004604struct notifier_block ftrace_module_exit_nb = {
4605 .notifier_call = ftrace_module_notify_exit,
4606 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4607};
4608
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004609void __init ftrace_init(void)
4610{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004611 extern unsigned long __start_mcount_loc[];
4612 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004613 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004614 int ret;
4615
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004616 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004617 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004618 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004619 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004620 goto failed;
4621
4622 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004623 if (!count) {
4624 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004625 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004626 }
4627
4628 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4629 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004630
4631 last_ftrace_enabled = ftrace_enabled = 1;
4632
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004633 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004634 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004635 __stop_mcount_loc);
4636
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004637 ret = register_module_notifier(&ftrace_module_exit_nb);
4638 if (ret)
4639 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004640
Steven Rostedt2af15d62009-05-28 13:37:24 -04004641 set_ftrace_early_filters();
4642
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004643 return;
4644 failed:
4645 ftrace_disabled = 1;
4646}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004647
Steven Rostedt3d083392008-05-12 21:20:42 +02004648#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004649
Steven Rostedt2b499382011-05-03 22:49:52 -04004650static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004651 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004652 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004653 INIT_OPS_HASH(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004654};
4655
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004656static int __init ftrace_nodyn_init(void)
4657{
4658 ftrace_enabled = 1;
4659 return 0;
4660}
Steven Rostedt6f415672012-10-05 12:13:07 -04004661core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004662
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004663static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4664static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004665/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004666# define ftrace_startup(ops, command) \
4667 ({ \
4668 int ___ret = __register_ftrace_function(ops); \
4669 if (!___ret) \
4670 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4671 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004672 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004673# define ftrace_shutdown(ops, command) \
4674 ({ \
4675 int ___ret = __unregister_ftrace_function(ops); \
4676 if (!___ret) \
4677 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4678 ___ret; \
4679 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004680
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004681# define ftrace_startup_sysctl() do { } while (0)
4682# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004683
4684static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004685ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004686{
4687 return 1;
4688}
4689
Steven Rostedt3d083392008-05-12 21:20:42 +02004690#endif /* CONFIG_DYNAMIC_FTRACE */
4691
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004692__init void ftrace_init_global_array_ops(struct trace_array *tr)
4693{
4694 tr->ops = &global_ops;
4695 tr->ops->private = tr;
4696}
4697
4698void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4699{
4700 /* If we filter on pids, update to use the pid function */
4701 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4702 if (WARN_ON(tr->ops->func != ftrace_stub))
4703 printk("ftrace ops had %pS for function\n",
4704 tr->ops->func);
4705 /* Only the top level instance does pid tracing */
4706 if (!list_empty(&ftrace_pids)) {
4707 set_ftrace_pid_function(func);
4708 func = ftrace_pid_func;
4709 }
4710 }
4711 tr->ops->func = func;
4712 tr->ops->private = tr;
4713}
4714
4715void ftrace_reset_array_ops(struct trace_array *tr)
4716{
4717 tr->ops->func = ftrace_stub;
4718}
4719
Steven Rostedtb8489142011-05-04 09:27:52 -04004720static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004721ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004722 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004723{
Jiri Olsae2484912012-02-15 15:51:48 +01004724 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4725 return;
4726
4727 /*
4728 * Some of the ops may be dynamically allocated,
4729 * they must be freed after a synchronize_sched().
4730 */
4731 preempt_disable_notrace();
4732 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004733
4734 /*
4735 * Control funcs (perf) uses RCU. Only trace if
4736 * RCU is currently active.
4737 */
4738 if (!rcu_is_watching())
4739 goto out;
4740
Steven Rostedt0a016402012-11-02 17:03:03 -04004741 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004742 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4743 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004744 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004745 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004746 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004747 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004748 trace_recursion_clear(TRACE_CONTROL_BIT);
4749 preempt_enable_notrace();
4750}
4751
4752static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004753 .func = ftrace_ops_control_func,
4754 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004755 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004756};
4757
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004758static inline void
4759__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004760 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004761{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004762 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004763 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004764
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004765 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4766 if (bit < 0)
4767 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004768
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004769 /*
4770 * Some of the ops may be dynamically allocated,
4771 * they must be freed after a synchronize_sched().
4772 */
4773 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004774 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004775 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004776 if (FTRACE_WARN_ON(!op->func)) {
4777 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004778 goto out;
4779 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004780 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004781 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004782 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004783out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004784 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004785 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004786}
4787
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004788/*
4789 * Some archs only support passing ip and parent_ip. Even though
4790 * the list function ignores the op parameter, we do not want any
4791 * C side effects, where a function is called without the caller
4792 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004793 * Archs are to support both the regs and ftrace_ops at the same time.
4794 * If they support ftrace_ops, it is assumed they support regs.
4795 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004796 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4797 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004798 * An architecture can pass partial regs with ftrace_ops and still
4799 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004800 */
4801#if ARCH_SUPPORTS_FTRACE_OPS
4802static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004803 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004804{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004805 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004806}
4807#else
4808static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4809{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004810 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004811}
4812#endif
4813
Steven Rostedte32d8952008-12-04 00:26:41 -05004814static void clear_ftrace_swapper(void)
4815{
4816 struct task_struct *p;
4817 int cpu;
4818
4819 get_online_cpus();
4820 for_each_online_cpu(cpu) {
4821 p = idle_task(cpu);
4822 clear_tsk_trace_trace(p);
4823 }
4824 put_online_cpus();
4825}
4826
4827static void set_ftrace_swapper(void)
4828{
4829 struct task_struct *p;
4830 int cpu;
4831
4832 get_online_cpus();
4833 for_each_online_cpu(cpu) {
4834 p = idle_task(cpu);
4835 set_tsk_trace_trace(p);
4836 }
4837 put_online_cpus();
4838}
4839
4840static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004841{
4842 struct task_struct *p;
4843
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004844 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004845 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004846 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004847 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004848 rcu_read_unlock();
4849
Steven Rostedte32d8952008-12-04 00:26:41 -05004850 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004851}
4852
Steven Rostedte32d8952008-12-04 00:26:41 -05004853static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004854{
4855 struct task_struct *p;
4856
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004857 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004858 do_each_pid_task(pid, PIDTYPE_PID, p) {
4859 set_tsk_trace_trace(p);
4860 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004861 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004862}
4863
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004864static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004865{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004866 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004867 clear_ftrace_swapper();
4868 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004869 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004870}
4871
4872static void set_ftrace_pid_task(struct pid *pid)
4873{
4874 if (pid == ftrace_swapper_pid)
4875 set_ftrace_swapper();
4876 else
4877 set_ftrace_pid(pid);
4878}
4879
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004880static int ftrace_pid_add(int p)
4881{
4882 struct pid *pid;
4883 struct ftrace_pid *fpid;
4884 int ret = -EINVAL;
4885
4886 mutex_lock(&ftrace_lock);
4887
4888 if (!p)
4889 pid = ftrace_swapper_pid;
4890 else
4891 pid = find_get_pid(p);
4892
4893 if (!pid)
4894 goto out;
4895
4896 ret = 0;
4897
4898 list_for_each_entry(fpid, &ftrace_pids, list)
4899 if (fpid->pid == pid)
4900 goto out_put;
4901
4902 ret = -ENOMEM;
4903
4904 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4905 if (!fpid)
4906 goto out_put;
4907
4908 list_add(&fpid->list, &ftrace_pids);
4909 fpid->pid = pid;
4910
4911 set_ftrace_pid_task(pid);
4912
4913 ftrace_update_pid_func();
4914 ftrace_startup_enable(0);
4915
4916 mutex_unlock(&ftrace_lock);
4917 return 0;
4918
4919out_put:
4920 if (pid != ftrace_swapper_pid)
4921 put_pid(pid);
4922
4923out:
4924 mutex_unlock(&ftrace_lock);
4925 return ret;
4926}
4927
4928static void ftrace_pid_reset(void)
4929{
4930 struct ftrace_pid *fpid, *safe;
4931
4932 mutex_lock(&ftrace_lock);
4933 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4934 struct pid *pid = fpid->pid;
4935
4936 clear_ftrace_pid_task(pid);
4937
4938 list_del(&fpid->list);
4939 kfree(fpid);
4940 }
4941
4942 ftrace_update_pid_func();
4943 ftrace_startup_enable(0);
4944
4945 mutex_unlock(&ftrace_lock);
4946}
4947
4948static void *fpid_start(struct seq_file *m, loff_t *pos)
4949{
4950 mutex_lock(&ftrace_lock);
4951
4952 if (list_empty(&ftrace_pids) && (!*pos))
4953 return (void *) 1;
4954
4955 return seq_list_start(&ftrace_pids, *pos);
4956}
4957
4958static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4959{
4960 if (v == (void *)1)
4961 return NULL;
4962
4963 return seq_list_next(v, &ftrace_pids, pos);
4964}
4965
4966static void fpid_stop(struct seq_file *m, void *p)
4967{
4968 mutex_unlock(&ftrace_lock);
4969}
4970
4971static int fpid_show(struct seq_file *m, void *v)
4972{
4973 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4974
4975 if (v == (void *)1) {
4976 seq_printf(m, "no pid\n");
4977 return 0;
4978 }
4979
4980 if (fpid->pid == ftrace_swapper_pid)
4981 seq_printf(m, "swapper tasks\n");
4982 else
4983 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4984
4985 return 0;
4986}
4987
4988static const struct seq_operations ftrace_pid_sops = {
4989 .start = fpid_start,
4990 .next = fpid_next,
4991 .stop = fpid_stop,
4992 .show = fpid_show,
4993};
4994
4995static int
4996ftrace_pid_open(struct inode *inode, struct file *file)
4997{
4998 int ret = 0;
4999
5000 if ((file->f_mode & FMODE_WRITE) &&
5001 (file->f_flags & O_TRUNC))
5002 ftrace_pid_reset();
5003
5004 if (file->f_mode & FMODE_READ)
5005 ret = seq_open(file, &ftrace_pid_sops);
5006
5007 return ret;
5008}
5009
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005010static ssize_t
5011ftrace_pid_write(struct file *filp, const char __user *ubuf,
5012 size_t cnt, loff_t *ppos)
5013{
Ingo Molnar457dc922009-11-23 11:03:28 +01005014 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005015 long val;
5016 int ret;
5017
5018 if (cnt >= sizeof(buf))
5019 return -EINVAL;
5020
5021 if (copy_from_user(&buf, ubuf, cnt))
5022 return -EFAULT;
5023
5024 buf[cnt] = 0;
5025
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005026 /*
5027 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5028 * to clean the filter quietly.
5029 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005030 tmp = strstrip(buf);
5031 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005032 return 1;
5033
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005034 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005035 if (ret < 0)
5036 return ret;
5037
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005038 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005039
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005040 return ret ? ret : cnt;
5041}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005042
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005043static int
5044ftrace_pid_release(struct inode *inode, struct file *file)
5045{
5046 if (file->f_mode & FMODE_READ)
5047 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005048
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005049 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005050}
5051
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005052static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005053 .open = ftrace_pid_open,
5054 .write = ftrace_pid_write,
5055 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005056 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005057 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005058};
5059
5060static __init int ftrace_init_debugfs(void)
5061{
5062 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005063
5064 d_tracer = tracing_init_dentry();
5065 if (!d_tracer)
5066 return 0;
5067
5068 ftrace_init_dyn_debugfs(d_tracer);
5069
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005070 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5071 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005072
5073 ftrace_profile_debugfs(d_tracer);
5074
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005075 return 0;
5076}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005077fs_initcall(ftrace_init_debugfs);
5078
Steven Rostedt3d083392008-05-12 21:20:42 +02005079/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005080 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005081 *
5082 * This function should be used by panic code. It stops ftrace
5083 * but in a not so nice way. If you need to simply kill ftrace
5084 * from a non-atomic section, use ftrace_kill.
5085 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005086void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005087{
5088 ftrace_disabled = 1;
5089 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005090 clear_ftrace_function();
5091}
5092
5093/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005094 * Test if ftrace is dead or not.
5095 */
5096int ftrace_is_dead(void)
5097{
5098 return ftrace_disabled;
5099}
5100
5101/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005102 * register_ftrace_function - register a function for profiling
5103 * @ops - ops structure that holds the function for profiling.
5104 *
5105 * Register a function to be called by all functions in the
5106 * kernel.
5107 *
5108 * Note: @ops->func and all the functions it calls must be labeled
5109 * with "notrace", otherwise it will go into a
5110 * recursive loop.
5111 */
5112int register_ftrace_function(struct ftrace_ops *ops)
5113{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005114 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005115
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005116 ftrace_ops_init(ops);
5117
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005118 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005119
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005120 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005121
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005122 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005123
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005124 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005125}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005126EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005127
5128/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005129 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005130 * @ops - ops structure that holds the function to unregister
5131 *
5132 * Unregister a function that was added to be called by ftrace profiling.
5133 */
5134int unregister_ftrace_function(struct ftrace_ops *ops)
5135{
5136 int ret;
5137
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005138 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005139 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005140 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005141
5142 return ret;
5143}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005144EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005145
Ingo Molnare309b412008-05-12 21:20:51 +02005146int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005147ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005148 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005149 loff_t *ppos)
5150{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005151 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005152
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005153 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005154
Steven Rostedt45a4a232011-04-21 23:16:46 -04005155 if (unlikely(ftrace_disabled))
5156 goto out;
5157
5158 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005159
Li Zefana32c7762009-06-26 16:55:51 +08005160 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005161 goto out;
5162
Li Zefana32c7762009-06-26 16:55:51 +08005163 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005164
5165 if (ftrace_enabled) {
5166
5167 ftrace_startup_sysctl();
5168
5169 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005170 if (ftrace_ops_list != &ftrace_list_end)
5171 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005172
5173 } else {
5174 /* stopping ftrace calls (just send to ftrace_stub) */
5175 ftrace_trace_function = ftrace_stub;
5176
5177 ftrace_shutdown_sysctl();
5178 }
5179
5180 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005181 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005182 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005183}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005184
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005185#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005186
Steven Rostedt597af812009-04-03 15:24:12 -04005187static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005188
Steven Rostedte49dc192008-12-02 23:50:05 -05005189int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5190{
5191 return 0;
5192}
5193
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005194/* The callbacks that hook a function */
5195trace_func_graph_ret_t ftrace_graph_return =
5196 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005197trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005198static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005199
5200/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5201static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5202{
5203 int i;
5204 int ret = 0;
5205 unsigned long flags;
5206 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5207 struct task_struct *g, *t;
5208
5209 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5210 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5211 * sizeof(struct ftrace_ret_stack),
5212 GFP_KERNEL);
5213 if (!ret_stack_list[i]) {
5214 start = 0;
5215 end = i;
5216 ret = -ENOMEM;
5217 goto free;
5218 }
5219 }
5220
5221 read_lock_irqsave(&tasklist_lock, flags);
5222 do_each_thread(g, t) {
5223 if (start == end) {
5224 ret = -EAGAIN;
5225 goto unlock;
5226 }
5227
5228 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005229 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005230 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005231 t->curr_ret_stack = -1;
5232 /* Make sure the tasks see the -1 first: */
5233 smp_wmb();
5234 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005235 }
5236 } while_each_thread(g, t);
5237
5238unlock:
5239 read_unlock_irqrestore(&tasklist_lock, flags);
5240free:
5241 for (i = start; i < end; i++)
5242 kfree(ret_stack_list[i]);
5243 return ret;
5244}
5245
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005246static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005247ftrace_graph_probe_sched_switch(void *ignore,
5248 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005249{
5250 unsigned long long timestamp;
5251 int index;
5252
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005253 /*
5254 * Does the user want to count the time a function was asleep.
5255 * If so, do not update the time stamps.
5256 */
5257 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5258 return;
5259
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005260 timestamp = trace_clock_local();
5261
5262 prev->ftrace_timestamp = timestamp;
5263
5264 /* only process tasks that we timestamped */
5265 if (!next->ftrace_timestamp)
5266 return;
5267
5268 /*
5269 * Update all the counters in next to make up for the
5270 * time next was sleeping.
5271 */
5272 timestamp -= next->ftrace_timestamp;
5273
5274 for (index = next->curr_ret_stack; index >= 0; index--)
5275 next->ret_stack[index].calltime += timestamp;
5276}
5277
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005278/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005279static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005280{
5281 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005282 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005283
5284 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5285 sizeof(struct ftrace_ret_stack *),
5286 GFP_KERNEL);
5287
5288 if (!ret_stack_list)
5289 return -ENOMEM;
5290
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005291 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005292 for_each_online_cpu(cpu) {
5293 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005294 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005295 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005296
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005297 do {
5298 ret = alloc_retstack_tasklist(ret_stack_list);
5299 } while (ret == -EAGAIN);
5300
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005301 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005302 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005303 if (ret)
5304 pr_info("ftrace_graph: Couldn't activate tracepoint"
5305 " probe to kernel_sched_switch\n");
5306 }
5307
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005308 kfree(ret_stack_list);
5309 return ret;
5310}
5311
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005312/*
5313 * Hibernation protection.
5314 * The state of the current task is too much unstable during
5315 * suspend/restore to disk. We want to protect against that.
5316 */
5317static int
5318ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5319 void *unused)
5320{
5321 switch (state) {
5322 case PM_HIBERNATION_PREPARE:
5323 pause_graph_tracing();
5324 break;
5325
5326 case PM_POST_HIBERNATION:
5327 unpause_graph_tracing();
5328 break;
5329 }
5330 return NOTIFY_DONE;
5331}
5332
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005333static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5334{
5335 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5336 return 0;
5337 return __ftrace_graph_entry(trace);
5338}
5339
5340/*
5341 * The function graph tracer should only trace the functions defined
5342 * by set_ftrace_filter and set_ftrace_notrace. If another function
5343 * tracer ops is registered, the graph tracer requires testing the
5344 * function against the global ops, and not just trace any function
5345 * that any ftrace_ops registered.
5346 */
5347static void update_function_graph_func(void)
5348{
5349 if (ftrace_ops_list == &ftrace_list_end ||
5350 (ftrace_ops_list == &global_ops &&
5351 global_ops.next == &ftrace_list_end))
5352 ftrace_graph_entry = __ftrace_graph_entry;
5353 else
5354 ftrace_graph_entry = ftrace_graph_entry_test;
5355}
5356
Mathias Krause8275f692014-03-30 15:31:50 +02005357static struct notifier_block ftrace_suspend_notifier = {
5358 .notifier_call = ftrace_suspend_notifier_call,
5359};
5360
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005361int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5362 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005363{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005364 int ret = 0;
5365
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005366 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005367
Steven Rostedt05ce5812009-03-24 00:18:31 -04005368 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005369 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005370 ret = -EBUSY;
5371 goto out;
5372 }
5373
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005374 register_pm_notifier(&ftrace_suspend_notifier);
5375
Steven Rostedt597af812009-04-03 15:24:12 -04005376 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005377 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005378 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005379 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005380 goto out;
5381 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005382
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005383 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005384
5385 /*
5386 * Update the indirect function to the entryfunc, and the
5387 * function that gets called to the entry_test first. Then
5388 * call the update fgraph entry function to determine if
5389 * the entryfunc should be called directly or not.
5390 */
5391 __ftrace_graph_entry = entryfunc;
5392 ftrace_graph_entry = ftrace_graph_entry_test;
5393 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005394
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005395 /* Function graph doesn't use the .func field of global_ops */
5396 global_ops.flags |= FTRACE_OPS_FL_STUB;
5397
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005398#ifdef CONFIG_DYNAMIC_FTRACE
5399 /* Optimize function graph calling (if implemented by arch) */
Steven Rostedt (Red Hat)646d7042014-07-11 14:39:10 -04005400 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5401 global_ops.trampoline = FTRACE_GRAPH_TRAMP_ADDR;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005402#endif
5403
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005404 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005405
5406out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005407 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005408 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005409}
5410
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005411void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005412{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005413 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005414
Steven Rostedt597af812009-04-03 15:24:12 -04005415 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005416 goto out;
5417
Steven Rostedt597af812009-04-03 15:24:12 -04005418 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005419 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005420 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005421 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005422 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5423 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005424#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)646d7042014-07-11 14:39:10 -04005425 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5426 global_ops.trampoline = 0;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005427#endif
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005428 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005429 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005430
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005431 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005432 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005433}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005434
Steven Rostedt868baf02011-02-10 21:26:13 -05005435static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5436
5437static void
5438graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5439{
5440 atomic_set(&t->tracing_graph_pause, 0);
5441 atomic_set(&t->trace_overrun, 0);
5442 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005443 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005444 smp_wmb();
5445 t->ret_stack = ret_stack;
5446}
5447
5448/*
5449 * Allocate a return stack for the idle task. May be the first
5450 * time through, or it may be done by CPU hotplug online.
5451 */
5452void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5453{
5454 t->curr_ret_stack = -1;
5455 /*
5456 * The idle task has no parent, it either has its own
5457 * stack or no stack at all.
5458 */
5459 if (t->ret_stack)
5460 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5461
5462 if (ftrace_graph_active) {
5463 struct ftrace_ret_stack *ret_stack;
5464
5465 ret_stack = per_cpu(idle_ret_stack, cpu);
5466 if (!ret_stack) {
5467 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5468 * sizeof(struct ftrace_ret_stack),
5469 GFP_KERNEL);
5470 if (!ret_stack)
5471 return;
5472 per_cpu(idle_ret_stack, cpu) = ret_stack;
5473 }
5474 graph_init_task(t, ret_stack);
5475 }
5476}
5477
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005478/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005479void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005480{
Steven Rostedt84047e32009-06-02 16:51:55 -04005481 /* Make sure we do not use the parent ret_stack */
5482 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005483 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005484
Steven Rostedt597af812009-04-03 15:24:12 -04005485 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005486 struct ftrace_ret_stack *ret_stack;
5487
5488 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005489 * sizeof(struct ftrace_ret_stack),
5490 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005491 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005492 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005493 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005494 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005495}
5496
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005497void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005498{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005499 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5500
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005501 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005502 /* NULL must become visible to IRQs before we free it: */
5503 barrier();
5504
5505 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005506}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005507#endif