blob: c92757adba79e7dcb15c555caa6a44940f2c17a9 [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
1295ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1296static void
1297ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1298
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 */
1349 ftrace_hash_rec_disable(ops, enable);
1350
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 Rostedt41fb61c2011-07-13 15:03:44 -04001355 ftrace_hash_rec_enable(ops, enable);
1356
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 Rostedt05736a42008-09-22 14:55:47 -07001689static void print_ip_ins(const char *fmt, unsigned char *p)
1690{
1691 int i;
1692
1693 printk(KERN_CONT "%s", fmt);
1694
1695 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1696 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1697}
1698
Steven Rostedtc88fd862011-08-16 09:53:39 -04001699/**
1700 * ftrace_bug - report and shutdown function tracer
1701 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1702 * @ip: The address that failed
1703 *
1704 * The arch code that enables or disables the function tracing
1705 * can call ftrace_bug() when it has detected a problem in
1706 * modifying the code. @failed should be one of either:
1707 * EFAULT - if the problem happens on reading the @ip address
1708 * EINVAL - if what is read at @ip is not what was expected
1709 * EPERM - if the problem happens on writting to the @ip address
1710 */
1711void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001712{
1713 switch (failed) {
1714 case -EFAULT:
1715 FTRACE_WARN_ON_ONCE(1);
1716 pr_info("ftrace faulted on modifying ");
1717 print_ip_sym(ip);
1718 break;
1719 case -EINVAL:
1720 FTRACE_WARN_ON_ONCE(1);
1721 pr_info("ftrace failed to modify ");
1722 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001723 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001724 printk(KERN_CONT "\n");
1725 break;
1726 case -EPERM:
1727 FTRACE_WARN_ON_ONCE(1);
1728 pr_info("ftrace faulted on writing ");
1729 print_ip_sym(ip);
1730 break;
1731 default:
1732 FTRACE_WARN_ON_ONCE(1);
1733 pr_info("ftrace faulted on unknown error ");
1734 print_ip_sym(ip);
1735 }
1736}
1737
Steven Rostedtc88fd862011-08-16 09:53:39 -04001738static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001739{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001740 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001741
Steven Rostedt982c3502008-11-15 16:31:41 -05001742 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001743 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001744 *
Steven Rostedted926f92011-05-03 13:25:24 -04001745 * If the record has a ref count, then we need to enable it
1746 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001747 *
Steven Rostedted926f92011-05-03 13:25:24 -04001748 * Otherwise we make sure its disabled.
1749 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001750 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001751 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001752 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001753 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001754 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001755
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001756 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001757 * If enabling and the REGS flag does not match the REGS_EN, or
1758 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1759 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001760 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001761 if (flag) {
1762 if (!(rec->flags & FTRACE_FL_REGS) !=
1763 !(rec->flags & FTRACE_FL_REGS_EN))
1764 flag |= FTRACE_FL_REGS;
1765
1766 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1767 !(rec->flags & FTRACE_FL_TRAMP_EN))
1768 flag |= FTRACE_FL_TRAMP;
1769 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001770
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001771 /* If the state of this record hasn't changed, then do nothing */
1772 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001773 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001774
1775 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001776 /* Save off if rec is being enabled (for return value) */
1777 flag ^= rec->flags & FTRACE_FL_ENABLED;
1778
1779 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001780 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001781 if (flag & FTRACE_FL_REGS) {
1782 if (rec->flags & FTRACE_FL_REGS)
1783 rec->flags |= FTRACE_FL_REGS_EN;
1784 else
1785 rec->flags &= ~FTRACE_FL_REGS_EN;
1786 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001787 if (flag & FTRACE_FL_TRAMP) {
1788 if (rec->flags & FTRACE_FL_TRAMP)
1789 rec->flags |= FTRACE_FL_TRAMP_EN;
1790 else
1791 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1792 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001793 }
1794
1795 /*
1796 * If this record is being updated from a nop, then
1797 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001798 * Otherwise,
1799 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001800 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001801 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001802 */
1803 if (flag & FTRACE_FL_ENABLED)
1804 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001805
1806 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001807 }
1808
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001809 if (update) {
1810 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001811 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001812 rec->flags = 0;
1813 else
1814 /* Just disable the record (keep REGS state) */
1815 rec->flags &= ~FTRACE_FL_ENABLED;
1816 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001817
1818 return FTRACE_UPDATE_MAKE_NOP;
1819}
1820
1821/**
1822 * ftrace_update_record, set a record that now is tracing or not
1823 * @rec: the record to update
1824 * @enable: set to 1 if the record is tracing, zero to force disable
1825 *
1826 * The records that represent all functions that can be traced need
1827 * to be updated when tracing has been enabled.
1828 */
1829int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1830{
1831 return ftrace_check_record(rec, enable, 1);
1832}
1833
1834/**
1835 * ftrace_test_record, check if the record has been enabled or not
1836 * @rec: the record to test
1837 * @enable: set to 1 to check if enabled, 0 if it is disabled
1838 *
1839 * The arch code may need to test if a record is already set to
1840 * tracing to determine how to modify the function code that it
1841 * represents.
1842 */
1843int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1844{
1845 return ftrace_check_record(rec, enable, 0);
1846}
1847
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001848static struct ftrace_ops *
1849ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1850{
1851 struct ftrace_ops *op;
1852
1853 /* Removed ops need to be tested first */
1854 if (removed_ops && removed_ops->tramp_hash) {
1855 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1856 return removed_ops;
1857 }
1858
1859 do_for_each_ftrace_op(op, ftrace_ops_list) {
1860 if (!op->tramp_hash)
1861 continue;
1862
1863 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1864 return op;
1865
1866 } while_for_each_ftrace_op(op);
1867
1868 return NULL;
1869}
1870
1871static struct ftrace_ops *
1872ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1873{
1874 struct ftrace_ops *op;
1875
1876 do_for_each_ftrace_op(op, ftrace_ops_list) {
1877 /* pass rec in as regs to have non-NULL val */
1878 if (ftrace_ops_test(op, rec->ip, rec))
1879 return op;
1880 } while_for_each_ftrace_op(op);
1881
1882 return NULL;
1883}
1884
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001885/**
1886 * ftrace_get_addr_new - Get the call address to set to
1887 * @rec: The ftrace record descriptor
1888 *
1889 * If the record has the FTRACE_FL_REGS set, that means that it
1890 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1891 * is not not set, then it wants to convert to the normal callback.
1892 *
1893 * Returns the address of the trampoline to set to
1894 */
1895unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1896{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001897 struct ftrace_ops *ops;
1898
1899 /* Trampolines take precedence over regs */
1900 if (rec->flags & FTRACE_FL_TRAMP) {
1901 ops = ftrace_find_tramp_ops_new(rec);
1902 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1903 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1904 (void *)rec->ip, (void *)rec->ip);
1905 /* Ftrace is shutting down, return anything */
1906 return (unsigned long)FTRACE_ADDR;
1907 }
1908 return ops->trampoline;
1909 }
1910
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001911 if (rec->flags & FTRACE_FL_REGS)
1912 return (unsigned long)FTRACE_REGS_ADDR;
1913 else
1914 return (unsigned long)FTRACE_ADDR;
1915}
1916
1917/**
1918 * ftrace_get_addr_curr - Get the call address that is already there
1919 * @rec: The ftrace record descriptor
1920 *
1921 * The FTRACE_FL_REGS_EN is set when the record already points to
1922 * a function that saves all the regs. Basically the '_EN' version
1923 * represents the current state of the function.
1924 *
1925 * Returns the address of the trampoline that is currently being called
1926 */
1927unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1928{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001929 struct ftrace_ops *ops;
1930
1931 /* Trampolines take precedence over regs */
1932 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1933 ops = ftrace_find_tramp_ops_curr(rec);
1934 if (FTRACE_WARN_ON(!ops)) {
1935 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1936 (void *)rec->ip, (void *)rec->ip);
1937 /* Ftrace is shutting down, return anything */
1938 return (unsigned long)FTRACE_ADDR;
1939 }
1940 return ops->trampoline;
1941 }
1942
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001943 if (rec->flags & FTRACE_FL_REGS_EN)
1944 return (unsigned long)FTRACE_REGS_ADDR;
1945 else
1946 return (unsigned long)FTRACE_ADDR;
1947}
1948
Steven Rostedtc88fd862011-08-16 09:53:39 -04001949static int
1950__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1951{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001952 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001953 unsigned long ftrace_addr;
1954 int ret;
1955
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001956 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001957
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001958 /* This needs to be done before we call ftrace_update_record */
1959 ftrace_old_addr = ftrace_get_addr_curr(rec);
1960
1961 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001962
Steven Rostedtc88fd862011-08-16 09:53:39 -04001963 switch (ret) {
1964 case FTRACE_UPDATE_IGNORE:
1965 return 0;
1966
1967 case FTRACE_UPDATE_MAKE_CALL:
1968 return ftrace_make_call(rec, ftrace_addr);
1969
1970 case FTRACE_UPDATE_MAKE_NOP:
1971 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001972
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001973 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001974 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001975 }
1976
1977 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001978}
1979
Steven Rostedte4f5d542012-04-27 09:13:18 -04001980void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001981{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001982 struct dyn_ftrace *rec;
1983 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001984 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001985
Steven Rostedt45a4a232011-04-21 23:16:46 -04001986 if (unlikely(ftrace_disabled))
1987 return;
1988
Steven Rostedt265c8312009-02-13 12:43:56 -05001989 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001990 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001991 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001992 ftrace_bug(failed, rec->ip);
1993 /* Stop processing */
1994 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001995 }
1996 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001997}
1998
Steven Rostedtc88fd862011-08-16 09:53:39 -04001999struct ftrace_rec_iter {
2000 struct ftrace_page *pg;
2001 int index;
2002};
2003
2004/**
2005 * ftrace_rec_iter_start, start up iterating over traced functions
2006 *
2007 * Returns an iterator handle that is used to iterate over all
2008 * the records that represent address locations where functions
2009 * are traced.
2010 *
2011 * May return NULL if no records are available.
2012 */
2013struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2014{
2015 /*
2016 * We only use a single iterator.
2017 * Protected by the ftrace_lock mutex.
2018 */
2019 static struct ftrace_rec_iter ftrace_rec_iter;
2020 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2021
2022 iter->pg = ftrace_pages_start;
2023 iter->index = 0;
2024
2025 /* Could have empty pages */
2026 while (iter->pg && !iter->pg->index)
2027 iter->pg = iter->pg->next;
2028
2029 if (!iter->pg)
2030 return NULL;
2031
2032 return iter;
2033}
2034
2035/**
2036 * ftrace_rec_iter_next, get the next record to process.
2037 * @iter: The handle to the iterator.
2038 *
2039 * Returns the next iterator after the given iterator @iter.
2040 */
2041struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2042{
2043 iter->index++;
2044
2045 if (iter->index >= iter->pg->index) {
2046 iter->pg = iter->pg->next;
2047 iter->index = 0;
2048
2049 /* Could have empty pages */
2050 while (iter->pg && !iter->pg->index)
2051 iter->pg = iter->pg->next;
2052 }
2053
2054 if (!iter->pg)
2055 return NULL;
2056
2057 return iter;
2058}
2059
2060/**
2061 * ftrace_rec_iter_record, get the record at the iterator location
2062 * @iter: The current iterator location
2063 *
2064 * Returns the record that the current @iter is at.
2065 */
2066struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2067{
2068 return &iter->pg->records[iter->index];
2069}
2070
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302071static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002072ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002073{
2074 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002075 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002076
2077 ip = rec->ip;
2078
Steven Rostedt45a4a232011-04-21 23:16:46 -04002079 if (unlikely(ftrace_disabled))
2080 return 0;
2081
Shaohua Li25aac9d2009-01-09 11:29:40 +08002082 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002083 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002084 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302085 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002086 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302087 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002088}
2089
Steven Rostedt000ab692009-02-17 13:35:06 -05002090/*
2091 * archs can override this function if they must do something
2092 * before the modifying code is performed.
2093 */
2094int __weak ftrace_arch_code_modify_prepare(void)
2095{
2096 return 0;
2097}
2098
2099/*
2100 * archs can override this function if they must do something
2101 * after the modifying code is performed.
2102 */
2103int __weak ftrace_arch_code_modify_post_process(void)
2104{
2105 return 0;
2106}
2107
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002108void ftrace_modify_all_code(int command)
2109{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002110 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002111 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002112
2113 /*
2114 * If the ftrace_caller calls a ftrace_ops func directly,
2115 * we need to make sure that it only traces functions it
2116 * expects to trace. When doing the switch of functions,
2117 * we need to update to the ftrace_ops_list_func first
2118 * before the transition between old and new calls are set,
2119 * as the ftrace_ops_list_func will check the ops hashes
2120 * to make sure the ops are having the right functions
2121 * traced.
2122 */
Petr Mladekcd210672014-02-24 17:12:21 +01002123 if (update) {
2124 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2125 if (FTRACE_WARN_ON(err))
2126 return;
2127 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002128
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002129 if (command & FTRACE_UPDATE_CALLS)
2130 ftrace_replace_code(1);
2131 else if (command & FTRACE_DISABLE_CALLS)
2132 ftrace_replace_code(0);
2133
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002134 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2135 function_trace_op = set_function_trace_op;
2136 smp_wmb();
2137 /* If irqs are disabled, we are in stop machine */
2138 if (!irqs_disabled())
2139 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002140 err = ftrace_update_ftrace_func(ftrace_trace_function);
2141 if (FTRACE_WARN_ON(err))
2142 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002143 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002144
2145 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002146 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002147 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002148 err = ftrace_disable_ftrace_graph_caller();
2149 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002150}
2151
Ingo Molnare309b412008-05-12 21:20:51 +02002152static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002153{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002154 int *command = data;
2155
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002156 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002157
Steven Rostedtc88fd862011-08-16 09:53:39 -04002158 return 0;
2159}
2160
2161/**
2162 * ftrace_run_stop_machine, go back to the stop machine method
2163 * @command: The command to tell ftrace what to do
2164 *
2165 * If an arch needs to fall back to the stop machine method, the
2166 * it can call this function.
2167 */
2168void ftrace_run_stop_machine(int command)
2169{
2170 stop_machine(__ftrace_modify_code, &command, NULL);
2171}
2172
2173/**
2174 * arch_ftrace_update_code, modify the code to trace or not trace
2175 * @command: The command that needs to be done
2176 *
2177 * Archs can override this function if it does not need to
2178 * run stop_machine() to modify code.
2179 */
2180void __weak arch_ftrace_update_code(int command)
2181{
2182 ftrace_run_stop_machine(command);
2183}
2184
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002185static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2186{
2187 struct ftrace_page *pg;
2188 struct dyn_ftrace *rec;
2189 int size, bits;
2190 int ret;
2191
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002192 size = ops->nr_trampolines;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002193 bits = 0;
2194 /*
2195 * Make the hash size about 1/2 the # found
2196 */
2197 for (size /= 2; size; size >>= 1)
2198 bits++;
2199
2200 ops->tramp_hash = alloc_ftrace_hash(bits);
2201 /*
2202 * TODO: a failed allocation is going to screw up
2203 * the accounting of what needs to be modified
2204 * and not. For now, we kill ftrace if we fail
2205 * to allocate here. But there are ways around this,
2206 * but that will take a little more work.
2207 */
2208 if (!ops->tramp_hash)
2209 return -ENOMEM;
2210
2211 do_for_each_ftrace_rec(pg, rec) {
2212 if (ftrace_rec_count(rec) == 1 &&
2213 ftrace_ops_test(ops, rec->ip, rec)) {
2214
Steven Rostedt (Red Hat)2a0343b2014-07-24 09:56:27 -04002215 /*
2216 * If another ops adds to a rec, the rec will
2217 * lose its trampoline and never get it back
2218 * until all ops are off of it.
2219 */
2220 if (!(rec->flags & FTRACE_FL_TRAMP))
2221 continue;
2222
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002223 /* This record had better have a trampoline */
2224 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2225 return -1;
2226
2227 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2228 if (ret < 0)
2229 return ret;
2230 }
2231 } while_for_each_ftrace_rec();
2232
Steven Rostedt (Red Hat)dc6f03f2014-07-24 11:26:11 -04002233 /* The number of recs in the hash must match nr_trampolines */
2234 FTRACE_WARN_ON(ops->tramp_hash->count != ops->nr_trampolines);
2235
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002236 return 0;
2237}
2238
2239static int ftrace_save_tramp_hashes(void)
2240{
2241 struct ftrace_ops *op;
2242 int ret;
2243
2244 /*
2245 * Now that any trampoline is being used, we need to save the
2246 * hashes for the ops that have them. This allows the mapping
2247 * back from the record to the ops that has the trampoline to
2248 * know what code is being replaced. Modifying code must always
2249 * verify what it is changing.
2250 */
2251 do_for_each_ftrace_op(op, ftrace_ops_list) {
2252
2253 /* The tramp_hash is recreated each time. */
2254 free_ftrace_hash(op->tramp_hash);
2255 op->tramp_hash = NULL;
2256
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002257 if (op->nr_trampolines) {
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002258 ret = ftrace_save_ops_tramp_hash(op);
2259 if (ret)
2260 return ret;
2261 }
2262
2263 } while_for_each_ftrace_op(op);
2264
2265 return 0;
2266}
2267
Steven Rostedtc88fd862011-08-16 09:53:39 -04002268static void ftrace_run_update_code(int command)
2269{
2270 int ret;
2271
2272 ret = ftrace_arch_code_modify_prepare();
2273 FTRACE_WARN_ON(ret);
2274 if (ret)
2275 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002276
2277 /*
2278 * By default we use stop_machine() to modify the code.
2279 * But archs can do what ever they want as long as it
2280 * is safe. The stop_machine() is the safest, but also
2281 * produces the most overhead.
2282 */
2283 arch_ftrace_update_code(command);
2284
Steven Rostedt000ab692009-02-17 13:35:06 -05002285 ret = ftrace_arch_code_modify_post_process();
2286 FTRACE_WARN_ON(ret);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002287
2288 ret = ftrace_save_tramp_hashes();
2289 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002290}
2291
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002292static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002293static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002294
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002295static void control_ops_free(struct ftrace_ops *ops)
2296{
2297 free_percpu(ops->disabled);
2298}
2299
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002300static void ftrace_startup_enable(int command)
2301{
2302 if (saved_ftrace_func != ftrace_trace_function) {
2303 saved_ftrace_func = ftrace_trace_function;
2304 command |= FTRACE_UPDATE_TRACE_FUNC;
2305 }
2306
2307 if (!command || !ftrace_enabled)
2308 return;
2309
2310 ftrace_run_update_code(command);
2311}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002312
Steven Rostedta1cd6172011-05-23 15:24:25 -04002313static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002314{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002315 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002316
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002317 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002318 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002319
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002320 ret = __register_ftrace_function(ops);
2321 if (ret)
2322 return ret;
2323
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002324 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002325 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002326
Steven Rostedted926f92011-05-03 13:25:24 -04002327 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002328
2329 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002330
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002331 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002332
2333 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002334}
2335
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002336static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002337{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002338 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002339
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002340 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002341 return -ENODEV;
2342
2343 ret = __unregister_ftrace_function(ops);
2344 if (ret)
2345 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002346
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002347 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002348 /*
2349 * Just warn in case of unbalance, no need to kill ftrace, it's not
2350 * critical but the ftrace_call callers may be never nopped again after
2351 * further ftrace uses.
2352 */
2353 WARN_ON_ONCE(ftrace_start_up < 0);
2354
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002355 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002356
Namhyung Kima737e6d2014-06-12 23:56:12 +09002357 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002358
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002359 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002360
2361 if (saved_ftrace_func != ftrace_trace_function) {
2362 saved_ftrace_func = ftrace_trace_function;
2363 command |= FTRACE_UPDATE_TRACE_FUNC;
2364 }
2365
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002366 if (!command || !ftrace_enabled) {
2367 /*
2368 * If these are control ops, they still need their
2369 * per_cpu field freed. Since, function tracing is
2370 * not currently active, we can just free them
2371 * without synchronizing all CPUs.
2372 */
2373 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2374 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002375 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002376 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002377
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002378 /*
2379 * If the ops uses a trampoline, then it needs to be
2380 * tested first on update.
2381 */
2382 removed_ops = ops;
2383
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002384 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002385
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002386 removed_ops = NULL;
2387
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002388 /*
2389 * Dynamic ops may be freed, we must make sure that all
2390 * callers are done before leaving this function.
2391 * The same goes for freeing the per_cpu data of the control
2392 * ops.
2393 *
2394 * Again, normal synchronize_sched() is not good enough.
2395 * We need to do a hard force of sched synchronization.
2396 * This is because we use preempt_disable() to do RCU, but
2397 * the function tracers can be called where RCU is not watching
2398 * (like before user_exit()). We can not rely on the RCU
2399 * infrastructure to do the synchronization, thus we must do it
2400 * ourselves.
2401 */
2402 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2403 schedule_on_each_cpu(ftrace_sync);
2404
2405 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2406 control_ops_free(ops);
2407 }
2408
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002409 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002410}
2411
Ingo Molnare309b412008-05-12 21:20:51 +02002412static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002413{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002414 if (unlikely(ftrace_disabled))
2415 return;
2416
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002417 /* Force update next time */
2418 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002419 /* ftrace_start_up is true if we want ftrace running */
2420 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002421 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002422}
2423
Ingo Molnare309b412008-05-12 21:20:51 +02002424static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002425{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002426 if (unlikely(ftrace_disabled))
2427 return;
2428
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002429 /* ftrace_start_up is true if ftrace is running */
2430 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002431 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002432}
2433
Steven Rostedt3d083392008-05-12 21:20:42 +02002434static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002435unsigned long ftrace_update_tot_cnt;
2436
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002437static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002438{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002439 /*
2440 * Filter_hash being empty will default to trace module.
2441 * But notrace hash requires a test of individual module functions.
2442 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002443 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2444 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002445}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002446
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002447/*
2448 * Check if the current ops references the record.
2449 *
2450 * If the ops traces all functions, then it was already accounted for.
2451 * If the ops does not trace the current record function, skip it.
2452 * If the ops ignores the function via notrace filter, skip it.
2453 */
2454static inline bool
2455ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2456{
2457 /* If ops isn't enabled, ignore it */
2458 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2459 return 0;
2460
2461 /* If ops traces all mods, we already accounted for it */
2462 if (ops_traces_mod(ops))
2463 return 0;
2464
2465 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002466 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2467 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002468 return 0;
2469
2470 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002471 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002472 return 0;
2473
2474 return 1;
2475}
2476
2477static int referenced_filters(struct dyn_ftrace *rec)
2478{
2479 struct ftrace_ops *ops;
2480 int cnt = 0;
2481
2482 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2483 if (ops_references_rec(ops, rec))
2484 cnt++;
2485 }
2486
2487 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002488}
2489
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002490static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002491{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002492 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002493 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302494 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002495 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002496 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002497 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002498 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002499
2500 /*
2501 * When adding a module, we need to check if tracers are
2502 * currently enabled and if they are set to trace all functions.
2503 * If they are, we need to enable the module functions as well
2504 * as update the reference counts for those function records.
2505 */
2506 if (mod) {
2507 struct ftrace_ops *ops;
2508
2509 for (ops = ftrace_ops_list;
2510 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002511 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2512 if (ops_traces_mod(ops))
2513 ref++;
2514 else
2515 test = true;
2516 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002517 }
2518 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002519
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002520 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002521
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002522 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302523
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002524 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002525 int cnt = ref;
2526
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002527 /* If something went wrong, bail without enabling anything */
2528 if (unlikely(ftrace_disabled))
2529 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002530
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002531 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002532 if (test)
2533 cnt += referenced_filters(p);
2534 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302535
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002536 /*
2537 * Do the initial record conversion from mcount jump
2538 * to the NOP instructions.
2539 */
2540 if (!ftrace_code_disable(mod, p))
2541 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002542
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002543 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002544
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002545 /*
2546 * If the tracing is enabled, go ahead and enable the record.
2547 *
2548 * The reason not to enable the record immediatelly is the
2549 * inherent check of ftrace_make_nop/ftrace_make_call for
2550 * correct previous instructions. Making first the NOP
2551 * conversion puts the module to the correct state, thus
2552 * passing the ftrace_make_call check.
2553 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002554 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002555 int failed = __ftrace_replace_code(p, 1);
2556 if (failed)
2557 ftrace_bug(failed, p->ip);
2558 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002559 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002560 }
2561
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002562 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002563 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002564 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002565
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002566 return 0;
2567}
2568
Steven Rostedta7900872011-12-16 16:23:44 -05002569static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002570{
Steven Rostedta7900872011-12-16 16:23:44 -05002571 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002572 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002573
Steven Rostedta7900872011-12-16 16:23:44 -05002574 if (WARN_ON(!count))
2575 return -EINVAL;
2576
2577 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002578
2579 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002580 * We want to fill as much as possible. No more than a page
2581 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002582 */
Steven Rostedta7900872011-12-16 16:23:44 -05002583 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2584 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002585
Steven Rostedta7900872011-12-16 16:23:44 -05002586 again:
2587 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2588
2589 if (!pg->records) {
2590 /* if we can't allocate this size, try something smaller */
2591 if (!order)
2592 return -ENOMEM;
2593 order >>= 1;
2594 goto again;
2595 }
2596
2597 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2598 pg->size = cnt;
2599
2600 if (cnt > count)
2601 cnt = count;
2602
2603 return cnt;
2604}
2605
2606static struct ftrace_page *
2607ftrace_allocate_pages(unsigned long num_to_init)
2608{
2609 struct ftrace_page *start_pg;
2610 struct ftrace_page *pg;
2611 int order;
2612 int cnt;
2613
2614 if (!num_to_init)
2615 return 0;
2616
2617 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2618 if (!pg)
2619 return NULL;
2620
2621 /*
2622 * Try to allocate as much as possible in one continues
2623 * location that fills in all of the space. We want to
2624 * waste as little space as possible.
2625 */
2626 for (;;) {
2627 cnt = ftrace_allocate_records(pg, num_to_init);
2628 if (cnt < 0)
2629 goto free_pages;
2630
2631 num_to_init -= cnt;
2632 if (!num_to_init)
2633 break;
2634
2635 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2636 if (!pg->next)
2637 goto free_pages;
2638
2639 pg = pg->next;
2640 }
2641
2642 return start_pg;
2643
2644 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002645 pg = start_pg;
2646 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002647 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2648 free_pages((unsigned long)pg->records, order);
2649 start_pg = pg->next;
2650 kfree(pg);
2651 pg = start_pg;
2652 }
2653 pr_info("ftrace: FAILED to allocate memory for functions\n");
2654 return NULL;
2655}
2656
Steven Rostedt5072c592008-05-12 21:20:43 +02002657#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2658
2659struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002660 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002661 loff_t func_pos;
2662 struct ftrace_page *pg;
2663 struct dyn_ftrace *func;
2664 struct ftrace_func_probe *probe;
2665 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002666 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002667 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002668 int hidx;
2669 int idx;
2670 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002671};
2672
Ingo Molnare309b412008-05-12 21:20:51 +02002673static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002674t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002675{
2676 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002677 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002678 struct hlist_head *hhd;
2679
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002680 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002681 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002682
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002683 if (iter->probe)
2684 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002685 retry:
2686 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2687 return NULL;
2688
2689 hhd = &ftrace_func_hash[iter->hidx];
2690
2691 if (hlist_empty(hhd)) {
2692 iter->hidx++;
2693 hnd = NULL;
2694 goto retry;
2695 }
2696
2697 if (!hnd)
2698 hnd = hhd->first;
2699 else {
2700 hnd = hnd->next;
2701 if (!hnd) {
2702 iter->hidx++;
2703 goto retry;
2704 }
2705 }
2706
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002707 if (WARN_ON_ONCE(!hnd))
2708 return NULL;
2709
2710 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2711
2712 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002713}
2714
2715static void *t_hash_start(struct seq_file *m, loff_t *pos)
2716{
2717 struct ftrace_iterator *iter = m->private;
2718 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002719 loff_t l;
2720
Steven Rostedt69a30832011-12-19 15:21:16 -05002721 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2722 return NULL;
2723
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002724 if (iter->func_pos > *pos)
2725 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002726
Li Zefand82d6242009-06-24 09:54:54 +08002727 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002728 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002729 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002730 if (!p)
2731 break;
2732 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002733 if (!p)
2734 return NULL;
2735
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002736 /* Only set this if we have an item */
2737 iter->flags |= FTRACE_ITER_HASH;
2738
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002739 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002740}
2741
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002742static int
2743t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002744{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002745 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002746
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002747 rec = iter->probe;
2748 if (WARN_ON_ONCE(!rec))
2749 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002750
Steven Rostedt809dcf22009-02-16 23:06:01 -05002751 if (rec->ops->print)
2752 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2753
Steven Rostedtb375a112009-09-17 00:05:58 -04002754 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002755
2756 if (rec->data)
2757 seq_printf(m, ":%p", rec->data);
2758 seq_putc(m, '\n');
2759
2760 return 0;
2761}
2762
2763static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002764t_next(struct seq_file *m, void *v, loff_t *pos)
2765{
2766 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002767 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002768 struct dyn_ftrace *rec = NULL;
2769
Steven Rostedt45a4a232011-04-21 23:16:46 -04002770 if (unlikely(ftrace_disabled))
2771 return NULL;
2772
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002773 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002774 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002775
Steven Rostedt5072c592008-05-12 21:20:43 +02002776 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002777 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002778
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002779 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002780 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002781
Steven Rostedt5072c592008-05-12 21:20:43 +02002782 retry:
2783 if (iter->idx >= iter->pg->index) {
2784 if (iter->pg->next) {
2785 iter->pg = iter->pg->next;
2786 iter->idx = 0;
2787 goto retry;
2788 }
2789 } else {
2790 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002791 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002792 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002793
Steven Rostedt41c52c02008-05-22 11:46:33 -04002794 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002795 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04002796
2797 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002798 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002799
Steven Rostedt5072c592008-05-12 21:20:43 +02002800 rec = NULL;
2801 goto retry;
2802 }
2803 }
2804
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002805 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002806 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002807
2808 iter->func = rec;
2809
2810 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002811}
2812
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002813static void reset_iter_read(struct ftrace_iterator *iter)
2814{
2815 iter->pos = 0;
2816 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002817 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002818}
2819
2820static void *t_start(struct seq_file *m, loff_t *pos)
2821{
2822 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002823 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002824 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002825 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002826
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002827 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002828
2829 if (unlikely(ftrace_disabled))
2830 return NULL;
2831
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002832 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002833 * If an lseek was done, then reset and start from beginning.
2834 */
2835 if (*pos < iter->pos)
2836 reset_iter_read(iter);
2837
2838 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002839 * For set_ftrace_filter reading, if we have the filter
2840 * off, we can short cut and just print out that all
2841 * functions are enabled.
2842 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002843 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002844 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002845 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002846 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002847 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002848 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002849 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002850 /* reset in case of seek/pread */
2851 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002852 return iter;
2853 }
2854
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002855 if (iter->flags & FTRACE_ITER_HASH)
2856 return t_hash_start(m, pos);
2857
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002858 /*
2859 * Unfortunately, we need to restart at ftrace_pages_start
2860 * every time we let go of the ftrace_mutex. This is because
2861 * those pointers can change without the lock.
2862 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002863 iter->pg = ftrace_pages_start;
2864 iter->idx = 0;
2865 for (l = 0; l <= *pos; ) {
2866 p = t_next(m, p, &l);
2867 if (!p)
2868 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002869 }
walimis5821e1b2008-11-15 15:19:06 +08002870
Steven Rostedt69a30832011-12-19 15:21:16 -05002871 if (!p)
2872 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002873
2874 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002875}
2876
2877static void t_stop(struct seq_file *m, void *p)
2878{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002879 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002880}
2881
2882static int t_show(struct seq_file *m, void *v)
2883{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002884 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002885 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002886
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002887 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002888 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002889
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002890 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002891 if (iter->flags & FTRACE_ITER_NOTRACE)
2892 seq_printf(m, "#### no functions disabled ####\n");
2893 else
2894 seq_printf(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002895 return 0;
2896 }
2897
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002898 rec = iter->func;
2899
Steven Rostedt5072c592008-05-12 21:20:43 +02002900 if (!rec)
2901 return 0;
2902
Steven Rostedt647bcd02011-05-03 14:39:21 -04002903 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002904 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002905 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002906 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002907 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2908 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2909 struct ftrace_ops *ops;
2910
2911 ops = ftrace_find_tramp_ops_curr(rec);
2912 if (ops && ops->trampoline)
2913 seq_printf(m, "\ttramp: %pS",
2914 (void *)ops->trampoline);
2915 else
2916 seq_printf(m, "\ttramp: ERROR!");
2917 }
2918 }
2919
Steven Rostedt647bcd02011-05-03 14:39:21 -04002920 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002921
2922 return 0;
2923}
2924
James Morris88e9d342009-09-22 16:43:43 -07002925static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002926 .start = t_start,
2927 .next = t_next,
2928 .stop = t_stop,
2929 .show = t_show,
2930};
2931
Ingo Molnare309b412008-05-12 21:20:51 +02002932static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002933ftrace_avail_open(struct inode *inode, struct file *file)
2934{
2935 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002936
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002937 if (unlikely(ftrace_disabled))
2938 return -ENODEV;
2939
Jiri Olsa50e18b92012-04-25 10:23:39 +02002940 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2941 if (iter) {
2942 iter->pg = ftrace_pages_start;
2943 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002944 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002945
Jiri Olsa50e18b92012-04-25 10:23:39 +02002946 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002947}
2948
Steven Rostedt647bcd02011-05-03 14:39:21 -04002949static int
2950ftrace_enabled_open(struct inode *inode, struct file *file)
2951{
2952 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002953
2954 if (unlikely(ftrace_disabled))
2955 return -ENODEV;
2956
Jiri Olsa50e18b92012-04-25 10:23:39 +02002957 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2958 if (iter) {
2959 iter->pg = ftrace_pages_start;
2960 iter->flags = FTRACE_ITER_ENABLED;
2961 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002962 }
2963
Jiri Olsa50e18b92012-04-25 10:23:39 +02002964 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002965}
2966
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002967/**
2968 * ftrace_regex_open - initialize function tracer filter files
2969 * @ops: The ftrace_ops that hold the hash filters
2970 * @flag: The type of filter to process
2971 * @inode: The inode, usually passed in to your open routine
2972 * @file: The file, usually passed in to your open routine
2973 *
2974 * ftrace_regex_open() initializes the filter files for the
2975 * @ops. Depending on @flag it may process the filter hash or
2976 * the notrace hash of @ops. With this called from the open
2977 * routine, you can use ftrace_filter_write() for the write
2978 * routine if @flag has FTRACE_ITER_FILTER set, or
2979 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002980 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002981 * release must call ftrace_regex_release().
2982 */
2983int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002984ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002985 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002986{
2987 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002988 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002989 int ret = 0;
2990
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002991 ftrace_ops_init(ops);
2992
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002993 if (unlikely(ftrace_disabled))
2994 return -ENODEV;
2995
Steven Rostedt5072c592008-05-12 21:20:43 +02002996 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2997 if (!iter)
2998 return -ENOMEM;
2999
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003000 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3001 kfree(iter);
3002 return -ENOMEM;
3003 }
3004
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003005 iter->ops = ops;
3006 iter->flags = flag;
3007
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003008 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003009
Steven Rostedtf45948e2011-05-02 12:29:25 -04003010 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003011 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003012 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003013 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003014
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003015 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003016 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3017
3018 if (file->f_flags & O_TRUNC)
3019 iter->hash = alloc_ftrace_hash(size_bits);
3020 else
3021 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3022
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003023 if (!iter->hash) {
3024 trace_parser_put(&iter->parser);
3025 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003026 ret = -ENOMEM;
3027 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003028 }
3029 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003030
Steven Rostedt5072c592008-05-12 21:20:43 +02003031 if (file->f_mode & FMODE_READ) {
3032 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003033
3034 ret = seq_open(file, &show_ftrace_seq_ops);
3035 if (!ret) {
3036 struct seq_file *m = file->private_data;
3037 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003038 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003039 /* Failed */
3040 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003041 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003042 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003043 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003044 } else
3045 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003046
3047 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003048 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003049
3050 return ret;
3051}
3052
Steven Rostedt41c52c02008-05-22 11:46:33 -04003053static int
3054ftrace_filter_open(struct inode *inode, struct file *file)
3055{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003056 struct ftrace_ops *ops = inode->i_private;
3057
3058 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003059 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3060 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003061}
3062
3063static int
3064ftrace_notrace_open(struct inode *inode, struct file *file)
3065{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003066 struct ftrace_ops *ops = inode->i_private;
3067
3068 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003069 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003070}
3071
Steven Rostedt64e7c442009-02-13 17:08:48 -05003072static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003073{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003074 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003075 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003076
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003077 switch (type) {
3078 case MATCH_FULL:
3079 if (strcmp(str, regex) == 0)
3080 matched = 1;
3081 break;
3082 case MATCH_FRONT_ONLY:
3083 if (strncmp(str, regex, len) == 0)
3084 matched = 1;
3085 break;
3086 case MATCH_MIDDLE_ONLY:
3087 if (strstr(str, regex))
3088 matched = 1;
3089 break;
3090 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003091 slen = strlen(str);
3092 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003093 matched = 1;
3094 break;
3095 }
3096
3097 return matched;
3098}
3099
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003100static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003101enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003102{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003103 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003104 int ret = 0;
3105
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003106 entry = ftrace_lookup_ip(hash, rec->ip);
3107 if (not) {
3108 /* Do nothing if it doesn't exist */
3109 if (!entry)
3110 return 0;
3111
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003112 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003113 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003114 /* Do nothing if it exists */
3115 if (entry)
3116 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003117
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003118 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003119 }
3120 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003121}
3122
Steven Rostedt64e7c442009-02-13 17:08:48 -05003123static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003124ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3125 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003126{
3127 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003128 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003129
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003130 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3131
3132 if (mod) {
3133 /* module lookup requires matching the module */
3134 if (!modname || strcmp(modname, mod))
3135 return 0;
3136
3137 /* blank search means to match all funcs in the mod */
3138 if (!len)
3139 return 1;
3140 }
3141
Steven Rostedt64e7c442009-02-13 17:08:48 -05003142 return ftrace_match(str, regex, len, type);
3143}
3144
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003145static int
3146match_records(struct ftrace_hash *hash, char *buff,
3147 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003148{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003149 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003150 struct ftrace_page *pg;
3151 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003152 int type = MATCH_FULL;
3153 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003154 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003155 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003156
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003157 if (len) {
3158 type = filter_parse_regex(buff, len, &search, &not);
3159 search_len = strlen(search);
3160 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003161
Steven Rostedt52baf112009-02-14 01:15:39 -05003162 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003163
3164 if (unlikely(ftrace_disabled))
3165 goto out_unlock;
3166
Steven Rostedt265c8312009-02-13 12:43:56 -05003167 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003168 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003169 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003170 if (ret < 0) {
3171 found = ret;
3172 goto out_unlock;
3173 }
Li Zefan311d16d2009-12-08 11:15:11 +08003174 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003175 }
3176 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003177 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003178 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003179
3180 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003181}
3182
Steven Rostedt64e7c442009-02-13 17:08:48 -05003183static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003184ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003185{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003186 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003187}
3188
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003189static int
3190ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003191{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003192 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003193
Steven Rostedt64e7c442009-02-13 17:08:48 -05003194 /* blank or '*' mean the same */
3195 if (strcmp(buff, "*") == 0)
3196 buff[0] = 0;
3197
3198 /* handle the case of 'dont filter this module' */
3199 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3200 buff[0] = 0;
3201 not = 1;
3202 }
3203
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003204 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003205}
3206
Steven Rostedtf6180772009-02-14 00:40:25 -05003207/*
3208 * We register the module command as a template to show others how
3209 * to register the a command as well.
3210 */
3211
3212static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003213ftrace_mod_callback(struct ftrace_hash *hash,
3214 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003215{
3216 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003217 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003218
3219 /*
3220 * cmd == 'mod' because we only registered this func
3221 * for the 'mod' ftrace_func_command.
3222 * But if you register one func with multiple commands,
3223 * you can tell which command was used by the cmd
3224 * parameter.
3225 */
3226
3227 /* we must have a module name */
3228 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003229 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003230
3231 mod = strsep(&param, ":");
3232 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003233 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003234
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003235 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003236 if (!ret)
3237 ret = -EINVAL;
3238 if (ret < 0)
3239 return ret;
3240
3241 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003242}
3243
3244static struct ftrace_func_command ftrace_mod_cmd = {
3245 .name = "mod",
3246 .func = ftrace_mod_callback,
3247};
3248
3249static int __init ftrace_mod_cmd_init(void)
3250{
3251 return register_ftrace_command(&ftrace_mod_cmd);
3252}
Steven Rostedt6f415672012-10-05 12:13:07 -04003253core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003254
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003255static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003256 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003257{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003258 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003259 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003260 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003261
3262 key = hash_long(ip, FTRACE_HASH_BITS);
3263
3264 hhd = &ftrace_func_hash[key];
3265
3266 if (hlist_empty(hhd))
3267 return;
3268
3269 /*
3270 * Disable preemption for these calls to prevent a RCU grace
3271 * period. This syncs the hash iteration and freeing of items
3272 * on the hash. rcu_read_lock is too dangerous here.
3273 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003274 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003275 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003276 if (entry->ip == ip)
3277 entry->ops->func(ip, parent_ip, &entry->data);
3278 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003279 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003280}
3281
Steven Rostedtb6887d72009-02-17 12:32:04 -05003282static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003283{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003284 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003285 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003286 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003287};
3288
Steven Rostedtb6887d72009-02-17 12:32:04 -05003289static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003290
Steven Rostedtb6887d72009-02-17 12:32:04 -05003291static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003292{
Steven Rostedtb8489142011-05-04 09:27:52 -04003293 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003294 int i;
3295
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003296 if (ftrace_probe_registered) {
3297 /* still need to update the function call sites */
3298 if (ftrace_enabled)
3299 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003300 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003301 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003302
3303 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3304 struct hlist_head *hhd = &ftrace_func_hash[i];
3305 if (hhd->first)
3306 break;
3307 }
3308 /* Nothing registered? */
3309 if (i == FTRACE_FUNC_HASHSIZE)
3310 return;
3311
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003312 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003313
Steven Rostedtb6887d72009-02-17 12:32:04 -05003314 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003315}
3316
Steven Rostedtb6887d72009-02-17 12:32:04 -05003317static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003318{
3319 int i;
3320
Steven Rostedtb6887d72009-02-17 12:32:04 -05003321 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003322 return;
3323
3324 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3325 struct hlist_head *hhd = &ftrace_func_hash[i];
3326 if (hhd->first)
3327 return;
3328 }
3329
3330 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003331 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003332
Steven Rostedtb6887d72009-02-17 12:32:04 -05003333 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003334}
3335
3336
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003337static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003338{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003339 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003340 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003341 kfree(entry);
3342}
3343
Steven Rostedt59df055f2009-02-14 15:29:06 -05003344int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003345register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003346 void *data)
3347{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003348 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003349 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003350 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003351 struct ftrace_page *pg;
3352 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003353 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003354 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003355 int count = 0;
3356 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003357 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003358
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003359 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003360 len = strlen(search);
3361
Steven Rostedtb6887d72009-02-17 12:32:04 -05003362 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003363 if (WARN_ON(not))
3364 return -EINVAL;
3365
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003366 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003367
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003368 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3369 if (!hash) {
3370 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003371 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003372 }
3373
3374 if (unlikely(ftrace_disabled)) {
3375 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003376 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003377 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003378
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003379 mutex_lock(&ftrace_lock);
3380
Steven Rostedt59df055f2009-02-14 15:29:06 -05003381 do_for_each_ftrace_rec(pg, rec) {
3382
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003383 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003384 continue;
3385
3386 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3387 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003388 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003389 if (!count)
3390 count = -ENOMEM;
3391 goto out_unlock;
3392 }
3393
3394 count++;
3395
3396 entry->data = data;
3397
3398 /*
3399 * The caller might want to do something special
3400 * for each function we find. We call the callback
3401 * to give the caller an opportunity to do so.
3402 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003403 if (ops->init) {
3404 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003405 /* caller does not like this func */
3406 kfree(entry);
3407 continue;
3408 }
3409 }
3410
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003411 ret = enter_record(hash, rec, 0);
3412 if (ret < 0) {
3413 kfree(entry);
3414 count = ret;
3415 goto out_unlock;
3416 }
3417
Steven Rostedt59df055f2009-02-14 15:29:06 -05003418 entry->ops = ops;
3419 entry->ip = rec->ip;
3420
3421 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3422 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3423
3424 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003425
3426 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3427 if (ret < 0)
3428 count = ret;
3429
Steven Rostedtb6887d72009-02-17 12:32:04 -05003430 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003431
3432 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003433 mutex_unlock(&ftrace_lock);
3434 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003435 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003436 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003437
3438 return count;
3439}
3440
3441enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003442 PROBE_TEST_FUNC = 1,
3443 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003444};
3445
3446static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003447__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003448 void *data, int flags)
3449{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003450 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003451 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003452 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003453 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003454 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003455 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003456 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003457 char str[KSYM_SYMBOL_LEN];
3458 int type = MATCH_FULL;
3459 int i, len = 0;
3460 char *search;
3461
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003462 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003463 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003464 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003465 int not;
3466
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003467 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003468 len = strlen(search);
3469
Steven Rostedtb6887d72009-02-17 12:32:04 -05003470 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003471 if (WARN_ON(not))
3472 return;
3473 }
3474
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003475 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003476
3477 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3478 if (!hash)
3479 /* Hmm, should report this somehow */
3480 goto out_unlock;
3481
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003482 INIT_LIST_HEAD(&free_list);
3483
Steven Rostedt59df055f2009-02-14 15:29:06 -05003484 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3485 struct hlist_head *hhd = &ftrace_func_hash[i];
3486
Sasha Levinb67bfe02013-02-27 17:06:00 -08003487 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003488
3489 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003490 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003491 continue;
3492
Steven Rostedtb6887d72009-02-17 12:32:04 -05003493 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003494 continue;
3495
3496 /* do this last, since it is the most expensive */
3497 if (glob) {
3498 kallsyms_lookup(entry->ip, NULL, NULL,
3499 NULL, str);
3500 if (!ftrace_match(str, glob, len, type))
3501 continue;
3502 }
3503
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003504 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3505 /* It is possible more than one entry had this ip */
3506 if (rec_entry)
3507 free_hash_entry(hash, rec_entry);
3508
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003509 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003510 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003511 }
3512 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003513 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003514 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003515 /*
3516 * Remove after the disable is called. Otherwise, if the last
3517 * probe is removed, a null hash means *all enabled*.
3518 */
3519 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003520 synchronize_sched();
3521 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3522 list_del(&entry->free_list);
3523 ftrace_free_entry(entry);
3524 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003525 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003526
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003527 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003528 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003529 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003530}
3531
3532void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003533unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003534 void *data)
3535{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003536 __unregister_ftrace_function_probe(glob, ops, data,
3537 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003538}
3539
3540void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003541unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003542{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003543 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003544}
3545
Steven Rostedtb6887d72009-02-17 12:32:04 -05003546void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003547{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003548 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003549}
3550
Steven Rostedtf6180772009-02-14 00:40:25 -05003551static LIST_HEAD(ftrace_commands);
3552static DEFINE_MUTEX(ftrace_cmd_mutex);
3553
Tom Zanussi38de93a2013-10-24 08:34:18 -05003554/*
3555 * Currently we only register ftrace commands from __init, so mark this
3556 * __init too.
3557 */
3558__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003559{
3560 struct ftrace_func_command *p;
3561 int ret = 0;
3562
3563 mutex_lock(&ftrace_cmd_mutex);
3564 list_for_each_entry(p, &ftrace_commands, list) {
3565 if (strcmp(cmd->name, p->name) == 0) {
3566 ret = -EBUSY;
3567 goto out_unlock;
3568 }
3569 }
3570 list_add(&cmd->list, &ftrace_commands);
3571 out_unlock:
3572 mutex_unlock(&ftrace_cmd_mutex);
3573
3574 return ret;
3575}
3576
Tom Zanussi38de93a2013-10-24 08:34:18 -05003577/*
3578 * Currently we only unregister ftrace commands from __init, so mark
3579 * this __init too.
3580 */
3581__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003582{
3583 struct ftrace_func_command *p, *n;
3584 int ret = -ENODEV;
3585
3586 mutex_lock(&ftrace_cmd_mutex);
3587 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3588 if (strcmp(cmd->name, p->name) == 0) {
3589 ret = 0;
3590 list_del_init(&p->list);
3591 goto out_unlock;
3592 }
3593 }
3594 out_unlock:
3595 mutex_unlock(&ftrace_cmd_mutex);
3596
3597 return ret;
3598}
3599
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003600static int ftrace_process_regex(struct ftrace_hash *hash,
3601 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003602{
Steven Rostedtf6180772009-02-14 00:40:25 -05003603 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003604 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003605 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003606
3607 func = strsep(&next, ":");
3608
3609 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003610 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003611 if (!ret)
3612 ret = -EINVAL;
3613 if (ret < 0)
3614 return ret;
3615 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003616 }
3617
Steven Rostedtf6180772009-02-14 00:40:25 -05003618 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003619
3620 command = strsep(&next, ":");
3621
Steven Rostedtf6180772009-02-14 00:40:25 -05003622 mutex_lock(&ftrace_cmd_mutex);
3623 list_for_each_entry(p, &ftrace_commands, list) {
3624 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003625 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003626 goto out_unlock;
3627 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003628 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003629 out_unlock:
3630 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003631
Steven Rostedtf6180772009-02-14 00:40:25 -05003632 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003633}
3634
Ingo Molnare309b412008-05-12 21:20:51 +02003635static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003636ftrace_regex_write(struct file *file, const char __user *ubuf,
3637 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003638{
3639 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003640 struct trace_parser *parser;
3641 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003642
Li Zefan4ba79782009-09-22 13:52:20 +08003643 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003644 return 0;
3645
Steven Rostedt5072c592008-05-12 21:20:43 +02003646 if (file->f_mode & FMODE_READ) {
3647 struct seq_file *m = file->private_data;
3648 iter = m->private;
3649 } else
3650 iter = file->private_data;
3651
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003652 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003653 return -ENODEV;
3654
3655 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003656
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003657 parser = &iter->parser;
3658 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003659
Li Zefan4ba79782009-09-22 13:52:20 +08003660 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003661 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003662 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003663 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003664 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003665 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003666 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003667 }
3668
Steven Rostedt5072c592008-05-12 21:20:43 +02003669 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003670 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003671 return ret;
3672}
3673
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003674ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003675ftrace_filter_write(struct file *file, const char __user *ubuf,
3676 size_t cnt, loff_t *ppos)
3677{
3678 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3679}
3680
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003681ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003682ftrace_notrace_write(struct file *file, const char __user *ubuf,
3683 size_t cnt, loff_t *ppos)
3684{
3685 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3686}
3687
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003688static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003689ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3690{
3691 struct ftrace_func_entry *entry;
3692
3693 if (!ftrace_location(ip))
3694 return -EINVAL;
3695
3696 if (remove) {
3697 entry = ftrace_lookup_ip(hash, ip);
3698 if (!entry)
3699 return -ENOENT;
3700 free_hash_entry(hash, entry);
3701 return 0;
3702 }
3703
3704 return add_hash_entry(hash, ip);
3705}
3706
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003707static void ftrace_ops_update_code(struct ftrace_ops *ops)
3708{
3709 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3710 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3711}
3712
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003713static int
3714ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3715 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003716{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003717 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003718 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003719 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003720
Steven Rostedt41c52c02008-05-22 11:46:33 -04003721 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003722 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003723
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003724 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003725
Steven Rostedtf45948e2011-05-02 12:29:25 -04003726 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003727 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003728 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003729 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003730
Wang Nanb972cc52014-07-15 08:40:20 +08003731 if (reset)
3732 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3733 else
3734 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3735
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003736 if (!hash) {
3737 ret = -ENOMEM;
3738 goto out_regex_unlock;
3739 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003740
Jiri Olsaac483c42012-01-02 10:04:14 +01003741 if (buf && !ftrace_match_records(hash, buf, len)) {
3742 ret = -EINVAL;
3743 goto out_regex_unlock;
3744 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003745 if (ip) {
3746 ret = ftrace_match_addr(hash, ip, remove);
3747 if (ret < 0)
3748 goto out_regex_unlock;
3749 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003750
3751 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003752 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003753 if (!ret)
3754 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003755
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003756 mutex_unlock(&ftrace_lock);
3757
Jiri Olsaac483c42012-01-02 10:04:14 +01003758 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003759 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003760
3761 free_ftrace_hash(hash);
3762 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003763}
3764
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003765static int
3766ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3767 int reset, int enable)
3768{
3769 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3770}
3771
3772/**
3773 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3774 * @ops - the ops to set the filter with
3775 * @ip - the address to add to or remove from the filter.
3776 * @remove - non zero to remove the ip from the filter
3777 * @reset - non zero to reset all filters before applying this filter.
3778 *
3779 * Filters denote which functions should be enabled when tracing is enabled
3780 * If @ip is NULL, it failes to update filter.
3781 */
3782int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3783 int remove, int reset)
3784{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003785 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003786 return ftrace_set_addr(ops, ip, remove, reset, 1);
3787}
3788EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3789
3790static int
3791ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3792 int reset, int enable)
3793{
3794 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3795}
3796
Steven Rostedt77a2b372008-05-12 21:20:45 +02003797/**
3798 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003799 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003800 * @buf - the string that holds the function filter text.
3801 * @len - the length of the string.
3802 * @reset - non zero to reset all filters before applying this filter.
3803 *
3804 * Filters denote which functions should be enabled when tracing is enabled.
3805 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3806 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003807int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003808 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003809{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003810 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003811 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003812}
Steven Rostedt936e0742011-05-05 22:54:01 -04003813EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003814
Steven Rostedt41c52c02008-05-22 11:46:33 -04003815/**
3816 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003817 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003818 * @buf - the string that holds the function notrace text.
3819 * @len - the length of the string.
3820 * @reset - non zero to reset all filters before applying this filter.
3821 *
3822 * Notrace Filters denote which functions should not be enabled when tracing
3823 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3824 * for tracing.
3825 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003826int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003827 int len, int reset)
3828{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003829 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003830 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003831}
3832EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3833/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003834 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003835 * @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 */
3842void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3843{
3844 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3845}
3846EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3847
3848/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003849 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003850 * @buf - the string that holds the function notrace text.
3851 * @len - the length of the string.
3852 * @reset - non zero to reset all filters before applying this filter.
3853 *
3854 * Notrace Filters denote which functions should not be enabled when tracing
3855 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3856 * for tracing.
3857 */
3858void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003859{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003860 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003861}
Steven Rostedt936e0742011-05-05 22:54:01 -04003862EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003863
Steven Rostedt2af15d62009-05-28 13:37:24 -04003864/*
3865 * command line interface to allow users to set filters on boot up.
3866 */
3867#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3868static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3869static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3870
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003871/* Used by function selftest to not test if filter is set */
3872bool ftrace_filter_param __initdata;
3873
Steven Rostedt2af15d62009-05-28 13:37:24 -04003874static int __init set_ftrace_notrace(char *str)
3875{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003876 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003877 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003878 return 1;
3879}
3880__setup("ftrace_notrace=", set_ftrace_notrace);
3881
3882static int __init set_ftrace_filter(char *str)
3883{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003884 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003885 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003886 return 1;
3887}
3888__setup("ftrace_filter=", set_ftrace_filter);
3889
Stefan Assmann369bc182009-10-12 22:17:21 +02003890#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003891static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003892static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003893static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003894
Stefan Assmann369bc182009-10-12 22:17:21 +02003895static int __init set_graph_function(char *str)
3896{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003897 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003898 return 1;
3899}
3900__setup("ftrace_graph_filter=", set_graph_function);
3901
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003902static int __init set_graph_notrace_function(char *str)
3903{
3904 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3905 return 1;
3906}
3907__setup("ftrace_graph_notrace=", set_graph_notrace_function);
3908
3909static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02003910{
3911 int ret;
3912 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003913 unsigned long *table = ftrace_graph_funcs;
3914 int *count = &ftrace_graph_count;
3915
3916 if (!enable) {
3917 table = ftrace_graph_notrace_funcs;
3918 count = &ftrace_graph_notrace_count;
3919 }
Stefan Assmann369bc182009-10-12 22:17:21 +02003920
3921 while (buf) {
3922 func = strsep(&buf, ",");
3923 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003924 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003925 if (ret)
3926 printk(KERN_DEBUG "ftrace: function %s not "
3927 "traceable\n", func);
3928 }
3929}
3930#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3931
Steven Rostedt2a85a372011-12-19 21:57:44 -05003932void __init
3933ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003934{
3935 char *func;
3936
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003937 ftrace_ops_init(ops);
3938
Steven Rostedt2af15d62009-05-28 13:37:24 -04003939 while (buf) {
3940 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003941 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003942 }
3943}
3944
3945static void __init set_ftrace_early_filters(void)
3946{
3947 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003948 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003949 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003950 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003951#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3952 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003953 set_ftrace_early_graph(ftrace_graph_buf, 1);
3954 if (ftrace_graph_notrace_buf[0])
3955 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003956#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003957}
3958
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003959int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003960{
3961 struct seq_file *m = (struct seq_file *)file->private_data;
3962 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003963 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003964 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003965 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003966 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003967
Steven Rostedt5072c592008-05-12 21:20:43 +02003968 if (file->f_mode & FMODE_READ) {
3969 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003970 seq_release(inode, file);
3971 } else
3972 iter = file->private_data;
3973
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003974 parser = &iter->parser;
3975 if (trace_parser_loaded(parser)) {
3976 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003977 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003978 }
3979
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003980 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003981
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003982 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003983
Steven Rostedt058e2972011-04-29 22:35:33 -04003984 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003985 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3986
3987 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003988 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003989 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003990 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003991
Steven Rostedt058e2972011-04-29 22:35:33 -04003992 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003993 ret = ftrace_hash_move(iter->ops, filter_hash,
3994 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003995 if (!ret)
3996 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003997
Steven Rostedt058e2972011-04-29 22:35:33 -04003998 mutex_unlock(&ftrace_lock);
3999 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004000
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004001 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004002 free_ftrace_hash(iter->hash);
4003 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004004
Steven Rostedt5072c592008-05-12 21:20:43 +02004005 return 0;
4006}
4007
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004008static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004009 .open = ftrace_avail_open,
4010 .read = seq_read,
4011 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004012 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004013};
4014
Steven Rostedt647bcd02011-05-03 14:39:21 -04004015static const struct file_operations ftrace_enabled_fops = {
4016 .open = ftrace_enabled_open,
4017 .read = seq_read,
4018 .llseek = seq_lseek,
4019 .release = seq_release_private,
4020};
4021
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004022static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004023 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004024 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004025 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004026 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004027 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004028};
4029
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004030static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004031 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004032 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004033 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004034 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004035 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004036};
4037
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004038#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4039
4040static DEFINE_MUTEX(graph_lock);
4041
4042int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004043int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004044unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004045unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004046
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004047struct ftrace_graph_data {
4048 unsigned long *table;
4049 size_t size;
4050 int *count;
4051 const struct seq_operations *seq_ops;
4052};
4053
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004054static void *
Li Zefan85951842009-06-24 09:54:00 +08004055__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004056{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004057 struct ftrace_graph_data *fgd = m->private;
4058
4059 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004060 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004061 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004062}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004063
Li Zefan85951842009-06-24 09:54:00 +08004064static void *
4065g_next(struct seq_file *m, void *v, loff_t *pos)
4066{
4067 (*pos)++;
4068 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004069}
4070
4071static void *g_start(struct seq_file *m, loff_t *pos)
4072{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004073 struct ftrace_graph_data *fgd = m->private;
4074
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004075 mutex_lock(&graph_lock);
4076
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004077 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004078 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004079 return (void *)1;
4080
Li Zefan85951842009-06-24 09:54:00 +08004081 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004082}
4083
4084static void g_stop(struct seq_file *m, void *p)
4085{
4086 mutex_unlock(&graph_lock);
4087}
4088
4089static int g_show(struct seq_file *m, void *v)
4090{
4091 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004092
4093 if (!ptr)
4094 return 0;
4095
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004096 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004097 struct ftrace_graph_data *fgd = m->private;
4098
4099 if (fgd->table == ftrace_graph_funcs)
4100 seq_printf(m, "#### all functions enabled ####\n");
4101 else
4102 seq_printf(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004103 return 0;
4104 }
4105
Steven Rostedtb375a112009-09-17 00:05:58 -04004106 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004107
4108 return 0;
4109}
4110
James Morris88e9d342009-09-22 16:43:43 -07004111static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004112 .start = g_start,
4113 .next = g_next,
4114 .stop = g_stop,
4115 .show = g_show,
4116};
4117
4118static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004119__ftrace_graph_open(struct inode *inode, struct file *file,
4120 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004121{
4122 int ret = 0;
4123
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004124 mutex_lock(&graph_lock);
4125 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004126 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004127 *fgd->count = 0;
4128 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004129 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004130 mutex_unlock(&graph_lock);
4131
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004132 if (file->f_mode & FMODE_READ) {
4133 ret = seq_open(file, fgd->seq_ops);
4134 if (!ret) {
4135 struct seq_file *m = file->private_data;
4136 m->private = fgd;
4137 }
4138 } else
4139 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004140
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004141 return ret;
4142}
4143
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004144static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004145ftrace_graph_open(struct inode *inode, struct file *file)
4146{
4147 struct ftrace_graph_data *fgd;
4148
4149 if (unlikely(ftrace_disabled))
4150 return -ENODEV;
4151
4152 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4153 if (fgd == NULL)
4154 return -ENOMEM;
4155
4156 fgd->table = ftrace_graph_funcs;
4157 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4158 fgd->count = &ftrace_graph_count;
4159 fgd->seq_ops = &ftrace_graph_seq_ops;
4160
4161 return __ftrace_graph_open(inode, file, fgd);
4162}
4163
4164static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004165ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4166{
4167 struct ftrace_graph_data *fgd;
4168
4169 if (unlikely(ftrace_disabled))
4170 return -ENODEV;
4171
4172 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4173 if (fgd == NULL)
4174 return -ENOMEM;
4175
4176 fgd->table = ftrace_graph_notrace_funcs;
4177 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4178 fgd->count = &ftrace_graph_notrace_count;
4179 fgd->seq_ops = &ftrace_graph_seq_ops;
4180
4181 return __ftrace_graph_open(inode, file, fgd);
4182}
4183
4184static int
Li Zefan87827112009-07-23 11:29:11 +08004185ftrace_graph_release(struct inode *inode, struct file *file)
4186{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004187 if (file->f_mode & FMODE_READ) {
4188 struct seq_file *m = file->private_data;
4189
4190 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004191 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004192 } else {
4193 kfree(file->private_data);
4194 }
4195
Li Zefan87827112009-07-23 11:29:11 +08004196 return 0;
4197}
4198
4199static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004200ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004201{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004202 struct dyn_ftrace *rec;
4203 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004204 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004205 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004206 int type, not;
4207 char *search;
4208 bool exists;
4209 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004210
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004211 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004212 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004213 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004214 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004215
4216 search_len = strlen(search);
4217
Steven Rostedt52baf112009-02-14 01:15:39 -05004218 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004219
4220 if (unlikely(ftrace_disabled)) {
4221 mutex_unlock(&ftrace_lock);
4222 return -ENODEV;
4223 }
4224
Steven Rostedt265c8312009-02-13 12:43:56 -05004225 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004226
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004227 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004228 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004229 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004230 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004231 if (array[i] == rec->ip) {
4232 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004233 break;
4234 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004235 }
4236
4237 if (!not) {
4238 fail = 0;
4239 if (!exists) {
4240 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004241 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004242 goto out;
4243 }
4244 } else {
4245 if (exists) {
4246 array[i] = array[--(*idx)];
4247 array[*idx] = 0;
4248 fail = 0;
4249 }
4250 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004251 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004252 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004253out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004254 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004255
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004256 if (fail)
4257 return -EINVAL;
4258
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004259 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004260}
4261
4262static ssize_t
4263ftrace_graph_write(struct file *file, const char __user *ubuf,
4264 size_t cnt, loff_t *ppos)
4265{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004266 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004267 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004268 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004269
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004270 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004271 return 0;
4272
Namhyung Kim6a101082013-10-14 17:24:25 +09004273 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4274 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004275
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004276 read = trace_get_user(&parser, ubuf, cnt, ppos);
4277
Li Zefan4ba79782009-09-22 13:52:20 +08004278 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004279 parser.buffer[parser.idx] = 0;
4280
Namhyung Kim6a101082013-10-14 17:24:25 +09004281 mutex_lock(&graph_lock);
4282
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004283 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004284 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4285 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004286
4287 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004288 }
4289
Namhyung Kim6a101082013-10-14 17:24:25 +09004290 if (!ret)
4291 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004292
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004293 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004294
4295 return ret;
4296}
4297
4298static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004299 .open = ftrace_graph_open,
4300 .read = seq_read,
4301 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004302 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004303 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004304};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004305
4306static const struct file_operations ftrace_graph_notrace_fops = {
4307 .open = ftrace_graph_notrace_open,
4308 .read = seq_read,
4309 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004310 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004311 .release = ftrace_graph_release,
4312};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004313#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4314
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004315void ftrace_create_filter_files(struct ftrace_ops *ops,
4316 struct dentry *parent)
4317{
4318
4319 trace_create_file("set_ftrace_filter", 0644, parent,
4320 ops, &ftrace_filter_fops);
4321
4322 trace_create_file("set_ftrace_notrace", 0644, parent,
4323 ops, &ftrace_notrace_fops);
4324}
4325
4326/*
4327 * The name "destroy_filter_files" is really a misnomer. Although
4328 * in the future, it may actualy delete the files, but this is
4329 * really intended to make sure the ops passed in are disabled
4330 * and that when this function returns, the caller is free to
4331 * free the ops.
4332 *
4333 * The "destroy" name is only to match the "create" name that this
4334 * should be paired with.
4335 */
4336void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4337{
4338 mutex_lock(&ftrace_lock);
4339 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4340 ftrace_shutdown(ops, 0);
4341 ops->flags |= FTRACE_OPS_FL_DELETED;
4342 mutex_unlock(&ftrace_lock);
4343}
4344
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004345static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004346{
Steven Rostedt5072c592008-05-12 21:20:43 +02004347
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004348 trace_create_file("available_filter_functions", 0444,
4349 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004350
Steven Rostedt647bcd02011-05-03 14:39:21 -04004351 trace_create_file("enabled_functions", 0444,
4352 d_tracer, NULL, &ftrace_enabled_fops);
4353
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004354 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004355
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004356#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004357 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004358 NULL,
4359 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004360 trace_create_file("set_graph_notrace", 0444, d_tracer,
4361 NULL,
4362 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004363#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4364
Steven Rostedt5072c592008-05-12 21:20:43 +02004365 return 0;
4366}
4367
Steven Rostedt9fd49322012-04-24 22:32:06 -04004368static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004369{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004370 const unsigned long *ipa = a;
4371 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004372
Steven Rostedt9fd49322012-04-24 22:32:06 -04004373 if (*ipa > *ipb)
4374 return 1;
4375 if (*ipa < *ipb)
4376 return -1;
4377 return 0;
4378}
4379
4380static void ftrace_swap_ips(void *a, void *b, int size)
4381{
4382 unsigned long *ipa = a;
4383 unsigned long *ipb = b;
4384 unsigned long t;
4385
4386 t = *ipa;
4387 *ipa = *ipb;
4388 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004389}
4390
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004391static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004392 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004393 unsigned long *end)
4394{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004395 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004396 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004397 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004398 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004399 unsigned long *p;
4400 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004401 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004402 int ret = -ENOMEM;
4403
4404 count = end - start;
4405
4406 if (!count)
4407 return 0;
4408
Steven Rostedt9fd49322012-04-24 22:32:06 -04004409 sort(start, count, sizeof(*start),
4410 ftrace_cmp_ips, ftrace_swap_ips);
4411
Steven Rostedt706c81f2012-04-24 23:45:26 -04004412 start_pg = ftrace_allocate_pages(count);
4413 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004414 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004415
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004416 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004417
Steven Rostedt32082302011-12-16 14:42:37 -05004418 /*
4419 * Core and each module needs their own pages, as
4420 * modules will free them when they are removed.
4421 * Force a new page to be allocated for modules.
4422 */
Steven Rostedta7900872011-12-16 16:23:44 -05004423 if (!mod) {
4424 WARN_ON(ftrace_pages || ftrace_pages_start);
4425 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004426 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004427 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004428 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004429 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004430
Steven Rostedta7900872011-12-16 16:23:44 -05004431 if (WARN_ON(ftrace_pages->next)) {
4432 /* Hmm, we have free pages? */
4433 while (ftrace_pages->next)
4434 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004435 }
Steven Rostedta7900872011-12-16 16:23:44 -05004436
Steven Rostedt706c81f2012-04-24 23:45:26 -04004437 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004438 }
4439
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004440 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004441 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004442 while (p < end) {
4443 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004444 /*
4445 * Some architecture linkers will pad between
4446 * the different mcount_loc sections of different
4447 * object files to satisfy alignments.
4448 * Skip any NULL pointers.
4449 */
4450 if (!addr)
4451 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004452
4453 if (pg->index == pg->size) {
4454 /* We should have allocated enough */
4455 if (WARN_ON(!pg->next))
4456 break;
4457 pg = pg->next;
4458 }
4459
4460 rec = &pg->records[pg->index++];
4461 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004462 }
4463
Steven Rostedt706c81f2012-04-24 23:45:26 -04004464 /* We should have used all pages */
4465 WARN_ON(pg->next);
4466
4467 /* Assign the last page to ftrace_pages */
4468 ftrace_pages = pg;
4469
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004470 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004471 * We only need to disable interrupts on start up
4472 * because we are modifying code that an interrupt
4473 * may execute, and the modification is not atomic.
4474 * But for modules, nothing runs the code we modify
4475 * until we are finished with it, and there's no
4476 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004477 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004478 if (!mod)
4479 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004480 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004481 if (!mod)
4482 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004483 ret = 0;
4484 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004485 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004486
Steven Rostedta7900872011-12-16 16:23:44 -05004487 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004488}
4489
Steven Rostedt93eb6772009-04-15 13:24:06 -04004490#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004491
4492#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4493
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004494void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004495{
4496 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004497 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004498 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004499 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004500
Steven Rostedt93eb6772009-04-15 13:24:06 -04004501 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004502
4503 if (ftrace_disabled)
4504 goto out_unlock;
4505
Steven Rostedt32082302011-12-16 14:42:37 -05004506 /*
4507 * Each module has its own ftrace_pages, remove
4508 * them from the list.
4509 */
4510 last_pg = &ftrace_pages_start;
4511 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4512 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004513 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004514 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004515 * As core pages are first, the first
4516 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004517 */
Steven Rostedt32082302011-12-16 14:42:37 -05004518 if (WARN_ON(pg == ftrace_pages_start))
4519 goto out_unlock;
4520
4521 /* Check if we are deleting the last page */
4522 if (pg == ftrace_pages)
4523 ftrace_pages = next_to_ftrace_page(last_pg);
4524
4525 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004526 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4527 free_pages((unsigned long)pg->records, order);
4528 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004529 } else
4530 last_pg = &pg->next;
4531 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004532 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004533 mutex_unlock(&ftrace_lock);
4534}
4535
4536static void ftrace_init_module(struct module *mod,
4537 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004538{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004539 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004540 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004541 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004542}
4543
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004544void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004545{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004546 ftrace_init_module(mod, mod->ftrace_callsites,
4547 mod->ftrace_callsites +
4548 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004549}
4550
4551static int ftrace_module_notify_exit(struct notifier_block *self,
4552 unsigned long val, void *data)
4553{
4554 struct module *mod = data;
4555
4556 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004557 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004558
4559 return 0;
4560}
4561#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004562static int ftrace_module_notify_exit(struct notifier_block *self,
4563 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004564{
4565 return 0;
4566}
4567#endif /* CONFIG_MODULES */
4568
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004569struct notifier_block ftrace_module_exit_nb = {
4570 .notifier_call = ftrace_module_notify_exit,
4571 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4572};
4573
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004574void __init ftrace_init(void)
4575{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004576 extern unsigned long __start_mcount_loc[];
4577 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004578 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004579 int ret;
4580
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004581 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004582 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004583 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004584 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004585 goto failed;
4586
4587 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004588 if (!count) {
4589 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004590 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004591 }
4592
4593 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4594 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004595
4596 last_ftrace_enabled = ftrace_enabled = 1;
4597
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004598 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004599 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004600 __stop_mcount_loc);
4601
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004602 ret = register_module_notifier(&ftrace_module_exit_nb);
4603 if (ret)
4604 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004605
Steven Rostedt2af15d62009-05-28 13:37:24 -04004606 set_ftrace_early_filters();
4607
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004608 return;
4609 failed:
4610 ftrace_disabled = 1;
4611}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004612
Steven Rostedt3d083392008-05-12 21:20:42 +02004613#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004614
Steven Rostedt2b499382011-05-03 22:49:52 -04004615static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004616 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004617 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004618 INIT_OPS_HASH(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004619};
4620
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004621static int __init ftrace_nodyn_init(void)
4622{
4623 ftrace_enabled = 1;
4624 return 0;
4625}
Steven Rostedt6f415672012-10-05 12:13:07 -04004626core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004627
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004628static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4629static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004630/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004631# define ftrace_startup(ops, command) \
4632 ({ \
4633 int ___ret = __register_ftrace_function(ops); \
4634 if (!___ret) \
4635 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4636 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004637 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004638# define ftrace_shutdown(ops, command) \
4639 ({ \
4640 int ___ret = __unregister_ftrace_function(ops); \
4641 if (!___ret) \
4642 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4643 ___ret; \
4644 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004645
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004646# define ftrace_startup_sysctl() do { } while (0)
4647# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004648
4649static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004650ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004651{
4652 return 1;
4653}
4654
Steven Rostedt3d083392008-05-12 21:20:42 +02004655#endif /* CONFIG_DYNAMIC_FTRACE */
4656
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004657__init void ftrace_init_global_array_ops(struct trace_array *tr)
4658{
4659 tr->ops = &global_ops;
4660 tr->ops->private = tr;
4661}
4662
4663void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4664{
4665 /* If we filter on pids, update to use the pid function */
4666 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4667 if (WARN_ON(tr->ops->func != ftrace_stub))
4668 printk("ftrace ops had %pS for function\n",
4669 tr->ops->func);
4670 /* Only the top level instance does pid tracing */
4671 if (!list_empty(&ftrace_pids)) {
4672 set_ftrace_pid_function(func);
4673 func = ftrace_pid_func;
4674 }
4675 }
4676 tr->ops->func = func;
4677 tr->ops->private = tr;
4678}
4679
4680void ftrace_reset_array_ops(struct trace_array *tr)
4681{
4682 tr->ops->func = ftrace_stub;
4683}
4684
Steven Rostedtb8489142011-05-04 09:27:52 -04004685static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004686ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004687 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004688{
Jiri Olsae2484912012-02-15 15:51:48 +01004689 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4690 return;
4691
4692 /*
4693 * Some of the ops may be dynamically allocated,
4694 * they must be freed after a synchronize_sched().
4695 */
4696 preempt_disable_notrace();
4697 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004698
4699 /*
4700 * Control funcs (perf) uses RCU. Only trace if
4701 * RCU is currently active.
4702 */
4703 if (!rcu_is_watching())
4704 goto out;
4705
Steven Rostedt0a016402012-11-02 17:03:03 -04004706 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004707 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4708 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004709 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004710 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004711 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004712 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004713 trace_recursion_clear(TRACE_CONTROL_BIT);
4714 preempt_enable_notrace();
4715}
4716
4717static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004718 .func = ftrace_ops_control_func,
4719 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004720 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004721};
4722
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004723static inline void
4724__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004725 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004726{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004727 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004728 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004729
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004730 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4731 if (bit < 0)
4732 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004733
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004734 /*
4735 * Some of the ops may be dynamically allocated,
4736 * they must be freed after a synchronize_sched().
4737 */
4738 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004739 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004740 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004741 if (FTRACE_WARN_ON(!op->func)) {
4742 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004743 goto out;
4744 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004745 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004746 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004747 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004748out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004749 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004750 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004751}
4752
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004753/*
4754 * Some archs only support passing ip and parent_ip. Even though
4755 * the list function ignores the op parameter, we do not want any
4756 * C side effects, where a function is called without the caller
4757 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004758 * Archs are to support both the regs and ftrace_ops at the same time.
4759 * If they support ftrace_ops, it is assumed they support regs.
4760 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004761 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4762 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004763 * An architecture can pass partial regs with ftrace_ops and still
4764 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004765 */
4766#if ARCH_SUPPORTS_FTRACE_OPS
4767static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004768 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004769{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004770 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004771}
4772#else
4773static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4774{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004775 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004776}
4777#endif
4778
Steven Rostedte32d8952008-12-04 00:26:41 -05004779static void clear_ftrace_swapper(void)
4780{
4781 struct task_struct *p;
4782 int cpu;
4783
4784 get_online_cpus();
4785 for_each_online_cpu(cpu) {
4786 p = idle_task(cpu);
4787 clear_tsk_trace_trace(p);
4788 }
4789 put_online_cpus();
4790}
4791
4792static void set_ftrace_swapper(void)
4793{
4794 struct task_struct *p;
4795 int cpu;
4796
4797 get_online_cpus();
4798 for_each_online_cpu(cpu) {
4799 p = idle_task(cpu);
4800 set_tsk_trace_trace(p);
4801 }
4802 put_online_cpus();
4803}
4804
4805static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004806{
4807 struct task_struct *p;
4808
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004809 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004810 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004811 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004812 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004813 rcu_read_unlock();
4814
Steven Rostedte32d8952008-12-04 00:26:41 -05004815 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004816}
4817
Steven Rostedte32d8952008-12-04 00:26:41 -05004818static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004819{
4820 struct task_struct *p;
4821
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004822 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004823 do_each_pid_task(pid, PIDTYPE_PID, p) {
4824 set_tsk_trace_trace(p);
4825 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004826 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004827}
4828
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004829static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004830{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004831 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004832 clear_ftrace_swapper();
4833 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004834 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004835}
4836
4837static void set_ftrace_pid_task(struct pid *pid)
4838{
4839 if (pid == ftrace_swapper_pid)
4840 set_ftrace_swapper();
4841 else
4842 set_ftrace_pid(pid);
4843}
4844
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004845static int ftrace_pid_add(int p)
4846{
4847 struct pid *pid;
4848 struct ftrace_pid *fpid;
4849 int ret = -EINVAL;
4850
4851 mutex_lock(&ftrace_lock);
4852
4853 if (!p)
4854 pid = ftrace_swapper_pid;
4855 else
4856 pid = find_get_pid(p);
4857
4858 if (!pid)
4859 goto out;
4860
4861 ret = 0;
4862
4863 list_for_each_entry(fpid, &ftrace_pids, list)
4864 if (fpid->pid == pid)
4865 goto out_put;
4866
4867 ret = -ENOMEM;
4868
4869 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4870 if (!fpid)
4871 goto out_put;
4872
4873 list_add(&fpid->list, &ftrace_pids);
4874 fpid->pid = pid;
4875
4876 set_ftrace_pid_task(pid);
4877
4878 ftrace_update_pid_func();
4879 ftrace_startup_enable(0);
4880
4881 mutex_unlock(&ftrace_lock);
4882 return 0;
4883
4884out_put:
4885 if (pid != ftrace_swapper_pid)
4886 put_pid(pid);
4887
4888out:
4889 mutex_unlock(&ftrace_lock);
4890 return ret;
4891}
4892
4893static void ftrace_pid_reset(void)
4894{
4895 struct ftrace_pid *fpid, *safe;
4896
4897 mutex_lock(&ftrace_lock);
4898 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4899 struct pid *pid = fpid->pid;
4900
4901 clear_ftrace_pid_task(pid);
4902
4903 list_del(&fpid->list);
4904 kfree(fpid);
4905 }
4906
4907 ftrace_update_pid_func();
4908 ftrace_startup_enable(0);
4909
4910 mutex_unlock(&ftrace_lock);
4911}
4912
4913static void *fpid_start(struct seq_file *m, loff_t *pos)
4914{
4915 mutex_lock(&ftrace_lock);
4916
4917 if (list_empty(&ftrace_pids) && (!*pos))
4918 return (void *) 1;
4919
4920 return seq_list_start(&ftrace_pids, *pos);
4921}
4922
4923static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4924{
4925 if (v == (void *)1)
4926 return NULL;
4927
4928 return seq_list_next(v, &ftrace_pids, pos);
4929}
4930
4931static void fpid_stop(struct seq_file *m, void *p)
4932{
4933 mutex_unlock(&ftrace_lock);
4934}
4935
4936static int fpid_show(struct seq_file *m, void *v)
4937{
4938 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4939
4940 if (v == (void *)1) {
4941 seq_printf(m, "no pid\n");
4942 return 0;
4943 }
4944
4945 if (fpid->pid == ftrace_swapper_pid)
4946 seq_printf(m, "swapper tasks\n");
4947 else
4948 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4949
4950 return 0;
4951}
4952
4953static const struct seq_operations ftrace_pid_sops = {
4954 .start = fpid_start,
4955 .next = fpid_next,
4956 .stop = fpid_stop,
4957 .show = fpid_show,
4958};
4959
4960static int
4961ftrace_pid_open(struct inode *inode, struct file *file)
4962{
4963 int ret = 0;
4964
4965 if ((file->f_mode & FMODE_WRITE) &&
4966 (file->f_flags & O_TRUNC))
4967 ftrace_pid_reset();
4968
4969 if (file->f_mode & FMODE_READ)
4970 ret = seq_open(file, &ftrace_pid_sops);
4971
4972 return ret;
4973}
4974
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004975static ssize_t
4976ftrace_pid_write(struct file *filp, const char __user *ubuf,
4977 size_t cnt, loff_t *ppos)
4978{
Ingo Molnar457dc922009-11-23 11:03:28 +01004979 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004980 long val;
4981 int ret;
4982
4983 if (cnt >= sizeof(buf))
4984 return -EINVAL;
4985
4986 if (copy_from_user(&buf, ubuf, cnt))
4987 return -EFAULT;
4988
4989 buf[cnt] = 0;
4990
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004991 /*
4992 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4993 * to clean the filter quietly.
4994 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004995 tmp = strstrip(buf);
4996 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004997 return 1;
4998
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004999 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005000 if (ret < 0)
5001 return ret;
5002
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005003 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005004
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005005 return ret ? ret : cnt;
5006}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005007
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005008static int
5009ftrace_pid_release(struct inode *inode, struct file *file)
5010{
5011 if (file->f_mode & FMODE_READ)
5012 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005013
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005014 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005015}
5016
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005017static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005018 .open = ftrace_pid_open,
5019 .write = ftrace_pid_write,
5020 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005021 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005022 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005023};
5024
5025static __init int ftrace_init_debugfs(void)
5026{
5027 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005028
5029 d_tracer = tracing_init_dentry();
5030 if (!d_tracer)
5031 return 0;
5032
5033 ftrace_init_dyn_debugfs(d_tracer);
5034
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005035 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5036 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005037
5038 ftrace_profile_debugfs(d_tracer);
5039
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005040 return 0;
5041}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005042fs_initcall(ftrace_init_debugfs);
5043
Steven Rostedt3d083392008-05-12 21:20:42 +02005044/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005045 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005046 *
5047 * This function should be used by panic code. It stops ftrace
5048 * but in a not so nice way. If you need to simply kill ftrace
5049 * from a non-atomic section, use ftrace_kill.
5050 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005051void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005052{
5053 ftrace_disabled = 1;
5054 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005055 clear_ftrace_function();
5056}
5057
5058/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005059 * Test if ftrace is dead or not.
5060 */
5061int ftrace_is_dead(void)
5062{
5063 return ftrace_disabled;
5064}
5065
5066/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005067 * register_ftrace_function - register a function for profiling
5068 * @ops - ops structure that holds the function for profiling.
5069 *
5070 * Register a function to be called by all functions in the
5071 * kernel.
5072 *
5073 * Note: @ops->func and all the functions it calls must be labeled
5074 * with "notrace", otherwise it will go into a
5075 * recursive loop.
5076 */
5077int register_ftrace_function(struct ftrace_ops *ops)
5078{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005079 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005080
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005081 ftrace_ops_init(ops);
5082
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005083 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005084
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005085 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005086
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005087 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005088
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005089 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005090}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005091EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005092
5093/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005094 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005095 * @ops - ops structure that holds the function to unregister
5096 *
5097 * Unregister a function that was added to be called by ftrace profiling.
5098 */
5099int unregister_ftrace_function(struct ftrace_ops *ops)
5100{
5101 int ret;
5102
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005103 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005104 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005105 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005106
5107 return ret;
5108}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005109EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005110
Ingo Molnare309b412008-05-12 21:20:51 +02005111int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005112ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005113 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005114 loff_t *ppos)
5115{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005116 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005117
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005118 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005119
Steven Rostedt45a4a232011-04-21 23:16:46 -04005120 if (unlikely(ftrace_disabled))
5121 goto out;
5122
5123 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005124
Li Zefana32c7762009-06-26 16:55:51 +08005125 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005126 goto out;
5127
Li Zefana32c7762009-06-26 16:55:51 +08005128 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005129
5130 if (ftrace_enabled) {
5131
5132 ftrace_startup_sysctl();
5133
5134 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005135 if (ftrace_ops_list != &ftrace_list_end)
5136 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005137
5138 } else {
5139 /* stopping ftrace calls (just send to ftrace_stub) */
5140 ftrace_trace_function = ftrace_stub;
5141
5142 ftrace_shutdown_sysctl();
5143 }
5144
5145 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005146 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005147 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005148}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005149
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005150#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005151
Steven Rostedt597af812009-04-03 15:24:12 -04005152static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005153
Steven Rostedte49dc192008-12-02 23:50:05 -05005154int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5155{
5156 return 0;
5157}
5158
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005159/* The callbacks that hook a function */
5160trace_func_graph_ret_t ftrace_graph_return =
5161 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005162trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005163static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005164
5165/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5166static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5167{
5168 int i;
5169 int ret = 0;
5170 unsigned long flags;
5171 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5172 struct task_struct *g, *t;
5173
5174 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5175 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5176 * sizeof(struct ftrace_ret_stack),
5177 GFP_KERNEL);
5178 if (!ret_stack_list[i]) {
5179 start = 0;
5180 end = i;
5181 ret = -ENOMEM;
5182 goto free;
5183 }
5184 }
5185
5186 read_lock_irqsave(&tasklist_lock, flags);
5187 do_each_thread(g, t) {
5188 if (start == end) {
5189 ret = -EAGAIN;
5190 goto unlock;
5191 }
5192
5193 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005194 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005195 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005196 t->curr_ret_stack = -1;
5197 /* Make sure the tasks see the -1 first: */
5198 smp_wmb();
5199 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005200 }
5201 } while_each_thread(g, t);
5202
5203unlock:
5204 read_unlock_irqrestore(&tasklist_lock, flags);
5205free:
5206 for (i = start; i < end; i++)
5207 kfree(ret_stack_list[i]);
5208 return ret;
5209}
5210
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005211static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005212ftrace_graph_probe_sched_switch(void *ignore,
5213 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005214{
5215 unsigned long long timestamp;
5216 int index;
5217
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005218 /*
5219 * Does the user want to count the time a function was asleep.
5220 * If so, do not update the time stamps.
5221 */
5222 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5223 return;
5224
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005225 timestamp = trace_clock_local();
5226
5227 prev->ftrace_timestamp = timestamp;
5228
5229 /* only process tasks that we timestamped */
5230 if (!next->ftrace_timestamp)
5231 return;
5232
5233 /*
5234 * Update all the counters in next to make up for the
5235 * time next was sleeping.
5236 */
5237 timestamp -= next->ftrace_timestamp;
5238
5239 for (index = next->curr_ret_stack; index >= 0; index--)
5240 next->ret_stack[index].calltime += timestamp;
5241}
5242
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005243/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005244static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005245{
5246 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005247 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005248
5249 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5250 sizeof(struct ftrace_ret_stack *),
5251 GFP_KERNEL);
5252
5253 if (!ret_stack_list)
5254 return -ENOMEM;
5255
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005256 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005257 for_each_online_cpu(cpu) {
5258 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005259 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005260 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005261
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005262 do {
5263 ret = alloc_retstack_tasklist(ret_stack_list);
5264 } while (ret == -EAGAIN);
5265
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005266 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005267 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005268 if (ret)
5269 pr_info("ftrace_graph: Couldn't activate tracepoint"
5270 " probe to kernel_sched_switch\n");
5271 }
5272
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005273 kfree(ret_stack_list);
5274 return ret;
5275}
5276
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005277/*
5278 * Hibernation protection.
5279 * The state of the current task is too much unstable during
5280 * suspend/restore to disk. We want to protect against that.
5281 */
5282static int
5283ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5284 void *unused)
5285{
5286 switch (state) {
5287 case PM_HIBERNATION_PREPARE:
5288 pause_graph_tracing();
5289 break;
5290
5291 case PM_POST_HIBERNATION:
5292 unpause_graph_tracing();
5293 break;
5294 }
5295 return NOTIFY_DONE;
5296}
5297
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005298static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5299{
5300 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5301 return 0;
5302 return __ftrace_graph_entry(trace);
5303}
5304
5305/*
5306 * The function graph tracer should only trace the functions defined
5307 * by set_ftrace_filter and set_ftrace_notrace. If another function
5308 * tracer ops is registered, the graph tracer requires testing the
5309 * function against the global ops, and not just trace any function
5310 * that any ftrace_ops registered.
5311 */
5312static void update_function_graph_func(void)
5313{
5314 if (ftrace_ops_list == &ftrace_list_end ||
5315 (ftrace_ops_list == &global_ops &&
5316 global_ops.next == &ftrace_list_end))
5317 ftrace_graph_entry = __ftrace_graph_entry;
5318 else
5319 ftrace_graph_entry = ftrace_graph_entry_test;
5320}
5321
Mathias Krause8275f692014-03-30 15:31:50 +02005322static struct notifier_block ftrace_suspend_notifier = {
5323 .notifier_call = ftrace_suspend_notifier_call,
5324};
5325
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005326int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5327 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005328{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005329 int ret = 0;
5330
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005331 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005332
Steven Rostedt05ce5812009-03-24 00:18:31 -04005333 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005334 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005335 ret = -EBUSY;
5336 goto out;
5337 }
5338
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005339 register_pm_notifier(&ftrace_suspend_notifier);
5340
Steven Rostedt597af812009-04-03 15:24:12 -04005341 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005342 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005343 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005344 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005345 goto out;
5346 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005347
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005348 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005349
5350 /*
5351 * Update the indirect function to the entryfunc, and the
5352 * function that gets called to the entry_test first. Then
5353 * call the update fgraph entry function to determine if
5354 * the entryfunc should be called directly or not.
5355 */
5356 __ftrace_graph_entry = entryfunc;
5357 ftrace_graph_entry = ftrace_graph_entry_test;
5358 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005359
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005360 /* Function graph doesn't use the .func field of global_ops */
5361 global_ops.flags |= FTRACE_OPS_FL_STUB;
5362
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005363#ifdef CONFIG_DYNAMIC_FTRACE
5364 /* Optimize function graph calling (if implemented by arch) */
Steven Rostedt (Red Hat)646d7042014-07-11 14:39:10 -04005365 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5366 global_ops.trampoline = FTRACE_GRAPH_TRAMP_ADDR;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005367#endif
5368
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005369 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005370
5371out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005372 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005373 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005374}
5375
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005376void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005377{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005378 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005379
Steven Rostedt597af812009-04-03 15:24:12 -04005380 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005381 goto out;
5382
Steven Rostedt597af812009-04-03 15:24:12 -04005383 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005384 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005385 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005386 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005387 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5388 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005389#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)646d7042014-07-11 14:39:10 -04005390 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5391 global_ops.trampoline = 0;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005392#endif
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005393 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005394 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005395
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005396 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005397 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005398}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005399
Steven Rostedt868baf02011-02-10 21:26:13 -05005400static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5401
5402static void
5403graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5404{
5405 atomic_set(&t->tracing_graph_pause, 0);
5406 atomic_set(&t->trace_overrun, 0);
5407 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005408 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005409 smp_wmb();
5410 t->ret_stack = ret_stack;
5411}
5412
5413/*
5414 * Allocate a return stack for the idle task. May be the first
5415 * time through, or it may be done by CPU hotplug online.
5416 */
5417void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5418{
5419 t->curr_ret_stack = -1;
5420 /*
5421 * The idle task has no parent, it either has its own
5422 * stack or no stack at all.
5423 */
5424 if (t->ret_stack)
5425 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5426
5427 if (ftrace_graph_active) {
5428 struct ftrace_ret_stack *ret_stack;
5429
5430 ret_stack = per_cpu(idle_ret_stack, cpu);
5431 if (!ret_stack) {
5432 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5433 * sizeof(struct ftrace_ret_stack),
5434 GFP_KERNEL);
5435 if (!ret_stack)
5436 return;
5437 per_cpu(idle_ret_stack, cpu) = ret_stack;
5438 }
5439 graph_init_task(t, ret_stack);
5440 }
5441}
5442
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005443/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005444void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005445{
Steven Rostedt84047e32009-06-02 16:51:55 -04005446 /* Make sure we do not use the parent ret_stack */
5447 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005448 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005449
Steven Rostedt597af812009-04-03 15:24:12 -04005450 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005451 struct ftrace_ret_stack *ret_stack;
5452
5453 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005454 * sizeof(struct ftrace_ret_stack),
5455 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005456 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005457 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005458 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005459 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005460}
5461
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005462void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005463{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005464 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5465
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005466 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005467 /* NULL must become visible to IRQs before we free it: */
5468 barrier();
5469
5470 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005471}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005472#endif