blob: 4c61f28a08e051108f1794d1ad0fc78631d7e5a2 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -050065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
Jiri Olsae2484912012-02-15 15:51:48 +010066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
68#define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70#else
71#define INIT_REGEX_LOCK(opsname)
72#endif
73
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040074static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040076 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/* ftrace_enabled is a method to turn ftrace on or off */
80int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020081static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040083/* Current function tracing op */
84struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050085/* What to set function_trace_op to */
86static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050087
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040088/* List for set_ftrace_pid's pids. */
89LIST_HEAD(ftrace_pids);
90struct ftrace_pid {
91 struct list_head list;
92 struct pid *pid;
93};
94
Steven Rostedt4eebcc82008-05-12 21:20:48 +020095/*
96 * ftrace_disabled is set when an anomaly is discovered.
97 * ftrace_disabled is much stronger than ftrace_enabled.
98 */
99static int ftrace_disabled __read_mostly;
100
Steven Rostedt52baf112009-02-14 01:15:39 -0500101static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200102
Jiri Olsae2484912012-02-15 15:51:48 +0100103static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400104static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200105ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500106ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400107static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100108static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400110#if ARCH_SUPPORTS_FTRACE_OPS
111static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400112 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400113#else
114/* See comment below, where ftrace_ops_list_func is defined */
115static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
116#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
117#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400118
Steven Rostedt0a016402012-11-02 17:03:03 -0400119/*
120 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400121 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400122 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400123 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400124 * concurrent insertions into the ftrace_global_list.
125 *
126 * Silly Alpha and silly pointer-speculation compiler optimizations!
127 */
128#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400129 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400130 do
131
132/*
133 * Optimized for just a single item in the list (as that is the normal case).
134 */
135#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400136 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400137 unlikely((op) != &ftrace_list_end))
138
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900139static inline void ftrace_ops_init(struct ftrace_ops *ops)
140{
141#ifdef CONFIG_DYNAMIC_FTRACE
142 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
143 mutex_init(&ops->regex_lock);
144 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
145 }
146#endif
147}
148
Steven Rostedtea701f12012-07-20 13:08:05 -0400149/**
150 * ftrace_nr_registered_ops - return number of ops registered
151 *
152 * Returns the number of ftrace_ops registered and tracing functions
153 */
154int ftrace_nr_registered_ops(void)
155{
156 struct ftrace_ops *ops;
157 int cnt = 0;
158
159 mutex_lock(&ftrace_lock);
160
161 for (ops = ftrace_ops_list;
162 ops != &ftrace_list_end; ops = ops->next)
163 cnt++;
164
165 mutex_unlock(&ftrace_lock);
166
167 return cnt;
168}
169
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400170static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400171 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500172{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500173 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500174 return;
175
Steven Rostedta1e2e312011-08-09 12:50:46 -0400176 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500177}
178
179static void set_ftrace_pid_function(ftrace_func_t func)
180{
181 /* do not set ftrace_pid_function to itself! */
182 if (func != ftrace_pid_func)
183 ftrace_pid_function = func;
184}
185
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200186/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200187 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200188 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200189 * This NULLs the ftrace function and in essence stops
190 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200192void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200193{
Steven Rostedt3d083392008-05-12 21:20:42 +0200194 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500195 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200196}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197
Jiri Olsae2484912012-02-15 15:51:48 +0100198static void control_ops_disable_all(struct ftrace_ops *ops)
199{
200 int cpu;
201
202 for_each_possible_cpu(cpu)
203 *per_cpu_ptr(ops->disabled, cpu) = 1;
204}
205
206static int control_ops_alloc(struct ftrace_ops *ops)
207{
208 int __percpu *disabled;
209
210 disabled = alloc_percpu(int);
211 if (!disabled)
212 return -ENOMEM;
213
214 ops->disabled = disabled;
215 control_ops_disable_all(ops);
216 return 0;
217}
218
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500219static void ftrace_sync(struct work_struct *work)
220{
221 /*
222 * This function is just a stub to implement a hard force
223 * of synchronize_sched(). This requires synchronizing
224 * tasks even in userspace and idle.
225 *
226 * Yes, function tracing is rude.
227 */
228}
229
230static void ftrace_sync_ipi(void *data)
231{
232 /* Probably not needed, but do it anyway */
233 smp_rmb();
234}
235
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500236#ifdef CONFIG_FUNCTION_GRAPH_TRACER
237static void update_function_graph_func(void);
238#else
239static inline void update_function_graph_func(void) { }
240#endif
241
Steven Rostedt2b499382011-05-03 22:49:52 -0400242static void update_ftrace_function(void)
243{
244 ftrace_func_t func;
245
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400246 /*
247 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400248 * recursion safe and not dynamic and the arch supports passing ops,
249 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400250 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400251 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400252 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400253 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400254 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400255 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400256 /* Set the ftrace_ops that the arch callback uses */
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500257 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400258 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400259 } else {
260 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500261 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400262 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400263 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400264
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500265 /* If there's no change, then do nothing more here */
266 if (ftrace_trace_function == func)
267 return;
268
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500269 update_function_graph_func();
270
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500271 /*
272 * If we are using the list function, it doesn't care
273 * about the function_trace_ops.
274 */
275 if (func == ftrace_ops_list_func) {
276 ftrace_trace_function = func;
277 /*
278 * Don't even bother setting function_trace_ops,
279 * it would be racy to do so anyway.
280 */
281 return;
282 }
283
284#ifndef CONFIG_DYNAMIC_FTRACE
285 /*
286 * For static tracing, we need to be a bit more careful.
287 * The function change takes affect immediately. Thus,
288 * we need to coorditate the setting of the function_trace_ops
289 * with the setting of the ftrace_trace_function.
290 *
291 * Set the function to the list ops, which will call the
292 * function we want, albeit indirectly, but it handles the
293 * ftrace_ops and doesn't depend on function_trace_op.
294 */
295 ftrace_trace_function = ftrace_ops_list_func;
296 /*
297 * Make sure all CPUs see this. Yes this is slow, but static
298 * tracing is slow and nasty to have enabled.
299 */
300 schedule_on_each_cpu(ftrace_sync);
301 /* Now all cpus are using the list ops. */
302 function_trace_op = set_function_trace_op;
303 /* Make sure the function_trace_op is visible on all CPUs */
304 smp_wmb();
305 /* Nasty way to force a rmb on all cpus */
306 smp_call_function(ftrace_sync_ipi, NULL, 1);
307 /* OK, we are all set to update the ftrace_trace_function now! */
308#endif /* !CONFIG_DYNAMIC_FTRACE */
309
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400310 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400311}
312
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800313int using_ftrace_ops_list_func(void)
314{
315 return ftrace_trace_function == ftrace_ops_list_func;
316}
317
Steven Rostedt2b499382011-05-03 22:49:52 -0400318static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200319{
Steven Rostedt2b499382011-05-03 22:49:52 -0400320 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200321 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400322 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200323 * CPU might be walking that list. We need to make sure
324 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400325 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200326 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400327 rcu_assign_pointer(*list, ops);
328}
Steven Rostedt3d083392008-05-12 21:20:42 +0200329
Steven Rostedt2b499382011-05-03 22:49:52 -0400330static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
331{
332 struct ftrace_ops **p;
333
334 /*
335 * If we are removing the last function, then simply point
336 * to the ftrace_stub.
337 */
338 if (*list == ops && ops->next == &ftrace_list_end) {
339 *list = &ftrace_list_end;
340 return 0;
341 }
342
343 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
344 if (*p == ops)
345 break;
346
347 if (*p != ops)
348 return -1;
349
350 *p = (*p)->next;
351 return 0;
352}
353
Jiri Olsae2484912012-02-15 15:51:48 +0100354static void add_ftrace_list_ops(struct ftrace_ops **list,
355 struct ftrace_ops *main_ops,
356 struct ftrace_ops *ops)
357{
358 int first = *list == &ftrace_list_end;
359 add_ftrace_ops(list, ops);
360 if (first)
361 add_ftrace_ops(&ftrace_ops_list, main_ops);
362}
363
364static int remove_ftrace_list_ops(struct ftrace_ops **list,
365 struct ftrace_ops *main_ops,
366 struct ftrace_ops *ops)
367{
368 int ret = remove_ftrace_ops(list, ops);
369 if (!ret && *list == &ftrace_list_end)
370 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
371 return ret;
372}
373
Steven Rostedt2b499382011-05-03 22:49:52 -0400374static int __register_ftrace_function(struct ftrace_ops *ops)
375{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500376 if (ops->flags & FTRACE_OPS_FL_DELETED)
377 return -EINVAL;
378
Steven Rostedtb8489142011-05-04 09:27:52 -0400379 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
380 return -EBUSY;
381
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900382#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400383 /*
384 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
385 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
386 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
387 */
388 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
389 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
390 return -EINVAL;
391
392 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
393 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
394#endif
395
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400396 if (!core_kernel_data((unsigned long)ops))
397 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
398
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500399 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100400 if (control_ops_alloc(ops))
401 return -ENOMEM;
402 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400403 } else
404 add_ftrace_ops(&ftrace_ops_list, ops);
405
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400406 if (ftrace_enabled)
407 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200408
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200409 return 0;
410}
411
Ingo Molnare309b412008-05-12 21:20:51 +0200412static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200413{
Steven Rostedt2b499382011-05-03 22:49:52 -0400414 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200415
Steven Rostedtb8489142011-05-04 09:27:52 -0400416 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
417 return -EBUSY;
418
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500419 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100420 ret = remove_ftrace_list_ops(&ftrace_control_list,
421 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400422 } else
423 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
424
Steven Rostedt2b499382011-05-03 22:49:52 -0400425 if (ret < 0)
426 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400427
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400428 if (ftrace_enabled)
429 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200430
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500431 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200432}
433
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500434static void ftrace_update_pid_func(void)
435{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400436 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500437 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900438 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500439
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400440 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500441}
442
Steven Rostedt493762f2009-03-23 17:12:36 -0400443#ifdef CONFIG_FUNCTION_PROFILER
444struct ftrace_profile {
445 struct hlist_node node;
446 unsigned long ip;
447 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400448#ifdef CONFIG_FUNCTION_GRAPH_TRACER
449 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400450 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400451#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400452};
453
454struct ftrace_profile_page {
455 struct ftrace_profile_page *next;
456 unsigned long index;
457 struct ftrace_profile records[];
458};
459
Steven Rostedtcafb1682009-03-24 20:50:39 -0400460struct ftrace_profile_stat {
461 atomic_t disabled;
462 struct hlist_head *hash;
463 struct ftrace_profile_page *pages;
464 struct ftrace_profile_page *start;
465 struct tracer_stat stat;
466};
467
Steven Rostedt493762f2009-03-23 17:12:36 -0400468#define PROFILE_RECORDS_SIZE \
469 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
470
471#define PROFILES_PER_PAGE \
472 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
473
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400474static int ftrace_profile_enabled __read_mostly;
475
476/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400477static DEFINE_MUTEX(ftrace_profile_lock);
478
Steven Rostedtcafb1682009-03-24 20:50:39 -0400479static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400480
Namhyung Kim20079eb2013-04-10 08:55:50 +0900481#define FTRACE_PROFILE_HASH_BITS 10
482#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400483
Steven Rostedt493762f2009-03-23 17:12:36 -0400484static void *
485function_stat_next(void *v, int idx)
486{
487 struct ftrace_profile *rec = v;
488 struct ftrace_profile_page *pg;
489
490 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
491
492 again:
Li Zefan0296e422009-06-26 11:15:37 +0800493 if (idx != 0)
494 rec++;
495
Steven Rostedt493762f2009-03-23 17:12:36 -0400496 if ((void *)rec >= (void *)&pg->records[pg->index]) {
497 pg = pg->next;
498 if (!pg)
499 return NULL;
500 rec = &pg->records[0];
501 if (!rec->counter)
502 goto again;
503 }
504
505 return rec;
506}
507
508static void *function_stat_start(struct tracer_stat *trace)
509{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400510 struct ftrace_profile_stat *stat =
511 container_of(trace, struct ftrace_profile_stat, stat);
512
513 if (!stat || !stat->start)
514 return NULL;
515
516 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400517}
518
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400519#ifdef CONFIG_FUNCTION_GRAPH_TRACER
520/* function graph compares on total time */
521static int function_stat_cmp(void *p1, void *p2)
522{
523 struct ftrace_profile *a = p1;
524 struct ftrace_profile *b = p2;
525
526 if (a->time < b->time)
527 return -1;
528 if (a->time > b->time)
529 return 1;
530 else
531 return 0;
532}
533#else
534/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400535static int function_stat_cmp(void *p1, void *p2)
536{
537 struct ftrace_profile *a = p1;
538 struct ftrace_profile *b = p2;
539
540 if (a->counter < b->counter)
541 return -1;
542 if (a->counter > b->counter)
543 return 1;
544 else
545 return 0;
546}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400547#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400548
549static int function_stat_headers(struct seq_file *m)
550{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400551#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400552 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400553 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400554 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400555 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400556#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400557 seq_printf(m, " Function Hit\n"
558 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400559#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400560 return 0;
561}
562
563static int function_stat_show(struct seq_file *m, void *v)
564{
565 struct ftrace_profile *rec = v;
566 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800567 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400568#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400569 static struct trace_seq s;
570 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400571 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400572#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800573 mutex_lock(&ftrace_profile_lock);
574
575 /* we raced with function_profile_reset() */
576 if (unlikely(rec->counter == 0)) {
577 ret = -EBUSY;
578 goto out;
579 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400580
581 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400582 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400583
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400584#ifdef CONFIG_FUNCTION_GRAPH_TRACER
585 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400586 avg = rec->time;
587 do_div(avg, rec->counter);
588
Chase Douglase330b3b2010-04-26 14:02:05 -0400589 /* Sample standard deviation (s^2) */
590 if (rec->counter <= 1)
591 stddev = 0;
592 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200593 /*
594 * Apply Welford's method:
595 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
596 */
597 stddev = rec->counter * rec->time_squared -
598 rec->time * rec->time;
599
Chase Douglase330b3b2010-04-26 14:02:05 -0400600 /*
601 * Divide only 1000 for ns^2 -> us^2 conversion.
602 * trace_print_graph_duration will divide 1000 again.
603 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200604 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400605 }
606
Steven Rostedt34886c82009-03-25 21:00:47 -0400607 trace_seq_init(&s);
608 trace_print_graph_duration(rec->time, &s);
609 trace_seq_puts(&s, " ");
610 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400611 trace_seq_puts(&s, " ");
612 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400613 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400614#endif
615 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800616out:
617 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400618
Li Zefan3aaba202010-08-23 16:50:12 +0800619 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400620}
621
Steven Rostedtcafb1682009-03-24 20:50:39 -0400622static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400623{
624 struct ftrace_profile_page *pg;
625
Steven Rostedtcafb1682009-03-24 20:50:39 -0400626 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400627
628 while (pg) {
629 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
630 pg->index = 0;
631 pg = pg->next;
632 }
633
Steven Rostedtcafb1682009-03-24 20:50:39 -0400634 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400635 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
636}
637
Steven Rostedtcafb1682009-03-24 20:50:39 -0400638int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400639{
640 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400641 int functions;
642 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400643 int i;
644
645 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400647 return 0;
648
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
650 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400651 return -ENOMEM;
652
Steven Rostedt318e0a72009-03-25 20:06:34 -0400653#ifdef CONFIG_DYNAMIC_FTRACE
654 functions = ftrace_update_tot_cnt;
655#else
656 /*
657 * We do not know the number of functions that exist because
658 * dynamic tracing is what counts them. With past experience
659 * we have around 20K functions. That should be more than enough.
660 * It is highly unlikely we will execute every function in
661 * the kernel.
662 */
663 functions = 20000;
664#endif
665
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400667
Steven Rostedt318e0a72009-03-25 20:06:34 -0400668 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
669
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900670 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400671 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400672 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400673 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400674 pg = pg->next;
675 }
676
677 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400678
679 out_free:
680 pg = stat->start;
681 while (pg) {
682 unsigned long tmp = (unsigned long)pg;
683
684 pg = pg->next;
685 free_page(tmp);
686 }
687
Steven Rostedt318e0a72009-03-25 20:06:34 -0400688 stat->pages = NULL;
689 stat->start = NULL;
690
691 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400692}
693
Steven Rostedtcafb1682009-03-24 20:50:39 -0400694static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400695{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400696 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400697 int size;
698
Steven Rostedtcafb1682009-03-24 20:50:39 -0400699 stat = &per_cpu(ftrace_profile_stats, cpu);
700
701 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400702 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400704 return 0;
705 }
706
707 /*
708 * We are profiling all functions, but usually only a few thousand
709 * functions are hit. We'll make a hash of 1024 items.
710 */
711 size = FTRACE_PROFILE_HASH_SIZE;
712
Steven Rostedtcafb1682009-03-24 20:50:39 -0400713 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400714
Steven Rostedtcafb1682009-03-24 20:50:39 -0400715 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400716 return -ENOMEM;
717
Steven Rostedt318e0a72009-03-25 20:06:34 -0400718 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 if (ftrace_profile_pages_init(stat) < 0) {
720 kfree(stat->hash);
721 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400722 return -ENOMEM;
723 }
724
725 return 0;
726}
727
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728static int ftrace_profile_init(void)
729{
730 int cpu;
731 int ret = 0;
732
Miao Xiec4602c12013-12-16 15:20:01 +0800733 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400734 ret = ftrace_profile_init_cpu(cpu);
735 if (ret)
736 break;
737 }
738
739 return ret;
740}
741
Steven Rostedt493762f2009-03-23 17:12:36 -0400742/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400743static struct ftrace_profile *
744ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400745{
746 struct ftrace_profile *rec;
747 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400748 unsigned long key;
749
Namhyung Kim20079eb2013-04-10 08:55:50 +0900750 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400751 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400752
753 if (hlist_empty(hhd))
754 return NULL;
755
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400756 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400757 if (rec->ip == ip)
758 return rec;
759 }
760
761 return NULL;
762}
763
Steven Rostedtcafb1682009-03-24 20:50:39 -0400764static void ftrace_add_profile(struct ftrace_profile_stat *stat,
765 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400766{
767 unsigned long key;
768
Namhyung Kim20079eb2013-04-10 08:55:50 +0900769 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400771}
772
Steven Rostedt318e0a72009-03-25 20:06:34 -0400773/*
774 * The memory is already allocated, this simply finds a new record to use.
775 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400776static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400777ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400778{
779 struct ftrace_profile *rec = NULL;
780
Steven Rostedt318e0a72009-03-25 20:06:34 -0400781 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400782 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400783 goto out;
784
Steven Rostedt493762f2009-03-23 17:12:36 -0400785 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400786 * Try to find the function again since an NMI
787 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400788 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400789 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400790 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400791 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400792
Steven Rostedtcafb1682009-03-24 20:50:39 -0400793 if (stat->pages->index == PROFILES_PER_PAGE) {
794 if (!stat->pages->next)
795 goto out;
796 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400797 }
798
Steven Rostedtcafb1682009-03-24 20:50:39 -0400799 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400800 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400801 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400802
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400805
806 return rec;
807}
808
Steven Rostedt493762f2009-03-23 17:12:36 -0400809static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400810function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400811 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400812{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400814 struct ftrace_profile *rec;
815 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400816
817 if (!ftrace_profile_enabled)
818 return;
819
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821
Christoph Lameterbdffd892014-04-29 14:17:40 -0500822 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400823 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 goto out;
825
826 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400827 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400828 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400829 if (!rec)
830 goto out;
831 }
832
833 rec->counter++;
834 out:
835 local_irq_restore(flags);
836}
837
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400838#ifdef CONFIG_FUNCTION_GRAPH_TRACER
839static int profile_graph_entry(struct ftrace_graph_ent *trace)
840{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400841 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400842 return 1;
843}
844
845static void profile_graph_return(struct ftrace_graph_ret *trace)
846{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400847 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400848 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400849 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400850 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400851
852 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500853 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400854 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400855 goto out;
856
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400857 /* If the calltime was zero'd ignore it */
858 if (!trace->calltime)
859 goto out;
860
Steven Rostedta2a16d62009-03-24 23:17:58 -0400861 calltime = trace->rettime - trace->calltime;
862
863 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
864 int index;
865
866 index = trace->depth;
867
868 /* Append this call time to the parent time to subtract */
869 if (index)
870 current->ret_stack[index - 1].subtime += calltime;
871
872 if (current->ret_stack[index].subtime < calltime)
873 calltime -= current->ret_stack[index].subtime;
874 else
875 calltime = 0;
876 }
877
Steven Rostedtcafb1682009-03-24 20:50:39 -0400878 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400879 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400880 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400881 rec->time_squared += calltime * calltime;
882 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400883
Steven Rostedtcafb1682009-03-24 20:50:39 -0400884 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400885 local_irq_restore(flags);
886}
887
888static int register_ftrace_profiler(void)
889{
890 return register_ftrace_graph(&profile_graph_return,
891 &profile_graph_entry);
892}
893
894static void unregister_ftrace_profiler(void)
895{
896 unregister_ftrace_graph();
897}
898#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100899static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400900 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900901 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
902 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400903};
904
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400905static int register_ftrace_profiler(void)
906{
907 return register_ftrace_function(&ftrace_profile_ops);
908}
909
910static void unregister_ftrace_profiler(void)
911{
912 unregister_ftrace_function(&ftrace_profile_ops);
913}
914#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
915
Steven Rostedt493762f2009-03-23 17:12:36 -0400916static ssize_t
917ftrace_profile_write(struct file *filp, const char __user *ubuf,
918 size_t cnt, loff_t *ppos)
919{
920 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400921 int ret;
922
Peter Huewe22fe9b52011-06-07 21:58:27 +0200923 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
924 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400925 return ret;
926
927 val = !!val;
928
929 mutex_lock(&ftrace_profile_lock);
930 if (ftrace_profile_enabled ^ val) {
931 if (val) {
932 ret = ftrace_profile_init();
933 if (ret < 0) {
934 cnt = ret;
935 goto out;
936 }
937
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400938 ret = register_ftrace_profiler();
939 if (ret < 0) {
940 cnt = ret;
941 goto out;
942 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400943 ftrace_profile_enabled = 1;
944 } else {
945 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400946 /*
947 * unregister_ftrace_profiler calls stop_machine
948 * so this acts like an synchronize_sched.
949 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400950 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400951 }
952 }
953 out:
954 mutex_unlock(&ftrace_profile_lock);
955
Jiri Olsacf8517c2009-10-23 19:36:16 -0400956 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400957
958 return cnt;
959}
960
961static ssize_t
962ftrace_profile_read(struct file *filp, char __user *ubuf,
963 size_t cnt, loff_t *ppos)
964{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400965 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 int r;
967
968 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
969 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
970}
971
972static const struct file_operations ftrace_profile_fops = {
973 .open = tracing_open_generic,
974 .read = ftrace_profile_read,
975 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200976 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400977};
978
Steven Rostedtcafb1682009-03-24 20:50:39 -0400979/* used to initialize the real stat files */
980static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400981 .name = "functions",
982 .stat_start = function_stat_start,
983 .stat_next = function_stat_next,
984 .stat_cmp = function_stat_cmp,
985 .stat_headers = function_stat_headers,
986 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400987};
988
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400989static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400990{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400991 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400992 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400993 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400994 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400995 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400996
Steven Rostedtcafb1682009-03-24 20:50:39 -0400997 for_each_possible_cpu(cpu) {
998 stat = &per_cpu(ftrace_profile_stats, cpu);
999
1000 /* allocate enough for function name + cpu number */
1001 name = kmalloc(32, GFP_KERNEL);
1002 if (!name) {
1003 /*
1004 * The files created are permanent, if something happens
1005 * we still do not free memory.
1006 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001007 WARN(1,
1008 "Could not allocate stat file for cpu %d\n",
1009 cpu);
1010 return;
1011 }
1012 stat->stat = function_stats;
1013 snprintf(name, 32, "function%d", cpu);
1014 stat->stat.name = name;
1015 ret = register_stat_tracer(&stat->stat);
1016 if (ret) {
1017 WARN(1,
1018 "Could not register function stat for cpu %d\n",
1019 cpu);
1020 kfree(name);
1021 return;
1022 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001023 }
1024
1025 entry = debugfs_create_file("function_profile_enabled", 0644,
1026 d_tracer, NULL, &ftrace_profile_fops);
1027 if (!entry)
1028 pr_warning("Could not create debugfs "
1029 "'function_profile_enabled' entry\n");
1030}
1031
1032#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001033static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001034{
1035}
1036#endif /* CONFIG_FUNCTION_PROFILER */
1037
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001038static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1039
Steven Rostedt3d083392008-05-12 21:20:42 +02001040#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001041
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001042static struct ftrace_ops *removed_ops;
1043
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001044#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001045# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001046#endif
1047
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001048static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1049
Steven Rostedtb6887d72009-02-17 12:32:04 -05001050struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001051 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001052 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001053 unsigned long flags;
1054 unsigned long ip;
1055 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001056 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001057};
1058
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001059struct ftrace_func_entry {
1060 struct hlist_node hlist;
1061 unsigned long ip;
1062};
1063
1064struct ftrace_hash {
1065 unsigned long size_bits;
1066 struct hlist_head *buckets;
1067 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001068 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001069};
1070
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001071/*
1072 * We make these constant because no one should touch them,
1073 * but they are used as the default "empty hash", to avoid allocating
1074 * it all the time. These are in a read only section such that if
1075 * anyone does try to modify it, it will cause an exception.
1076 */
1077static const struct hlist_head empty_buckets[1];
1078static const struct ftrace_hash empty_hash = {
1079 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001080};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001081#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001082
Steven Rostedt2b499382011-05-03 22:49:52 -04001083static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001084 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001085 .notrace_hash = EMPTY_HASH,
1086 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001087 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1088 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001089};
1090
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001091struct ftrace_page {
1092 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001093 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001094 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001095 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001096};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001097
Steven Rostedta7900872011-12-16 16:23:44 -05001098#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1099#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001100
1101/* estimate from running different kernels */
1102#define NR_TO_INIT 10000
1103
1104static struct ftrace_page *ftrace_pages_start;
1105static struct ftrace_page *ftrace_pages;
1106
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001107static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001108{
1109 return !hash || !hash->count;
1110}
1111
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001112static struct ftrace_func_entry *
1113ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1114{
1115 unsigned long key;
1116 struct ftrace_func_entry *entry;
1117 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001118
Steven Rostedt06a51d92011-12-19 19:07:36 -05001119 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001120 return NULL;
1121
1122 if (hash->size_bits > 0)
1123 key = hash_long(ip, hash->size_bits);
1124 else
1125 key = 0;
1126
1127 hhd = &hash->buckets[key];
1128
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001129 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001130 if (entry->ip == ip)
1131 return entry;
1132 }
1133 return NULL;
1134}
1135
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001136static void __add_hash_entry(struct ftrace_hash *hash,
1137 struct ftrace_func_entry *entry)
1138{
1139 struct hlist_head *hhd;
1140 unsigned long key;
1141
1142 if (hash->size_bits)
1143 key = hash_long(entry->ip, hash->size_bits);
1144 else
1145 key = 0;
1146
1147 hhd = &hash->buckets[key];
1148 hlist_add_head(&entry->hlist, hhd);
1149 hash->count++;
1150}
1151
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001152static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1153{
1154 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001155
1156 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1157 if (!entry)
1158 return -ENOMEM;
1159
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001160 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001161 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001162
1163 return 0;
1164}
1165
1166static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001167free_hash_entry(struct ftrace_hash *hash,
1168 struct ftrace_func_entry *entry)
1169{
1170 hlist_del(&entry->hlist);
1171 kfree(entry);
1172 hash->count--;
1173}
1174
1175static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001176remove_hash_entry(struct ftrace_hash *hash,
1177 struct ftrace_func_entry *entry)
1178{
1179 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001180 hash->count--;
1181}
1182
1183static void ftrace_hash_clear(struct ftrace_hash *hash)
1184{
1185 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001186 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001187 struct ftrace_func_entry *entry;
1188 int size = 1 << hash->size_bits;
1189 int i;
1190
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001191 if (!hash->count)
1192 return;
1193
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001194 for (i = 0; i < size; i++) {
1195 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001196 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001197 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001198 }
1199 FTRACE_WARN_ON(hash->count);
1200}
1201
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001202static void free_ftrace_hash(struct ftrace_hash *hash)
1203{
1204 if (!hash || hash == EMPTY_HASH)
1205 return;
1206 ftrace_hash_clear(hash);
1207 kfree(hash->buckets);
1208 kfree(hash);
1209}
1210
Steven Rostedt07fd5512011-05-05 18:03:47 -04001211static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1212{
1213 struct ftrace_hash *hash;
1214
1215 hash = container_of(rcu, struct ftrace_hash, rcu);
1216 free_ftrace_hash(hash);
1217}
1218
1219static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1220{
1221 if (!hash || hash == EMPTY_HASH)
1222 return;
1223 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1224}
1225
Jiri Olsa5500fa52012-02-15 15:51:54 +01001226void ftrace_free_filter(struct ftrace_ops *ops)
1227{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001228 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001229 free_ftrace_hash(ops->filter_hash);
1230 free_ftrace_hash(ops->notrace_hash);
1231}
1232
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001233static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1234{
1235 struct ftrace_hash *hash;
1236 int size;
1237
1238 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1239 if (!hash)
1240 return NULL;
1241
1242 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001243 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001244
1245 if (!hash->buckets) {
1246 kfree(hash);
1247 return NULL;
1248 }
1249
1250 hash->size_bits = size_bits;
1251
1252 return hash;
1253}
1254
1255static struct ftrace_hash *
1256alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1257{
1258 struct ftrace_func_entry *entry;
1259 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001260 int size;
1261 int ret;
1262 int i;
1263
1264 new_hash = alloc_ftrace_hash(size_bits);
1265 if (!new_hash)
1266 return NULL;
1267
1268 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001269 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001270 return new_hash;
1271
1272 size = 1 << hash->size_bits;
1273 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001274 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001275 ret = add_hash_entry(new_hash, entry->ip);
1276 if (ret < 0)
1277 goto free_hash;
1278 }
1279 }
1280
1281 FTRACE_WARN_ON(new_hash->count != hash->count);
1282
1283 return new_hash;
1284
1285 free_hash:
1286 free_ftrace_hash(new_hash);
1287 return NULL;
1288}
1289
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001290static void
1291ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1292static void
1293ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1294
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001295static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001296ftrace_hash_move(struct ftrace_ops *ops, int enable,
1297 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001298{
1299 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001300 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001301 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001302 struct ftrace_hash *old_hash;
1303 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001304 int size = src->count;
1305 int bits = 0;
1306 int i;
1307
1308 /*
1309 * If the new source is empty, just free dst and assign it
1310 * the empty_hash.
1311 */
1312 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001313 new_hash = EMPTY_HASH;
1314 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001315 }
1316
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001317 /*
1318 * Make the hash size about 1/2 the # found
1319 */
1320 for (size /= 2; size; size >>= 1)
1321 bits++;
1322
1323 /* Don't allocate too much */
1324 if (bits > FTRACE_HASH_MAX_BITS)
1325 bits = FTRACE_HASH_MAX_BITS;
1326
Steven Rostedt07fd5512011-05-05 18:03:47 -04001327 new_hash = alloc_ftrace_hash(bits);
1328 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001329 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001330
1331 size = 1 << src->size_bits;
1332 for (i = 0; i < size; i++) {
1333 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001334 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001335 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001336 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 }
1338 }
1339
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001340update:
1341 /*
1342 * Remove the current set, update the hash and add
1343 * them back.
1344 */
1345 ftrace_hash_rec_disable(ops, enable);
1346
Steven Rostedt07fd5512011-05-05 18:03:47 -04001347 old_hash = *dst;
1348 rcu_assign_pointer(*dst, new_hash);
1349 free_ftrace_hash_rcu(old_hash);
1350
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001351 ftrace_hash_rec_enable(ops, enable);
1352
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001353 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001354}
1355
Steven Rostedt265c8312009-02-13 12:43:56 -05001356/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001357 * Test the hashes for this ops to see if we want to call
1358 * the ops->func or not.
1359 *
1360 * It's a match if the ip is in the ops->filter_hash or
1361 * the filter_hash does not exist or is empty,
1362 * AND
1363 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001364 *
1365 * This needs to be called with preemption disabled as
1366 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001367 */
1368static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001369ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001370{
1371 struct ftrace_hash *filter_hash;
1372 struct ftrace_hash *notrace_hash;
1373 int ret;
1374
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001375#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1376 /*
1377 * There's a small race when adding ops that the ftrace handler
1378 * that wants regs, may be called without them. We can not
1379 * allow that handler to be called if regs is NULL.
1380 */
1381 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1382 return 0;
1383#endif
1384
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001385 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1386 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001387
Steven Rostedt06a51d92011-12-19 19:07:36 -05001388 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001389 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001390 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001391 !ftrace_lookup_ip(notrace_hash, ip)))
1392 ret = 1;
1393 else
1394 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001395
1396 return ret;
1397}
1398
1399/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001400 * This is a double for. Do not use 'break' to break out of the loop,
1401 * you must use a goto.
1402 */
1403#define do_for_each_ftrace_rec(pg, rec) \
1404 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1405 int _____i; \
1406 for (_____i = 0; _____i < pg->index; _____i++) { \
1407 rec = &pg->records[_____i];
1408
1409#define while_for_each_ftrace_rec() \
1410 } \
1411 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301412
Steven Rostedt5855fea2011-12-16 19:27:42 -05001413
1414static int ftrace_cmp_recs(const void *a, const void *b)
1415{
Steven Rostedta650e022012-04-25 13:48:13 -04001416 const struct dyn_ftrace *key = a;
1417 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001418
Steven Rostedta650e022012-04-25 13:48:13 -04001419 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001420 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001421 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1422 return 1;
1423 return 0;
1424}
1425
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001426static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001427{
1428 struct ftrace_page *pg;
1429 struct dyn_ftrace *rec;
1430 struct dyn_ftrace key;
1431
1432 key.ip = start;
1433 key.flags = end; /* overload flags, as it is unsigned long */
1434
1435 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1436 if (end < pg->records[0].ip ||
1437 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1438 continue;
1439 rec = bsearch(&key, pg->records, pg->index,
1440 sizeof(struct dyn_ftrace),
1441 ftrace_cmp_recs);
1442 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001443 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001444 }
1445
Steven Rostedt5855fea2011-12-16 19:27:42 -05001446 return 0;
1447}
1448
Steven Rostedtc88fd862011-08-16 09:53:39 -04001449/**
1450 * ftrace_location - return true if the ip giving is a traced location
1451 * @ip: the instruction pointer to check
1452 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001453 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001454 * That is, the instruction that is either a NOP or call to
1455 * the function tracer. It checks the ftrace internal tables to
1456 * determine if the address belongs or not.
1457 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001458unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001459{
Steven Rostedta650e022012-04-25 13:48:13 -04001460 return ftrace_location_range(ip, ip);
1461}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001462
Steven Rostedta650e022012-04-25 13:48:13 -04001463/**
1464 * ftrace_text_reserved - return true if range contains an ftrace location
1465 * @start: start of range to search
1466 * @end: end of range to search (inclusive). @end points to the last byte to check.
1467 *
1468 * Returns 1 if @start and @end contains a ftrace location.
1469 * That is, the instruction that is either a NOP or call to
1470 * the function tracer. It checks the ftrace internal tables to
1471 * determine if the address belongs or not.
1472 */
Sasha Levind88471c2013-01-09 18:09:20 -05001473int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001474{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001475 unsigned long ret;
1476
1477 ret = ftrace_location_range((unsigned long)start,
1478 (unsigned long)end);
1479
1480 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001481}
1482
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001483/* Test if ops registered to this rec needs regs */
1484static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1485{
1486 struct ftrace_ops *ops;
1487 bool keep_regs = false;
1488
1489 for (ops = ftrace_ops_list;
1490 ops != &ftrace_list_end; ops = ops->next) {
1491 /* pass rec in as regs to have non-NULL val */
1492 if (ftrace_ops_test(ops, rec->ip, rec)) {
1493 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1494 keep_regs = true;
1495 break;
1496 }
1497 }
1498 }
1499
1500 return keep_regs;
1501}
1502
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001503static void ftrace_remove_tramp(struct ftrace_ops *ops,
1504 struct dyn_ftrace *rec)
1505{
1506 struct ftrace_func_entry *entry;
1507
1508 entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1509 if (!entry)
1510 return;
1511
1512 /*
1513 * The tramp_hash entry will be removed at time
1514 * of update.
1515 */
1516 ops->trampolines--;
1517 rec->flags &= ~FTRACE_FL_TRAMP;
1518}
1519
1520static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1521{
1522 struct ftrace_ops *op;
1523
1524 do_for_each_ftrace_op(op, ftrace_ops_list) {
1525 if (op->trampolines)
1526 ftrace_remove_tramp(op, rec);
1527 } while_for_each_ftrace_op(op);
1528}
1529
Steven Rostedted926f92011-05-03 13:25:24 -04001530static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1531 int filter_hash,
1532 bool inc)
1533{
1534 struct ftrace_hash *hash;
1535 struct ftrace_hash *other_hash;
1536 struct ftrace_page *pg;
1537 struct dyn_ftrace *rec;
1538 int count = 0;
1539 int all = 0;
1540
1541 /* Only update if the ops has been registered */
1542 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1543 return;
1544
1545 /*
1546 * In the filter_hash case:
1547 * If the count is zero, we update all records.
1548 * Otherwise we just update the items in the hash.
1549 *
1550 * In the notrace_hash case:
1551 * We enable the update in the hash.
1552 * As disabling notrace means enabling the tracing,
1553 * and enabling notrace means disabling, the inc variable
1554 * gets inversed.
1555 */
1556 if (filter_hash) {
1557 hash = ops->filter_hash;
1558 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001559 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001560 all = 1;
1561 } else {
1562 inc = !inc;
1563 hash = ops->notrace_hash;
1564 other_hash = ops->filter_hash;
1565 /*
1566 * If the notrace hash has no items,
1567 * then there's nothing to do.
1568 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001569 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001570 return;
1571 }
1572
1573 do_for_each_ftrace_rec(pg, rec) {
1574 int in_other_hash = 0;
1575 int in_hash = 0;
1576 int match = 0;
1577
1578 if (all) {
1579 /*
1580 * Only the filter_hash affects all records.
1581 * Update if the record is not in the notrace hash.
1582 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001583 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001584 match = 1;
1585 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001586 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1587 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001588
1589 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001590 * If filter_hash is set, we want to match all functions
1591 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001592 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001593 * If filter_hash is not set, then we are decrementing.
1594 * That means we match anything that is in the hash
1595 * and also in the other_hash. That is, we need to turn
1596 * off functions in the other hash because they are disabled
1597 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001598 */
1599 if (filter_hash && in_hash && !in_other_hash)
1600 match = 1;
1601 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001602 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001603 match = 1;
1604 }
1605 if (!match)
1606 continue;
1607
1608 if (inc) {
1609 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001610 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001611 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001612
1613 /*
1614 * If there's only a single callback registered to a
1615 * function, and the ops has a trampoline registered
1616 * for it, then we can call it directly.
1617 */
1618 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1619 rec->flags |= FTRACE_FL_TRAMP;
1620 ops->trampolines++;
1621 } else {
1622 /*
1623 * If we are adding another function callback
1624 * to this function, and the previous had a
1625 * trampoline used, then we need to go back to
1626 * the default trampoline.
1627 */
1628 rec->flags &= ~FTRACE_FL_TRAMP;
1629
1630 /* remove trampolines from any ops for this rec */
1631 ftrace_clear_tramps(rec);
1632 }
1633
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001634 /*
1635 * If any ops wants regs saved for this function
1636 * then all ops will get saved regs.
1637 */
1638 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1639 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001640 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001641 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001642 return;
1643 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001644
1645 if (ops->trampoline && !ftrace_rec_count(rec))
1646 ftrace_remove_tramp(ops, rec);
1647
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001648 /*
1649 * If the rec had REGS enabled and the ops that is
1650 * being removed had REGS set, then see if there is
1651 * still any ops for this record that wants regs.
1652 * If not, we can stop recording them.
1653 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001654 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001655 rec->flags & FTRACE_FL_REGS &&
1656 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1657 if (!test_rec_ops_needs_regs(rec))
1658 rec->flags &= ~FTRACE_FL_REGS;
1659 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001660
1661 /*
1662 * flags will be cleared in ftrace_check_record()
1663 * if rec count is zero.
1664 */
Steven Rostedted926f92011-05-03 13:25:24 -04001665 }
1666 count++;
1667 /* Shortcut, if we handled all records, we are done. */
1668 if (!all && count == hash->count)
1669 return;
1670 } while_for_each_ftrace_rec();
1671}
1672
1673static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1674 int filter_hash)
1675{
1676 __ftrace_hash_rec_update(ops, filter_hash, 0);
1677}
1678
1679static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1680 int filter_hash)
1681{
1682 __ftrace_hash_rec_update(ops, filter_hash, 1);
1683}
1684
Steven Rostedt05736a42008-09-22 14:55:47 -07001685static void print_ip_ins(const char *fmt, unsigned char *p)
1686{
1687 int i;
1688
1689 printk(KERN_CONT "%s", fmt);
1690
1691 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1692 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1693}
1694
Steven Rostedtc88fd862011-08-16 09:53:39 -04001695/**
1696 * ftrace_bug - report and shutdown function tracer
1697 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1698 * @ip: The address that failed
1699 *
1700 * The arch code that enables or disables the function tracing
1701 * can call ftrace_bug() when it has detected a problem in
1702 * modifying the code. @failed should be one of either:
1703 * EFAULT - if the problem happens on reading the @ip address
1704 * EINVAL - if what is read at @ip is not what was expected
1705 * EPERM - if the problem happens on writting to the @ip address
1706 */
1707void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001708{
1709 switch (failed) {
1710 case -EFAULT:
1711 FTRACE_WARN_ON_ONCE(1);
1712 pr_info("ftrace faulted on modifying ");
1713 print_ip_sym(ip);
1714 break;
1715 case -EINVAL:
1716 FTRACE_WARN_ON_ONCE(1);
1717 pr_info("ftrace failed to modify ");
1718 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001719 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001720 printk(KERN_CONT "\n");
1721 break;
1722 case -EPERM:
1723 FTRACE_WARN_ON_ONCE(1);
1724 pr_info("ftrace faulted on writing ");
1725 print_ip_sym(ip);
1726 break;
1727 default:
1728 FTRACE_WARN_ON_ONCE(1);
1729 pr_info("ftrace faulted on unknown error ");
1730 print_ip_sym(ip);
1731 }
1732}
1733
Steven Rostedtc88fd862011-08-16 09:53:39 -04001734static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001735{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001736 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001737
Steven Rostedt982c3502008-11-15 16:31:41 -05001738 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001739 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001740 *
Steven Rostedted926f92011-05-03 13:25:24 -04001741 * If the record has a ref count, then we need to enable it
1742 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001743 *
Steven Rostedted926f92011-05-03 13:25:24 -04001744 * Otherwise we make sure its disabled.
1745 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001746 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001747 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001748 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001749 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001750 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001751
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001752 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001753 * If enabling and the REGS flag does not match the REGS_EN, or
1754 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1755 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001756 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001757 if (flag) {
1758 if (!(rec->flags & FTRACE_FL_REGS) !=
1759 !(rec->flags & FTRACE_FL_REGS_EN))
1760 flag |= FTRACE_FL_REGS;
1761
1762 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1763 !(rec->flags & FTRACE_FL_TRAMP_EN))
1764 flag |= FTRACE_FL_TRAMP;
1765 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001766
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001767 /* If the state of this record hasn't changed, then do nothing */
1768 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001769 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001770
1771 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001772 /* Save off if rec is being enabled (for return value) */
1773 flag ^= rec->flags & FTRACE_FL_ENABLED;
1774
1775 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001776 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001777 if (flag & FTRACE_FL_REGS) {
1778 if (rec->flags & FTRACE_FL_REGS)
1779 rec->flags |= FTRACE_FL_REGS_EN;
1780 else
1781 rec->flags &= ~FTRACE_FL_REGS_EN;
1782 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001783 if (flag & FTRACE_FL_TRAMP) {
1784 if (rec->flags & FTRACE_FL_TRAMP)
1785 rec->flags |= FTRACE_FL_TRAMP_EN;
1786 else
1787 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1788 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001789 }
1790
1791 /*
1792 * If this record is being updated from a nop, then
1793 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001794 * Otherwise,
1795 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001796 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001797 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001798 */
1799 if (flag & FTRACE_FL_ENABLED)
1800 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001801
1802 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001803 }
1804
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001805 if (update) {
1806 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001807 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001808 rec->flags = 0;
1809 else
1810 /* Just disable the record (keep REGS state) */
1811 rec->flags &= ~FTRACE_FL_ENABLED;
1812 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001813
1814 return FTRACE_UPDATE_MAKE_NOP;
1815}
1816
1817/**
1818 * ftrace_update_record, set a record that now is tracing or not
1819 * @rec: the record to update
1820 * @enable: set to 1 if the record is tracing, zero to force disable
1821 *
1822 * The records that represent all functions that can be traced need
1823 * to be updated when tracing has been enabled.
1824 */
1825int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1826{
1827 return ftrace_check_record(rec, enable, 1);
1828}
1829
1830/**
1831 * ftrace_test_record, check if the record has been enabled or not
1832 * @rec: the record to test
1833 * @enable: set to 1 to check if enabled, 0 if it is disabled
1834 *
1835 * The arch code may need to test if a record is already set to
1836 * tracing to determine how to modify the function code that it
1837 * represents.
1838 */
1839int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1840{
1841 return ftrace_check_record(rec, enable, 0);
1842}
1843
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001844static struct ftrace_ops *
1845ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1846{
1847 struct ftrace_ops *op;
1848
1849 /* Removed ops need to be tested first */
1850 if (removed_ops && removed_ops->tramp_hash) {
1851 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1852 return removed_ops;
1853 }
1854
1855 do_for_each_ftrace_op(op, ftrace_ops_list) {
1856 if (!op->tramp_hash)
1857 continue;
1858
1859 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1860 return op;
1861
1862 } while_for_each_ftrace_op(op);
1863
1864 return NULL;
1865}
1866
1867static struct ftrace_ops *
1868ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1869{
1870 struct ftrace_ops *op;
1871
1872 do_for_each_ftrace_op(op, ftrace_ops_list) {
1873 /* pass rec in as regs to have non-NULL val */
1874 if (ftrace_ops_test(op, rec->ip, rec))
1875 return op;
1876 } while_for_each_ftrace_op(op);
1877
1878 return NULL;
1879}
1880
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001881/**
1882 * ftrace_get_addr_new - Get the call address to set to
1883 * @rec: The ftrace record descriptor
1884 *
1885 * If the record has the FTRACE_FL_REGS set, that means that it
1886 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1887 * is not not set, then it wants to convert to the normal callback.
1888 *
1889 * Returns the address of the trampoline to set to
1890 */
1891unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1892{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001893 struct ftrace_ops *ops;
1894
1895 /* Trampolines take precedence over regs */
1896 if (rec->flags & FTRACE_FL_TRAMP) {
1897 ops = ftrace_find_tramp_ops_new(rec);
1898 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1899 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1900 (void *)rec->ip, (void *)rec->ip);
1901 /* Ftrace is shutting down, return anything */
1902 return (unsigned long)FTRACE_ADDR;
1903 }
1904 return ops->trampoline;
1905 }
1906
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001907 if (rec->flags & FTRACE_FL_REGS)
1908 return (unsigned long)FTRACE_REGS_ADDR;
1909 else
1910 return (unsigned long)FTRACE_ADDR;
1911}
1912
1913/**
1914 * ftrace_get_addr_curr - Get the call address that is already there
1915 * @rec: The ftrace record descriptor
1916 *
1917 * The FTRACE_FL_REGS_EN is set when the record already points to
1918 * a function that saves all the regs. Basically the '_EN' version
1919 * represents the current state of the function.
1920 *
1921 * Returns the address of the trampoline that is currently being called
1922 */
1923unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1924{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001925 struct ftrace_ops *ops;
1926
1927 /* Trampolines take precedence over regs */
1928 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1929 ops = ftrace_find_tramp_ops_curr(rec);
1930 if (FTRACE_WARN_ON(!ops)) {
1931 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1932 (void *)rec->ip, (void *)rec->ip);
1933 /* Ftrace is shutting down, return anything */
1934 return (unsigned long)FTRACE_ADDR;
1935 }
1936 return ops->trampoline;
1937 }
1938
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001939 if (rec->flags & FTRACE_FL_REGS_EN)
1940 return (unsigned long)FTRACE_REGS_ADDR;
1941 else
1942 return (unsigned long)FTRACE_ADDR;
1943}
1944
Steven Rostedtc88fd862011-08-16 09:53:39 -04001945static int
1946__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1947{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001948 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001949 unsigned long ftrace_addr;
1950 int ret;
1951
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001952 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001953
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001954 /* This needs to be done before we call ftrace_update_record */
1955 ftrace_old_addr = ftrace_get_addr_curr(rec);
1956
1957 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001958
Steven Rostedtc88fd862011-08-16 09:53:39 -04001959 switch (ret) {
1960 case FTRACE_UPDATE_IGNORE:
1961 return 0;
1962
1963 case FTRACE_UPDATE_MAKE_CALL:
1964 return ftrace_make_call(rec, ftrace_addr);
1965
1966 case FTRACE_UPDATE_MAKE_NOP:
1967 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001968
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001969 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001970 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001971 }
1972
1973 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001974}
1975
Steven Rostedte4f5d542012-04-27 09:13:18 -04001976void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001977{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001978 struct dyn_ftrace *rec;
1979 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001980 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001981
Steven Rostedt45a4a232011-04-21 23:16:46 -04001982 if (unlikely(ftrace_disabled))
1983 return;
1984
Steven Rostedt265c8312009-02-13 12:43:56 -05001985 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001986 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001987 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001988 ftrace_bug(failed, rec->ip);
1989 /* Stop processing */
1990 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001991 }
1992 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001993}
1994
Steven Rostedtc88fd862011-08-16 09:53:39 -04001995struct ftrace_rec_iter {
1996 struct ftrace_page *pg;
1997 int index;
1998};
1999
2000/**
2001 * ftrace_rec_iter_start, start up iterating over traced functions
2002 *
2003 * Returns an iterator handle that is used to iterate over all
2004 * the records that represent address locations where functions
2005 * are traced.
2006 *
2007 * May return NULL if no records are available.
2008 */
2009struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2010{
2011 /*
2012 * We only use a single iterator.
2013 * Protected by the ftrace_lock mutex.
2014 */
2015 static struct ftrace_rec_iter ftrace_rec_iter;
2016 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2017
2018 iter->pg = ftrace_pages_start;
2019 iter->index = 0;
2020
2021 /* Could have empty pages */
2022 while (iter->pg && !iter->pg->index)
2023 iter->pg = iter->pg->next;
2024
2025 if (!iter->pg)
2026 return NULL;
2027
2028 return iter;
2029}
2030
2031/**
2032 * ftrace_rec_iter_next, get the next record to process.
2033 * @iter: The handle to the iterator.
2034 *
2035 * Returns the next iterator after the given iterator @iter.
2036 */
2037struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2038{
2039 iter->index++;
2040
2041 if (iter->index >= iter->pg->index) {
2042 iter->pg = iter->pg->next;
2043 iter->index = 0;
2044
2045 /* Could have empty pages */
2046 while (iter->pg && !iter->pg->index)
2047 iter->pg = iter->pg->next;
2048 }
2049
2050 if (!iter->pg)
2051 return NULL;
2052
2053 return iter;
2054}
2055
2056/**
2057 * ftrace_rec_iter_record, get the record at the iterator location
2058 * @iter: The current iterator location
2059 *
2060 * Returns the record that the current @iter is at.
2061 */
2062struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2063{
2064 return &iter->pg->records[iter->index];
2065}
2066
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302067static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002068ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002069{
2070 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002071 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002072
2073 ip = rec->ip;
2074
Steven Rostedt45a4a232011-04-21 23:16:46 -04002075 if (unlikely(ftrace_disabled))
2076 return 0;
2077
Shaohua Li25aac9d2009-01-09 11:29:40 +08002078 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002079 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002080 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302081 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002082 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302083 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002084}
2085
Steven Rostedt000ab692009-02-17 13:35:06 -05002086/*
2087 * archs can override this function if they must do something
2088 * before the modifying code is performed.
2089 */
2090int __weak ftrace_arch_code_modify_prepare(void)
2091{
2092 return 0;
2093}
2094
2095/*
2096 * archs can override this function if they must do something
2097 * after the modifying code is performed.
2098 */
2099int __weak ftrace_arch_code_modify_post_process(void)
2100{
2101 return 0;
2102}
2103
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002104void ftrace_modify_all_code(int command)
2105{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002106 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002107 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002108
2109 /*
2110 * If the ftrace_caller calls a ftrace_ops func directly,
2111 * we need to make sure that it only traces functions it
2112 * expects to trace. When doing the switch of functions,
2113 * we need to update to the ftrace_ops_list_func first
2114 * before the transition between old and new calls are set,
2115 * as the ftrace_ops_list_func will check the ops hashes
2116 * to make sure the ops are having the right functions
2117 * traced.
2118 */
Petr Mladekcd210672014-02-24 17:12:21 +01002119 if (update) {
2120 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2121 if (FTRACE_WARN_ON(err))
2122 return;
2123 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002124
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002125 if (command & FTRACE_UPDATE_CALLS)
2126 ftrace_replace_code(1);
2127 else if (command & FTRACE_DISABLE_CALLS)
2128 ftrace_replace_code(0);
2129
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002130 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2131 function_trace_op = set_function_trace_op;
2132 smp_wmb();
2133 /* If irqs are disabled, we are in stop machine */
2134 if (!irqs_disabled())
2135 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002136 err = ftrace_update_ftrace_func(ftrace_trace_function);
2137 if (FTRACE_WARN_ON(err))
2138 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002139 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002140
2141 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002142 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002143 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002144 err = ftrace_disable_ftrace_graph_caller();
2145 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002146}
2147
Ingo Molnare309b412008-05-12 21:20:51 +02002148static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002149{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002150 int *command = data;
2151
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002152 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002153
Steven Rostedtc88fd862011-08-16 09:53:39 -04002154 return 0;
2155}
2156
2157/**
2158 * ftrace_run_stop_machine, go back to the stop machine method
2159 * @command: The command to tell ftrace what to do
2160 *
2161 * If an arch needs to fall back to the stop machine method, the
2162 * it can call this function.
2163 */
2164void ftrace_run_stop_machine(int command)
2165{
2166 stop_machine(__ftrace_modify_code, &command, NULL);
2167}
2168
2169/**
2170 * arch_ftrace_update_code, modify the code to trace or not trace
2171 * @command: The command that needs to be done
2172 *
2173 * Archs can override this function if it does not need to
2174 * run stop_machine() to modify code.
2175 */
2176void __weak arch_ftrace_update_code(int command)
2177{
2178 ftrace_run_stop_machine(command);
2179}
2180
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002181static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2182{
2183 struct ftrace_page *pg;
2184 struct dyn_ftrace *rec;
2185 int size, bits;
2186 int ret;
2187
2188 size = ops->trampolines;
2189 bits = 0;
2190 /*
2191 * Make the hash size about 1/2 the # found
2192 */
2193 for (size /= 2; size; size >>= 1)
2194 bits++;
2195
2196 ops->tramp_hash = alloc_ftrace_hash(bits);
2197 /*
2198 * TODO: a failed allocation is going to screw up
2199 * the accounting of what needs to be modified
2200 * and not. For now, we kill ftrace if we fail
2201 * to allocate here. But there are ways around this,
2202 * but that will take a little more work.
2203 */
2204 if (!ops->tramp_hash)
2205 return -ENOMEM;
2206
2207 do_for_each_ftrace_rec(pg, rec) {
2208 if (ftrace_rec_count(rec) == 1 &&
2209 ftrace_ops_test(ops, rec->ip, rec)) {
2210
2211 /* This record had better have a trampoline */
2212 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2213 return -1;
2214
2215 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2216 if (ret < 0)
2217 return ret;
2218 }
2219 } while_for_each_ftrace_rec();
2220
2221 return 0;
2222}
2223
2224static int ftrace_save_tramp_hashes(void)
2225{
2226 struct ftrace_ops *op;
2227 int ret;
2228
2229 /*
2230 * Now that any trampoline is being used, we need to save the
2231 * hashes for the ops that have them. This allows the mapping
2232 * back from the record to the ops that has the trampoline to
2233 * know what code is being replaced. Modifying code must always
2234 * verify what it is changing.
2235 */
2236 do_for_each_ftrace_op(op, ftrace_ops_list) {
2237
2238 /* The tramp_hash is recreated each time. */
2239 free_ftrace_hash(op->tramp_hash);
2240 op->tramp_hash = NULL;
2241
2242 if (op->trampolines) {
2243 ret = ftrace_save_ops_tramp_hash(op);
2244 if (ret)
2245 return ret;
2246 }
2247
2248 } while_for_each_ftrace_op(op);
2249
2250 return 0;
2251}
2252
Steven Rostedtc88fd862011-08-16 09:53:39 -04002253static void ftrace_run_update_code(int command)
2254{
2255 int ret;
2256
2257 ret = ftrace_arch_code_modify_prepare();
2258 FTRACE_WARN_ON(ret);
2259 if (ret)
2260 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002261
2262 /*
2263 * By default we use stop_machine() to modify the code.
2264 * But archs can do what ever they want as long as it
2265 * is safe. The stop_machine() is the safest, but also
2266 * produces the most overhead.
2267 */
2268 arch_ftrace_update_code(command);
2269
Steven Rostedt000ab692009-02-17 13:35:06 -05002270 ret = ftrace_arch_code_modify_post_process();
2271 FTRACE_WARN_ON(ret);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002272
2273 ret = ftrace_save_tramp_hashes();
2274 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002275}
2276
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002277static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002278static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002279
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002280static void control_ops_free(struct ftrace_ops *ops)
2281{
2282 free_percpu(ops->disabled);
2283}
2284
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002285static void ftrace_startup_enable(int command)
2286{
2287 if (saved_ftrace_func != ftrace_trace_function) {
2288 saved_ftrace_func = ftrace_trace_function;
2289 command |= FTRACE_UPDATE_TRACE_FUNC;
2290 }
2291
2292 if (!command || !ftrace_enabled)
2293 return;
2294
2295 ftrace_run_update_code(command);
2296}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002297
Steven Rostedta1cd6172011-05-23 15:24:25 -04002298static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002299{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002300 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002301
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002302 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002303 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002304
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002305 ret = __register_ftrace_function(ops);
2306 if (ret)
2307 return ret;
2308
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002309 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002310 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002311
Steven Rostedted926f92011-05-03 13:25:24 -04002312 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002313
2314 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002315
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002316 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002317
2318 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002319}
2320
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002321static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002322{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002323 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002324
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002325 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002326 return -ENODEV;
2327
2328 ret = __unregister_ftrace_function(ops);
2329 if (ret)
2330 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002331
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002332 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002333 /*
2334 * Just warn in case of unbalance, no need to kill ftrace, it's not
2335 * critical but the ftrace_call callers may be never nopped again after
2336 * further ftrace uses.
2337 */
2338 WARN_ON_ONCE(ftrace_start_up < 0);
2339
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002340 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002341
Namhyung Kima737e6d2014-06-12 23:56:12 +09002342 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002343
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002344 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002345
2346 if (saved_ftrace_func != ftrace_trace_function) {
2347 saved_ftrace_func = ftrace_trace_function;
2348 command |= FTRACE_UPDATE_TRACE_FUNC;
2349 }
2350
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002351 if (!command || !ftrace_enabled) {
2352 /*
2353 * If these are control ops, they still need their
2354 * per_cpu field freed. Since, function tracing is
2355 * not currently active, we can just free them
2356 * without synchronizing all CPUs.
2357 */
2358 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2359 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002360 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002361 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002362
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002363 /*
2364 * If the ops uses a trampoline, then it needs to be
2365 * tested first on update.
2366 */
2367 removed_ops = ops;
2368
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002369 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002370
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002371 removed_ops = NULL;
2372
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002373 /*
2374 * Dynamic ops may be freed, we must make sure that all
2375 * callers are done before leaving this function.
2376 * The same goes for freeing the per_cpu data of the control
2377 * ops.
2378 *
2379 * Again, normal synchronize_sched() is not good enough.
2380 * We need to do a hard force of sched synchronization.
2381 * This is because we use preempt_disable() to do RCU, but
2382 * the function tracers can be called where RCU is not watching
2383 * (like before user_exit()). We can not rely on the RCU
2384 * infrastructure to do the synchronization, thus we must do it
2385 * ourselves.
2386 */
2387 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2388 schedule_on_each_cpu(ftrace_sync);
2389
2390 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2391 control_ops_free(ops);
2392 }
2393
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002394 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002395}
2396
Ingo Molnare309b412008-05-12 21:20:51 +02002397static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002398{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002399 if (unlikely(ftrace_disabled))
2400 return;
2401
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002402 /* Force update next time */
2403 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002404 /* ftrace_start_up is true if we want ftrace running */
2405 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002406 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002407}
2408
Ingo Molnare309b412008-05-12 21:20:51 +02002409static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002410{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002411 if (unlikely(ftrace_disabled))
2412 return;
2413
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002414 /* ftrace_start_up is true if ftrace is running */
2415 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002416 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002417}
2418
Steven Rostedt3d083392008-05-12 21:20:42 +02002419static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002420unsigned long ftrace_update_tot_cnt;
2421
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002422static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002423{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002424 /*
2425 * Filter_hash being empty will default to trace module.
2426 * But notrace hash requires a test of individual module functions.
2427 */
2428 return ftrace_hash_empty(ops->filter_hash) &&
2429 ftrace_hash_empty(ops->notrace_hash);
2430}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002431
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002432/*
2433 * Check if the current ops references the record.
2434 *
2435 * If the ops traces all functions, then it was already accounted for.
2436 * If the ops does not trace the current record function, skip it.
2437 * If the ops ignores the function via notrace filter, skip it.
2438 */
2439static inline bool
2440ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2441{
2442 /* If ops isn't enabled, ignore it */
2443 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2444 return 0;
2445
2446 /* If ops traces all mods, we already accounted for it */
2447 if (ops_traces_mod(ops))
2448 return 0;
2449
2450 /* The function must be in the filter */
2451 if (!ftrace_hash_empty(ops->filter_hash) &&
2452 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2453 return 0;
2454
2455 /* If in notrace hash, we ignore it too */
2456 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2457 return 0;
2458
2459 return 1;
2460}
2461
2462static int referenced_filters(struct dyn_ftrace *rec)
2463{
2464 struct ftrace_ops *ops;
2465 int cnt = 0;
2466
2467 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2468 if (ops_references_rec(ops, rec))
2469 cnt++;
2470 }
2471
2472 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002473}
2474
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002475static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002476{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002477 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002478 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302479 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002480 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002481 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002482 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002483 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002484
2485 /*
2486 * When adding a module, we need to check if tracers are
2487 * currently enabled and if they are set to trace all functions.
2488 * If they are, we need to enable the module functions as well
2489 * as update the reference counts for those function records.
2490 */
2491 if (mod) {
2492 struct ftrace_ops *ops;
2493
2494 for (ops = ftrace_ops_list;
2495 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002496 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2497 if (ops_traces_mod(ops))
2498 ref++;
2499 else
2500 test = true;
2501 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002502 }
2503 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002504
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002505 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002506
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002507 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302508
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002509 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002510 int cnt = ref;
2511
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002512 /* If something went wrong, bail without enabling anything */
2513 if (unlikely(ftrace_disabled))
2514 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002515
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002516 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002517 if (test)
2518 cnt += referenced_filters(p);
2519 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302520
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002521 /*
2522 * Do the initial record conversion from mcount jump
2523 * to the NOP instructions.
2524 */
2525 if (!ftrace_code_disable(mod, p))
2526 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002527
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002528 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002529
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002530 /*
2531 * If the tracing is enabled, go ahead and enable the record.
2532 *
2533 * The reason not to enable the record immediatelly is the
2534 * inherent check of ftrace_make_nop/ftrace_make_call for
2535 * correct previous instructions. Making first the NOP
2536 * conversion puts the module to the correct state, thus
2537 * passing the ftrace_make_call check.
2538 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002539 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002540 int failed = __ftrace_replace_code(p, 1);
2541 if (failed)
2542 ftrace_bug(failed, p->ip);
2543 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002544 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002545 }
2546
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002547 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002548 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002549 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002550
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002551 return 0;
2552}
2553
Steven Rostedta7900872011-12-16 16:23:44 -05002554static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002555{
Steven Rostedta7900872011-12-16 16:23:44 -05002556 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002557 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002558
Steven Rostedta7900872011-12-16 16:23:44 -05002559 if (WARN_ON(!count))
2560 return -EINVAL;
2561
2562 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002563
2564 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002565 * We want to fill as much as possible. No more than a page
2566 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002567 */
Steven Rostedta7900872011-12-16 16:23:44 -05002568 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2569 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002570
Steven Rostedta7900872011-12-16 16:23:44 -05002571 again:
2572 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2573
2574 if (!pg->records) {
2575 /* if we can't allocate this size, try something smaller */
2576 if (!order)
2577 return -ENOMEM;
2578 order >>= 1;
2579 goto again;
2580 }
2581
2582 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2583 pg->size = cnt;
2584
2585 if (cnt > count)
2586 cnt = count;
2587
2588 return cnt;
2589}
2590
2591static struct ftrace_page *
2592ftrace_allocate_pages(unsigned long num_to_init)
2593{
2594 struct ftrace_page *start_pg;
2595 struct ftrace_page *pg;
2596 int order;
2597 int cnt;
2598
2599 if (!num_to_init)
2600 return 0;
2601
2602 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2603 if (!pg)
2604 return NULL;
2605
2606 /*
2607 * Try to allocate as much as possible in one continues
2608 * location that fills in all of the space. We want to
2609 * waste as little space as possible.
2610 */
2611 for (;;) {
2612 cnt = ftrace_allocate_records(pg, num_to_init);
2613 if (cnt < 0)
2614 goto free_pages;
2615
2616 num_to_init -= cnt;
2617 if (!num_to_init)
2618 break;
2619
2620 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2621 if (!pg->next)
2622 goto free_pages;
2623
2624 pg = pg->next;
2625 }
2626
2627 return start_pg;
2628
2629 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002630 pg = start_pg;
2631 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002632 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2633 free_pages((unsigned long)pg->records, order);
2634 start_pg = pg->next;
2635 kfree(pg);
2636 pg = start_pg;
2637 }
2638 pr_info("ftrace: FAILED to allocate memory for functions\n");
2639 return NULL;
2640}
2641
Steven Rostedt5072c592008-05-12 21:20:43 +02002642#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2643
2644struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002645 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002646 loff_t func_pos;
2647 struct ftrace_page *pg;
2648 struct dyn_ftrace *func;
2649 struct ftrace_func_probe *probe;
2650 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002651 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002652 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002653 int hidx;
2654 int idx;
2655 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002656};
2657
Ingo Molnare309b412008-05-12 21:20:51 +02002658static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002659t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002660{
2661 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002662 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002663 struct hlist_head *hhd;
2664
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002665 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002666 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002667
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002668 if (iter->probe)
2669 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002670 retry:
2671 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2672 return NULL;
2673
2674 hhd = &ftrace_func_hash[iter->hidx];
2675
2676 if (hlist_empty(hhd)) {
2677 iter->hidx++;
2678 hnd = NULL;
2679 goto retry;
2680 }
2681
2682 if (!hnd)
2683 hnd = hhd->first;
2684 else {
2685 hnd = hnd->next;
2686 if (!hnd) {
2687 iter->hidx++;
2688 goto retry;
2689 }
2690 }
2691
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002692 if (WARN_ON_ONCE(!hnd))
2693 return NULL;
2694
2695 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2696
2697 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002698}
2699
2700static void *t_hash_start(struct seq_file *m, loff_t *pos)
2701{
2702 struct ftrace_iterator *iter = m->private;
2703 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002704 loff_t l;
2705
Steven Rostedt69a30832011-12-19 15:21:16 -05002706 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2707 return NULL;
2708
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002709 if (iter->func_pos > *pos)
2710 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002711
Li Zefand82d6242009-06-24 09:54:54 +08002712 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002713 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002714 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002715 if (!p)
2716 break;
2717 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002718 if (!p)
2719 return NULL;
2720
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002721 /* Only set this if we have an item */
2722 iter->flags |= FTRACE_ITER_HASH;
2723
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002724 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002725}
2726
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002727static int
2728t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002729{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002730 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002731
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002732 rec = iter->probe;
2733 if (WARN_ON_ONCE(!rec))
2734 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002735
Steven Rostedt809dcf22009-02-16 23:06:01 -05002736 if (rec->ops->print)
2737 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2738
Steven Rostedtb375a112009-09-17 00:05:58 -04002739 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002740
2741 if (rec->data)
2742 seq_printf(m, ":%p", rec->data);
2743 seq_putc(m, '\n');
2744
2745 return 0;
2746}
2747
2748static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002749t_next(struct seq_file *m, void *v, loff_t *pos)
2750{
2751 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002752 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002753 struct dyn_ftrace *rec = NULL;
2754
Steven Rostedt45a4a232011-04-21 23:16:46 -04002755 if (unlikely(ftrace_disabled))
2756 return NULL;
2757
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002758 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002759 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002760
Steven Rostedt5072c592008-05-12 21:20:43 +02002761 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002762 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002763
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002764 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002765 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002766
Steven Rostedt5072c592008-05-12 21:20:43 +02002767 retry:
2768 if (iter->idx >= iter->pg->index) {
2769 if (iter->pg->next) {
2770 iter->pg = iter->pg->next;
2771 iter->idx = 0;
2772 goto retry;
2773 }
2774 } else {
2775 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002776 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002777 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002778
Steven Rostedt41c52c02008-05-22 11:46:33 -04002779 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002780 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2781
2782 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002783 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002784
Steven Rostedt5072c592008-05-12 21:20:43 +02002785 rec = NULL;
2786 goto retry;
2787 }
2788 }
2789
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002790 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002791 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002792
2793 iter->func = rec;
2794
2795 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002796}
2797
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002798static void reset_iter_read(struct ftrace_iterator *iter)
2799{
2800 iter->pos = 0;
2801 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002802 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002803}
2804
2805static void *t_start(struct seq_file *m, loff_t *pos)
2806{
2807 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002808 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002809 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002810 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002811
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002812 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002813
2814 if (unlikely(ftrace_disabled))
2815 return NULL;
2816
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002817 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002818 * If an lseek was done, then reset and start from beginning.
2819 */
2820 if (*pos < iter->pos)
2821 reset_iter_read(iter);
2822
2823 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002824 * For set_ftrace_filter reading, if we have the filter
2825 * off, we can short cut and just print out that all
2826 * functions are enabled.
2827 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002828 if ((iter->flags & FTRACE_ITER_FILTER &&
2829 ftrace_hash_empty(ops->filter_hash)) ||
2830 (iter->flags & FTRACE_ITER_NOTRACE &&
2831 ftrace_hash_empty(ops->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002832 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002833 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002834 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002835 /* reset in case of seek/pread */
2836 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002837 return iter;
2838 }
2839
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002840 if (iter->flags & FTRACE_ITER_HASH)
2841 return t_hash_start(m, pos);
2842
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002843 /*
2844 * Unfortunately, we need to restart at ftrace_pages_start
2845 * every time we let go of the ftrace_mutex. This is because
2846 * those pointers can change without the lock.
2847 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002848 iter->pg = ftrace_pages_start;
2849 iter->idx = 0;
2850 for (l = 0; l <= *pos; ) {
2851 p = t_next(m, p, &l);
2852 if (!p)
2853 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002854 }
walimis5821e1b2008-11-15 15:19:06 +08002855
Steven Rostedt69a30832011-12-19 15:21:16 -05002856 if (!p)
2857 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002858
2859 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002860}
2861
2862static void t_stop(struct seq_file *m, void *p)
2863{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002864 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002865}
2866
2867static int t_show(struct seq_file *m, void *v)
2868{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002869 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002870 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002871
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002872 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002873 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002874
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002875 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002876 if (iter->flags & FTRACE_ITER_NOTRACE)
2877 seq_printf(m, "#### no functions disabled ####\n");
2878 else
2879 seq_printf(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002880 return 0;
2881 }
2882
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002883 rec = iter->func;
2884
Steven Rostedt5072c592008-05-12 21:20:43 +02002885 if (!rec)
2886 return 0;
2887
Steven Rostedt647bcd02011-05-03 14:39:21 -04002888 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002889 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002890 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002891 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002892 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2893 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2894 struct ftrace_ops *ops;
2895
2896 ops = ftrace_find_tramp_ops_curr(rec);
2897 if (ops && ops->trampoline)
2898 seq_printf(m, "\ttramp: %pS",
2899 (void *)ops->trampoline);
2900 else
2901 seq_printf(m, "\ttramp: ERROR!");
2902 }
2903 }
2904
Steven Rostedt647bcd02011-05-03 14:39:21 -04002905 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002906
2907 return 0;
2908}
2909
James Morris88e9d342009-09-22 16:43:43 -07002910static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002911 .start = t_start,
2912 .next = t_next,
2913 .stop = t_stop,
2914 .show = t_show,
2915};
2916
Ingo Molnare309b412008-05-12 21:20:51 +02002917static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002918ftrace_avail_open(struct inode *inode, struct file *file)
2919{
2920 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002921
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002922 if (unlikely(ftrace_disabled))
2923 return -ENODEV;
2924
Jiri Olsa50e18b92012-04-25 10:23:39 +02002925 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2926 if (iter) {
2927 iter->pg = ftrace_pages_start;
2928 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002929 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002930
Jiri Olsa50e18b92012-04-25 10:23:39 +02002931 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002932}
2933
Steven Rostedt647bcd02011-05-03 14:39:21 -04002934static int
2935ftrace_enabled_open(struct inode *inode, struct file *file)
2936{
2937 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002938
2939 if (unlikely(ftrace_disabled))
2940 return -ENODEV;
2941
Jiri Olsa50e18b92012-04-25 10:23:39 +02002942 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2943 if (iter) {
2944 iter->pg = ftrace_pages_start;
2945 iter->flags = FTRACE_ITER_ENABLED;
2946 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002947 }
2948
Jiri Olsa50e18b92012-04-25 10:23:39 +02002949 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002950}
2951
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002952static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002953{
Steven Rostedt52baf112009-02-14 01:15:39 -05002954 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002955 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002956 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002957}
2958
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002959/**
2960 * ftrace_regex_open - initialize function tracer filter files
2961 * @ops: The ftrace_ops that hold the hash filters
2962 * @flag: The type of filter to process
2963 * @inode: The inode, usually passed in to your open routine
2964 * @file: The file, usually passed in to your open routine
2965 *
2966 * ftrace_regex_open() initializes the filter files for the
2967 * @ops. Depending on @flag it may process the filter hash or
2968 * the notrace hash of @ops. With this called from the open
2969 * routine, you can use ftrace_filter_write() for the write
2970 * routine if @flag has FTRACE_ITER_FILTER set, or
2971 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002972 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002973 * release must call ftrace_regex_release().
2974 */
2975int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002976ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002977 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002978{
2979 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002980 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002981 int ret = 0;
2982
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002983 ftrace_ops_init(ops);
2984
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002985 if (unlikely(ftrace_disabled))
2986 return -ENODEV;
2987
Steven Rostedt5072c592008-05-12 21:20:43 +02002988 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2989 if (!iter)
2990 return -ENOMEM;
2991
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002992 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2993 kfree(iter);
2994 return -ENOMEM;
2995 }
2996
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002997 iter->ops = ops;
2998 iter->flags = flag;
2999
3000 mutex_lock(&ops->regex_lock);
3001
Steven Rostedtf45948e2011-05-02 12:29:25 -04003002 if (flag & FTRACE_ITER_NOTRACE)
3003 hash = ops->notrace_hash;
3004 else
3005 hash = ops->filter_hash;
3006
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003007 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003008 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3009
3010 if (file->f_flags & O_TRUNC)
3011 iter->hash = alloc_ftrace_hash(size_bits);
3012 else
3013 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3014
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003015 if (!iter->hash) {
3016 trace_parser_put(&iter->parser);
3017 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003018 ret = -ENOMEM;
3019 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003020 }
3021 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003022
Steven Rostedt5072c592008-05-12 21:20:43 +02003023 if (file->f_mode & FMODE_READ) {
3024 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003025
3026 ret = seq_open(file, &show_ftrace_seq_ops);
3027 if (!ret) {
3028 struct seq_file *m = file->private_data;
3029 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003030 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003031 /* Failed */
3032 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003033 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003034 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003035 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003036 } else
3037 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003038
3039 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003040 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003041
3042 return ret;
3043}
3044
Steven Rostedt41c52c02008-05-22 11:46:33 -04003045static int
3046ftrace_filter_open(struct inode *inode, struct file *file)
3047{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003048 struct ftrace_ops *ops = inode->i_private;
3049
3050 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003051 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3052 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003053}
3054
3055static int
3056ftrace_notrace_open(struct inode *inode, struct file *file)
3057{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003058 struct ftrace_ops *ops = inode->i_private;
3059
3060 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003061 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003062}
3063
Steven Rostedt64e7c442009-02-13 17:08:48 -05003064static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003065{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003066 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003067 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003068
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003069 switch (type) {
3070 case MATCH_FULL:
3071 if (strcmp(str, regex) == 0)
3072 matched = 1;
3073 break;
3074 case MATCH_FRONT_ONLY:
3075 if (strncmp(str, regex, len) == 0)
3076 matched = 1;
3077 break;
3078 case MATCH_MIDDLE_ONLY:
3079 if (strstr(str, regex))
3080 matched = 1;
3081 break;
3082 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003083 slen = strlen(str);
3084 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003085 matched = 1;
3086 break;
3087 }
3088
3089 return matched;
3090}
3091
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003092static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003093enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003094{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003095 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003096 int ret = 0;
3097
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003098 entry = ftrace_lookup_ip(hash, rec->ip);
3099 if (not) {
3100 /* Do nothing if it doesn't exist */
3101 if (!entry)
3102 return 0;
3103
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003104 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003105 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003106 /* Do nothing if it exists */
3107 if (entry)
3108 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003109
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003110 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003111 }
3112 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003113}
3114
Steven Rostedt64e7c442009-02-13 17:08:48 -05003115static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003116ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3117 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003118{
3119 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003120 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003121
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003122 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3123
3124 if (mod) {
3125 /* module lookup requires matching the module */
3126 if (!modname || strcmp(modname, mod))
3127 return 0;
3128
3129 /* blank search means to match all funcs in the mod */
3130 if (!len)
3131 return 1;
3132 }
3133
Steven Rostedt64e7c442009-02-13 17:08:48 -05003134 return ftrace_match(str, regex, len, type);
3135}
3136
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003137static int
3138match_records(struct ftrace_hash *hash, char *buff,
3139 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003140{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003141 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003142 struct ftrace_page *pg;
3143 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003144 int type = MATCH_FULL;
3145 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003146 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003147 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003148
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003149 if (len) {
3150 type = filter_parse_regex(buff, len, &search, &not);
3151 search_len = strlen(search);
3152 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003153
Steven Rostedt52baf112009-02-14 01:15:39 -05003154 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003155
3156 if (unlikely(ftrace_disabled))
3157 goto out_unlock;
3158
Steven Rostedt265c8312009-02-13 12:43:56 -05003159 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003160 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003161 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003162 if (ret < 0) {
3163 found = ret;
3164 goto out_unlock;
3165 }
Li Zefan311d16d2009-12-08 11:15:11 +08003166 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003167 }
3168 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003169 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003170 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003171
3172 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003173}
3174
Steven Rostedt64e7c442009-02-13 17:08:48 -05003175static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003176ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003177{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003178 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003179}
3180
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003181static int
3182ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003183{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003184 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003185
Steven Rostedt64e7c442009-02-13 17:08:48 -05003186 /* blank or '*' mean the same */
3187 if (strcmp(buff, "*") == 0)
3188 buff[0] = 0;
3189
3190 /* handle the case of 'dont filter this module' */
3191 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3192 buff[0] = 0;
3193 not = 1;
3194 }
3195
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003196 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003197}
3198
Steven Rostedtf6180772009-02-14 00:40:25 -05003199/*
3200 * We register the module command as a template to show others how
3201 * to register the a command as well.
3202 */
3203
3204static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003205ftrace_mod_callback(struct ftrace_hash *hash,
3206 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003207{
3208 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003209 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003210
3211 /*
3212 * cmd == 'mod' because we only registered this func
3213 * for the 'mod' ftrace_func_command.
3214 * But if you register one func with multiple commands,
3215 * you can tell which command was used by the cmd
3216 * parameter.
3217 */
3218
3219 /* we must have a module name */
3220 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003221 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003222
3223 mod = strsep(&param, ":");
3224 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003225 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003226
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003227 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003228 if (!ret)
3229 ret = -EINVAL;
3230 if (ret < 0)
3231 return ret;
3232
3233 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003234}
3235
3236static struct ftrace_func_command ftrace_mod_cmd = {
3237 .name = "mod",
3238 .func = ftrace_mod_callback,
3239};
3240
3241static int __init ftrace_mod_cmd_init(void)
3242{
3243 return register_ftrace_command(&ftrace_mod_cmd);
3244}
Steven Rostedt6f415672012-10-05 12:13:07 -04003245core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003246
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003247static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003248 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003249{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003250 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003251 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003252 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003253
3254 key = hash_long(ip, FTRACE_HASH_BITS);
3255
3256 hhd = &ftrace_func_hash[key];
3257
3258 if (hlist_empty(hhd))
3259 return;
3260
3261 /*
3262 * Disable preemption for these calls to prevent a RCU grace
3263 * period. This syncs the hash iteration and freeing of items
3264 * on the hash. rcu_read_lock is too dangerous here.
3265 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003266 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003267 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003268 if (entry->ip == ip)
3269 entry->ops->func(ip, parent_ip, &entry->data);
3270 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003271 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003272}
3273
Steven Rostedtb6887d72009-02-17 12:32:04 -05003274static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003275{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003276 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003277 .flags = FTRACE_OPS_FL_INITIALIZED,
3278 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003279};
3280
Steven Rostedtb6887d72009-02-17 12:32:04 -05003281static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003282
Steven Rostedtb6887d72009-02-17 12:32:04 -05003283static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003284{
Steven Rostedtb8489142011-05-04 09:27:52 -04003285 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003286 int i;
3287
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003288 if (ftrace_probe_registered) {
3289 /* still need to update the function call sites */
3290 if (ftrace_enabled)
3291 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003292 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003293 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003294
3295 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3296 struct hlist_head *hhd = &ftrace_func_hash[i];
3297 if (hhd->first)
3298 break;
3299 }
3300 /* Nothing registered? */
3301 if (i == FTRACE_FUNC_HASHSIZE)
3302 return;
3303
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003304 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003305
Steven Rostedtb6887d72009-02-17 12:32:04 -05003306 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003307}
3308
Steven Rostedtb6887d72009-02-17 12:32:04 -05003309static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003310{
3311 int i;
3312
Steven Rostedtb6887d72009-02-17 12:32:04 -05003313 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003314 return;
3315
3316 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3317 struct hlist_head *hhd = &ftrace_func_hash[i];
3318 if (hhd->first)
3319 return;
3320 }
3321
3322 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003323 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003324
Steven Rostedtb6887d72009-02-17 12:32:04 -05003325 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003326}
3327
3328
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003329static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003330{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003331 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003332 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003333 kfree(entry);
3334}
3335
Steven Rostedt59df055f2009-02-14 15:29:06 -05003336int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003337register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003338 void *data)
3339{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003340 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003341 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3342 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003343 struct ftrace_page *pg;
3344 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003345 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003346 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003347 int count = 0;
3348 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003349 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003350
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003351 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003352 len = strlen(search);
3353
Steven Rostedtb6887d72009-02-17 12:32:04 -05003354 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003355 if (WARN_ON(not))
3356 return -EINVAL;
3357
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003358 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003359
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003360 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3361 if (!hash) {
3362 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003363 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003364 }
3365
3366 if (unlikely(ftrace_disabled)) {
3367 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003368 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003369 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003370
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003371 mutex_lock(&ftrace_lock);
3372
Steven Rostedt59df055f2009-02-14 15:29:06 -05003373 do_for_each_ftrace_rec(pg, rec) {
3374
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003375 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003376 continue;
3377
3378 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3379 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003380 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003381 if (!count)
3382 count = -ENOMEM;
3383 goto out_unlock;
3384 }
3385
3386 count++;
3387
3388 entry->data = data;
3389
3390 /*
3391 * The caller might want to do something special
3392 * for each function we find. We call the callback
3393 * to give the caller an opportunity to do so.
3394 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003395 if (ops->init) {
3396 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003397 /* caller does not like this func */
3398 kfree(entry);
3399 continue;
3400 }
3401 }
3402
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003403 ret = enter_record(hash, rec, 0);
3404 if (ret < 0) {
3405 kfree(entry);
3406 count = ret;
3407 goto out_unlock;
3408 }
3409
Steven Rostedt59df055f2009-02-14 15:29:06 -05003410 entry->ops = ops;
3411 entry->ip = rec->ip;
3412
3413 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3414 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3415
3416 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003417
3418 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3419 if (ret < 0)
3420 count = ret;
3421
Steven Rostedtb6887d72009-02-17 12:32:04 -05003422 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003423
3424 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003425 mutex_unlock(&ftrace_lock);
3426 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003427 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003428 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003429
3430 return count;
3431}
3432
3433enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003434 PROBE_TEST_FUNC = 1,
3435 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003436};
3437
3438static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003439__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003440 void *data, int flags)
3441{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003442 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003443 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003444 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003445 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003446 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003447 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003448 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003449 char str[KSYM_SYMBOL_LEN];
3450 int type = MATCH_FULL;
3451 int i, len = 0;
3452 char *search;
3453
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003454 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003455 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003456 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003457 int not;
3458
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003459 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003460 len = strlen(search);
3461
Steven Rostedtb6887d72009-02-17 12:32:04 -05003462 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003463 if (WARN_ON(not))
3464 return;
3465 }
3466
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003467 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003468
3469 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3470 if (!hash)
3471 /* Hmm, should report this somehow */
3472 goto out_unlock;
3473
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003474 INIT_LIST_HEAD(&free_list);
3475
Steven Rostedt59df055f2009-02-14 15:29:06 -05003476 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3477 struct hlist_head *hhd = &ftrace_func_hash[i];
3478
Sasha Levinb67bfe02013-02-27 17:06:00 -08003479 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003480
3481 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003482 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003483 continue;
3484
Steven Rostedtb6887d72009-02-17 12:32:04 -05003485 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003486 continue;
3487
3488 /* do this last, since it is the most expensive */
3489 if (glob) {
3490 kallsyms_lookup(entry->ip, NULL, NULL,
3491 NULL, str);
3492 if (!ftrace_match(str, glob, len, type))
3493 continue;
3494 }
3495
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003496 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3497 /* It is possible more than one entry had this ip */
3498 if (rec_entry)
3499 free_hash_entry(hash, rec_entry);
3500
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003501 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003502 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003503 }
3504 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003505 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003506 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003507 /*
3508 * Remove after the disable is called. Otherwise, if the last
3509 * probe is removed, a null hash means *all enabled*.
3510 */
3511 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003512 synchronize_sched();
3513 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3514 list_del(&entry->free_list);
3515 ftrace_free_entry(entry);
3516 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003517 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003518
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003519 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003520 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003521 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003522}
3523
3524void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003525unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003526 void *data)
3527{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003528 __unregister_ftrace_function_probe(glob, ops, data,
3529 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003530}
3531
3532void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003533unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003534{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003535 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003536}
3537
Steven Rostedtb6887d72009-02-17 12:32:04 -05003538void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003539{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003540 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003541}
3542
Steven Rostedtf6180772009-02-14 00:40:25 -05003543static LIST_HEAD(ftrace_commands);
3544static DEFINE_MUTEX(ftrace_cmd_mutex);
3545
Tom Zanussi38de93a2013-10-24 08:34:18 -05003546/*
3547 * Currently we only register ftrace commands from __init, so mark this
3548 * __init too.
3549 */
3550__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003551{
3552 struct ftrace_func_command *p;
3553 int ret = 0;
3554
3555 mutex_lock(&ftrace_cmd_mutex);
3556 list_for_each_entry(p, &ftrace_commands, list) {
3557 if (strcmp(cmd->name, p->name) == 0) {
3558 ret = -EBUSY;
3559 goto out_unlock;
3560 }
3561 }
3562 list_add(&cmd->list, &ftrace_commands);
3563 out_unlock:
3564 mutex_unlock(&ftrace_cmd_mutex);
3565
3566 return ret;
3567}
3568
Tom Zanussi38de93a2013-10-24 08:34:18 -05003569/*
3570 * Currently we only unregister ftrace commands from __init, so mark
3571 * this __init too.
3572 */
3573__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003574{
3575 struct ftrace_func_command *p, *n;
3576 int ret = -ENODEV;
3577
3578 mutex_lock(&ftrace_cmd_mutex);
3579 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3580 if (strcmp(cmd->name, p->name) == 0) {
3581 ret = 0;
3582 list_del_init(&p->list);
3583 goto out_unlock;
3584 }
3585 }
3586 out_unlock:
3587 mutex_unlock(&ftrace_cmd_mutex);
3588
3589 return ret;
3590}
3591
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003592static int ftrace_process_regex(struct ftrace_hash *hash,
3593 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003594{
Steven Rostedtf6180772009-02-14 00:40:25 -05003595 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003596 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003597 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003598
3599 func = strsep(&next, ":");
3600
3601 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003602 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003603 if (!ret)
3604 ret = -EINVAL;
3605 if (ret < 0)
3606 return ret;
3607 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003608 }
3609
Steven Rostedtf6180772009-02-14 00:40:25 -05003610 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003611
3612 command = strsep(&next, ":");
3613
Steven Rostedtf6180772009-02-14 00:40:25 -05003614 mutex_lock(&ftrace_cmd_mutex);
3615 list_for_each_entry(p, &ftrace_commands, list) {
3616 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003617 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003618 goto out_unlock;
3619 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003620 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003621 out_unlock:
3622 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003623
Steven Rostedtf6180772009-02-14 00:40:25 -05003624 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003625}
3626
Ingo Molnare309b412008-05-12 21:20:51 +02003627static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003628ftrace_regex_write(struct file *file, const char __user *ubuf,
3629 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003630{
3631 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003632 struct trace_parser *parser;
3633 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003634
Li Zefan4ba79782009-09-22 13:52:20 +08003635 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003636 return 0;
3637
Steven Rostedt5072c592008-05-12 21:20:43 +02003638 if (file->f_mode & FMODE_READ) {
3639 struct seq_file *m = file->private_data;
3640 iter = m->private;
3641 } else
3642 iter = file->private_data;
3643
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003644 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003645 return -ENODEV;
3646
3647 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003648
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003649 parser = &iter->parser;
3650 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003651
Li Zefan4ba79782009-09-22 13:52:20 +08003652 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003653 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003654 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003655 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003656 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003657 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003658 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003659 }
3660
Steven Rostedt5072c592008-05-12 21:20:43 +02003661 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003662 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003663 return ret;
3664}
3665
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003666ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003667ftrace_filter_write(struct file *file, const char __user *ubuf,
3668 size_t cnt, loff_t *ppos)
3669{
3670 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3671}
3672
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003673ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003674ftrace_notrace_write(struct file *file, const char __user *ubuf,
3675 size_t cnt, loff_t *ppos)
3676{
3677 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3678}
3679
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003680static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003681ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3682{
3683 struct ftrace_func_entry *entry;
3684
3685 if (!ftrace_location(ip))
3686 return -EINVAL;
3687
3688 if (remove) {
3689 entry = ftrace_lookup_ip(hash, ip);
3690 if (!entry)
3691 return -ENOENT;
3692 free_hash_entry(hash, entry);
3693 return 0;
3694 }
3695
3696 return add_hash_entry(hash, ip);
3697}
3698
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003699static void ftrace_ops_update_code(struct ftrace_ops *ops)
3700{
3701 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3702 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3703}
3704
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003705static int
3706ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3707 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003708{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003709 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003710 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003711 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003712
Steven Rostedt41c52c02008-05-22 11:46:33 -04003713 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003714 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003715
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003716 mutex_lock(&ops->regex_lock);
3717
Steven Rostedtf45948e2011-05-02 12:29:25 -04003718 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003719 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003720 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003721 orig_hash = &ops->notrace_hash;
3722
3723 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003724 if (!hash) {
3725 ret = -ENOMEM;
3726 goto out_regex_unlock;
3727 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003728
Steven Rostedt41c52c02008-05-22 11:46:33 -04003729 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003730 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003731 if (buf && !ftrace_match_records(hash, buf, len)) {
3732 ret = -EINVAL;
3733 goto out_regex_unlock;
3734 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003735 if (ip) {
3736 ret = ftrace_match_addr(hash, ip, remove);
3737 if (ret < 0)
3738 goto out_regex_unlock;
3739 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003740
3741 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003742 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003743 if (!ret)
3744 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003745
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003746 mutex_unlock(&ftrace_lock);
3747
Jiri Olsaac483c42012-01-02 10:04:14 +01003748 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003749 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003750
3751 free_ftrace_hash(hash);
3752 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003753}
3754
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003755static int
3756ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3757 int reset, int enable)
3758{
3759 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3760}
3761
3762/**
3763 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3764 * @ops - the ops to set the filter with
3765 * @ip - the address to add to or remove from the filter.
3766 * @remove - non zero to remove the ip from the filter
3767 * @reset - non zero to reset all filters before applying this filter.
3768 *
3769 * Filters denote which functions should be enabled when tracing is enabled
3770 * If @ip is NULL, it failes to update filter.
3771 */
3772int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3773 int remove, int reset)
3774{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003775 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003776 return ftrace_set_addr(ops, ip, remove, reset, 1);
3777}
3778EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3779
3780static int
3781ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3782 int reset, int enable)
3783{
3784 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3785}
3786
Steven Rostedt77a2b372008-05-12 21:20:45 +02003787/**
3788 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003789 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003790 * @buf - the string that holds the function filter text.
3791 * @len - the length of the string.
3792 * @reset - non zero to reset all filters before applying this filter.
3793 *
3794 * Filters denote which functions should be enabled when tracing is enabled.
3795 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3796 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003797int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003798 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003799{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003800 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003801 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003802}
Steven Rostedt936e0742011-05-05 22:54:01 -04003803EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003804
Steven Rostedt41c52c02008-05-22 11:46:33 -04003805/**
3806 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003807 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003808 * @buf - the string that holds the function notrace text.
3809 * @len - the length of the string.
3810 * @reset - non zero to reset all filters before applying this filter.
3811 *
3812 * Notrace Filters denote which functions should not be enabled when tracing
3813 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3814 * for tracing.
3815 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003816int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003817 int len, int reset)
3818{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003819 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003820 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003821}
3822EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3823/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003824 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003825 * @buf - the string that holds the function filter text.
3826 * @len - the length of the string.
3827 * @reset - non zero to reset all filters before applying this filter.
3828 *
3829 * Filters denote which functions should be enabled when tracing is enabled.
3830 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3831 */
3832void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3833{
3834 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3835}
3836EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3837
3838/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003839 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003840 * @buf - the string that holds the function notrace text.
3841 * @len - the length of the string.
3842 * @reset - non zero to reset all filters before applying this filter.
3843 *
3844 * Notrace Filters denote which functions should not be enabled when tracing
3845 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3846 * for tracing.
3847 */
3848void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003849{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003850 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003851}
Steven Rostedt936e0742011-05-05 22:54:01 -04003852EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003853
Steven Rostedt2af15d62009-05-28 13:37:24 -04003854/*
3855 * command line interface to allow users to set filters on boot up.
3856 */
3857#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3858static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3859static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3860
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003861/* Used by function selftest to not test if filter is set */
3862bool ftrace_filter_param __initdata;
3863
Steven Rostedt2af15d62009-05-28 13:37:24 -04003864static int __init set_ftrace_notrace(char *str)
3865{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003866 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003867 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003868 return 1;
3869}
3870__setup("ftrace_notrace=", set_ftrace_notrace);
3871
3872static int __init set_ftrace_filter(char *str)
3873{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003874 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003875 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003876 return 1;
3877}
3878__setup("ftrace_filter=", set_ftrace_filter);
3879
Stefan Assmann369bc182009-10-12 22:17:21 +02003880#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003881static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003882static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003883static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003884
Stefan Assmann369bc182009-10-12 22:17:21 +02003885static int __init set_graph_function(char *str)
3886{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003887 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003888 return 1;
3889}
3890__setup("ftrace_graph_filter=", set_graph_function);
3891
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003892static int __init set_graph_notrace_function(char *str)
3893{
3894 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3895 return 1;
3896}
3897__setup("ftrace_graph_notrace=", set_graph_notrace_function);
3898
3899static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02003900{
3901 int ret;
3902 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003903 unsigned long *table = ftrace_graph_funcs;
3904 int *count = &ftrace_graph_count;
3905
3906 if (!enable) {
3907 table = ftrace_graph_notrace_funcs;
3908 count = &ftrace_graph_notrace_count;
3909 }
Stefan Assmann369bc182009-10-12 22:17:21 +02003910
3911 while (buf) {
3912 func = strsep(&buf, ",");
3913 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003914 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003915 if (ret)
3916 printk(KERN_DEBUG "ftrace: function %s not "
3917 "traceable\n", func);
3918 }
3919}
3920#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3921
Steven Rostedt2a85a372011-12-19 21:57:44 -05003922void __init
3923ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003924{
3925 char *func;
3926
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003927 ftrace_ops_init(ops);
3928
Steven Rostedt2af15d62009-05-28 13:37:24 -04003929 while (buf) {
3930 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003931 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003932 }
3933}
3934
3935static void __init set_ftrace_early_filters(void)
3936{
3937 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003938 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003939 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003940 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003941#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3942 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003943 set_ftrace_early_graph(ftrace_graph_buf, 1);
3944 if (ftrace_graph_notrace_buf[0])
3945 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003946#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003947}
3948
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003949int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003950{
3951 struct seq_file *m = (struct seq_file *)file->private_data;
3952 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003953 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003954 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003955 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003956 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003957
Steven Rostedt5072c592008-05-12 21:20:43 +02003958 if (file->f_mode & FMODE_READ) {
3959 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003960 seq_release(inode, file);
3961 } else
3962 iter = file->private_data;
3963
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003964 parser = &iter->parser;
3965 if (trace_parser_loaded(parser)) {
3966 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003967 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003968 }
3969
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003970 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003971
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003972 mutex_lock(&iter->ops->regex_lock);
3973
Steven Rostedt058e2972011-04-29 22:35:33 -04003974 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003975 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3976
3977 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003978 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003979 else
3980 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003981
Steven Rostedt058e2972011-04-29 22:35:33 -04003982 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003983 ret = ftrace_hash_move(iter->ops, filter_hash,
3984 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003985 if (!ret)
3986 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003987
Steven Rostedt058e2972011-04-29 22:35:33 -04003988 mutex_unlock(&ftrace_lock);
3989 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003990
3991 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003992 free_ftrace_hash(iter->hash);
3993 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003994
Steven Rostedt5072c592008-05-12 21:20:43 +02003995 return 0;
3996}
3997
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003998static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003999 .open = ftrace_avail_open,
4000 .read = seq_read,
4001 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004002 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004003};
4004
Steven Rostedt647bcd02011-05-03 14:39:21 -04004005static const struct file_operations ftrace_enabled_fops = {
4006 .open = ftrace_enabled_open,
4007 .read = seq_read,
4008 .llseek = seq_lseek,
4009 .release = seq_release_private,
4010};
4011
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004012static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004013 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004014 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004015 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004016 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004017 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004018};
4019
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004020static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004021 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004022 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004023 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004024 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004025 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004026};
4027
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004028#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4029
4030static DEFINE_MUTEX(graph_lock);
4031
4032int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004033int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004034unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004035unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004036
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004037struct ftrace_graph_data {
4038 unsigned long *table;
4039 size_t size;
4040 int *count;
4041 const struct seq_operations *seq_ops;
4042};
4043
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004044static void *
Li Zefan85951842009-06-24 09:54:00 +08004045__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004046{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004047 struct ftrace_graph_data *fgd = m->private;
4048
4049 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004050 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004051 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004052}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004053
Li Zefan85951842009-06-24 09:54:00 +08004054static void *
4055g_next(struct seq_file *m, void *v, loff_t *pos)
4056{
4057 (*pos)++;
4058 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004059}
4060
4061static void *g_start(struct seq_file *m, loff_t *pos)
4062{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004063 struct ftrace_graph_data *fgd = m->private;
4064
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004065 mutex_lock(&graph_lock);
4066
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004067 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004068 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004069 return (void *)1;
4070
Li Zefan85951842009-06-24 09:54:00 +08004071 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004072}
4073
4074static void g_stop(struct seq_file *m, void *p)
4075{
4076 mutex_unlock(&graph_lock);
4077}
4078
4079static int g_show(struct seq_file *m, void *v)
4080{
4081 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004082
4083 if (!ptr)
4084 return 0;
4085
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004086 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004087 struct ftrace_graph_data *fgd = m->private;
4088
4089 if (fgd->table == ftrace_graph_funcs)
4090 seq_printf(m, "#### all functions enabled ####\n");
4091 else
4092 seq_printf(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004093 return 0;
4094 }
4095
Steven Rostedtb375a112009-09-17 00:05:58 -04004096 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004097
4098 return 0;
4099}
4100
James Morris88e9d342009-09-22 16:43:43 -07004101static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004102 .start = g_start,
4103 .next = g_next,
4104 .stop = g_stop,
4105 .show = g_show,
4106};
4107
4108static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004109__ftrace_graph_open(struct inode *inode, struct file *file,
4110 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004111{
4112 int ret = 0;
4113
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004114 mutex_lock(&graph_lock);
4115 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004116 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004117 *fgd->count = 0;
4118 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004119 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004120 mutex_unlock(&graph_lock);
4121
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004122 if (file->f_mode & FMODE_READ) {
4123 ret = seq_open(file, fgd->seq_ops);
4124 if (!ret) {
4125 struct seq_file *m = file->private_data;
4126 m->private = fgd;
4127 }
4128 } else
4129 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004130
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004131 return ret;
4132}
4133
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004134static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004135ftrace_graph_open(struct inode *inode, struct file *file)
4136{
4137 struct ftrace_graph_data *fgd;
4138
4139 if (unlikely(ftrace_disabled))
4140 return -ENODEV;
4141
4142 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4143 if (fgd == NULL)
4144 return -ENOMEM;
4145
4146 fgd->table = ftrace_graph_funcs;
4147 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4148 fgd->count = &ftrace_graph_count;
4149 fgd->seq_ops = &ftrace_graph_seq_ops;
4150
4151 return __ftrace_graph_open(inode, file, fgd);
4152}
4153
4154static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004155ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4156{
4157 struct ftrace_graph_data *fgd;
4158
4159 if (unlikely(ftrace_disabled))
4160 return -ENODEV;
4161
4162 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4163 if (fgd == NULL)
4164 return -ENOMEM;
4165
4166 fgd->table = ftrace_graph_notrace_funcs;
4167 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4168 fgd->count = &ftrace_graph_notrace_count;
4169 fgd->seq_ops = &ftrace_graph_seq_ops;
4170
4171 return __ftrace_graph_open(inode, file, fgd);
4172}
4173
4174static int
Li Zefan87827112009-07-23 11:29:11 +08004175ftrace_graph_release(struct inode *inode, struct file *file)
4176{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004177 if (file->f_mode & FMODE_READ) {
4178 struct seq_file *m = file->private_data;
4179
4180 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004181 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004182 } else {
4183 kfree(file->private_data);
4184 }
4185
Li Zefan87827112009-07-23 11:29:11 +08004186 return 0;
4187}
4188
4189static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004190ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004191{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004192 struct dyn_ftrace *rec;
4193 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004194 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004195 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004196 int type, not;
4197 char *search;
4198 bool exists;
4199 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004200
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004201 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004202 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004203 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004204 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004205
4206 search_len = strlen(search);
4207
Steven Rostedt52baf112009-02-14 01:15:39 -05004208 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004209
4210 if (unlikely(ftrace_disabled)) {
4211 mutex_unlock(&ftrace_lock);
4212 return -ENODEV;
4213 }
4214
Steven Rostedt265c8312009-02-13 12:43:56 -05004215 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004216
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004217 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004218 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004219 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004220 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004221 if (array[i] == rec->ip) {
4222 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004223 break;
4224 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004225 }
4226
4227 if (!not) {
4228 fail = 0;
4229 if (!exists) {
4230 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004231 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004232 goto out;
4233 }
4234 } else {
4235 if (exists) {
4236 array[i] = array[--(*idx)];
4237 array[*idx] = 0;
4238 fail = 0;
4239 }
4240 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004241 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004242 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004243out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004244 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004245
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004246 if (fail)
4247 return -EINVAL;
4248
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004249 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004250}
4251
4252static ssize_t
4253ftrace_graph_write(struct file *file, const char __user *ubuf,
4254 size_t cnt, loff_t *ppos)
4255{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004256 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004257 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004258 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004259
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004260 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004261 return 0;
4262
Namhyung Kim6a101082013-10-14 17:24:25 +09004263 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4264 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004265
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004266 read = trace_get_user(&parser, ubuf, cnt, ppos);
4267
Li Zefan4ba79782009-09-22 13:52:20 +08004268 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004269 parser.buffer[parser.idx] = 0;
4270
Namhyung Kim6a101082013-10-14 17:24:25 +09004271 mutex_lock(&graph_lock);
4272
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004273 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004274 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4275 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004276
4277 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004278 }
4279
Namhyung Kim6a101082013-10-14 17:24:25 +09004280 if (!ret)
4281 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004282
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004283 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004284
4285 return ret;
4286}
4287
4288static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004289 .open = ftrace_graph_open,
4290 .read = seq_read,
4291 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004292 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004293 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004294};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004295
4296static const struct file_operations ftrace_graph_notrace_fops = {
4297 .open = ftrace_graph_notrace_open,
4298 .read = seq_read,
4299 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004300 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004301 .release = ftrace_graph_release,
4302};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004303#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4304
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004305void ftrace_create_filter_files(struct ftrace_ops *ops,
4306 struct dentry *parent)
4307{
4308
4309 trace_create_file("set_ftrace_filter", 0644, parent,
4310 ops, &ftrace_filter_fops);
4311
4312 trace_create_file("set_ftrace_notrace", 0644, parent,
4313 ops, &ftrace_notrace_fops);
4314}
4315
4316/*
4317 * The name "destroy_filter_files" is really a misnomer. Although
4318 * in the future, it may actualy delete the files, but this is
4319 * really intended to make sure the ops passed in are disabled
4320 * and that when this function returns, the caller is free to
4321 * free the ops.
4322 *
4323 * The "destroy" name is only to match the "create" name that this
4324 * should be paired with.
4325 */
4326void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4327{
4328 mutex_lock(&ftrace_lock);
4329 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4330 ftrace_shutdown(ops, 0);
4331 ops->flags |= FTRACE_OPS_FL_DELETED;
4332 mutex_unlock(&ftrace_lock);
4333}
4334
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004335static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004336{
Steven Rostedt5072c592008-05-12 21:20:43 +02004337
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004338 trace_create_file("available_filter_functions", 0444,
4339 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004340
Steven Rostedt647bcd02011-05-03 14:39:21 -04004341 trace_create_file("enabled_functions", 0444,
4342 d_tracer, NULL, &ftrace_enabled_fops);
4343
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004344 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004345
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004346#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004347 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004348 NULL,
4349 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004350 trace_create_file("set_graph_notrace", 0444, d_tracer,
4351 NULL,
4352 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004353#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4354
Steven Rostedt5072c592008-05-12 21:20:43 +02004355 return 0;
4356}
4357
Steven Rostedt9fd49322012-04-24 22:32:06 -04004358static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004359{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004360 const unsigned long *ipa = a;
4361 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004362
Steven Rostedt9fd49322012-04-24 22:32:06 -04004363 if (*ipa > *ipb)
4364 return 1;
4365 if (*ipa < *ipb)
4366 return -1;
4367 return 0;
4368}
4369
4370static void ftrace_swap_ips(void *a, void *b, int size)
4371{
4372 unsigned long *ipa = a;
4373 unsigned long *ipb = b;
4374 unsigned long t;
4375
4376 t = *ipa;
4377 *ipa = *ipb;
4378 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004379}
4380
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004381static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004382 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004383 unsigned long *end)
4384{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004385 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004386 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004387 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004388 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004389 unsigned long *p;
4390 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004391 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004392 int ret = -ENOMEM;
4393
4394 count = end - start;
4395
4396 if (!count)
4397 return 0;
4398
Steven Rostedt9fd49322012-04-24 22:32:06 -04004399 sort(start, count, sizeof(*start),
4400 ftrace_cmp_ips, ftrace_swap_ips);
4401
Steven Rostedt706c81f2012-04-24 23:45:26 -04004402 start_pg = ftrace_allocate_pages(count);
4403 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004404 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004405
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004406 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004407
Steven Rostedt32082302011-12-16 14:42:37 -05004408 /*
4409 * Core and each module needs their own pages, as
4410 * modules will free them when they are removed.
4411 * Force a new page to be allocated for modules.
4412 */
Steven Rostedta7900872011-12-16 16:23:44 -05004413 if (!mod) {
4414 WARN_ON(ftrace_pages || ftrace_pages_start);
4415 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004416 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004417 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004418 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004419 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004420
Steven Rostedta7900872011-12-16 16:23:44 -05004421 if (WARN_ON(ftrace_pages->next)) {
4422 /* Hmm, we have free pages? */
4423 while (ftrace_pages->next)
4424 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004425 }
Steven Rostedta7900872011-12-16 16:23:44 -05004426
Steven Rostedt706c81f2012-04-24 23:45:26 -04004427 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004428 }
4429
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004430 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004431 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004432 while (p < end) {
4433 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004434 /*
4435 * Some architecture linkers will pad between
4436 * the different mcount_loc sections of different
4437 * object files to satisfy alignments.
4438 * Skip any NULL pointers.
4439 */
4440 if (!addr)
4441 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004442
4443 if (pg->index == pg->size) {
4444 /* We should have allocated enough */
4445 if (WARN_ON(!pg->next))
4446 break;
4447 pg = pg->next;
4448 }
4449
4450 rec = &pg->records[pg->index++];
4451 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004452 }
4453
Steven Rostedt706c81f2012-04-24 23:45:26 -04004454 /* We should have used all pages */
4455 WARN_ON(pg->next);
4456
4457 /* Assign the last page to ftrace_pages */
4458 ftrace_pages = pg;
4459
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004460 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004461 * We only need to disable interrupts on start up
4462 * because we are modifying code that an interrupt
4463 * may execute, and the modification is not atomic.
4464 * But for modules, nothing runs the code we modify
4465 * until we are finished with it, and there's no
4466 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004467 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004468 if (!mod)
4469 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004470 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004471 if (!mod)
4472 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004473 ret = 0;
4474 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004475 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004476
Steven Rostedta7900872011-12-16 16:23:44 -05004477 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004478}
4479
Steven Rostedt93eb6772009-04-15 13:24:06 -04004480#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004481
4482#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4483
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004484void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004485{
4486 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004487 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004488 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004489 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004490
Steven Rostedt93eb6772009-04-15 13:24:06 -04004491 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004492
4493 if (ftrace_disabled)
4494 goto out_unlock;
4495
Steven Rostedt32082302011-12-16 14:42:37 -05004496 /*
4497 * Each module has its own ftrace_pages, remove
4498 * them from the list.
4499 */
4500 last_pg = &ftrace_pages_start;
4501 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4502 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004503 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004504 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004505 * As core pages are first, the first
4506 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004507 */
Steven Rostedt32082302011-12-16 14:42:37 -05004508 if (WARN_ON(pg == ftrace_pages_start))
4509 goto out_unlock;
4510
4511 /* Check if we are deleting the last page */
4512 if (pg == ftrace_pages)
4513 ftrace_pages = next_to_ftrace_page(last_pg);
4514
4515 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004516 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4517 free_pages((unsigned long)pg->records, order);
4518 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004519 } else
4520 last_pg = &pg->next;
4521 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004522 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004523 mutex_unlock(&ftrace_lock);
4524}
4525
4526static void ftrace_init_module(struct module *mod,
4527 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004528{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004529 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004530 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004531 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004532}
4533
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004534void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004535{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004536 ftrace_init_module(mod, mod->ftrace_callsites,
4537 mod->ftrace_callsites +
4538 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004539}
4540
4541static int ftrace_module_notify_exit(struct notifier_block *self,
4542 unsigned long val, void *data)
4543{
4544 struct module *mod = data;
4545
4546 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004547 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004548
4549 return 0;
4550}
4551#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004552static int ftrace_module_notify_exit(struct notifier_block *self,
4553 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004554{
4555 return 0;
4556}
4557#endif /* CONFIG_MODULES */
4558
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004559struct notifier_block ftrace_module_exit_nb = {
4560 .notifier_call = ftrace_module_notify_exit,
4561 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4562};
4563
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004564void __init ftrace_init(void)
4565{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004566 extern unsigned long __start_mcount_loc[];
4567 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004568 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004569 int ret;
4570
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004571 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004572 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004573 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004574 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004575 goto failed;
4576
4577 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004578 if (!count) {
4579 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004580 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004581 }
4582
4583 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4584 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004585
4586 last_ftrace_enabled = ftrace_enabled = 1;
4587
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004588 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004589 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004590 __stop_mcount_loc);
4591
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004592 ret = register_module_notifier(&ftrace_module_exit_nb);
4593 if (ret)
4594 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004595
Steven Rostedt2af15d62009-05-28 13:37:24 -04004596 set_ftrace_early_filters();
4597
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004598 return;
4599 failed:
4600 ftrace_disabled = 1;
4601}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004602
Steven Rostedt3d083392008-05-12 21:20:42 +02004603#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004604
Steven Rostedt2b499382011-05-03 22:49:52 -04004605static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004606 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004607 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4608 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004609};
4610
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004611static int __init ftrace_nodyn_init(void)
4612{
4613 ftrace_enabled = 1;
4614 return 0;
4615}
Steven Rostedt6f415672012-10-05 12:13:07 -04004616core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004617
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004618static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4619static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004620/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004621# define ftrace_startup(ops, command) \
4622 ({ \
4623 int ___ret = __register_ftrace_function(ops); \
4624 if (!___ret) \
4625 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4626 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004627 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004628# define ftrace_shutdown(ops, command) \
4629 ({ \
4630 int ___ret = __unregister_ftrace_function(ops); \
4631 if (!___ret) \
4632 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4633 ___ret; \
4634 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004635
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004636# define ftrace_startup_sysctl() do { } while (0)
4637# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004638
4639static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004640ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004641{
4642 return 1;
4643}
4644
Steven Rostedt3d083392008-05-12 21:20:42 +02004645#endif /* CONFIG_DYNAMIC_FTRACE */
4646
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004647__init void ftrace_init_global_array_ops(struct trace_array *tr)
4648{
4649 tr->ops = &global_ops;
4650 tr->ops->private = tr;
4651}
4652
4653void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4654{
4655 /* If we filter on pids, update to use the pid function */
4656 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4657 if (WARN_ON(tr->ops->func != ftrace_stub))
4658 printk("ftrace ops had %pS for function\n",
4659 tr->ops->func);
4660 /* Only the top level instance does pid tracing */
4661 if (!list_empty(&ftrace_pids)) {
4662 set_ftrace_pid_function(func);
4663 func = ftrace_pid_func;
4664 }
4665 }
4666 tr->ops->func = func;
4667 tr->ops->private = tr;
4668}
4669
4670void ftrace_reset_array_ops(struct trace_array *tr)
4671{
4672 tr->ops->func = ftrace_stub;
4673}
4674
Steven Rostedtb8489142011-05-04 09:27:52 -04004675static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004676ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004677 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004678{
Jiri Olsae2484912012-02-15 15:51:48 +01004679 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4680 return;
4681
4682 /*
4683 * Some of the ops may be dynamically allocated,
4684 * they must be freed after a synchronize_sched().
4685 */
4686 preempt_disable_notrace();
4687 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004688
4689 /*
4690 * Control funcs (perf) uses RCU. Only trace if
4691 * RCU is currently active.
4692 */
4693 if (!rcu_is_watching())
4694 goto out;
4695
Steven Rostedt0a016402012-11-02 17:03:03 -04004696 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004697 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4698 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004699 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004700 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004701 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004702 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004703 trace_recursion_clear(TRACE_CONTROL_BIT);
4704 preempt_enable_notrace();
4705}
4706
4707static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004708 .func = ftrace_ops_control_func,
4709 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4710 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004711};
4712
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004713static inline void
4714__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004715 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004716{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004717 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004718 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004719
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004720 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4721 if (bit < 0)
4722 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004723
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004724 /*
4725 * Some of the ops may be dynamically allocated,
4726 * they must be freed after a synchronize_sched().
4727 */
4728 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004729 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004730 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004731 if (FTRACE_WARN_ON(!op->func)) {
4732 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004733 goto out;
4734 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004735 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004736 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004737 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004738out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004739 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004740 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004741}
4742
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004743/*
4744 * Some archs only support passing ip and parent_ip. Even though
4745 * the list function ignores the op parameter, we do not want any
4746 * C side effects, where a function is called without the caller
4747 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004748 * Archs are to support both the regs and ftrace_ops at the same time.
4749 * If they support ftrace_ops, it is assumed they support regs.
4750 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004751 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4752 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004753 * An architecture can pass partial regs with ftrace_ops and still
4754 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004755 */
4756#if ARCH_SUPPORTS_FTRACE_OPS
4757static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004758 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004759{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004760 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004761}
4762#else
4763static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4764{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004765 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004766}
4767#endif
4768
Steven Rostedte32d8952008-12-04 00:26:41 -05004769static void clear_ftrace_swapper(void)
4770{
4771 struct task_struct *p;
4772 int cpu;
4773
4774 get_online_cpus();
4775 for_each_online_cpu(cpu) {
4776 p = idle_task(cpu);
4777 clear_tsk_trace_trace(p);
4778 }
4779 put_online_cpus();
4780}
4781
4782static void set_ftrace_swapper(void)
4783{
4784 struct task_struct *p;
4785 int cpu;
4786
4787 get_online_cpus();
4788 for_each_online_cpu(cpu) {
4789 p = idle_task(cpu);
4790 set_tsk_trace_trace(p);
4791 }
4792 put_online_cpus();
4793}
4794
4795static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004796{
4797 struct task_struct *p;
4798
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004799 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004800 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004801 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004802 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004803 rcu_read_unlock();
4804
Steven Rostedte32d8952008-12-04 00:26:41 -05004805 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004806}
4807
Steven Rostedte32d8952008-12-04 00:26:41 -05004808static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004809{
4810 struct task_struct *p;
4811
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004812 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004813 do_each_pid_task(pid, PIDTYPE_PID, p) {
4814 set_tsk_trace_trace(p);
4815 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004816 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004817}
4818
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004819static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004820{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004821 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004822 clear_ftrace_swapper();
4823 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004824 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004825}
4826
4827static void set_ftrace_pid_task(struct pid *pid)
4828{
4829 if (pid == ftrace_swapper_pid)
4830 set_ftrace_swapper();
4831 else
4832 set_ftrace_pid(pid);
4833}
4834
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004835static int ftrace_pid_add(int p)
4836{
4837 struct pid *pid;
4838 struct ftrace_pid *fpid;
4839 int ret = -EINVAL;
4840
4841 mutex_lock(&ftrace_lock);
4842
4843 if (!p)
4844 pid = ftrace_swapper_pid;
4845 else
4846 pid = find_get_pid(p);
4847
4848 if (!pid)
4849 goto out;
4850
4851 ret = 0;
4852
4853 list_for_each_entry(fpid, &ftrace_pids, list)
4854 if (fpid->pid == pid)
4855 goto out_put;
4856
4857 ret = -ENOMEM;
4858
4859 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4860 if (!fpid)
4861 goto out_put;
4862
4863 list_add(&fpid->list, &ftrace_pids);
4864 fpid->pid = pid;
4865
4866 set_ftrace_pid_task(pid);
4867
4868 ftrace_update_pid_func();
4869 ftrace_startup_enable(0);
4870
4871 mutex_unlock(&ftrace_lock);
4872 return 0;
4873
4874out_put:
4875 if (pid != ftrace_swapper_pid)
4876 put_pid(pid);
4877
4878out:
4879 mutex_unlock(&ftrace_lock);
4880 return ret;
4881}
4882
4883static void ftrace_pid_reset(void)
4884{
4885 struct ftrace_pid *fpid, *safe;
4886
4887 mutex_lock(&ftrace_lock);
4888 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4889 struct pid *pid = fpid->pid;
4890
4891 clear_ftrace_pid_task(pid);
4892
4893 list_del(&fpid->list);
4894 kfree(fpid);
4895 }
4896
4897 ftrace_update_pid_func();
4898 ftrace_startup_enable(0);
4899
4900 mutex_unlock(&ftrace_lock);
4901}
4902
4903static void *fpid_start(struct seq_file *m, loff_t *pos)
4904{
4905 mutex_lock(&ftrace_lock);
4906
4907 if (list_empty(&ftrace_pids) && (!*pos))
4908 return (void *) 1;
4909
4910 return seq_list_start(&ftrace_pids, *pos);
4911}
4912
4913static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4914{
4915 if (v == (void *)1)
4916 return NULL;
4917
4918 return seq_list_next(v, &ftrace_pids, pos);
4919}
4920
4921static void fpid_stop(struct seq_file *m, void *p)
4922{
4923 mutex_unlock(&ftrace_lock);
4924}
4925
4926static int fpid_show(struct seq_file *m, void *v)
4927{
4928 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4929
4930 if (v == (void *)1) {
4931 seq_printf(m, "no pid\n");
4932 return 0;
4933 }
4934
4935 if (fpid->pid == ftrace_swapper_pid)
4936 seq_printf(m, "swapper tasks\n");
4937 else
4938 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4939
4940 return 0;
4941}
4942
4943static const struct seq_operations ftrace_pid_sops = {
4944 .start = fpid_start,
4945 .next = fpid_next,
4946 .stop = fpid_stop,
4947 .show = fpid_show,
4948};
4949
4950static int
4951ftrace_pid_open(struct inode *inode, struct file *file)
4952{
4953 int ret = 0;
4954
4955 if ((file->f_mode & FMODE_WRITE) &&
4956 (file->f_flags & O_TRUNC))
4957 ftrace_pid_reset();
4958
4959 if (file->f_mode & FMODE_READ)
4960 ret = seq_open(file, &ftrace_pid_sops);
4961
4962 return ret;
4963}
4964
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004965static ssize_t
4966ftrace_pid_write(struct file *filp, const char __user *ubuf,
4967 size_t cnt, loff_t *ppos)
4968{
Ingo Molnar457dc922009-11-23 11:03:28 +01004969 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004970 long val;
4971 int ret;
4972
4973 if (cnt >= sizeof(buf))
4974 return -EINVAL;
4975
4976 if (copy_from_user(&buf, ubuf, cnt))
4977 return -EFAULT;
4978
4979 buf[cnt] = 0;
4980
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004981 /*
4982 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4983 * to clean the filter quietly.
4984 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004985 tmp = strstrip(buf);
4986 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004987 return 1;
4988
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004989 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004990 if (ret < 0)
4991 return ret;
4992
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004993 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004994
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004995 return ret ? ret : cnt;
4996}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004997
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004998static int
4999ftrace_pid_release(struct inode *inode, struct file *file)
5000{
5001 if (file->f_mode & FMODE_READ)
5002 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005003
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005004 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005005}
5006
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005007static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005008 .open = ftrace_pid_open,
5009 .write = ftrace_pid_write,
5010 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005011 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005012 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005013};
5014
5015static __init int ftrace_init_debugfs(void)
5016{
5017 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005018
5019 d_tracer = tracing_init_dentry();
5020 if (!d_tracer)
5021 return 0;
5022
5023 ftrace_init_dyn_debugfs(d_tracer);
5024
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005025 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5026 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005027
5028 ftrace_profile_debugfs(d_tracer);
5029
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005030 return 0;
5031}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005032fs_initcall(ftrace_init_debugfs);
5033
Steven Rostedt3d083392008-05-12 21:20:42 +02005034/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005035 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005036 *
5037 * This function should be used by panic code. It stops ftrace
5038 * but in a not so nice way. If you need to simply kill ftrace
5039 * from a non-atomic section, use ftrace_kill.
5040 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005041void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005042{
5043 ftrace_disabled = 1;
5044 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005045 clear_ftrace_function();
5046}
5047
5048/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005049 * Test if ftrace is dead or not.
5050 */
5051int ftrace_is_dead(void)
5052{
5053 return ftrace_disabled;
5054}
5055
5056/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005057 * register_ftrace_function - register a function for profiling
5058 * @ops - ops structure that holds the function for profiling.
5059 *
5060 * Register a function to be called by all functions in the
5061 * kernel.
5062 *
5063 * Note: @ops->func and all the functions it calls must be labeled
5064 * with "notrace", otherwise it will go into a
5065 * recursive loop.
5066 */
5067int register_ftrace_function(struct ftrace_ops *ops)
5068{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005069 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005070
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005071 ftrace_ops_init(ops);
5072
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005073 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005074
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005075 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005076
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005077 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005078
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005079 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005080}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005081EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005082
5083/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005084 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005085 * @ops - ops structure that holds the function to unregister
5086 *
5087 * Unregister a function that was added to be called by ftrace profiling.
5088 */
5089int unregister_ftrace_function(struct ftrace_ops *ops)
5090{
5091 int ret;
5092
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005093 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005094 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005095 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005096
5097 return ret;
5098}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005099EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005100
Ingo Molnare309b412008-05-12 21:20:51 +02005101int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005102ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005103 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005104 loff_t *ppos)
5105{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005106 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005107
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005108 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005109
Steven Rostedt45a4a232011-04-21 23:16:46 -04005110 if (unlikely(ftrace_disabled))
5111 goto out;
5112
5113 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005114
Li Zefana32c7762009-06-26 16:55:51 +08005115 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005116 goto out;
5117
Li Zefana32c7762009-06-26 16:55:51 +08005118 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005119
5120 if (ftrace_enabled) {
5121
5122 ftrace_startup_sysctl();
5123
5124 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005125 if (ftrace_ops_list != &ftrace_list_end)
5126 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005127
5128 } else {
5129 /* stopping ftrace calls (just send to ftrace_stub) */
5130 ftrace_trace_function = ftrace_stub;
5131
5132 ftrace_shutdown_sysctl();
5133 }
5134
5135 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005136 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005137 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005138}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005139
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005140#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005141
Steven Rostedt597af812009-04-03 15:24:12 -04005142static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005143
Steven Rostedte49dc192008-12-02 23:50:05 -05005144int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5145{
5146 return 0;
5147}
5148
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005149/* The callbacks that hook a function */
5150trace_func_graph_ret_t ftrace_graph_return =
5151 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005152trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005153static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005154
5155/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5156static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5157{
5158 int i;
5159 int ret = 0;
5160 unsigned long flags;
5161 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5162 struct task_struct *g, *t;
5163
5164 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5165 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5166 * sizeof(struct ftrace_ret_stack),
5167 GFP_KERNEL);
5168 if (!ret_stack_list[i]) {
5169 start = 0;
5170 end = i;
5171 ret = -ENOMEM;
5172 goto free;
5173 }
5174 }
5175
5176 read_lock_irqsave(&tasklist_lock, flags);
5177 do_each_thread(g, t) {
5178 if (start == end) {
5179 ret = -EAGAIN;
5180 goto unlock;
5181 }
5182
5183 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005184 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005185 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005186 t->curr_ret_stack = -1;
5187 /* Make sure the tasks see the -1 first: */
5188 smp_wmb();
5189 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005190 }
5191 } while_each_thread(g, t);
5192
5193unlock:
5194 read_unlock_irqrestore(&tasklist_lock, flags);
5195free:
5196 for (i = start; i < end; i++)
5197 kfree(ret_stack_list[i]);
5198 return ret;
5199}
5200
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005201static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005202ftrace_graph_probe_sched_switch(void *ignore,
5203 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005204{
5205 unsigned long long timestamp;
5206 int index;
5207
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005208 /*
5209 * Does the user want to count the time a function was asleep.
5210 * If so, do not update the time stamps.
5211 */
5212 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5213 return;
5214
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005215 timestamp = trace_clock_local();
5216
5217 prev->ftrace_timestamp = timestamp;
5218
5219 /* only process tasks that we timestamped */
5220 if (!next->ftrace_timestamp)
5221 return;
5222
5223 /*
5224 * Update all the counters in next to make up for the
5225 * time next was sleeping.
5226 */
5227 timestamp -= next->ftrace_timestamp;
5228
5229 for (index = next->curr_ret_stack; index >= 0; index--)
5230 next->ret_stack[index].calltime += timestamp;
5231}
5232
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005233/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005234static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005235{
5236 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005237 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005238
5239 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5240 sizeof(struct ftrace_ret_stack *),
5241 GFP_KERNEL);
5242
5243 if (!ret_stack_list)
5244 return -ENOMEM;
5245
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005246 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005247 for_each_online_cpu(cpu) {
5248 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005249 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005250 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005251
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005252 do {
5253 ret = alloc_retstack_tasklist(ret_stack_list);
5254 } while (ret == -EAGAIN);
5255
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005256 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005257 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005258 if (ret)
5259 pr_info("ftrace_graph: Couldn't activate tracepoint"
5260 " probe to kernel_sched_switch\n");
5261 }
5262
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005263 kfree(ret_stack_list);
5264 return ret;
5265}
5266
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005267/*
5268 * Hibernation protection.
5269 * The state of the current task is too much unstable during
5270 * suspend/restore to disk. We want to protect against that.
5271 */
5272static int
5273ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5274 void *unused)
5275{
5276 switch (state) {
5277 case PM_HIBERNATION_PREPARE:
5278 pause_graph_tracing();
5279 break;
5280
5281 case PM_POST_HIBERNATION:
5282 unpause_graph_tracing();
5283 break;
5284 }
5285 return NOTIFY_DONE;
5286}
5287
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005288static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5289{
5290 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5291 return 0;
5292 return __ftrace_graph_entry(trace);
5293}
5294
5295/*
5296 * The function graph tracer should only trace the functions defined
5297 * by set_ftrace_filter and set_ftrace_notrace. If another function
5298 * tracer ops is registered, the graph tracer requires testing the
5299 * function against the global ops, and not just trace any function
5300 * that any ftrace_ops registered.
5301 */
5302static void update_function_graph_func(void)
5303{
5304 if (ftrace_ops_list == &ftrace_list_end ||
5305 (ftrace_ops_list == &global_ops &&
5306 global_ops.next == &ftrace_list_end))
5307 ftrace_graph_entry = __ftrace_graph_entry;
5308 else
5309 ftrace_graph_entry = ftrace_graph_entry_test;
5310}
5311
Mathias Krause8275f692014-03-30 15:31:50 +02005312static struct notifier_block ftrace_suspend_notifier = {
5313 .notifier_call = ftrace_suspend_notifier_call,
5314};
5315
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005316int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5317 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005318{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005319 int ret = 0;
5320
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005321 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005322
Steven Rostedt05ce5812009-03-24 00:18:31 -04005323 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005324 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005325 ret = -EBUSY;
5326 goto out;
5327 }
5328
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005329 register_pm_notifier(&ftrace_suspend_notifier);
5330
Steven Rostedt597af812009-04-03 15:24:12 -04005331 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005332 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005333 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005334 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005335 goto out;
5336 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005337
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005338 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005339
5340 /*
5341 * Update the indirect function to the entryfunc, and the
5342 * function that gets called to the entry_test first. Then
5343 * call the update fgraph entry function to determine if
5344 * the entryfunc should be called directly or not.
5345 */
5346 __ftrace_graph_entry = entryfunc;
5347 ftrace_graph_entry = ftrace_graph_entry_test;
5348 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005349
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005350 /* Function graph doesn't use the .func field of global_ops */
5351 global_ops.flags |= FTRACE_OPS_FL_STUB;
5352
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005353#ifdef CONFIG_DYNAMIC_FTRACE
5354 /* Optimize function graph calling (if implemented by arch) */
Steven Rostedt (Red Hat)646d7042014-07-11 14:39:10 -04005355 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5356 global_ops.trampoline = FTRACE_GRAPH_TRAMP_ADDR;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005357#endif
5358
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005359 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005360
5361out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005362 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005363 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005364}
5365
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005366void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005367{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005368 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005369
Steven Rostedt597af812009-04-03 15:24:12 -04005370 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005371 goto out;
5372
Steven Rostedt597af812009-04-03 15:24:12 -04005373 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005374 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005375 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005376 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005377 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5378 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005379#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)646d7042014-07-11 14:39:10 -04005380 if (FTRACE_GRAPH_TRAMP_ADDR != 0)
5381 global_ops.trampoline = 0;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005382#endif
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005383 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005384 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005385
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005386 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005387 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005388}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005389
Steven Rostedt868baf02011-02-10 21:26:13 -05005390static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5391
5392static void
5393graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5394{
5395 atomic_set(&t->tracing_graph_pause, 0);
5396 atomic_set(&t->trace_overrun, 0);
5397 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005398 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005399 smp_wmb();
5400 t->ret_stack = ret_stack;
5401}
5402
5403/*
5404 * Allocate a return stack for the idle task. May be the first
5405 * time through, or it may be done by CPU hotplug online.
5406 */
5407void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5408{
5409 t->curr_ret_stack = -1;
5410 /*
5411 * The idle task has no parent, it either has its own
5412 * stack or no stack at all.
5413 */
5414 if (t->ret_stack)
5415 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5416
5417 if (ftrace_graph_active) {
5418 struct ftrace_ret_stack *ret_stack;
5419
5420 ret_stack = per_cpu(idle_ret_stack, cpu);
5421 if (!ret_stack) {
5422 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5423 * sizeof(struct ftrace_ret_stack),
5424 GFP_KERNEL);
5425 if (!ret_stack)
5426 return;
5427 per_cpu(idle_ret_stack, cpu) = ret_stack;
5428 }
5429 graph_init_task(t, ret_stack);
5430 }
5431}
5432
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005433/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005434void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005435{
Steven Rostedt84047e32009-06-02 16:51:55 -04005436 /* Make sure we do not use the parent ret_stack */
5437 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005438 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005439
Steven Rostedt597af812009-04-03 15:24:12 -04005440 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005441 struct ftrace_ret_stack *ret_stack;
5442
5443 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005444 * sizeof(struct ftrace_ret_stack),
5445 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005446 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005447 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005448 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005449 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005450}
5451
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005452void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005453{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005454 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5455
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005456 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005457 /* NULL must become visible to IRQs before we free it: */
5458 barrier();
5459
5460 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005461}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005462#endif