blob: eb11011b5292add880af7038800560aa29c5a674 [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 Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050021#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -050065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
Jiri Olsae2484912012-02-15 15:51:48 +010066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040068#define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040071#define ASSIGN_OPS_HASH(opsname, val) \
72 .func_hash = val, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090074#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040075#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040076#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090077#endif
78
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040079static struct ftrace_ops ftrace_list_end __read_mostly = {
80 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040081 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040082 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040083};
84
Steven Rostedt4eebcc82008-05-12 21:20:48 +020085/* ftrace_enabled is a method to turn ftrace on or off */
86int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020087static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020088
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040089/* Current function tracing op */
90struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050091/* What to set function_trace_op to */
92static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050093
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040094/* List for set_ftrace_pid's pids. */
95LIST_HEAD(ftrace_pids);
96struct ftrace_pid {
97 struct list_head list;
98 struct pid *pid;
99};
100
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400101static bool ftrace_pids_enabled(void)
102{
103 return !list_empty(&ftrace_pids);
104}
105
106static void ftrace_update_trampoline(struct ftrace_ops *ops);
107
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200108/*
109 * ftrace_disabled is set when an anomaly is discovered.
110 * ftrace_disabled is much stronger than ftrace_enabled.
111 */
112static int ftrace_disabled __read_mostly;
113
Steven Rostedt52baf112009-02-14 01:15:39 -0500114static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200115
Jiri Olsae2484912012-02-15 15:51:48 +0100116static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400117static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200118ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400119static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100120static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200121
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -0400122static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
123 struct ftrace_ops *op, struct pt_regs *regs);
124
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400125#if ARCH_SUPPORTS_FTRACE_OPS
126static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400127 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400128#else
129/* See comment below, where ftrace_ops_list_func is defined */
130static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
131#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
132#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400133
Steven Rostedt0a016402012-11-02 17:03:03 -0400134/*
135 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400136 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400137 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 * concurrent insertions into the ftrace_global_list.
140 *
141 * Silly Alpha and silly pointer-speculation compiler optimizations!
142 */
143#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400144 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400145 do
146
147/*
148 * Optimized for just a single item in the list (as that is the normal case).
149 */
150#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400151 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400152 unlikely((op) != &ftrace_list_end))
153
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900154static inline void ftrace_ops_init(struct ftrace_ops *ops)
155{
156#ifdef CONFIG_DYNAMIC_FTRACE
157 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400158 mutex_init(&ops->local_hash.regex_lock);
159 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900160 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
161 }
162#endif
163}
164
Steven Rostedtea701f12012-07-20 13:08:05 -0400165/**
166 * ftrace_nr_registered_ops - return number of ops registered
167 *
168 * Returns the number of ftrace_ops registered and tracing functions
169 */
170int ftrace_nr_registered_ops(void)
171{
172 struct ftrace_ops *ops;
173 int cnt = 0;
174
175 mutex_lock(&ftrace_lock);
176
177 for (ops = ftrace_ops_list;
178 ops != &ftrace_list_end; ops = ops->next)
179 cnt++;
180
181 mutex_unlock(&ftrace_lock);
182
183 return cnt;
184}
185
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400186static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400187 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500188{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500189 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500190 return;
191
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400192 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500193}
194
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200195/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200196 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200198 * This NULLs the ftrace function and in essence stops
199 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200200 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200201void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200202{
Steven Rostedt3d083392008-05-12 21:20:42 +0200203 ftrace_trace_function = ftrace_stub;
204}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200205
Jiri Olsae2484912012-02-15 15:51:48 +0100206static void control_ops_disable_all(struct ftrace_ops *ops)
207{
208 int cpu;
209
210 for_each_possible_cpu(cpu)
211 *per_cpu_ptr(ops->disabled, cpu) = 1;
212}
213
214static int control_ops_alloc(struct ftrace_ops *ops)
215{
216 int __percpu *disabled;
217
218 disabled = alloc_percpu(int);
219 if (!disabled)
220 return -ENOMEM;
221
222 ops->disabled = disabled;
223 control_ops_disable_all(ops);
224 return 0;
225}
226
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500227static void ftrace_sync(struct work_struct *work)
228{
229 /*
230 * This function is just a stub to implement a hard force
231 * of synchronize_sched(). This requires synchronizing
232 * tasks even in userspace and idle.
233 *
234 * Yes, function tracing is rude.
235 */
236}
237
238static void ftrace_sync_ipi(void *data)
239{
240 /* Probably not needed, but do it anyway */
241 smp_rmb();
242}
243
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500244#ifdef CONFIG_FUNCTION_GRAPH_TRACER
245static void update_function_graph_func(void);
246#else
247static inline void update_function_graph_func(void) { }
248#endif
249
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100250
251static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
252{
253 /*
254 * If this is a dynamic ops or we force list func,
255 * then it needs to call the list anyway.
256 */
257 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
258 return ftrace_ops_list_func;
259
260 return ftrace_ops_get_func(ops);
261}
262
Steven Rostedt2b499382011-05-03 22:49:52 -0400263static void update_ftrace_function(void)
264{
265 ftrace_func_t func;
266
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400267 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400268 * Prepare the ftrace_ops that the arch callback will use.
269 * If there's only one ftrace_ops registered, the ftrace_ops_list
270 * will point to the ops we want.
271 */
272 set_function_trace_op = ftrace_ops_list;
273
274 /* If there's no ftrace_ops registered, just call the stub function */
275 if (ftrace_ops_list == &ftrace_list_end) {
276 func = ftrace_stub;
277
278 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400279 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400280 * recursion safe and not dynamic and the arch supports passing ops,
281 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400282 */
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400283 } else if (ftrace_ops_list->next == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100284 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400285
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400286 } else {
287 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500288 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400289 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400290 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400291
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400292 update_function_graph_func();
293
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500294 /* If there's no change, then do nothing more here */
295 if (ftrace_trace_function == func)
296 return;
297
298 /*
299 * If we are using the list function, it doesn't care
300 * about the function_trace_ops.
301 */
302 if (func == ftrace_ops_list_func) {
303 ftrace_trace_function = func;
304 /*
305 * Don't even bother setting function_trace_ops,
306 * it would be racy to do so anyway.
307 */
308 return;
309 }
310
311#ifndef CONFIG_DYNAMIC_FTRACE
312 /*
313 * For static tracing, we need to be a bit more careful.
314 * The function change takes affect immediately. Thus,
315 * we need to coorditate the setting of the function_trace_ops
316 * with the setting of the ftrace_trace_function.
317 *
318 * Set the function to the list ops, which will call the
319 * function we want, albeit indirectly, but it handles the
320 * ftrace_ops and doesn't depend on function_trace_op.
321 */
322 ftrace_trace_function = ftrace_ops_list_func;
323 /*
324 * Make sure all CPUs see this. Yes this is slow, but static
325 * tracing is slow and nasty to have enabled.
326 */
327 schedule_on_each_cpu(ftrace_sync);
328 /* Now all cpus are using the list ops. */
329 function_trace_op = set_function_trace_op;
330 /* Make sure the function_trace_op is visible on all CPUs */
331 smp_wmb();
332 /* Nasty way to force a rmb on all cpus */
333 smp_call_function(ftrace_sync_ipi, NULL, 1);
334 /* OK, we are all set to update the ftrace_trace_function now! */
335#endif /* !CONFIG_DYNAMIC_FTRACE */
336
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400337 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400338}
339
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800340int using_ftrace_ops_list_func(void)
341{
342 return ftrace_trace_function == ftrace_ops_list_func;
343}
344
Steven Rostedt2b499382011-05-03 22:49:52 -0400345static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200346{
Steven Rostedt2b499382011-05-03 22:49:52 -0400347 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200348 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400349 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200350 * CPU might be walking that list. We need to make sure
351 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400352 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200353 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400354 rcu_assign_pointer(*list, ops);
355}
Steven Rostedt3d083392008-05-12 21:20:42 +0200356
Steven Rostedt2b499382011-05-03 22:49:52 -0400357static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
358{
359 struct ftrace_ops **p;
360
361 /*
362 * If we are removing the last function, then simply point
363 * to the ftrace_stub.
364 */
365 if (*list == ops && ops->next == &ftrace_list_end) {
366 *list = &ftrace_list_end;
367 return 0;
368 }
369
370 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
371 if (*p == ops)
372 break;
373
374 if (*p != ops)
375 return -1;
376
377 *p = (*p)->next;
378 return 0;
379}
380
Jiri Olsae2484912012-02-15 15:51:48 +0100381static void add_ftrace_list_ops(struct ftrace_ops **list,
382 struct ftrace_ops *main_ops,
383 struct ftrace_ops *ops)
384{
385 int first = *list == &ftrace_list_end;
386 add_ftrace_ops(list, ops);
387 if (first)
388 add_ftrace_ops(&ftrace_ops_list, main_ops);
389}
390
391static int remove_ftrace_list_ops(struct ftrace_ops **list,
392 struct ftrace_ops *main_ops,
393 struct ftrace_ops *ops)
394{
395 int ret = remove_ftrace_ops(list, ops);
396 if (!ret && *list == &ftrace_list_end)
397 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
398 return ret;
399}
400
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400401static void ftrace_update_trampoline(struct ftrace_ops *ops);
402
Steven Rostedt2b499382011-05-03 22:49:52 -0400403static int __register_ftrace_function(struct ftrace_ops *ops)
404{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500405 if (ops->flags & FTRACE_OPS_FL_DELETED)
406 return -EINVAL;
407
Steven Rostedtb8489142011-05-04 09:27:52 -0400408 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
409 return -EBUSY;
410
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900411#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400412 /*
413 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
414 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
415 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
416 */
417 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
418 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
419 return -EINVAL;
420
421 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
422 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
423#endif
424
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400425 if (!core_kernel_data((unsigned long)ops))
426 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
427
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500428 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100429 if (control_ops_alloc(ops))
430 return -ENOMEM;
431 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedt (Red Hat)fe578ba2014-11-13 19:40:56 -0500432 /* The control_ops needs the trampoline update */
433 ops = &control_ops;
Steven Rostedtb8489142011-05-04 09:27:52 -0400434 } else
435 add_ftrace_ops(&ftrace_ops_list, ops);
436
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400437 /* Always save the function, and reset at unregistering */
438 ops->saved_func = ops->func;
439
440 if (ops->flags & FTRACE_OPS_FL_PID && ftrace_pids_enabled())
441 ops->func = ftrace_pid_func;
442
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400443 ftrace_update_trampoline(ops);
444
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400445 if (ftrace_enabled)
446 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200447
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200448 return 0;
449}
450
Ingo Molnare309b412008-05-12 21:20:51 +0200451static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200452{
Steven Rostedt2b499382011-05-03 22:49:52 -0400453 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200454
Steven Rostedtb8489142011-05-04 09:27:52 -0400455 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
456 return -EBUSY;
457
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500458 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100459 ret = remove_ftrace_list_ops(&ftrace_control_list,
460 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400461 } else
462 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
463
Steven Rostedt2b499382011-05-03 22:49:52 -0400464 if (ret < 0)
465 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400466
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400467 if (ftrace_enabled)
468 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200469
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400470 ops->func = ops->saved_func;
471
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500472 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200473}
474
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500475static void ftrace_update_pid_func(void)
476{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400477 bool enabled = ftrace_pids_enabled();
478 struct ftrace_ops *op;
479
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400480 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500481 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900482 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500483
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400484 do_for_each_ftrace_op(op, ftrace_ops_list) {
485 if (op->flags & FTRACE_OPS_FL_PID) {
486 op->func = enabled ? ftrace_pid_func :
487 op->saved_func;
488 ftrace_update_trampoline(op);
489 }
490 } while_for_each_ftrace_op(op);
491
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400492 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500493}
494
Steven Rostedt493762f2009-03-23 17:12:36 -0400495#ifdef CONFIG_FUNCTION_PROFILER
496struct ftrace_profile {
497 struct hlist_node node;
498 unsigned long ip;
499 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400500#ifdef CONFIG_FUNCTION_GRAPH_TRACER
501 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400502 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400503#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400504};
505
506struct ftrace_profile_page {
507 struct ftrace_profile_page *next;
508 unsigned long index;
509 struct ftrace_profile records[];
510};
511
Steven Rostedtcafb1682009-03-24 20:50:39 -0400512struct ftrace_profile_stat {
513 atomic_t disabled;
514 struct hlist_head *hash;
515 struct ftrace_profile_page *pages;
516 struct ftrace_profile_page *start;
517 struct tracer_stat stat;
518};
519
Steven Rostedt493762f2009-03-23 17:12:36 -0400520#define PROFILE_RECORDS_SIZE \
521 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
522
523#define PROFILES_PER_PAGE \
524 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
525
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400526static int ftrace_profile_enabled __read_mostly;
527
528/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400529static DEFINE_MUTEX(ftrace_profile_lock);
530
Steven Rostedtcafb1682009-03-24 20:50:39 -0400531static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400532
Namhyung Kim20079eb2013-04-10 08:55:50 +0900533#define FTRACE_PROFILE_HASH_BITS 10
534#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400535
Steven Rostedt493762f2009-03-23 17:12:36 -0400536static void *
537function_stat_next(void *v, int idx)
538{
539 struct ftrace_profile *rec = v;
540 struct ftrace_profile_page *pg;
541
542 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
543
544 again:
Li Zefan0296e422009-06-26 11:15:37 +0800545 if (idx != 0)
546 rec++;
547
Steven Rostedt493762f2009-03-23 17:12:36 -0400548 if ((void *)rec >= (void *)&pg->records[pg->index]) {
549 pg = pg->next;
550 if (!pg)
551 return NULL;
552 rec = &pg->records[0];
553 if (!rec->counter)
554 goto again;
555 }
556
557 return rec;
558}
559
560static void *function_stat_start(struct tracer_stat *trace)
561{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400562 struct ftrace_profile_stat *stat =
563 container_of(trace, struct ftrace_profile_stat, stat);
564
565 if (!stat || !stat->start)
566 return NULL;
567
568 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400569}
570
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#ifdef CONFIG_FUNCTION_GRAPH_TRACER
572/* function graph compares on total time */
573static int function_stat_cmp(void *p1, void *p2)
574{
575 struct ftrace_profile *a = p1;
576 struct ftrace_profile *b = p2;
577
578 if (a->time < b->time)
579 return -1;
580 if (a->time > b->time)
581 return 1;
582 else
583 return 0;
584}
585#else
586/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400587static int function_stat_cmp(void *p1, void *p2)
588{
589 struct ftrace_profile *a = p1;
590 struct ftrace_profile *b = p2;
591
592 if (a->counter < b->counter)
593 return -1;
594 if (a->counter > b->counter)
595 return 1;
596 else
597 return 0;
598}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400599#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400600
601static int function_stat_headers(struct seq_file *m)
602{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400603#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100604 seq_puts(m, " Function "
605 "Hit Time Avg s^2\n"
606 " -------- "
607 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400608#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100609 seq_puts(m, " Function Hit\n"
610 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400611#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400612 return 0;
613}
614
615static int function_stat_show(struct seq_file *m, void *v)
616{
617 struct ftrace_profile *rec = v;
618 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800619 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400620#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400621 static struct trace_seq s;
622 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400623 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400624#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800625 mutex_lock(&ftrace_profile_lock);
626
627 /* we raced with function_profile_reset() */
628 if (unlikely(rec->counter == 0)) {
629 ret = -EBUSY;
630 goto out;
631 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400632
633 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400634 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400635
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400636#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100637 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400638 avg = rec->time;
639 do_div(avg, rec->counter);
640
Chase Douglase330b3b2010-04-26 14:02:05 -0400641 /* Sample standard deviation (s^2) */
642 if (rec->counter <= 1)
643 stddev = 0;
644 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200645 /*
646 * Apply Welford's method:
647 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
648 */
649 stddev = rec->counter * rec->time_squared -
650 rec->time * rec->time;
651
Chase Douglase330b3b2010-04-26 14:02:05 -0400652 /*
653 * Divide only 1000 for ns^2 -> us^2 conversion.
654 * trace_print_graph_duration will divide 1000 again.
655 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200656 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400657 }
658
Steven Rostedt34886c82009-03-25 21:00:47 -0400659 trace_seq_init(&s);
660 trace_print_graph_duration(rec->time, &s);
661 trace_seq_puts(&s, " ");
662 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400663 trace_seq_puts(&s, " ");
664 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400665 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400666#endif
667 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800668out:
669 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400670
Li Zefan3aaba202010-08-23 16:50:12 +0800671 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400672}
673
Steven Rostedtcafb1682009-03-24 20:50:39 -0400674static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400675{
676 struct ftrace_profile_page *pg;
677
Steven Rostedtcafb1682009-03-24 20:50:39 -0400678 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400679
680 while (pg) {
681 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
682 pg->index = 0;
683 pg = pg->next;
684 }
685
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400687 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
688}
689
Steven Rostedtcafb1682009-03-24 20:50:39 -0400690int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400691{
692 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400693 int functions;
694 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400695 int i;
696
697 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400698 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400699 return 0;
700
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
702 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400703 return -ENOMEM;
704
Steven Rostedt318e0a72009-03-25 20:06:34 -0400705#ifdef CONFIG_DYNAMIC_FTRACE
706 functions = ftrace_update_tot_cnt;
707#else
708 /*
709 * We do not know the number of functions that exist because
710 * dynamic tracing is what counts them. With past experience
711 * we have around 20K functions. That should be more than enough.
712 * It is highly unlikely we will execute every function in
713 * the kernel.
714 */
715 functions = 20000;
716#endif
717
Steven Rostedtcafb1682009-03-24 20:50:39 -0400718 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400719
Steven Rostedt318e0a72009-03-25 20:06:34 -0400720 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
721
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900722 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400723 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400725 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400726 pg = pg->next;
727 }
728
729 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400730
731 out_free:
732 pg = stat->start;
733 while (pg) {
734 unsigned long tmp = (unsigned long)pg;
735
736 pg = pg->next;
737 free_page(tmp);
738 }
739
Steven Rostedt318e0a72009-03-25 20:06:34 -0400740 stat->pages = NULL;
741 stat->start = NULL;
742
743 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400744}
745
Steven Rostedtcafb1682009-03-24 20:50:39 -0400746static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400747{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 int size;
750
Steven Rostedtcafb1682009-03-24 20:50:39 -0400751 stat = &per_cpu(ftrace_profile_stats, cpu);
752
753 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400754 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400755 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400756 return 0;
757 }
758
759 /*
760 * We are profiling all functions, but usually only a few thousand
761 * functions are hit. We'll make a hash of 1024 items.
762 */
763 size = FTRACE_PROFILE_HASH_SIZE;
764
Steven Rostedtcafb1682009-03-24 20:50:39 -0400765 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400766
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400768 return -ENOMEM;
769
Steven Rostedt318e0a72009-03-25 20:06:34 -0400770 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771 if (ftrace_profile_pages_init(stat) < 0) {
772 kfree(stat->hash);
773 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400774 return -ENOMEM;
775 }
776
777 return 0;
778}
779
Steven Rostedtcafb1682009-03-24 20:50:39 -0400780static int ftrace_profile_init(void)
781{
782 int cpu;
783 int ret = 0;
784
Miao Xiec4602c12013-12-16 15:20:01 +0800785 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400786 ret = ftrace_profile_init_cpu(cpu);
787 if (ret)
788 break;
789 }
790
791 return ret;
792}
793
Steven Rostedt493762f2009-03-23 17:12:36 -0400794/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400795static struct ftrace_profile *
796ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400797{
798 struct ftrace_profile *rec;
799 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400800 unsigned long key;
801
Namhyung Kim20079eb2013-04-10 08:55:50 +0900802 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400803 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400804
805 if (hlist_empty(hhd))
806 return NULL;
807
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400808 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400809 if (rec->ip == ip)
810 return rec;
811 }
812
813 return NULL;
814}
815
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816static void ftrace_add_profile(struct ftrace_profile_stat *stat,
817 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400818{
819 unsigned long key;
820
Namhyung Kim20079eb2013-04-10 08:55:50 +0900821 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400823}
824
Steven Rostedt318e0a72009-03-25 20:06:34 -0400825/*
826 * The memory is already allocated, this simply finds a new record to use.
827 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400828static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400829ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400830{
831 struct ftrace_profile *rec = NULL;
832
Steven Rostedt318e0a72009-03-25 20:06:34 -0400833 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400834 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 goto out;
836
Steven Rostedt493762f2009-03-23 17:12:36 -0400837 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400838 * Try to find the function again since an NMI
839 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400840 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400841 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400842 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400843 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400844
Steven Rostedtcafb1682009-03-24 20:50:39 -0400845 if (stat->pages->index == PROFILES_PER_PAGE) {
846 if (!stat->pages->next)
847 goto out;
848 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400849 }
850
Steven Rostedtcafb1682009-03-24 20:50:39 -0400851 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400852 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400853 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400854
Steven Rostedt493762f2009-03-23 17:12:36 -0400855 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400856 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400857
858 return rec;
859}
860
Steven Rostedt493762f2009-03-23 17:12:36 -0400861static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400862function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400863 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400864{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400865 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400866 struct ftrace_profile *rec;
867 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400868
869 if (!ftrace_profile_enabled)
870 return;
871
Steven Rostedt493762f2009-03-23 17:12:36 -0400872 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400873
Christoph Lameterbdffd892014-04-29 14:17:40 -0500874 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400875 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400876 goto out;
877
878 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400879 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400880 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400881 if (!rec)
882 goto out;
883 }
884
885 rec->counter++;
886 out:
887 local_irq_restore(flags);
888}
889
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400890#ifdef CONFIG_FUNCTION_GRAPH_TRACER
891static int profile_graph_entry(struct ftrace_graph_ent *trace)
892{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400893 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400894 return 1;
895}
896
897static void profile_graph_return(struct ftrace_graph_ret *trace)
898{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400899 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400900 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400901 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400902 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400903
904 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500905 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400906 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400907 goto out;
908
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400909 /* If the calltime was zero'd ignore it */
910 if (!trace->calltime)
911 goto out;
912
Steven Rostedta2a16d62009-03-24 23:17:58 -0400913 calltime = trace->rettime - trace->calltime;
914
915 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
916 int index;
917
918 index = trace->depth;
919
920 /* Append this call time to the parent time to subtract */
921 if (index)
922 current->ret_stack[index - 1].subtime += calltime;
923
924 if (current->ret_stack[index].subtime < calltime)
925 calltime -= current->ret_stack[index].subtime;
926 else
927 calltime = 0;
928 }
929
Steven Rostedtcafb1682009-03-24 20:50:39 -0400930 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400931 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400932 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400933 rec->time_squared += calltime * calltime;
934 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400935
Steven Rostedtcafb1682009-03-24 20:50:39 -0400936 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400937 local_irq_restore(flags);
938}
939
940static int register_ftrace_profiler(void)
941{
942 return register_ftrace_graph(&profile_graph_return,
943 &profile_graph_entry);
944}
945
946static void unregister_ftrace_profiler(void)
947{
948 unregister_ftrace_graph();
949}
950#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100951static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400952 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900953 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400954 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400955};
956
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400957static int register_ftrace_profiler(void)
958{
959 return register_ftrace_function(&ftrace_profile_ops);
960}
961
962static void unregister_ftrace_profiler(void)
963{
964 unregister_ftrace_function(&ftrace_profile_ops);
965}
966#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
967
Steven Rostedt493762f2009-03-23 17:12:36 -0400968static ssize_t
969ftrace_profile_write(struct file *filp, const char __user *ubuf,
970 size_t cnt, loff_t *ppos)
971{
972 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400973 int ret;
974
Peter Huewe22fe9b52011-06-07 21:58:27 +0200975 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
976 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400977 return ret;
978
979 val = !!val;
980
981 mutex_lock(&ftrace_profile_lock);
982 if (ftrace_profile_enabled ^ val) {
983 if (val) {
984 ret = ftrace_profile_init();
985 if (ret < 0) {
986 cnt = ret;
987 goto out;
988 }
989
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400990 ret = register_ftrace_profiler();
991 if (ret < 0) {
992 cnt = ret;
993 goto out;
994 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400995 ftrace_profile_enabled = 1;
996 } else {
997 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400998 /*
999 * unregister_ftrace_profiler calls stop_machine
1000 * so this acts like an synchronize_sched.
1001 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001002 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001003 }
1004 }
1005 out:
1006 mutex_unlock(&ftrace_profile_lock);
1007
Jiri Olsacf8517c2009-10-23 19:36:16 -04001008 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001009
1010 return cnt;
1011}
1012
1013static ssize_t
1014ftrace_profile_read(struct file *filp, char __user *ubuf,
1015 size_t cnt, loff_t *ppos)
1016{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001017 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001018 int r;
1019
1020 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1021 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1022}
1023
1024static const struct file_operations ftrace_profile_fops = {
1025 .open = tracing_open_generic,
1026 .read = ftrace_profile_read,
1027 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001028 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001029};
1030
Steven Rostedtcafb1682009-03-24 20:50:39 -04001031/* used to initialize the real stat files */
1032static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001033 .name = "functions",
1034 .stat_start = function_stat_start,
1035 .stat_next = function_stat_next,
1036 .stat_cmp = function_stat_cmp,
1037 .stat_headers = function_stat_headers,
1038 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001039};
1040
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001041static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001042{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001043 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001044 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001045 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001046 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001047 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001048
Steven Rostedtcafb1682009-03-24 20:50:39 -04001049 for_each_possible_cpu(cpu) {
1050 stat = &per_cpu(ftrace_profile_stats, cpu);
1051
1052 /* allocate enough for function name + cpu number */
1053 name = kmalloc(32, GFP_KERNEL);
1054 if (!name) {
1055 /*
1056 * The files created are permanent, if something happens
1057 * we still do not free memory.
1058 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001059 WARN(1,
1060 "Could not allocate stat file for cpu %d\n",
1061 cpu);
1062 return;
1063 }
1064 stat->stat = function_stats;
1065 snprintf(name, 32, "function%d", cpu);
1066 stat->stat.name = name;
1067 ret = register_stat_tracer(&stat->stat);
1068 if (ret) {
1069 WARN(1,
1070 "Could not register function stat for cpu %d\n",
1071 cpu);
1072 kfree(name);
1073 return;
1074 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001075 }
1076
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001077 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001078 d_tracer, NULL, &ftrace_profile_fops);
1079 if (!entry)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001080 pr_warning("Could not create tracefs "
Steven Rostedt493762f2009-03-23 17:12:36 -04001081 "'function_profile_enabled' entry\n");
1082}
1083
1084#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001085static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001086{
1087}
1088#endif /* CONFIG_FUNCTION_PROFILER */
1089
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001090static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1091
Pratyush Anand1619dc32015-03-06 23:58:06 +05301092#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1093static int ftrace_graph_active;
1094#else
1095# define ftrace_graph_active 0
1096#endif
1097
Steven Rostedt3d083392008-05-12 21:20:42 +02001098#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001099
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001100static struct ftrace_ops *removed_ops;
1101
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001102/*
1103 * Set when doing a global update, like enabling all recs or disabling them.
1104 * It is not set when just updating a single ftrace_ops.
1105 */
1106static bool update_all_ops;
1107
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001108#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001109# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001110#endif
1111
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001112static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1113
Steven Rostedtb6887d72009-02-17 12:32:04 -05001114struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001115 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001116 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001117 unsigned long flags;
1118 unsigned long ip;
1119 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001120 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001121};
1122
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123struct ftrace_func_entry {
1124 struct hlist_node hlist;
1125 unsigned long ip;
1126};
1127
1128struct ftrace_hash {
1129 unsigned long size_bits;
1130 struct hlist_head *buckets;
1131 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001132 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001133};
1134
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001135/*
1136 * We make these constant because no one should touch them,
1137 * but they are used as the default "empty hash", to avoid allocating
1138 * it all the time. These are in a read only section such that if
1139 * anyone does try to modify it, it will cause an exception.
1140 */
1141static const struct hlist_head empty_buckets[1];
1142static const struct ftrace_hash empty_hash = {
1143 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001144};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001145#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001146
Steven Rostedt2b499382011-05-03 22:49:52 -04001147static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001148 .func = ftrace_stub,
1149 .local_hash.notrace_hash = EMPTY_HASH,
1150 .local_hash.filter_hash = EMPTY_HASH,
1151 INIT_OPS_HASH(global_ops)
1152 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001153 FTRACE_OPS_FL_INITIALIZED |
1154 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001155};
1156
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001157/*
1158 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001159 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001160 * not return true for either core_kernel_text() or
1161 * is_module_text_address().
1162 */
1163bool is_ftrace_trampoline(unsigned long addr)
1164{
1165 struct ftrace_ops *op;
1166 bool ret = false;
1167
1168 /*
1169 * Some of the ops may be dynamically allocated,
1170 * they are freed after a synchronize_sched().
1171 */
1172 preempt_disable_notrace();
1173
1174 do_for_each_ftrace_op(op, ftrace_ops_list) {
1175 /*
1176 * This is to check for dynamically allocated trampolines.
1177 * Trampolines that are in kernel text will have
1178 * core_kernel_text() return true.
1179 */
1180 if (op->trampoline && op->trampoline_size)
1181 if (addr >= op->trampoline &&
1182 addr < op->trampoline + op->trampoline_size) {
1183 ret = true;
1184 goto out;
1185 }
1186 } while_for_each_ftrace_op(op);
1187
1188 out:
1189 preempt_enable_notrace();
1190
1191 return ret;
1192}
1193
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001194struct ftrace_page {
1195 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001196 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001197 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001198 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001199};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001200
Steven Rostedta7900872011-12-16 16:23:44 -05001201#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1202#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001203
1204/* estimate from running different kernels */
1205#define NR_TO_INIT 10000
1206
1207static struct ftrace_page *ftrace_pages_start;
1208static struct ftrace_page *ftrace_pages;
1209
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001210static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001211{
1212 return !hash || !hash->count;
1213}
1214
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001215static struct ftrace_func_entry *
1216ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1217{
1218 unsigned long key;
1219 struct ftrace_func_entry *entry;
1220 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001221
Steven Rostedt06a51d92011-12-19 19:07:36 -05001222 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001223 return NULL;
1224
1225 if (hash->size_bits > 0)
1226 key = hash_long(ip, hash->size_bits);
1227 else
1228 key = 0;
1229
1230 hhd = &hash->buckets[key];
1231
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001232 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001233 if (entry->ip == ip)
1234 return entry;
1235 }
1236 return NULL;
1237}
1238
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001239static void __add_hash_entry(struct ftrace_hash *hash,
1240 struct ftrace_func_entry *entry)
1241{
1242 struct hlist_head *hhd;
1243 unsigned long key;
1244
1245 if (hash->size_bits)
1246 key = hash_long(entry->ip, hash->size_bits);
1247 else
1248 key = 0;
1249
1250 hhd = &hash->buckets[key];
1251 hlist_add_head(&entry->hlist, hhd);
1252 hash->count++;
1253}
1254
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001255static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1256{
1257 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001258
1259 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1260 if (!entry)
1261 return -ENOMEM;
1262
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001263 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001264 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001265
1266 return 0;
1267}
1268
1269static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001270free_hash_entry(struct ftrace_hash *hash,
1271 struct ftrace_func_entry *entry)
1272{
1273 hlist_del(&entry->hlist);
1274 kfree(entry);
1275 hash->count--;
1276}
1277
1278static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001279remove_hash_entry(struct ftrace_hash *hash,
1280 struct ftrace_func_entry *entry)
1281{
1282 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001283 hash->count--;
1284}
1285
1286static void ftrace_hash_clear(struct ftrace_hash *hash)
1287{
1288 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001289 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001290 struct ftrace_func_entry *entry;
1291 int size = 1 << hash->size_bits;
1292 int i;
1293
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001294 if (!hash->count)
1295 return;
1296
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001297 for (i = 0; i < size; i++) {
1298 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001299 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001300 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001301 }
1302 FTRACE_WARN_ON(hash->count);
1303}
1304
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001305static void free_ftrace_hash(struct ftrace_hash *hash)
1306{
1307 if (!hash || hash == EMPTY_HASH)
1308 return;
1309 ftrace_hash_clear(hash);
1310 kfree(hash->buckets);
1311 kfree(hash);
1312}
1313
Steven Rostedt07fd5512011-05-05 18:03:47 -04001314static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1315{
1316 struct ftrace_hash *hash;
1317
1318 hash = container_of(rcu, struct ftrace_hash, rcu);
1319 free_ftrace_hash(hash);
1320}
1321
1322static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1323{
1324 if (!hash || hash == EMPTY_HASH)
1325 return;
1326 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1327}
1328
Jiri Olsa5500fa52012-02-15 15:51:54 +01001329void ftrace_free_filter(struct ftrace_ops *ops)
1330{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001331 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001332 free_ftrace_hash(ops->func_hash->filter_hash);
1333 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001334}
1335
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001336static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1337{
1338 struct ftrace_hash *hash;
1339 int size;
1340
1341 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1342 if (!hash)
1343 return NULL;
1344
1345 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001346 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001347
1348 if (!hash->buckets) {
1349 kfree(hash);
1350 return NULL;
1351 }
1352
1353 hash->size_bits = size_bits;
1354
1355 return hash;
1356}
1357
1358static struct ftrace_hash *
1359alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1360{
1361 struct ftrace_func_entry *entry;
1362 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001363 int size;
1364 int ret;
1365 int i;
1366
1367 new_hash = alloc_ftrace_hash(size_bits);
1368 if (!new_hash)
1369 return NULL;
1370
1371 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001372 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001373 return new_hash;
1374
1375 size = 1 << hash->size_bits;
1376 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001377 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001378 ret = add_hash_entry(new_hash, entry->ip);
1379 if (ret < 0)
1380 goto free_hash;
1381 }
1382 }
1383
1384 FTRACE_WARN_ON(new_hash->count != hash->count);
1385
1386 return new_hash;
1387
1388 free_hash:
1389 free_ftrace_hash(new_hash);
1390 return NULL;
1391}
1392
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001393static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001394ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001395static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001396ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001397
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001398static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1399 struct ftrace_hash *new_hash);
1400
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001401static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001402ftrace_hash_move(struct ftrace_ops *ops, int enable,
1403 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001404{
1405 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001406 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001407 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001408 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001409 int size = src->count;
1410 int bits = 0;
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001411 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001412 int i;
1413
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001414 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1415 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1416 return -EINVAL;
1417
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001418 /*
1419 * If the new source is empty, just free dst and assign it
1420 * the empty_hash.
1421 */
1422 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001423 new_hash = EMPTY_HASH;
1424 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001425 }
1426
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001427 /*
1428 * Make the hash size about 1/2 the # found
1429 */
1430 for (size /= 2; size; size >>= 1)
1431 bits++;
1432
1433 /* Don't allocate too much */
1434 if (bits > FTRACE_HASH_MAX_BITS)
1435 bits = FTRACE_HASH_MAX_BITS;
1436
Steven Rostedt07fd5512011-05-05 18:03:47 -04001437 new_hash = alloc_ftrace_hash(bits);
1438 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001439 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001440
1441 size = 1 << src->size_bits;
1442 for (i = 0; i < size; i++) {
1443 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001444 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001445 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001446 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001447 }
1448 }
1449
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001450update:
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001451 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1452 if (enable) {
1453 /* IPMODIFY should be updated only when filter_hash updating */
1454 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1455 if (ret < 0) {
1456 free_ftrace_hash(new_hash);
1457 return ret;
1458 }
1459 }
1460
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001461 /*
1462 * Remove the current set, update the hash and add
1463 * them back.
1464 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001465 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001466
Steven Rostedt07fd5512011-05-05 18:03:47 -04001467 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001468
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001469 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001470
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001471 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001472}
1473
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001474static bool hash_contains_ip(unsigned long ip,
1475 struct ftrace_ops_hash *hash)
1476{
1477 /*
1478 * The function record is a match if it exists in the filter
1479 * hash and not in the notrace hash. Note, an emty hash is
1480 * considered a match for the filter hash, but an empty
1481 * notrace hash is considered not in the notrace hash.
1482 */
1483 return (ftrace_hash_empty(hash->filter_hash) ||
1484 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1485 (ftrace_hash_empty(hash->notrace_hash) ||
1486 !ftrace_lookup_ip(hash->notrace_hash, ip));
1487}
1488
Steven Rostedt265c8312009-02-13 12:43:56 -05001489/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001490 * Test the hashes for this ops to see if we want to call
1491 * the ops->func or not.
1492 *
1493 * It's a match if the ip is in the ops->filter_hash or
1494 * the filter_hash does not exist or is empty,
1495 * AND
1496 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001497 *
1498 * This needs to be called with preemption disabled as
1499 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001500 */
1501static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001502ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001503{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001504 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001505 int ret;
1506
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001507#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1508 /*
1509 * There's a small race when adding ops that the ftrace handler
1510 * that wants regs, may be called without them. We can not
1511 * allow that handler to be called if regs is NULL.
1512 */
1513 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1514 return 0;
1515#endif
1516
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001517 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1518 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001519
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001520 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001521 ret = 1;
1522 else
1523 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001524
1525 return ret;
1526}
1527
1528/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001529 * This is a double for. Do not use 'break' to break out of the loop,
1530 * you must use a goto.
1531 */
1532#define do_for_each_ftrace_rec(pg, rec) \
1533 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1534 int _____i; \
1535 for (_____i = 0; _____i < pg->index; _____i++) { \
1536 rec = &pg->records[_____i];
1537
1538#define while_for_each_ftrace_rec() \
1539 } \
1540 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301541
Steven Rostedt5855fea2011-12-16 19:27:42 -05001542
1543static int ftrace_cmp_recs(const void *a, const void *b)
1544{
Steven Rostedta650e022012-04-25 13:48:13 -04001545 const struct dyn_ftrace *key = a;
1546 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001547
Steven Rostedta650e022012-04-25 13:48:13 -04001548 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001549 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001550 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1551 return 1;
1552 return 0;
1553}
1554
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001555static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001556{
1557 struct ftrace_page *pg;
1558 struct dyn_ftrace *rec;
1559 struct dyn_ftrace key;
1560
1561 key.ip = start;
1562 key.flags = end; /* overload flags, as it is unsigned long */
1563
1564 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1565 if (end < pg->records[0].ip ||
1566 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1567 continue;
1568 rec = bsearch(&key, pg->records, pg->index,
1569 sizeof(struct dyn_ftrace),
1570 ftrace_cmp_recs);
1571 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001572 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001573 }
1574
Steven Rostedt5855fea2011-12-16 19:27:42 -05001575 return 0;
1576}
1577
Steven Rostedtc88fd862011-08-16 09:53:39 -04001578/**
1579 * ftrace_location - return true if the ip giving is a traced location
1580 * @ip: the instruction pointer to check
1581 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001582 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001583 * That is, the instruction that is either a NOP or call to
1584 * the function tracer. It checks the ftrace internal tables to
1585 * determine if the address belongs or not.
1586 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001587unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001588{
Steven Rostedta650e022012-04-25 13:48:13 -04001589 return ftrace_location_range(ip, ip);
1590}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001591
Steven Rostedta650e022012-04-25 13:48:13 -04001592/**
1593 * ftrace_text_reserved - return true if range contains an ftrace location
1594 * @start: start of range to search
1595 * @end: end of range to search (inclusive). @end points to the last byte to check.
1596 *
1597 * Returns 1 if @start and @end contains a ftrace location.
1598 * That is, the instruction that is either a NOP or call to
1599 * the function tracer. It checks the ftrace internal tables to
1600 * determine if the address belongs or not.
1601 */
Sasha Levind88471c2013-01-09 18:09:20 -05001602int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001603{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001604 unsigned long ret;
1605
1606 ret = ftrace_location_range((unsigned long)start,
1607 (unsigned long)end);
1608
1609 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001610}
1611
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001612/* Test if ops registered to this rec needs regs */
1613static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1614{
1615 struct ftrace_ops *ops;
1616 bool keep_regs = false;
1617
1618 for (ops = ftrace_ops_list;
1619 ops != &ftrace_list_end; ops = ops->next) {
1620 /* pass rec in as regs to have non-NULL val */
1621 if (ftrace_ops_test(ops, rec->ip, rec)) {
1622 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1623 keep_regs = true;
1624 break;
1625 }
1626 }
1627 }
1628
1629 return keep_regs;
1630}
1631
Steven Rostedted926f92011-05-03 13:25:24 -04001632static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1633 int filter_hash,
1634 bool inc)
1635{
1636 struct ftrace_hash *hash;
1637 struct ftrace_hash *other_hash;
1638 struct ftrace_page *pg;
1639 struct dyn_ftrace *rec;
1640 int count = 0;
1641 int all = 0;
1642
1643 /* Only update if the ops has been registered */
1644 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1645 return;
1646
1647 /*
1648 * In the filter_hash case:
1649 * If the count is zero, we update all records.
1650 * Otherwise we just update the items in the hash.
1651 *
1652 * In the notrace_hash case:
1653 * We enable the update in the hash.
1654 * As disabling notrace means enabling the tracing,
1655 * and enabling notrace means disabling, the inc variable
1656 * gets inversed.
1657 */
1658 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001659 hash = ops->func_hash->filter_hash;
1660 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001661 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001662 all = 1;
1663 } else {
1664 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001665 hash = ops->func_hash->notrace_hash;
1666 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001667 /*
1668 * If the notrace hash has no items,
1669 * then there's nothing to do.
1670 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001671 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001672 return;
1673 }
1674
1675 do_for_each_ftrace_rec(pg, rec) {
1676 int in_other_hash = 0;
1677 int in_hash = 0;
1678 int match = 0;
1679
1680 if (all) {
1681 /*
1682 * Only the filter_hash affects all records.
1683 * Update if the record is not in the notrace hash.
1684 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001685 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001686 match = 1;
1687 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001688 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1689 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001690
1691 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001692 * If filter_hash is set, we want to match all functions
1693 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001694 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001695 * If filter_hash is not set, then we are decrementing.
1696 * That means we match anything that is in the hash
1697 * and also in the other_hash. That is, we need to turn
1698 * off functions in the other hash because they are disabled
1699 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001700 */
1701 if (filter_hash && in_hash && !in_other_hash)
1702 match = 1;
1703 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001704 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001705 match = 1;
1706 }
1707 if (!match)
1708 continue;
1709
1710 if (inc) {
1711 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001712 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001713 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001714
1715 /*
1716 * If there's only a single callback registered to a
1717 * function, and the ops has a trampoline registered
1718 * for it, then we can call it directly.
1719 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001720 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001721 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001722 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001723 /*
1724 * If we are adding another function callback
1725 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001726 * custom trampoline in use, then we need to go
1727 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001728 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001729 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001730
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001731 /*
1732 * If any ops wants regs saved for this function
1733 * then all ops will get saved regs.
1734 */
1735 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1736 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001737 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001738 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001739 return;
1740 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001741
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001742 /*
1743 * If the rec had REGS enabled and the ops that is
1744 * being removed had REGS set, then see if there is
1745 * still any ops for this record that wants regs.
1746 * If not, we can stop recording them.
1747 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001748 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001749 rec->flags & FTRACE_FL_REGS &&
1750 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1751 if (!test_rec_ops_needs_regs(rec))
1752 rec->flags &= ~FTRACE_FL_REGS;
1753 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001754
1755 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001756 * If the rec had TRAMP enabled, then it needs to
1757 * be cleared. As TRAMP can only be enabled iff
1758 * there is only a single ops attached to it.
1759 * In otherwords, always disable it on decrementing.
1760 * In the future, we may set it if rec count is
1761 * decremented to one, and the ops that is left
1762 * has a trampoline.
1763 */
1764 rec->flags &= ~FTRACE_FL_TRAMP;
1765
1766 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001767 * flags will be cleared in ftrace_check_record()
1768 * if rec count is zero.
1769 */
Steven Rostedted926f92011-05-03 13:25:24 -04001770 }
1771 count++;
1772 /* Shortcut, if we handled all records, we are done. */
1773 if (!all && count == hash->count)
1774 return;
1775 } while_for_each_ftrace_rec();
1776}
1777
1778static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1779 int filter_hash)
1780{
1781 __ftrace_hash_rec_update(ops, filter_hash, 0);
1782}
1783
1784static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1785 int filter_hash)
1786{
1787 __ftrace_hash_rec_update(ops, filter_hash, 1);
1788}
1789
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001790static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1791 int filter_hash, int inc)
1792{
1793 struct ftrace_ops *op;
1794
1795 __ftrace_hash_rec_update(ops, filter_hash, inc);
1796
1797 if (ops->func_hash != &global_ops.local_hash)
1798 return;
1799
1800 /*
1801 * If the ops shares the global_ops hash, then we need to update
1802 * all ops that are enabled and use this hash.
1803 */
1804 do_for_each_ftrace_op(op, ftrace_ops_list) {
1805 /* Already done */
1806 if (op == ops)
1807 continue;
1808 if (op->func_hash == &global_ops.local_hash)
1809 __ftrace_hash_rec_update(op, filter_hash, inc);
1810 } while_for_each_ftrace_op(op);
1811}
1812
1813static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1814 int filter_hash)
1815{
1816 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1817}
1818
1819static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1820 int filter_hash)
1821{
1822 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1823}
1824
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001825/*
1826 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1827 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1828 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1829 * Note that old_hash and new_hash has below meanings
1830 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1831 * - If the hash is EMPTY_HASH, it hits nothing
1832 * - Anything else hits the recs which match the hash entries.
1833 */
1834static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1835 struct ftrace_hash *old_hash,
1836 struct ftrace_hash *new_hash)
1837{
1838 struct ftrace_page *pg;
1839 struct dyn_ftrace *rec, *end = NULL;
1840 int in_old, in_new;
1841
1842 /* Only update if the ops has been registered */
1843 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1844 return 0;
1845
1846 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1847 return 0;
1848
1849 /*
1850 * Since the IPMODIFY is a very address sensitive action, we do not
1851 * allow ftrace_ops to set all functions to new hash.
1852 */
1853 if (!new_hash || !old_hash)
1854 return -EINVAL;
1855
1856 /* Update rec->flags */
1857 do_for_each_ftrace_rec(pg, rec) {
1858 /* We need to update only differences of filter_hash */
1859 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1860 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1861 if (in_old == in_new)
1862 continue;
1863
1864 if (in_new) {
1865 /* New entries must ensure no others are using it */
1866 if (rec->flags & FTRACE_FL_IPMODIFY)
1867 goto rollback;
1868 rec->flags |= FTRACE_FL_IPMODIFY;
1869 } else /* Removed entry */
1870 rec->flags &= ~FTRACE_FL_IPMODIFY;
1871 } while_for_each_ftrace_rec();
1872
1873 return 0;
1874
1875rollback:
1876 end = rec;
1877
1878 /* Roll back what we did above */
1879 do_for_each_ftrace_rec(pg, rec) {
1880 if (rec == end)
1881 goto err_out;
1882
1883 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1884 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1885 if (in_old == in_new)
1886 continue;
1887
1888 if (in_new)
1889 rec->flags &= ~FTRACE_FL_IPMODIFY;
1890 else
1891 rec->flags |= FTRACE_FL_IPMODIFY;
1892 } while_for_each_ftrace_rec();
1893
1894err_out:
1895 return -EBUSY;
1896}
1897
1898static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1899{
1900 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1901
1902 if (ftrace_hash_empty(hash))
1903 hash = NULL;
1904
1905 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1906}
1907
1908/* Disabling always succeeds */
1909static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1910{
1911 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1912
1913 if (ftrace_hash_empty(hash))
1914 hash = NULL;
1915
1916 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1917}
1918
1919static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1920 struct ftrace_hash *new_hash)
1921{
1922 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1923
1924 if (ftrace_hash_empty(old_hash))
1925 old_hash = NULL;
1926
1927 if (ftrace_hash_empty(new_hash))
1928 new_hash = NULL;
1929
1930 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1931}
1932
Steven Rostedt05736a42008-09-22 14:55:47 -07001933static void print_ip_ins(const char *fmt, unsigned char *p)
1934{
1935 int i;
1936
1937 printk(KERN_CONT "%s", fmt);
1938
1939 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1940 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1941}
1942
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001943static struct ftrace_ops *
1944ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1945
Steven Rostedtc88fd862011-08-16 09:53:39 -04001946/**
1947 * ftrace_bug - report and shutdown function tracer
1948 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001949 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001950 *
1951 * The arch code that enables or disables the function tracing
1952 * can call ftrace_bug() when it has detected a problem in
1953 * modifying the code. @failed should be one of either:
1954 * EFAULT - if the problem happens on reading the @ip address
1955 * EINVAL - if what is read at @ip is not what was expected
1956 * EPERM - if the problem happens on writting to the @ip address
1957 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001958void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001959{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001960 unsigned long ip = rec ? rec->ip : 0;
1961
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001962 switch (failed) {
1963 case -EFAULT:
1964 FTRACE_WARN_ON_ONCE(1);
1965 pr_info("ftrace faulted on modifying ");
1966 print_ip_sym(ip);
1967 break;
1968 case -EINVAL:
1969 FTRACE_WARN_ON_ONCE(1);
1970 pr_info("ftrace failed to modify ");
1971 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001972 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001973 pr_cont("\n");
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001974 break;
1975 case -EPERM:
1976 FTRACE_WARN_ON_ONCE(1);
1977 pr_info("ftrace faulted on writing ");
1978 print_ip_sym(ip);
1979 break;
1980 default:
1981 FTRACE_WARN_ON_ONCE(1);
1982 pr_info("ftrace faulted on unknown error ");
1983 print_ip_sym(ip);
1984 }
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001985 if (rec) {
1986 struct ftrace_ops *ops = NULL;
1987
1988 pr_info("ftrace record flags: %lx\n", rec->flags);
1989 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
1990 rec->flags & FTRACE_FL_REGS ? " R" : " ");
1991 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1992 ops = ftrace_find_tramp_ops_any(rec);
1993 if (ops)
1994 pr_cont("\ttramp: %pS",
1995 (void *)ops->trampoline);
1996 else
1997 pr_cont("\ttramp: ERROR!");
1998
1999 }
2000 ip = ftrace_get_addr_curr(rec);
2001 pr_cont(" expected tramp: %lx\n", ip);
2002 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002003}
2004
Steven Rostedtc88fd862011-08-16 09:53:39 -04002005static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002006{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002007 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002008
Steven Rostedt982c3502008-11-15 16:31:41 -05002009 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002010 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002011 *
Steven Rostedted926f92011-05-03 13:25:24 -04002012 * If the record has a ref count, then we need to enable it
2013 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002014 *
Steven Rostedted926f92011-05-03 13:25:24 -04002015 * Otherwise we make sure its disabled.
2016 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002017 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002018 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002019 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002020 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002021 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002022
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002023 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002024 * If enabling and the REGS flag does not match the REGS_EN, or
2025 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2026 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002027 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002028 if (flag) {
2029 if (!(rec->flags & FTRACE_FL_REGS) !=
2030 !(rec->flags & FTRACE_FL_REGS_EN))
2031 flag |= FTRACE_FL_REGS;
2032
2033 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2034 !(rec->flags & FTRACE_FL_TRAMP_EN))
2035 flag |= FTRACE_FL_TRAMP;
2036 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002037
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002038 /* If the state of this record hasn't changed, then do nothing */
2039 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002040 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002041
2042 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002043 /* Save off if rec is being enabled (for return value) */
2044 flag ^= rec->flags & FTRACE_FL_ENABLED;
2045
2046 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002047 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002048 if (flag & FTRACE_FL_REGS) {
2049 if (rec->flags & FTRACE_FL_REGS)
2050 rec->flags |= FTRACE_FL_REGS_EN;
2051 else
2052 rec->flags &= ~FTRACE_FL_REGS_EN;
2053 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002054 if (flag & FTRACE_FL_TRAMP) {
2055 if (rec->flags & FTRACE_FL_TRAMP)
2056 rec->flags |= FTRACE_FL_TRAMP_EN;
2057 else
2058 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2059 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002060 }
2061
2062 /*
2063 * If this record is being updated from a nop, then
2064 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002065 * Otherwise,
2066 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002067 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002068 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002069 */
2070 if (flag & FTRACE_FL_ENABLED)
2071 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002072
2073 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002074 }
2075
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002076 if (update) {
2077 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002078 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002079 rec->flags = 0;
2080 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002081 /*
2082 * Just disable the record, but keep the ops TRAMP
2083 * and REGS states. The _EN flags must be disabled though.
2084 */
2085 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2086 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002087 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002088
2089 return FTRACE_UPDATE_MAKE_NOP;
2090}
2091
2092/**
2093 * ftrace_update_record, set a record that now is tracing or not
2094 * @rec: the record to update
2095 * @enable: set to 1 if the record is tracing, zero to force disable
2096 *
2097 * The records that represent all functions that can be traced need
2098 * to be updated when tracing has been enabled.
2099 */
2100int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2101{
2102 return ftrace_check_record(rec, enable, 1);
2103}
2104
2105/**
2106 * ftrace_test_record, check if the record has been enabled or not
2107 * @rec: the record to test
2108 * @enable: set to 1 to check if enabled, 0 if it is disabled
2109 *
2110 * The arch code may need to test if a record is already set to
2111 * tracing to determine how to modify the function code that it
2112 * represents.
2113 */
2114int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2115{
2116 return ftrace_check_record(rec, enable, 0);
2117}
2118
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002119static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002120ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2121{
2122 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002123 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002124
2125 do_for_each_ftrace_op(op, ftrace_ops_list) {
2126
2127 if (!op->trampoline)
2128 continue;
2129
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002130 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002131 return op;
2132 } while_for_each_ftrace_op(op);
2133
2134 return NULL;
2135}
2136
2137static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002138ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2139{
2140 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002141 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002142
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002143 /*
2144 * Need to check removed ops first.
2145 * If they are being removed, and this rec has a tramp,
2146 * and this rec is in the ops list, then it would be the
2147 * one with the tramp.
2148 */
2149 if (removed_ops) {
2150 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002151 return removed_ops;
2152 }
2153
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002154 /*
2155 * Need to find the current trampoline for a rec.
2156 * Now, a trampoline is only attached to a rec if there
2157 * was a single 'ops' attached to it. But this can be called
2158 * when we are adding another op to the rec or removing the
2159 * current one. Thus, if the op is being added, we can
2160 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002161 * yet.
2162 *
2163 * If an ops is being modified (hooking to different functions)
2164 * then we don't care about the new functions that are being
2165 * added, just the old ones (that are probably being removed).
2166 *
2167 * If we are adding an ops to a function that already is using
2168 * a trampoline, it needs to be removed (trampolines are only
2169 * for single ops connected), then an ops that is not being
2170 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002171 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002172 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002173
2174 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002175 continue;
2176
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002177 /*
2178 * If the ops is being added, it hasn't gotten to
2179 * the point to be removed from this tree yet.
2180 */
2181 if (op->flags & FTRACE_OPS_FL_ADDING)
2182 continue;
2183
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002184
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002185 /*
2186 * If the ops is being modified and is in the old
2187 * hash, then it is probably being removed from this
2188 * function.
2189 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002190 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2191 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002192 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002193 /*
2194 * If the ops is not being added or modified, and it's
2195 * in its normal filter hash, then this must be the one
2196 * we want!
2197 */
2198 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2199 hash_contains_ip(ip, op->func_hash))
2200 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002201
2202 } while_for_each_ftrace_op(op);
2203
2204 return NULL;
2205}
2206
2207static struct ftrace_ops *
2208ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2209{
2210 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002211 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002212
2213 do_for_each_ftrace_op(op, ftrace_ops_list) {
2214 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002215 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002216 return op;
2217 } while_for_each_ftrace_op(op);
2218
2219 return NULL;
2220}
2221
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002222/**
2223 * ftrace_get_addr_new - Get the call address to set to
2224 * @rec: The ftrace record descriptor
2225 *
2226 * If the record has the FTRACE_FL_REGS set, that means that it
2227 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2228 * is not not set, then it wants to convert to the normal callback.
2229 *
2230 * Returns the address of the trampoline to set to
2231 */
2232unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2233{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002234 struct ftrace_ops *ops;
2235
2236 /* Trampolines take precedence over regs */
2237 if (rec->flags & FTRACE_FL_TRAMP) {
2238 ops = ftrace_find_tramp_ops_new(rec);
2239 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002240 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2241 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002242 /* Ftrace is shutting down, return anything */
2243 return (unsigned long)FTRACE_ADDR;
2244 }
2245 return ops->trampoline;
2246 }
2247
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002248 if (rec->flags & FTRACE_FL_REGS)
2249 return (unsigned long)FTRACE_REGS_ADDR;
2250 else
2251 return (unsigned long)FTRACE_ADDR;
2252}
2253
2254/**
2255 * ftrace_get_addr_curr - Get the call address that is already there
2256 * @rec: The ftrace record descriptor
2257 *
2258 * The FTRACE_FL_REGS_EN is set when the record already points to
2259 * a function that saves all the regs. Basically the '_EN' version
2260 * represents the current state of the function.
2261 *
2262 * Returns the address of the trampoline that is currently being called
2263 */
2264unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2265{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002266 struct ftrace_ops *ops;
2267
2268 /* Trampolines take precedence over regs */
2269 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2270 ops = ftrace_find_tramp_ops_curr(rec);
2271 if (FTRACE_WARN_ON(!ops)) {
2272 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2273 (void *)rec->ip, (void *)rec->ip);
2274 /* Ftrace is shutting down, return anything */
2275 return (unsigned long)FTRACE_ADDR;
2276 }
2277 return ops->trampoline;
2278 }
2279
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002280 if (rec->flags & FTRACE_FL_REGS_EN)
2281 return (unsigned long)FTRACE_REGS_ADDR;
2282 else
2283 return (unsigned long)FTRACE_ADDR;
2284}
2285
Steven Rostedtc88fd862011-08-16 09:53:39 -04002286static int
2287__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2288{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002289 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002290 unsigned long ftrace_addr;
2291 int ret;
2292
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002293 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002294
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002295 /* This needs to be done before we call ftrace_update_record */
2296 ftrace_old_addr = ftrace_get_addr_curr(rec);
2297
2298 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002299
Steven Rostedtc88fd862011-08-16 09:53:39 -04002300 switch (ret) {
2301 case FTRACE_UPDATE_IGNORE:
2302 return 0;
2303
2304 case FTRACE_UPDATE_MAKE_CALL:
2305 return ftrace_make_call(rec, ftrace_addr);
2306
2307 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002308 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002309
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002310 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002311 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002312 }
2313
2314 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002315}
2316
Steven Rostedte4f5d542012-04-27 09:13:18 -04002317void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002318{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002319 struct dyn_ftrace *rec;
2320 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002321 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002322
Steven Rostedt45a4a232011-04-21 23:16:46 -04002323 if (unlikely(ftrace_disabled))
2324 return;
2325
Steven Rostedt265c8312009-02-13 12:43:56 -05002326 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002327 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002328 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002329 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002330 /* Stop processing */
2331 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002332 }
2333 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002334}
2335
Steven Rostedtc88fd862011-08-16 09:53:39 -04002336struct ftrace_rec_iter {
2337 struct ftrace_page *pg;
2338 int index;
2339};
2340
2341/**
2342 * ftrace_rec_iter_start, start up iterating over traced functions
2343 *
2344 * Returns an iterator handle that is used to iterate over all
2345 * the records that represent address locations where functions
2346 * are traced.
2347 *
2348 * May return NULL if no records are available.
2349 */
2350struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2351{
2352 /*
2353 * We only use a single iterator.
2354 * Protected by the ftrace_lock mutex.
2355 */
2356 static struct ftrace_rec_iter ftrace_rec_iter;
2357 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2358
2359 iter->pg = ftrace_pages_start;
2360 iter->index = 0;
2361
2362 /* Could have empty pages */
2363 while (iter->pg && !iter->pg->index)
2364 iter->pg = iter->pg->next;
2365
2366 if (!iter->pg)
2367 return NULL;
2368
2369 return iter;
2370}
2371
2372/**
2373 * ftrace_rec_iter_next, get the next record to process.
2374 * @iter: The handle to the iterator.
2375 *
2376 * Returns the next iterator after the given iterator @iter.
2377 */
2378struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2379{
2380 iter->index++;
2381
2382 if (iter->index >= iter->pg->index) {
2383 iter->pg = iter->pg->next;
2384 iter->index = 0;
2385
2386 /* Could have empty pages */
2387 while (iter->pg && !iter->pg->index)
2388 iter->pg = iter->pg->next;
2389 }
2390
2391 if (!iter->pg)
2392 return NULL;
2393
2394 return iter;
2395}
2396
2397/**
2398 * ftrace_rec_iter_record, get the record at the iterator location
2399 * @iter: The current iterator location
2400 *
2401 * Returns the record that the current @iter is at.
2402 */
2403struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2404{
2405 return &iter->pg->records[iter->index];
2406}
2407
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302408static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002409ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002410{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002411 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002412
Steven Rostedt45a4a232011-04-21 23:16:46 -04002413 if (unlikely(ftrace_disabled))
2414 return 0;
2415
Shaohua Li25aac9d2009-01-09 11:29:40 +08002416 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002417 if (ret) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002418 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302419 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002420 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302421 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002422}
2423
Steven Rostedt000ab692009-02-17 13:35:06 -05002424/*
2425 * archs can override this function if they must do something
2426 * before the modifying code is performed.
2427 */
2428int __weak ftrace_arch_code_modify_prepare(void)
2429{
2430 return 0;
2431}
2432
2433/*
2434 * archs can override this function if they must do something
2435 * after the modifying code is performed.
2436 */
2437int __weak ftrace_arch_code_modify_post_process(void)
2438{
2439 return 0;
2440}
2441
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002442void ftrace_modify_all_code(int command)
2443{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002444 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002445 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002446
2447 /*
2448 * If the ftrace_caller calls a ftrace_ops func directly,
2449 * we need to make sure that it only traces functions it
2450 * expects to trace. When doing the switch of functions,
2451 * we need to update to the ftrace_ops_list_func first
2452 * before the transition between old and new calls are set,
2453 * as the ftrace_ops_list_func will check the ops hashes
2454 * to make sure the ops are having the right functions
2455 * traced.
2456 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002457 if (update) {
2458 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2459 if (FTRACE_WARN_ON(err))
2460 return;
2461 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002462
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002463 if (command & FTRACE_UPDATE_CALLS)
2464 ftrace_replace_code(1);
2465 else if (command & FTRACE_DISABLE_CALLS)
2466 ftrace_replace_code(0);
2467
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002468 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2469 function_trace_op = set_function_trace_op;
2470 smp_wmb();
2471 /* If irqs are disabled, we are in stop machine */
2472 if (!irqs_disabled())
2473 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002474 err = ftrace_update_ftrace_func(ftrace_trace_function);
2475 if (FTRACE_WARN_ON(err))
2476 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002477 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002478
2479 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002480 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002481 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002482 err = ftrace_disable_ftrace_graph_caller();
2483 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002484}
2485
Ingo Molnare309b412008-05-12 21:20:51 +02002486static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002487{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002488 int *command = data;
2489
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002490 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002491
Steven Rostedtc88fd862011-08-16 09:53:39 -04002492 return 0;
2493}
2494
2495/**
2496 * ftrace_run_stop_machine, go back to the stop machine method
2497 * @command: The command to tell ftrace what to do
2498 *
2499 * If an arch needs to fall back to the stop machine method, the
2500 * it can call this function.
2501 */
2502void ftrace_run_stop_machine(int command)
2503{
2504 stop_machine(__ftrace_modify_code, &command, NULL);
2505}
2506
2507/**
2508 * arch_ftrace_update_code, modify the code to trace or not trace
2509 * @command: The command that needs to be done
2510 *
2511 * Archs can override this function if it does not need to
2512 * run stop_machine() to modify code.
2513 */
2514void __weak arch_ftrace_update_code(int command)
2515{
2516 ftrace_run_stop_machine(command);
2517}
2518
2519static void ftrace_run_update_code(int command)
2520{
2521 int ret;
2522
2523 ret = ftrace_arch_code_modify_prepare();
2524 FTRACE_WARN_ON(ret);
2525 if (ret)
2526 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002527
2528 /*
2529 * By default we use stop_machine() to modify the code.
2530 * But archs can do what ever they want as long as it
2531 * is safe. The stop_machine() is the safest, but also
2532 * produces the most overhead.
2533 */
2534 arch_ftrace_update_code(command);
2535
Steven Rostedt000ab692009-02-17 13:35:06 -05002536 ret = ftrace_arch_code_modify_post_process();
2537 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002538}
2539
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002540static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002541 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002542{
2543 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002544 ops->old_hash.filter_hash = old_hash->filter_hash;
2545 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002546 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002547 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002548 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002549 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2550}
2551
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002552static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002553static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002554
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002555void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2556{
2557}
2558
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002559static void control_ops_free(struct ftrace_ops *ops)
2560{
2561 free_percpu(ops->disabled);
2562}
2563
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002564static void ftrace_startup_enable(int command)
2565{
2566 if (saved_ftrace_func != ftrace_trace_function) {
2567 saved_ftrace_func = ftrace_trace_function;
2568 command |= FTRACE_UPDATE_TRACE_FUNC;
2569 }
2570
2571 if (!command || !ftrace_enabled)
2572 return;
2573
2574 ftrace_run_update_code(command);
2575}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002576
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002577static void ftrace_startup_all(int command)
2578{
2579 update_all_ops = true;
2580 ftrace_startup_enable(command);
2581 update_all_ops = false;
2582}
2583
Steven Rostedta1cd6172011-05-23 15:24:25 -04002584static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002585{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002586 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002587
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002588 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002589 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002590
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002591 ret = __register_ftrace_function(ops);
2592 if (ret)
2593 return ret;
2594
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002595 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002596 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002597
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002598 /*
2599 * Note that ftrace probes uses this to start up
2600 * and modify functions it will probe. But we still
2601 * set the ADDING flag for modification, as probes
2602 * do not have trampolines. If they add them in the
2603 * future, then the probes will need to distinguish
2604 * between adding and updating probes.
2605 */
2606 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002607
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002608 ret = ftrace_hash_ipmodify_enable(ops);
2609 if (ret < 0) {
2610 /* Rollback registration process */
2611 __unregister_ftrace_function(ops);
2612 ftrace_start_up--;
2613 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2614 return ret;
2615 }
2616
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002617 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002618
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002619 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002620
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002621 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2622
Steven Rostedta1cd6172011-05-23 15:24:25 -04002623 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002624}
2625
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002626static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002627{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002628 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002629
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002630 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002631 return -ENODEV;
2632
2633 ret = __unregister_ftrace_function(ops);
2634 if (ret)
2635 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002636
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002637 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002638 /*
2639 * Just warn in case of unbalance, no need to kill ftrace, it's not
2640 * critical but the ftrace_call callers may be never nopped again after
2641 * further ftrace uses.
2642 */
2643 WARN_ON_ONCE(ftrace_start_up < 0);
2644
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002645 /* Disabling ipmodify never fails */
2646 ftrace_hash_ipmodify_disable(ops);
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002647 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002648
Namhyung Kima737e6d2014-06-12 23:56:12 +09002649 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002650
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002651 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002652
2653 if (saved_ftrace_func != ftrace_trace_function) {
2654 saved_ftrace_func = ftrace_trace_function;
2655 command |= FTRACE_UPDATE_TRACE_FUNC;
2656 }
2657
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002658 if (!command || !ftrace_enabled) {
2659 /*
2660 * If these are control ops, they still need their
2661 * per_cpu field freed. Since, function tracing is
2662 * not currently active, we can just free them
2663 * without synchronizing all CPUs.
2664 */
2665 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2666 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002667 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002668 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002669
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002670 /*
2671 * If the ops uses a trampoline, then it needs to be
2672 * tested first on update.
2673 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002674 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002675 removed_ops = ops;
2676
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002677 /* The trampoline logic checks the old hashes */
2678 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2679 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2680
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002681 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002682
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002683 /*
2684 * If there's no more ops registered with ftrace, run a
2685 * sanity check to make sure all rec flags are cleared.
2686 */
2687 if (ftrace_ops_list == &ftrace_list_end) {
2688 struct ftrace_page *pg;
2689 struct dyn_ftrace *rec;
2690
2691 do_for_each_ftrace_rec(pg, rec) {
2692 if (FTRACE_WARN_ON_ONCE(rec->flags))
2693 pr_warn(" %pS flags:%lx\n",
2694 (void *)rec->ip, rec->flags);
2695 } while_for_each_ftrace_rec();
2696 }
2697
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002698 ops->old_hash.filter_hash = NULL;
2699 ops->old_hash.notrace_hash = NULL;
2700
2701 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002702 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002703
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002704 /*
2705 * Dynamic ops may be freed, we must make sure that all
2706 * callers are done before leaving this function.
2707 * The same goes for freeing the per_cpu data of the control
2708 * ops.
2709 *
2710 * Again, normal synchronize_sched() is not good enough.
2711 * We need to do a hard force of sched synchronization.
2712 * This is because we use preempt_disable() to do RCU, but
2713 * the function tracers can be called where RCU is not watching
2714 * (like before user_exit()). We can not rely on the RCU
2715 * infrastructure to do the synchronization, thus we must do it
2716 * ourselves.
2717 */
2718 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2719 schedule_on_each_cpu(ftrace_sync);
2720
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002721 arch_ftrace_trampoline_free(ops);
2722
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002723 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2724 control_ops_free(ops);
2725 }
2726
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002727 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002728}
2729
Ingo Molnare309b412008-05-12 21:20:51 +02002730static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002731{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302732 int command;
2733
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002734 if (unlikely(ftrace_disabled))
2735 return;
2736
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002737 /* Force update next time */
2738 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002739 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302740 if (ftrace_start_up) {
2741 command = FTRACE_UPDATE_CALLS;
2742 if (ftrace_graph_active)
2743 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002744 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302745 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002746}
2747
Ingo Molnare309b412008-05-12 21:20:51 +02002748static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002749{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302750 int command;
2751
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002752 if (unlikely(ftrace_disabled))
2753 return;
2754
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002755 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302756 if (ftrace_start_up) {
2757 command = FTRACE_DISABLE_CALLS;
2758 if (ftrace_graph_active)
2759 command |= FTRACE_STOP_FUNC_RET;
2760 ftrace_run_update_code(command);
2761 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002762}
2763
Steven Rostedt3d083392008-05-12 21:20:42 +02002764static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002765unsigned long ftrace_update_tot_cnt;
2766
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002767static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002768{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002769 /*
2770 * Filter_hash being empty will default to trace module.
2771 * But notrace hash requires a test of individual module functions.
2772 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002773 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2774 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002775}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002776
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002777/*
2778 * Check if the current ops references the record.
2779 *
2780 * If the ops traces all functions, then it was already accounted for.
2781 * If the ops does not trace the current record function, skip it.
2782 * If the ops ignores the function via notrace filter, skip it.
2783 */
2784static inline bool
2785ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2786{
2787 /* If ops isn't enabled, ignore it */
2788 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2789 return 0;
2790
2791 /* If ops traces all mods, we already accounted for it */
2792 if (ops_traces_mod(ops))
2793 return 0;
2794
2795 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002796 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2797 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002798 return 0;
2799
2800 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002801 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002802 return 0;
2803
2804 return 1;
2805}
2806
2807static int referenced_filters(struct dyn_ftrace *rec)
2808{
2809 struct ftrace_ops *ops;
2810 int cnt = 0;
2811
2812 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2813 if (ops_references_rec(ops, rec))
2814 cnt++;
2815 }
2816
2817 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002818}
2819
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002820static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002821{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002822 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002823 struct dyn_ftrace *p;
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302824 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002825 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002826 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002827 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002828 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002829
2830 /*
2831 * When adding a module, we need to check if tracers are
2832 * currently enabled and if they are set to trace all functions.
2833 * If they are, we need to enable the module functions as well
2834 * as update the reference counts for those function records.
2835 */
2836 if (mod) {
2837 struct ftrace_ops *ops;
2838
2839 for (ops = ftrace_ops_list;
2840 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002841 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2842 if (ops_traces_mod(ops))
2843 ref++;
2844 else
2845 test = true;
2846 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002847 }
2848 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002849
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002850 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002851
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002852 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302853
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002854 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002855 int cnt = ref;
2856
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002857 /* If something went wrong, bail without enabling anything */
2858 if (unlikely(ftrace_disabled))
2859 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002860
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002861 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002862 if (test)
2863 cnt += referenced_filters(p);
2864 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302865
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002866 /*
2867 * Do the initial record conversion from mcount jump
2868 * to the NOP instructions.
2869 */
2870 if (!ftrace_code_disable(mod, p))
2871 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002872
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002873 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002874
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002875 /*
2876 * If the tracing is enabled, go ahead and enable the record.
2877 *
2878 * The reason not to enable the record immediatelly is the
2879 * inherent check of ftrace_make_nop/ftrace_make_call for
2880 * correct previous instructions. Making first the NOP
2881 * conversion puts the module to the correct state, thus
2882 * passing the ftrace_make_call check.
2883 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002884 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002885 int failed = __ftrace_replace_code(p, 1);
2886 if (failed)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002887 ftrace_bug(failed, p);
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002888 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002889 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002890 }
2891
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002892 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002893 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002894 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002895
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002896 return 0;
2897}
2898
Steven Rostedta7900872011-12-16 16:23:44 -05002899static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002900{
Steven Rostedta7900872011-12-16 16:23:44 -05002901 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002902 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002903
Steven Rostedta7900872011-12-16 16:23:44 -05002904 if (WARN_ON(!count))
2905 return -EINVAL;
2906
2907 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002908
2909 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002910 * We want to fill as much as possible. No more than a page
2911 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002912 */
Steven Rostedta7900872011-12-16 16:23:44 -05002913 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2914 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002915
Steven Rostedta7900872011-12-16 16:23:44 -05002916 again:
2917 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2918
2919 if (!pg->records) {
2920 /* if we can't allocate this size, try something smaller */
2921 if (!order)
2922 return -ENOMEM;
2923 order >>= 1;
2924 goto again;
2925 }
2926
2927 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2928 pg->size = cnt;
2929
2930 if (cnt > count)
2931 cnt = count;
2932
2933 return cnt;
2934}
2935
2936static struct ftrace_page *
2937ftrace_allocate_pages(unsigned long num_to_init)
2938{
2939 struct ftrace_page *start_pg;
2940 struct ftrace_page *pg;
2941 int order;
2942 int cnt;
2943
2944 if (!num_to_init)
2945 return 0;
2946
2947 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2948 if (!pg)
2949 return NULL;
2950
2951 /*
2952 * Try to allocate as much as possible in one continues
2953 * location that fills in all of the space. We want to
2954 * waste as little space as possible.
2955 */
2956 for (;;) {
2957 cnt = ftrace_allocate_records(pg, num_to_init);
2958 if (cnt < 0)
2959 goto free_pages;
2960
2961 num_to_init -= cnt;
2962 if (!num_to_init)
2963 break;
2964
2965 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2966 if (!pg->next)
2967 goto free_pages;
2968
2969 pg = pg->next;
2970 }
2971
2972 return start_pg;
2973
2974 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002975 pg = start_pg;
2976 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002977 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2978 free_pages((unsigned long)pg->records, order);
2979 start_pg = pg->next;
2980 kfree(pg);
2981 pg = start_pg;
2982 }
2983 pr_info("ftrace: FAILED to allocate memory for functions\n");
2984 return NULL;
2985}
2986
Steven Rostedt5072c592008-05-12 21:20:43 +02002987#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2988
2989struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002990 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002991 loff_t func_pos;
2992 struct ftrace_page *pg;
2993 struct dyn_ftrace *func;
2994 struct ftrace_func_probe *probe;
2995 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002996 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002997 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002998 int hidx;
2999 int idx;
3000 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003001};
3002
Ingo Molnare309b412008-05-12 21:20:51 +02003003static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003004t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003005{
3006 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003007 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003008 struct hlist_head *hhd;
3009
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003010 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003011 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003012
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003013 if (iter->probe)
3014 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003015 retry:
3016 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3017 return NULL;
3018
3019 hhd = &ftrace_func_hash[iter->hidx];
3020
3021 if (hlist_empty(hhd)) {
3022 iter->hidx++;
3023 hnd = NULL;
3024 goto retry;
3025 }
3026
3027 if (!hnd)
3028 hnd = hhd->first;
3029 else {
3030 hnd = hnd->next;
3031 if (!hnd) {
3032 iter->hidx++;
3033 goto retry;
3034 }
3035 }
3036
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003037 if (WARN_ON_ONCE(!hnd))
3038 return NULL;
3039
3040 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3041
3042 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003043}
3044
3045static void *t_hash_start(struct seq_file *m, loff_t *pos)
3046{
3047 struct ftrace_iterator *iter = m->private;
3048 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003049 loff_t l;
3050
Steven Rostedt69a30832011-12-19 15:21:16 -05003051 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3052 return NULL;
3053
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003054 if (iter->func_pos > *pos)
3055 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003056
Li Zefand82d6242009-06-24 09:54:54 +08003057 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003058 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003059 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003060 if (!p)
3061 break;
3062 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003063 if (!p)
3064 return NULL;
3065
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003066 /* Only set this if we have an item */
3067 iter->flags |= FTRACE_ITER_HASH;
3068
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003069 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003070}
3071
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003072static int
3073t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003074{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003075 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003076
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003077 rec = iter->probe;
3078 if (WARN_ON_ONCE(!rec))
3079 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003080
Steven Rostedt809dcf22009-02-16 23:06:01 -05003081 if (rec->ops->print)
3082 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3083
Steven Rostedtb375a112009-09-17 00:05:58 -04003084 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003085
3086 if (rec->data)
3087 seq_printf(m, ":%p", rec->data);
3088 seq_putc(m, '\n');
3089
3090 return 0;
3091}
3092
3093static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02003094t_next(struct seq_file *m, void *v, loff_t *pos)
3095{
3096 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003097 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003098 struct dyn_ftrace *rec = NULL;
3099
Steven Rostedt45a4a232011-04-21 23:16:46 -04003100 if (unlikely(ftrace_disabled))
3101 return NULL;
3102
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003103 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003104 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003105
Steven Rostedt5072c592008-05-12 21:20:43 +02003106 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01003107 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02003108
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003109 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003110 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003111
Steven Rostedt5072c592008-05-12 21:20:43 +02003112 retry:
3113 if (iter->idx >= iter->pg->index) {
3114 if (iter->pg->next) {
3115 iter->pg = iter->pg->next;
3116 iter->idx = 0;
3117 goto retry;
3118 }
3119 } else {
3120 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05003121 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003122 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05003123
Steven Rostedt41c52c02008-05-22 11:46:33 -04003124 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003125 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003126
3127 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003128 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003129
Steven Rostedt5072c592008-05-12 21:20:43 +02003130 rec = NULL;
3131 goto retry;
3132 }
3133 }
3134
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003135 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003136 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003137
3138 iter->func = rec;
3139
3140 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003141}
3142
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003143static void reset_iter_read(struct ftrace_iterator *iter)
3144{
3145 iter->pos = 0;
3146 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03003147 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003148}
3149
3150static void *t_start(struct seq_file *m, loff_t *pos)
3151{
3152 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003153 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003154 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003155 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003156
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003157 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003158
3159 if (unlikely(ftrace_disabled))
3160 return NULL;
3161
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003162 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003163 * If an lseek was done, then reset and start from beginning.
3164 */
3165 if (*pos < iter->pos)
3166 reset_iter_read(iter);
3167
3168 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003169 * For set_ftrace_filter reading, if we have the filter
3170 * off, we can short cut and just print out that all
3171 * functions are enabled.
3172 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003173 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003174 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003175 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003176 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003177 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003178 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003179 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003180 /* reset in case of seek/pread */
3181 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003182 return iter;
3183 }
3184
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003185 if (iter->flags & FTRACE_ITER_HASH)
3186 return t_hash_start(m, pos);
3187
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003188 /*
3189 * Unfortunately, we need to restart at ftrace_pages_start
3190 * every time we let go of the ftrace_mutex. This is because
3191 * those pointers can change without the lock.
3192 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003193 iter->pg = ftrace_pages_start;
3194 iter->idx = 0;
3195 for (l = 0; l <= *pos; ) {
3196 p = t_next(m, p, &l);
3197 if (!p)
3198 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003199 }
walimis5821e1b2008-11-15 15:19:06 +08003200
Steven Rostedt69a30832011-12-19 15:21:16 -05003201 if (!p)
3202 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003203
3204 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003205}
3206
3207static void t_stop(struct seq_file *m, void *p)
3208{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003209 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003210}
3211
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003212void * __weak
3213arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3214{
3215 return NULL;
3216}
3217
3218static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3219 struct dyn_ftrace *rec)
3220{
3221 void *ptr;
3222
3223 ptr = arch_ftrace_trampoline_func(ops, rec);
3224 if (ptr)
3225 seq_printf(m, " ->%pS", ptr);
3226}
3227
Steven Rostedt5072c592008-05-12 21:20:43 +02003228static int t_show(struct seq_file *m, void *v)
3229{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003230 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003231 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003232
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003233 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003234 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003235
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003236 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003237 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003238 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003239 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003240 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003241 return 0;
3242 }
3243
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003244 rec = iter->func;
3245
Steven Rostedt5072c592008-05-12 21:20:43 +02003246 if (!rec)
3247 return 0;
3248
Steven Rostedt647bcd02011-05-03 14:39:21 -04003249 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003250 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003251 struct ftrace_ops *ops = NULL;
3252
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003253 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003254 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003255 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3256 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003257 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003258 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04003259 if (ops)
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003260 seq_printf(m, "\ttramp: %pS",
3261 (void *)ops->trampoline);
3262 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003263 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003264
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003265 }
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003266 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003267 }
3268
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003269 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003270
3271 return 0;
3272}
3273
James Morris88e9d342009-09-22 16:43:43 -07003274static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003275 .start = t_start,
3276 .next = t_next,
3277 .stop = t_stop,
3278 .show = t_show,
3279};
3280
Ingo Molnare309b412008-05-12 21:20:51 +02003281static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003282ftrace_avail_open(struct inode *inode, struct file *file)
3283{
3284 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003285
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003286 if (unlikely(ftrace_disabled))
3287 return -ENODEV;
3288
Jiri Olsa50e18b92012-04-25 10:23:39 +02003289 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3290 if (iter) {
3291 iter->pg = ftrace_pages_start;
3292 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003293 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003294
Jiri Olsa50e18b92012-04-25 10:23:39 +02003295 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003296}
3297
Steven Rostedt647bcd02011-05-03 14:39:21 -04003298static int
3299ftrace_enabled_open(struct inode *inode, struct file *file)
3300{
3301 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003302
Jiri Olsa50e18b92012-04-25 10:23:39 +02003303 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3304 if (iter) {
3305 iter->pg = ftrace_pages_start;
3306 iter->flags = FTRACE_ITER_ENABLED;
3307 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003308 }
3309
Jiri Olsa50e18b92012-04-25 10:23:39 +02003310 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003311}
3312
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003313/**
3314 * ftrace_regex_open - initialize function tracer filter files
3315 * @ops: The ftrace_ops that hold the hash filters
3316 * @flag: The type of filter to process
3317 * @inode: The inode, usually passed in to your open routine
3318 * @file: The file, usually passed in to your open routine
3319 *
3320 * ftrace_regex_open() initializes the filter files for the
3321 * @ops. Depending on @flag it may process the filter hash or
3322 * the notrace hash of @ops. With this called from the open
3323 * routine, you can use ftrace_filter_write() for the write
3324 * routine if @flag has FTRACE_ITER_FILTER set, or
3325 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003326 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003327 * release must call ftrace_regex_release().
3328 */
3329int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003330ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003331 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003332{
3333 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003334 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003335 int ret = 0;
3336
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003337 ftrace_ops_init(ops);
3338
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003339 if (unlikely(ftrace_disabled))
3340 return -ENODEV;
3341
Steven Rostedt5072c592008-05-12 21:20:43 +02003342 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3343 if (!iter)
3344 return -ENOMEM;
3345
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003346 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3347 kfree(iter);
3348 return -ENOMEM;
3349 }
3350
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003351 iter->ops = ops;
3352 iter->flags = flag;
3353
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003354 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003355
Steven Rostedtf45948e2011-05-02 12:29:25 -04003356 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003357 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003358 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003359 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003360
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003361 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003362 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3363
3364 if (file->f_flags & O_TRUNC)
3365 iter->hash = alloc_ftrace_hash(size_bits);
3366 else
3367 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3368
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003369 if (!iter->hash) {
3370 trace_parser_put(&iter->parser);
3371 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003372 ret = -ENOMEM;
3373 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003374 }
3375 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003376
Steven Rostedt5072c592008-05-12 21:20:43 +02003377 if (file->f_mode & FMODE_READ) {
3378 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003379
3380 ret = seq_open(file, &show_ftrace_seq_ops);
3381 if (!ret) {
3382 struct seq_file *m = file->private_data;
3383 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003384 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003385 /* Failed */
3386 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003387 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003388 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003389 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003390 } else
3391 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003392
3393 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003394 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003395
3396 return ret;
3397}
3398
Steven Rostedt41c52c02008-05-22 11:46:33 -04003399static int
3400ftrace_filter_open(struct inode *inode, struct file *file)
3401{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003402 struct ftrace_ops *ops = inode->i_private;
3403
3404 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003405 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3406 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003407}
3408
3409static int
3410ftrace_notrace_open(struct inode *inode, struct file *file)
3411{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003412 struct ftrace_ops *ops = inode->i_private;
3413
3414 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003415 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003416}
3417
Steven Rostedt64e7c442009-02-13 17:08:48 -05003418static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003419{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003420 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003421 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003422
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003423 switch (type) {
3424 case MATCH_FULL:
3425 if (strcmp(str, regex) == 0)
3426 matched = 1;
3427 break;
3428 case MATCH_FRONT_ONLY:
3429 if (strncmp(str, regex, len) == 0)
3430 matched = 1;
3431 break;
3432 case MATCH_MIDDLE_ONLY:
3433 if (strstr(str, regex))
3434 matched = 1;
3435 break;
3436 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003437 slen = strlen(str);
3438 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003439 matched = 1;
3440 break;
3441 }
3442
3443 return matched;
3444}
3445
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003446static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003447enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003448{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003449 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003450 int ret = 0;
3451
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003452 entry = ftrace_lookup_ip(hash, rec->ip);
3453 if (not) {
3454 /* Do nothing if it doesn't exist */
3455 if (!entry)
3456 return 0;
3457
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003458 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003459 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003460 /* Do nothing if it exists */
3461 if (entry)
3462 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003463
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003464 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003465 }
3466 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003467}
3468
Steven Rostedt64e7c442009-02-13 17:08:48 -05003469static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003470ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3471 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003472{
3473 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003474 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003475
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003476 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3477
3478 if (mod) {
3479 /* module lookup requires matching the module */
3480 if (!modname || strcmp(modname, mod))
3481 return 0;
3482
3483 /* blank search means to match all funcs in the mod */
3484 if (!len)
3485 return 1;
3486 }
3487
Steven Rostedt64e7c442009-02-13 17:08:48 -05003488 return ftrace_match(str, regex, len, type);
3489}
3490
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003491static int
3492match_records(struct ftrace_hash *hash, char *buff,
3493 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003494{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003495 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003496 struct ftrace_page *pg;
3497 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003498 int type = MATCH_FULL;
3499 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003500 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003501 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003502
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003503 if (len) {
3504 type = filter_parse_regex(buff, len, &search, &not);
3505 search_len = strlen(search);
3506 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003507
Steven Rostedt52baf112009-02-14 01:15:39 -05003508 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003509
3510 if (unlikely(ftrace_disabled))
3511 goto out_unlock;
3512
Steven Rostedt265c8312009-02-13 12:43:56 -05003513 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003514 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003515 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003516 if (ret < 0) {
3517 found = ret;
3518 goto out_unlock;
3519 }
Li Zefan311d16d2009-12-08 11:15:11 +08003520 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003521 }
3522 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003523 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003524 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003525
3526 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003527}
3528
Steven Rostedt64e7c442009-02-13 17:08:48 -05003529static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003530ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003531{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003532 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003533}
3534
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003535static int
3536ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003537{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003538 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003539
Steven Rostedt64e7c442009-02-13 17:08:48 -05003540 /* blank or '*' mean the same */
3541 if (strcmp(buff, "*") == 0)
3542 buff[0] = 0;
3543
3544 /* handle the case of 'dont filter this module' */
3545 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3546 buff[0] = 0;
3547 not = 1;
3548 }
3549
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003550 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003551}
3552
Steven Rostedtf6180772009-02-14 00:40:25 -05003553/*
3554 * We register the module command as a template to show others how
3555 * to register the a command as well.
3556 */
3557
3558static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003559ftrace_mod_callback(struct ftrace_hash *hash,
3560 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003561{
3562 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003563 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003564
3565 /*
3566 * cmd == 'mod' because we only registered this func
3567 * for the 'mod' ftrace_func_command.
3568 * But if you register one func with multiple commands,
3569 * you can tell which command was used by the cmd
3570 * parameter.
3571 */
3572
3573 /* we must have a module name */
3574 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003575 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003576
3577 mod = strsep(&param, ":");
3578 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003579 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003580
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003581 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003582 if (!ret)
3583 ret = -EINVAL;
3584 if (ret < 0)
3585 return ret;
3586
3587 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003588}
3589
3590static struct ftrace_func_command ftrace_mod_cmd = {
3591 .name = "mod",
3592 .func = ftrace_mod_callback,
3593};
3594
3595static int __init ftrace_mod_cmd_init(void)
3596{
3597 return register_ftrace_command(&ftrace_mod_cmd);
3598}
Steven Rostedt6f415672012-10-05 12:13:07 -04003599core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003600
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003601static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003602 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003603{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003604 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003605 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003606 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003607
3608 key = hash_long(ip, FTRACE_HASH_BITS);
3609
3610 hhd = &ftrace_func_hash[key];
3611
3612 if (hlist_empty(hhd))
3613 return;
3614
3615 /*
3616 * Disable preemption for these calls to prevent a RCU grace
3617 * period. This syncs the hash iteration and freeing of items
3618 * on the hash. rcu_read_lock is too dangerous here.
3619 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003620 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003621 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003622 if (entry->ip == ip)
3623 entry->ops->func(ip, parent_ip, &entry->data);
3624 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003625 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003626}
3627
Steven Rostedtb6887d72009-02-17 12:32:04 -05003628static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003629{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003630 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003631 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003632 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003633};
3634
Steven Rostedtb6887d72009-02-17 12:32:04 -05003635static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003636
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003637static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003638{
Steven Rostedtb8489142011-05-04 09:27:52 -04003639 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003640 int i;
3641
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003642 if (ftrace_probe_registered) {
3643 /* still need to update the function call sites */
3644 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003645 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3646 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003647 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003648 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003649
3650 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3651 struct hlist_head *hhd = &ftrace_func_hash[i];
3652 if (hhd->first)
3653 break;
3654 }
3655 /* Nothing registered? */
3656 if (i == FTRACE_FUNC_HASHSIZE)
3657 return;
3658
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003659 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003660
Steven Rostedtb6887d72009-02-17 12:32:04 -05003661 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003662}
3663
Steven Rostedtb6887d72009-02-17 12:32:04 -05003664static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003665{
3666 int i;
3667
Steven Rostedtb6887d72009-02-17 12:32:04 -05003668 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003669 return;
3670
3671 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3672 struct hlist_head *hhd = &ftrace_func_hash[i];
3673 if (hhd->first)
3674 return;
3675 }
3676
3677 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003678 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003679
Steven Rostedtb6887d72009-02-17 12:32:04 -05003680 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003681}
3682
3683
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003684static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003685{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003686 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003687 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003688 kfree(entry);
3689}
3690
Steven Rostedt59df055f2009-02-14 15:29:06 -05003691int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003692register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003693 void *data)
3694{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003695 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003696 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003697 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003698 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003699 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003700 struct ftrace_page *pg;
3701 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003702 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003703 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003704 int count = 0;
3705 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003706 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003707
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003708 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003709 len = strlen(search);
3710
Steven Rostedtb6887d72009-02-17 12:32:04 -05003711 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003712 if (WARN_ON(not))
3713 return -EINVAL;
3714
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003715 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003716
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003717 old_hash_ops.filter_hash = old_hash;
3718 /* Probes only have filters */
3719 old_hash_ops.notrace_hash = NULL;
3720
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003721 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003722 if (!hash) {
3723 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003724 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003725 }
3726
3727 if (unlikely(ftrace_disabled)) {
3728 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003729 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003730 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003731
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003732 mutex_lock(&ftrace_lock);
3733
Steven Rostedt59df055f2009-02-14 15:29:06 -05003734 do_for_each_ftrace_rec(pg, rec) {
3735
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003736 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003737 continue;
3738
3739 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3740 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003741 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003742 if (!count)
3743 count = -ENOMEM;
3744 goto out_unlock;
3745 }
3746
3747 count++;
3748
3749 entry->data = data;
3750
3751 /*
3752 * The caller might want to do something special
3753 * for each function we find. We call the callback
3754 * to give the caller an opportunity to do so.
3755 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003756 if (ops->init) {
3757 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003758 /* caller does not like this func */
3759 kfree(entry);
3760 continue;
3761 }
3762 }
3763
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003764 ret = enter_record(hash, rec, 0);
3765 if (ret < 0) {
3766 kfree(entry);
3767 count = ret;
3768 goto out_unlock;
3769 }
3770
Steven Rostedt59df055f2009-02-14 15:29:06 -05003771 entry->ops = ops;
3772 entry->ip = rec->ip;
3773
3774 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3775 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3776
3777 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003778
3779 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003780
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003781 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003782
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003783 if (!ret)
3784 free_ftrace_hash_rcu(old_hash);
3785 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003786 count = ret;
3787
Steven Rostedt59df055f2009-02-14 15:29:06 -05003788 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003789 mutex_unlock(&ftrace_lock);
3790 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003791 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003792 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003793
3794 return count;
3795}
3796
3797enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003798 PROBE_TEST_FUNC = 1,
3799 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003800};
3801
3802static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003803__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003804 void *data, int flags)
3805{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003806 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003807 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003808 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003809 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003810 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003811 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003812 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003813 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003814 char str[KSYM_SYMBOL_LEN];
3815 int type = MATCH_FULL;
3816 int i, len = 0;
3817 char *search;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003818 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003819
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003820 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003821 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003822 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003823 int not;
3824
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003825 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003826 len = strlen(search);
3827
Steven Rostedtb6887d72009-02-17 12:32:04 -05003828 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003829 if (WARN_ON(not))
3830 return;
3831 }
3832
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003833 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003834
3835 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3836 if (!hash)
3837 /* Hmm, should report this somehow */
3838 goto out_unlock;
3839
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003840 INIT_LIST_HEAD(&free_list);
3841
Steven Rostedt59df055f2009-02-14 15:29:06 -05003842 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3843 struct hlist_head *hhd = &ftrace_func_hash[i];
3844
Sasha Levinb67bfe02013-02-27 17:06:00 -08003845 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003846
3847 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003848 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003849 continue;
3850
Steven Rostedtb6887d72009-02-17 12:32:04 -05003851 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003852 continue;
3853
3854 /* do this last, since it is the most expensive */
3855 if (glob) {
3856 kallsyms_lookup(entry->ip, NULL, NULL,
3857 NULL, str);
3858 if (!ftrace_match(str, glob, len, type))
3859 continue;
3860 }
3861
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003862 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3863 /* It is possible more than one entry had this ip */
3864 if (rec_entry)
3865 free_hash_entry(hash, rec_entry);
3866
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003867 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003868 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003869 }
3870 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003871 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003872 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003873 /*
3874 * Remove after the disable is called. Otherwise, if the last
3875 * probe is removed, a null hash means *all enabled*.
3876 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003877 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003878 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003879 if (!ret)
3880 free_ftrace_hash_rcu(old_hash);
3881
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003882 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3883 list_del(&entry->free_list);
3884 ftrace_free_entry(entry);
3885 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003886 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003887
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003888 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003889 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003890 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003891}
3892
3893void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003894unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003895 void *data)
3896{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003897 __unregister_ftrace_function_probe(glob, ops, data,
3898 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003899}
3900
3901void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003902unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003903{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003904 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003905}
3906
Steven Rostedtb6887d72009-02-17 12:32:04 -05003907void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003908{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003909 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003910}
3911
Steven Rostedtf6180772009-02-14 00:40:25 -05003912static LIST_HEAD(ftrace_commands);
3913static DEFINE_MUTEX(ftrace_cmd_mutex);
3914
Tom Zanussi38de93a2013-10-24 08:34:18 -05003915/*
3916 * Currently we only register ftrace commands from __init, so mark this
3917 * __init too.
3918 */
3919__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003920{
3921 struct ftrace_func_command *p;
3922 int ret = 0;
3923
3924 mutex_lock(&ftrace_cmd_mutex);
3925 list_for_each_entry(p, &ftrace_commands, list) {
3926 if (strcmp(cmd->name, p->name) == 0) {
3927 ret = -EBUSY;
3928 goto out_unlock;
3929 }
3930 }
3931 list_add(&cmd->list, &ftrace_commands);
3932 out_unlock:
3933 mutex_unlock(&ftrace_cmd_mutex);
3934
3935 return ret;
3936}
3937
Tom Zanussi38de93a2013-10-24 08:34:18 -05003938/*
3939 * Currently we only unregister ftrace commands from __init, so mark
3940 * this __init too.
3941 */
3942__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003943{
3944 struct ftrace_func_command *p, *n;
3945 int ret = -ENODEV;
3946
3947 mutex_lock(&ftrace_cmd_mutex);
3948 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3949 if (strcmp(cmd->name, p->name) == 0) {
3950 ret = 0;
3951 list_del_init(&p->list);
3952 goto out_unlock;
3953 }
3954 }
3955 out_unlock:
3956 mutex_unlock(&ftrace_cmd_mutex);
3957
3958 return ret;
3959}
3960
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003961static int ftrace_process_regex(struct ftrace_hash *hash,
3962 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003963{
Steven Rostedtf6180772009-02-14 00:40:25 -05003964 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003965 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003966 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003967
3968 func = strsep(&next, ":");
3969
3970 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003971 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003972 if (!ret)
3973 ret = -EINVAL;
3974 if (ret < 0)
3975 return ret;
3976 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003977 }
3978
Steven Rostedtf6180772009-02-14 00:40:25 -05003979 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003980
3981 command = strsep(&next, ":");
3982
Steven Rostedtf6180772009-02-14 00:40:25 -05003983 mutex_lock(&ftrace_cmd_mutex);
3984 list_for_each_entry(p, &ftrace_commands, list) {
3985 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003986 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003987 goto out_unlock;
3988 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003989 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003990 out_unlock:
3991 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003992
Steven Rostedtf6180772009-02-14 00:40:25 -05003993 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003994}
3995
Ingo Molnare309b412008-05-12 21:20:51 +02003996static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003997ftrace_regex_write(struct file *file, const char __user *ubuf,
3998 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003999{
4000 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004001 struct trace_parser *parser;
4002 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004003
Li Zefan4ba79782009-09-22 13:52:20 +08004004 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004005 return 0;
4006
Steven Rostedt5072c592008-05-12 21:20:43 +02004007 if (file->f_mode & FMODE_READ) {
4008 struct seq_file *m = file->private_data;
4009 iter = m->private;
4010 } else
4011 iter = file->private_data;
4012
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004013 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004014 return -ENODEV;
4015
4016 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004017
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004018 parser = &iter->parser;
4019 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004020
Li Zefan4ba79782009-09-22 13:52:20 +08004021 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004022 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004023 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004024 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004025 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004026 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004027 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004028 }
4029
Steven Rostedt5072c592008-05-12 21:20:43 +02004030 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004031 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004032 return ret;
4033}
4034
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004035ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004036ftrace_filter_write(struct file *file, const char __user *ubuf,
4037 size_t cnt, loff_t *ppos)
4038{
4039 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4040}
4041
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004042ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004043ftrace_notrace_write(struct file *file, const char __user *ubuf,
4044 size_t cnt, loff_t *ppos)
4045{
4046 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4047}
4048
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004049static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004050ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4051{
4052 struct ftrace_func_entry *entry;
4053
4054 if (!ftrace_location(ip))
4055 return -EINVAL;
4056
4057 if (remove) {
4058 entry = ftrace_lookup_ip(hash, ip);
4059 if (!entry)
4060 return -ENOENT;
4061 free_hash_entry(hash, entry);
4062 return 0;
4063 }
4064
4065 return add_hash_entry(hash, ip);
4066}
4067
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004068static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004069 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004070{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004071 struct ftrace_ops *op;
4072
4073 if (!ftrace_enabled)
4074 return;
4075
4076 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004077 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004078 return;
4079 }
4080
4081 /*
4082 * If this is the shared global_ops filter, then we need to
4083 * check if there is another ops that shares it, is enabled.
4084 * If so, we still need to run the modify code.
4085 */
4086 if (ops->func_hash != &global_ops.local_hash)
4087 return;
4088
4089 do_for_each_ftrace_op(op, ftrace_ops_list) {
4090 if (op->func_hash == &global_ops.local_hash &&
4091 op->flags & FTRACE_OPS_FL_ENABLED) {
4092 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4093 /* Only need to do this once */
4094 return;
4095 }
4096 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004097}
4098
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004099static int
4100ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4101 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004102{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004103 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004104 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004105 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004106 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004107 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004108
Steven Rostedt41c52c02008-05-22 11:46:33 -04004109 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004110 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004111
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004112 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004113
Steven Rostedtf45948e2011-05-02 12:29:25 -04004114 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004115 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004116 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004117 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004118
Wang Nanb972cc52014-07-15 08:40:20 +08004119 if (reset)
4120 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4121 else
4122 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4123
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004124 if (!hash) {
4125 ret = -ENOMEM;
4126 goto out_regex_unlock;
4127 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004128
Jiri Olsaac483c42012-01-02 10:04:14 +01004129 if (buf && !ftrace_match_records(hash, buf, len)) {
4130 ret = -EINVAL;
4131 goto out_regex_unlock;
4132 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004133 if (ip) {
4134 ret = ftrace_match_addr(hash, ip, remove);
4135 if (ret < 0)
4136 goto out_regex_unlock;
4137 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004138
4139 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004140 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004141 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4142 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004143 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004144 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004145 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004146 free_ftrace_hash_rcu(old_hash);
4147 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004148 mutex_unlock(&ftrace_lock);
4149
Jiri Olsaac483c42012-01-02 10:04:14 +01004150 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004151 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004152
4153 free_ftrace_hash(hash);
4154 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004155}
4156
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004157static int
4158ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4159 int reset, int enable)
4160{
4161 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4162}
4163
4164/**
4165 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4166 * @ops - the ops to set the filter with
4167 * @ip - the address to add to or remove from the filter.
4168 * @remove - non zero to remove the ip from the filter
4169 * @reset - non zero to reset all filters before applying this filter.
4170 *
4171 * Filters denote which functions should be enabled when tracing is enabled
4172 * If @ip is NULL, it failes to update filter.
4173 */
4174int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4175 int remove, int reset)
4176{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004177 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004178 return ftrace_set_addr(ops, ip, remove, reset, 1);
4179}
4180EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4181
4182static int
4183ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4184 int reset, int enable)
4185{
4186 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4187}
4188
Steven Rostedt77a2b372008-05-12 21:20:45 +02004189/**
4190 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004191 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004192 * @buf - the string that holds the function filter text.
4193 * @len - the length of the string.
4194 * @reset - non zero to reset all filters before applying this filter.
4195 *
4196 * Filters denote which functions should be enabled when tracing is enabled.
4197 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4198 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004199int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004200 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004201{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004202 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004203 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004204}
Steven Rostedt936e0742011-05-05 22:54:01 -04004205EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004206
Steven Rostedt41c52c02008-05-22 11:46:33 -04004207/**
4208 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004209 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004210 * @buf - the string that holds the function notrace text.
4211 * @len - the length of the string.
4212 * @reset - non zero to reset all filters before applying this filter.
4213 *
4214 * Notrace Filters denote which functions should not be enabled when tracing
4215 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4216 * for tracing.
4217 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004218int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004219 int len, int reset)
4220{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004221 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004222 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004223}
4224EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4225/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004226 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004227 * @buf - the string that holds the function filter text.
4228 * @len - the length of the string.
4229 * @reset - non zero to reset all filters before applying this filter.
4230 *
4231 * Filters denote which functions should be enabled when tracing is enabled.
4232 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4233 */
4234void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4235{
4236 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4237}
4238EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4239
4240/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004241 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004242 * @buf - the string that holds the function notrace text.
4243 * @len - the length of the string.
4244 * @reset - non zero to reset all filters before applying this filter.
4245 *
4246 * Notrace Filters denote which functions should not be enabled when tracing
4247 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4248 * for tracing.
4249 */
4250void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004251{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004252 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004253}
Steven Rostedt936e0742011-05-05 22:54:01 -04004254EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004255
Steven Rostedt2af15d62009-05-28 13:37:24 -04004256/*
4257 * command line interface to allow users to set filters on boot up.
4258 */
4259#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4260static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4261static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4262
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004263/* Used by function selftest to not test if filter is set */
4264bool ftrace_filter_param __initdata;
4265
Steven Rostedt2af15d62009-05-28 13:37:24 -04004266static int __init set_ftrace_notrace(char *str)
4267{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004268 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004269 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004270 return 1;
4271}
4272__setup("ftrace_notrace=", set_ftrace_notrace);
4273
4274static int __init set_ftrace_filter(char *str)
4275{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004276 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004277 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004278 return 1;
4279}
4280__setup("ftrace_filter=", set_ftrace_filter);
4281
Stefan Assmann369bc182009-10-12 22:17:21 +02004282#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004283static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004284static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004285static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004286
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004287static unsigned long save_global_trampoline;
4288static unsigned long save_global_flags;
4289
Stefan Assmann369bc182009-10-12 22:17:21 +02004290static int __init set_graph_function(char *str)
4291{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004292 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004293 return 1;
4294}
4295__setup("ftrace_graph_filter=", set_graph_function);
4296
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004297static int __init set_graph_notrace_function(char *str)
4298{
4299 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4300 return 1;
4301}
4302__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4303
4304static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004305{
4306 int ret;
4307 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004308 unsigned long *table = ftrace_graph_funcs;
4309 int *count = &ftrace_graph_count;
4310
4311 if (!enable) {
4312 table = ftrace_graph_notrace_funcs;
4313 count = &ftrace_graph_notrace_count;
4314 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004315
4316 while (buf) {
4317 func = strsep(&buf, ",");
4318 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004319 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004320 if (ret)
4321 printk(KERN_DEBUG "ftrace: function %s not "
4322 "traceable\n", func);
4323 }
4324}
4325#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4326
Steven Rostedt2a85a372011-12-19 21:57:44 -05004327void __init
4328ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004329{
4330 char *func;
4331
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004332 ftrace_ops_init(ops);
4333
Steven Rostedt2af15d62009-05-28 13:37:24 -04004334 while (buf) {
4335 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004336 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004337 }
4338}
4339
4340static void __init set_ftrace_early_filters(void)
4341{
4342 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004343 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004344 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004345 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004346#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4347 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004348 set_ftrace_early_graph(ftrace_graph_buf, 1);
4349 if (ftrace_graph_notrace_buf[0])
4350 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004351#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004352}
4353
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004354int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004355{
4356 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004357 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004358 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004359 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004360 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004361 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004362 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004363 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004364
Steven Rostedt5072c592008-05-12 21:20:43 +02004365 if (file->f_mode & FMODE_READ) {
4366 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004367 seq_release(inode, file);
4368 } else
4369 iter = file->private_data;
4370
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004371 parser = &iter->parser;
4372 if (trace_parser_loaded(parser)) {
4373 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004374 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004375 }
4376
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004377 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004378
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004379 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004380
Steven Rostedt058e2972011-04-29 22:35:33 -04004381 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004382 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4383
4384 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004385 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004386 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004387 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004388
Steven Rostedt058e2972011-04-29 22:35:33 -04004389 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004390 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004391 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4392 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004393 ret = ftrace_hash_move(iter->ops, filter_hash,
4394 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004395 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004396 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004397 free_ftrace_hash_rcu(old_hash);
4398 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004399 mutex_unlock(&ftrace_lock);
4400 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004401
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004402 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004403 free_ftrace_hash(iter->hash);
4404 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004405
Steven Rostedt5072c592008-05-12 21:20:43 +02004406 return 0;
4407}
4408
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004409static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004410 .open = ftrace_avail_open,
4411 .read = seq_read,
4412 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004413 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004414};
4415
Steven Rostedt647bcd02011-05-03 14:39:21 -04004416static const struct file_operations ftrace_enabled_fops = {
4417 .open = ftrace_enabled_open,
4418 .read = seq_read,
4419 .llseek = seq_lseek,
4420 .release = seq_release_private,
4421};
4422
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004423static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004424 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004425 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004426 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004427 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004428 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004429};
4430
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004431static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004432 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004433 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004434 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004435 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004436 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004437};
4438
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004439#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4440
4441static DEFINE_MUTEX(graph_lock);
4442
4443int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004444int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004445unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004446unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004447
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004448struct ftrace_graph_data {
4449 unsigned long *table;
4450 size_t size;
4451 int *count;
4452 const struct seq_operations *seq_ops;
4453};
4454
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004455static void *
Li Zefan85951842009-06-24 09:54:00 +08004456__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004457{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004458 struct ftrace_graph_data *fgd = m->private;
4459
4460 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004461 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004462 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004463}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004464
Li Zefan85951842009-06-24 09:54:00 +08004465static void *
4466g_next(struct seq_file *m, void *v, loff_t *pos)
4467{
4468 (*pos)++;
4469 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004470}
4471
4472static void *g_start(struct seq_file *m, loff_t *pos)
4473{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004474 struct ftrace_graph_data *fgd = m->private;
4475
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004476 mutex_lock(&graph_lock);
4477
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004478 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004479 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004480 return (void *)1;
4481
Li Zefan85951842009-06-24 09:54:00 +08004482 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004483}
4484
4485static void g_stop(struct seq_file *m, void *p)
4486{
4487 mutex_unlock(&graph_lock);
4488}
4489
4490static int g_show(struct seq_file *m, void *v)
4491{
4492 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004493
4494 if (!ptr)
4495 return 0;
4496
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004497 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004498 struct ftrace_graph_data *fgd = m->private;
4499
4500 if (fgd->table == ftrace_graph_funcs)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004501 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004502 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004503 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004504 return 0;
4505 }
4506
Steven Rostedtb375a112009-09-17 00:05:58 -04004507 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004508
4509 return 0;
4510}
4511
James Morris88e9d342009-09-22 16:43:43 -07004512static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004513 .start = g_start,
4514 .next = g_next,
4515 .stop = g_stop,
4516 .show = g_show,
4517};
4518
4519static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004520__ftrace_graph_open(struct inode *inode, struct file *file,
4521 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004522{
4523 int ret = 0;
4524
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004525 mutex_lock(&graph_lock);
4526 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004527 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004528 *fgd->count = 0;
4529 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004530 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004531 mutex_unlock(&graph_lock);
4532
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004533 if (file->f_mode & FMODE_READ) {
4534 ret = seq_open(file, fgd->seq_ops);
4535 if (!ret) {
4536 struct seq_file *m = file->private_data;
4537 m->private = fgd;
4538 }
4539 } else
4540 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004541
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004542 return ret;
4543}
4544
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004545static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004546ftrace_graph_open(struct inode *inode, struct file *file)
4547{
4548 struct ftrace_graph_data *fgd;
4549
4550 if (unlikely(ftrace_disabled))
4551 return -ENODEV;
4552
4553 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4554 if (fgd == NULL)
4555 return -ENOMEM;
4556
4557 fgd->table = ftrace_graph_funcs;
4558 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4559 fgd->count = &ftrace_graph_count;
4560 fgd->seq_ops = &ftrace_graph_seq_ops;
4561
4562 return __ftrace_graph_open(inode, file, fgd);
4563}
4564
4565static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004566ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4567{
4568 struct ftrace_graph_data *fgd;
4569
4570 if (unlikely(ftrace_disabled))
4571 return -ENODEV;
4572
4573 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4574 if (fgd == NULL)
4575 return -ENOMEM;
4576
4577 fgd->table = ftrace_graph_notrace_funcs;
4578 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4579 fgd->count = &ftrace_graph_notrace_count;
4580 fgd->seq_ops = &ftrace_graph_seq_ops;
4581
4582 return __ftrace_graph_open(inode, file, fgd);
4583}
4584
4585static int
Li Zefan87827112009-07-23 11:29:11 +08004586ftrace_graph_release(struct inode *inode, struct file *file)
4587{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004588 if (file->f_mode & FMODE_READ) {
4589 struct seq_file *m = file->private_data;
4590
4591 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004592 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004593 } else {
4594 kfree(file->private_data);
4595 }
4596
Li Zefan87827112009-07-23 11:29:11 +08004597 return 0;
4598}
4599
4600static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004601ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004602{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004603 struct dyn_ftrace *rec;
4604 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004605 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004606 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004607 int type, not;
4608 char *search;
4609 bool exists;
4610 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004611
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004612 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004613 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004614 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004615 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004616
4617 search_len = strlen(search);
4618
Steven Rostedt52baf112009-02-14 01:15:39 -05004619 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004620
4621 if (unlikely(ftrace_disabled)) {
4622 mutex_unlock(&ftrace_lock);
4623 return -ENODEV;
4624 }
4625
Steven Rostedt265c8312009-02-13 12:43:56 -05004626 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004627
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004628 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004629 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004630 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004631 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004632 if (array[i] == rec->ip) {
4633 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004634 break;
4635 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004636 }
4637
4638 if (!not) {
4639 fail = 0;
4640 if (!exists) {
4641 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004642 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004643 goto out;
4644 }
4645 } else {
4646 if (exists) {
4647 array[i] = array[--(*idx)];
4648 array[*idx] = 0;
4649 fail = 0;
4650 }
4651 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004652 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004653 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004654out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004655 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004656
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004657 if (fail)
4658 return -EINVAL;
4659
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004660 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004661}
4662
4663static ssize_t
4664ftrace_graph_write(struct file *file, const char __user *ubuf,
4665 size_t cnt, loff_t *ppos)
4666{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004667 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004668 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004669 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004670
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004671 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004672 return 0;
4673
Namhyung Kim6a101082013-10-14 17:24:25 +09004674 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4675 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004676
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004677 read = trace_get_user(&parser, ubuf, cnt, ppos);
4678
Li Zefan4ba79782009-09-22 13:52:20 +08004679 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004680 parser.buffer[parser.idx] = 0;
4681
Namhyung Kim6a101082013-10-14 17:24:25 +09004682 mutex_lock(&graph_lock);
4683
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004684 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004685 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4686 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004687
4688 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004689 }
4690
Namhyung Kim6a101082013-10-14 17:24:25 +09004691 if (!ret)
4692 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004693
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004694 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004695
4696 return ret;
4697}
4698
4699static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004700 .open = ftrace_graph_open,
4701 .read = seq_read,
4702 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004703 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004704 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004705};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004706
4707static const struct file_operations ftrace_graph_notrace_fops = {
4708 .open = ftrace_graph_notrace_open,
4709 .read = seq_read,
4710 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004711 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004712 .release = ftrace_graph_release,
4713};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004714#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4715
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004716void ftrace_create_filter_files(struct ftrace_ops *ops,
4717 struct dentry *parent)
4718{
4719
4720 trace_create_file("set_ftrace_filter", 0644, parent,
4721 ops, &ftrace_filter_fops);
4722
4723 trace_create_file("set_ftrace_notrace", 0644, parent,
4724 ops, &ftrace_notrace_fops);
4725}
4726
4727/*
4728 * The name "destroy_filter_files" is really a misnomer. Although
4729 * in the future, it may actualy delete the files, but this is
4730 * really intended to make sure the ops passed in are disabled
4731 * and that when this function returns, the caller is free to
4732 * free the ops.
4733 *
4734 * The "destroy" name is only to match the "create" name that this
4735 * should be paired with.
4736 */
4737void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4738{
4739 mutex_lock(&ftrace_lock);
4740 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4741 ftrace_shutdown(ops, 0);
4742 ops->flags |= FTRACE_OPS_FL_DELETED;
4743 mutex_unlock(&ftrace_lock);
4744}
4745
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05004746static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004747{
Steven Rostedt5072c592008-05-12 21:20:43 +02004748
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004749 trace_create_file("available_filter_functions", 0444,
4750 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004751
Steven Rostedt647bcd02011-05-03 14:39:21 -04004752 trace_create_file("enabled_functions", 0444,
4753 d_tracer, NULL, &ftrace_enabled_fops);
4754
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004755 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004756
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004757#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004758 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004759 NULL,
4760 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004761 trace_create_file("set_graph_notrace", 0444, d_tracer,
4762 NULL,
4763 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004764#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4765
Steven Rostedt5072c592008-05-12 21:20:43 +02004766 return 0;
4767}
4768
Steven Rostedt9fd49322012-04-24 22:32:06 -04004769static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004770{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004771 const unsigned long *ipa = a;
4772 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004773
Steven Rostedt9fd49322012-04-24 22:32:06 -04004774 if (*ipa > *ipb)
4775 return 1;
4776 if (*ipa < *ipb)
4777 return -1;
4778 return 0;
4779}
4780
4781static void ftrace_swap_ips(void *a, void *b, int size)
4782{
4783 unsigned long *ipa = a;
4784 unsigned long *ipb = b;
4785 unsigned long t;
4786
4787 t = *ipa;
4788 *ipa = *ipb;
4789 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004790}
4791
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004792static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004793 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004794 unsigned long *end)
4795{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004796 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004797 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004798 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004799 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004800 unsigned long *p;
4801 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004802 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004803 int ret = -ENOMEM;
4804
4805 count = end - start;
4806
4807 if (!count)
4808 return 0;
4809
Steven Rostedt9fd49322012-04-24 22:32:06 -04004810 sort(start, count, sizeof(*start),
4811 ftrace_cmp_ips, ftrace_swap_ips);
4812
Steven Rostedt706c81f2012-04-24 23:45:26 -04004813 start_pg = ftrace_allocate_pages(count);
4814 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004815 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004816
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004817 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004818
Steven Rostedt320823092011-12-16 14:42:37 -05004819 /*
4820 * Core and each module needs their own pages, as
4821 * modules will free them when they are removed.
4822 * Force a new page to be allocated for modules.
4823 */
Steven Rostedta7900872011-12-16 16:23:44 -05004824 if (!mod) {
4825 WARN_ON(ftrace_pages || ftrace_pages_start);
4826 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004827 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004828 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004829 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004830 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004831
Steven Rostedta7900872011-12-16 16:23:44 -05004832 if (WARN_ON(ftrace_pages->next)) {
4833 /* Hmm, we have free pages? */
4834 while (ftrace_pages->next)
4835 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004836 }
Steven Rostedta7900872011-12-16 16:23:44 -05004837
Steven Rostedt706c81f2012-04-24 23:45:26 -04004838 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004839 }
4840
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004841 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004842 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004843 while (p < end) {
4844 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004845 /*
4846 * Some architecture linkers will pad between
4847 * the different mcount_loc sections of different
4848 * object files to satisfy alignments.
4849 * Skip any NULL pointers.
4850 */
4851 if (!addr)
4852 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004853
4854 if (pg->index == pg->size) {
4855 /* We should have allocated enough */
4856 if (WARN_ON(!pg->next))
4857 break;
4858 pg = pg->next;
4859 }
4860
4861 rec = &pg->records[pg->index++];
4862 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004863 }
4864
Steven Rostedt706c81f2012-04-24 23:45:26 -04004865 /* We should have used all pages */
4866 WARN_ON(pg->next);
4867
4868 /* Assign the last page to ftrace_pages */
4869 ftrace_pages = pg;
4870
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004871 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004872 * We only need to disable interrupts on start up
4873 * because we are modifying code that an interrupt
4874 * may execute, and the modification is not atomic.
4875 * But for modules, nothing runs the code we modify
4876 * until we are finished with it, and there's no
4877 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004878 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004879 if (!mod)
4880 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004881 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004882 if (!mod)
4883 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004884 ret = 0;
4885 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004886 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004887
Steven Rostedta7900872011-12-16 16:23:44 -05004888 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004889}
4890
Steven Rostedt93eb6772009-04-15 13:24:06 -04004891#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004892
4893#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4894
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004895void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004896{
4897 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004898 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004899 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004900 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004901
Steven Rostedt93eb6772009-04-15 13:24:06 -04004902 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004903
4904 if (ftrace_disabled)
4905 goto out_unlock;
4906
Steven Rostedt320823092011-12-16 14:42:37 -05004907 /*
4908 * Each module has its own ftrace_pages, remove
4909 * them from the list.
4910 */
4911 last_pg = &ftrace_pages_start;
4912 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4913 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004914 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004915 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004916 * As core pages are first, the first
4917 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004918 */
Steven Rostedt320823092011-12-16 14:42:37 -05004919 if (WARN_ON(pg == ftrace_pages_start))
4920 goto out_unlock;
4921
4922 /* Check if we are deleting the last page */
4923 if (pg == ftrace_pages)
4924 ftrace_pages = next_to_ftrace_page(last_pg);
4925
4926 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004927 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4928 free_pages((unsigned long)pg->records, order);
4929 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004930 } else
4931 last_pg = &pg->next;
4932 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004933 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004934 mutex_unlock(&ftrace_lock);
4935}
4936
4937static void ftrace_init_module(struct module *mod,
4938 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004939{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004940 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004941 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004942 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004943}
4944
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004945void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004946{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004947 ftrace_init_module(mod, mod->ftrace_callsites,
4948 mod->ftrace_callsites +
4949 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004950}
4951
4952static int ftrace_module_notify_exit(struct notifier_block *self,
4953 unsigned long val, void *data)
4954{
4955 struct module *mod = data;
4956
4957 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004958 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004959
4960 return 0;
4961}
4962#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004963static int ftrace_module_notify_exit(struct notifier_block *self,
4964 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004965{
4966 return 0;
4967}
4968#endif /* CONFIG_MODULES */
4969
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004970struct notifier_block ftrace_module_exit_nb = {
4971 .notifier_call = ftrace_module_notify_exit,
4972 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4973};
4974
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004975void __init ftrace_init(void)
4976{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004977 extern unsigned long __start_mcount_loc[];
4978 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004979 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004980 int ret;
4981
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004982 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004983 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004984 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004985 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004986 goto failed;
4987
4988 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004989 if (!count) {
4990 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004991 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004992 }
4993
4994 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4995 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004996
4997 last_ftrace_enabled = ftrace_enabled = 1;
4998
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004999 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005000 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005001 __stop_mcount_loc);
5002
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005003 ret = register_module_notifier(&ftrace_module_exit_nb);
5004 if (ret)
5005 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04005006
Steven Rostedt2af15d62009-05-28 13:37:24 -04005007 set_ftrace_early_filters();
5008
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005009 return;
5010 failed:
5011 ftrace_disabled = 1;
5012}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005013
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005014/* Do nothing if arch does not support this */
5015void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5016{
5017}
5018
5019static void ftrace_update_trampoline(struct ftrace_ops *ops)
5020{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005021
5022/*
5023 * Currently there's no safe way to free a trampoline when the kernel
5024 * is configured with PREEMPT. That is because a task could be preempted
5025 * when it jumped to the trampoline, it may be preempted for a long time
5026 * depending on the system load, and currently there's no way to know
5027 * when it will be off the trampoline. If the trampoline is freed
5028 * too early, when the task runs again, it will be executing on freed
5029 * memory and crash.
5030 */
5031#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005032 /* Currently, only non dynamic ops can have a trampoline */
5033 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5034 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005035#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005036
5037 arch_ftrace_update_trampoline(ops);
5038}
5039
Steven Rostedt3d083392008-05-12 21:20:42 +02005040#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005041
Steven Rostedt2b499382011-05-03 22:49:52 -04005042static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005043 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005044 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5045 FTRACE_OPS_FL_INITIALIZED |
5046 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005047};
5048
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005049static int __init ftrace_nodyn_init(void)
5050{
5051 ftrace_enabled = 1;
5052 return 0;
5053}
Steven Rostedt6f415672012-10-05 12:13:07 -04005054core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005055
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005056static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005057static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005058static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005059/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005060# define ftrace_startup(ops, command) \
5061 ({ \
5062 int ___ret = __register_ftrace_function(ops); \
5063 if (!___ret) \
5064 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5065 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005066 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005067# define ftrace_shutdown(ops, command) \
5068 ({ \
5069 int ___ret = __unregister_ftrace_function(ops); \
5070 if (!___ret) \
5071 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5072 ___ret; \
5073 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005074
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005075# define ftrace_startup_sysctl() do { } while (0)
5076# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005077
5078static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005079ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005080{
5081 return 1;
5082}
5083
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005084static void ftrace_update_trampoline(struct ftrace_ops *ops)
5085{
5086}
5087
Steven Rostedt3d083392008-05-12 21:20:42 +02005088#endif /* CONFIG_DYNAMIC_FTRACE */
5089
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005090__init void ftrace_init_global_array_ops(struct trace_array *tr)
5091{
5092 tr->ops = &global_ops;
5093 tr->ops->private = tr;
5094}
5095
5096void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5097{
5098 /* If we filter on pids, update to use the pid function */
5099 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5100 if (WARN_ON(tr->ops->func != ftrace_stub))
5101 printk("ftrace ops had %pS for function\n",
5102 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005103 }
5104 tr->ops->func = func;
5105 tr->ops->private = tr;
5106}
5107
5108void ftrace_reset_array_ops(struct trace_array *tr)
5109{
5110 tr->ops->func = ftrace_stub;
5111}
5112
Steven Rostedtb8489142011-05-04 09:27:52 -04005113static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005114ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005115 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01005116{
Jiri Olsae2484912012-02-15 15:51:48 +01005117 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
5118 return;
5119
5120 /*
5121 * Some of the ops may be dynamically allocated,
5122 * they must be freed after a synchronize_sched().
5123 */
5124 preempt_disable_notrace();
5125 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05005126
5127 /*
5128 * Control funcs (perf) uses RCU. Only trace if
5129 * RCU is currently active.
5130 */
5131 if (!rcu_is_watching())
5132 goto out;
5133
Steven Rostedt0a016402012-11-02 17:03:03 -04005134 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04005135 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
5136 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005137 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04005138 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04005139 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05005140 out:
Jiri Olsae2484912012-02-15 15:51:48 +01005141 trace_recursion_clear(TRACE_CONTROL_BIT);
5142 preempt_enable_notrace();
5143}
5144
5145static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005146 .func = ftrace_ops_control_func,
5147 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005148 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01005149};
5150
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005151static inline void
5152__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005153 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005154{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005155 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005156 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005157
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005158 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5159 if (bit < 0)
5160 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005161
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005162 /*
5163 * Some of the ops may be dynamically allocated,
5164 * they must be freed after a synchronize_sched().
5165 */
5166 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04005167 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005168 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005169 if (FTRACE_WARN_ON(!op->func)) {
5170 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005171 goto out;
5172 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005173 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005174 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005175 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005176out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005177 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005178 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005179}
5180
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005181/*
5182 * Some archs only support passing ip and parent_ip. Even though
5183 * the list function ignores the op parameter, we do not want any
5184 * C side effects, where a function is called without the caller
5185 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005186 * Archs are to support both the regs and ftrace_ops at the same time.
5187 * If they support ftrace_ops, it is assumed they support regs.
5188 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005189 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5190 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005191 * An architecture can pass partial regs with ftrace_ops and still
5192 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005193 */
5194#if ARCH_SUPPORTS_FTRACE_OPS
5195static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005196 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005197{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005198 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005199}
5200#else
5201static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5202{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005203 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005204}
5205#endif
5206
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005207/*
5208 * If there's only one function registered but it does not support
5209 * recursion, this function will be called by the mcount trampoline.
5210 * This function will handle recursion protection.
5211 */
5212static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
5213 struct ftrace_ops *op, struct pt_regs *regs)
5214{
5215 int bit;
5216
5217 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5218 if (bit < 0)
5219 return;
5220
5221 op->func(ip, parent_ip, op, regs);
5222
5223 trace_clear_recursion(bit);
5224}
5225
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005226/**
5227 * ftrace_ops_get_func - get the function a trampoline should call
5228 * @ops: the ops to get the function for
5229 *
5230 * Normally the mcount trampoline will call the ops->func, but there
5231 * are times that it should not. For example, if the ops does not
5232 * have its own recursion protection, then it should call the
5233 * ftrace_ops_recurs_func() instead.
5234 *
5235 * Returns the function that the trampoline should call for @ops.
5236 */
5237ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5238{
5239 /*
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005240 * If the func handles its own recursion, call it directly.
5241 * Otherwise call the recursion protected function that
5242 * will call the ftrace ops function.
5243 */
5244 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
5245 return ftrace_ops_recurs_func;
5246
5247 return ops->func;
5248}
5249
Steven Rostedte32d8952008-12-04 00:26:41 -05005250static void clear_ftrace_swapper(void)
5251{
5252 struct task_struct *p;
5253 int cpu;
5254
5255 get_online_cpus();
5256 for_each_online_cpu(cpu) {
5257 p = idle_task(cpu);
5258 clear_tsk_trace_trace(p);
5259 }
5260 put_online_cpus();
5261}
5262
5263static void set_ftrace_swapper(void)
5264{
5265 struct task_struct *p;
5266 int cpu;
5267
5268 get_online_cpus();
5269 for_each_online_cpu(cpu) {
5270 p = idle_task(cpu);
5271 set_tsk_trace_trace(p);
5272 }
5273 put_online_cpus();
5274}
5275
5276static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005277{
5278 struct task_struct *p;
5279
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005280 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05005281 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05005282 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05005283 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005284 rcu_read_unlock();
5285
Steven Rostedte32d8952008-12-04 00:26:41 -05005286 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005287}
5288
Steven Rostedte32d8952008-12-04 00:26:41 -05005289static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005290{
5291 struct task_struct *p;
5292
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005293 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005294 do_each_pid_task(pid, PIDTYPE_PID, p) {
5295 set_tsk_trace_trace(p);
5296 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005297 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005298}
5299
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005300static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005301{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005302 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005303 clear_ftrace_swapper();
5304 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005305 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05005306}
5307
5308static void set_ftrace_pid_task(struct pid *pid)
5309{
5310 if (pid == ftrace_swapper_pid)
5311 set_ftrace_swapper();
5312 else
5313 set_ftrace_pid(pid);
5314}
5315
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005316static int ftrace_pid_add(int p)
5317{
5318 struct pid *pid;
5319 struct ftrace_pid *fpid;
5320 int ret = -EINVAL;
5321
5322 mutex_lock(&ftrace_lock);
5323
5324 if (!p)
5325 pid = ftrace_swapper_pid;
5326 else
5327 pid = find_get_pid(p);
5328
5329 if (!pid)
5330 goto out;
5331
5332 ret = 0;
5333
5334 list_for_each_entry(fpid, &ftrace_pids, list)
5335 if (fpid->pid == pid)
5336 goto out_put;
5337
5338 ret = -ENOMEM;
5339
5340 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5341 if (!fpid)
5342 goto out_put;
5343
5344 list_add(&fpid->list, &ftrace_pids);
5345 fpid->pid = pid;
5346
5347 set_ftrace_pid_task(pid);
5348
5349 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005350
5351 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005352
5353 mutex_unlock(&ftrace_lock);
5354 return 0;
5355
5356out_put:
5357 if (pid != ftrace_swapper_pid)
5358 put_pid(pid);
5359
5360out:
5361 mutex_unlock(&ftrace_lock);
5362 return ret;
5363}
5364
5365static void ftrace_pid_reset(void)
5366{
5367 struct ftrace_pid *fpid, *safe;
5368
5369 mutex_lock(&ftrace_lock);
5370 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5371 struct pid *pid = fpid->pid;
5372
5373 clear_ftrace_pid_task(pid);
5374
5375 list_del(&fpid->list);
5376 kfree(fpid);
5377 }
5378
5379 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005380 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005381
5382 mutex_unlock(&ftrace_lock);
5383}
5384
5385static void *fpid_start(struct seq_file *m, loff_t *pos)
5386{
5387 mutex_lock(&ftrace_lock);
5388
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005389 if (!ftrace_pids_enabled() && (!*pos))
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005390 return (void *) 1;
5391
5392 return seq_list_start(&ftrace_pids, *pos);
5393}
5394
5395static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5396{
5397 if (v == (void *)1)
5398 return NULL;
5399
5400 return seq_list_next(v, &ftrace_pids, pos);
5401}
5402
5403static void fpid_stop(struct seq_file *m, void *p)
5404{
5405 mutex_unlock(&ftrace_lock);
5406}
5407
5408static int fpid_show(struct seq_file *m, void *v)
5409{
5410 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5411
5412 if (v == (void *)1) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005413 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005414 return 0;
5415 }
5416
5417 if (fpid->pid == ftrace_swapper_pid)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005418 seq_puts(m, "swapper tasks\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005419 else
5420 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5421
5422 return 0;
5423}
5424
5425static const struct seq_operations ftrace_pid_sops = {
5426 .start = fpid_start,
5427 .next = fpid_next,
5428 .stop = fpid_stop,
5429 .show = fpid_show,
5430};
5431
5432static int
5433ftrace_pid_open(struct inode *inode, struct file *file)
5434{
5435 int ret = 0;
5436
5437 if ((file->f_mode & FMODE_WRITE) &&
5438 (file->f_flags & O_TRUNC))
5439 ftrace_pid_reset();
5440
5441 if (file->f_mode & FMODE_READ)
5442 ret = seq_open(file, &ftrace_pid_sops);
5443
5444 return ret;
5445}
5446
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005447static ssize_t
5448ftrace_pid_write(struct file *filp, const char __user *ubuf,
5449 size_t cnt, loff_t *ppos)
5450{
Ingo Molnar457dc922009-11-23 11:03:28 +01005451 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005452 long val;
5453 int ret;
5454
5455 if (cnt >= sizeof(buf))
5456 return -EINVAL;
5457
5458 if (copy_from_user(&buf, ubuf, cnt))
5459 return -EFAULT;
5460
5461 buf[cnt] = 0;
5462
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005463 /*
5464 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5465 * to clean the filter quietly.
5466 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005467 tmp = strstrip(buf);
5468 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005469 return 1;
5470
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005471 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005472 if (ret < 0)
5473 return ret;
5474
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005475 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005476
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005477 return ret ? ret : cnt;
5478}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005479
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005480static int
5481ftrace_pid_release(struct inode *inode, struct file *file)
5482{
5483 if (file->f_mode & FMODE_READ)
5484 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005485
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005486 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005487}
5488
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005489static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005490 .open = ftrace_pid_open,
5491 .write = ftrace_pid_write,
5492 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005493 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005494 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005495};
5496
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005497static __init int ftrace_init_tracefs(void)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005498{
5499 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005500
5501 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05005502 if (IS_ERR(d_tracer))
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005503 return 0;
5504
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005505 ftrace_init_dyn_tracefs(d_tracer);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005506
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005507 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5508 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005509
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005510 ftrace_profile_tracefs(d_tracer);
Steven Rostedt493762f2009-03-23 17:12:36 -04005511
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005512 return 0;
5513}
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005514fs_initcall(ftrace_init_tracefs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005515
Steven Rostedt3d083392008-05-12 21:20:42 +02005516/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005517 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005518 *
5519 * This function should be used by panic code. It stops ftrace
5520 * but in a not so nice way. If you need to simply kill ftrace
5521 * from a non-atomic section, use ftrace_kill.
5522 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005523void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005524{
5525 ftrace_disabled = 1;
5526 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005527 clear_ftrace_function();
5528}
5529
5530/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005531 * Test if ftrace is dead or not.
5532 */
5533int ftrace_is_dead(void)
5534{
5535 return ftrace_disabled;
5536}
5537
5538/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005539 * register_ftrace_function - register a function for profiling
5540 * @ops - ops structure that holds the function for profiling.
5541 *
5542 * Register a function to be called by all functions in the
5543 * kernel.
5544 *
5545 * Note: @ops->func and all the functions it calls must be labeled
5546 * with "notrace", otherwise it will go into a
5547 * recursive loop.
5548 */
5549int register_ftrace_function(struct ftrace_ops *ops)
5550{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005551 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005552
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005553 ftrace_ops_init(ops);
5554
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005555 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005556
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005557 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005558
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005559 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005560
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005561 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005562}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005563EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005564
5565/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005566 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005567 * @ops - ops structure that holds the function to unregister
5568 *
5569 * Unregister a function that was added to be called by ftrace profiling.
5570 */
5571int unregister_ftrace_function(struct ftrace_ops *ops)
5572{
5573 int ret;
5574
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005575 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005576 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005577 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005578
5579 return ret;
5580}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005581EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005582
Ingo Molnare309b412008-05-12 21:20:51 +02005583int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005584ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005585 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005586 loff_t *ppos)
5587{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005588 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005589
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005590 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005591
Steven Rostedt45a4a232011-04-21 23:16:46 -04005592 if (unlikely(ftrace_disabled))
5593 goto out;
5594
5595 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005596
Li Zefana32c7762009-06-26 16:55:51 +08005597 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005598 goto out;
5599
Li Zefana32c7762009-06-26 16:55:51 +08005600 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005601
5602 if (ftrace_enabled) {
5603
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005604 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005605 if (ftrace_ops_list != &ftrace_list_end)
5606 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005607
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05005608 ftrace_startup_sysctl();
5609
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005610 } else {
5611 /* stopping ftrace calls (just send to ftrace_stub) */
5612 ftrace_trace_function = ftrace_stub;
5613
5614 ftrace_shutdown_sysctl();
5615 }
5616
5617 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005618 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005619 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005620}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005621
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005622#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005623
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005624static struct ftrace_ops graph_ops = {
5625 .func = ftrace_stub,
5626 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5627 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005628 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005629 FTRACE_OPS_FL_STUB,
5630#ifdef FTRACE_GRAPH_TRAMP_ADDR
5631 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05005632 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005633#endif
5634 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5635};
5636
Steven Rostedte49dc192008-12-02 23:50:05 -05005637int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5638{
5639 return 0;
5640}
5641
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005642/* The callbacks that hook a function */
5643trace_func_graph_ret_t ftrace_graph_return =
5644 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005645trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005646static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005647
5648/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5649static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5650{
5651 int i;
5652 int ret = 0;
5653 unsigned long flags;
5654 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5655 struct task_struct *g, *t;
5656
5657 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5658 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5659 * sizeof(struct ftrace_ret_stack),
5660 GFP_KERNEL);
5661 if (!ret_stack_list[i]) {
5662 start = 0;
5663 end = i;
5664 ret = -ENOMEM;
5665 goto free;
5666 }
5667 }
5668
5669 read_lock_irqsave(&tasklist_lock, flags);
5670 do_each_thread(g, t) {
5671 if (start == end) {
5672 ret = -EAGAIN;
5673 goto unlock;
5674 }
5675
5676 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005677 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005678 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005679 t->curr_ret_stack = -1;
5680 /* Make sure the tasks see the -1 first: */
5681 smp_wmb();
5682 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005683 }
5684 } while_each_thread(g, t);
5685
5686unlock:
5687 read_unlock_irqrestore(&tasklist_lock, flags);
5688free:
5689 for (i = start; i < end; i++)
5690 kfree(ret_stack_list[i]);
5691 return ret;
5692}
5693
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005694static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005695ftrace_graph_probe_sched_switch(void *ignore,
5696 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005697{
5698 unsigned long long timestamp;
5699 int index;
5700
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005701 /*
5702 * Does the user want to count the time a function was asleep.
5703 * If so, do not update the time stamps.
5704 */
5705 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5706 return;
5707
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005708 timestamp = trace_clock_local();
5709
5710 prev->ftrace_timestamp = timestamp;
5711
5712 /* only process tasks that we timestamped */
5713 if (!next->ftrace_timestamp)
5714 return;
5715
5716 /*
5717 * Update all the counters in next to make up for the
5718 * time next was sleeping.
5719 */
5720 timestamp -= next->ftrace_timestamp;
5721
5722 for (index = next->curr_ret_stack; index >= 0; index--)
5723 next->ret_stack[index].calltime += timestamp;
5724}
5725
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005726/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005727static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005728{
5729 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005730 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005731
5732 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5733 sizeof(struct ftrace_ret_stack *),
5734 GFP_KERNEL);
5735
5736 if (!ret_stack_list)
5737 return -ENOMEM;
5738
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005739 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005740 for_each_online_cpu(cpu) {
5741 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005742 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005743 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005744
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005745 do {
5746 ret = alloc_retstack_tasklist(ret_stack_list);
5747 } while (ret == -EAGAIN);
5748
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005749 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005750 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005751 if (ret)
5752 pr_info("ftrace_graph: Couldn't activate tracepoint"
5753 " probe to kernel_sched_switch\n");
5754 }
5755
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005756 kfree(ret_stack_list);
5757 return ret;
5758}
5759
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005760/*
5761 * Hibernation protection.
5762 * The state of the current task is too much unstable during
5763 * suspend/restore to disk. We want to protect against that.
5764 */
5765static int
5766ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5767 void *unused)
5768{
5769 switch (state) {
5770 case PM_HIBERNATION_PREPARE:
5771 pause_graph_tracing();
5772 break;
5773
5774 case PM_POST_HIBERNATION:
5775 unpause_graph_tracing();
5776 break;
5777 }
5778 return NOTIFY_DONE;
5779}
5780
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005781static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5782{
5783 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5784 return 0;
5785 return __ftrace_graph_entry(trace);
5786}
5787
5788/*
5789 * The function graph tracer should only trace the functions defined
5790 * by set_ftrace_filter and set_ftrace_notrace. If another function
5791 * tracer ops is registered, the graph tracer requires testing the
5792 * function against the global ops, and not just trace any function
5793 * that any ftrace_ops registered.
5794 */
5795static void update_function_graph_func(void)
5796{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005797 struct ftrace_ops *op;
5798 bool do_test = false;
5799
5800 /*
5801 * The graph and global ops share the same set of functions
5802 * to test. If any other ops is on the list, then
5803 * the graph tracing needs to test if its the function
5804 * it should call.
5805 */
5806 do_for_each_ftrace_op(op, ftrace_ops_list) {
5807 if (op != &global_ops && op != &graph_ops &&
5808 op != &ftrace_list_end) {
5809 do_test = true;
5810 /* in double loop, break out with goto */
5811 goto out;
5812 }
5813 } while_for_each_ftrace_op(op);
5814 out:
5815 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005816 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005817 else
5818 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005819}
5820
Mathias Krause8275f692014-03-30 15:31:50 +02005821static struct notifier_block ftrace_suspend_notifier = {
5822 .notifier_call = ftrace_suspend_notifier_call,
5823};
5824
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005825int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5826 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005827{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005828 int ret = 0;
5829
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005830 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005831
Steven Rostedt05ce5812009-03-24 00:18:31 -04005832 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005833 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005834 ret = -EBUSY;
5835 goto out;
5836 }
5837
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005838 register_pm_notifier(&ftrace_suspend_notifier);
5839
Steven Rostedt597af812009-04-03 15:24:12 -04005840 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005841 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005842 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005843 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005844 goto out;
5845 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005846
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005847 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005848
5849 /*
5850 * Update the indirect function to the entryfunc, and the
5851 * function that gets called to the entry_test first. Then
5852 * call the update fgraph entry function to determine if
5853 * the entryfunc should be called directly or not.
5854 */
5855 __ftrace_graph_entry = entryfunc;
5856 ftrace_graph_entry = ftrace_graph_entry_test;
5857 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005858
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005859 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005860out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005861 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005862 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005863}
5864
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005865void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005866{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005867 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005868
Steven Rostedt597af812009-04-03 15:24:12 -04005869 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005870 goto out;
5871
Steven Rostedt597af812009-04-03 15:24:12 -04005872 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005873 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005874 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005875 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005876 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005877 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005878 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005879
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005880#ifdef CONFIG_DYNAMIC_FTRACE
5881 /*
5882 * Function graph does not allocate the trampoline, but
5883 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5884 * if one was used.
5885 */
5886 global_ops.trampoline = save_global_trampoline;
5887 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5888 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5889#endif
5890
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005891 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005892 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005893}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005894
Steven Rostedt868baf02011-02-10 21:26:13 -05005895static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5896
5897static void
5898graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5899{
5900 atomic_set(&t->tracing_graph_pause, 0);
5901 atomic_set(&t->trace_overrun, 0);
5902 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005903 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005904 smp_wmb();
5905 t->ret_stack = ret_stack;
5906}
5907
5908/*
5909 * Allocate a return stack for the idle task. May be the first
5910 * time through, or it may be done by CPU hotplug online.
5911 */
5912void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5913{
5914 t->curr_ret_stack = -1;
5915 /*
5916 * The idle task has no parent, it either has its own
5917 * stack or no stack at all.
5918 */
5919 if (t->ret_stack)
5920 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5921
5922 if (ftrace_graph_active) {
5923 struct ftrace_ret_stack *ret_stack;
5924
5925 ret_stack = per_cpu(idle_ret_stack, cpu);
5926 if (!ret_stack) {
5927 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5928 * sizeof(struct ftrace_ret_stack),
5929 GFP_KERNEL);
5930 if (!ret_stack)
5931 return;
5932 per_cpu(idle_ret_stack, cpu) = ret_stack;
5933 }
5934 graph_init_task(t, ret_stack);
5935 }
5936}
5937
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005938/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005939void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005940{
Steven Rostedt84047e32009-06-02 16:51:55 -04005941 /* Make sure we do not use the parent ret_stack */
5942 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005943 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005944
Steven Rostedt597af812009-04-03 15:24:12 -04005945 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005946 struct ftrace_ret_stack *ret_stack;
5947
5948 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005949 * sizeof(struct ftrace_ret_stack),
5950 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005951 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005952 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005953 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005954 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005955}
5956
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005957void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005958{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005959 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5960
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005961 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005962 /* NULL must become visible to IRQs before we free it: */
5963 barrier();
5964
5965 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005966}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005967#endif