blob: 02bece4a99ea36bb835fc45a9aa55c1aedd69f9f [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050021#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -050065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
Jiri Olsae2484912012-02-15 15:51:48 +010066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040068#define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040071#define ASSIGN_OPS_HASH(opsname, val) \
72 .func_hash = val, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090074#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040075#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040076#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090077#endif
78
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040079static struct ftrace_ops ftrace_list_end __read_mostly = {
80 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040081 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040082 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040083};
84
Steven Rostedt4eebcc82008-05-12 21:20:48 +020085/* ftrace_enabled is a method to turn ftrace on or off */
86int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020087static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020088
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040089/* Current function tracing op */
90struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050091/* What to set function_trace_op to */
92static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050093
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040094/* List for set_ftrace_pid's pids. */
95LIST_HEAD(ftrace_pids);
96struct ftrace_pid {
97 struct list_head list;
98 struct pid *pid;
99};
100
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200101/*
102 * ftrace_disabled is set when an anomaly is discovered.
103 * ftrace_disabled is much stronger than ftrace_enabled.
104 */
105static int ftrace_disabled __read_mostly;
106
Steven Rostedt52baf112009-02-14 01:15:39 -0500107static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200108
Jiri Olsae2484912012-02-15 15:51:48 +0100109static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400110static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500112ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400113static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100114static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -0400116static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
117 struct ftrace_ops *op, struct pt_regs *regs);
118
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400119#if ARCH_SUPPORTS_FTRACE_OPS
120static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400121 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122#else
123/* See comment below, where ftrace_ops_list_func is defined */
124static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
125#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
126#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400127
Steven Rostedt0a016402012-11-02 17:03:03 -0400128/*
129 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400130 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400131 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400132 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400133 * concurrent insertions into the ftrace_global_list.
134 *
135 * Silly Alpha and silly pointer-speculation compiler optimizations!
136 */
137#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 do
140
141/*
142 * Optimized for just a single item in the list (as that is the normal case).
143 */
144#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400145 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400146 unlikely((op) != &ftrace_list_end))
147
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900148static inline void ftrace_ops_init(struct ftrace_ops *ops)
149{
150#ifdef CONFIG_DYNAMIC_FTRACE
151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
155 }
156#endif
157}
158
Steven Rostedtea701f12012-07-20 13:08:05 -0400159/**
160 * ftrace_nr_registered_ops - return number of ops registered
161 *
162 * Returns the number of ftrace_ops registered and tracing functions
163 */
164int ftrace_nr_registered_ops(void)
165{
166 struct ftrace_ops *ops;
167 int cnt = 0;
168
169 mutex_lock(&ftrace_lock);
170
171 for (ops = ftrace_ops_list;
172 ops != &ftrace_list_end; ops = ops->next)
173 cnt++;
174
175 mutex_unlock(&ftrace_lock);
176
177 return cnt;
178}
179
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400180static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400181 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500182{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500183 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500184 return;
185
Steven Rostedta1e2e312011-08-09 12:50:46 -0400186 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500187}
188
189static void set_ftrace_pid_function(ftrace_func_t func)
190{
191 /* do not set ftrace_pid_function to itself! */
192 if (func != ftrace_pid_func)
193 ftrace_pid_function = func;
194}
195
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200197 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200202void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200203{
Steven Rostedt3d083392008-05-12 21:20:42 +0200204 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500205 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200206}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207
Jiri Olsae2484912012-02-15 15:51:48 +0100208static void control_ops_disable_all(struct ftrace_ops *ops)
209{
210 int cpu;
211
212 for_each_possible_cpu(cpu)
213 *per_cpu_ptr(ops->disabled, cpu) = 1;
214}
215
216static int control_ops_alloc(struct ftrace_ops *ops)
217{
218 int __percpu *disabled;
219
220 disabled = alloc_percpu(int);
221 if (!disabled)
222 return -ENOMEM;
223
224 ops->disabled = disabled;
225 control_ops_disable_all(ops);
226 return 0;
227}
228
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500229static void ftrace_sync(struct work_struct *work)
230{
231 /*
232 * This function is just a stub to implement a hard force
233 * of synchronize_sched(). This requires synchronizing
234 * tasks even in userspace and idle.
235 *
236 * Yes, function tracing is rude.
237 */
238}
239
240static void ftrace_sync_ipi(void *data)
241{
242 /* Probably not needed, but do it anyway */
243 smp_rmb();
244}
245
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500246#ifdef CONFIG_FUNCTION_GRAPH_TRACER
247static void update_function_graph_func(void);
248#else
249static inline void update_function_graph_func(void) { }
250#endif
251
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100252
253static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
254{
255 /*
256 * If this is a dynamic ops or we force list func,
257 * then it needs to call the list anyway.
258 */
259 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
260 return ftrace_ops_list_func;
261
262 return ftrace_ops_get_func(ops);
263}
264
Steven Rostedt2b499382011-05-03 22:49:52 -0400265static void update_ftrace_function(void)
266{
267 ftrace_func_t func;
268
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400269 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400270 * Prepare the ftrace_ops that the arch callback will use.
271 * If there's only one ftrace_ops registered, the ftrace_ops_list
272 * will point to the ops we want.
273 */
274 set_function_trace_op = ftrace_ops_list;
275
276 /* If there's no ftrace_ops registered, just call the stub function */
277 if (ftrace_ops_list == &ftrace_list_end) {
278 func = ftrace_stub;
279
280 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400281 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400282 * recursion safe and not dynamic and the arch supports passing ops,
283 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400284 */
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400285 } else if (ftrace_ops_list->next == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100286 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400287
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400288 } else {
289 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500290 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400291 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400292 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400293
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400294 update_function_graph_func();
295
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500296 /* If there's no change, then do nothing more here */
297 if (ftrace_trace_function == func)
298 return;
299
300 /*
301 * If we are using the list function, it doesn't care
302 * about the function_trace_ops.
303 */
304 if (func == ftrace_ops_list_func) {
305 ftrace_trace_function = func;
306 /*
307 * Don't even bother setting function_trace_ops,
308 * it would be racy to do so anyway.
309 */
310 return;
311 }
312
313#ifndef CONFIG_DYNAMIC_FTRACE
314 /*
315 * For static tracing, we need to be a bit more careful.
316 * The function change takes affect immediately. Thus,
317 * we need to coorditate the setting of the function_trace_ops
318 * with the setting of the ftrace_trace_function.
319 *
320 * Set the function to the list ops, which will call the
321 * function we want, albeit indirectly, but it handles the
322 * ftrace_ops and doesn't depend on function_trace_op.
323 */
324 ftrace_trace_function = ftrace_ops_list_func;
325 /*
326 * Make sure all CPUs see this. Yes this is slow, but static
327 * tracing is slow and nasty to have enabled.
328 */
329 schedule_on_each_cpu(ftrace_sync);
330 /* Now all cpus are using the list ops. */
331 function_trace_op = set_function_trace_op;
332 /* Make sure the function_trace_op is visible on all CPUs */
333 smp_wmb();
334 /* Nasty way to force a rmb on all cpus */
335 smp_call_function(ftrace_sync_ipi, NULL, 1);
336 /* OK, we are all set to update the ftrace_trace_function now! */
337#endif /* !CONFIG_DYNAMIC_FTRACE */
338
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400339 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400340}
341
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800342int using_ftrace_ops_list_func(void)
343{
344 return ftrace_trace_function == ftrace_ops_list_func;
345}
346
Steven Rostedt2b499382011-05-03 22:49:52 -0400347static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200348{
Steven Rostedt2b499382011-05-03 22:49:52 -0400349 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200350 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400351 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200352 * CPU might be walking that list. We need to make sure
353 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400354 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200355 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400356 rcu_assign_pointer(*list, ops);
357}
Steven Rostedt3d083392008-05-12 21:20:42 +0200358
Steven Rostedt2b499382011-05-03 22:49:52 -0400359static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
360{
361 struct ftrace_ops **p;
362
363 /*
364 * If we are removing the last function, then simply point
365 * to the ftrace_stub.
366 */
367 if (*list == ops && ops->next == &ftrace_list_end) {
368 *list = &ftrace_list_end;
369 return 0;
370 }
371
372 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
373 if (*p == ops)
374 break;
375
376 if (*p != ops)
377 return -1;
378
379 *p = (*p)->next;
380 return 0;
381}
382
Jiri Olsae2484912012-02-15 15:51:48 +0100383static void add_ftrace_list_ops(struct ftrace_ops **list,
384 struct ftrace_ops *main_ops,
385 struct ftrace_ops *ops)
386{
387 int first = *list == &ftrace_list_end;
388 add_ftrace_ops(list, ops);
389 if (first)
390 add_ftrace_ops(&ftrace_ops_list, main_ops);
391}
392
393static int remove_ftrace_list_ops(struct ftrace_ops **list,
394 struct ftrace_ops *main_ops,
395 struct ftrace_ops *ops)
396{
397 int ret = remove_ftrace_ops(list, ops);
398 if (!ret && *list == &ftrace_list_end)
399 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
400 return ret;
401}
402
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400403static void ftrace_update_trampoline(struct ftrace_ops *ops);
404
Steven Rostedt2b499382011-05-03 22:49:52 -0400405static int __register_ftrace_function(struct ftrace_ops *ops)
406{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500407 if (ops->flags & FTRACE_OPS_FL_DELETED)
408 return -EINVAL;
409
Steven Rostedtb8489142011-05-04 09:27:52 -0400410 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
411 return -EBUSY;
412
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900413#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400414 /*
415 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
416 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
417 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
418 */
419 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
420 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
421 return -EINVAL;
422
423 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
424 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
425#endif
426
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400427 if (!core_kernel_data((unsigned long)ops))
428 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
429
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500430 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100431 if (control_ops_alloc(ops))
432 return -ENOMEM;
433 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedt (Red Hat)fe578ba2014-11-13 19:40:56 -0500434 /* The control_ops needs the trampoline update */
435 ops = &control_ops;
Steven Rostedtb8489142011-05-04 09:27:52 -0400436 } else
437 add_ftrace_ops(&ftrace_ops_list, ops);
438
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400439 ftrace_update_trampoline(ops);
440
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400441 if (ftrace_enabled)
442 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200443
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200444 return 0;
445}
446
Ingo Molnare309b412008-05-12 21:20:51 +0200447static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200448{
Steven Rostedt2b499382011-05-03 22:49:52 -0400449 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200450
Steven Rostedtb8489142011-05-04 09:27:52 -0400451 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
452 return -EBUSY;
453
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500454 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100455 ret = remove_ftrace_list_ops(&ftrace_control_list,
456 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400457 } else
458 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
459
Steven Rostedt2b499382011-05-03 22:49:52 -0400460 if (ret < 0)
461 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400462
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400463 if (ftrace_enabled)
464 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200465
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500466 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200467}
468
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500469static void ftrace_update_pid_func(void)
470{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400471 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500472 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900473 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500474
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400475 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500476}
477
Steven Rostedt493762f2009-03-23 17:12:36 -0400478#ifdef CONFIG_FUNCTION_PROFILER
479struct ftrace_profile {
480 struct hlist_node node;
481 unsigned long ip;
482 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400483#ifdef CONFIG_FUNCTION_GRAPH_TRACER
484 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400485 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400486#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400487};
488
489struct ftrace_profile_page {
490 struct ftrace_profile_page *next;
491 unsigned long index;
492 struct ftrace_profile records[];
493};
494
Steven Rostedtcafb1682009-03-24 20:50:39 -0400495struct ftrace_profile_stat {
496 atomic_t disabled;
497 struct hlist_head *hash;
498 struct ftrace_profile_page *pages;
499 struct ftrace_profile_page *start;
500 struct tracer_stat stat;
501};
502
Steven Rostedt493762f2009-03-23 17:12:36 -0400503#define PROFILE_RECORDS_SIZE \
504 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
505
506#define PROFILES_PER_PAGE \
507 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
508
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400509static int ftrace_profile_enabled __read_mostly;
510
511/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400512static DEFINE_MUTEX(ftrace_profile_lock);
513
Steven Rostedtcafb1682009-03-24 20:50:39 -0400514static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400515
Namhyung Kim20079eb2013-04-10 08:55:50 +0900516#define FTRACE_PROFILE_HASH_BITS 10
517#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400518
Steven Rostedt493762f2009-03-23 17:12:36 -0400519static void *
520function_stat_next(void *v, int idx)
521{
522 struct ftrace_profile *rec = v;
523 struct ftrace_profile_page *pg;
524
525 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
526
527 again:
Li Zefan0296e422009-06-26 11:15:37 +0800528 if (idx != 0)
529 rec++;
530
Steven Rostedt493762f2009-03-23 17:12:36 -0400531 if ((void *)rec >= (void *)&pg->records[pg->index]) {
532 pg = pg->next;
533 if (!pg)
534 return NULL;
535 rec = &pg->records[0];
536 if (!rec->counter)
537 goto again;
538 }
539
540 return rec;
541}
542
543static void *function_stat_start(struct tracer_stat *trace)
544{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400545 struct ftrace_profile_stat *stat =
546 container_of(trace, struct ftrace_profile_stat, stat);
547
548 if (!stat || !stat->start)
549 return NULL;
550
551 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400552}
553
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400554#ifdef CONFIG_FUNCTION_GRAPH_TRACER
555/* function graph compares on total time */
556static int function_stat_cmp(void *p1, void *p2)
557{
558 struct ftrace_profile *a = p1;
559 struct ftrace_profile *b = p2;
560
561 if (a->time < b->time)
562 return -1;
563 if (a->time > b->time)
564 return 1;
565 else
566 return 0;
567}
568#else
569/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400570static int function_stat_cmp(void *p1, void *p2)
571{
572 struct ftrace_profile *a = p1;
573 struct ftrace_profile *b = p2;
574
575 if (a->counter < b->counter)
576 return -1;
577 if (a->counter > b->counter)
578 return 1;
579 else
580 return 0;
581}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400582#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400583
584static int function_stat_headers(struct seq_file *m)
585{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400586#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100587 seq_puts(m, " Function "
588 "Hit Time Avg s^2\n"
589 " -------- "
590 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400591#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100592 seq_puts(m, " Function Hit\n"
593 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400594#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400595 return 0;
596}
597
598static int function_stat_show(struct seq_file *m, void *v)
599{
600 struct ftrace_profile *rec = v;
601 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800602 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400603#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400604 static struct trace_seq s;
605 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400606 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400607#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800608 mutex_lock(&ftrace_profile_lock);
609
610 /* we raced with function_profile_reset() */
611 if (unlikely(rec->counter == 0)) {
612 ret = -EBUSY;
613 goto out;
614 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400615
616 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400617 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400618
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400619#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100620 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400621 avg = rec->time;
622 do_div(avg, rec->counter);
623
Chase Douglase330b3b2010-04-26 14:02:05 -0400624 /* Sample standard deviation (s^2) */
625 if (rec->counter <= 1)
626 stddev = 0;
627 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200628 /*
629 * Apply Welford's method:
630 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
631 */
632 stddev = rec->counter * rec->time_squared -
633 rec->time * rec->time;
634
Chase Douglase330b3b2010-04-26 14:02:05 -0400635 /*
636 * Divide only 1000 for ns^2 -> us^2 conversion.
637 * trace_print_graph_duration will divide 1000 again.
638 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200639 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400640 }
641
Steven Rostedt34886c82009-03-25 21:00:47 -0400642 trace_seq_init(&s);
643 trace_print_graph_duration(rec->time, &s);
644 trace_seq_puts(&s, " ");
645 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400646 trace_seq_puts(&s, " ");
647 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400648 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400649#endif
650 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800651out:
652 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400653
Li Zefan3aaba202010-08-23 16:50:12 +0800654 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400655}
656
Steven Rostedtcafb1682009-03-24 20:50:39 -0400657static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400658{
659 struct ftrace_profile_page *pg;
660
Steven Rostedtcafb1682009-03-24 20:50:39 -0400661 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400662
663 while (pg) {
664 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
665 pg->index = 0;
666 pg = pg->next;
667 }
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400670 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
671}
672
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400674{
675 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400676 int functions;
677 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400678 int i;
679
680 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400681 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400682 return 0;
683
Steven Rostedtcafb1682009-03-24 20:50:39 -0400684 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
685 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400686 return -ENOMEM;
687
Steven Rostedt318e0a72009-03-25 20:06:34 -0400688#ifdef CONFIG_DYNAMIC_FTRACE
689 functions = ftrace_update_tot_cnt;
690#else
691 /*
692 * We do not know the number of functions that exist because
693 * dynamic tracing is what counts them. With past experience
694 * we have around 20K functions. That should be more than enough.
695 * It is highly unlikely we will execute every function in
696 * the kernel.
697 */
698 functions = 20000;
699#endif
700
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400702
Steven Rostedt318e0a72009-03-25 20:06:34 -0400703 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
704
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900705 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400706 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400707 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400708 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400709 pg = pg->next;
710 }
711
712 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400713
714 out_free:
715 pg = stat->start;
716 while (pg) {
717 unsigned long tmp = (unsigned long)pg;
718
719 pg = pg->next;
720 free_page(tmp);
721 }
722
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723 stat->pages = NULL;
724 stat->start = NULL;
725
726 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400727}
728
Steven Rostedtcafb1682009-03-24 20:50:39 -0400729static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400730{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400731 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400732 int size;
733
Steven Rostedtcafb1682009-03-24 20:50:39 -0400734 stat = &per_cpu(ftrace_profile_stats, cpu);
735
736 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400737 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400738 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400739 return 0;
740 }
741
742 /*
743 * We are profiling all functions, but usually only a few thousand
744 * functions are hit. We'll make a hash of 1024 items.
745 */
746 size = FTRACE_PROFILE_HASH_SIZE;
747
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400749
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400751 return -ENOMEM;
752
Steven Rostedt318e0a72009-03-25 20:06:34 -0400753 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 if (ftrace_profile_pages_init(stat) < 0) {
755 kfree(stat->hash);
756 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400757 return -ENOMEM;
758 }
759
760 return 0;
761}
762
Steven Rostedtcafb1682009-03-24 20:50:39 -0400763static int ftrace_profile_init(void)
764{
765 int cpu;
766 int ret = 0;
767
Miao Xiec4602c12013-12-16 15:20:01 +0800768 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400769 ret = ftrace_profile_init_cpu(cpu);
770 if (ret)
771 break;
772 }
773
774 return ret;
775}
776
Steven Rostedt493762f2009-03-23 17:12:36 -0400777/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400778static struct ftrace_profile *
779ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400780{
781 struct ftrace_profile *rec;
782 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400783 unsigned long key;
784
Namhyung Kim20079eb2013-04-10 08:55:50 +0900785 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400786 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400787
788 if (hlist_empty(hhd))
789 return NULL;
790
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400791 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400792 if (rec->ip == ip)
793 return rec;
794 }
795
796 return NULL;
797}
798
Steven Rostedtcafb1682009-03-24 20:50:39 -0400799static void ftrace_add_profile(struct ftrace_profile_stat *stat,
800 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400801{
802 unsigned long key;
803
Namhyung Kim20079eb2013-04-10 08:55:50 +0900804 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400805 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400806}
807
Steven Rostedt318e0a72009-03-25 20:06:34 -0400808/*
809 * The memory is already allocated, this simply finds a new record to use.
810 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400811static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400812ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400813{
814 struct ftrace_profile *rec = NULL;
815
Steven Rostedt318e0a72009-03-25 20:06:34 -0400816 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400818 goto out;
819
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400821 * Try to find the function again since an NMI
822 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400825 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400826 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400827
Steven Rostedtcafb1682009-03-24 20:50:39 -0400828 if (stat->pages->index == PROFILES_PER_PAGE) {
829 if (!stat->pages->next)
830 goto out;
831 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400832 }
833
Steven Rostedtcafb1682009-03-24 20:50:39 -0400834 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400836 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400837
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400839 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400840
841 return rec;
842}
843
Steven Rostedt493762f2009-03-23 17:12:36 -0400844static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400845function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400846 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400847{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400849 struct ftrace_profile *rec;
850 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400851
852 if (!ftrace_profile_enabled)
853 return;
854
Steven Rostedt493762f2009-03-23 17:12:36 -0400855 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400856
Christoph Lameterbdffd892014-04-29 14:17:40 -0500857 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400858 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400859 goto out;
860
861 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400862 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400863 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400864 if (!rec)
865 goto out;
866 }
867
868 rec->counter++;
869 out:
870 local_irq_restore(flags);
871}
872
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400873#ifdef CONFIG_FUNCTION_GRAPH_TRACER
874static int profile_graph_entry(struct ftrace_graph_ent *trace)
875{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400876 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400877 return 1;
878}
879
880static void profile_graph_return(struct ftrace_graph_ret *trace)
881{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400882 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400883 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400884 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400885 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400886
887 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500888 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400889 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400890 goto out;
891
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400892 /* If the calltime was zero'd ignore it */
893 if (!trace->calltime)
894 goto out;
895
Steven Rostedta2a16d62009-03-24 23:17:58 -0400896 calltime = trace->rettime - trace->calltime;
897
898 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
899 int index;
900
901 index = trace->depth;
902
903 /* Append this call time to the parent time to subtract */
904 if (index)
905 current->ret_stack[index - 1].subtime += calltime;
906
907 if (current->ret_stack[index].subtime < calltime)
908 calltime -= current->ret_stack[index].subtime;
909 else
910 calltime = 0;
911 }
912
Steven Rostedtcafb1682009-03-24 20:50:39 -0400913 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400914 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400915 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400916 rec->time_squared += calltime * calltime;
917 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400918
Steven Rostedtcafb1682009-03-24 20:50:39 -0400919 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400920 local_irq_restore(flags);
921}
922
923static int register_ftrace_profiler(void)
924{
925 return register_ftrace_graph(&profile_graph_return,
926 &profile_graph_entry);
927}
928
929static void unregister_ftrace_profiler(void)
930{
931 unregister_ftrace_graph();
932}
933#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100934static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400935 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900936 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400937 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400938};
939
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400940static int register_ftrace_profiler(void)
941{
942 return register_ftrace_function(&ftrace_profile_ops);
943}
944
945static void unregister_ftrace_profiler(void)
946{
947 unregister_ftrace_function(&ftrace_profile_ops);
948}
949#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
950
Steven Rostedt493762f2009-03-23 17:12:36 -0400951static ssize_t
952ftrace_profile_write(struct file *filp, const char __user *ubuf,
953 size_t cnt, loff_t *ppos)
954{
955 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400956 int ret;
957
Peter Huewe22fe9b52011-06-07 21:58:27 +0200958 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
959 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400960 return ret;
961
962 val = !!val;
963
964 mutex_lock(&ftrace_profile_lock);
965 if (ftrace_profile_enabled ^ val) {
966 if (val) {
967 ret = ftrace_profile_init();
968 if (ret < 0) {
969 cnt = ret;
970 goto out;
971 }
972
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400973 ret = register_ftrace_profiler();
974 if (ret < 0) {
975 cnt = ret;
976 goto out;
977 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400978 ftrace_profile_enabled = 1;
979 } else {
980 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400981 /*
982 * unregister_ftrace_profiler calls stop_machine
983 * so this acts like an synchronize_sched.
984 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400985 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400986 }
987 }
988 out:
989 mutex_unlock(&ftrace_profile_lock);
990
Jiri Olsacf8517c2009-10-23 19:36:16 -0400991 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400992
993 return cnt;
994}
995
996static ssize_t
997ftrace_profile_read(struct file *filp, char __user *ubuf,
998 size_t cnt, loff_t *ppos)
999{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001000 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001001 int r;
1002
1003 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1004 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1005}
1006
1007static const struct file_operations ftrace_profile_fops = {
1008 .open = tracing_open_generic,
1009 .read = ftrace_profile_read,
1010 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001011 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001012};
1013
Steven Rostedtcafb1682009-03-24 20:50:39 -04001014/* used to initialize the real stat files */
1015static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001016 .name = "functions",
1017 .stat_start = function_stat_start,
1018 .stat_next = function_stat_next,
1019 .stat_cmp = function_stat_cmp,
1020 .stat_headers = function_stat_headers,
1021 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001022};
1023
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001024static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001025{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001026 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001027 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001028 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001029 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001030 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001031
Steven Rostedtcafb1682009-03-24 20:50:39 -04001032 for_each_possible_cpu(cpu) {
1033 stat = &per_cpu(ftrace_profile_stats, cpu);
1034
1035 /* allocate enough for function name + cpu number */
1036 name = kmalloc(32, GFP_KERNEL);
1037 if (!name) {
1038 /*
1039 * The files created are permanent, if something happens
1040 * we still do not free memory.
1041 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001042 WARN(1,
1043 "Could not allocate stat file for cpu %d\n",
1044 cpu);
1045 return;
1046 }
1047 stat->stat = function_stats;
1048 snprintf(name, 32, "function%d", cpu);
1049 stat->stat.name = name;
1050 ret = register_stat_tracer(&stat->stat);
1051 if (ret) {
1052 WARN(1,
1053 "Could not register function stat for cpu %d\n",
1054 cpu);
1055 kfree(name);
1056 return;
1057 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001058 }
1059
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001060 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001061 d_tracer, NULL, &ftrace_profile_fops);
1062 if (!entry)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001063 pr_warning("Could not create tracefs "
Steven Rostedt493762f2009-03-23 17:12:36 -04001064 "'function_profile_enabled' entry\n");
1065}
1066
1067#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001068static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001069{
1070}
1071#endif /* CONFIG_FUNCTION_PROFILER */
1072
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001073static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1074
Pratyush Anand1619dc32015-03-06 23:58:06 +05301075#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1076static int ftrace_graph_active;
1077#else
1078# define ftrace_graph_active 0
1079#endif
1080
Steven Rostedt3d083392008-05-12 21:20:42 +02001081#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001082
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001083static struct ftrace_ops *removed_ops;
1084
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001085/*
1086 * Set when doing a global update, like enabling all recs or disabling them.
1087 * It is not set when just updating a single ftrace_ops.
1088 */
1089static bool update_all_ops;
1090
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001091#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001092# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001093#endif
1094
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001095static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1096
Steven Rostedtb6887d72009-02-17 12:32:04 -05001097struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001098 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001099 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001100 unsigned long flags;
1101 unsigned long ip;
1102 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001103 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001104};
1105
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001106struct ftrace_func_entry {
1107 struct hlist_node hlist;
1108 unsigned long ip;
1109};
1110
1111struct ftrace_hash {
1112 unsigned long size_bits;
1113 struct hlist_head *buckets;
1114 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001115 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001116};
1117
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001118/*
1119 * We make these constant because no one should touch them,
1120 * but they are used as the default "empty hash", to avoid allocating
1121 * it all the time. These are in a read only section such that if
1122 * anyone does try to modify it, it will cause an exception.
1123 */
1124static const struct hlist_head empty_buckets[1];
1125static const struct ftrace_hash empty_hash = {
1126 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001127};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001128#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001129
Steven Rostedt2b499382011-05-03 22:49:52 -04001130static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001131 .func = ftrace_stub,
1132 .local_hash.notrace_hash = EMPTY_HASH,
1133 .local_hash.filter_hash = EMPTY_HASH,
1134 INIT_OPS_HASH(global_ops)
1135 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1136 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001137};
1138
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001139/*
1140 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001141 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001142 * not return true for either core_kernel_text() or
1143 * is_module_text_address().
1144 */
1145bool is_ftrace_trampoline(unsigned long addr)
1146{
1147 struct ftrace_ops *op;
1148 bool ret = false;
1149
1150 /*
1151 * Some of the ops may be dynamically allocated,
1152 * they are freed after a synchronize_sched().
1153 */
1154 preempt_disable_notrace();
1155
1156 do_for_each_ftrace_op(op, ftrace_ops_list) {
1157 /*
1158 * This is to check for dynamically allocated trampolines.
1159 * Trampolines that are in kernel text will have
1160 * core_kernel_text() return true.
1161 */
1162 if (op->trampoline && op->trampoline_size)
1163 if (addr >= op->trampoline &&
1164 addr < op->trampoline + op->trampoline_size) {
1165 ret = true;
1166 goto out;
1167 }
1168 } while_for_each_ftrace_op(op);
1169
1170 out:
1171 preempt_enable_notrace();
1172
1173 return ret;
1174}
1175
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001176struct ftrace_page {
1177 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001178 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001179 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001180 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001181};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001182
Steven Rostedta7900872011-12-16 16:23:44 -05001183#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1184#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001185
1186/* estimate from running different kernels */
1187#define NR_TO_INIT 10000
1188
1189static struct ftrace_page *ftrace_pages_start;
1190static struct ftrace_page *ftrace_pages;
1191
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001192static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001193{
1194 return !hash || !hash->count;
1195}
1196
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001197static struct ftrace_func_entry *
1198ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1199{
1200 unsigned long key;
1201 struct ftrace_func_entry *entry;
1202 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001203
Steven Rostedt06a51d92011-12-19 19:07:36 -05001204 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001205 return NULL;
1206
1207 if (hash->size_bits > 0)
1208 key = hash_long(ip, hash->size_bits);
1209 else
1210 key = 0;
1211
1212 hhd = &hash->buckets[key];
1213
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001214 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001215 if (entry->ip == ip)
1216 return entry;
1217 }
1218 return NULL;
1219}
1220
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001221static void __add_hash_entry(struct ftrace_hash *hash,
1222 struct ftrace_func_entry *entry)
1223{
1224 struct hlist_head *hhd;
1225 unsigned long key;
1226
1227 if (hash->size_bits)
1228 key = hash_long(entry->ip, hash->size_bits);
1229 else
1230 key = 0;
1231
1232 hhd = &hash->buckets[key];
1233 hlist_add_head(&entry->hlist, hhd);
1234 hash->count++;
1235}
1236
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001237static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1238{
1239 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001240
1241 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1242 if (!entry)
1243 return -ENOMEM;
1244
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001245 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001246 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001247
1248 return 0;
1249}
1250
1251static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001252free_hash_entry(struct ftrace_hash *hash,
1253 struct ftrace_func_entry *entry)
1254{
1255 hlist_del(&entry->hlist);
1256 kfree(entry);
1257 hash->count--;
1258}
1259
1260static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001261remove_hash_entry(struct ftrace_hash *hash,
1262 struct ftrace_func_entry *entry)
1263{
1264 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001265 hash->count--;
1266}
1267
1268static void ftrace_hash_clear(struct ftrace_hash *hash)
1269{
1270 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001271 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001272 struct ftrace_func_entry *entry;
1273 int size = 1 << hash->size_bits;
1274 int i;
1275
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276 if (!hash->count)
1277 return;
1278
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001279 for (i = 0; i < size; i++) {
1280 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001281 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001282 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001283 }
1284 FTRACE_WARN_ON(hash->count);
1285}
1286
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001287static void free_ftrace_hash(struct ftrace_hash *hash)
1288{
1289 if (!hash || hash == EMPTY_HASH)
1290 return;
1291 ftrace_hash_clear(hash);
1292 kfree(hash->buckets);
1293 kfree(hash);
1294}
1295
Steven Rostedt07fd5512011-05-05 18:03:47 -04001296static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1297{
1298 struct ftrace_hash *hash;
1299
1300 hash = container_of(rcu, struct ftrace_hash, rcu);
1301 free_ftrace_hash(hash);
1302}
1303
1304static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1305{
1306 if (!hash || hash == EMPTY_HASH)
1307 return;
1308 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1309}
1310
Jiri Olsa5500fa52012-02-15 15:51:54 +01001311void ftrace_free_filter(struct ftrace_ops *ops)
1312{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001313 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001314 free_ftrace_hash(ops->func_hash->filter_hash);
1315 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001316}
1317
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001318static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1319{
1320 struct ftrace_hash *hash;
1321 int size;
1322
1323 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1324 if (!hash)
1325 return NULL;
1326
1327 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001328 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001329
1330 if (!hash->buckets) {
1331 kfree(hash);
1332 return NULL;
1333 }
1334
1335 hash->size_bits = size_bits;
1336
1337 return hash;
1338}
1339
1340static struct ftrace_hash *
1341alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1342{
1343 struct ftrace_func_entry *entry;
1344 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001345 int size;
1346 int ret;
1347 int i;
1348
1349 new_hash = alloc_ftrace_hash(size_bits);
1350 if (!new_hash)
1351 return NULL;
1352
1353 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001354 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001355 return new_hash;
1356
1357 size = 1 << hash->size_bits;
1358 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001359 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001360 ret = add_hash_entry(new_hash, entry->ip);
1361 if (ret < 0)
1362 goto free_hash;
1363 }
1364 }
1365
1366 FTRACE_WARN_ON(new_hash->count != hash->count);
1367
1368 return new_hash;
1369
1370 free_hash:
1371 free_ftrace_hash(new_hash);
1372 return NULL;
1373}
1374
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001375static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001376ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001377static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001378ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001379
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001380static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1381 struct ftrace_hash *new_hash);
1382
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001383static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001384ftrace_hash_move(struct ftrace_ops *ops, int enable,
1385 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001386{
1387 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001388 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001389 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001390 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001391 int size = src->count;
1392 int bits = 0;
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001393 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001394 int i;
1395
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001396 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1397 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1398 return -EINVAL;
1399
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001400 /*
1401 * If the new source is empty, just free dst and assign it
1402 * the empty_hash.
1403 */
1404 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001405 new_hash = EMPTY_HASH;
1406 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001407 }
1408
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001409 /*
1410 * Make the hash size about 1/2 the # found
1411 */
1412 for (size /= 2; size; size >>= 1)
1413 bits++;
1414
1415 /* Don't allocate too much */
1416 if (bits > FTRACE_HASH_MAX_BITS)
1417 bits = FTRACE_HASH_MAX_BITS;
1418
Steven Rostedt07fd5512011-05-05 18:03:47 -04001419 new_hash = alloc_ftrace_hash(bits);
1420 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001421 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001422
1423 size = 1 << src->size_bits;
1424 for (i = 0; i < size; i++) {
1425 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001426 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001427 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001428 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001429 }
1430 }
1431
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001432update:
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001433 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1434 if (enable) {
1435 /* IPMODIFY should be updated only when filter_hash updating */
1436 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1437 if (ret < 0) {
1438 free_ftrace_hash(new_hash);
1439 return ret;
1440 }
1441 }
1442
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001443 /*
1444 * Remove the current set, update the hash and add
1445 * them back.
1446 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001447 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001448
Steven Rostedt07fd5512011-05-05 18:03:47 -04001449 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001450
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001451 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001452
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001453 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001454}
1455
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001456static bool hash_contains_ip(unsigned long ip,
1457 struct ftrace_ops_hash *hash)
1458{
1459 /*
1460 * The function record is a match if it exists in the filter
1461 * hash and not in the notrace hash. Note, an emty hash is
1462 * considered a match for the filter hash, but an empty
1463 * notrace hash is considered not in the notrace hash.
1464 */
1465 return (ftrace_hash_empty(hash->filter_hash) ||
1466 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1467 (ftrace_hash_empty(hash->notrace_hash) ||
1468 !ftrace_lookup_ip(hash->notrace_hash, ip));
1469}
1470
Steven Rostedt265c8312009-02-13 12:43:56 -05001471/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001472 * Test the hashes for this ops to see if we want to call
1473 * the ops->func or not.
1474 *
1475 * It's a match if the ip is in the ops->filter_hash or
1476 * the filter_hash does not exist or is empty,
1477 * AND
1478 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001479 *
1480 * This needs to be called with preemption disabled as
1481 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001482 */
1483static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001484ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001485{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001486 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001487 int ret;
1488
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001489#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1490 /*
1491 * There's a small race when adding ops that the ftrace handler
1492 * that wants regs, may be called without them. We can not
1493 * allow that handler to be called if regs is NULL.
1494 */
1495 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1496 return 0;
1497#endif
1498
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001499 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1500 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001501
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001502 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001503 ret = 1;
1504 else
1505 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001506
1507 return ret;
1508}
1509
1510/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001511 * This is a double for. Do not use 'break' to break out of the loop,
1512 * you must use a goto.
1513 */
1514#define do_for_each_ftrace_rec(pg, rec) \
1515 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1516 int _____i; \
1517 for (_____i = 0; _____i < pg->index; _____i++) { \
1518 rec = &pg->records[_____i];
1519
1520#define while_for_each_ftrace_rec() \
1521 } \
1522 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301523
Steven Rostedt5855fea2011-12-16 19:27:42 -05001524
1525static int ftrace_cmp_recs(const void *a, const void *b)
1526{
Steven Rostedta650e022012-04-25 13:48:13 -04001527 const struct dyn_ftrace *key = a;
1528 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001529
Steven Rostedta650e022012-04-25 13:48:13 -04001530 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001531 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001532 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1533 return 1;
1534 return 0;
1535}
1536
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001537static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001538{
1539 struct ftrace_page *pg;
1540 struct dyn_ftrace *rec;
1541 struct dyn_ftrace key;
1542
1543 key.ip = start;
1544 key.flags = end; /* overload flags, as it is unsigned long */
1545
1546 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1547 if (end < pg->records[0].ip ||
1548 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1549 continue;
1550 rec = bsearch(&key, pg->records, pg->index,
1551 sizeof(struct dyn_ftrace),
1552 ftrace_cmp_recs);
1553 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001554 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001555 }
1556
Steven Rostedt5855fea2011-12-16 19:27:42 -05001557 return 0;
1558}
1559
Steven Rostedtc88fd862011-08-16 09:53:39 -04001560/**
1561 * ftrace_location - return true if the ip giving is a traced location
1562 * @ip: the instruction pointer to check
1563 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001564 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001565 * That is, the instruction that is either a NOP or call to
1566 * the function tracer. It checks the ftrace internal tables to
1567 * determine if the address belongs or not.
1568 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001569unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001570{
Steven Rostedta650e022012-04-25 13:48:13 -04001571 return ftrace_location_range(ip, ip);
1572}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001573
Steven Rostedta650e022012-04-25 13:48:13 -04001574/**
1575 * ftrace_text_reserved - return true if range contains an ftrace location
1576 * @start: start of range to search
1577 * @end: end of range to search (inclusive). @end points to the last byte to check.
1578 *
1579 * Returns 1 if @start and @end contains a ftrace location.
1580 * That is, the instruction that is either a NOP or call to
1581 * the function tracer. It checks the ftrace internal tables to
1582 * determine if the address belongs or not.
1583 */
Sasha Levind88471c2013-01-09 18:09:20 -05001584int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001585{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001586 unsigned long ret;
1587
1588 ret = ftrace_location_range((unsigned long)start,
1589 (unsigned long)end);
1590
1591 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001592}
1593
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001594/* Test if ops registered to this rec needs regs */
1595static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1596{
1597 struct ftrace_ops *ops;
1598 bool keep_regs = false;
1599
1600 for (ops = ftrace_ops_list;
1601 ops != &ftrace_list_end; ops = ops->next) {
1602 /* pass rec in as regs to have non-NULL val */
1603 if (ftrace_ops_test(ops, rec->ip, rec)) {
1604 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1605 keep_regs = true;
1606 break;
1607 }
1608 }
1609 }
1610
1611 return keep_regs;
1612}
1613
Steven Rostedted926f92011-05-03 13:25:24 -04001614static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1615 int filter_hash,
1616 bool inc)
1617{
1618 struct ftrace_hash *hash;
1619 struct ftrace_hash *other_hash;
1620 struct ftrace_page *pg;
1621 struct dyn_ftrace *rec;
1622 int count = 0;
1623 int all = 0;
1624
1625 /* Only update if the ops has been registered */
1626 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1627 return;
1628
1629 /*
1630 * In the filter_hash case:
1631 * If the count is zero, we update all records.
1632 * Otherwise we just update the items in the hash.
1633 *
1634 * In the notrace_hash case:
1635 * We enable the update in the hash.
1636 * As disabling notrace means enabling the tracing,
1637 * and enabling notrace means disabling, the inc variable
1638 * gets inversed.
1639 */
1640 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001641 hash = ops->func_hash->filter_hash;
1642 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001643 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001644 all = 1;
1645 } else {
1646 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001647 hash = ops->func_hash->notrace_hash;
1648 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001649 /*
1650 * If the notrace hash has no items,
1651 * then there's nothing to do.
1652 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001653 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001654 return;
1655 }
1656
1657 do_for_each_ftrace_rec(pg, rec) {
1658 int in_other_hash = 0;
1659 int in_hash = 0;
1660 int match = 0;
1661
1662 if (all) {
1663 /*
1664 * Only the filter_hash affects all records.
1665 * Update if the record is not in the notrace hash.
1666 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001667 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001668 match = 1;
1669 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001670 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1671 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001672
1673 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001674 * If filter_hash is set, we want to match all functions
1675 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001676 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001677 * If filter_hash is not set, then we are decrementing.
1678 * That means we match anything that is in the hash
1679 * and also in the other_hash. That is, we need to turn
1680 * off functions in the other hash because they are disabled
1681 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001682 */
1683 if (filter_hash && in_hash && !in_other_hash)
1684 match = 1;
1685 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001686 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001687 match = 1;
1688 }
1689 if (!match)
1690 continue;
1691
1692 if (inc) {
1693 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001694 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001695 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001696
1697 /*
1698 * If there's only a single callback registered to a
1699 * function, and the ops has a trampoline registered
1700 * for it, then we can call it directly.
1701 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001702 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001703 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001704 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001705 /*
1706 * If we are adding another function callback
1707 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001708 * custom trampoline in use, then we need to go
1709 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001710 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001711 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001712
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001713 /*
1714 * If any ops wants regs saved for this function
1715 * then all ops will get saved regs.
1716 */
1717 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1718 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001719 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001720 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001721 return;
1722 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001723
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001724 /*
1725 * If the rec had REGS enabled and the ops that is
1726 * being removed had REGS set, then see if there is
1727 * still any ops for this record that wants regs.
1728 * If not, we can stop recording them.
1729 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001730 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001731 rec->flags & FTRACE_FL_REGS &&
1732 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1733 if (!test_rec_ops_needs_regs(rec))
1734 rec->flags &= ~FTRACE_FL_REGS;
1735 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001736
1737 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001738 * If the rec had TRAMP enabled, then it needs to
1739 * be cleared. As TRAMP can only be enabled iff
1740 * there is only a single ops attached to it.
1741 * In otherwords, always disable it on decrementing.
1742 * In the future, we may set it if rec count is
1743 * decremented to one, and the ops that is left
1744 * has a trampoline.
1745 */
1746 rec->flags &= ~FTRACE_FL_TRAMP;
1747
1748 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001749 * flags will be cleared in ftrace_check_record()
1750 * if rec count is zero.
1751 */
Steven Rostedted926f92011-05-03 13:25:24 -04001752 }
1753 count++;
1754 /* Shortcut, if we handled all records, we are done. */
1755 if (!all && count == hash->count)
1756 return;
1757 } while_for_each_ftrace_rec();
1758}
1759
1760static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1761 int filter_hash)
1762{
1763 __ftrace_hash_rec_update(ops, filter_hash, 0);
1764}
1765
1766static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1767 int filter_hash)
1768{
1769 __ftrace_hash_rec_update(ops, filter_hash, 1);
1770}
1771
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001772static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1773 int filter_hash, int inc)
1774{
1775 struct ftrace_ops *op;
1776
1777 __ftrace_hash_rec_update(ops, filter_hash, inc);
1778
1779 if (ops->func_hash != &global_ops.local_hash)
1780 return;
1781
1782 /*
1783 * If the ops shares the global_ops hash, then we need to update
1784 * all ops that are enabled and use this hash.
1785 */
1786 do_for_each_ftrace_op(op, ftrace_ops_list) {
1787 /* Already done */
1788 if (op == ops)
1789 continue;
1790 if (op->func_hash == &global_ops.local_hash)
1791 __ftrace_hash_rec_update(op, filter_hash, inc);
1792 } while_for_each_ftrace_op(op);
1793}
1794
1795static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1796 int filter_hash)
1797{
1798 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1799}
1800
1801static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1802 int filter_hash)
1803{
1804 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1805}
1806
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001807/*
1808 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1809 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1810 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1811 * Note that old_hash and new_hash has below meanings
1812 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1813 * - If the hash is EMPTY_HASH, it hits nothing
1814 * - Anything else hits the recs which match the hash entries.
1815 */
1816static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1817 struct ftrace_hash *old_hash,
1818 struct ftrace_hash *new_hash)
1819{
1820 struct ftrace_page *pg;
1821 struct dyn_ftrace *rec, *end = NULL;
1822 int in_old, in_new;
1823
1824 /* Only update if the ops has been registered */
1825 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1826 return 0;
1827
1828 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1829 return 0;
1830
1831 /*
1832 * Since the IPMODIFY is a very address sensitive action, we do not
1833 * allow ftrace_ops to set all functions to new hash.
1834 */
1835 if (!new_hash || !old_hash)
1836 return -EINVAL;
1837
1838 /* Update rec->flags */
1839 do_for_each_ftrace_rec(pg, rec) {
1840 /* We need to update only differences of filter_hash */
1841 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1842 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1843 if (in_old == in_new)
1844 continue;
1845
1846 if (in_new) {
1847 /* New entries must ensure no others are using it */
1848 if (rec->flags & FTRACE_FL_IPMODIFY)
1849 goto rollback;
1850 rec->flags |= FTRACE_FL_IPMODIFY;
1851 } else /* Removed entry */
1852 rec->flags &= ~FTRACE_FL_IPMODIFY;
1853 } while_for_each_ftrace_rec();
1854
1855 return 0;
1856
1857rollback:
1858 end = rec;
1859
1860 /* Roll back what we did above */
1861 do_for_each_ftrace_rec(pg, rec) {
1862 if (rec == end)
1863 goto err_out;
1864
1865 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1866 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1867 if (in_old == in_new)
1868 continue;
1869
1870 if (in_new)
1871 rec->flags &= ~FTRACE_FL_IPMODIFY;
1872 else
1873 rec->flags |= FTRACE_FL_IPMODIFY;
1874 } while_for_each_ftrace_rec();
1875
1876err_out:
1877 return -EBUSY;
1878}
1879
1880static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1881{
1882 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1883
1884 if (ftrace_hash_empty(hash))
1885 hash = NULL;
1886
1887 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1888}
1889
1890/* Disabling always succeeds */
1891static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1892{
1893 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1894
1895 if (ftrace_hash_empty(hash))
1896 hash = NULL;
1897
1898 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1899}
1900
1901static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1902 struct ftrace_hash *new_hash)
1903{
1904 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1905
1906 if (ftrace_hash_empty(old_hash))
1907 old_hash = NULL;
1908
1909 if (ftrace_hash_empty(new_hash))
1910 new_hash = NULL;
1911
1912 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1913}
1914
Steven Rostedt05736a42008-09-22 14:55:47 -07001915static void print_ip_ins(const char *fmt, unsigned char *p)
1916{
1917 int i;
1918
1919 printk(KERN_CONT "%s", fmt);
1920
1921 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1922 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1923}
1924
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001925static struct ftrace_ops *
1926ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1927
Steven Rostedtc88fd862011-08-16 09:53:39 -04001928/**
1929 * ftrace_bug - report and shutdown function tracer
1930 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001931 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001932 *
1933 * The arch code that enables or disables the function tracing
1934 * can call ftrace_bug() when it has detected a problem in
1935 * modifying the code. @failed should be one of either:
1936 * EFAULT - if the problem happens on reading the @ip address
1937 * EINVAL - if what is read at @ip is not what was expected
1938 * EPERM - if the problem happens on writting to the @ip address
1939 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001940void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001941{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001942 unsigned long ip = rec ? rec->ip : 0;
1943
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001944 switch (failed) {
1945 case -EFAULT:
1946 FTRACE_WARN_ON_ONCE(1);
1947 pr_info("ftrace faulted on modifying ");
1948 print_ip_sym(ip);
1949 break;
1950 case -EINVAL:
1951 FTRACE_WARN_ON_ONCE(1);
1952 pr_info("ftrace failed to modify ");
1953 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001954 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001955 pr_cont("\n");
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001956 break;
1957 case -EPERM:
1958 FTRACE_WARN_ON_ONCE(1);
1959 pr_info("ftrace faulted on writing ");
1960 print_ip_sym(ip);
1961 break;
1962 default:
1963 FTRACE_WARN_ON_ONCE(1);
1964 pr_info("ftrace faulted on unknown error ");
1965 print_ip_sym(ip);
1966 }
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001967 if (rec) {
1968 struct ftrace_ops *ops = NULL;
1969
1970 pr_info("ftrace record flags: %lx\n", rec->flags);
1971 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
1972 rec->flags & FTRACE_FL_REGS ? " R" : " ");
1973 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1974 ops = ftrace_find_tramp_ops_any(rec);
1975 if (ops)
1976 pr_cont("\ttramp: %pS",
1977 (void *)ops->trampoline);
1978 else
1979 pr_cont("\ttramp: ERROR!");
1980
1981 }
1982 ip = ftrace_get_addr_curr(rec);
1983 pr_cont(" expected tramp: %lx\n", ip);
1984 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001985}
1986
Steven Rostedtc88fd862011-08-16 09:53:39 -04001987static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001988{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001989 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001990
Steven Rostedt982c3502008-11-15 16:31:41 -05001991 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001992 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001993 *
Steven Rostedted926f92011-05-03 13:25:24 -04001994 * If the record has a ref count, then we need to enable it
1995 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001996 *
Steven Rostedted926f92011-05-03 13:25:24 -04001997 * Otherwise we make sure its disabled.
1998 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001999 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002000 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002001 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002002 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002003 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002004
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002005 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002006 * If enabling and the REGS flag does not match the REGS_EN, or
2007 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2008 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002009 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002010 if (flag) {
2011 if (!(rec->flags & FTRACE_FL_REGS) !=
2012 !(rec->flags & FTRACE_FL_REGS_EN))
2013 flag |= FTRACE_FL_REGS;
2014
2015 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2016 !(rec->flags & FTRACE_FL_TRAMP_EN))
2017 flag |= FTRACE_FL_TRAMP;
2018 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002019
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002020 /* If the state of this record hasn't changed, then do nothing */
2021 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002022 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002023
2024 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002025 /* Save off if rec is being enabled (for return value) */
2026 flag ^= rec->flags & FTRACE_FL_ENABLED;
2027
2028 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002029 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002030 if (flag & FTRACE_FL_REGS) {
2031 if (rec->flags & FTRACE_FL_REGS)
2032 rec->flags |= FTRACE_FL_REGS_EN;
2033 else
2034 rec->flags &= ~FTRACE_FL_REGS_EN;
2035 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002036 if (flag & FTRACE_FL_TRAMP) {
2037 if (rec->flags & FTRACE_FL_TRAMP)
2038 rec->flags |= FTRACE_FL_TRAMP_EN;
2039 else
2040 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2041 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002042 }
2043
2044 /*
2045 * If this record is being updated from a nop, then
2046 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002047 * Otherwise,
2048 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002049 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002050 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002051 */
2052 if (flag & FTRACE_FL_ENABLED)
2053 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002054
2055 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002056 }
2057
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002058 if (update) {
2059 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002060 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002061 rec->flags = 0;
2062 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002063 /*
2064 * Just disable the record, but keep the ops TRAMP
2065 * and REGS states. The _EN flags must be disabled though.
2066 */
2067 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2068 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002069 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002070
2071 return FTRACE_UPDATE_MAKE_NOP;
2072}
2073
2074/**
2075 * ftrace_update_record, set a record that now is tracing or not
2076 * @rec: the record to update
2077 * @enable: set to 1 if the record is tracing, zero to force disable
2078 *
2079 * The records that represent all functions that can be traced need
2080 * to be updated when tracing has been enabled.
2081 */
2082int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2083{
2084 return ftrace_check_record(rec, enable, 1);
2085}
2086
2087/**
2088 * ftrace_test_record, check if the record has been enabled or not
2089 * @rec: the record to test
2090 * @enable: set to 1 to check if enabled, 0 if it is disabled
2091 *
2092 * The arch code may need to test if a record is already set to
2093 * tracing to determine how to modify the function code that it
2094 * represents.
2095 */
2096int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2097{
2098 return ftrace_check_record(rec, enable, 0);
2099}
2100
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002101static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002102ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2103{
2104 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002105 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002106
2107 do_for_each_ftrace_op(op, ftrace_ops_list) {
2108
2109 if (!op->trampoline)
2110 continue;
2111
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002112 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002113 return op;
2114 } while_for_each_ftrace_op(op);
2115
2116 return NULL;
2117}
2118
2119static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002120ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2121{
2122 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002123 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002124
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002125 /*
2126 * Need to check removed ops first.
2127 * If they are being removed, and this rec has a tramp,
2128 * and this rec is in the ops list, then it would be the
2129 * one with the tramp.
2130 */
2131 if (removed_ops) {
2132 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002133 return removed_ops;
2134 }
2135
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002136 /*
2137 * Need to find the current trampoline for a rec.
2138 * Now, a trampoline is only attached to a rec if there
2139 * was a single 'ops' attached to it. But this can be called
2140 * when we are adding another op to the rec or removing the
2141 * current one. Thus, if the op is being added, we can
2142 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002143 * yet.
2144 *
2145 * If an ops is being modified (hooking to different functions)
2146 * then we don't care about the new functions that are being
2147 * added, just the old ones (that are probably being removed).
2148 *
2149 * If we are adding an ops to a function that already is using
2150 * a trampoline, it needs to be removed (trampolines are only
2151 * for single ops connected), then an ops that is not being
2152 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002153 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002154 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002155
2156 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002157 continue;
2158
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002159 /*
2160 * If the ops is being added, it hasn't gotten to
2161 * the point to be removed from this tree yet.
2162 */
2163 if (op->flags & FTRACE_OPS_FL_ADDING)
2164 continue;
2165
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002166
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002167 /*
2168 * If the ops is being modified and is in the old
2169 * hash, then it is probably being removed from this
2170 * function.
2171 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002172 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2173 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002174 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002175 /*
2176 * If the ops is not being added or modified, and it's
2177 * in its normal filter hash, then this must be the one
2178 * we want!
2179 */
2180 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2181 hash_contains_ip(ip, op->func_hash))
2182 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002183
2184 } while_for_each_ftrace_op(op);
2185
2186 return NULL;
2187}
2188
2189static struct ftrace_ops *
2190ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2191{
2192 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002193 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002194
2195 do_for_each_ftrace_op(op, ftrace_ops_list) {
2196 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002197 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002198 return op;
2199 } while_for_each_ftrace_op(op);
2200
2201 return NULL;
2202}
2203
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002204/**
2205 * ftrace_get_addr_new - Get the call address to set to
2206 * @rec: The ftrace record descriptor
2207 *
2208 * If the record has the FTRACE_FL_REGS set, that means that it
2209 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2210 * is not not set, then it wants to convert to the normal callback.
2211 *
2212 * Returns the address of the trampoline to set to
2213 */
2214unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2215{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002216 struct ftrace_ops *ops;
2217
2218 /* Trampolines take precedence over regs */
2219 if (rec->flags & FTRACE_FL_TRAMP) {
2220 ops = ftrace_find_tramp_ops_new(rec);
2221 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002222 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2223 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002224 /* Ftrace is shutting down, return anything */
2225 return (unsigned long)FTRACE_ADDR;
2226 }
2227 return ops->trampoline;
2228 }
2229
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002230 if (rec->flags & FTRACE_FL_REGS)
2231 return (unsigned long)FTRACE_REGS_ADDR;
2232 else
2233 return (unsigned long)FTRACE_ADDR;
2234}
2235
2236/**
2237 * ftrace_get_addr_curr - Get the call address that is already there
2238 * @rec: The ftrace record descriptor
2239 *
2240 * The FTRACE_FL_REGS_EN is set when the record already points to
2241 * a function that saves all the regs. Basically the '_EN' version
2242 * represents the current state of the function.
2243 *
2244 * Returns the address of the trampoline that is currently being called
2245 */
2246unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2247{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002248 struct ftrace_ops *ops;
2249
2250 /* Trampolines take precedence over regs */
2251 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2252 ops = ftrace_find_tramp_ops_curr(rec);
2253 if (FTRACE_WARN_ON(!ops)) {
2254 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2255 (void *)rec->ip, (void *)rec->ip);
2256 /* Ftrace is shutting down, return anything */
2257 return (unsigned long)FTRACE_ADDR;
2258 }
2259 return ops->trampoline;
2260 }
2261
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002262 if (rec->flags & FTRACE_FL_REGS_EN)
2263 return (unsigned long)FTRACE_REGS_ADDR;
2264 else
2265 return (unsigned long)FTRACE_ADDR;
2266}
2267
Steven Rostedtc88fd862011-08-16 09:53:39 -04002268static int
2269__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2270{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002271 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002272 unsigned long ftrace_addr;
2273 int ret;
2274
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002275 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002276
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002277 /* This needs to be done before we call ftrace_update_record */
2278 ftrace_old_addr = ftrace_get_addr_curr(rec);
2279
2280 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002281
Steven Rostedtc88fd862011-08-16 09:53:39 -04002282 switch (ret) {
2283 case FTRACE_UPDATE_IGNORE:
2284 return 0;
2285
2286 case FTRACE_UPDATE_MAKE_CALL:
2287 return ftrace_make_call(rec, ftrace_addr);
2288
2289 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002290 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002291
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002292 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002293 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002294 }
2295
2296 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002297}
2298
Steven Rostedte4f5d542012-04-27 09:13:18 -04002299void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002300{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002301 struct dyn_ftrace *rec;
2302 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002303 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002304
Steven Rostedt45a4a232011-04-21 23:16:46 -04002305 if (unlikely(ftrace_disabled))
2306 return;
2307
Steven Rostedt265c8312009-02-13 12:43:56 -05002308 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002309 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002310 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002311 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002312 /* Stop processing */
2313 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002314 }
2315 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002316}
2317
Steven Rostedtc88fd862011-08-16 09:53:39 -04002318struct ftrace_rec_iter {
2319 struct ftrace_page *pg;
2320 int index;
2321};
2322
2323/**
2324 * ftrace_rec_iter_start, start up iterating over traced functions
2325 *
2326 * Returns an iterator handle that is used to iterate over all
2327 * the records that represent address locations where functions
2328 * are traced.
2329 *
2330 * May return NULL if no records are available.
2331 */
2332struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2333{
2334 /*
2335 * We only use a single iterator.
2336 * Protected by the ftrace_lock mutex.
2337 */
2338 static struct ftrace_rec_iter ftrace_rec_iter;
2339 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2340
2341 iter->pg = ftrace_pages_start;
2342 iter->index = 0;
2343
2344 /* Could have empty pages */
2345 while (iter->pg && !iter->pg->index)
2346 iter->pg = iter->pg->next;
2347
2348 if (!iter->pg)
2349 return NULL;
2350
2351 return iter;
2352}
2353
2354/**
2355 * ftrace_rec_iter_next, get the next record to process.
2356 * @iter: The handle to the iterator.
2357 *
2358 * Returns the next iterator after the given iterator @iter.
2359 */
2360struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2361{
2362 iter->index++;
2363
2364 if (iter->index >= iter->pg->index) {
2365 iter->pg = iter->pg->next;
2366 iter->index = 0;
2367
2368 /* Could have empty pages */
2369 while (iter->pg && !iter->pg->index)
2370 iter->pg = iter->pg->next;
2371 }
2372
2373 if (!iter->pg)
2374 return NULL;
2375
2376 return iter;
2377}
2378
2379/**
2380 * ftrace_rec_iter_record, get the record at the iterator location
2381 * @iter: The current iterator location
2382 *
2383 * Returns the record that the current @iter is at.
2384 */
2385struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2386{
2387 return &iter->pg->records[iter->index];
2388}
2389
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302390static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002391ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002392{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002393 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002394
Steven Rostedt45a4a232011-04-21 23:16:46 -04002395 if (unlikely(ftrace_disabled))
2396 return 0;
2397
Shaohua Li25aac9d2009-01-09 11:29:40 +08002398 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002399 if (ret) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002400 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302401 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002402 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302403 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002404}
2405
Steven Rostedt000ab692009-02-17 13:35:06 -05002406/*
2407 * archs can override this function if they must do something
2408 * before the modifying code is performed.
2409 */
2410int __weak ftrace_arch_code_modify_prepare(void)
2411{
2412 return 0;
2413}
2414
2415/*
2416 * archs can override this function if they must do something
2417 * after the modifying code is performed.
2418 */
2419int __weak ftrace_arch_code_modify_post_process(void)
2420{
2421 return 0;
2422}
2423
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002424void ftrace_modify_all_code(int command)
2425{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002426 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002427 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002428
2429 /*
2430 * If the ftrace_caller calls a ftrace_ops func directly,
2431 * we need to make sure that it only traces functions it
2432 * expects to trace. When doing the switch of functions,
2433 * we need to update to the ftrace_ops_list_func first
2434 * before the transition between old and new calls are set,
2435 * as the ftrace_ops_list_func will check the ops hashes
2436 * to make sure the ops are having the right functions
2437 * traced.
2438 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002439 if (update) {
2440 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2441 if (FTRACE_WARN_ON(err))
2442 return;
2443 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002444
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002445 if (command & FTRACE_UPDATE_CALLS)
2446 ftrace_replace_code(1);
2447 else if (command & FTRACE_DISABLE_CALLS)
2448 ftrace_replace_code(0);
2449
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002450 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2451 function_trace_op = set_function_trace_op;
2452 smp_wmb();
2453 /* If irqs are disabled, we are in stop machine */
2454 if (!irqs_disabled())
2455 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002456 err = ftrace_update_ftrace_func(ftrace_trace_function);
2457 if (FTRACE_WARN_ON(err))
2458 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002459 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002460
2461 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002462 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002463 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002464 err = ftrace_disable_ftrace_graph_caller();
2465 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002466}
2467
Ingo Molnare309b412008-05-12 21:20:51 +02002468static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002469{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002470 int *command = data;
2471
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002472 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002473
Steven Rostedtc88fd862011-08-16 09:53:39 -04002474 return 0;
2475}
2476
2477/**
2478 * ftrace_run_stop_machine, go back to the stop machine method
2479 * @command: The command to tell ftrace what to do
2480 *
2481 * If an arch needs to fall back to the stop machine method, the
2482 * it can call this function.
2483 */
2484void ftrace_run_stop_machine(int command)
2485{
2486 stop_machine(__ftrace_modify_code, &command, NULL);
2487}
2488
2489/**
2490 * arch_ftrace_update_code, modify the code to trace or not trace
2491 * @command: The command that needs to be done
2492 *
2493 * Archs can override this function if it does not need to
2494 * run stop_machine() to modify code.
2495 */
2496void __weak arch_ftrace_update_code(int command)
2497{
2498 ftrace_run_stop_machine(command);
2499}
2500
2501static void ftrace_run_update_code(int command)
2502{
2503 int ret;
2504
2505 ret = ftrace_arch_code_modify_prepare();
2506 FTRACE_WARN_ON(ret);
2507 if (ret)
2508 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002509
2510 /*
2511 * By default we use stop_machine() to modify the code.
2512 * But archs can do what ever they want as long as it
2513 * is safe. The stop_machine() is the safest, but also
2514 * produces the most overhead.
2515 */
2516 arch_ftrace_update_code(command);
2517
Steven Rostedt000ab692009-02-17 13:35:06 -05002518 ret = ftrace_arch_code_modify_post_process();
2519 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002520}
2521
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002522static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002523 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002524{
2525 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002526 ops->old_hash.filter_hash = old_hash->filter_hash;
2527 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002528 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002529 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002530 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002531 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2532}
2533
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002534static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002535static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002536
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002537void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2538{
2539}
2540
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002541static void control_ops_free(struct ftrace_ops *ops)
2542{
2543 free_percpu(ops->disabled);
2544}
2545
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002546static void ftrace_startup_enable(int command)
2547{
2548 if (saved_ftrace_func != ftrace_trace_function) {
2549 saved_ftrace_func = ftrace_trace_function;
2550 command |= FTRACE_UPDATE_TRACE_FUNC;
2551 }
2552
2553 if (!command || !ftrace_enabled)
2554 return;
2555
2556 ftrace_run_update_code(command);
2557}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002558
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002559static void ftrace_startup_all(int command)
2560{
2561 update_all_ops = true;
2562 ftrace_startup_enable(command);
2563 update_all_ops = false;
2564}
2565
Steven Rostedta1cd6172011-05-23 15:24:25 -04002566static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002567{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002568 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002569
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002570 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002571 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002572
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002573 ret = __register_ftrace_function(ops);
2574 if (ret)
2575 return ret;
2576
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002577 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002578 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002579
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002580 /*
2581 * Note that ftrace probes uses this to start up
2582 * and modify functions it will probe. But we still
2583 * set the ADDING flag for modification, as probes
2584 * do not have trampolines. If they add them in the
2585 * future, then the probes will need to distinguish
2586 * between adding and updating probes.
2587 */
2588 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002589
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002590 ret = ftrace_hash_ipmodify_enable(ops);
2591 if (ret < 0) {
2592 /* Rollback registration process */
2593 __unregister_ftrace_function(ops);
2594 ftrace_start_up--;
2595 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2596 return ret;
2597 }
2598
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002599 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002600
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002601 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002602
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002603 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2604
Steven Rostedta1cd6172011-05-23 15:24:25 -04002605 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002606}
2607
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002608static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002609{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002610 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002611
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002612 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002613 return -ENODEV;
2614
2615 ret = __unregister_ftrace_function(ops);
2616 if (ret)
2617 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002618
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002619 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002620 /*
2621 * Just warn in case of unbalance, no need to kill ftrace, it's not
2622 * critical but the ftrace_call callers may be never nopped again after
2623 * further ftrace uses.
2624 */
2625 WARN_ON_ONCE(ftrace_start_up < 0);
2626
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002627 /* Disabling ipmodify never fails */
2628 ftrace_hash_ipmodify_disable(ops);
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002629 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002630
Namhyung Kima737e6d2014-06-12 23:56:12 +09002631 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002632
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002633 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002634
2635 if (saved_ftrace_func != ftrace_trace_function) {
2636 saved_ftrace_func = ftrace_trace_function;
2637 command |= FTRACE_UPDATE_TRACE_FUNC;
2638 }
2639
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002640 if (!command || !ftrace_enabled) {
2641 /*
2642 * If these are control ops, they still need their
2643 * per_cpu field freed. Since, function tracing is
2644 * not currently active, we can just free them
2645 * without synchronizing all CPUs.
2646 */
2647 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2648 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002649 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002650 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002651
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002652 /*
2653 * If the ops uses a trampoline, then it needs to be
2654 * tested first on update.
2655 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002656 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002657 removed_ops = ops;
2658
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002659 /* The trampoline logic checks the old hashes */
2660 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2661 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2662
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002663 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002664
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002665 /*
2666 * If there's no more ops registered with ftrace, run a
2667 * sanity check to make sure all rec flags are cleared.
2668 */
2669 if (ftrace_ops_list == &ftrace_list_end) {
2670 struct ftrace_page *pg;
2671 struct dyn_ftrace *rec;
2672
2673 do_for_each_ftrace_rec(pg, rec) {
2674 if (FTRACE_WARN_ON_ONCE(rec->flags))
2675 pr_warn(" %pS flags:%lx\n",
2676 (void *)rec->ip, rec->flags);
2677 } while_for_each_ftrace_rec();
2678 }
2679
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002680 ops->old_hash.filter_hash = NULL;
2681 ops->old_hash.notrace_hash = NULL;
2682
2683 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002684 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002685
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002686 /*
2687 * Dynamic ops may be freed, we must make sure that all
2688 * callers are done before leaving this function.
2689 * The same goes for freeing the per_cpu data of the control
2690 * ops.
2691 *
2692 * Again, normal synchronize_sched() is not good enough.
2693 * We need to do a hard force of sched synchronization.
2694 * This is because we use preempt_disable() to do RCU, but
2695 * the function tracers can be called where RCU is not watching
2696 * (like before user_exit()). We can not rely on the RCU
2697 * infrastructure to do the synchronization, thus we must do it
2698 * ourselves.
2699 */
2700 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2701 schedule_on_each_cpu(ftrace_sync);
2702
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002703 arch_ftrace_trampoline_free(ops);
2704
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002705 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2706 control_ops_free(ops);
2707 }
2708
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002709 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002710}
2711
Ingo Molnare309b412008-05-12 21:20:51 +02002712static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002713{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302714 int command;
2715
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002716 if (unlikely(ftrace_disabled))
2717 return;
2718
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002719 /* Force update next time */
2720 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002721 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302722 if (ftrace_start_up) {
2723 command = FTRACE_UPDATE_CALLS;
2724 if (ftrace_graph_active)
2725 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002726 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302727 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002728}
2729
Ingo Molnare309b412008-05-12 21:20:51 +02002730static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002731{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302732 int command;
2733
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002734 if (unlikely(ftrace_disabled))
2735 return;
2736
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002737 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302738 if (ftrace_start_up) {
2739 command = FTRACE_DISABLE_CALLS;
2740 if (ftrace_graph_active)
2741 command |= FTRACE_STOP_FUNC_RET;
2742 ftrace_run_update_code(command);
2743 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002744}
2745
Steven Rostedt3d083392008-05-12 21:20:42 +02002746static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002747unsigned long ftrace_update_tot_cnt;
2748
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002749static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002750{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002751 /*
2752 * Filter_hash being empty will default to trace module.
2753 * But notrace hash requires a test of individual module functions.
2754 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002755 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2756 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002757}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002758
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002759/*
2760 * Check if the current ops references the record.
2761 *
2762 * If the ops traces all functions, then it was already accounted for.
2763 * If the ops does not trace the current record function, skip it.
2764 * If the ops ignores the function via notrace filter, skip it.
2765 */
2766static inline bool
2767ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2768{
2769 /* If ops isn't enabled, ignore it */
2770 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2771 return 0;
2772
2773 /* If ops traces all mods, we already accounted for it */
2774 if (ops_traces_mod(ops))
2775 return 0;
2776
2777 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002778 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2779 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002780 return 0;
2781
2782 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002783 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002784 return 0;
2785
2786 return 1;
2787}
2788
2789static int referenced_filters(struct dyn_ftrace *rec)
2790{
2791 struct ftrace_ops *ops;
2792 int cnt = 0;
2793
2794 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2795 if (ops_references_rec(ops, rec))
2796 cnt++;
2797 }
2798
2799 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002800}
2801
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002802static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002803{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002804 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002805 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302806 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002807 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002808 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002809 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002810 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002811
2812 /*
2813 * When adding a module, we need to check if tracers are
2814 * currently enabled and if they are set to trace all functions.
2815 * If they are, we need to enable the module functions as well
2816 * as update the reference counts for those function records.
2817 */
2818 if (mod) {
2819 struct ftrace_ops *ops;
2820
2821 for (ops = ftrace_ops_list;
2822 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002823 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2824 if (ops_traces_mod(ops))
2825 ref++;
2826 else
2827 test = true;
2828 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002829 }
2830 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002831
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002832 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002833
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002834 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302835
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002836 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002837 int cnt = ref;
2838
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002839 /* If something went wrong, bail without enabling anything */
2840 if (unlikely(ftrace_disabled))
2841 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002842
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002843 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002844 if (test)
2845 cnt += referenced_filters(p);
2846 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302847
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002848 /*
2849 * Do the initial record conversion from mcount jump
2850 * to the NOP instructions.
2851 */
2852 if (!ftrace_code_disable(mod, p))
2853 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002854
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002855 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002856
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002857 /*
2858 * If the tracing is enabled, go ahead and enable the record.
2859 *
2860 * The reason not to enable the record immediatelly is the
2861 * inherent check of ftrace_make_nop/ftrace_make_call for
2862 * correct previous instructions. Making first the NOP
2863 * conversion puts the module to the correct state, thus
2864 * passing the ftrace_make_call check.
2865 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002866 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002867 int failed = __ftrace_replace_code(p, 1);
2868 if (failed)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002869 ftrace_bug(failed, p);
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002870 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002871 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002872 }
2873
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002874 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002875 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002876 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002877
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002878 return 0;
2879}
2880
Steven Rostedta7900872011-12-16 16:23:44 -05002881static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002882{
Steven Rostedta7900872011-12-16 16:23:44 -05002883 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002884 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002885
Steven Rostedta7900872011-12-16 16:23:44 -05002886 if (WARN_ON(!count))
2887 return -EINVAL;
2888
2889 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002890
2891 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002892 * We want to fill as much as possible. No more than a page
2893 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002894 */
Steven Rostedta7900872011-12-16 16:23:44 -05002895 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2896 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002897
Steven Rostedta7900872011-12-16 16:23:44 -05002898 again:
2899 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2900
2901 if (!pg->records) {
2902 /* if we can't allocate this size, try something smaller */
2903 if (!order)
2904 return -ENOMEM;
2905 order >>= 1;
2906 goto again;
2907 }
2908
2909 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2910 pg->size = cnt;
2911
2912 if (cnt > count)
2913 cnt = count;
2914
2915 return cnt;
2916}
2917
2918static struct ftrace_page *
2919ftrace_allocate_pages(unsigned long num_to_init)
2920{
2921 struct ftrace_page *start_pg;
2922 struct ftrace_page *pg;
2923 int order;
2924 int cnt;
2925
2926 if (!num_to_init)
2927 return 0;
2928
2929 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2930 if (!pg)
2931 return NULL;
2932
2933 /*
2934 * Try to allocate as much as possible in one continues
2935 * location that fills in all of the space. We want to
2936 * waste as little space as possible.
2937 */
2938 for (;;) {
2939 cnt = ftrace_allocate_records(pg, num_to_init);
2940 if (cnt < 0)
2941 goto free_pages;
2942
2943 num_to_init -= cnt;
2944 if (!num_to_init)
2945 break;
2946
2947 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2948 if (!pg->next)
2949 goto free_pages;
2950
2951 pg = pg->next;
2952 }
2953
2954 return start_pg;
2955
2956 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002957 pg = start_pg;
2958 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002959 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2960 free_pages((unsigned long)pg->records, order);
2961 start_pg = pg->next;
2962 kfree(pg);
2963 pg = start_pg;
2964 }
2965 pr_info("ftrace: FAILED to allocate memory for functions\n");
2966 return NULL;
2967}
2968
Steven Rostedt5072c592008-05-12 21:20:43 +02002969#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2970
2971struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002972 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002973 loff_t func_pos;
2974 struct ftrace_page *pg;
2975 struct dyn_ftrace *func;
2976 struct ftrace_func_probe *probe;
2977 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002978 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002979 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002980 int hidx;
2981 int idx;
2982 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002983};
2984
Ingo Molnare309b412008-05-12 21:20:51 +02002985static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002986t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002987{
2988 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002989 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002990 struct hlist_head *hhd;
2991
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002992 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002993 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002994
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002995 if (iter->probe)
2996 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002997 retry:
2998 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2999 return NULL;
3000
3001 hhd = &ftrace_func_hash[iter->hidx];
3002
3003 if (hlist_empty(hhd)) {
3004 iter->hidx++;
3005 hnd = NULL;
3006 goto retry;
3007 }
3008
3009 if (!hnd)
3010 hnd = hhd->first;
3011 else {
3012 hnd = hnd->next;
3013 if (!hnd) {
3014 iter->hidx++;
3015 goto retry;
3016 }
3017 }
3018
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003019 if (WARN_ON_ONCE(!hnd))
3020 return NULL;
3021
3022 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3023
3024 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003025}
3026
3027static void *t_hash_start(struct seq_file *m, loff_t *pos)
3028{
3029 struct ftrace_iterator *iter = m->private;
3030 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003031 loff_t l;
3032
Steven Rostedt69a30832011-12-19 15:21:16 -05003033 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3034 return NULL;
3035
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003036 if (iter->func_pos > *pos)
3037 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003038
Li Zefand82d6242009-06-24 09:54:54 +08003039 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003040 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003041 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003042 if (!p)
3043 break;
3044 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003045 if (!p)
3046 return NULL;
3047
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003048 /* Only set this if we have an item */
3049 iter->flags |= FTRACE_ITER_HASH;
3050
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003051 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003052}
3053
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003054static int
3055t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003056{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003057 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003058
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003059 rec = iter->probe;
3060 if (WARN_ON_ONCE(!rec))
3061 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003062
Steven Rostedt809dcf22009-02-16 23:06:01 -05003063 if (rec->ops->print)
3064 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3065
Steven Rostedtb375a112009-09-17 00:05:58 -04003066 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003067
3068 if (rec->data)
3069 seq_printf(m, ":%p", rec->data);
3070 seq_putc(m, '\n');
3071
3072 return 0;
3073}
3074
3075static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02003076t_next(struct seq_file *m, void *v, loff_t *pos)
3077{
3078 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003079 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003080 struct dyn_ftrace *rec = NULL;
3081
Steven Rostedt45a4a232011-04-21 23:16:46 -04003082 if (unlikely(ftrace_disabled))
3083 return NULL;
3084
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003085 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003086 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003087
Steven Rostedt5072c592008-05-12 21:20:43 +02003088 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01003089 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02003090
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003091 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003092 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003093
Steven Rostedt5072c592008-05-12 21:20:43 +02003094 retry:
3095 if (iter->idx >= iter->pg->index) {
3096 if (iter->pg->next) {
3097 iter->pg = iter->pg->next;
3098 iter->idx = 0;
3099 goto retry;
3100 }
3101 } else {
3102 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05003103 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003104 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05003105
Steven Rostedt41c52c02008-05-22 11:46:33 -04003106 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003107 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003108
3109 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003110 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003111
Steven Rostedt5072c592008-05-12 21:20:43 +02003112 rec = NULL;
3113 goto retry;
3114 }
3115 }
3116
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003117 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003118 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003119
3120 iter->func = rec;
3121
3122 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003123}
3124
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003125static void reset_iter_read(struct ftrace_iterator *iter)
3126{
3127 iter->pos = 0;
3128 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03003129 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003130}
3131
3132static void *t_start(struct seq_file *m, loff_t *pos)
3133{
3134 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003135 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003136 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003137 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003138
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003139 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003140
3141 if (unlikely(ftrace_disabled))
3142 return NULL;
3143
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003144 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003145 * If an lseek was done, then reset and start from beginning.
3146 */
3147 if (*pos < iter->pos)
3148 reset_iter_read(iter);
3149
3150 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003151 * For set_ftrace_filter reading, if we have the filter
3152 * off, we can short cut and just print out that all
3153 * functions are enabled.
3154 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003155 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003156 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003157 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003158 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003159 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003160 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003161 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003162 /* reset in case of seek/pread */
3163 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003164 return iter;
3165 }
3166
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003167 if (iter->flags & FTRACE_ITER_HASH)
3168 return t_hash_start(m, pos);
3169
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003170 /*
3171 * Unfortunately, we need to restart at ftrace_pages_start
3172 * every time we let go of the ftrace_mutex. This is because
3173 * those pointers can change without the lock.
3174 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003175 iter->pg = ftrace_pages_start;
3176 iter->idx = 0;
3177 for (l = 0; l <= *pos; ) {
3178 p = t_next(m, p, &l);
3179 if (!p)
3180 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003181 }
walimis5821e1b2008-11-15 15:19:06 +08003182
Steven Rostedt69a30832011-12-19 15:21:16 -05003183 if (!p)
3184 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003185
3186 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003187}
3188
3189static void t_stop(struct seq_file *m, void *p)
3190{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003191 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003192}
3193
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003194void * __weak
3195arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3196{
3197 return NULL;
3198}
3199
3200static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3201 struct dyn_ftrace *rec)
3202{
3203 void *ptr;
3204
3205 ptr = arch_ftrace_trampoline_func(ops, rec);
3206 if (ptr)
3207 seq_printf(m, " ->%pS", ptr);
3208}
3209
Steven Rostedt5072c592008-05-12 21:20:43 +02003210static int t_show(struct seq_file *m, void *v)
3211{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003212 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003213 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003214
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003215 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003216 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003217
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003218 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003219 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003220 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003221 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003222 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003223 return 0;
3224 }
3225
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003226 rec = iter->func;
3227
Steven Rostedt5072c592008-05-12 21:20:43 +02003228 if (!rec)
3229 return 0;
3230
Steven Rostedt647bcd02011-05-03 14:39:21 -04003231 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003232 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003233 struct ftrace_ops *ops = NULL;
3234
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003235 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003236 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003237 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3238 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003239 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003240 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04003241 if (ops)
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003242 seq_printf(m, "\ttramp: %pS",
3243 (void *)ops->trampoline);
3244 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003245 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003246
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003247 }
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003248 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003249 }
3250
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003251 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003252
3253 return 0;
3254}
3255
James Morris88e9d342009-09-22 16:43:43 -07003256static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003257 .start = t_start,
3258 .next = t_next,
3259 .stop = t_stop,
3260 .show = t_show,
3261};
3262
Ingo Molnare309b412008-05-12 21:20:51 +02003263static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003264ftrace_avail_open(struct inode *inode, struct file *file)
3265{
3266 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003267
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003268 if (unlikely(ftrace_disabled))
3269 return -ENODEV;
3270
Jiri Olsa50e18b92012-04-25 10:23:39 +02003271 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3272 if (iter) {
3273 iter->pg = ftrace_pages_start;
3274 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003275 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003276
Jiri Olsa50e18b92012-04-25 10:23:39 +02003277 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003278}
3279
Steven Rostedt647bcd02011-05-03 14:39:21 -04003280static int
3281ftrace_enabled_open(struct inode *inode, struct file *file)
3282{
3283 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003284
Jiri Olsa50e18b92012-04-25 10:23:39 +02003285 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3286 if (iter) {
3287 iter->pg = ftrace_pages_start;
3288 iter->flags = FTRACE_ITER_ENABLED;
3289 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003290 }
3291
Jiri Olsa50e18b92012-04-25 10:23:39 +02003292 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003293}
3294
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003295/**
3296 * ftrace_regex_open - initialize function tracer filter files
3297 * @ops: The ftrace_ops that hold the hash filters
3298 * @flag: The type of filter to process
3299 * @inode: The inode, usually passed in to your open routine
3300 * @file: The file, usually passed in to your open routine
3301 *
3302 * ftrace_regex_open() initializes the filter files for the
3303 * @ops. Depending on @flag it may process the filter hash or
3304 * the notrace hash of @ops. With this called from the open
3305 * routine, you can use ftrace_filter_write() for the write
3306 * routine if @flag has FTRACE_ITER_FILTER set, or
3307 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003308 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003309 * release must call ftrace_regex_release().
3310 */
3311int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003312ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003313 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003314{
3315 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003316 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003317 int ret = 0;
3318
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003319 ftrace_ops_init(ops);
3320
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003321 if (unlikely(ftrace_disabled))
3322 return -ENODEV;
3323
Steven Rostedt5072c592008-05-12 21:20:43 +02003324 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3325 if (!iter)
3326 return -ENOMEM;
3327
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003328 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3329 kfree(iter);
3330 return -ENOMEM;
3331 }
3332
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003333 iter->ops = ops;
3334 iter->flags = flag;
3335
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003336 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003337
Steven Rostedtf45948e2011-05-02 12:29:25 -04003338 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003339 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003340 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003341 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003342
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003343 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003344 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3345
3346 if (file->f_flags & O_TRUNC)
3347 iter->hash = alloc_ftrace_hash(size_bits);
3348 else
3349 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3350
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003351 if (!iter->hash) {
3352 trace_parser_put(&iter->parser);
3353 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003354 ret = -ENOMEM;
3355 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003356 }
3357 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003358
Steven Rostedt5072c592008-05-12 21:20:43 +02003359 if (file->f_mode & FMODE_READ) {
3360 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003361
3362 ret = seq_open(file, &show_ftrace_seq_ops);
3363 if (!ret) {
3364 struct seq_file *m = file->private_data;
3365 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003366 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003367 /* Failed */
3368 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003369 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003370 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003371 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003372 } else
3373 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003374
3375 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003376 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003377
3378 return ret;
3379}
3380
Steven Rostedt41c52c02008-05-22 11:46:33 -04003381static int
3382ftrace_filter_open(struct inode *inode, struct file *file)
3383{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003384 struct ftrace_ops *ops = inode->i_private;
3385
3386 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003387 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3388 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003389}
3390
3391static int
3392ftrace_notrace_open(struct inode *inode, struct file *file)
3393{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003394 struct ftrace_ops *ops = inode->i_private;
3395
3396 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003397 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003398}
3399
Steven Rostedt64e7c442009-02-13 17:08:48 -05003400static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003401{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003402 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003403 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003404
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003405 switch (type) {
3406 case MATCH_FULL:
3407 if (strcmp(str, regex) == 0)
3408 matched = 1;
3409 break;
3410 case MATCH_FRONT_ONLY:
3411 if (strncmp(str, regex, len) == 0)
3412 matched = 1;
3413 break;
3414 case MATCH_MIDDLE_ONLY:
3415 if (strstr(str, regex))
3416 matched = 1;
3417 break;
3418 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003419 slen = strlen(str);
3420 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003421 matched = 1;
3422 break;
3423 }
3424
3425 return matched;
3426}
3427
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003428static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003429enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003430{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003431 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003432 int ret = 0;
3433
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003434 entry = ftrace_lookup_ip(hash, rec->ip);
3435 if (not) {
3436 /* Do nothing if it doesn't exist */
3437 if (!entry)
3438 return 0;
3439
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003440 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003441 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003442 /* Do nothing if it exists */
3443 if (entry)
3444 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003445
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003446 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003447 }
3448 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003449}
3450
Steven Rostedt64e7c442009-02-13 17:08:48 -05003451static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003452ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3453 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003454{
3455 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003456 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003457
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003458 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3459
3460 if (mod) {
3461 /* module lookup requires matching the module */
3462 if (!modname || strcmp(modname, mod))
3463 return 0;
3464
3465 /* blank search means to match all funcs in the mod */
3466 if (!len)
3467 return 1;
3468 }
3469
Steven Rostedt64e7c442009-02-13 17:08:48 -05003470 return ftrace_match(str, regex, len, type);
3471}
3472
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003473static int
3474match_records(struct ftrace_hash *hash, char *buff,
3475 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003476{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003477 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003478 struct ftrace_page *pg;
3479 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003480 int type = MATCH_FULL;
3481 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003482 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003483 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003484
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003485 if (len) {
3486 type = filter_parse_regex(buff, len, &search, &not);
3487 search_len = strlen(search);
3488 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003489
Steven Rostedt52baf112009-02-14 01:15:39 -05003490 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003491
3492 if (unlikely(ftrace_disabled))
3493 goto out_unlock;
3494
Steven Rostedt265c8312009-02-13 12:43:56 -05003495 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003496 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003497 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003498 if (ret < 0) {
3499 found = ret;
3500 goto out_unlock;
3501 }
Li Zefan311d16d2009-12-08 11:15:11 +08003502 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003503 }
3504 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003505 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003506 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003507
3508 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003509}
3510
Steven Rostedt64e7c442009-02-13 17:08:48 -05003511static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003512ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003513{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003514 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003515}
3516
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003517static int
3518ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003519{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003520 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003521
Steven Rostedt64e7c442009-02-13 17:08:48 -05003522 /* blank or '*' mean the same */
3523 if (strcmp(buff, "*") == 0)
3524 buff[0] = 0;
3525
3526 /* handle the case of 'dont filter this module' */
3527 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3528 buff[0] = 0;
3529 not = 1;
3530 }
3531
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003532 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003533}
3534
Steven Rostedtf6180772009-02-14 00:40:25 -05003535/*
3536 * We register the module command as a template to show others how
3537 * to register the a command as well.
3538 */
3539
3540static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003541ftrace_mod_callback(struct ftrace_hash *hash,
3542 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003543{
3544 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003545 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003546
3547 /*
3548 * cmd == 'mod' because we only registered this func
3549 * for the 'mod' ftrace_func_command.
3550 * But if you register one func with multiple commands,
3551 * you can tell which command was used by the cmd
3552 * parameter.
3553 */
3554
3555 /* we must have a module name */
3556 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003557 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003558
3559 mod = strsep(&param, ":");
3560 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003561 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003562
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003563 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003564 if (!ret)
3565 ret = -EINVAL;
3566 if (ret < 0)
3567 return ret;
3568
3569 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003570}
3571
3572static struct ftrace_func_command ftrace_mod_cmd = {
3573 .name = "mod",
3574 .func = ftrace_mod_callback,
3575};
3576
3577static int __init ftrace_mod_cmd_init(void)
3578{
3579 return register_ftrace_command(&ftrace_mod_cmd);
3580}
Steven Rostedt6f415672012-10-05 12:13:07 -04003581core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003582
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003583static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003584 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003585{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003586 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003587 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003588 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003589
3590 key = hash_long(ip, FTRACE_HASH_BITS);
3591
3592 hhd = &ftrace_func_hash[key];
3593
3594 if (hlist_empty(hhd))
3595 return;
3596
3597 /*
3598 * Disable preemption for these calls to prevent a RCU grace
3599 * period. This syncs the hash iteration and freeing of items
3600 * on the hash. rcu_read_lock is too dangerous here.
3601 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003602 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003603 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003604 if (entry->ip == ip)
3605 entry->ops->func(ip, parent_ip, &entry->data);
3606 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003607 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003608}
3609
Steven Rostedtb6887d72009-02-17 12:32:04 -05003610static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003611{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003612 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003613 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003614 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003615};
3616
Steven Rostedtb6887d72009-02-17 12:32:04 -05003617static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003618
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003619static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003620{
Steven Rostedtb8489142011-05-04 09:27:52 -04003621 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003622 int i;
3623
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003624 if (ftrace_probe_registered) {
3625 /* still need to update the function call sites */
3626 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003627 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3628 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003629 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003630 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003631
3632 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3633 struct hlist_head *hhd = &ftrace_func_hash[i];
3634 if (hhd->first)
3635 break;
3636 }
3637 /* Nothing registered? */
3638 if (i == FTRACE_FUNC_HASHSIZE)
3639 return;
3640
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003641 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003642
Steven Rostedtb6887d72009-02-17 12:32:04 -05003643 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003644}
3645
Steven Rostedtb6887d72009-02-17 12:32:04 -05003646static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003647{
3648 int i;
3649
Steven Rostedtb6887d72009-02-17 12:32:04 -05003650 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003651 return;
3652
3653 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3654 struct hlist_head *hhd = &ftrace_func_hash[i];
3655 if (hhd->first)
3656 return;
3657 }
3658
3659 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003660 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003661
Steven Rostedtb6887d72009-02-17 12:32:04 -05003662 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003663}
3664
3665
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003666static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003667{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003668 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003669 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003670 kfree(entry);
3671}
3672
Steven Rostedt59df055f2009-02-14 15:29:06 -05003673int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003674register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003675 void *data)
3676{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003677 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003678 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003679 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003680 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003681 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003682 struct ftrace_page *pg;
3683 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003684 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003685 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003686 int count = 0;
3687 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003688 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003689
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003690 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003691 len = strlen(search);
3692
Steven Rostedtb6887d72009-02-17 12:32:04 -05003693 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003694 if (WARN_ON(not))
3695 return -EINVAL;
3696
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003697 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003698
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003699 old_hash_ops.filter_hash = old_hash;
3700 /* Probes only have filters */
3701 old_hash_ops.notrace_hash = NULL;
3702
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003703 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003704 if (!hash) {
3705 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003706 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003707 }
3708
3709 if (unlikely(ftrace_disabled)) {
3710 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003711 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003712 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003713
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003714 mutex_lock(&ftrace_lock);
3715
Steven Rostedt59df055f2009-02-14 15:29:06 -05003716 do_for_each_ftrace_rec(pg, rec) {
3717
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003718 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003719 continue;
3720
3721 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3722 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003723 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003724 if (!count)
3725 count = -ENOMEM;
3726 goto out_unlock;
3727 }
3728
3729 count++;
3730
3731 entry->data = data;
3732
3733 /*
3734 * The caller might want to do something special
3735 * for each function we find. We call the callback
3736 * to give the caller an opportunity to do so.
3737 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003738 if (ops->init) {
3739 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003740 /* caller does not like this func */
3741 kfree(entry);
3742 continue;
3743 }
3744 }
3745
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003746 ret = enter_record(hash, rec, 0);
3747 if (ret < 0) {
3748 kfree(entry);
3749 count = ret;
3750 goto out_unlock;
3751 }
3752
Steven Rostedt59df055f2009-02-14 15:29:06 -05003753 entry->ops = ops;
3754 entry->ip = rec->ip;
3755
3756 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3757 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3758
3759 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003760
3761 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003762
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003763 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003764
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003765 if (!ret)
3766 free_ftrace_hash_rcu(old_hash);
3767 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003768 count = ret;
3769
Steven Rostedt59df055f2009-02-14 15:29:06 -05003770 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003771 mutex_unlock(&ftrace_lock);
3772 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003773 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003774 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003775
3776 return count;
3777}
3778
3779enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003780 PROBE_TEST_FUNC = 1,
3781 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003782};
3783
3784static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003785__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003786 void *data, int flags)
3787{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003788 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003789 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003790 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003791 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003792 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003793 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003794 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003795 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003796 char str[KSYM_SYMBOL_LEN];
3797 int type = MATCH_FULL;
3798 int i, len = 0;
3799 char *search;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003800 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003801
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003802 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003803 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003804 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003805 int not;
3806
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003807 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003808 len = strlen(search);
3809
Steven Rostedtb6887d72009-02-17 12:32:04 -05003810 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003811 if (WARN_ON(not))
3812 return;
3813 }
3814
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003815 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003816
3817 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3818 if (!hash)
3819 /* Hmm, should report this somehow */
3820 goto out_unlock;
3821
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003822 INIT_LIST_HEAD(&free_list);
3823
Steven Rostedt59df055f2009-02-14 15:29:06 -05003824 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3825 struct hlist_head *hhd = &ftrace_func_hash[i];
3826
Sasha Levinb67bfe02013-02-27 17:06:00 -08003827 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003828
3829 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003830 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003831 continue;
3832
Steven Rostedtb6887d72009-02-17 12:32:04 -05003833 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003834 continue;
3835
3836 /* do this last, since it is the most expensive */
3837 if (glob) {
3838 kallsyms_lookup(entry->ip, NULL, NULL,
3839 NULL, str);
3840 if (!ftrace_match(str, glob, len, type))
3841 continue;
3842 }
3843
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003844 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3845 /* It is possible more than one entry had this ip */
3846 if (rec_entry)
3847 free_hash_entry(hash, rec_entry);
3848
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003849 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003850 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003851 }
3852 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003853 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003854 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003855 /*
3856 * Remove after the disable is called. Otherwise, if the last
3857 * probe is removed, a null hash means *all enabled*.
3858 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003859 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003860 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003861 if (!ret)
3862 free_ftrace_hash_rcu(old_hash);
3863
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003864 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3865 list_del(&entry->free_list);
3866 ftrace_free_entry(entry);
3867 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003868 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003869
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003870 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003871 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003872 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003873}
3874
3875void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003876unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003877 void *data)
3878{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003879 __unregister_ftrace_function_probe(glob, ops, data,
3880 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003881}
3882
3883void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003884unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003885{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003886 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003887}
3888
Steven Rostedtb6887d72009-02-17 12:32:04 -05003889void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003890{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003891 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003892}
3893
Steven Rostedtf6180772009-02-14 00:40:25 -05003894static LIST_HEAD(ftrace_commands);
3895static DEFINE_MUTEX(ftrace_cmd_mutex);
3896
Tom Zanussi38de93a2013-10-24 08:34:18 -05003897/*
3898 * Currently we only register ftrace commands from __init, so mark this
3899 * __init too.
3900 */
3901__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003902{
3903 struct ftrace_func_command *p;
3904 int ret = 0;
3905
3906 mutex_lock(&ftrace_cmd_mutex);
3907 list_for_each_entry(p, &ftrace_commands, list) {
3908 if (strcmp(cmd->name, p->name) == 0) {
3909 ret = -EBUSY;
3910 goto out_unlock;
3911 }
3912 }
3913 list_add(&cmd->list, &ftrace_commands);
3914 out_unlock:
3915 mutex_unlock(&ftrace_cmd_mutex);
3916
3917 return ret;
3918}
3919
Tom Zanussi38de93a2013-10-24 08:34:18 -05003920/*
3921 * Currently we only unregister ftrace commands from __init, so mark
3922 * this __init too.
3923 */
3924__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003925{
3926 struct ftrace_func_command *p, *n;
3927 int ret = -ENODEV;
3928
3929 mutex_lock(&ftrace_cmd_mutex);
3930 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3931 if (strcmp(cmd->name, p->name) == 0) {
3932 ret = 0;
3933 list_del_init(&p->list);
3934 goto out_unlock;
3935 }
3936 }
3937 out_unlock:
3938 mutex_unlock(&ftrace_cmd_mutex);
3939
3940 return ret;
3941}
3942
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003943static int ftrace_process_regex(struct ftrace_hash *hash,
3944 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003945{
Steven Rostedtf6180772009-02-14 00:40:25 -05003946 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003947 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003948 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003949
3950 func = strsep(&next, ":");
3951
3952 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003953 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003954 if (!ret)
3955 ret = -EINVAL;
3956 if (ret < 0)
3957 return ret;
3958 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003959 }
3960
Steven Rostedtf6180772009-02-14 00:40:25 -05003961 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003962
3963 command = strsep(&next, ":");
3964
Steven Rostedtf6180772009-02-14 00:40:25 -05003965 mutex_lock(&ftrace_cmd_mutex);
3966 list_for_each_entry(p, &ftrace_commands, list) {
3967 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003968 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003969 goto out_unlock;
3970 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003971 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003972 out_unlock:
3973 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003974
Steven Rostedtf6180772009-02-14 00:40:25 -05003975 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003976}
3977
Ingo Molnare309b412008-05-12 21:20:51 +02003978static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003979ftrace_regex_write(struct file *file, const char __user *ubuf,
3980 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003981{
3982 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003983 struct trace_parser *parser;
3984 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003985
Li Zefan4ba79782009-09-22 13:52:20 +08003986 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003987 return 0;
3988
Steven Rostedt5072c592008-05-12 21:20:43 +02003989 if (file->f_mode & FMODE_READ) {
3990 struct seq_file *m = file->private_data;
3991 iter = m->private;
3992 } else
3993 iter = file->private_data;
3994
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003995 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003996 return -ENODEV;
3997
3998 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003999
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004000 parser = &iter->parser;
4001 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004002
Li Zefan4ba79782009-09-22 13:52:20 +08004003 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004004 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004005 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004006 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004007 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004008 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004009 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004010 }
4011
Steven Rostedt5072c592008-05-12 21:20:43 +02004012 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004013 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004014 return ret;
4015}
4016
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004017ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004018ftrace_filter_write(struct file *file, const char __user *ubuf,
4019 size_t cnt, loff_t *ppos)
4020{
4021 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4022}
4023
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004024ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004025ftrace_notrace_write(struct file *file, const char __user *ubuf,
4026 size_t cnt, loff_t *ppos)
4027{
4028 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4029}
4030
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004031static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004032ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4033{
4034 struct ftrace_func_entry *entry;
4035
4036 if (!ftrace_location(ip))
4037 return -EINVAL;
4038
4039 if (remove) {
4040 entry = ftrace_lookup_ip(hash, ip);
4041 if (!entry)
4042 return -ENOENT;
4043 free_hash_entry(hash, entry);
4044 return 0;
4045 }
4046
4047 return add_hash_entry(hash, ip);
4048}
4049
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004050static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004051 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004052{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004053 struct ftrace_ops *op;
4054
4055 if (!ftrace_enabled)
4056 return;
4057
4058 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004059 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004060 return;
4061 }
4062
4063 /*
4064 * If this is the shared global_ops filter, then we need to
4065 * check if there is another ops that shares it, is enabled.
4066 * If so, we still need to run the modify code.
4067 */
4068 if (ops->func_hash != &global_ops.local_hash)
4069 return;
4070
4071 do_for_each_ftrace_op(op, ftrace_ops_list) {
4072 if (op->func_hash == &global_ops.local_hash &&
4073 op->flags & FTRACE_OPS_FL_ENABLED) {
4074 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4075 /* Only need to do this once */
4076 return;
4077 }
4078 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004079}
4080
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004081static int
4082ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4083 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004084{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004085 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004086 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004087 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004088 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004089 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004090
Steven Rostedt41c52c02008-05-22 11:46:33 -04004091 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004092 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004093
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004094 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004095
Steven Rostedtf45948e2011-05-02 12:29:25 -04004096 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004097 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004098 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004099 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004100
Wang Nanb972cc52014-07-15 08:40:20 +08004101 if (reset)
4102 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4103 else
4104 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4105
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004106 if (!hash) {
4107 ret = -ENOMEM;
4108 goto out_regex_unlock;
4109 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004110
Jiri Olsaac483c42012-01-02 10:04:14 +01004111 if (buf && !ftrace_match_records(hash, buf, len)) {
4112 ret = -EINVAL;
4113 goto out_regex_unlock;
4114 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004115 if (ip) {
4116 ret = ftrace_match_addr(hash, ip, remove);
4117 if (ret < 0)
4118 goto out_regex_unlock;
4119 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004120
4121 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004122 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004123 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4124 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004125 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004126 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004127 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004128 free_ftrace_hash_rcu(old_hash);
4129 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004130 mutex_unlock(&ftrace_lock);
4131
Jiri Olsaac483c42012-01-02 10:04:14 +01004132 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004133 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004134
4135 free_ftrace_hash(hash);
4136 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004137}
4138
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004139static int
4140ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4141 int reset, int enable)
4142{
4143 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4144}
4145
4146/**
4147 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4148 * @ops - the ops to set the filter with
4149 * @ip - the address to add to or remove from the filter.
4150 * @remove - non zero to remove the ip from the filter
4151 * @reset - non zero to reset all filters before applying this filter.
4152 *
4153 * Filters denote which functions should be enabled when tracing is enabled
4154 * If @ip is NULL, it failes to update filter.
4155 */
4156int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4157 int remove, int reset)
4158{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004159 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004160 return ftrace_set_addr(ops, ip, remove, reset, 1);
4161}
4162EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4163
4164static int
4165ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4166 int reset, int enable)
4167{
4168 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4169}
4170
Steven Rostedt77a2b372008-05-12 21:20:45 +02004171/**
4172 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004173 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004174 * @buf - the string that holds the function filter text.
4175 * @len - the length of the string.
4176 * @reset - non zero to reset all filters before applying this filter.
4177 *
4178 * Filters denote which functions should be enabled when tracing is enabled.
4179 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4180 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004181int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004182 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004183{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004184 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004185 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004186}
Steven Rostedt936e0742011-05-05 22:54:01 -04004187EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004188
Steven Rostedt41c52c02008-05-22 11:46:33 -04004189/**
4190 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004191 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004192 * @buf - the string that holds the function notrace text.
4193 * @len - the length of the string.
4194 * @reset - non zero to reset all filters before applying this filter.
4195 *
4196 * Notrace Filters denote which functions should not be enabled when tracing
4197 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4198 * for tracing.
4199 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004200int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004201 int len, int reset)
4202{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004203 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004204 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004205}
4206EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4207/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004208 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004209 * @buf - the string that holds the function filter text.
4210 * @len - the length of the string.
4211 * @reset - non zero to reset all filters before applying this filter.
4212 *
4213 * Filters denote which functions should be enabled when tracing is enabled.
4214 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4215 */
4216void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4217{
4218 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4219}
4220EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4221
4222/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004223 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004224 * @buf - the string that holds the function notrace text.
4225 * @len - the length of the string.
4226 * @reset - non zero to reset all filters before applying this filter.
4227 *
4228 * Notrace Filters denote which functions should not be enabled when tracing
4229 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4230 * for tracing.
4231 */
4232void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004233{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004234 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004235}
Steven Rostedt936e0742011-05-05 22:54:01 -04004236EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004237
Steven Rostedt2af15d62009-05-28 13:37:24 -04004238/*
4239 * command line interface to allow users to set filters on boot up.
4240 */
4241#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4242static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4243static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4244
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004245/* Used by function selftest to not test if filter is set */
4246bool ftrace_filter_param __initdata;
4247
Steven Rostedt2af15d62009-05-28 13:37:24 -04004248static int __init set_ftrace_notrace(char *str)
4249{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004250 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004251 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004252 return 1;
4253}
4254__setup("ftrace_notrace=", set_ftrace_notrace);
4255
4256static int __init set_ftrace_filter(char *str)
4257{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004258 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004259 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004260 return 1;
4261}
4262__setup("ftrace_filter=", set_ftrace_filter);
4263
Stefan Assmann369bc182009-10-12 22:17:21 +02004264#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004265static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004266static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004267static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004268
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004269static unsigned long save_global_trampoline;
4270static unsigned long save_global_flags;
4271
Stefan Assmann369bc182009-10-12 22:17:21 +02004272static int __init set_graph_function(char *str)
4273{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004274 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004275 return 1;
4276}
4277__setup("ftrace_graph_filter=", set_graph_function);
4278
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004279static int __init set_graph_notrace_function(char *str)
4280{
4281 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4282 return 1;
4283}
4284__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4285
4286static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004287{
4288 int ret;
4289 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004290 unsigned long *table = ftrace_graph_funcs;
4291 int *count = &ftrace_graph_count;
4292
4293 if (!enable) {
4294 table = ftrace_graph_notrace_funcs;
4295 count = &ftrace_graph_notrace_count;
4296 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004297
4298 while (buf) {
4299 func = strsep(&buf, ",");
4300 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004301 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004302 if (ret)
4303 printk(KERN_DEBUG "ftrace: function %s not "
4304 "traceable\n", func);
4305 }
4306}
4307#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4308
Steven Rostedt2a85a372011-12-19 21:57:44 -05004309void __init
4310ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004311{
4312 char *func;
4313
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004314 ftrace_ops_init(ops);
4315
Steven Rostedt2af15d62009-05-28 13:37:24 -04004316 while (buf) {
4317 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004318 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004319 }
4320}
4321
4322static void __init set_ftrace_early_filters(void)
4323{
4324 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004325 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004326 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004327 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004328#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4329 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004330 set_ftrace_early_graph(ftrace_graph_buf, 1);
4331 if (ftrace_graph_notrace_buf[0])
4332 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004333#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004334}
4335
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004336int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004337{
4338 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004339 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004340 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004341 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004342 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004343 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004344 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004345 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004346
Steven Rostedt5072c592008-05-12 21:20:43 +02004347 if (file->f_mode & FMODE_READ) {
4348 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004349 seq_release(inode, file);
4350 } else
4351 iter = file->private_data;
4352
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004353 parser = &iter->parser;
4354 if (trace_parser_loaded(parser)) {
4355 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004356 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004357 }
4358
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004359 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004360
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004361 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004362
Steven Rostedt058e2972011-04-29 22:35:33 -04004363 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004364 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4365
4366 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004367 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004368 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004369 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004370
Steven Rostedt058e2972011-04-29 22:35:33 -04004371 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004372 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004373 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4374 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004375 ret = ftrace_hash_move(iter->ops, filter_hash,
4376 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004377 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004378 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004379 free_ftrace_hash_rcu(old_hash);
4380 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004381 mutex_unlock(&ftrace_lock);
4382 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004383
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004384 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004385 free_ftrace_hash(iter->hash);
4386 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004387
Steven Rostedt5072c592008-05-12 21:20:43 +02004388 return 0;
4389}
4390
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004391static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004392 .open = ftrace_avail_open,
4393 .read = seq_read,
4394 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004395 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004396};
4397
Steven Rostedt647bcd02011-05-03 14:39:21 -04004398static const struct file_operations ftrace_enabled_fops = {
4399 .open = ftrace_enabled_open,
4400 .read = seq_read,
4401 .llseek = seq_lseek,
4402 .release = seq_release_private,
4403};
4404
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004405static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004406 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004407 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004408 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004409 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004410 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004411};
4412
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004413static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004414 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004415 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004416 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004417 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004418 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004419};
4420
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004421#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4422
4423static DEFINE_MUTEX(graph_lock);
4424
4425int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004426int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004427unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004428unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004429
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004430struct ftrace_graph_data {
4431 unsigned long *table;
4432 size_t size;
4433 int *count;
4434 const struct seq_operations *seq_ops;
4435};
4436
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004437static void *
Li Zefan85951842009-06-24 09:54:00 +08004438__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004439{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004440 struct ftrace_graph_data *fgd = m->private;
4441
4442 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004443 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004444 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004445}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004446
Li Zefan85951842009-06-24 09:54:00 +08004447static void *
4448g_next(struct seq_file *m, void *v, loff_t *pos)
4449{
4450 (*pos)++;
4451 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004452}
4453
4454static void *g_start(struct seq_file *m, loff_t *pos)
4455{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004456 struct ftrace_graph_data *fgd = m->private;
4457
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004458 mutex_lock(&graph_lock);
4459
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004460 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004461 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004462 return (void *)1;
4463
Li Zefan85951842009-06-24 09:54:00 +08004464 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004465}
4466
4467static void g_stop(struct seq_file *m, void *p)
4468{
4469 mutex_unlock(&graph_lock);
4470}
4471
4472static int g_show(struct seq_file *m, void *v)
4473{
4474 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004475
4476 if (!ptr)
4477 return 0;
4478
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004479 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004480 struct ftrace_graph_data *fgd = m->private;
4481
4482 if (fgd->table == ftrace_graph_funcs)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004483 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004484 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004485 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004486 return 0;
4487 }
4488
Steven Rostedtb375a112009-09-17 00:05:58 -04004489 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004490
4491 return 0;
4492}
4493
James Morris88e9d342009-09-22 16:43:43 -07004494static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004495 .start = g_start,
4496 .next = g_next,
4497 .stop = g_stop,
4498 .show = g_show,
4499};
4500
4501static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004502__ftrace_graph_open(struct inode *inode, struct file *file,
4503 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004504{
4505 int ret = 0;
4506
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004507 mutex_lock(&graph_lock);
4508 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004509 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004510 *fgd->count = 0;
4511 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004512 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004513 mutex_unlock(&graph_lock);
4514
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004515 if (file->f_mode & FMODE_READ) {
4516 ret = seq_open(file, fgd->seq_ops);
4517 if (!ret) {
4518 struct seq_file *m = file->private_data;
4519 m->private = fgd;
4520 }
4521 } else
4522 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004523
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004524 return ret;
4525}
4526
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004527static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004528ftrace_graph_open(struct inode *inode, struct file *file)
4529{
4530 struct ftrace_graph_data *fgd;
4531
4532 if (unlikely(ftrace_disabled))
4533 return -ENODEV;
4534
4535 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4536 if (fgd == NULL)
4537 return -ENOMEM;
4538
4539 fgd->table = ftrace_graph_funcs;
4540 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4541 fgd->count = &ftrace_graph_count;
4542 fgd->seq_ops = &ftrace_graph_seq_ops;
4543
4544 return __ftrace_graph_open(inode, file, fgd);
4545}
4546
4547static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004548ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4549{
4550 struct ftrace_graph_data *fgd;
4551
4552 if (unlikely(ftrace_disabled))
4553 return -ENODEV;
4554
4555 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4556 if (fgd == NULL)
4557 return -ENOMEM;
4558
4559 fgd->table = ftrace_graph_notrace_funcs;
4560 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4561 fgd->count = &ftrace_graph_notrace_count;
4562 fgd->seq_ops = &ftrace_graph_seq_ops;
4563
4564 return __ftrace_graph_open(inode, file, fgd);
4565}
4566
4567static int
Li Zefan87827112009-07-23 11:29:11 +08004568ftrace_graph_release(struct inode *inode, struct file *file)
4569{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004570 if (file->f_mode & FMODE_READ) {
4571 struct seq_file *m = file->private_data;
4572
4573 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004574 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004575 } else {
4576 kfree(file->private_data);
4577 }
4578
Li Zefan87827112009-07-23 11:29:11 +08004579 return 0;
4580}
4581
4582static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004583ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004584{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004585 struct dyn_ftrace *rec;
4586 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004587 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004588 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004589 int type, not;
4590 char *search;
4591 bool exists;
4592 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004593
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004594 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004595 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004596 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004597 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004598
4599 search_len = strlen(search);
4600
Steven Rostedt52baf112009-02-14 01:15:39 -05004601 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004602
4603 if (unlikely(ftrace_disabled)) {
4604 mutex_unlock(&ftrace_lock);
4605 return -ENODEV;
4606 }
4607
Steven Rostedt265c8312009-02-13 12:43:56 -05004608 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004609
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004610 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004611 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004612 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004613 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004614 if (array[i] == rec->ip) {
4615 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004616 break;
4617 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004618 }
4619
4620 if (!not) {
4621 fail = 0;
4622 if (!exists) {
4623 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004624 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004625 goto out;
4626 }
4627 } else {
4628 if (exists) {
4629 array[i] = array[--(*idx)];
4630 array[*idx] = 0;
4631 fail = 0;
4632 }
4633 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004634 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004635 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004636out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004637 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004638
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004639 if (fail)
4640 return -EINVAL;
4641
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004642 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004643}
4644
4645static ssize_t
4646ftrace_graph_write(struct file *file, const char __user *ubuf,
4647 size_t cnt, loff_t *ppos)
4648{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004649 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004650 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004651 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004652
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004653 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004654 return 0;
4655
Namhyung Kim6a101082013-10-14 17:24:25 +09004656 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4657 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004658
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004659 read = trace_get_user(&parser, ubuf, cnt, ppos);
4660
Li Zefan4ba79782009-09-22 13:52:20 +08004661 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004662 parser.buffer[parser.idx] = 0;
4663
Namhyung Kim6a101082013-10-14 17:24:25 +09004664 mutex_lock(&graph_lock);
4665
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004666 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004667 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4668 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004669
4670 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004671 }
4672
Namhyung Kim6a101082013-10-14 17:24:25 +09004673 if (!ret)
4674 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004675
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004676 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004677
4678 return ret;
4679}
4680
4681static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004682 .open = ftrace_graph_open,
4683 .read = seq_read,
4684 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004685 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004686 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004687};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004688
4689static const struct file_operations ftrace_graph_notrace_fops = {
4690 .open = ftrace_graph_notrace_open,
4691 .read = seq_read,
4692 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004693 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004694 .release = ftrace_graph_release,
4695};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004696#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4697
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004698void ftrace_create_filter_files(struct ftrace_ops *ops,
4699 struct dentry *parent)
4700{
4701
4702 trace_create_file("set_ftrace_filter", 0644, parent,
4703 ops, &ftrace_filter_fops);
4704
4705 trace_create_file("set_ftrace_notrace", 0644, parent,
4706 ops, &ftrace_notrace_fops);
4707}
4708
4709/*
4710 * The name "destroy_filter_files" is really a misnomer. Although
4711 * in the future, it may actualy delete the files, but this is
4712 * really intended to make sure the ops passed in are disabled
4713 * and that when this function returns, the caller is free to
4714 * free the ops.
4715 *
4716 * The "destroy" name is only to match the "create" name that this
4717 * should be paired with.
4718 */
4719void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4720{
4721 mutex_lock(&ftrace_lock);
4722 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4723 ftrace_shutdown(ops, 0);
4724 ops->flags |= FTRACE_OPS_FL_DELETED;
4725 mutex_unlock(&ftrace_lock);
4726}
4727
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05004728static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004729{
Steven Rostedt5072c592008-05-12 21:20:43 +02004730
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004731 trace_create_file("available_filter_functions", 0444,
4732 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004733
Steven Rostedt647bcd02011-05-03 14:39:21 -04004734 trace_create_file("enabled_functions", 0444,
4735 d_tracer, NULL, &ftrace_enabled_fops);
4736
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004737 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004738
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004739#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004740 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004741 NULL,
4742 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004743 trace_create_file("set_graph_notrace", 0444, d_tracer,
4744 NULL,
4745 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004746#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4747
Steven Rostedt5072c592008-05-12 21:20:43 +02004748 return 0;
4749}
4750
Steven Rostedt9fd49322012-04-24 22:32:06 -04004751static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004752{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004753 const unsigned long *ipa = a;
4754 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004755
Steven Rostedt9fd49322012-04-24 22:32:06 -04004756 if (*ipa > *ipb)
4757 return 1;
4758 if (*ipa < *ipb)
4759 return -1;
4760 return 0;
4761}
4762
4763static void ftrace_swap_ips(void *a, void *b, int size)
4764{
4765 unsigned long *ipa = a;
4766 unsigned long *ipb = b;
4767 unsigned long t;
4768
4769 t = *ipa;
4770 *ipa = *ipb;
4771 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004772}
4773
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004774static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004775 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004776 unsigned long *end)
4777{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004778 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004779 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004780 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004781 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004782 unsigned long *p;
4783 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004784 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004785 int ret = -ENOMEM;
4786
4787 count = end - start;
4788
4789 if (!count)
4790 return 0;
4791
Steven Rostedt9fd49322012-04-24 22:32:06 -04004792 sort(start, count, sizeof(*start),
4793 ftrace_cmp_ips, ftrace_swap_ips);
4794
Steven Rostedt706c81f2012-04-24 23:45:26 -04004795 start_pg = ftrace_allocate_pages(count);
4796 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004797 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004798
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004799 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004800
Steven Rostedt320823092011-12-16 14:42:37 -05004801 /*
4802 * Core and each module needs their own pages, as
4803 * modules will free them when they are removed.
4804 * Force a new page to be allocated for modules.
4805 */
Steven Rostedta7900872011-12-16 16:23:44 -05004806 if (!mod) {
4807 WARN_ON(ftrace_pages || ftrace_pages_start);
4808 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004809 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004810 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004811 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004812 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004813
Steven Rostedta7900872011-12-16 16:23:44 -05004814 if (WARN_ON(ftrace_pages->next)) {
4815 /* Hmm, we have free pages? */
4816 while (ftrace_pages->next)
4817 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004818 }
Steven Rostedta7900872011-12-16 16:23:44 -05004819
Steven Rostedt706c81f2012-04-24 23:45:26 -04004820 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004821 }
4822
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004823 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004824 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004825 while (p < end) {
4826 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004827 /*
4828 * Some architecture linkers will pad between
4829 * the different mcount_loc sections of different
4830 * object files to satisfy alignments.
4831 * Skip any NULL pointers.
4832 */
4833 if (!addr)
4834 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004835
4836 if (pg->index == pg->size) {
4837 /* We should have allocated enough */
4838 if (WARN_ON(!pg->next))
4839 break;
4840 pg = pg->next;
4841 }
4842
4843 rec = &pg->records[pg->index++];
4844 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004845 }
4846
Steven Rostedt706c81f2012-04-24 23:45:26 -04004847 /* We should have used all pages */
4848 WARN_ON(pg->next);
4849
4850 /* Assign the last page to ftrace_pages */
4851 ftrace_pages = pg;
4852
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004853 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004854 * We only need to disable interrupts on start up
4855 * because we are modifying code that an interrupt
4856 * may execute, and the modification is not atomic.
4857 * But for modules, nothing runs the code we modify
4858 * until we are finished with it, and there's no
4859 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004860 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004861 if (!mod)
4862 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004863 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004864 if (!mod)
4865 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004866 ret = 0;
4867 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004868 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004869
Steven Rostedta7900872011-12-16 16:23:44 -05004870 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004871}
4872
Steven Rostedt93eb6772009-04-15 13:24:06 -04004873#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004874
4875#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4876
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004877void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004878{
4879 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004880 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004881 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004882 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004883
Steven Rostedt93eb6772009-04-15 13:24:06 -04004884 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004885
4886 if (ftrace_disabled)
4887 goto out_unlock;
4888
Steven Rostedt320823092011-12-16 14:42:37 -05004889 /*
4890 * Each module has its own ftrace_pages, remove
4891 * them from the list.
4892 */
4893 last_pg = &ftrace_pages_start;
4894 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4895 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004896 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004897 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004898 * As core pages are first, the first
4899 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004900 */
Steven Rostedt320823092011-12-16 14:42:37 -05004901 if (WARN_ON(pg == ftrace_pages_start))
4902 goto out_unlock;
4903
4904 /* Check if we are deleting the last page */
4905 if (pg == ftrace_pages)
4906 ftrace_pages = next_to_ftrace_page(last_pg);
4907
4908 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004909 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4910 free_pages((unsigned long)pg->records, order);
4911 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004912 } else
4913 last_pg = &pg->next;
4914 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004915 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004916 mutex_unlock(&ftrace_lock);
4917}
4918
4919static void ftrace_init_module(struct module *mod,
4920 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004921{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004922 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004923 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004924 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004925}
4926
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004927void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004928{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004929 ftrace_init_module(mod, mod->ftrace_callsites,
4930 mod->ftrace_callsites +
4931 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004932}
4933
4934static int ftrace_module_notify_exit(struct notifier_block *self,
4935 unsigned long val, void *data)
4936{
4937 struct module *mod = data;
4938
4939 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004940 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004941
4942 return 0;
4943}
4944#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004945static int ftrace_module_notify_exit(struct notifier_block *self,
4946 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004947{
4948 return 0;
4949}
4950#endif /* CONFIG_MODULES */
4951
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004952struct notifier_block ftrace_module_exit_nb = {
4953 .notifier_call = ftrace_module_notify_exit,
4954 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4955};
4956
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004957void __init ftrace_init(void)
4958{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004959 extern unsigned long __start_mcount_loc[];
4960 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004961 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004962 int ret;
4963
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004964 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004965 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004966 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004967 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004968 goto failed;
4969
4970 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004971 if (!count) {
4972 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004973 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004974 }
4975
4976 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4977 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004978
4979 last_ftrace_enabled = ftrace_enabled = 1;
4980
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004981 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004982 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004983 __stop_mcount_loc);
4984
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004985 ret = register_module_notifier(&ftrace_module_exit_nb);
4986 if (ret)
4987 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004988
Steven Rostedt2af15d62009-05-28 13:37:24 -04004989 set_ftrace_early_filters();
4990
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004991 return;
4992 failed:
4993 ftrace_disabled = 1;
4994}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004995
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004996/* Do nothing if arch does not support this */
4997void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
4998{
4999}
5000
5001static void ftrace_update_trampoline(struct ftrace_ops *ops)
5002{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005003
5004/*
5005 * Currently there's no safe way to free a trampoline when the kernel
5006 * is configured with PREEMPT. That is because a task could be preempted
5007 * when it jumped to the trampoline, it may be preempted for a long time
5008 * depending on the system load, and currently there's no way to know
5009 * when it will be off the trampoline. If the trampoline is freed
5010 * too early, when the task runs again, it will be executing on freed
5011 * memory and crash.
5012 */
5013#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005014 /* Currently, only non dynamic ops can have a trampoline */
5015 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5016 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005017#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005018
5019 arch_ftrace_update_trampoline(ops);
5020}
5021
Steven Rostedt3d083392008-05-12 21:20:42 +02005022#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005023
Steven Rostedt2b499382011-05-03 22:49:52 -04005024static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005025 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005026 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005027};
5028
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005029static int __init ftrace_nodyn_init(void)
5030{
5031 ftrace_enabled = 1;
5032 return 0;
5033}
Steven Rostedt6f415672012-10-05 12:13:07 -04005034core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005035
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005036static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005037static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005038static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005039/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005040# define ftrace_startup(ops, command) \
5041 ({ \
5042 int ___ret = __register_ftrace_function(ops); \
5043 if (!___ret) \
5044 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5045 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005046 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005047# define ftrace_shutdown(ops, command) \
5048 ({ \
5049 int ___ret = __unregister_ftrace_function(ops); \
5050 if (!___ret) \
5051 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5052 ___ret; \
5053 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005054
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005055# define ftrace_startup_sysctl() do { } while (0)
5056# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005057
5058static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005059ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005060{
5061 return 1;
5062}
5063
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005064static void ftrace_update_trampoline(struct ftrace_ops *ops)
5065{
5066}
5067
Steven Rostedt3d083392008-05-12 21:20:42 +02005068#endif /* CONFIG_DYNAMIC_FTRACE */
5069
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005070__init void ftrace_init_global_array_ops(struct trace_array *tr)
5071{
5072 tr->ops = &global_ops;
5073 tr->ops->private = tr;
5074}
5075
5076void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5077{
5078 /* If we filter on pids, update to use the pid function */
5079 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5080 if (WARN_ON(tr->ops->func != ftrace_stub))
5081 printk("ftrace ops had %pS for function\n",
5082 tr->ops->func);
5083 /* Only the top level instance does pid tracing */
5084 if (!list_empty(&ftrace_pids)) {
5085 set_ftrace_pid_function(func);
5086 func = ftrace_pid_func;
5087 }
5088 }
5089 tr->ops->func = func;
5090 tr->ops->private = tr;
5091}
5092
5093void ftrace_reset_array_ops(struct trace_array *tr)
5094{
5095 tr->ops->func = ftrace_stub;
5096}
5097
Steven Rostedtb8489142011-05-04 09:27:52 -04005098static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005099ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005100 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01005101{
Jiri Olsae2484912012-02-15 15:51:48 +01005102 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
5103 return;
5104
5105 /*
5106 * Some of the ops may be dynamically allocated,
5107 * they must be freed after a synchronize_sched().
5108 */
5109 preempt_disable_notrace();
5110 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05005111
5112 /*
5113 * Control funcs (perf) uses RCU. Only trace if
5114 * RCU is currently active.
5115 */
5116 if (!rcu_is_watching())
5117 goto out;
5118
Steven Rostedt0a016402012-11-02 17:03:03 -04005119 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04005120 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
5121 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005122 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04005123 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04005124 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05005125 out:
Jiri Olsae2484912012-02-15 15:51:48 +01005126 trace_recursion_clear(TRACE_CONTROL_BIT);
5127 preempt_enable_notrace();
5128}
5129
5130static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005131 .func = ftrace_ops_control_func,
5132 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005133 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01005134};
5135
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005136static inline void
5137__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005138 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005139{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005140 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005141 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005142
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005143 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5144 if (bit < 0)
5145 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005146
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005147 /*
5148 * Some of the ops may be dynamically allocated,
5149 * they must be freed after a synchronize_sched().
5150 */
5151 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04005152 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005153 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005154 if (FTRACE_WARN_ON(!op->func)) {
5155 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005156 goto out;
5157 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005158 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005159 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005160 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005161out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005162 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005163 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005164}
5165
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005166/*
5167 * Some archs only support passing ip and parent_ip. Even though
5168 * the list function ignores the op parameter, we do not want any
5169 * C side effects, where a function is called without the caller
5170 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005171 * Archs are to support both the regs and ftrace_ops at the same time.
5172 * If they support ftrace_ops, it is assumed they support regs.
5173 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005174 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5175 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005176 * An architecture can pass partial regs with ftrace_ops and still
5177 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005178 */
5179#if ARCH_SUPPORTS_FTRACE_OPS
5180static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005181 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005182{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005183 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005184}
5185#else
5186static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5187{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005188 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005189}
5190#endif
5191
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005192/*
5193 * If there's only one function registered but it does not support
5194 * recursion, this function will be called by the mcount trampoline.
5195 * This function will handle recursion protection.
5196 */
5197static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
5198 struct ftrace_ops *op, struct pt_regs *regs)
5199{
5200 int bit;
5201
5202 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5203 if (bit < 0)
5204 return;
5205
5206 op->func(ip, parent_ip, op, regs);
5207
5208 trace_clear_recursion(bit);
5209}
5210
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005211/**
5212 * ftrace_ops_get_func - get the function a trampoline should call
5213 * @ops: the ops to get the function for
5214 *
5215 * Normally the mcount trampoline will call the ops->func, but there
5216 * are times that it should not. For example, if the ops does not
5217 * have its own recursion protection, then it should call the
5218 * ftrace_ops_recurs_func() instead.
5219 *
5220 * Returns the function that the trampoline should call for @ops.
5221 */
5222ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5223{
5224 /*
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005225 * If the func handles its own recursion, call it directly.
5226 * Otherwise call the recursion protected function that
5227 * will call the ftrace ops function.
5228 */
5229 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
5230 return ftrace_ops_recurs_func;
5231
5232 return ops->func;
5233}
5234
Steven Rostedte32d8952008-12-04 00:26:41 -05005235static void clear_ftrace_swapper(void)
5236{
5237 struct task_struct *p;
5238 int cpu;
5239
5240 get_online_cpus();
5241 for_each_online_cpu(cpu) {
5242 p = idle_task(cpu);
5243 clear_tsk_trace_trace(p);
5244 }
5245 put_online_cpus();
5246}
5247
5248static void set_ftrace_swapper(void)
5249{
5250 struct task_struct *p;
5251 int cpu;
5252
5253 get_online_cpus();
5254 for_each_online_cpu(cpu) {
5255 p = idle_task(cpu);
5256 set_tsk_trace_trace(p);
5257 }
5258 put_online_cpus();
5259}
5260
5261static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005262{
5263 struct task_struct *p;
5264
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005265 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05005266 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05005267 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05005268 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005269 rcu_read_unlock();
5270
Steven Rostedte32d8952008-12-04 00:26:41 -05005271 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005272}
5273
Steven Rostedte32d8952008-12-04 00:26:41 -05005274static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005275{
5276 struct task_struct *p;
5277
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005278 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005279 do_each_pid_task(pid, PIDTYPE_PID, p) {
5280 set_tsk_trace_trace(p);
5281 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005282 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005283}
5284
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005285static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005286{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005287 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005288 clear_ftrace_swapper();
5289 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005290 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05005291}
5292
5293static void set_ftrace_pid_task(struct pid *pid)
5294{
5295 if (pid == ftrace_swapper_pid)
5296 set_ftrace_swapper();
5297 else
5298 set_ftrace_pid(pid);
5299}
5300
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005301static int ftrace_pid_add(int p)
5302{
5303 struct pid *pid;
5304 struct ftrace_pid *fpid;
5305 int ret = -EINVAL;
5306
5307 mutex_lock(&ftrace_lock);
5308
5309 if (!p)
5310 pid = ftrace_swapper_pid;
5311 else
5312 pid = find_get_pid(p);
5313
5314 if (!pid)
5315 goto out;
5316
5317 ret = 0;
5318
5319 list_for_each_entry(fpid, &ftrace_pids, list)
5320 if (fpid->pid == pid)
5321 goto out_put;
5322
5323 ret = -ENOMEM;
5324
5325 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5326 if (!fpid)
5327 goto out_put;
5328
5329 list_add(&fpid->list, &ftrace_pids);
5330 fpid->pid = pid;
5331
5332 set_ftrace_pid_task(pid);
5333
5334 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005335
5336 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005337
5338 mutex_unlock(&ftrace_lock);
5339 return 0;
5340
5341out_put:
5342 if (pid != ftrace_swapper_pid)
5343 put_pid(pid);
5344
5345out:
5346 mutex_unlock(&ftrace_lock);
5347 return ret;
5348}
5349
5350static void ftrace_pid_reset(void)
5351{
5352 struct ftrace_pid *fpid, *safe;
5353
5354 mutex_lock(&ftrace_lock);
5355 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5356 struct pid *pid = fpid->pid;
5357
5358 clear_ftrace_pid_task(pid);
5359
5360 list_del(&fpid->list);
5361 kfree(fpid);
5362 }
5363
5364 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005365 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005366
5367 mutex_unlock(&ftrace_lock);
5368}
5369
5370static void *fpid_start(struct seq_file *m, loff_t *pos)
5371{
5372 mutex_lock(&ftrace_lock);
5373
5374 if (list_empty(&ftrace_pids) && (!*pos))
5375 return (void *) 1;
5376
5377 return seq_list_start(&ftrace_pids, *pos);
5378}
5379
5380static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5381{
5382 if (v == (void *)1)
5383 return NULL;
5384
5385 return seq_list_next(v, &ftrace_pids, pos);
5386}
5387
5388static void fpid_stop(struct seq_file *m, void *p)
5389{
5390 mutex_unlock(&ftrace_lock);
5391}
5392
5393static int fpid_show(struct seq_file *m, void *v)
5394{
5395 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5396
5397 if (v == (void *)1) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005398 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005399 return 0;
5400 }
5401
5402 if (fpid->pid == ftrace_swapper_pid)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005403 seq_puts(m, "swapper tasks\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005404 else
5405 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5406
5407 return 0;
5408}
5409
5410static const struct seq_operations ftrace_pid_sops = {
5411 .start = fpid_start,
5412 .next = fpid_next,
5413 .stop = fpid_stop,
5414 .show = fpid_show,
5415};
5416
5417static int
5418ftrace_pid_open(struct inode *inode, struct file *file)
5419{
5420 int ret = 0;
5421
5422 if ((file->f_mode & FMODE_WRITE) &&
5423 (file->f_flags & O_TRUNC))
5424 ftrace_pid_reset();
5425
5426 if (file->f_mode & FMODE_READ)
5427 ret = seq_open(file, &ftrace_pid_sops);
5428
5429 return ret;
5430}
5431
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005432static ssize_t
5433ftrace_pid_write(struct file *filp, const char __user *ubuf,
5434 size_t cnt, loff_t *ppos)
5435{
Ingo Molnar457dc922009-11-23 11:03:28 +01005436 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005437 long val;
5438 int ret;
5439
5440 if (cnt >= sizeof(buf))
5441 return -EINVAL;
5442
5443 if (copy_from_user(&buf, ubuf, cnt))
5444 return -EFAULT;
5445
5446 buf[cnt] = 0;
5447
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005448 /*
5449 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5450 * to clean the filter quietly.
5451 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005452 tmp = strstrip(buf);
5453 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005454 return 1;
5455
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005456 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005457 if (ret < 0)
5458 return ret;
5459
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005460 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005461
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005462 return ret ? ret : cnt;
5463}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005464
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005465static int
5466ftrace_pid_release(struct inode *inode, struct file *file)
5467{
5468 if (file->f_mode & FMODE_READ)
5469 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005470
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005471 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005472}
5473
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005474static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005475 .open = ftrace_pid_open,
5476 .write = ftrace_pid_write,
5477 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005478 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005479 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005480};
5481
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005482static __init int ftrace_init_tracefs(void)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005483{
5484 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005485
5486 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05005487 if (IS_ERR(d_tracer))
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005488 return 0;
5489
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005490 ftrace_init_dyn_tracefs(d_tracer);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005491
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005492 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5493 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005494
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005495 ftrace_profile_tracefs(d_tracer);
Steven Rostedt493762f2009-03-23 17:12:36 -04005496
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005497 return 0;
5498}
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005499fs_initcall(ftrace_init_tracefs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005500
Steven Rostedt3d083392008-05-12 21:20:42 +02005501/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005502 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005503 *
5504 * This function should be used by panic code. It stops ftrace
5505 * but in a not so nice way. If you need to simply kill ftrace
5506 * from a non-atomic section, use ftrace_kill.
5507 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005508void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005509{
5510 ftrace_disabled = 1;
5511 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005512 clear_ftrace_function();
5513}
5514
5515/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005516 * Test if ftrace is dead or not.
5517 */
5518int ftrace_is_dead(void)
5519{
5520 return ftrace_disabled;
5521}
5522
5523/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005524 * register_ftrace_function - register a function for profiling
5525 * @ops - ops structure that holds the function for profiling.
5526 *
5527 * Register a function to be called by all functions in the
5528 * kernel.
5529 *
5530 * Note: @ops->func and all the functions it calls must be labeled
5531 * with "notrace", otherwise it will go into a
5532 * recursive loop.
5533 */
5534int register_ftrace_function(struct ftrace_ops *ops)
5535{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005536 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005537
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005538 ftrace_ops_init(ops);
5539
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005540 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005541
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005542 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005543
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005544 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005545
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005546 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005547}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005548EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005549
5550/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005551 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005552 * @ops - ops structure that holds the function to unregister
5553 *
5554 * Unregister a function that was added to be called by ftrace profiling.
5555 */
5556int unregister_ftrace_function(struct ftrace_ops *ops)
5557{
5558 int ret;
5559
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005560 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005561 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005562 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005563
5564 return ret;
5565}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005566EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005567
Ingo Molnare309b412008-05-12 21:20:51 +02005568int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005569ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005570 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005571 loff_t *ppos)
5572{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005573 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005574
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005575 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005576
Steven Rostedt45a4a232011-04-21 23:16:46 -04005577 if (unlikely(ftrace_disabled))
5578 goto out;
5579
5580 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005581
Li Zefana32c7762009-06-26 16:55:51 +08005582 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005583 goto out;
5584
Li Zefana32c7762009-06-26 16:55:51 +08005585 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005586
5587 if (ftrace_enabled) {
5588
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005589 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005590 if (ftrace_ops_list != &ftrace_list_end)
5591 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005592
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05005593 ftrace_startup_sysctl();
5594
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005595 } else {
5596 /* stopping ftrace calls (just send to ftrace_stub) */
5597 ftrace_trace_function = ftrace_stub;
5598
5599 ftrace_shutdown_sysctl();
5600 }
5601
5602 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005603 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005604 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005605}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005606
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005607#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005608
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005609static struct ftrace_ops graph_ops = {
5610 .func = ftrace_stub,
5611 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5612 FTRACE_OPS_FL_INITIALIZED |
5613 FTRACE_OPS_FL_STUB,
5614#ifdef FTRACE_GRAPH_TRAMP_ADDR
5615 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05005616 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005617#endif
5618 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5619};
5620
Steven Rostedte49dc192008-12-02 23:50:05 -05005621int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5622{
5623 return 0;
5624}
5625
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005626/* The callbacks that hook a function */
5627trace_func_graph_ret_t ftrace_graph_return =
5628 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005629trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005630static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005631
5632/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5633static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5634{
5635 int i;
5636 int ret = 0;
5637 unsigned long flags;
5638 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5639 struct task_struct *g, *t;
5640
5641 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5642 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5643 * sizeof(struct ftrace_ret_stack),
5644 GFP_KERNEL);
5645 if (!ret_stack_list[i]) {
5646 start = 0;
5647 end = i;
5648 ret = -ENOMEM;
5649 goto free;
5650 }
5651 }
5652
5653 read_lock_irqsave(&tasklist_lock, flags);
5654 do_each_thread(g, t) {
5655 if (start == end) {
5656 ret = -EAGAIN;
5657 goto unlock;
5658 }
5659
5660 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005661 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005662 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005663 t->curr_ret_stack = -1;
5664 /* Make sure the tasks see the -1 first: */
5665 smp_wmb();
5666 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005667 }
5668 } while_each_thread(g, t);
5669
5670unlock:
5671 read_unlock_irqrestore(&tasklist_lock, flags);
5672free:
5673 for (i = start; i < end; i++)
5674 kfree(ret_stack_list[i]);
5675 return ret;
5676}
5677
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005678static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005679ftrace_graph_probe_sched_switch(void *ignore,
5680 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005681{
5682 unsigned long long timestamp;
5683 int index;
5684
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005685 /*
5686 * Does the user want to count the time a function was asleep.
5687 * If so, do not update the time stamps.
5688 */
5689 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5690 return;
5691
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005692 timestamp = trace_clock_local();
5693
5694 prev->ftrace_timestamp = timestamp;
5695
5696 /* only process tasks that we timestamped */
5697 if (!next->ftrace_timestamp)
5698 return;
5699
5700 /*
5701 * Update all the counters in next to make up for the
5702 * time next was sleeping.
5703 */
5704 timestamp -= next->ftrace_timestamp;
5705
5706 for (index = next->curr_ret_stack; index >= 0; index--)
5707 next->ret_stack[index].calltime += timestamp;
5708}
5709
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005710/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005711static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005712{
5713 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005714 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005715
5716 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5717 sizeof(struct ftrace_ret_stack *),
5718 GFP_KERNEL);
5719
5720 if (!ret_stack_list)
5721 return -ENOMEM;
5722
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005723 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005724 for_each_online_cpu(cpu) {
5725 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005726 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005727 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005728
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005729 do {
5730 ret = alloc_retstack_tasklist(ret_stack_list);
5731 } while (ret == -EAGAIN);
5732
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005733 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005734 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005735 if (ret)
5736 pr_info("ftrace_graph: Couldn't activate tracepoint"
5737 " probe to kernel_sched_switch\n");
5738 }
5739
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005740 kfree(ret_stack_list);
5741 return ret;
5742}
5743
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005744/*
5745 * Hibernation protection.
5746 * The state of the current task is too much unstable during
5747 * suspend/restore to disk. We want to protect against that.
5748 */
5749static int
5750ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5751 void *unused)
5752{
5753 switch (state) {
5754 case PM_HIBERNATION_PREPARE:
5755 pause_graph_tracing();
5756 break;
5757
5758 case PM_POST_HIBERNATION:
5759 unpause_graph_tracing();
5760 break;
5761 }
5762 return NOTIFY_DONE;
5763}
5764
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005765static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5766{
5767 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5768 return 0;
5769 return __ftrace_graph_entry(trace);
5770}
5771
5772/*
5773 * The function graph tracer should only trace the functions defined
5774 * by set_ftrace_filter and set_ftrace_notrace. If another function
5775 * tracer ops is registered, the graph tracer requires testing the
5776 * function against the global ops, and not just trace any function
5777 * that any ftrace_ops registered.
5778 */
5779static void update_function_graph_func(void)
5780{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005781 struct ftrace_ops *op;
5782 bool do_test = false;
5783
5784 /*
5785 * The graph and global ops share the same set of functions
5786 * to test. If any other ops is on the list, then
5787 * the graph tracing needs to test if its the function
5788 * it should call.
5789 */
5790 do_for_each_ftrace_op(op, ftrace_ops_list) {
5791 if (op != &global_ops && op != &graph_ops &&
5792 op != &ftrace_list_end) {
5793 do_test = true;
5794 /* in double loop, break out with goto */
5795 goto out;
5796 }
5797 } while_for_each_ftrace_op(op);
5798 out:
5799 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005800 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005801 else
5802 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005803}
5804
Mathias Krause8275f692014-03-30 15:31:50 +02005805static struct notifier_block ftrace_suspend_notifier = {
5806 .notifier_call = ftrace_suspend_notifier_call,
5807};
5808
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005809int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5810 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005811{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005812 int ret = 0;
5813
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005814 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005815
Steven Rostedt05ce5812009-03-24 00:18:31 -04005816 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005817 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005818 ret = -EBUSY;
5819 goto out;
5820 }
5821
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005822 register_pm_notifier(&ftrace_suspend_notifier);
5823
Steven Rostedt597af812009-04-03 15:24:12 -04005824 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005825 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005826 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005827 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005828 goto out;
5829 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005830
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005831 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005832
5833 /*
5834 * Update the indirect function to the entryfunc, and the
5835 * function that gets called to the entry_test first. Then
5836 * call the update fgraph entry function to determine if
5837 * the entryfunc should be called directly or not.
5838 */
5839 __ftrace_graph_entry = entryfunc;
5840 ftrace_graph_entry = ftrace_graph_entry_test;
5841 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005842
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005843 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005844out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005845 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005846 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005847}
5848
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005849void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005850{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005851 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005852
Steven Rostedt597af812009-04-03 15:24:12 -04005853 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005854 goto out;
5855
Steven Rostedt597af812009-04-03 15:24:12 -04005856 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005857 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005858 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005859 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005860 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005861 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005862 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005863
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005864#ifdef CONFIG_DYNAMIC_FTRACE
5865 /*
5866 * Function graph does not allocate the trampoline, but
5867 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5868 * if one was used.
5869 */
5870 global_ops.trampoline = save_global_trampoline;
5871 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5872 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5873#endif
5874
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005875 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005876 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005877}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005878
Steven Rostedt868baf02011-02-10 21:26:13 -05005879static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5880
5881static void
5882graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5883{
5884 atomic_set(&t->tracing_graph_pause, 0);
5885 atomic_set(&t->trace_overrun, 0);
5886 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005887 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005888 smp_wmb();
5889 t->ret_stack = ret_stack;
5890}
5891
5892/*
5893 * Allocate a return stack for the idle task. May be the first
5894 * time through, or it may be done by CPU hotplug online.
5895 */
5896void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5897{
5898 t->curr_ret_stack = -1;
5899 /*
5900 * The idle task has no parent, it either has its own
5901 * stack or no stack at all.
5902 */
5903 if (t->ret_stack)
5904 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5905
5906 if (ftrace_graph_active) {
5907 struct ftrace_ret_stack *ret_stack;
5908
5909 ret_stack = per_cpu(idle_ret_stack, cpu);
5910 if (!ret_stack) {
5911 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5912 * sizeof(struct ftrace_ret_stack),
5913 GFP_KERNEL);
5914 if (!ret_stack)
5915 return;
5916 per_cpu(idle_ret_stack, cpu) = ret_stack;
5917 }
5918 graph_init_task(t, ret_stack);
5919 }
5920}
5921
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005922/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005923void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005924{
Steven Rostedt84047e32009-06-02 16:51:55 -04005925 /* Make sure we do not use the parent ret_stack */
5926 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005927 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005928
Steven Rostedt597af812009-04-03 15:24:12 -04005929 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005930 struct ftrace_ret_stack *ret_stack;
5931
5932 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005933 * sizeof(struct ftrace_ret_stack),
5934 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005935 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005936 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005937 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005938 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005939}
5940
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005941void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005942{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005943 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5944
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005945 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005946 /* NULL must become visible to IRQs before we free it: */
5947 barrier();
5948
5949 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005950}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005951#endif