blob: 02004ae918608f915b28341b6ed1b3b7f551c5a5 [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>
Ingo Molnar29930022017-02-08 18:51:36 +010018#include <linux/sched/task.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020019#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020020#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080021#include <linux/suspend.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050022#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020023#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010024#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020025#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050026#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040027#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010028#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020029#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020031#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050032#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020033#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050034#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080035#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020036
Steven Rostedtad8d75f2009-04-14 19:39:12 -040037#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040038
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -040039#include <asm/sections.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040040#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053041
Steven Rostedt0706f1c2009-03-23 23:12:58 -040042#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040043#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020044
Steven Rostedt6912896e2008-10-23 09:33:03 -040045#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040046 ({ \
47 int ___r = cond; \
48 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040049 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040050 ___r; \
51 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040052
53#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040054 ({ \
55 int ___r = cond; \
56 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040057 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040058 ___r; \
59 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040060
Steven Rostedt8fc0c702009-02-16 15:28:00 -050061/* hash bits for specific function selection */
62#define FTRACE_HASH_BITS 7
63#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040064#define FTRACE_HASH_DEFAULT_BITS 10
65#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050066
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
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040094static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040095{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040096 struct trace_array *tr;
97
98 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
99 return false;
100
101 tr = ops->private;
102
103 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400104}
105
106static void ftrace_update_trampoline(struct ftrace_ops *ops);
107
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200108/*
109 * ftrace_disabled is set when an anomaly is discovered.
110 * ftrace_disabled is much stronger than ftrace_enabled.
111 */
112static int ftrace_disabled __read_mostly;
113
Steven Rostedt52baf112009-02-14 01:15:39 -0500114static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200115
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800116static struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400118static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200119
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400120#if ARCH_SUPPORTS_FTRACE_OPS
121static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400122 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400123#else
124/* See comment below, where ftrace_ops_list_func is defined */
125static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
126#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
127#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400128
Steven Rostedt0a016402012-11-02 17:03:03 -0400129/*
130 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400131 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400132 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400133 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400134 * concurrent insertions into the ftrace_global_list.
135 *
136 * Silly Alpha and silly pointer-speculation compiler optimizations!
137 */
138#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400139 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400140 do
141
142/*
143 * Optimized for just a single item in the list (as that is the normal case).
144 */
145#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400146 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400147 unlikely((op) != &ftrace_list_end))
148
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900149static inline void ftrace_ops_init(struct ftrace_ops *ops)
150{
151#ifdef CONFIG_DYNAMIC_FTRACE
152 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400153 mutex_init(&ops->local_hash.regex_lock);
154 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900155 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
156 }
157#endif
158}
159
Steven Rostedtea701f12012-07-20 13:08:05 -0400160/**
161 * ftrace_nr_registered_ops - return number of ops registered
162 *
163 * Returns the number of ftrace_ops registered and tracing functions
164 */
165int ftrace_nr_registered_ops(void)
166{
167 struct ftrace_ops *ops;
168 int cnt = 0;
169
170 mutex_lock(&ftrace_lock);
171
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800172 for (ops = rcu_dereference_protected(ftrace_ops_list,
173 lockdep_is_held(&ftrace_lock));
174 ops != &ftrace_list_end;
175 ops = rcu_dereference_protected(ops->next,
176 lockdep_is_held(&ftrace_lock)))
Steven Rostedtea701f12012-07-20 13:08:05 -0400177 cnt++;
178
179 mutex_unlock(&ftrace_lock);
180
181 return cnt;
182}
183
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400184static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400185 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500186{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400187 struct trace_array *tr = op->private;
188
189 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500190 return;
191
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400192 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500193}
194
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200195/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200196 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200198 * This NULLs the ftrace function and in essence stops
199 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200200 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200201void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200202{
Steven Rostedt3d083392008-05-12 21:20:42 +0200203 ftrace_trace_function = ftrace_stub;
204}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200205
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500206static void per_cpu_ops_disable_all(struct ftrace_ops *ops)
Jiri Olsae2484912012-02-15 15:51:48 +0100207{
208 int cpu;
209
210 for_each_possible_cpu(cpu)
211 *per_cpu_ptr(ops->disabled, cpu) = 1;
212}
213
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500214static int per_cpu_ops_alloc(struct ftrace_ops *ops)
Jiri Olsae2484912012-02-15 15:51:48 +0100215{
216 int __percpu *disabled;
217
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500218 if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
219 return -EINVAL;
220
Jiri Olsae2484912012-02-15 15:51:48 +0100221 disabled = alloc_percpu(int);
222 if (!disabled)
223 return -ENOMEM;
224
225 ops->disabled = disabled;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500226 per_cpu_ops_disable_all(ops);
Jiri Olsae2484912012-02-15 15:51:48 +0100227 return 0;
228}
229
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500230static void ftrace_sync(struct work_struct *work)
231{
232 /*
233 * This function is just a stub to implement a hard force
234 * of synchronize_sched(). This requires synchronizing
235 * tasks even in userspace and idle.
236 *
237 * Yes, function tracing is rude.
238 */
239}
240
241static void ftrace_sync_ipi(void *data)
242{
243 /* Probably not needed, but do it anyway */
244 smp_rmb();
245}
246
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500247#ifdef CONFIG_FUNCTION_GRAPH_TRACER
248static void update_function_graph_func(void);
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400249
250/* Both enabled by default (can be cleared by function_graph tracer flags */
251static bool fgraph_sleep_time = true;
252static bool fgraph_graph_time = true;
253
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500254#else
255static inline void update_function_graph_func(void) { }
256#endif
257
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100258
259static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
260{
261 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500262 * If this is a dynamic, RCU, or per CPU ops, or we force list func,
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100263 * then it needs to call the list anyway.
264 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500265 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU |
266 FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100267 return ftrace_ops_list_func;
268
269 return ftrace_ops_get_func(ops);
270}
271
Steven Rostedt2b499382011-05-03 22:49:52 -0400272static void update_ftrace_function(void)
273{
274 ftrace_func_t func;
275
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400276 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400277 * Prepare the ftrace_ops that the arch callback will use.
278 * If there's only one ftrace_ops registered, the ftrace_ops_list
279 * will point to the ops we want.
280 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800281 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
282 lockdep_is_held(&ftrace_lock));
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400283
284 /* If there's no ftrace_ops registered, just call the stub function */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800285 if (set_function_trace_op == &ftrace_list_end) {
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400286 func = ftrace_stub;
287
288 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400289 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400290 * recursion safe and not dynamic and the arch supports passing ops,
291 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400292 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800293 } else if (rcu_dereference_protected(ftrace_ops_list->next,
294 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100295 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400296
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400297 } else {
298 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500299 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400300 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400301 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400302
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400303 update_function_graph_func();
304
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500305 /* If there's no change, then do nothing more here */
306 if (ftrace_trace_function == func)
307 return;
308
309 /*
310 * If we are using the list function, it doesn't care
311 * about the function_trace_ops.
312 */
313 if (func == ftrace_ops_list_func) {
314 ftrace_trace_function = func;
315 /*
316 * Don't even bother setting function_trace_ops,
317 * it would be racy to do so anyway.
318 */
319 return;
320 }
321
322#ifndef CONFIG_DYNAMIC_FTRACE
323 /*
324 * For static tracing, we need to be a bit more careful.
325 * The function change takes affect immediately. Thus,
326 * we need to coorditate the setting of the function_trace_ops
327 * with the setting of the ftrace_trace_function.
328 *
329 * Set the function to the list ops, which will call the
330 * function we want, albeit indirectly, but it handles the
331 * ftrace_ops and doesn't depend on function_trace_op.
332 */
333 ftrace_trace_function = ftrace_ops_list_func;
334 /*
335 * Make sure all CPUs see this. Yes this is slow, but static
336 * tracing is slow and nasty to have enabled.
337 */
338 schedule_on_each_cpu(ftrace_sync);
339 /* Now all cpus are using the list ops. */
340 function_trace_op = set_function_trace_op;
341 /* Make sure the function_trace_op is visible on all CPUs */
342 smp_wmb();
343 /* Nasty way to force a rmb on all cpus */
344 smp_call_function(ftrace_sync_ipi, NULL, 1);
345 /* OK, we are all set to update the ftrace_trace_function now! */
346#endif /* !CONFIG_DYNAMIC_FTRACE */
347
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400348 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400349}
350
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800351int using_ftrace_ops_list_func(void)
352{
353 return ftrace_trace_function == ftrace_ops_list_func;
354}
355
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800356static void add_ftrace_ops(struct ftrace_ops __rcu **list,
357 struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200358{
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800359 rcu_assign_pointer(ops->next, *list);
360
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200361 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400362 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200363 * CPU might be walking that list. We need to make sure
364 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400365 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200366 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400367 rcu_assign_pointer(*list, ops);
368}
Steven Rostedt3d083392008-05-12 21:20:42 +0200369
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800370static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
371 struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400372{
373 struct ftrace_ops **p;
374
375 /*
376 * If we are removing the last function, then simply point
377 * to the ftrace_stub.
378 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800379 if (rcu_dereference_protected(*list,
380 lockdep_is_held(&ftrace_lock)) == ops &&
381 rcu_dereference_protected(ops->next,
382 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt2b499382011-05-03 22:49:52 -0400383 *list = &ftrace_list_end;
384 return 0;
385 }
386
387 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
388 if (*p == ops)
389 break;
390
391 if (*p != ops)
392 return -1;
393
394 *p = (*p)->next;
395 return 0;
396}
397
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400398static void ftrace_update_trampoline(struct ftrace_ops *ops);
399
Steven Rostedt2b499382011-05-03 22:49:52 -0400400static int __register_ftrace_function(struct ftrace_ops *ops)
401{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500402 if (ops->flags & FTRACE_OPS_FL_DELETED)
403 return -EINVAL;
404
Steven Rostedtb8489142011-05-04 09:27:52 -0400405 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
406 return -EBUSY;
407
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900408#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400409 /*
410 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
411 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
412 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
413 */
414 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
415 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
416 return -EINVAL;
417
418 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
419 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
420#endif
421
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400422 if (!core_kernel_data((unsigned long)ops))
423 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
424
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500425 if (ops->flags & FTRACE_OPS_FL_PER_CPU) {
426 if (per_cpu_ops_alloc(ops))
Jiri Olsae2484912012-02-15 15:51:48 +0100427 return -ENOMEM;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500428 }
429
430 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400431
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400432 /* Always save the function, and reset at unregistering */
433 ops->saved_func = ops->func;
434
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400435 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400436 ops->func = ftrace_pid_func;
437
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400438 ftrace_update_trampoline(ops);
439
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400440 if (ftrace_enabled)
441 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200442
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200443 return 0;
444}
445
Ingo Molnare309b412008-05-12 21:20:51 +0200446static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200447{
Steven Rostedt2b499382011-05-03 22:49:52 -0400448 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200449
Steven Rostedtb8489142011-05-04 09:27:52 -0400450 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
451 return -EBUSY;
452
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500453 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400454
Steven Rostedt2b499382011-05-03 22:49:52 -0400455 if (ret < 0)
456 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400457
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400458 if (ftrace_enabled)
459 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200460
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400461 ops->func = ops->saved_func;
462
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500463 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200464}
465
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500466static void ftrace_update_pid_func(void)
467{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400468 struct ftrace_ops *op;
469
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400470 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500471 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900472 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500473
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400474 do_for_each_ftrace_op(op, ftrace_ops_list) {
475 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400476 op->func = ftrace_pids_enabled(op) ?
477 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400478 ftrace_update_trampoline(op);
479 }
480 } while_for_each_ftrace_op(op);
481
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400482 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500483}
484
Steven Rostedt493762f2009-03-23 17:12:36 -0400485#ifdef CONFIG_FUNCTION_PROFILER
486struct ftrace_profile {
487 struct hlist_node node;
488 unsigned long ip;
489 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400490#ifdef CONFIG_FUNCTION_GRAPH_TRACER
491 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400492 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400493#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400494};
495
496struct ftrace_profile_page {
497 struct ftrace_profile_page *next;
498 unsigned long index;
499 struct ftrace_profile records[];
500};
501
Steven Rostedtcafb1682009-03-24 20:50:39 -0400502struct ftrace_profile_stat {
503 atomic_t disabled;
504 struct hlist_head *hash;
505 struct ftrace_profile_page *pages;
506 struct ftrace_profile_page *start;
507 struct tracer_stat stat;
508};
509
Steven Rostedt493762f2009-03-23 17:12:36 -0400510#define PROFILE_RECORDS_SIZE \
511 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
512
513#define PROFILES_PER_PAGE \
514 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
515
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400516static int ftrace_profile_enabled __read_mostly;
517
518/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400519static DEFINE_MUTEX(ftrace_profile_lock);
520
Steven Rostedtcafb1682009-03-24 20:50:39 -0400521static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400522
Namhyung Kim20079eb2013-04-10 08:55:50 +0900523#define FTRACE_PROFILE_HASH_BITS 10
524#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400525
Steven Rostedt493762f2009-03-23 17:12:36 -0400526static void *
527function_stat_next(void *v, int idx)
528{
529 struct ftrace_profile *rec = v;
530 struct ftrace_profile_page *pg;
531
532 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
533
534 again:
Li Zefan0296e422009-06-26 11:15:37 +0800535 if (idx != 0)
536 rec++;
537
Steven Rostedt493762f2009-03-23 17:12:36 -0400538 if ((void *)rec >= (void *)&pg->records[pg->index]) {
539 pg = pg->next;
540 if (!pg)
541 return NULL;
542 rec = &pg->records[0];
543 if (!rec->counter)
544 goto again;
545 }
546
547 return rec;
548}
549
550static void *function_stat_start(struct tracer_stat *trace)
551{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400552 struct ftrace_profile_stat *stat =
553 container_of(trace, struct ftrace_profile_stat, stat);
554
555 if (!stat || !stat->start)
556 return NULL;
557
558 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400559}
560
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400561#ifdef CONFIG_FUNCTION_GRAPH_TRACER
562/* function graph compares on total time */
563static int function_stat_cmp(void *p1, void *p2)
564{
565 struct ftrace_profile *a = p1;
566 struct ftrace_profile *b = p2;
567
568 if (a->time < b->time)
569 return -1;
570 if (a->time > b->time)
571 return 1;
572 else
573 return 0;
574}
575#else
576/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400577static int function_stat_cmp(void *p1, void *p2)
578{
579 struct ftrace_profile *a = p1;
580 struct ftrace_profile *b = p2;
581
582 if (a->counter < b->counter)
583 return -1;
584 if (a->counter > b->counter)
585 return 1;
586 else
587 return 0;
588}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400589#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400590
591static int function_stat_headers(struct seq_file *m)
592{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400593#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100594 seq_puts(m, " Function "
595 "Hit Time Avg s^2\n"
596 " -------- "
597 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400598#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100599 seq_puts(m, " Function Hit\n"
600 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400601#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400602 return 0;
603}
604
605static int function_stat_show(struct seq_file *m, void *v)
606{
607 struct ftrace_profile *rec = v;
608 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800609 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400610#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400611 static struct trace_seq s;
612 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400613 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400614#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800615 mutex_lock(&ftrace_profile_lock);
616
617 /* we raced with function_profile_reset() */
618 if (unlikely(rec->counter == 0)) {
619 ret = -EBUSY;
620 goto out;
621 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400622
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530623#ifdef CONFIG_FUNCTION_GRAPH_TRACER
624 avg = rec->time;
625 do_div(avg, rec->counter);
626 if (tracing_thresh && (avg < tracing_thresh))
627 goto out;
628#endif
629
Steven Rostedt493762f2009-03-23 17:12:36 -0400630 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400631 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400632
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400633#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100634 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400635
Chase Douglase330b3b2010-04-26 14:02:05 -0400636 /* Sample standard deviation (s^2) */
637 if (rec->counter <= 1)
638 stddev = 0;
639 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200640 /*
641 * Apply Welford's method:
642 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
643 */
644 stddev = rec->counter * rec->time_squared -
645 rec->time * rec->time;
646
Chase Douglase330b3b2010-04-26 14:02:05 -0400647 /*
648 * Divide only 1000 for ns^2 -> us^2 conversion.
649 * trace_print_graph_duration will divide 1000 again.
650 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200651 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400652 }
653
Steven Rostedt34886c82009-03-25 21:00:47 -0400654 trace_seq_init(&s);
655 trace_print_graph_duration(rec->time, &s);
656 trace_seq_puts(&s, " ");
657 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400658 trace_seq_puts(&s, " ");
659 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400660 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400661#endif
662 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800663out:
664 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400665
Li Zefan3aaba202010-08-23 16:50:12 +0800666 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400667}
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400670{
671 struct ftrace_profile_page *pg;
672
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400674
675 while (pg) {
676 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
677 pg->index = 0;
678 pg = pg->next;
679 }
680
Steven Rostedtcafb1682009-03-24 20:50:39 -0400681 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400682 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
683}
684
Steven Rostedtcafb1682009-03-24 20:50:39 -0400685int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400686{
687 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400688 int functions;
689 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400690 int i;
691
692 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400693 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400694 return 0;
695
Steven Rostedtcafb1682009-03-24 20:50:39 -0400696 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
697 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400698 return -ENOMEM;
699
Steven Rostedt318e0a72009-03-25 20:06:34 -0400700#ifdef CONFIG_DYNAMIC_FTRACE
701 functions = ftrace_update_tot_cnt;
702#else
703 /*
704 * We do not know the number of functions that exist because
705 * dynamic tracing is what counts them. With past experience
706 * we have around 20K functions. That should be more than enough.
707 * It is highly unlikely we will execute every function in
708 * the kernel.
709 */
710 functions = 20000;
711#endif
712
Steven Rostedtcafb1682009-03-24 20:50:39 -0400713 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400714
Steven Rostedt318e0a72009-03-25 20:06:34 -0400715 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
716
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900717 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400718 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400719 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400720 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400721 pg = pg->next;
722 }
723
724 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400725
726 out_free:
727 pg = stat->start;
728 while (pg) {
729 unsigned long tmp = (unsigned long)pg;
730
731 pg = pg->next;
732 free_page(tmp);
733 }
734
Steven Rostedt318e0a72009-03-25 20:06:34 -0400735 stat->pages = NULL;
736 stat->start = NULL;
737
738 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400739}
740
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400742{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400743 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 int size;
745
Steven Rostedtcafb1682009-03-24 20:50:39 -0400746 stat = &per_cpu(ftrace_profile_stats, cpu);
747
748 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400751 return 0;
752 }
753
754 /*
755 * We are profiling all functions, but usually only a few thousand
756 * functions are hit. We'll make a hash of 1024 items.
757 */
758 size = FTRACE_PROFILE_HASH_SIZE;
759
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400761
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400763 return -ENOMEM;
764
Steven Rostedt318e0a72009-03-25 20:06:34 -0400765 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400766 if (ftrace_profile_pages_init(stat) < 0) {
767 kfree(stat->hash);
768 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400769 return -ENOMEM;
770 }
771
772 return 0;
773}
774
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775static int ftrace_profile_init(void)
776{
777 int cpu;
778 int ret = 0;
779
Miao Xiec4602c12013-12-16 15:20:01 +0800780 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400781 ret = ftrace_profile_init_cpu(cpu);
782 if (ret)
783 break;
784 }
785
786 return ret;
787}
788
Steven Rostedt493762f2009-03-23 17:12:36 -0400789/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790static struct ftrace_profile *
791ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400792{
793 struct ftrace_profile *rec;
794 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400795 unsigned long key;
796
Namhyung Kim20079eb2013-04-10 08:55:50 +0900797 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400799
800 if (hlist_empty(hhd))
801 return NULL;
802
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400803 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400804 if (rec->ip == ip)
805 return rec;
806 }
807
808 return NULL;
809}
810
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811static void ftrace_add_profile(struct ftrace_profile_stat *stat,
812 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400813{
814 unsigned long key;
815
Namhyung Kim20079eb2013-04-10 08:55:50 +0900816 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400818}
819
Steven Rostedt318e0a72009-03-25 20:06:34 -0400820/*
821 * The memory is already allocated, this simply finds a new record to use.
822 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400823static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400824ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400825{
826 struct ftrace_profile *rec = NULL;
827
Steven Rostedt318e0a72009-03-25 20:06:34 -0400828 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400829 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 goto out;
831
Steven Rostedt493762f2009-03-23 17:12:36 -0400832 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400833 * Try to find the function again since an NMI
834 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400836 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400837 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400838 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400839
Steven Rostedtcafb1682009-03-24 20:50:39 -0400840 if (stat->pages->index == PROFILES_PER_PAGE) {
841 if (!stat->pages->next)
842 goto out;
843 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400844 }
845
Steven Rostedtcafb1682009-03-24 20:50:39 -0400846 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400847 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400849
Steven Rostedt493762f2009-03-23 17:12:36 -0400850 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400851 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400852
853 return rec;
854}
855
Steven Rostedt493762f2009-03-23 17:12:36 -0400856static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400857function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400858 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400859{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400860 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400861 struct ftrace_profile *rec;
862 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400863
864 if (!ftrace_profile_enabled)
865 return;
866
Steven Rostedt493762f2009-03-23 17:12:36 -0400867 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400868
Christoph Lameterbdffd892014-04-29 14:17:40 -0500869 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400870 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400871 goto out;
872
873 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400874 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400875 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400876 if (!rec)
877 goto out;
878 }
879
880 rec->counter++;
881 out:
882 local_irq_restore(flags);
883}
884
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400885#ifdef CONFIG_FUNCTION_GRAPH_TRACER
886static int profile_graph_entry(struct ftrace_graph_ent *trace)
887{
Namhyung Kim8861dd32016-08-31 11:55:29 +0900888 int index = trace->depth;
889
Steven Rostedta1e2e312011-08-09 12:50:46 -0400890 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900891
892 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
893 current->ret_stack[index].subtime = 0;
894
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400895 return 1;
896}
897
898static void profile_graph_return(struct ftrace_graph_ret *trace)
899{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400901 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400902 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400903 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400904
905 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500906 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400907 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400908 goto out;
909
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400910 /* If the calltime was zero'd ignore it */
911 if (!trace->calltime)
912 goto out;
913
Steven Rostedta2a16d62009-03-24 23:17:58 -0400914 calltime = trace->rettime - trace->calltime;
915
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400916 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400917 int index;
918
919 index = trace->depth;
920
921 /* Append this call time to the parent time to subtract */
922 if (index)
923 current->ret_stack[index - 1].subtime += calltime;
924
925 if (current->ret_stack[index].subtime < calltime)
926 calltime -= current->ret_stack[index].subtime;
927 else
928 calltime = 0;
929 }
930
Steven Rostedtcafb1682009-03-24 20:50:39 -0400931 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400932 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400933 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400934 rec->time_squared += calltime * calltime;
935 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400936
Steven Rostedtcafb1682009-03-24 20:50:39 -0400937 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400938 local_irq_restore(flags);
939}
940
941static int register_ftrace_profiler(void)
942{
943 return register_ftrace_graph(&profile_graph_return,
944 &profile_graph_entry);
945}
946
947static void unregister_ftrace_profiler(void)
948{
949 unregister_ftrace_graph();
950}
951#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100952static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400953 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900954 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400955 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400956};
957
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400958static int register_ftrace_profiler(void)
959{
960 return register_ftrace_function(&ftrace_profile_ops);
961}
962
963static void unregister_ftrace_profiler(void)
964{
965 unregister_ftrace_function(&ftrace_profile_ops);
966}
967#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
968
Steven Rostedt493762f2009-03-23 17:12:36 -0400969static ssize_t
970ftrace_profile_write(struct file *filp, const char __user *ubuf,
971 size_t cnt, loff_t *ppos)
972{
973 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400974 int ret;
975
Peter Huewe22fe9b52011-06-07 21:58:27 +0200976 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
977 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400978 return ret;
979
980 val = !!val;
981
982 mutex_lock(&ftrace_profile_lock);
983 if (ftrace_profile_enabled ^ val) {
984 if (val) {
985 ret = ftrace_profile_init();
986 if (ret < 0) {
987 cnt = ret;
988 goto out;
989 }
990
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400991 ret = register_ftrace_profiler();
992 if (ret < 0) {
993 cnt = ret;
994 goto out;
995 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400996 ftrace_profile_enabled = 1;
997 } else {
998 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400999 /*
1000 * unregister_ftrace_profiler calls stop_machine
1001 * so this acts like an synchronize_sched.
1002 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001003 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001004 }
1005 }
1006 out:
1007 mutex_unlock(&ftrace_profile_lock);
1008
Jiri Olsacf8517c2009-10-23 19:36:16 -04001009 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001010
1011 return cnt;
1012}
1013
1014static ssize_t
1015ftrace_profile_read(struct file *filp, char __user *ubuf,
1016 size_t cnt, loff_t *ppos)
1017{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001018 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001019 int r;
1020
1021 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1022 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1023}
1024
1025static const struct file_operations ftrace_profile_fops = {
1026 .open = tracing_open_generic,
1027 .read = ftrace_profile_read,
1028 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001029 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001030};
1031
Steven Rostedtcafb1682009-03-24 20:50:39 -04001032/* used to initialize the real stat files */
1033static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001034 .name = "functions",
1035 .stat_start = function_stat_start,
1036 .stat_next = function_stat_next,
1037 .stat_cmp = function_stat_cmp,
1038 .stat_headers = function_stat_headers,
1039 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001040};
1041
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001042static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001043{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001044 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001045 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001046 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001047 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001048 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001049
Steven Rostedtcafb1682009-03-24 20:50:39 -04001050 for_each_possible_cpu(cpu) {
1051 stat = &per_cpu(ftrace_profile_stats, cpu);
1052
Geliang Tang6363c6b2016-03-15 22:12:34 +08001053 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -04001054 if (!name) {
1055 /*
1056 * The files created are permanent, if something happens
1057 * we still do not free memory.
1058 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001059 WARN(1,
1060 "Could not allocate stat file for cpu %d\n",
1061 cpu);
1062 return;
1063 }
1064 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001065 stat->stat.name = name;
1066 ret = register_stat_tracer(&stat->stat);
1067 if (ret) {
1068 WARN(1,
1069 "Could not register function stat for cpu %d\n",
1070 cpu);
1071 kfree(name);
1072 return;
1073 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001074 }
1075
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001076 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001077 d_tracer, NULL, &ftrace_profile_fops);
1078 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001079 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001080}
1081
1082#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001083static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001084{
1085}
1086#endif /* CONFIG_FUNCTION_PROFILER */
1087
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001088static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1089
Pratyush Anand1619dc32015-03-06 23:58:06 +05301090#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1091static int ftrace_graph_active;
1092#else
1093# define ftrace_graph_active 0
1094#endif
1095
Steven Rostedt3d083392008-05-12 21:20:42 +02001096#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001097
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001098static struct ftrace_ops *removed_ops;
1099
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001100/*
1101 * Set when doing a global update, like enabling all recs or disabling them.
1102 * It is not set when just updating a single ftrace_ops.
1103 */
1104static bool update_all_ops;
1105
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001106#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001107# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001108#endif
1109
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001110struct ftrace_func_entry {
1111 struct hlist_node hlist;
1112 unsigned long ip;
1113};
1114
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001115struct ftrace_func_probe {
1116 struct ftrace_probe_ops *probe_ops;
1117 struct ftrace_ops ops;
1118 struct trace_array *tr;
1119 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001120 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001121 int ref;
1122};
1123
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001124/*
1125 * We make these constant because no one should touch them,
1126 * but they are used as the default "empty hash", to avoid allocating
1127 * it all the time. These are in a read only section such that if
1128 * anyone does try to modify it, it will cause an exception.
1129 */
1130static const struct hlist_head empty_buckets[1];
1131static const struct ftrace_hash empty_hash = {
1132 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001133};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001134#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001135
Steven Rostedt2b499382011-05-03 22:49:52 -04001136static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001137 .func = ftrace_stub,
1138 .local_hash.notrace_hash = EMPTY_HASH,
1139 .local_hash.filter_hash = EMPTY_HASH,
1140 INIT_OPS_HASH(global_ops)
1141 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001142 FTRACE_OPS_FL_INITIALIZED |
1143 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001144};
1145
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001146/*
1147 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001148 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001149 * not return true for either core_kernel_text() or
1150 * is_module_text_address().
1151 */
1152bool is_ftrace_trampoline(unsigned long addr)
1153{
1154 struct ftrace_ops *op;
1155 bool ret = false;
1156
1157 /*
1158 * Some of the ops may be dynamically allocated,
1159 * they are freed after a synchronize_sched().
1160 */
1161 preempt_disable_notrace();
1162
1163 do_for_each_ftrace_op(op, ftrace_ops_list) {
1164 /*
1165 * This is to check for dynamically allocated trampolines.
1166 * Trampolines that are in kernel text will have
1167 * core_kernel_text() return true.
1168 */
1169 if (op->trampoline && op->trampoline_size)
1170 if (addr >= op->trampoline &&
1171 addr < op->trampoline + op->trampoline_size) {
1172 ret = true;
1173 goto out;
1174 }
1175 } while_for_each_ftrace_op(op);
1176
1177 out:
1178 preempt_enable_notrace();
1179
1180 return ret;
1181}
1182
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001183struct ftrace_page {
1184 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001185 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001186 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001187 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001188};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001189
Steven Rostedta7900872011-12-16 16:23:44 -05001190#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1191#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001192
1193/* estimate from running different kernels */
1194#define NR_TO_INIT 10000
1195
1196static struct ftrace_page *ftrace_pages_start;
1197static struct ftrace_page *ftrace_pages;
1198
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001199static __always_inline unsigned long
1200ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1201{
1202 if (hash->size_bits > 0)
1203 return hash_long(ip, hash->size_bits);
1204
1205 return 0;
1206}
1207
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001208/* Only use this function if ftrace_hash_empty() has already been tested */
1209static __always_inline struct ftrace_func_entry *
1210__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001211{
1212 unsigned long key;
1213 struct ftrace_func_entry *entry;
1214 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001215
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001216 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001217 hhd = &hash->buckets[key];
1218
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001219 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001220 if (entry->ip == ip)
1221 return entry;
1222 }
1223 return NULL;
1224}
1225
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001226/**
1227 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1228 * @hash: The hash to look at
1229 * @ip: The instruction pointer to test
1230 *
1231 * Search a given @hash to see if a given instruction pointer (@ip)
1232 * exists in it.
1233 *
1234 * Returns the entry that holds the @ip if found. NULL otherwise.
1235 */
1236struct ftrace_func_entry *
1237ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1238{
1239 if (ftrace_hash_empty(hash))
1240 return NULL;
1241
1242 return __ftrace_lookup_ip(hash, ip);
1243}
1244
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001245static void __add_hash_entry(struct ftrace_hash *hash,
1246 struct ftrace_func_entry *entry)
1247{
1248 struct hlist_head *hhd;
1249 unsigned long key;
1250
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001251 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001252 hhd = &hash->buckets[key];
1253 hlist_add_head(&entry->hlist, hhd);
1254 hash->count++;
1255}
1256
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001257static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1258{
1259 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001260
1261 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1262 if (!entry)
1263 return -ENOMEM;
1264
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001265 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001266 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001267
1268 return 0;
1269}
1270
1271static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001272free_hash_entry(struct ftrace_hash *hash,
1273 struct ftrace_func_entry *entry)
1274{
1275 hlist_del(&entry->hlist);
1276 kfree(entry);
1277 hash->count--;
1278}
1279
1280static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001281remove_hash_entry(struct ftrace_hash *hash,
1282 struct ftrace_func_entry *entry)
1283{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001284 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001285 hash->count--;
1286}
1287
1288static void ftrace_hash_clear(struct ftrace_hash *hash)
1289{
1290 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001291 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001292 struct ftrace_func_entry *entry;
1293 int size = 1 << hash->size_bits;
1294 int i;
1295
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001296 if (!hash->count)
1297 return;
1298
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001299 for (i = 0; i < size; i++) {
1300 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001301 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001303 }
1304 FTRACE_WARN_ON(hash->count);
1305}
1306
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001307static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1308{
1309 list_del(&ftrace_mod->list);
1310 kfree(ftrace_mod->module);
1311 kfree(ftrace_mod->func);
1312 kfree(ftrace_mod);
1313}
1314
1315static void clear_ftrace_mod_list(struct list_head *head)
1316{
1317 struct ftrace_mod_load *p, *n;
1318
1319 /* stack tracer isn't supported yet */
1320 if (!head)
1321 return;
1322
1323 mutex_lock(&ftrace_lock);
1324 list_for_each_entry_safe(p, n, head, list)
1325 free_ftrace_mod(p);
1326 mutex_unlock(&ftrace_lock);
1327}
1328
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001329static void free_ftrace_hash(struct ftrace_hash *hash)
1330{
1331 if (!hash || hash == EMPTY_HASH)
1332 return;
1333 ftrace_hash_clear(hash);
1334 kfree(hash->buckets);
1335 kfree(hash);
1336}
1337
Steven Rostedt07fd5512011-05-05 18:03:47 -04001338static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1339{
1340 struct ftrace_hash *hash;
1341
1342 hash = container_of(rcu, struct ftrace_hash, rcu);
1343 free_ftrace_hash(hash);
1344}
1345
1346static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1347{
1348 if (!hash || hash == EMPTY_HASH)
1349 return;
1350 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1351}
1352
Jiri Olsa5500fa52012-02-15 15:51:54 +01001353void ftrace_free_filter(struct ftrace_ops *ops)
1354{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001355 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001356 free_ftrace_hash(ops->func_hash->filter_hash);
1357 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001358}
1359
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001360static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1361{
1362 struct ftrace_hash *hash;
1363 int size;
1364
1365 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1366 if (!hash)
1367 return NULL;
1368
1369 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001370 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001371
1372 if (!hash->buckets) {
1373 kfree(hash);
1374 return NULL;
1375 }
1376
1377 hash->size_bits = size_bits;
1378
1379 return hash;
1380}
1381
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001382
1383static int ftrace_add_mod(struct trace_array *tr,
1384 const char *func, const char *module,
1385 int enable)
1386{
1387 struct ftrace_mod_load *ftrace_mod;
1388 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1389
1390 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1391 if (!ftrace_mod)
1392 return -ENOMEM;
1393
1394 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1395 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1396 ftrace_mod->enable = enable;
1397
1398 if (!ftrace_mod->func || !ftrace_mod->module)
1399 goto out_free;
1400
1401 list_add(&ftrace_mod->list, mod_head);
1402
1403 return 0;
1404
1405 out_free:
1406 free_ftrace_mod(ftrace_mod);
1407
1408 return -ENOMEM;
1409}
1410
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001411static struct ftrace_hash *
1412alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1413{
1414 struct ftrace_func_entry *entry;
1415 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001416 int size;
1417 int ret;
1418 int i;
1419
1420 new_hash = alloc_ftrace_hash(size_bits);
1421 if (!new_hash)
1422 return NULL;
1423
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001424 if (hash)
1425 new_hash->flags = hash->flags;
1426
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001427 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001428 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001429 return new_hash;
1430
1431 size = 1 << hash->size_bits;
1432 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001433 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001434 ret = add_hash_entry(new_hash, entry->ip);
1435 if (ret < 0)
1436 goto free_hash;
1437 }
1438 }
1439
1440 FTRACE_WARN_ON(new_hash->count != hash->count);
1441
1442 return new_hash;
1443
1444 free_hash:
1445 free_ftrace_hash(new_hash);
1446 return NULL;
1447}
1448
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001449static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001450ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001451static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001452ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001453
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001454static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1455 struct ftrace_hash *new_hash);
1456
Namhyung Kim3e278c02017-01-20 11:44:45 +09001457static struct ftrace_hash *
1458__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001459{
1460 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001461 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001462 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001463 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001464 int size = src->count;
1465 int bits = 0;
1466 int i;
1467
1468 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001469 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001470 */
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001471 if (ftrace_hash_empty(src))
Namhyung Kim3e278c02017-01-20 11:44:45 +09001472 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001473
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001474 /*
1475 * Make the hash size about 1/2 the # found
1476 */
1477 for (size /= 2; size; size >>= 1)
1478 bits++;
1479
1480 /* Don't allocate too much */
1481 if (bits > FTRACE_HASH_MAX_BITS)
1482 bits = FTRACE_HASH_MAX_BITS;
1483
Steven Rostedt07fd5512011-05-05 18:03:47 -04001484 new_hash = alloc_ftrace_hash(bits);
1485 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001486 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001487
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001488 new_hash->flags = src->flags;
1489
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001490 size = 1 << src->size_bits;
1491 for (i = 0; i < size; i++) {
1492 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001493 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001494 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001495 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001496 }
1497 }
1498
Namhyung Kim3e278c02017-01-20 11:44:45 +09001499 return new_hash;
1500}
1501
1502static int
1503ftrace_hash_move(struct ftrace_ops *ops, int enable,
1504 struct ftrace_hash **dst, struct ftrace_hash *src)
1505{
1506 struct ftrace_hash *new_hash;
1507 int ret;
1508
1509 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1510 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1511 return -EINVAL;
1512
1513 new_hash = __ftrace_hash_move(src);
1514 if (!new_hash)
1515 return -ENOMEM;
1516
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001517 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1518 if (enable) {
1519 /* IPMODIFY should be updated only when filter_hash updating */
1520 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1521 if (ret < 0) {
1522 free_ftrace_hash(new_hash);
1523 return ret;
1524 }
1525 }
1526
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001527 /*
1528 * Remove the current set, update the hash and add
1529 * them back.
1530 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001531 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001532
Steven Rostedt07fd5512011-05-05 18:03:47 -04001533 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001534
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001535 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001536
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001537 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001538}
1539
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001540static bool hash_contains_ip(unsigned long ip,
1541 struct ftrace_ops_hash *hash)
1542{
1543 /*
1544 * The function record is a match if it exists in the filter
1545 * hash and not in the notrace hash. Note, an emty hash is
1546 * considered a match for the filter hash, but an empty
1547 * notrace hash is considered not in the notrace hash.
1548 */
1549 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001550 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001551 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001552 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001553}
1554
Steven Rostedt265c8312009-02-13 12:43:56 -05001555/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001556 * Test the hashes for this ops to see if we want to call
1557 * the ops->func or not.
1558 *
1559 * It's a match if the ip is in the ops->filter_hash or
1560 * the filter_hash does not exist or is empty,
1561 * AND
1562 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001563 *
1564 * This needs to be called with preemption disabled as
1565 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001566 */
1567static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001568ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001569{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001570 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001571 int ret;
1572
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001573#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1574 /*
1575 * There's a small race when adding ops that the ftrace handler
1576 * that wants regs, may be called without them. We can not
1577 * allow that handler to be called if regs is NULL.
1578 */
1579 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1580 return 0;
1581#endif
1582
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001583 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1584 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001585
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001586 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001587 ret = 1;
1588 else
1589 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001590
1591 return ret;
1592}
1593
1594/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001595 * This is a double for. Do not use 'break' to break out of the loop,
1596 * you must use a goto.
1597 */
1598#define do_for_each_ftrace_rec(pg, rec) \
1599 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1600 int _____i; \
1601 for (_____i = 0; _____i < pg->index; _____i++) { \
1602 rec = &pg->records[_____i];
1603
1604#define while_for_each_ftrace_rec() \
1605 } \
1606 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301607
Steven Rostedt5855fea2011-12-16 19:27:42 -05001608
1609static int ftrace_cmp_recs(const void *a, const void *b)
1610{
Steven Rostedta650e022012-04-25 13:48:13 -04001611 const struct dyn_ftrace *key = a;
1612 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001613
Steven Rostedta650e022012-04-25 13:48:13 -04001614 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001615 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001616 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1617 return 1;
1618 return 0;
1619}
1620
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001621/**
1622 * ftrace_location_range - return the first address of a traced location
1623 * if it touches the given ip range
1624 * @start: start of range to search.
1625 * @end: end of range to search (inclusive). @end points to the last byte
1626 * to check.
1627 *
1628 * Returns rec->ip if the related ftrace location is a least partly within
1629 * the given address range. That is, the first address of the instruction
1630 * that is either a NOP or call to the function tracer. It checks the ftrace
1631 * internal tables to determine if the address belongs or not.
1632 */
1633unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001634{
1635 struct ftrace_page *pg;
1636 struct dyn_ftrace *rec;
1637 struct dyn_ftrace key;
1638
1639 key.ip = start;
1640 key.flags = end; /* overload flags, as it is unsigned long */
1641
1642 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1643 if (end < pg->records[0].ip ||
1644 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1645 continue;
1646 rec = bsearch(&key, pg->records, pg->index,
1647 sizeof(struct dyn_ftrace),
1648 ftrace_cmp_recs);
1649 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001650 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001651 }
1652
Steven Rostedt5855fea2011-12-16 19:27:42 -05001653 return 0;
1654}
1655
Steven Rostedtc88fd862011-08-16 09:53:39 -04001656/**
1657 * ftrace_location - return true if the ip giving is a traced location
1658 * @ip: the instruction pointer to check
1659 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001660 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001661 * That is, the instruction that is either a NOP or call to
1662 * the function tracer. It checks the ftrace internal tables to
1663 * determine if the address belongs or not.
1664 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001665unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001666{
Steven Rostedta650e022012-04-25 13:48:13 -04001667 return ftrace_location_range(ip, ip);
1668}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001669
Steven Rostedta650e022012-04-25 13:48:13 -04001670/**
1671 * ftrace_text_reserved - return true if range contains an ftrace location
1672 * @start: start of range to search
1673 * @end: end of range to search (inclusive). @end points to the last byte to check.
1674 *
1675 * Returns 1 if @start and @end contains a ftrace location.
1676 * That is, the instruction that is either a NOP or call to
1677 * the function tracer. It checks the ftrace internal tables to
1678 * determine if the address belongs or not.
1679 */
Sasha Levind88471c2013-01-09 18:09:20 -05001680int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001681{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001682 unsigned long ret;
1683
1684 ret = ftrace_location_range((unsigned long)start,
1685 (unsigned long)end);
1686
1687 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001688}
1689
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001690/* Test if ops registered to this rec needs regs */
1691static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1692{
1693 struct ftrace_ops *ops;
1694 bool keep_regs = false;
1695
1696 for (ops = ftrace_ops_list;
1697 ops != &ftrace_list_end; ops = ops->next) {
1698 /* pass rec in as regs to have non-NULL val */
1699 if (ftrace_ops_test(ops, rec->ip, rec)) {
1700 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1701 keep_regs = true;
1702 break;
1703 }
1704 }
1705 }
1706
1707 return keep_regs;
1708}
1709
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001710static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001711 int filter_hash,
1712 bool inc)
1713{
1714 struct ftrace_hash *hash;
1715 struct ftrace_hash *other_hash;
1716 struct ftrace_page *pg;
1717 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001718 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001719 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001720 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001721
1722 /* Only update if the ops has been registered */
1723 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001724 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001725
1726 /*
1727 * In the filter_hash case:
1728 * If the count is zero, we update all records.
1729 * Otherwise we just update the items in the hash.
1730 *
1731 * In the notrace_hash case:
1732 * We enable the update in the hash.
1733 * As disabling notrace means enabling the tracing,
1734 * and enabling notrace means disabling, the inc variable
1735 * gets inversed.
1736 */
1737 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001738 hash = ops->func_hash->filter_hash;
1739 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001740 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001741 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001742 } else {
1743 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001744 hash = ops->func_hash->notrace_hash;
1745 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001746 /*
1747 * If the notrace hash has no items,
1748 * then there's nothing to do.
1749 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001750 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001751 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001752 }
1753
1754 do_for_each_ftrace_rec(pg, rec) {
1755 int in_other_hash = 0;
1756 int in_hash = 0;
1757 int match = 0;
1758
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001759 if (rec->flags & FTRACE_FL_DISABLED)
1760 continue;
1761
Steven Rostedted926f92011-05-03 13:25:24 -04001762 if (all) {
1763 /*
1764 * Only the filter_hash affects all records.
1765 * Update if the record is not in the notrace hash.
1766 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001767 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001768 match = 1;
1769 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001770 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1771 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001772
1773 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001774 * If filter_hash is set, we want to match all functions
1775 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001776 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001777 * If filter_hash is not set, then we are decrementing.
1778 * That means we match anything that is in the hash
1779 * and also in the other_hash. That is, we need to turn
1780 * off functions in the other hash because they are disabled
1781 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001782 */
1783 if (filter_hash && in_hash && !in_other_hash)
1784 match = 1;
1785 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001786 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001787 match = 1;
1788 }
1789 if (!match)
1790 continue;
1791
1792 if (inc) {
1793 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001794 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001795 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001796
1797 /*
1798 * If there's only a single callback registered to a
1799 * function, and the ops has a trampoline registered
1800 * for it, then we can call it directly.
1801 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001802 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001803 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001804 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001805 /*
1806 * If we are adding another function callback
1807 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001808 * custom trampoline in use, then we need to go
1809 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001810 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001811 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001812
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001813 /*
1814 * If any ops wants regs saved for this function
1815 * then all ops will get saved regs.
1816 */
1817 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1818 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001819 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001820 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001821 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001822 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001823
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001824 /*
1825 * If the rec had REGS enabled and the ops that is
1826 * being removed had REGS set, then see if there is
1827 * still any ops for this record that wants regs.
1828 * If not, we can stop recording them.
1829 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001830 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001831 rec->flags & FTRACE_FL_REGS &&
1832 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1833 if (!test_rec_ops_needs_regs(rec))
1834 rec->flags &= ~FTRACE_FL_REGS;
1835 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001836
1837 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001838 * If the rec had TRAMP enabled, then it needs to
1839 * be cleared. As TRAMP can only be enabled iff
1840 * there is only a single ops attached to it.
1841 * In otherwords, always disable it on decrementing.
1842 * In the future, we may set it if rec count is
1843 * decremented to one, and the ops that is left
1844 * has a trampoline.
1845 */
1846 rec->flags &= ~FTRACE_FL_TRAMP;
1847
1848 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001849 * flags will be cleared in ftrace_check_record()
1850 * if rec count is zero.
1851 */
Steven Rostedted926f92011-05-03 13:25:24 -04001852 }
1853 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001854
1855 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1856 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1857
Steven Rostedted926f92011-05-03 13:25:24 -04001858 /* Shortcut, if we handled all records, we are done. */
1859 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001860 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001861 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001862
1863 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001864}
1865
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001866static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001867 int filter_hash)
1868{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001869 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001870}
1871
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001872static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001873 int filter_hash)
1874{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001875 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001876}
1877
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001878static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1879 int filter_hash, int inc)
1880{
1881 struct ftrace_ops *op;
1882
1883 __ftrace_hash_rec_update(ops, filter_hash, inc);
1884
1885 if (ops->func_hash != &global_ops.local_hash)
1886 return;
1887
1888 /*
1889 * If the ops shares the global_ops hash, then we need to update
1890 * all ops that are enabled and use this hash.
1891 */
1892 do_for_each_ftrace_op(op, ftrace_ops_list) {
1893 /* Already done */
1894 if (op == ops)
1895 continue;
1896 if (op->func_hash == &global_ops.local_hash)
1897 __ftrace_hash_rec_update(op, filter_hash, inc);
1898 } while_for_each_ftrace_op(op);
1899}
1900
1901static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1902 int filter_hash)
1903{
1904 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1905}
1906
1907static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1908 int filter_hash)
1909{
1910 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1911}
1912
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001913/*
1914 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1915 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1916 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1917 * Note that old_hash and new_hash has below meanings
1918 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1919 * - If the hash is EMPTY_HASH, it hits nothing
1920 * - Anything else hits the recs which match the hash entries.
1921 */
1922static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1923 struct ftrace_hash *old_hash,
1924 struct ftrace_hash *new_hash)
1925{
1926 struct ftrace_page *pg;
1927 struct dyn_ftrace *rec, *end = NULL;
1928 int in_old, in_new;
1929
1930 /* Only update if the ops has been registered */
1931 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1932 return 0;
1933
1934 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1935 return 0;
1936
1937 /*
1938 * Since the IPMODIFY is a very address sensitive action, we do not
1939 * allow ftrace_ops to set all functions to new hash.
1940 */
1941 if (!new_hash || !old_hash)
1942 return -EINVAL;
1943
1944 /* Update rec->flags */
1945 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001946
1947 if (rec->flags & FTRACE_FL_DISABLED)
1948 continue;
1949
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001950 /* We need to update only differences of filter_hash */
1951 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1952 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1953 if (in_old == in_new)
1954 continue;
1955
1956 if (in_new) {
1957 /* New entries must ensure no others are using it */
1958 if (rec->flags & FTRACE_FL_IPMODIFY)
1959 goto rollback;
1960 rec->flags |= FTRACE_FL_IPMODIFY;
1961 } else /* Removed entry */
1962 rec->flags &= ~FTRACE_FL_IPMODIFY;
1963 } while_for_each_ftrace_rec();
1964
1965 return 0;
1966
1967rollback:
1968 end = rec;
1969
1970 /* Roll back what we did above */
1971 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001972
1973 if (rec->flags & FTRACE_FL_DISABLED)
1974 continue;
1975
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001976 if (rec == end)
1977 goto err_out;
1978
1979 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1980 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1981 if (in_old == in_new)
1982 continue;
1983
1984 if (in_new)
1985 rec->flags &= ~FTRACE_FL_IPMODIFY;
1986 else
1987 rec->flags |= FTRACE_FL_IPMODIFY;
1988 } while_for_each_ftrace_rec();
1989
1990err_out:
1991 return -EBUSY;
1992}
1993
1994static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1995{
1996 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1997
1998 if (ftrace_hash_empty(hash))
1999 hash = NULL;
2000
2001 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
2002}
2003
2004/* Disabling always succeeds */
2005static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
2006{
2007 struct ftrace_hash *hash = ops->func_hash->filter_hash;
2008
2009 if (ftrace_hash_empty(hash))
2010 hash = NULL;
2011
2012 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
2013}
2014
2015static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
2016 struct ftrace_hash *new_hash)
2017{
2018 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
2019
2020 if (ftrace_hash_empty(old_hash))
2021 old_hash = NULL;
2022
2023 if (ftrace_hash_empty(new_hash))
2024 new_hash = NULL;
2025
2026 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
2027}
2028
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002029static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07002030{
2031 int i;
2032
2033 printk(KERN_CONT "%s", fmt);
2034
2035 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
2036 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
2037}
2038
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002039static struct ftrace_ops *
2040ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002041static struct ftrace_ops *
2042ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002043
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002044enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002045const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002046
2047static void print_bug_type(void)
2048{
2049 switch (ftrace_bug_type) {
2050 case FTRACE_BUG_UNKNOWN:
2051 break;
2052 case FTRACE_BUG_INIT:
2053 pr_info("Initializing ftrace call sites\n");
2054 break;
2055 case FTRACE_BUG_NOP:
2056 pr_info("Setting ftrace call site to NOP\n");
2057 break;
2058 case FTRACE_BUG_CALL:
2059 pr_info("Setting ftrace call site to call ftrace function\n");
2060 break;
2061 case FTRACE_BUG_UPDATE:
2062 pr_info("Updating ftrace call site to call a different ftrace function\n");
2063 break;
2064 }
2065}
2066
Steven Rostedtc88fd862011-08-16 09:53:39 -04002067/**
2068 * ftrace_bug - report and shutdown function tracer
2069 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002070 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04002071 *
2072 * The arch code that enables or disables the function tracing
2073 * can call ftrace_bug() when it has detected a problem in
2074 * modifying the code. @failed should be one of either:
2075 * EFAULT - if the problem happens on reading the @ip address
2076 * EINVAL - if what is read at @ip is not what was expected
2077 * EPERM - if the problem happens on writting to the @ip address
2078 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002079void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002080{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002081 unsigned long ip = rec ? rec->ip : 0;
2082
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002083 switch (failed) {
2084 case -EFAULT:
2085 FTRACE_WARN_ON_ONCE(1);
2086 pr_info("ftrace faulted on modifying ");
2087 print_ip_sym(ip);
2088 break;
2089 case -EINVAL:
2090 FTRACE_WARN_ON_ONCE(1);
2091 pr_info("ftrace failed to modify ");
2092 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002093 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002094 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002095 if (ftrace_expected) {
2096 print_ip_ins(" expected: ", ftrace_expected);
2097 pr_cont("\n");
2098 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002099 break;
2100 case -EPERM:
2101 FTRACE_WARN_ON_ONCE(1);
2102 pr_info("ftrace faulted on writing ");
2103 print_ip_sym(ip);
2104 break;
2105 default:
2106 FTRACE_WARN_ON_ONCE(1);
2107 pr_info("ftrace faulted on unknown error ");
2108 print_ip_sym(ip);
2109 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002110 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002111 if (rec) {
2112 struct ftrace_ops *ops = NULL;
2113
2114 pr_info("ftrace record flags: %lx\n", rec->flags);
2115 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2116 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2117 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2118 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002119 if (ops) {
2120 do {
2121 pr_cont("\ttramp: %pS (%pS)",
2122 (void *)ops->trampoline,
2123 (void *)ops->func);
2124 ops = ftrace_find_tramp_ops_next(rec, ops);
2125 } while (ops);
2126 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002127 pr_cont("\ttramp: ERROR!");
2128
2129 }
2130 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002131 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002132 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002133}
2134
Steven Rostedtc88fd862011-08-16 09:53:39 -04002135static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002136{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002137 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002138
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002139 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2140
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002141 if (rec->flags & FTRACE_FL_DISABLED)
2142 return FTRACE_UPDATE_IGNORE;
2143
Steven Rostedt982c3502008-11-15 16:31:41 -05002144 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002145 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002146 *
Steven Rostedted926f92011-05-03 13:25:24 -04002147 * If the record has a ref count, then we need to enable it
2148 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002149 *
Steven Rostedted926f92011-05-03 13:25:24 -04002150 * Otherwise we make sure its disabled.
2151 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002152 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002153 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002154 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002155 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002156 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002157
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002158 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002159 * If enabling and the REGS flag does not match the REGS_EN, or
2160 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2161 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002162 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002163 if (flag) {
2164 if (!(rec->flags & FTRACE_FL_REGS) !=
2165 !(rec->flags & FTRACE_FL_REGS_EN))
2166 flag |= FTRACE_FL_REGS;
2167
2168 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2169 !(rec->flags & FTRACE_FL_TRAMP_EN))
2170 flag |= FTRACE_FL_TRAMP;
2171 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002172
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002173 /* If the state of this record hasn't changed, then do nothing */
2174 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002175 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002176
2177 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002178 /* Save off if rec is being enabled (for return value) */
2179 flag ^= rec->flags & FTRACE_FL_ENABLED;
2180
2181 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002182 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002183 if (flag & FTRACE_FL_REGS) {
2184 if (rec->flags & FTRACE_FL_REGS)
2185 rec->flags |= FTRACE_FL_REGS_EN;
2186 else
2187 rec->flags &= ~FTRACE_FL_REGS_EN;
2188 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002189 if (flag & FTRACE_FL_TRAMP) {
2190 if (rec->flags & FTRACE_FL_TRAMP)
2191 rec->flags |= FTRACE_FL_TRAMP_EN;
2192 else
2193 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2194 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002195 }
2196
2197 /*
2198 * If this record is being updated from a nop, then
2199 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002200 * Otherwise,
2201 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002202 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002203 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002204 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002205 if (flag & FTRACE_FL_ENABLED) {
2206 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002207 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002208 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002209
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002210 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002211 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002212 }
2213
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002214 if (update) {
2215 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002216 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002217 rec->flags = 0;
2218 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002219 /*
2220 * Just disable the record, but keep the ops TRAMP
2221 * and REGS states. The _EN flags must be disabled though.
2222 */
2223 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2224 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002225 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002226
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002227 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002228 return FTRACE_UPDATE_MAKE_NOP;
2229}
2230
2231/**
2232 * ftrace_update_record, set a record that now is tracing or not
2233 * @rec: the record to update
2234 * @enable: set to 1 if the record is tracing, zero to force disable
2235 *
2236 * The records that represent all functions that can be traced need
2237 * to be updated when tracing has been enabled.
2238 */
2239int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2240{
2241 return ftrace_check_record(rec, enable, 1);
2242}
2243
2244/**
2245 * ftrace_test_record, check if the record has been enabled or not
2246 * @rec: the record to test
2247 * @enable: set to 1 to check if enabled, 0 if it is disabled
2248 *
2249 * The arch code may need to test if a record is already set to
2250 * tracing to determine how to modify the function code that it
2251 * represents.
2252 */
2253int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2254{
2255 return ftrace_check_record(rec, enable, 0);
2256}
2257
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002258static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002259ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2260{
2261 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002262 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002263
2264 do_for_each_ftrace_op(op, ftrace_ops_list) {
2265
2266 if (!op->trampoline)
2267 continue;
2268
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002269 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002270 return op;
2271 } while_for_each_ftrace_op(op);
2272
2273 return NULL;
2274}
2275
2276static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002277ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2278 struct ftrace_ops *op)
2279{
2280 unsigned long ip = rec->ip;
2281
2282 while_for_each_ftrace_op(op) {
2283
2284 if (!op->trampoline)
2285 continue;
2286
2287 if (hash_contains_ip(ip, op->func_hash))
2288 return op;
2289 }
2290
2291 return NULL;
2292}
2293
2294static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002295ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2296{
2297 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002298 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002299
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002300 /*
2301 * Need to check removed ops first.
2302 * If they are being removed, and this rec has a tramp,
2303 * and this rec is in the ops list, then it would be the
2304 * one with the tramp.
2305 */
2306 if (removed_ops) {
2307 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002308 return removed_ops;
2309 }
2310
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002311 /*
2312 * Need to find the current trampoline for a rec.
2313 * Now, a trampoline is only attached to a rec if there
2314 * was a single 'ops' attached to it. But this can be called
2315 * when we are adding another op to the rec or removing the
2316 * current one. Thus, if the op is being added, we can
2317 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002318 * yet.
2319 *
2320 * If an ops is being modified (hooking to different functions)
2321 * then we don't care about the new functions that are being
2322 * added, just the old ones (that are probably being removed).
2323 *
2324 * If we are adding an ops to a function that already is using
2325 * a trampoline, it needs to be removed (trampolines are only
2326 * for single ops connected), then an ops that is not being
2327 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002328 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002329 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002330
2331 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002332 continue;
2333
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002334 /*
2335 * If the ops is being added, it hasn't gotten to
2336 * the point to be removed from this tree yet.
2337 */
2338 if (op->flags & FTRACE_OPS_FL_ADDING)
2339 continue;
2340
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002341
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002342 /*
2343 * If the ops is being modified and is in the old
2344 * hash, then it is probably being removed from this
2345 * function.
2346 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002347 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2348 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002349 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002350 /*
2351 * If the ops is not being added or modified, and it's
2352 * in its normal filter hash, then this must be the one
2353 * we want!
2354 */
2355 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2356 hash_contains_ip(ip, op->func_hash))
2357 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002358
2359 } while_for_each_ftrace_op(op);
2360
2361 return NULL;
2362}
2363
2364static struct ftrace_ops *
2365ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2366{
2367 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002368 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002369
2370 do_for_each_ftrace_op(op, ftrace_ops_list) {
2371 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002372 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002373 return op;
2374 } while_for_each_ftrace_op(op);
2375
2376 return NULL;
2377}
2378
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002379/**
2380 * ftrace_get_addr_new - Get the call address to set to
2381 * @rec: The ftrace record descriptor
2382 *
2383 * If the record has the FTRACE_FL_REGS set, that means that it
2384 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2385 * is not not set, then it wants to convert to the normal callback.
2386 *
2387 * Returns the address of the trampoline to set to
2388 */
2389unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2390{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002391 struct ftrace_ops *ops;
2392
2393 /* Trampolines take precedence over regs */
2394 if (rec->flags & FTRACE_FL_TRAMP) {
2395 ops = ftrace_find_tramp_ops_new(rec);
2396 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002397 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2398 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002399 /* Ftrace is shutting down, return anything */
2400 return (unsigned long)FTRACE_ADDR;
2401 }
2402 return ops->trampoline;
2403 }
2404
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002405 if (rec->flags & FTRACE_FL_REGS)
2406 return (unsigned long)FTRACE_REGS_ADDR;
2407 else
2408 return (unsigned long)FTRACE_ADDR;
2409}
2410
2411/**
2412 * ftrace_get_addr_curr - Get the call address that is already there
2413 * @rec: The ftrace record descriptor
2414 *
2415 * The FTRACE_FL_REGS_EN is set when the record already points to
2416 * a function that saves all the regs. Basically the '_EN' version
2417 * represents the current state of the function.
2418 *
2419 * Returns the address of the trampoline that is currently being called
2420 */
2421unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2422{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002423 struct ftrace_ops *ops;
2424
2425 /* Trampolines take precedence over regs */
2426 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2427 ops = ftrace_find_tramp_ops_curr(rec);
2428 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002429 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2430 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002431 /* Ftrace is shutting down, return anything */
2432 return (unsigned long)FTRACE_ADDR;
2433 }
2434 return ops->trampoline;
2435 }
2436
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002437 if (rec->flags & FTRACE_FL_REGS_EN)
2438 return (unsigned long)FTRACE_REGS_ADDR;
2439 else
2440 return (unsigned long)FTRACE_ADDR;
2441}
2442
Steven Rostedtc88fd862011-08-16 09:53:39 -04002443static int
2444__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2445{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002446 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002447 unsigned long ftrace_addr;
2448 int ret;
2449
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002450 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002451
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002452 /* This needs to be done before we call ftrace_update_record */
2453 ftrace_old_addr = ftrace_get_addr_curr(rec);
2454
2455 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002456
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002457 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2458
Steven Rostedtc88fd862011-08-16 09:53:39 -04002459 switch (ret) {
2460 case FTRACE_UPDATE_IGNORE:
2461 return 0;
2462
2463 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002464 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002465 return ftrace_make_call(rec, ftrace_addr);
2466
2467 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002468 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002469 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002470
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002471 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002472 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002473 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002474 }
2475
2476 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002477}
2478
Steven Rostedte4f5d542012-04-27 09:13:18 -04002479void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002480{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002481 struct dyn_ftrace *rec;
2482 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002483 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002484
Steven Rostedt45a4a232011-04-21 23:16:46 -04002485 if (unlikely(ftrace_disabled))
2486 return;
2487
Steven Rostedt265c8312009-02-13 12:43:56 -05002488 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002489
2490 if (rec->flags & FTRACE_FL_DISABLED)
2491 continue;
2492
Steven Rostedte4f5d542012-04-27 09:13:18 -04002493 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002494 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002495 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002496 /* Stop processing */
2497 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002498 }
2499 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002500}
2501
Steven Rostedtc88fd862011-08-16 09:53:39 -04002502struct ftrace_rec_iter {
2503 struct ftrace_page *pg;
2504 int index;
2505};
2506
2507/**
2508 * ftrace_rec_iter_start, start up iterating over traced functions
2509 *
2510 * Returns an iterator handle that is used to iterate over all
2511 * the records that represent address locations where functions
2512 * are traced.
2513 *
2514 * May return NULL if no records are available.
2515 */
2516struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2517{
2518 /*
2519 * We only use a single iterator.
2520 * Protected by the ftrace_lock mutex.
2521 */
2522 static struct ftrace_rec_iter ftrace_rec_iter;
2523 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2524
2525 iter->pg = ftrace_pages_start;
2526 iter->index = 0;
2527
2528 /* Could have empty pages */
2529 while (iter->pg && !iter->pg->index)
2530 iter->pg = iter->pg->next;
2531
2532 if (!iter->pg)
2533 return NULL;
2534
2535 return iter;
2536}
2537
2538/**
2539 * ftrace_rec_iter_next, get the next record to process.
2540 * @iter: The handle to the iterator.
2541 *
2542 * Returns the next iterator after the given iterator @iter.
2543 */
2544struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2545{
2546 iter->index++;
2547
2548 if (iter->index >= iter->pg->index) {
2549 iter->pg = iter->pg->next;
2550 iter->index = 0;
2551
2552 /* Could have empty pages */
2553 while (iter->pg && !iter->pg->index)
2554 iter->pg = iter->pg->next;
2555 }
2556
2557 if (!iter->pg)
2558 return NULL;
2559
2560 return iter;
2561}
2562
2563/**
2564 * ftrace_rec_iter_record, get the record at the iterator location
2565 * @iter: The current iterator location
2566 *
2567 * Returns the record that the current @iter is at.
2568 */
2569struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2570{
2571 return &iter->pg->records[iter->index];
2572}
2573
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302574static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002575ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002576{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002577 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002578
Steven Rostedt45a4a232011-04-21 23:16:46 -04002579 if (unlikely(ftrace_disabled))
2580 return 0;
2581
Shaohua Li25aac9d2009-01-09 11:29:40 +08002582 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002583 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002584 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002585 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302586 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002587 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302588 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002589}
2590
Steven Rostedt000ab692009-02-17 13:35:06 -05002591/*
2592 * archs can override this function if they must do something
2593 * before the modifying code is performed.
2594 */
2595int __weak ftrace_arch_code_modify_prepare(void)
2596{
2597 return 0;
2598}
2599
2600/*
2601 * archs can override this function if they must do something
2602 * after the modifying code is performed.
2603 */
2604int __weak ftrace_arch_code_modify_post_process(void)
2605{
2606 return 0;
2607}
2608
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002609void ftrace_modify_all_code(int command)
2610{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002611 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002612 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002613
2614 /*
2615 * If the ftrace_caller calls a ftrace_ops func directly,
2616 * we need to make sure that it only traces functions it
2617 * expects to trace. When doing the switch of functions,
2618 * we need to update to the ftrace_ops_list_func first
2619 * before the transition between old and new calls are set,
2620 * as the ftrace_ops_list_func will check the ops hashes
2621 * to make sure the ops are having the right functions
2622 * traced.
2623 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002624 if (update) {
2625 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2626 if (FTRACE_WARN_ON(err))
2627 return;
2628 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002629
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002630 if (command & FTRACE_UPDATE_CALLS)
2631 ftrace_replace_code(1);
2632 else if (command & FTRACE_DISABLE_CALLS)
2633 ftrace_replace_code(0);
2634
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002635 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2636 function_trace_op = set_function_trace_op;
2637 smp_wmb();
2638 /* If irqs are disabled, we are in stop machine */
2639 if (!irqs_disabled())
2640 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002641 err = ftrace_update_ftrace_func(ftrace_trace_function);
2642 if (FTRACE_WARN_ON(err))
2643 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002644 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002645
2646 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002647 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002648 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002649 err = ftrace_disable_ftrace_graph_caller();
2650 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002651}
2652
Ingo Molnare309b412008-05-12 21:20:51 +02002653static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002654{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002655 int *command = data;
2656
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002657 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002658
Steven Rostedtc88fd862011-08-16 09:53:39 -04002659 return 0;
2660}
2661
2662/**
2663 * ftrace_run_stop_machine, go back to the stop machine method
2664 * @command: The command to tell ftrace what to do
2665 *
2666 * If an arch needs to fall back to the stop machine method, the
2667 * it can call this function.
2668 */
2669void ftrace_run_stop_machine(int command)
2670{
2671 stop_machine(__ftrace_modify_code, &command, NULL);
2672}
2673
2674/**
2675 * arch_ftrace_update_code, modify the code to trace or not trace
2676 * @command: The command that needs to be done
2677 *
2678 * Archs can override this function if it does not need to
2679 * run stop_machine() to modify code.
2680 */
2681void __weak arch_ftrace_update_code(int command)
2682{
2683 ftrace_run_stop_machine(command);
2684}
2685
2686static void ftrace_run_update_code(int command)
2687{
2688 int ret;
2689
2690 ret = ftrace_arch_code_modify_prepare();
2691 FTRACE_WARN_ON(ret);
2692 if (ret)
2693 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002694
2695 /*
2696 * By default we use stop_machine() to modify the code.
2697 * But archs can do what ever they want as long as it
2698 * is safe. The stop_machine() is the safest, but also
2699 * produces the most overhead.
2700 */
2701 arch_ftrace_update_code(command);
2702
Steven Rostedt000ab692009-02-17 13:35:06 -05002703 ret = ftrace_arch_code_modify_post_process();
2704 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002705}
2706
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002707static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002708 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002709{
2710 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002711 ops->old_hash.filter_hash = old_hash->filter_hash;
2712 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002713 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002714 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002715 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002716 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2717}
2718
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002719static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002720static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002721
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002722void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2723{
2724}
2725
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002726static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002727{
2728 free_percpu(ops->disabled);
2729}
2730
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002731static void ftrace_startup_enable(int command)
2732{
2733 if (saved_ftrace_func != ftrace_trace_function) {
2734 saved_ftrace_func = ftrace_trace_function;
2735 command |= FTRACE_UPDATE_TRACE_FUNC;
2736 }
2737
2738 if (!command || !ftrace_enabled)
2739 return;
2740
2741 ftrace_run_update_code(command);
2742}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002743
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002744static void ftrace_startup_all(int command)
2745{
2746 update_all_ops = true;
2747 ftrace_startup_enable(command);
2748 update_all_ops = false;
2749}
2750
Steven Rostedta1cd6172011-05-23 15:24:25 -04002751static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002752{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002753 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002754
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002755 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002756 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002757
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002758 ret = __register_ftrace_function(ops);
2759 if (ret)
2760 return ret;
2761
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002762 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002763
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002764 /*
2765 * Note that ftrace probes uses this to start up
2766 * and modify functions it will probe. But we still
2767 * set the ADDING flag for modification, as probes
2768 * do not have trampolines. If they add them in the
2769 * future, then the probes will need to distinguish
2770 * between adding and updating probes.
2771 */
2772 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002773
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002774 ret = ftrace_hash_ipmodify_enable(ops);
2775 if (ret < 0) {
2776 /* Rollback registration process */
2777 __unregister_ftrace_function(ops);
2778 ftrace_start_up--;
2779 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2780 return ret;
2781 }
2782
Jiri Olsa7f50d062016-03-16 15:34:33 +01002783 if (ftrace_hash_rec_enable(ops, 1))
2784 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002785
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002786 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002787
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002788 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2789
Steven Rostedta1cd6172011-05-23 15:24:25 -04002790 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002791}
2792
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002793static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002794{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002795 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002796
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002797 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002798 return -ENODEV;
2799
2800 ret = __unregister_ftrace_function(ops);
2801 if (ret)
2802 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002803
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002804 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002805 /*
2806 * Just warn in case of unbalance, no need to kill ftrace, it's not
2807 * critical but the ftrace_call callers may be never nopped again after
2808 * further ftrace uses.
2809 */
2810 WARN_ON_ONCE(ftrace_start_up < 0);
2811
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002812 /* Disabling ipmodify never fails */
2813 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002814
2815 if (ftrace_hash_rec_disable(ops, 1))
2816 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002817
Namhyung Kima737e6d2014-06-12 23:56:12 +09002818 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002819
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002820 if (saved_ftrace_func != ftrace_trace_function) {
2821 saved_ftrace_func = ftrace_trace_function;
2822 command |= FTRACE_UPDATE_TRACE_FUNC;
2823 }
2824
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002825 if (!command || !ftrace_enabled) {
2826 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002827 * If these are per_cpu ops, they still need their
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002828 * per_cpu field freed. Since, function tracing is
2829 * not currently active, we can just free them
2830 * without synchronizing all CPUs.
2831 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002832 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2833 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002834 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002835 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002836
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002837 /*
2838 * If the ops uses a trampoline, then it needs to be
2839 * tested first on update.
2840 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002841 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002842 removed_ops = ops;
2843
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002844 /* The trampoline logic checks the old hashes */
2845 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2846 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2847
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002848 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002849
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002850 /*
2851 * If there's no more ops registered with ftrace, run a
2852 * sanity check to make sure all rec flags are cleared.
2853 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002854 if (rcu_dereference_protected(ftrace_ops_list,
2855 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002856 struct ftrace_page *pg;
2857 struct dyn_ftrace *rec;
2858
2859 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002860 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002861 pr_warn(" %pS flags:%lx\n",
2862 (void *)rec->ip, rec->flags);
2863 } while_for_each_ftrace_rec();
2864 }
2865
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002866 ops->old_hash.filter_hash = NULL;
2867 ops->old_hash.notrace_hash = NULL;
2868
2869 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002870 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002871
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002872 /*
2873 * Dynamic ops may be freed, we must make sure that all
2874 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002875 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002876 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002877 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002878 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002879 /*
2880 * We need to do a hard force of sched synchronization.
2881 * This is because we use preempt_disable() to do RCU, but
2882 * the function tracers can be called where RCU is not watching
2883 * (like before user_exit()). We can not rely on the RCU
2884 * infrastructure to do the synchronization, thus we must do it
2885 * ourselves.
2886 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002887 schedule_on_each_cpu(ftrace_sync);
2888
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002889 /*
2890 * When the kernel is preeptive, tasks can be preempted
2891 * while on a ftrace trampoline. Just scheduling a task on
2892 * a CPU is not good enough to flush them. Calling
2893 * synchornize_rcu_tasks() will wait for those tasks to
2894 * execute and either schedule voluntarily or enter user space.
2895 */
2896 if (IS_ENABLED(CONFIG_PREEMPT))
2897 synchronize_rcu_tasks();
2898
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002899 arch_ftrace_trampoline_free(ops);
2900
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002901 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2902 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002903 }
2904
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002905 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002906}
2907
Ingo Molnare309b412008-05-12 21:20:51 +02002908static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002909{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302910 int command;
2911
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002912 if (unlikely(ftrace_disabled))
2913 return;
2914
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002915 /* Force update next time */
2916 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002917 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302918 if (ftrace_start_up) {
2919 command = FTRACE_UPDATE_CALLS;
2920 if (ftrace_graph_active)
2921 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002922 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302923 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002924}
2925
Ingo Molnare309b412008-05-12 21:20:51 +02002926static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002927{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302928 int command;
2929
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002930 if (unlikely(ftrace_disabled))
2931 return;
2932
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002933 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302934 if (ftrace_start_up) {
2935 command = FTRACE_DISABLE_CALLS;
2936 if (ftrace_graph_active)
2937 command |= FTRACE_STOP_FUNC_RET;
2938 ftrace_run_update_code(command);
2939 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002940}
2941
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002942static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002943unsigned long ftrace_update_tot_cnt;
2944
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002945static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002946{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002947 /*
2948 * Filter_hash being empty will default to trace module.
2949 * But notrace hash requires a test of individual module functions.
2950 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002951 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2952 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002953}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002954
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002955/*
2956 * Check if the current ops references the record.
2957 *
2958 * If the ops traces all functions, then it was already accounted for.
2959 * If the ops does not trace the current record function, skip it.
2960 * If the ops ignores the function via notrace filter, skip it.
2961 */
2962static inline bool
2963ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2964{
2965 /* If ops isn't enabled, ignore it */
2966 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2967 return 0;
2968
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002969 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002970 if (ops_traces_mod(ops))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002971 return 1;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002972
2973 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002974 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002975 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002976 return 0;
2977
2978 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002979 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002980 return 0;
2981
2982 return 1;
2983}
2984
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002985static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002986{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002987 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002988 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002989 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002990 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002991 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002992 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002993
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002994 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002995
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002996 /*
2997 * When a module is loaded, this function is called to convert
2998 * the calls to mcount in its text to nops, and also to create
2999 * an entry in the ftrace data. Now, if ftrace is activated
3000 * after this call, but before the module sets its text to
3001 * read-only, the modification of enabling ftrace can fail if
3002 * the read-only is done while ftrace is converting the calls.
3003 * To prevent this, the module's records are set as disabled
3004 * and will be enabled after the call to set the module's text
3005 * to read-only.
3006 */
3007 if (mod)
3008 rec_flags |= FTRACE_FL_DISABLED;
3009
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003010 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05303011
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003012 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003013
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003014 /* If something went wrong, bail without enabling anything */
3015 if (unlikely(ftrace_disabled))
3016 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02003017
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003018 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003019 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05303020
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003021 /*
3022 * Do the initial record conversion from mcount jump
3023 * to the NOP instructions.
3024 */
3025 if (!ftrace_code_disable(mod, p))
3026 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003027
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003028 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003029 }
Steven Rostedt3d083392008-05-12 21:20:42 +02003030 }
3031
Ingo Molnar750ed1a2008-05-12 21:20:46 +02003032 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02003033 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003034 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02003035
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003036 return 0;
3037}
3038
Steven Rostedta7900872011-12-16 16:23:44 -05003039static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003040{
Steven Rostedta7900872011-12-16 16:23:44 -05003041 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003042 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003043
Steven Rostedta7900872011-12-16 16:23:44 -05003044 if (WARN_ON(!count))
3045 return -EINVAL;
3046
3047 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003048
3049 /*
Steven Rostedta7900872011-12-16 16:23:44 -05003050 * We want to fill as much as possible. No more than a page
3051 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003052 */
Steven Rostedta7900872011-12-16 16:23:44 -05003053 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3054 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003055
Steven Rostedta7900872011-12-16 16:23:44 -05003056 again:
3057 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3058
3059 if (!pg->records) {
3060 /* if we can't allocate this size, try something smaller */
3061 if (!order)
3062 return -ENOMEM;
3063 order >>= 1;
3064 goto again;
3065 }
3066
3067 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3068 pg->size = cnt;
3069
3070 if (cnt > count)
3071 cnt = count;
3072
3073 return cnt;
3074}
3075
3076static struct ftrace_page *
3077ftrace_allocate_pages(unsigned long num_to_init)
3078{
3079 struct ftrace_page *start_pg;
3080 struct ftrace_page *pg;
3081 int order;
3082 int cnt;
3083
3084 if (!num_to_init)
3085 return 0;
3086
3087 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3088 if (!pg)
3089 return NULL;
3090
3091 /*
3092 * Try to allocate as much as possible in one continues
3093 * location that fills in all of the space. We want to
3094 * waste as little space as possible.
3095 */
3096 for (;;) {
3097 cnt = ftrace_allocate_records(pg, num_to_init);
3098 if (cnt < 0)
3099 goto free_pages;
3100
3101 num_to_init -= cnt;
3102 if (!num_to_init)
3103 break;
3104
3105 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3106 if (!pg->next)
3107 goto free_pages;
3108
3109 pg = pg->next;
3110 }
3111
3112 return start_pg;
3113
3114 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003115 pg = start_pg;
3116 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003117 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3118 free_pages((unsigned long)pg->records, order);
3119 start_pg = pg->next;
3120 kfree(pg);
3121 pg = start_pg;
3122 }
3123 pr_info("ftrace: FAILED to allocate memory for functions\n");
3124 return NULL;
3125}
3126
Steven Rostedt5072c592008-05-12 21:20:43 +02003127#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3128
3129struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003130 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003131 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003132 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003133 struct ftrace_page *pg;
3134 struct dyn_ftrace *func;
3135 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003136 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003137 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003138 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003139 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003140 struct trace_array *tr;
3141 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003142 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003143 int idx;
3144 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003145};
3146
Ingo Molnare309b412008-05-12 21:20:51 +02003147static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003148t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003149{
3150 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003151 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003152 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003153 struct ftrace_hash *hash;
3154 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003155 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003156 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003157 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003158
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003159 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003160 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003161
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003162 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003163 return NULL;
3164
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003165 func_probes = &tr->func_probes;
3166 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003167 return NULL;
3168
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003169 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003170 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003171 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003172 }
3173
3174 if (iter->probe_entry)
3175 hnd = &iter->probe_entry->hlist;
3176
3177 hash = iter->probe->ops.func_hash->filter_hash;
3178 size = 1 << hash->size_bits;
3179
3180 retry:
3181 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003182 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003183 return NULL;
3184 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003185 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003186 hash = iter->probe->ops.func_hash->filter_hash;
3187 size = 1 << hash->size_bits;
3188 iter->pidx = 0;
3189 }
3190
3191 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003192
3193 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003194 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003195 hnd = NULL;
3196 goto retry;
3197 }
3198
3199 if (!hnd)
3200 hnd = hhd->first;
3201 else {
3202 hnd = hnd->next;
3203 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003204 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003205 goto retry;
3206 }
3207 }
3208
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003209 if (WARN_ON_ONCE(!hnd))
3210 return NULL;
3211
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003212 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003213
3214 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003215}
3216
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003217static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003218{
3219 struct ftrace_iterator *iter = m->private;
3220 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003221 loff_t l;
3222
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003223 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003224 return NULL;
3225
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003226 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003227 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003228
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003229 iter->probe = NULL;
3230 iter->probe_entry = NULL;
3231 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003232 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003233 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003234 if (!p)
3235 break;
3236 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003237 if (!p)
3238 return NULL;
3239
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003240 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003241 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003242
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003243 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003244}
3245
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003246static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003247t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003248{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003249 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003250 struct ftrace_probe_ops *probe_ops;
3251 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003252
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003253 probe = iter->probe;
3254 probe_entry = iter->probe_entry;
3255
3256 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003257 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003258
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003259 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003260
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003261 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003262 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003263
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003264 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3265 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003266
3267 return 0;
3268}
3269
3270static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003271t_mod_next(struct seq_file *m, loff_t *pos)
3272{
3273 struct ftrace_iterator *iter = m->private;
3274 struct trace_array *tr = iter->tr;
3275
3276 (*pos)++;
3277 iter->pos = *pos;
3278
3279 iter->mod_list = iter->mod_list->next;
3280
3281 if (iter->mod_list == &tr->mod_trace ||
3282 iter->mod_list == &tr->mod_notrace) {
3283 iter->flags &= ~FTRACE_ITER_MOD;
3284 return NULL;
3285 }
3286
3287 iter->mod_pos = *pos;
3288
3289 return iter;
3290}
3291
3292static void *t_mod_start(struct seq_file *m, loff_t *pos)
3293{
3294 struct ftrace_iterator *iter = m->private;
3295 void *p = NULL;
3296 loff_t l;
3297
3298 if (iter->func_pos > *pos)
3299 return NULL;
3300
3301 iter->mod_pos = iter->func_pos;
3302
3303 /* probes are only available if tr is set */
3304 if (!iter->tr)
3305 return NULL;
3306
3307 for (l = 0; l <= (*pos - iter->func_pos); ) {
3308 p = t_mod_next(m, &l);
3309 if (!p)
3310 break;
3311 }
3312 if (!p) {
3313 iter->flags &= ~FTRACE_ITER_MOD;
3314 return t_probe_start(m, pos);
3315 }
3316
3317 /* Only set this if we have an item */
3318 iter->flags |= FTRACE_ITER_MOD;
3319
3320 return iter;
3321}
3322
3323static int
3324t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3325{
3326 struct ftrace_mod_load *ftrace_mod;
3327 struct trace_array *tr = iter->tr;
3328
3329 if (WARN_ON_ONCE(!iter->mod_list) ||
3330 iter->mod_list == &tr->mod_trace ||
3331 iter->mod_list == &tr->mod_notrace)
3332 return -EIO;
3333
3334 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3335
3336 if (ftrace_mod->func)
3337 seq_printf(m, "%s", ftrace_mod->func);
3338 else
3339 seq_putc(m, '*');
3340
3341 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3342
3343 return 0;
3344}
3345
3346static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003347t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003348{
3349 struct ftrace_iterator *iter = m->private;
3350 struct dyn_ftrace *rec = NULL;
3351
3352 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003353
Steven Rostedt5072c592008-05-12 21:20:43 +02003354 retry:
3355 if (iter->idx >= iter->pg->index) {
3356 if (iter->pg->next) {
3357 iter->pg = iter->pg->next;
3358 iter->idx = 0;
3359 goto retry;
3360 }
3361 } else {
3362 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003363 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3364 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003365
3366 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003367 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003368
Steven Rostedt5072c592008-05-12 21:20:43 +02003369 rec = NULL;
3370 goto retry;
3371 }
3372 }
3373
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003374 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003375 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003376
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003377 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003378 iter->func = rec;
3379
3380 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003381}
3382
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003383static void *
3384t_next(struct seq_file *m, void *v, loff_t *pos)
3385{
3386 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003387 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003388 void *ret;
3389
3390 if (unlikely(ftrace_disabled))
3391 return NULL;
3392
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003393 if (iter->flags & FTRACE_ITER_PROBE)
3394 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003395
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003396 if (iter->flags & FTRACE_ITER_MOD)
3397 return t_mod_next(m, pos);
3398
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003399 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003400 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003401 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003402 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003403 }
3404
3405 ret = t_func_next(m, pos);
3406
3407 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003408 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003409
3410 return ret;
3411}
3412
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003413static void reset_iter_read(struct ftrace_iterator *iter)
3414{
3415 iter->pos = 0;
3416 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003417 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003418}
3419
3420static void *t_start(struct seq_file *m, loff_t *pos)
3421{
3422 struct ftrace_iterator *iter = m->private;
3423 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003424 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003425
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003426 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003427
3428 if (unlikely(ftrace_disabled))
3429 return NULL;
3430
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003431 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003432 * If an lseek was done, then reset and start from beginning.
3433 */
3434 if (*pos < iter->pos)
3435 reset_iter_read(iter);
3436
3437 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003438 * For set_ftrace_filter reading, if we have the filter
3439 * off, we can short cut and just print out that all
3440 * functions are enabled.
3441 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003442 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3443 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003444 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003445 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003446 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003447 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003448 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003449 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003450 return iter;
3451 }
3452
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003453 if (iter->flags & FTRACE_ITER_MOD)
3454 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003455
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003456 /*
3457 * Unfortunately, we need to restart at ftrace_pages_start
3458 * every time we let go of the ftrace_mutex. This is because
3459 * those pointers can change without the lock.
3460 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003461 iter->pg = ftrace_pages_start;
3462 iter->idx = 0;
3463 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003464 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003465 if (!p)
3466 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003467 }
walimis5821e1b2008-11-15 15:19:06 +08003468
Steven Rostedt69a30832011-12-19 15:21:16 -05003469 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003470 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003471
3472 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003473}
3474
3475static void t_stop(struct seq_file *m, void *p)
3476{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003477 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003478}
3479
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003480void * __weak
3481arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3482{
3483 return NULL;
3484}
3485
3486static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3487 struct dyn_ftrace *rec)
3488{
3489 void *ptr;
3490
3491 ptr = arch_ftrace_trampoline_func(ops, rec);
3492 if (ptr)
3493 seq_printf(m, " ->%pS", ptr);
3494}
3495
Steven Rostedt5072c592008-05-12 21:20:43 +02003496static int t_show(struct seq_file *m, void *v)
3497{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003498 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003499 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003500
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003501 if (iter->flags & FTRACE_ITER_PROBE)
3502 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003503
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003504 if (iter->flags & FTRACE_ITER_MOD)
3505 return t_mod_show(m, iter);
3506
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003507 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003508 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003509 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003510 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003511 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003512 return 0;
3513 }
3514
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003515 rec = iter->func;
3516
Steven Rostedt5072c592008-05-12 21:20:43 +02003517 if (!rec)
3518 return 0;
3519
Steven Rostedt647bcd02011-05-03 14:39:21 -04003520 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003521 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003522 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003523
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003524 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003525 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003526 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3527 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003528 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003529 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003530 if (ops) {
3531 do {
3532 seq_printf(m, "\ttramp: %pS (%pS)",
3533 (void *)ops->trampoline,
3534 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003535 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003536 ops = ftrace_find_tramp_ops_next(rec, ops);
3537 } while (ops);
3538 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003539 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003540 } else {
3541 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003542 }
3543 }
3544
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003545 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003546
3547 return 0;
3548}
3549
James Morris88e9d342009-09-22 16:43:43 -07003550static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003551 .start = t_start,
3552 .next = t_next,
3553 .stop = t_stop,
3554 .show = t_show,
3555};
3556
Ingo Molnare309b412008-05-12 21:20:51 +02003557static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003558ftrace_avail_open(struct inode *inode, struct file *file)
3559{
3560 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003561
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003562 if (unlikely(ftrace_disabled))
3563 return -ENODEV;
3564
Jiri Olsa50e18b92012-04-25 10:23:39 +02003565 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003566 if (!iter)
3567 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003568
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003569 iter->pg = ftrace_pages_start;
3570 iter->ops = &global_ops;
3571
3572 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003573}
3574
Steven Rostedt647bcd02011-05-03 14:39:21 -04003575static int
3576ftrace_enabled_open(struct inode *inode, struct file *file)
3577{
3578 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003579
Jiri Olsa50e18b92012-04-25 10:23:39 +02003580 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003581 if (!iter)
3582 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003583
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003584 iter->pg = ftrace_pages_start;
3585 iter->flags = FTRACE_ITER_ENABLED;
3586 iter->ops = &global_ops;
3587
3588 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003589}
3590
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003591/**
3592 * ftrace_regex_open - initialize function tracer filter files
3593 * @ops: The ftrace_ops that hold the hash filters
3594 * @flag: The type of filter to process
3595 * @inode: The inode, usually passed in to your open routine
3596 * @file: The file, usually passed in to your open routine
3597 *
3598 * ftrace_regex_open() initializes the filter files for the
3599 * @ops. Depending on @flag it may process the filter hash or
3600 * the notrace hash of @ops. With this called from the open
3601 * routine, you can use ftrace_filter_write() for the write
3602 * routine if @flag has FTRACE_ITER_FILTER set, or
3603 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003604 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003605 * release must call ftrace_regex_release().
3606 */
3607int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003608ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003609 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003610{
3611 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003612 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003613 struct list_head *mod_head;
3614 struct trace_array *tr = ops->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003615 int ret = 0;
3616
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003617 ftrace_ops_init(ops);
3618
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003619 if (unlikely(ftrace_disabled))
3620 return -ENODEV;
3621
Steven Rostedt5072c592008-05-12 21:20:43 +02003622 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3623 if (!iter)
3624 return -ENOMEM;
3625
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003626 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3627 kfree(iter);
3628 return -ENOMEM;
3629 }
3630
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003631 iter->ops = ops;
3632 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003633 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003634
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003635 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003636
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003637 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003638 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003639 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003640 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003641 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003642 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003643 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003644
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003645 iter->mod_list = mod_head;
3646
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003647 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003648 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3649
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003650 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003651 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003652 clear_ftrace_mod_list(mod_head);
3653 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003654 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003655 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003656
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003657 if (!iter->hash) {
3658 trace_parser_put(&iter->parser);
3659 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003660 ret = -ENOMEM;
3661 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003662 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003663 } else
3664 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003665
Steven Rostedt5072c592008-05-12 21:20:43 +02003666 if (file->f_mode & FMODE_READ) {
3667 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003668
3669 ret = seq_open(file, &show_ftrace_seq_ops);
3670 if (!ret) {
3671 struct seq_file *m = file->private_data;
3672 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003673 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003674 /* Failed */
3675 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003676 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003677 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003678 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003679 } else
3680 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003681
3682 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003683 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003684
3685 return ret;
3686}
3687
Steven Rostedt41c52c02008-05-22 11:46:33 -04003688static int
3689ftrace_filter_open(struct inode *inode, struct file *file)
3690{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003691 struct ftrace_ops *ops = inode->i_private;
3692
3693 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003694 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003695 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003696}
3697
3698static int
3699ftrace_notrace_open(struct inode *inode, struct file *file)
3700{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003701 struct ftrace_ops *ops = inode->i_private;
3702
3703 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003704 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003705}
3706
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003707/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3708struct ftrace_glob {
3709 char *search;
3710 unsigned len;
3711 int type;
3712};
3713
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003714/*
3715 * If symbols in an architecture don't correspond exactly to the user-visible
3716 * name of what they represent, it is possible to define this function to
3717 * perform the necessary adjustments.
3718*/
3719char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3720{
3721 return str;
3722}
3723
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003724static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003725{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003726 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003727 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003728
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003729 str = arch_ftrace_match_adjust(str, g->search);
3730
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003731 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003732 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003733 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003734 matched = 1;
3735 break;
3736 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003737 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003738 matched = 1;
3739 break;
3740 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003741 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003742 matched = 1;
3743 break;
3744 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003745 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003746 if (slen >= g->len &&
3747 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003748 matched = 1;
3749 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003750 case MATCH_GLOB:
3751 if (glob_match(g->search, str))
3752 matched = 1;
3753 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003754 }
3755
3756 return matched;
3757}
3758
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003759static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003760enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003761{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003762 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003763 int ret = 0;
3764
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003765 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003766 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003767 /* Do nothing if it doesn't exist */
3768 if (!entry)
3769 return 0;
3770
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003771 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003772 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003773 /* Do nothing if it exists */
3774 if (entry)
3775 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003776
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003777 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003778 }
3779 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003780}
3781
Steven Rostedt64e7c442009-02-13 17:08:48 -05003782static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003783ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3784 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003785{
3786 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003787 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003788
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003789 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3790
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003791 if (mod_g) {
3792 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3793
3794 /* blank module name to match all modules */
3795 if (!mod_g->len) {
3796 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003797 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003798 goto func_match;
3799 return 0;
3800 }
3801
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003802 /*
3803 * exclude_mod is set to trace everything but the given
3804 * module. If it is set and the module matches, then
3805 * return 0. If it is not set, and the module doesn't match
3806 * also return 0. Otherwise, check the function to see if
3807 * that matches.
3808 */
3809 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003810 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003811func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003812 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003813 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003814 return 1;
3815 }
3816
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003817 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003818}
3819
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003820static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003821match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003822{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003823 struct ftrace_page *pg;
3824 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003825 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003826 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3827 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3828 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003829 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003830 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03003831 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003832
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003833 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003834 func_g.type = filter_parse_regex(func, len, &func_g.search,
3835 &clear_filter);
3836 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003837 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003838
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003839 if (mod) {
3840 mod_g.type = filter_parse_regex(mod, strlen(mod),
3841 &mod_g.search, &exclude_mod);
3842 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003843 }
3844
Steven Rostedt52baf112009-02-14 01:15:39 -05003845 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003846
3847 if (unlikely(ftrace_disabled))
3848 goto out_unlock;
3849
Steven Rostedt265c8312009-02-13 12:43:56 -05003850 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003851
3852 if (rec->flags & FTRACE_FL_DISABLED)
3853 continue;
3854
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003855 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003856 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003857 if (ret < 0) {
3858 found = ret;
3859 goto out_unlock;
3860 }
Li Zefan311d16d2009-12-08 11:15:11 +08003861 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003862 }
3863 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003864 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003865 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003866
3867 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003868}
3869
Steven Rostedt64e7c442009-02-13 17:08:48 -05003870static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003871ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003872{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003873 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003874}
3875
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003876static void ftrace_ops_update_code(struct ftrace_ops *ops,
3877 struct ftrace_ops_hash *old_hash)
3878{
3879 struct ftrace_ops *op;
3880
3881 if (!ftrace_enabled)
3882 return;
3883
3884 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3885 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3886 return;
3887 }
3888
3889 /*
3890 * If this is the shared global_ops filter, then we need to
3891 * check if there is another ops that shares it, is enabled.
3892 * If so, we still need to run the modify code.
3893 */
3894 if (ops->func_hash != &global_ops.local_hash)
3895 return;
3896
3897 do_for_each_ftrace_op(op, ftrace_ops_list) {
3898 if (op->func_hash == &global_ops.local_hash &&
3899 op->flags & FTRACE_OPS_FL_ENABLED) {
3900 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3901 /* Only need to do this once */
3902 return;
3903 }
3904 } while_for_each_ftrace_op(op);
3905}
3906
3907static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3908 struct ftrace_hash **orig_hash,
3909 struct ftrace_hash *hash,
3910 int enable)
3911{
3912 struct ftrace_ops_hash old_hash_ops;
3913 struct ftrace_hash *old_hash;
3914 int ret;
3915
3916 old_hash = *orig_hash;
3917 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3918 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3919 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3920 if (!ret) {
3921 ftrace_ops_update_code(ops, &old_hash_ops);
3922 free_ftrace_hash_rcu(old_hash);
3923 }
3924 return ret;
3925}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003926
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003927static bool module_exists(const char *module)
3928{
3929 /* All modules have the symbol __this_module */
3930 const char this_mod[] = "__this_module";
3931 const int modname_size = MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 1;
3932 char modname[modname_size + 1];
3933 unsigned long val;
3934 int n;
3935
3936 n = snprintf(modname, modname_size + 1, "%s:%s", module, this_mod);
3937
3938 if (n > modname_size)
3939 return false;
3940
3941 val = module_kallsyms_lookup_name(modname);
3942 return val != 0;
3943}
3944
3945static int cache_mod(struct trace_array *tr,
3946 const char *func, char *module, int enable)
3947{
3948 struct ftrace_mod_load *ftrace_mod, *n;
3949 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3950 int ret;
3951
3952 mutex_lock(&ftrace_lock);
3953
3954 /* We do not cache inverse filters */
3955 if (func[0] == '!') {
3956 func++;
3957 ret = -EINVAL;
3958
3959 /* Look to remove this hash */
3960 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3961 if (strcmp(ftrace_mod->module, module) != 0)
3962 continue;
3963
3964 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03003965 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003966 (ftrace_mod->func &&
3967 strcmp(ftrace_mod->func, func) == 0)) {
3968 ret = 0;
3969 free_ftrace_mod(ftrace_mod);
3970 continue;
3971 }
3972 }
3973 goto out;
3974 }
3975
3976 ret = -EINVAL;
3977 /* We only care about modules that have not been loaded yet */
3978 if (module_exists(module))
3979 goto out;
3980
3981 /* Save this string off, and execute it when the module is loaded */
3982 ret = ftrace_add_mod(tr, func, module, enable);
3983 out:
3984 mutex_unlock(&ftrace_lock);
3985
3986 return ret;
3987}
3988
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003989static int
3990ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3991 int reset, int enable);
3992
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003993#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003994static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3995 char *mod, bool enable)
3996{
3997 struct ftrace_mod_load *ftrace_mod, *n;
3998 struct ftrace_hash **orig_hash, *new_hash;
3999 LIST_HEAD(process_mods);
4000 char *func;
4001 int ret;
4002
4003 mutex_lock(&ops->func_hash->regex_lock);
4004
4005 if (enable)
4006 orig_hash = &ops->func_hash->filter_hash;
4007 else
4008 orig_hash = &ops->func_hash->notrace_hash;
4009
4010 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4011 *orig_hash);
4012 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004013 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004014
4015 mutex_lock(&ftrace_lock);
4016
4017 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4018
4019 if (strcmp(ftrace_mod->module, mod) != 0)
4020 continue;
4021
4022 if (ftrace_mod->func)
4023 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4024 else
4025 func = kstrdup("*", GFP_KERNEL);
4026
4027 if (!func) /* warn? */
4028 continue;
4029
4030 list_del(&ftrace_mod->list);
4031 list_add(&ftrace_mod->list, &process_mods);
4032
4033 /* Use the newly allocated func, as it may be "*" */
4034 kfree(ftrace_mod->func);
4035 ftrace_mod->func = func;
4036 }
4037
4038 mutex_unlock(&ftrace_lock);
4039
4040 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4041
4042 func = ftrace_mod->func;
4043
4044 /* Grabs ftrace_lock, which is why we have this extra step */
4045 match_records(new_hash, func, strlen(func), mod);
4046 free_ftrace_mod(ftrace_mod);
4047 }
4048
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004049 if (enable && list_empty(head))
4050 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4051
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004052 mutex_lock(&ftrace_lock);
4053
4054 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4055 new_hash, enable);
4056 mutex_unlock(&ftrace_lock);
4057
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004058 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004059 mutex_unlock(&ops->func_hash->regex_lock);
4060
4061 free_ftrace_hash(new_hash);
4062}
4063
4064static void process_cached_mods(const char *mod_name)
4065{
4066 struct trace_array *tr;
4067 char *mod;
4068
4069 mod = kstrdup(mod_name, GFP_KERNEL);
4070 if (!mod)
4071 return;
4072
4073 mutex_lock(&trace_types_lock);
4074 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4075 if (!list_empty(&tr->mod_trace))
4076 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4077 if (!list_empty(&tr->mod_notrace))
4078 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4079 }
4080 mutex_unlock(&trace_types_lock);
4081
4082 kfree(mod);
4083}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004084#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004085
Steven Rostedtf6180772009-02-14 00:40:25 -05004086/*
4087 * We register the module command as a template to show others how
4088 * to register the a command as well.
4089 */
4090
4091static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004092ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004093 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004094{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004095 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004096 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004097
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004098 /* match_records() modifies func, and we need the original */
4099 func = kstrdup(func_orig, GFP_KERNEL);
4100 if (!func)
4101 return -ENOMEM;
4102
Steven Rostedtf6180772009-02-14 00:40:25 -05004103 /*
4104 * cmd == 'mod' because we only registered this func
4105 * for the 'mod' ftrace_func_command.
4106 * But if you register one func with multiple commands,
4107 * you can tell which command was used by the cmd
4108 * parameter.
4109 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004110 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004111 kfree(func);
4112
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004113 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004114 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004115 if (ret < 0)
4116 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004117 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004118}
4119
4120static struct ftrace_func_command ftrace_mod_cmd = {
4121 .name = "mod",
4122 .func = ftrace_mod_callback,
4123};
4124
4125static int __init ftrace_mod_cmd_init(void)
4126{
4127 return register_ftrace_command(&ftrace_mod_cmd);
4128}
Steven Rostedt6f415672012-10-05 12:13:07 -04004129core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004130
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004131static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004132 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004133{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004134 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004135 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004136
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004137 probe = container_of(op, struct ftrace_func_probe, ops);
4138 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004139
4140 /*
4141 * Disable preemption for these calls to prevent a RCU grace
4142 * period. This syncs the hash iteration and freeing of items
4143 * on the hash. rcu_read_lock is too dangerous here.
4144 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004145 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004146 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004147 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004148}
4149
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004150struct ftrace_func_map {
4151 struct ftrace_func_entry entry;
4152 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004153};
4154
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004155struct ftrace_func_mapper {
4156 struct ftrace_hash hash;
4157};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004158
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004159/**
4160 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4161 *
4162 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4163 */
4164struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004165{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004166 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004167
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004168 /*
4169 * The mapper is simply a ftrace_hash, but since the entries
4170 * in the hash are not ftrace_func_entry type, we define it
4171 * as a separate structure.
4172 */
4173 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4174 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004175}
4176
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004177/**
4178 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4179 * @mapper: The mapper that has the ip maps
4180 * @ip: the instruction pointer to find the data for
4181 *
4182 * Returns the data mapped to @ip if found otherwise NULL. The return
4183 * is actually the address of the mapper data pointer. The address is
4184 * returned for use cases where the data is no bigger than a long, and
4185 * the user can use the data pointer as its data instead of having to
4186 * allocate more memory for the reference.
4187 */
4188void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4189 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004190{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004191 struct ftrace_func_entry *entry;
4192 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004193
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004194 entry = ftrace_lookup_ip(&mapper->hash, ip);
4195 if (!entry)
4196 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004197
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004198 map = (struct ftrace_func_map *)entry;
4199 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004200}
4201
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004202/**
4203 * ftrace_func_mapper_add_ip - Map some data to an ip
4204 * @mapper: The mapper that has the ip maps
4205 * @ip: The instruction pointer address to map @data to
4206 * @data: The data to map to @ip
4207 *
4208 * Returns 0 on succes otherwise an error.
4209 */
4210int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4211 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004212{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004213 struct ftrace_func_entry *entry;
4214 struct ftrace_func_map *map;
4215
4216 entry = ftrace_lookup_ip(&mapper->hash, ip);
4217 if (entry)
4218 return -EBUSY;
4219
4220 map = kmalloc(sizeof(*map), GFP_KERNEL);
4221 if (!map)
4222 return -ENOMEM;
4223
4224 map->entry.ip = ip;
4225 map->data = data;
4226
4227 __add_hash_entry(&mapper->hash, &map->entry);
4228
4229 return 0;
4230}
4231
4232/**
4233 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4234 * @mapper: The mapper that has the ip maps
4235 * @ip: The instruction pointer address to remove the data from
4236 *
4237 * Returns the data if it is found, otherwise NULL.
4238 * Note, if the data pointer is used as the data itself, (see
4239 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4240 * if the data pointer was set to zero.
4241 */
4242void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4243 unsigned long ip)
4244{
4245 struct ftrace_func_entry *entry;
4246 struct ftrace_func_map *map;
4247 void *data;
4248
4249 entry = ftrace_lookup_ip(&mapper->hash, ip);
4250 if (!entry)
4251 return NULL;
4252
4253 map = (struct ftrace_func_map *)entry;
4254 data = map->data;
4255
4256 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004257 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004258
4259 return data;
4260}
4261
4262/**
4263 * free_ftrace_func_mapper - free a mapping of ips and data
4264 * @mapper: The mapper that has the ip maps
4265 * @free_func: A function to be called on each data item.
4266 *
4267 * This is used to free the function mapper. The @free_func is optional
4268 * and can be used if the data needs to be freed as well.
4269 */
4270void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4271 ftrace_mapper_func free_func)
4272{
4273 struct ftrace_func_entry *entry;
4274 struct ftrace_func_map *map;
4275 struct hlist_head *hhd;
4276 int size = 1 << mapper->hash.size_bits;
4277 int i;
4278
4279 if (free_func && mapper->hash.count) {
4280 for (i = 0; i < size; i++) {
4281 hhd = &mapper->hash.buckets[i];
4282 hlist_for_each_entry(entry, hhd, hlist) {
4283 map = (struct ftrace_func_map *)entry;
4284 free_func(map);
4285 }
4286 }
4287 }
4288 free_ftrace_hash(&mapper->hash);
4289}
4290
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004291static void release_probe(struct ftrace_func_probe *probe)
4292{
4293 struct ftrace_probe_ops *probe_ops;
4294
4295 mutex_lock(&ftrace_lock);
4296
4297 WARN_ON(probe->ref <= 0);
4298
4299 /* Subtract the ref that was used to protect this instance */
4300 probe->ref--;
4301
4302 if (!probe->ref) {
4303 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004304 /*
4305 * Sending zero as ip tells probe_ops to free
4306 * the probe->data itself
4307 */
4308 if (probe_ops->free)
4309 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004310 list_del(&probe->list);
4311 kfree(probe);
4312 }
4313 mutex_unlock(&ftrace_lock);
4314}
4315
4316static void acquire_probe_locked(struct ftrace_func_probe *probe)
4317{
4318 /*
4319 * Add one ref to keep it from being freed when releasing the
4320 * ftrace_lock mutex.
4321 */
4322 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004323}
4324
Steven Rostedt59df055f2009-02-14 15:29:06 -05004325int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004326register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004327 struct ftrace_probe_ops *probe_ops,
4328 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004329{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004330 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004331 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004332 struct ftrace_hash **orig_hash;
4333 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004334 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004335 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004336 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004337 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004338 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004339
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004340 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004341 return -EINVAL;
4342
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004343 /* We do not support '!' for function probes */
4344 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004345 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004346
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004347
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004348 mutex_lock(&ftrace_lock);
4349 /* Check if the probe_ops is already registered */
4350 list_for_each_entry(probe, &tr->func_probes, list) {
4351 if (probe->probe_ops == probe_ops)
4352 break;
4353 }
4354 if (&probe->list == &tr->func_probes) {
4355 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4356 if (!probe) {
4357 mutex_unlock(&ftrace_lock);
4358 return -ENOMEM;
4359 }
4360 probe->probe_ops = probe_ops;
4361 probe->ops.func = function_trace_probe_call;
4362 probe->tr = tr;
4363 ftrace_ops_init(&probe->ops);
4364 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004365 }
4366
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004367 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004368
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004369 mutex_unlock(&ftrace_lock);
4370
4371 mutex_lock(&probe->ops.func_hash->regex_lock);
4372
4373 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004374 old_hash = *orig_hash;
4375 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4376
4377 ret = ftrace_match_records(hash, glob, strlen(glob));
4378
4379 /* Nothing found? */
4380 if (!ret)
4381 ret = -EINVAL;
4382
4383 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004384 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004385
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004386 size = 1 << hash->size_bits;
4387 for (i = 0; i < size; i++) {
4388 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4389 if (ftrace_lookup_ip(old_hash, entry->ip))
4390 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004391 /*
4392 * The caller might want to do something special
4393 * for each function we find. We call the callback
4394 * to give the caller an opportunity to do so.
4395 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004396 if (probe_ops->init) {
4397 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004398 entry->ip, data,
4399 &probe->data);
4400 if (ret < 0) {
4401 if (probe_ops->free && count)
4402 probe_ops->free(probe_ops, tr,
4403 0, probe->data);
4404 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004405 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004406 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004407 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004408 count++;
4409 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004410 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004411
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004412 mutex_lock(&ftrace_lock);
4413
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004414 if (!count) {
4415 /* Nothing was added? */
4416 ret = -EINVAL;
4417 goto out_unlock;
4418 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004419
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004420 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4421 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004422 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004423 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004424
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004425 /* One ref for each new function traced */
4426 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004427
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004428 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4429 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004430
Steven Rostedt59df055f2009-02-14 15:29:06 -05004431 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004432 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004433
4434 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004435 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004436 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004437 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004438 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004439
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004440 release_probe(probe);
4441
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004442 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004443
4444 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004445 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004446 goto out_unlock;
4447
4448 /* Failed to do the move, need to call the free functions */
4449 for (i = 0; i < size; i++) {
4450 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4451 if (ftrace_lookup_ip(old_hash, entry->ip))
4452 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004453 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004454 }
4455 }
4456 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004457}
4458
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004459int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004460unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4461 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004462{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004463 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004464 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004465 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004466 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004467 struct ftrace_hash **orig_hash;
4468 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004469 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004470 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004471 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004472 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004473 int count = 0;
4474 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004475 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004476
Naveen N. Raocbab5672017-05-16 23:21:25 +05304477 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004478 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304479 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004480 int not;
4481
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004482 func_g.type = filter_parse_regex(glob, strlen(glob),
4483 &func_g.search, &not);
4484 func_g.len = strlen(func_g.search);
4485 func_g.search = glob;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004486
Steven Rostedtb6887d72009-02-17 12:32:04 -05004487 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004488 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004489 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004490 }
4491
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004492 mutex_lock(&ftrace_lock);
4493 /* Check if the probe_ops is already registered */
4494 list_for_each_entry(probe, &tr->func_probes, list) {
4495 if (probe->probe_ops == probe_ops)
4496 break;
4497 }
4498 if (&probe->list == &tr->func_probes)
4499 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004500
4501 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004502 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4503 goto err_unlock_ftrace;
4504
4505 acquire_probe_locked(probe);
4506
4507 mutex_unlock(&ftrace_lock);
4508
4509 mutex_lock(&probe->ops.func_hash->regex_lock);
4510
4511 orig_hash = &probe->ops.func_hash->filter_hash;
4512 old_hash = *orig_hash;
4513
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004514 if (ftrace_hash_empty(old_hash))
4515 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004516
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004517 old_hash_ops.filter_hash = old_hash;
4518 /* Probes only have filters */
4519 old_hash_ops.notrace_hash = NULL;
4520
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004521 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004522 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004523 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004524 goto out_unlock;
4525
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004526 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004527
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004528 size = 1 << hash->size_bits;
4529 for (i = 0; i < size; i++) {
4530 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004531
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004532 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004533 kallsyms_lookup(entry->ip, NULL, NULL,
4534 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004535 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004536 continue;
4537 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004538 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004539 remove_hash_entry(hash, entry);
4540 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004541 }
4542 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004543
4544 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004545 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004546 ret = -EINVAL;
4547 goto out_unlock;
4548 }
4549
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004550 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004551
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004552 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004553
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004554 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004555
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004556 if (ftrace_hash_empty(hash))
4557 ftrace_shutdown(&probe->ops, 0);
4558
4559 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004560 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004561
4562 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004563 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004564 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004565 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004566 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004567
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004568 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4569 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004570 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004571 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004572 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004573 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004574 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004575
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004576 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004577 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004578 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004579
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004580 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004581
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004582 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004583
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004584 err_unlock_ftrace:
4585 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004586 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004587}
4588
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304589void clear_ftrace_function_probes(struct trace_array *tr)
4590{
4591 struct ftrace_func_probe *probe, *n;
4592
4593 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4594 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4595}
4596
Steven Rostedtf6180772009-02-14 00:40:25 -05004597static LIST_HEAD(ftrace_commands);
4598static DEFINE_MUTEX(ftrace_cmd_mutex);
4599
Tom Zanussi38de93a2013-10-24 08:34:18 -05004600/*
4601 * Currently we only register ftrace commands from __init, so mark this
4602 * __init too.
4603 */
4604__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004605{
4606 struct ftrace_func_command *p;
4607 int ret = 0;
4608
4609 mutex_lock(&ftrace_cmd_mutex);
4610 list_for_each_entry(p, &ftrace_commands, list) {
4611 if (strcmp(cmd->name, p->name) == 0) {
4612 ret = -EBUSY;
4613 goto out_unlock;
4614 }
4615 }
4616 list_add(&cmd->list, &ftrace_commands);
4617 out_unlock:
4618 mutex_unlock(&ftrace_cmd_mutex);
4619
4620 return ret;
4621}
4622
Tom Zanussi38de93a2013-10-24 08:34:18 -05004623/*
4624 * Currently we only unregister ftrace commands from __init, so mark
4625 * this __init too.
4626 */
4627__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004628{
4629 struct ftrace_func_command *p, *n;
4630 int ret = -ENODEV;
4631
4632 mutex_lock(&ftrace_cmd_mutex);
4633 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4634 if (strcmp(cmd->name, p->name) == 0) {
4635 ret = 0;
4636 list_del_init(&p->list);
4637 goto out_unlock;
4638 }
4639 }
4640 out_unlock:
4641 mutex_unlock(&ftrace_cmd_mutex);
4642
4643 return ret;
4644}
4645
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004646static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004647 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004648{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004649 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004650 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004651 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004652 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004653 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004654
4655 func = strsep(&next, ":");
4656
4657 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004658 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004659 if (!ret)
4660 ret = -EINVAL;
4661 if (ret < 0)
4662 return ret;
4663 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004664 }
4665
Steven Rostedtf6180772009-02-14 00:40:25 -05004666 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004667
4668 command = strsep(&next, ":");
4669
Steven Rostedtf6180772009-02-14 00:40:25 -05004670 mutex_lock(&ftrace_cmd_mutex);
4671 list_for_each_entry(p, &ftrace_commands, list) {
4672 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004673 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004674 goto out_unlock;
4675 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004676 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004677 out_unlock:
4678 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004679
Steven Rostedtf6180772009-02-14 00:40:25 -05004680 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004681}
4682
Ingo Molnare309b412008-05-12 21:20:51 +02004683static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004684ftrace_regex_write(struct file *file, const char __user *ubuf,
4685 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004686{
4687 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004688 struct trace_parser *parser;
4689 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004690
Li Zefan4ba79782009-09-22 13:52:20 +08004691 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004692 return 0;
4693
Steven Rostedt5072c592008-05-12 21:20:43 +02004694 if (file->f_mode & FMODE_READ) {
4695 struct seq_file *m = file->private_data;
4696 iter = m->private;
4697 } else
4698 iter = file->private_data;
4699
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004700 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004701 return -ENODEV;
4702
4703 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004704
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004705 parser = &iter->parser;
4706 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004707
Li Zefan4ba79782009-09-22 13:52:20 +08004708 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004709 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004710 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004711 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004712 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004713 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004714 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004715 }
4716
Steven Rostedt5072c592008-05-12 21:20:43 +02004717 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004718 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004719 return ret;
4720}
4721
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004722ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004723ftrace_filter_write(struct file *file, const char __user *ubuf,
4724 size_t cnt, loff_t *ppos)
4725{
4726 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4727}
4728
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004729ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004730ftrace_notrace_write(struct file *file, const char __user *ubuf,
4731 size_t cnt, loff_t *ppos)
4732{
4733 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4734}
4735
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004736static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004737ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4738{
4739 struct ftrace_func_entry *entry;
4740
4741 if (!ftrace_location(ip))
4742 return -EINVAL;
4743
4744 if (remove) {
4745 entry = ftrace_lookup_ip(hash, ip);
4746 if (!entry)
4747 return -ENOENT;
4748 free_hash_entry(hash, entry);
4749 return 0;
4750 }
4751
4752 return add_hash_entry(hash, ip);
4753}
4754
4755static int
4756ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4757 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004758{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004759 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004760 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004761 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004762
Steven Rostedt41c52c02008-05-22 11:46:33 -04004763 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004764 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004765
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004766 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004767
Steven Rostedtf45948e2011-05-02 12:29:25 -04004768 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004769 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004770 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004771 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004772
Wang Nanb972cc52014-07-15 08:40:20 +08004773 if (reset)
4774 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4775 else
4776 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4777
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004778 if (!hash) {
4779 ret = -ENOMEM;
4780 goto out_regex_unlock;
4781 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004782
Jiri Olsaac483c42012-01-02 10:04:14 +01004783 if (buf && !ftrace_match_records(hash, buf, len)) {
4784 ret = -EINVAL;
4785 goto out_regex_unlock;
4786 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004787 if (ip) {
4788 ret = ftrace_match_addr(hash, ip, remove);
4789 if (ret < 0)
4790 goto out_regex_unlock;
4791 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004792
4793 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004794 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004795 mutex_unlock(&ftrace_lock);
4796
Jiri Olsaac483c42012-01-02 10:04:14 +01004797 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004798 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004799
4800 free_ftrace_hash(hash);
4801 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004802}
4803
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004804static int
4805ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4806 int reset, int enable)
4807{
4808 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4809}
4810
4811/**
4812 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4813 * @ops - the ops to set the filter with
4814 * @ip - the address to add to or remove from the filter.
4815 * @remove - non zero to remove the ip from the filter
4816 * @reset - non zero to reset all filters before applying this filter.
4817 *
4818 * Filters denote which functions should be enabled when tracing is enabled
4819 * If @ip is NULL, it failes to update filter.
4820 */
4821int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4822 int remove, int reset)
4823{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004824 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004825 return ftrace_set_addr(ops, ip, remove, reset, 1);
4826}
4827EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4828
Joel Fernandesd032ae82016-11-15 12:31:20 -08004829/**
4830 * ftrace_ops_set_global_filter - setup ops to use global filters
4831 * @ops - the ops which will use the global filters
4832 *
4833 * ftrace users who need global function trace filtering should call this.
4834 * It can set the global filter only if ops were not initialized before.
4835 */
4836void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4837{
4838 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4839 return;
4840
4841 ftrace_ops_init(ops);
4842 ops->func_hash = &global_ops.local_hash;
4843}
4844EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4845
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004846static int
4847ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4848 int reset, int enable)
4849{
4850 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4851}
4852
Steven Rostedt77a2b372008-05-12 21:20:45 +02004853/**
4854 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004855 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004856 * @buf - the string that holds the function filter text.
4857 * @len - the length of the string.
4858 * @reset - non zero to reset all filters before applying this filter.
4859 *
4860 * Filters denote which functions should be enabled when tracing is enabled.
4861 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4862 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004863int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004864 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004865{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004866 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004867 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004868}
Steven Rostedt936e0742011-05-05 22:54:01 -04004869EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004870
Steven Rostedt41c52c02008-05-22 11:46:33 -04004871/**
4872 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004873 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004874 * @buf - the string that holds the function notrace text.
4875 * @len - the length of the string.
4876 * @reset - non zero to reset all filters before applying this filter.
4877 *
4878 * Notrace Filters denote which functions should not be enabled when tracing
4879 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4880 * for tracing.
4881 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004882int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004883 int len, int reset)
4884{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004885 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004886 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004887}
4888EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4889/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004890 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004891 * @buf - the string that holds the function filter text.
4892 * @len - the length of the string.
4893 * @reset - non zero to reset all filters before applying this filter.
4894 *
4895 * Filters denote which functions should be enabled when tracing is enabled.
4896 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4897 */
4898void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4899{
4900 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4901}
4902EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4903
4904/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004905 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004906 * @buf - the string that holds the function notrace text.
4907 * @len - the length of the string.
4908 * @reset - non zero to reset all filters before applying this filter.
4909 *
4910 * Notrace Filters denote which functions should not be enabled when tracing
4911 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4912 * for tracing.
4913 */
4914void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004915{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004916 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004917}
Steven Rostedt936e0742011-05-05 22:54:01 -04004918EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004919
Steven Rostedt2af15d62009-05-28 13:37:24 -04004920/*
4921 * command line interface to allow users to set filters on boot up.
4922 */
4923#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4924static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4925static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4926
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004927/* Used by function selftest to not test if filter is set */
4928bool ftrace_filter_param __initdata;
4929
Steven Rostedt2af15d62009-05-28 13:37:24 -04004930static int __init set_ftrace_notrace(char *str)
4931{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004932 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004933 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004934 return 1;
4935}
4936__setup("ftrace_notrace=", set_ftrace_notrace);
4937
4938static int __init set_ftrace_filter(char *str)
4939{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004940 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004941 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004942 return 1;
4943}
4944__setup("ftrace_filter=", set_ftrace_filter);
4945
Stefan Assmann369bc182009-10-12 22:17:21 +02004946#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004947static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004948static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004949static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004950
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004951static unsigned long save_global_trampoline;
4952static unsigned long save_global_flags;
4953
Stefan Assmann369bc182009-10-12 22:17:21 +02004954static int __init set_graph_function(char *str)
4955{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004956 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004957 return 1;
4958}
4959__setup("ftrace_graph_filter=", set_graph_function);
4960
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004961static int __init set_graph_notrace_function(char *str)
4962{
4963 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4964 return 1;
4965}
4966__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4967
Todd Brandt65a50c652017-03-02 16:12:15 -08004968static int __init set_graph_max_depth_function(char *str)
4969{
4970 if (!str)
4971 return 0;
4972 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4973 return 1;
4974}
4975__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4976
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004977static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004978{
4979 int ret;
4980 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004981 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004982
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004983 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4984 if (WARN_ON(!hash))
4985 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004986
4987 while (buf) {
4988 func = strsep(&buf, ",");
4989 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004990 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004991 if (ret)
4992 printk(KERN_DEBUG "ftrace: function %s not "
4993 "traceable\n", func);
4994 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004995
4996 if (enable)
4997 ftrace_graph_hash = hash;
4998 else
4999 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02005000}
5001#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5002
Steven Rostedt2a85a372011-12-19 21:57:44 -05005003void __init
5004ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04005005{
5006 char *func;
5007
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005008 ftrace_ops_init(ops);
5009
Steven Rostedt2af15d62009-05-28 13:37:24 -04005010 while (buf) {
5011 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04005012 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005013 }
5014}
5015
5016static void __init set_ftrace_early_filters(void)
5017{
5018 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005019 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005020 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005021 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005022#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5023 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005024 set_ftrace_early_graph(ftrace_graph_buf, 1);
5025 if (ftrace_graph_notrace_buf[0])
5026 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005027#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04005028}
5029
Steven Rostedtfc13cb02011-12-19 14:41:25 -05005030int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02005031{
5032 struct seq_file *m = (struct seq_file *)file->private_data;
5033 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005034 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005035 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04005036 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005037 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02005038
Steven Rostedt5072c592008-05-12 21:20:43 +02005039 if (file->f_mode & FMODE_READ) {
5040 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02005041 seq_release(inode, file);
5042 } else
5043 iter = file->private_data;
5044
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005045 parser = &iter->parser;
5046 if (trace_parser_loaded(parser)) {
5047 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005048 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02005049 }
5050
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005051 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005052
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005053 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005054
Steven Rostedt058e2972011-04-29 22:35:33 -04005055 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04005056 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5057
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005058 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005059 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04005060 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005061 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5062 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005063 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005064
Steven Rostedt058e2972011-04-29 22:35:33 -04005065 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04005066 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5067 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04005068 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04005069 } else {
5070 /* For read only, the hash is the ops hash */
5071 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04005072 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005073
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005074 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005075 free_ftrace_hash(iter->hash);
5076 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04005077
Steven Rostedt5072c592008-05-12 21:20:43 +02005078 return 0;
5079}
5080
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005081static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005082 .open = ftrace_avail_open,
5083 .read = seq_read,
5084 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08005085 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02005086};
5087
Steven Rostedt647bcd02011-05-03 14:39:21 -04005088static const struct file_operations ftrace_enabled_fops = {
5089 .open = ftrace_enabled_open,
5090 .read = seq_read,
5091 .llseek = seq_lseek,
5092 .release = seq_release_private,
5093};
5094
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005095static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005096 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005097 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005098 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005099 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005100 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005101};
5102
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005103static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005104 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005105 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005106 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005107 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005108 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005109};
5110
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005111#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5112
5113static DEFINE_MUTEX(graph_lock);
5114
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005115struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5116struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5117
5118enum graph_filter_type {
5119 GRAPH_FILTER_NOTRACE = 0,
5120 GRAPH_FILTER_FUNCTION,
5121};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005122
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005123#define FTRACE_GRAPH_EMPTY ((void *)1)
5124
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005125struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005126 struct ftrace_hash *hash;
5127 struct ftrace_func_entry *entry;
5128 int idx; /* for hash table iteration */
5129 enum graph_filter_type type;
5130 struct ftrace_hash *new_hash;
5131 const struct seq_operations *seq_ops;
5132 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005133};
5134
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005135static void *
Li Zefan85951842009-06-24 09:54:00 +08005136__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005137{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005138 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005139 struct ftrace_func_entry *entry = fgd->entry;
5140 struct hlist_head *head;
5141 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005142
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005143 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005144 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005145
5146 if (entry) {
5147 hlist_for_each_entry_continue(entry, hlist) {
5148 fgd->entry = entry;
5149 return entry;
5150 }
5151
5152 idx++;
5153 }
5154
5155 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5156 head = &fgd->hash->buckets[i];
5157 hlist_for_each_entry(entry, head, hlist) {
5158 fgd->entry = entry;
5159 fgd->idx = i;
5160 return entry;
5161 }
5162 }
5163 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005164}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005165
Li Zefan85951842009-06-24 09:54:00 +08005166static void *
5167g_next(struct seq_file *m, void *v, loff_t *pos)
5168{
5169 (*pos)++;
5170 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005171}
5172
5173static void *g_start(struct seq_file *m, loff_t *pos)
5174{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005175 struct ftrace_graph_data *fgd = m->private;
5176
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005177 mutex_lock(&graph_lock);
5178
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005179 if (fgd->type == GRAPH_FILTER_FUNCTION)
5180 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5181 lockdep_is_held(&graph_lock));
5182 else
5183 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5184 lockdep_is_held(&graph_lock));
5185
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005186 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005187 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005188 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005189
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005190 fgd->idx = 0;
5191 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005192 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005193}
5194
5195static void g_stop(struct seq_file *m, void *p)
5196{
5197 mutex_unlock(&graph_lock);
5198}
5199
5200static int g_show(struct seq_file *m, void *v)
5201{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005202 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005203
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005204 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005205 return 0;
5206
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005207 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005208 struct ftrace_graph_data *fgd = m->private;
5209
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005210 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005211 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005212 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005213 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005214 return 0;
5215 }
5216
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005217 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005218
5219 return 0;
5220}
5221
James Morris88e9d342009-09-22 16:43:43 -07005222static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005223 .start = g_start,
5224 .next = g_next,
5225 .stop = g_stop,
5226 .show = g_show,
5227};
5228
5229static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005230__ftrace_graph_open(struct inode *inode, struct file *file,
5231 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005232{
5233 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005234 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005235
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005236 if (file->f_mode & FMODE_WRITE) {
5237 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5238
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005239 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5240 return -ENOMEM;
5241
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005242 if (file->f_flags & O_TRUNC)
5243 new_hash = alloc_ftrace_hash(size_bits);
5244 else
5245 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5246 fgd->hash);
5247 if (!new_hash) {
5248 ret = -ENOMEM;
5249 goto out;
5250 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005251 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005252
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005253 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005254 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005255 if (!ret) {
5256 struct seq_file *m = file->private_data;
5257 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005258 } else {
5259 /* Failed */
5260 free_ftrace_hash(new_hash);
5261 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005262 }
5263 } else
5264 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005265
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005266out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005267 if (ret < 0 && file->f_mode & FMODE_WRITE)
5268 trace_parser_put(&fgd->parser);
5269
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005270 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005271
5272 /*
5273 * All uses of fgd->hash must be taken with the graph_lock
5274 * held. The graph_lock is going to be released, so force
5275 * fgd->hash to be reinitialized when it is taken again.
5276 */
5277 fgd->hash = NULL;
5278
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005279 return ret;
5280}
5281
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005282static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005283ftrace_graph_open(struct inode *inode, struct file *file)
5284{
5285 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005286 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005287
5288 if (unlikely(ftrace_disabled))
5289 return -ENODEV;
5290
5291 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5292 if (fgd == NULL)
5293 return -ENOMEM;
5294
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005295 mutex_lock(&graph_lock);
5296
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005297 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5298 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005299 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005300 fgd->seq_ops = &ftrace_graph_seq_ops;
5301
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005302 ret = __ftrace_graph_open(inode, file, fgd);
5303 if (ret < 0)
5304 kfree(fgd);
5305
5306 mutex_unlock(&graph_lock);
5307 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005308}
5309
5310static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005311ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5312{
5313 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005314 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005315
5316 if (unlikely(ftrace_disabled))
5317 return -ENODEV;
5318
5319 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5320 if (fgd == NULL)
5321 return -ENOMEM;
5322
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005323 mutex_lock(&graph_lock);
5324
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005325 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5326 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005327 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005328 fgd->seq_ops = &ftrace_graph_seq_ops;
5329
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005330 ret = __ftrace_graph_open(inode, file, fgd);
5331 if (ret < 0)
5332 kfree(fgd);
5333
5334 mutex_unlock(&graph_lock);
5335 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005336}
5337
5338static int
Li Zefan87827112009-07-23 11:29:11 +08005339ftrace_graph_release(struct inode *inode, struct file *file)
5340{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005341 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005342 struct ftrace_hash *old_hash, *new_hash;
5343 struct trace_parser *parser;
5344 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005345
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005346 if (file->f_mode & FMODE_READ) {
5347 struct seq_file *m = file->private_data;
5348
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005349 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005350 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005351 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005352 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005353 }
5354
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005355
5356 if (file->f_mode & FMODE_WRITE) {
5357
5358 parser = &fgd->parser;
5359
5360 if (trace_parser_loaded((parser))) {
5361 parser->buffer[parser->idx] = 0;
5362 ret = ftrace_graph_set_hash(fgd->new_hash,
5363 parser->buffer);
5364 }
5365
5366 trace_parser_put(parser);
5367
5368 new_hash = __ftrace_hash_move(fgd->new_hash);
5369 if (!new_hash) {
5370 ret = -ENOMEM;
5371 goto out;
5372 }
5373
5374 mutex_lock(&graph_lock);
5375
5376 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5377 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5378 lockdep_is_held(&graph_lock));
5379 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5380 } else {
5381 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5382 lockdep_is_held(&graph_lock));
5383 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5384 }
5385
5386 mutex_unlock(&graph_lock);
5387
5388 /* Wait till all users are no longer using the old hash */
5389 synchronize_sched();
5390
5391 free_ftrace_hash(old_hash);
5392 }
5393
5394 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005395 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005396 kfree(fgd);
5397
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005398 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005399}
5400
5401static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005402ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005403{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005404 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005405 struct dyn_ftrace *rec;
5406 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005407 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005408 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005409 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005410
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005411 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005412 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5413 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005414
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005415 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005416
Steven Rostedt52baf112009-02-14 01:15:39 -05005417 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005418
5419 if (unlikely(ftrace_disabled)) {
5420 mutex_unlock(&ftrace_lock);
5421 return -ENODEV;
5422 }
5423
Steven Rostedt265c8312009-02-13 12:43:56 -05005424 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005425
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005426 if (rec->flags & FTRACE_FL_DISABLED)
5427 continue;
5428
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005429 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005430 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005431
5432 if (!not) {
5433 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005434
5435 if (entry)
5436 continue;
5437 if (add_hash_entry(hash, rec->ip) < 0)
5438 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005439 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005440 if (entry) {
5441 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005442 fail = 0;
5443 }
5444 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005445 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005446 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005447out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005448 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005449
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005450 if (fail)
5451 return -EINVAL;
5452
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005453 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005454}
5455
5456static ssize_t
5457ftrace_graph_write(struct file *file, const char __user *ubuf,
5458 size_t cnt, loff_t *ppos)
5459{
Namhyung Kim6a101082013-10-14 17:24:25 +09005460 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005461 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005462 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005463
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005464 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005465 return 0;
5466
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005467 /* Read mode uses seq functions */
5468 if (file->f_mode & FMODE_READ) {
5469 struct seq_file *m = file->private_data;
5470 fgd = m->private;
5471 }
5472
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005473 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005474
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005475 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005476
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005477 if (read >= 0 && trace_parser_loaded(parser) &&
5478 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005479
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005480 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005481 parser->buffer);
5482 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005483 }
5484
Namhyung Kim6a101082013-10-14 17:24:25 +09005485 if (!ret)
5486 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005487
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005488 return ret;
5489}
5490
5491static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005492 .open = ftrace_graph_open,
5493 .read = seq_read,
5494 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005495 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005496 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005497};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005498
5499static const struct file_operations ftrace_graph_notrace_fops = {
5500 .open = ftrace_graph_notrace_open,
5501 .read = seq_read,
5502 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005503 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005504 .release = ftrace_graph_release,
5505};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005506#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5507
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005508void ftrace_create_filter_files(struct ftrace_ops *ops,
5509 struct dentry *parent)
5510{
5511
5512 trace_create_file("set_ftrace_filter", 0644, parent,
5513 ops, &ftrace_filter_fops);
5514
5515 trace_create_file("set_ftrace_notrace", 0644, parent,
5516 ops, &ftrace_notrace_fops);
5517}
5518
5519/*
5520 * The name "destroy_filter_files" is really a misnomer. Although
5521 * in the future, it may actualy delete the files, but this is
5522 * really intended to make sure the ops passed in are disabled
5523 * and that when this function returns, the caller is free to
5524 * free the ops.
5525 *
5526 * The "destroy" name is only to match the "create" name that this
5527 * should be paired with.
5528 */
5529void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5530{
5531 mutex_lock(&ftrace_lock);
5532 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5533 ftrace_shutdown(ops, 0);
5534 ops->flags |= FTRACE_OPS_FL_DELETED;
5535 mutex_unlock(&ftrace_lock);
5536}
5537
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005538static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005539{
Steven Rostedt5072c592008-05-12 21:20:43 +02005540
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005541 trace_create_file("available_filter_functions", 0444,
5542 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005543
Steven Rostedt647bcd02011-05-03 14:39:21 -04005544 trace_create_file("enabled_functions", 0444,
5545 d_tracer, NULL, &ftrace_enabled_fops);
5546
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005547 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005548
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005549#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005550 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005551 NULL,
5552 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005553 trace_create_file("set_graph_notrace", 0444, d_tracer,
5554 NULL,
5555 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005556#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5557
Steven Rostedt5072c592008-05-12 21:20:43 +02005558 return 0;
5559}
5560
Steven Rostedt9fd49322012-04-24 22:32:06 -04005561static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005562{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005563 const unsigned long *ipa = a;
5564 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005565
Steven Rostedt9fd49322012-04-24 22:32:06 -04005566 if (*ipa > *ipb)
5567 return 1;
5568 if (*ipa < *ipb)
5569 return -1;
5570 return 0;
5571}
5572
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005573static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005574 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005575 unsigned long *end)
5576{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005577 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005578 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005579 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005580 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005581 unsigned long *p;
5582 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005583 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005584 int ret = -ENOMEM;
5585
5586 count = end - start;
5587
5588 if (!count)
5589 return 0;
5590
Steven Rostedt9fd49322012-04-24 22:32:06 -04005591 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005592 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005593
Steven Rostedt706c81f2012-04-24 23:45:26 -04005594 start_pg = ftrace_allocate_pages(count);
5595 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005596 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005597
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005598 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005599
Steven Rostedt320823092011-12-16 14:42:37 -05005600 /*
5601 * Core and each module needs their own pages, as
5602 * modules will free them when they are removed.
5603 * Force a new page to be allocated for modules.
5604 */
Steven Rostedta7900872011-12-16 16:23:44 -05005605 if (!mod) {
5606 WARN_ON(ftrace_pages || ftrace_pages_start);
5607 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005608 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005609 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005610 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005611 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005612
Steven Rostedta7900872011-12-16 16:23:44 -05005613 if (WARN_ON(ftrace_pages->next)) {
5614 /* Hmm, we have free pages? */
5615 while (ftrace_pages->next)
5616 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005617 }
Steven Rostedta7900872011-12-16 16:23:44 -05005618
Steven Rostedt706c81f2012-04-24 23:45:26 -04005619 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005620 }
5621
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005622 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005623 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005624 while (p < end) {
5625 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005626 /*
5627 * Some architecture linkers will pad between
5628 * the different mcount_loc sections of different
5629 * object files to satisfy alignments.
5630 * Skip any NULL pointers.
5631 */
5632 if (!addr)
5633 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005634
5635 if (pg->index == pg->size) {
5636 /* We should have allocated enough */
5637 if (WARN_ON(!pg->next))
5638 break;
5639 pg = pg->next;
5640 }
5641
5642 rec = &pg->records[pg->index++];
5643 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005644 }
5645
Steven Rostedt706c81f2012-04-24 23:45:26 -04005646 /* We should have used all pages */
5647 WARN_ON(pg->next);
5648
5649 /* Assign the last page to ftrace_pages */
5650 ftrace_pages = pg;
5651
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005652 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005653 * We only need to disable interrupts on start up
5654 * because we are modifying code that an interrupt
5655 * may execute, and the modification is not atomic.
5656 * But for modules, nothing runs the code we modify
5657 * until we are finished with it, and there's no
5658 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005659 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005660 if (!mod)
5661 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005662 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005663 if (!mod)
5664 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005665 ret = 0;
5666 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005667 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005668
Steven Rostedta7900872011-12-16 16:23:44 -05005669 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005670}
5671
Steven Rostedt93eb6772009-04-15 13:24:06 -04005672#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005673
5674#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5675
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005676static int referenced_filters(struct dyn_ftrace *rec)
5677{
5678 struct ftrace_ops *ops;
5679 int cnt = 0;
5680
5681 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5682 if (ops_references_rec(ops, rec))
5683 cnt++;
5684 }
5685
5686 return cnt;
5687}
5688
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005689void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005690{
5691 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005692 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005693 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005694 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005695
Steven Rostedt93eb6772009-04-15 13:24:06 -04005696 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005697
5698 if (ftrace_disabled)
5699 goto out_unlock;
5700
Steven Rostedt320823092011-12-16 14:42:37 -05005701 /*
5702 * Each module has its own ftrace_pages, remove
5703 * them from the list.
5704 */
5705 last_pg = &ftrace_pages_start;
5706 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5707 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005708 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005709 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005710 * As core pages are first, the first
5711 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005712 */
Steven Rostedt320823092011-12-16 14:42:37 -05005713 if (WARN_ON(pg == ftrace_pages_start))
5714 goto out_unlock;
5715
5716 /* Check if we are deleting the last page */
5717 if (pg == ftrace_pages)
5718 ftrace_pages = next_to_ftrace_page(last_pg);
5719
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04005720 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05005721 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05005722 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5723 free_pages((unsigned long)pg->records, order);
5724 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05005725 } else
5726 last_pg = &pg->next;
5727 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005728 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005729 mutex_unlock(&ftrace_lock);
5730}
5731
Jessica Yu7dcd1822016-02-16 17:32:33 -05005732void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005733{
5734 struct dyn_ftrace *rec;
5735 struct ftrace_page *pg;
5736
5737 mutex_lock(&ftrace_lock);
5738
5739 if (ftrace_disabled)
5740 goto out_unlock;
5741
5742 /*
5743 * If the tracing is enabled, go ahead and enable the record.
5744 *
5745 * The reason not to enable the record immediatelly is the
5746 * inherent check of ftrace_make_nop/ftrace_make_call for
5747 * correct previous instructions. Making first the NOP
5748 * conversion puts the module to the correct state, thus
5749 * passing the ftrace_make_call check.
5750 *
5751 * We also delay this to after the module code already set the
5752 * text to read-only, as we now need to set it back to read-write
5753 * so that we can modify the text.
5754 */
5755 if (ftrace_start_up)
5756 ftrace_arch_code_modify_prepare();
5757
5758 do_for_each_ftrace_rec(pg, rec) {
5759 int cnt;
5760 /*
5761 * do_for_each_ftrace_rec() is a double loop.
5762 * module text shares the pg. If a record is
5763 * not part of this module, then skip this pg,
5764 * which the "break" will do.
5765 */
5766 if (!within_module_core(rec->ip, mod))
5767 break;
5768
5769 cnt = 0;
5770
5771 /*
5772 * When adding a module, we need to check if tracers are
5773 * currently enabled and if they are, and can trace this record,
5774 * we need to enable the module functions as well as update the
5775 * reference counts for those function records.
5776 */
5777 if (ftrace_start_up)
5778 cnt += referenced_filters(rec);
5779
5780 /* This clears FTRACE_FL_DISABLED */
5781 rec->flags = cnt;
5782
5783 if (ftrace_start_up && cnt) {
5784 int failed = __ftrace_replace_code(rec, 1);
5785 if (failed) {
5786 ftrace_bug(failed, rec);
5787 goto out_loop;
5788 }
5789 }
5790
5791 } while_for_each_ftrace_rec();
5792
5793 out_loop:
5794 if (ftrace_start_up)
5795 ftrace_arch_code_modify_post_process();
5796
5797 out_unlock:
5798 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04005799
5800 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005801}
5802
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005803void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005804{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005805 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005806 return;
5807
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005808 ftrace_process_locs(mod, mod->ftrace_callsites,
5809 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005810}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005811#endif /* CONFIG_MODULES */
5812
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -04005813void __init ftrace_free_init_mem(void)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005814{
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -04005815 unsigned long start = (unsigned long)(&__init_begin);
5816 unsigned long end = (unsigned long)(&__init_end);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005817 struct ftrace_page **last_pg = &ftrace_pages_start;
5818 struct ftrace_page *pg;
5819 struct dyn_ftrace *rec;
5820 struct dyn_ftrace key;
5821 int order;
5822
5823 key.ip = start;
5824 key.flags = end; /* overload flags, as it is unsigned long */
5825
5826 mutex_lock(&ftrace_lock);
5827
5828 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
5829 if (end < pg->records[0].ip ||
5830 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
5831 continue;
5832 again:
5833 rec = bsearch(&key, pg->records, pg->index,
5834 sizeof(struct dyn_ftrace),
5835 ftrace_cmp_recs);
5836 if (!rec)
5837 continue;
5838 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04005839 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005840 if (!pg->index) {
5841 *last_pg = pg->next;
5842 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5843 free_pages((unsigned long)pg->records, order);
5844 kfree(pg);
5845 pg = container_of(last_pg, struct ftrace_page, next);
5846 if (!(*last_pg))
5847 ftrace_pages = pg;
5848 continue;
5849 }
5850 memmove(rec, rec + 1,
5851 (pg->index - (rec - pg->records)) * sizeof(*rec));
5852 /* More than one function may be in this block */
5853 goto again;
5854 }
5855 mutex_unlock(&ftrace_lock);
5856}
5857
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005858void __init ftrace_init(void)
5859{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005860 extern unsigned long __start_mcount_loc[];
5861 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005862 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005863 int ret;
5864
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005865 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005866 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005867 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01005868 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005869 goto failed;
5870
5871 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005872 if (!count) {
5873 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005874 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005875 }
5876
5877 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5878 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005879
5880 last_ftrace_enabled = ftrace_enabled = 1;
5881
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005882 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005883 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005884 __stop_mcount_loc);
5885
Steven Rostedt2af15d62009-05-28 13:37:24 -04005886 set_ftrace_early_filters();
5887
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005888 return;
5889 failed:
5890 ftrace_disabled = 1;
5891}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005892
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005893/* Do nothing if arch does not support this */
5894void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5895{
5896}
5897
5898static void ftrace_update_trampoline(struct ftrace_ops *ops)
5899{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005900 arch_ftrace_update_trampoline(ops);
5901}
5902
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04005903void ftrace_init_trace_array(struct trace_array *tr)
5904{
5905 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04005906 INIT_LIST_HEAD(&tr->mod_trace);
5907 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04005908}
Steven Rostedt3d083392008-05-12 21:20:42 +02005909#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005910
Steven Rostedt2b499382011-05-03 22:49:52 -04005911static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005912 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005913 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5914 FTRACE_OPS_FL_INITIALIZED |
5915 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005916};
5917
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005918static int __init ftrace_nodyn_init(void)
5919{
5920 ftrace_enabled = 1;
5921 return 0;
5922}
Steven Rostedt6f415672012-10-05 12:13:07 -04005923core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005924
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005925static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005926static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005927static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005928/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005929# define ftrace_startup(ops, command) \
5930 ({ \
5931 int ___ret = __register_ftrace_function(ops); \
5932 if (!___ret) \
5933 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5934 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005935 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005936# define ftrace_shutdown(ops, command) \
5937 ({ \
5938 int ___ret = __unregister_ftrace_function(ops); \
5939 if (!___ret) \
5940 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5941 ___ret; \
5942 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005943
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005944# define ftrace_startup_sysctl() do { } while (0)
5945# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005946
5947static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005948ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005949{
5950 return 1;
5951}
5952
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005953static void ftrace_update_trampoline(struct ftrace_ops *ops)
5954{
5955}
5956
Steven Rostedt3d083392008-05-12 21:20:42 +02005957#endif /* CONFIG_DYNAMIC_FTRACE */
5958
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005959__init void ftrace_init_global_array_ops(struct trace_array *tr)
5960{
5961 tr->ops = &global_ops;
5962 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04005963 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005964}
5965
5966void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5967{
5968 /* If we filter on pids, update to use the pid function */
5969 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5970 if (WARN_ON(tr->ops->func != ftrace_stub))
5971 printk("ftrace ops had %pS for function\n",
5972 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005973 }
5974 tr->ops->func = func;
5975 tr->ops->private = tr;
5976}
5977
5978void ftrace_reset_array_ops(struct trace_array *tr)
5979{
5980 tr->ops->func = ftrace_stub;
5981}
5982
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005983static inline void
5984__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005985 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005986{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005987 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005988 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005989
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005990 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5991 if (bit < 0)
5992 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005993
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005994 /*
5995 * Some of the ops may be dynamically allocated,
5996 * they must be freed after a synchronize_sched().
5997 */
5998 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005999
Steven Rostedt0a016402012-11-02 17:03:03 -04006000 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006001 /*
6002 * Check the following for each ops before calling their func:
6003 * if RCU flag is set, then rcu_is_watching() must be true
6004 * if PER_CPU is set, then ftrace_function_local_disable()
6005 * must be false
6006 * Otherwise test if the ip matches the ops filter
6007 *
6008 * If any of the above fails then the op->func() is not executed.
6009 */
6010 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6011 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
6012 !ftrace_function_local_disabled(op)) &&
6013 ftrace_ops_test(op, ip, regs)) {
6014
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04006015 if (FTRACE_WARN_ON(!op->func)) {
6016 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006017 goto out;
6018 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04006019 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006020 }
Steven Rostedt0a016402012-11-02 17:03:03 -04006021 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006022out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006023 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006024 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04006025}
6026
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006027/*
6028 * Some archs only support passing ip and parent_ip. Even though
6029 * the list function ignores the op parameter, we do not want any
6030 * C side effects, where a function is called without the caller
6031 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006032 * Archs are to support both the regs and ftrace_ops at the same time.
6033 * If they support ftrace_ops, it is assumed they support regs.
6034 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09006035 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6036 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006037 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08006038 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006039 */
6040#if ARCH_SUPPORTS_FTRACE_OPS
6041static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006042 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006043{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006044 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006045}
6046#else
6047static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6048{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006049 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006050}
6051#endif
6052
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006053/*
6054 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006055 * recursion, needs RCU protection and/or requires per cpu handling, then
6056 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006057 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006058static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006059 struct ftrace_ops *op, struct pt_regs *regs)
6060{
6061 int bit;
6062
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006063 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6064 return;
6065
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006066 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6067 if (bit < 0)
6068 return;
6069
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006070 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006071
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006072 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
6073 !ftrace_function_local_disabled(op)) {
6074 op->func(ip, parent_ip, op, regs);
6075 }
6076
6077 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006078 trace_clear_recursion(bit);
6079}
6080
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006081/**
6082 * ftrace_ops_get_func - get the function a trampoline should call
6083 * @ops: the ops to get the function for
6084 *
6085 * Normally the mcount trampoline will call the ops->func, but there
6086 * are times that it should not. For example, if the ops does not
6087 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08006088 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006089 *
6090 * Returns the function that the trampoline should call for @ops.
6091 */
6092ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6093{
6094 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006095 * If the function does not handle recursion, needs to be RCU safe,
6096 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006097 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006098 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6099 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
6100 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006101
6102 return ops->func;
6103}
6104
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006105static void
6106ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6107 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05006108{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006109 struct trace_array *tr = data;
6110 struct trace_pid_list *pid_list;
6111
6112 pid_list = rcu_dereference_sched(tr->function_pids);
6113
6114 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6115 trace_ignore_this_task(pid_list, next));
6116}
6117
Namhyung Kim1e104862017-04-17 11:44:28 +09006118static void
6119ftrace_pid_follow_sched_process_fork(void *data,
6120 struct task_struct *self,
6121 struct task_struct *task)
6122{
6123 struct trace_pid_list *pid_list;
6124 struct trace_array *tr = data;
6125
6126 pid_list = rcu_dereference_sched(tr->function_pids);
6127 trace_filter_add_remove_task(pid_list, self, task);
6128}
6129
6130static void
6131ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6132{
6133 struct trace_pid_list *pid_list;
6134 struct trace_array *tr = data;
6135
6136 pid_list = rcu_dereference_sched(tr->function_pids);
6137 trace_filter_add_remove_task(pid_list, NULL, task);
6138}
6139
6140void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6141{
6142 if (enable) {
6143 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6144 tr);
6145 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6146 tr);
6147 } else {
6148 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6149 tr);
6150 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6151 tr);
6152 }
6153}
6154
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006155static void clear_ftrace_pids(struct trace_array *tr)
6156{
6157 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05006158 int cpu;
6159
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006160 pid_list = rcu_dereference_protected(tr->function_pids,
6161 lockdep_is_held(&ftrace_lock));
6162 if (!pid_list)
6163 return;
6164
6165 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6166
6167 for_each_possible_cpu(cpu)
6168 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6169
6170 rcu_assign_pointer(tr->function_pids, NULL);
6171
6172 /* Wait till all users are no longer using pid filtering */
6173 synchronize_sched();
6174
6175 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05006176}
6177
Namhyung Kimd879d0b2017-04-17 11:44:27 +09006178void ftrace_clear_pids(struct trace_array *tr)
6179{
6180 mutex_lock(&ftrace_lock);
6181
6182 clear_ftrace_pids(tr);
6183
6184 mutex_unlock(&ftrace_lock);
6185}
6186
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006187static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05006188{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006189 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006190 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006191
6192 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006193 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006194
6195 mutex_unlock(&ftrace_lock);
6196}
6197
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006198/* Greater than any max PID */
6199#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6200
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006201static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006202 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006203{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006204 struct trace_pid_list *pid_list;
6205 struct trace_array *tr = m->private;
6206
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006207 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006208 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006209
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006210 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006211
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006212 if (!pid_list)
6213 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6214
6215 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006216}
6217
6218static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6219{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006220 struct trace_array *tr = m->private;
6221 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6222
6223 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006224 return NULL;
6225
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006226 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006227}
6228
6229static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006230 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006231{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006232 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006233 mutex_unlock(&ftrace_lock);
6234}
6235
6236static int fpid_show(struct seq_file *m, void *v)
6237{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006238 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006239 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006240 return 0;
6241 }
6242
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006243 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006244}
6245
6246static const struct seq_operations ftrace_pid_sops = {
6247 .start = fpid_start,
6248 .next = fpid_next,
6249 .stop = fpid_stop,
6250 .show = fpid_show,
6251};
6252
6253static int
6254ftrace_pid_open(struct inode *inode, struct file *file)
6255{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006256 struct trace_array *tr = inode->i_private;
6257 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006258 int ret = 0;
6259
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006260 if (trace_array_get(tr) < 0)
6261 return -ENODEV;
6262
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006263 if ((file->f_mode & FMODE_WRITE) &&
6264 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006265 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006266
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006267 ret = seq_open(file, &ftrace_pid_sops);
6268 if (ret < 0) {
6269 trace_array_put(tr);
6270 } else {
6271 m = file->private_data;
6272 /* copy tr over to seq ops */
6273 m->private = tr;
6274 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006275
6276 return ret;
6277}
6278
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006279static void ignore_task_cpu(void *data)
6280{
6281 struct trace_array *tr = data;
6282 struct trace_pid_list *pid_list;
6283
6284 /*
6285 * This function is called by on_each_cpu() while the
6286 * event_mutex is held.
6287 */
6288 pid_list = rcu_dereference_protected(tr->function_pids,
6289 mutex_is_locked(&ftrace_lock));
6290
6291 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6292 trace_ignore_this_task(pid_list, current));
6293}
6294
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006295static ssize_t
6296ftrace_pid_write(struct file *filp, const char __user *ubuf,
6297 size_t cnt, loff_t *ppos)
6298{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006299 struct seq_file *m = filp->private_data;
6300 struct trace_array *tr = m->private;
6301 struct trace_pid_list *filtered_pids = NULL;
6302 struct trace_pid_list *pid_list;
6303 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006304
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006305 if (!cnt)
6306 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006307
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006308 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006309
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006310 filtered_pids = rcu_dereference_protected(tr->function_pids,
6311 lockdep_is_held(&ftrace_lock));
6312
6313 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6314 if (ret < 0)
6315 goto out;
6316
6317 rcu_assign_pointer(tr->function_pids, pid_list);
6318
6319 if (filtered_pids) {
6320 synchronize_sched();
6321 trace_free_pid_list(filtered_pids);
6322 } else if (pid_list) {
6323 /* Register a probe to set whether to ignore the tracing of a task */
6324 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6325 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006326
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006327 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006328 * Ignoring of pids is done at task switch. But we have to
6329 * check for those tasks that are currently running.
6330 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006331 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006332 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006333
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006334 ftrace_update_pid_func();
6335 ftrace_startup_all(0);
6336 out:
6337 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006338
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006339 if (ret > 0)
6340 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05006341
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006342 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006343}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006344
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006345static int
6346ftrace_pid_release(struct inode *inode, struct file *file)
6347{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006348 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006349
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006350 trace_array_put(tr);
6351
6352 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006353}
6354
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006355static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006356 .open = ftrace_pid_open,
6357 .write = ftrace_pid_write,
6358 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006359 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006360 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006361};
6362
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006363void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006364{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006365 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006366 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006367}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006368
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04006369void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6370 struct dentry *d_tracer)
6371{
6372 /* Only the top level directory has the dyn_tracefs and profile */
6373 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6374
6375 ftrace_init_dyn_tracefs(d_tracer);
6376 ftrace_profile_tracefs(d_tracer);
6377}
6378
Steven Rostedt3d083392008-05-12 21:20:42 +02006379/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006380 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006381 *
6382 * This function should be used by panic code. It stops ftrace
6383 * but in a not so nice way. If you need to simply kill ftrace
6384 * from a non-atomic section, use ftrace_kill.
6385 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006386void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006387{
6388 ftrace_disabled = 1;
6389 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006390 clear_ftrace_function();
6391}
6392
6393/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04006394 * Test if ftrace is dead or not.
6395 */
6396int ftrace_is_dead(void)
6397{
6398 return ftrace_disabled;
6399}
6400
6401/**
Steven Rostedt3d083392008-05-12 21:20:42 +02006402 * register_ftrace_function - register a function for profiling
6403 * @ops - ops structure that holds the function for profiling.
6404 *
6405 * Register a function to be called by all functions in the
6406 * kernel.
6407 *
6408 * Note: @ops->func and all the functions it calls must be labeled
6409 * with "notrace", otherwise it will go into a
6410 * recursive loop.
6411 */
6412int register_ftrace_function(struct ftrace_ops *ops)
6413{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006414 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006415
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09006416 ftrace_ops_init(ops);
6417
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006418 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006419
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006420 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04006421
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006422 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02006423
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006424 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02006425}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006426EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02006427
6428/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01006429 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02006430 * @ops - ops structure that holds the function to unregister
6431 *
6432 * Unregister a function that was added to be called by ftrace profiling.
6433 */
6434int unregister_ftrace_function(struct ftrace_ops *ops)
6435{
6436 int ret;
6437
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006438 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006439 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006440 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006441
6442 return ret;
6443}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006444EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006445
Ingo Molnare309b412008-05-12 21:20:51 +02006446int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006447ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006448 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006449 loff_t *ppos)
6450{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006451 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006452
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006453 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006454
Steven Rostedt45a4a232011-04-21 23:16:46 -04006455 if (unlikely(ftrace_disabled))
6456 goto out;
6457
6458 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006459
Li Zefana32c7762009-06-26 16:55:51 +08006460 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006461 goto out;
6462
Li Zefana32c7762009-06-26 16:55:51 +08006463 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006464
6465 if (ftrace_enabled) {
6466
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006467 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08006468 if (rcu_dereference_protected(ftrace_ops_list,
6469 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01006470 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006471
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006472 ftrace_startup_sysctl();
6473
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006474 } else {
6475 /* stopping ftrace calls (just send to ftrace_stub) */
6476 ftrace_trace_function = ftrace_stub;
6477
6478 ftrace_shutdown_sysctl();
6479 }
6480
6481 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006482 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006483 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006484}
Ingo Molnarf17845e2008-10-24 12:47:10 +02006485
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006486#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006487
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006488static struct ftrace_ops graph_ops = {
6489 .func = ftrace_stub,
6490 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6491 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006492 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006493 FTRACE_OPS_FL_STUB,
6494#ifdef FTRACE_GRAPH_TRAMP_ADDR
6495 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05006496 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006497#endif
6498 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6499};
6500
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006501void ftrace_graph_sleep_time_control(bool enable)
6502{
6503 fgraph_sleep_time = enable;
6504}
6505
6506void ftrace_graph_graph_time_control(bool enable)
6507{
6508 fgraph_graph_time = enable;
6509}
6510
Steven Rostedte49dc192008-12-02 23:50:05 -05006511int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6512{
6513 return 0;
6514}
6515
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006516/* The callbacks that hook a function */
6517trace_func_graph_ret_t ftrace_graph_return =
6518 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006519trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006520static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006521
6522/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6523static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6524{
6525 int i;
6526 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006527 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6528 struct task_struct *g, *t;
6529
6530 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6531 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
6532 * sizeof(struct ftrace_ret_stack),
6533 GFP_KERNEL);
6534 if (!ret_stack_list[i]) {
6535 start = 0;
6536 end = i;
6537 ret = -ENOMEM;
6538 goto free;
6539 }
6540 }
6541
Soumya PN6112a302016-05-17 21:31:14 +05306542 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006543 do_each_thread(g, t) {
6544 if (start == end) {
6545 ret = -EAGAIN;
6546 goto unlock;
6547 }
6548
6549 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01006550 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006551 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04006552 t->curr_ret_stack = -1;
6553 /* Make sure the tasks see the -1 first: */
6554 smp_wmb();
6555 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006556 }
6557 } while_each_thread(g, t);
6558
6559unlock:
Soumya PN6112a302016-05-17 21:31:14 +05306560 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006561free:
6562 for (i = start; i < end; i++)
6563 kfree(ret_stack_list[i]);
6564 return ret;
6565}
6566
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006567static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02006568ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04006569 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006570{
6571 unsigned long long timestamp;
6572 int index;
6573
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006574 /*
6575 * Does the user want to count the time a function was asleep.
6576 * If so, do not update the time stamps.
6577 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006578 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006579 return;
6580
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006581 timestamp = trace_clock_local();
6582
6583 prev->ftrace_timestamp = timestamp;
6584
6585 /* only process tasks that we timestamped */
6586 if (!next->ftrace_timestamp)
6587 return;
6588
6589 /*
6590 * Update all the counters in next to make up for the
6591 * time next was sleeping.
6592 */
6593 timestamp -= next->ftrace_timestamp;
6594
6595 for (index = next->curr_ret_stack; index >= 0; index--)
6596 next->ret_stack[index].calltime += timestamp;
6597}
6598
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006599/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006600static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006601{
6602 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006603 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006604
6605 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
6606 sizeof(struct ftrace_ret_stack *),
6607 GFP_KERNEL);
6608
6609 if (!ret_stack_list)
6610 return -ENOMEM;
6611
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006612 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04006613 for_each_online_cpu(cpu) {
6614 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05006615 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04006616 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006617
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006618 do {
6619 ret = alloc_retstack_tasklist(ret_stack_list);
6620 } while (ret == -EAGAIN);
6621
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006622 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04006623 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006624 if (ret)
6625 pr_info("ftrace_graph: Couldn't activate tracepoint"
6626 " probe to kernel_sched_switch\n");
6627 }
6628
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006629 kfree(ret_stack_list);
6630 return ret;
6631}
6632
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006633/*
6634 * Hibernation protection.
6635 * The state of the current task is too much unstable during
6636 * suspend/restore to disk. We want to protect against that.
6637 */
6638static int
6639ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6640 void *unused)
6641{
6642 switch (state) {
6643 case PM_HIBERNATION_PREPARE:
6644 pause_graph_tracing();
6645 break;
6646
6647 case PM_POST_HIBERNATION:
6648 unpause_graph_tracing();
6649 break;
6650 }
6651 return NOTIFY_DONE;
6652}
6653
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006654static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6655{
6656 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6657 return 0;
6658 return __ftrace_graph_entry(trace);
6659}
6660
6661/*
6662 * The function graph tracer should only trace the functions defined
6663 * by set_ftrace_filter and set_ftrace_notrace. If another function
6664 * tracer ops is registered, the graph tracer requires testing the
6665 * function against the global ops, and not just trace any function
6666 * that any ftrace_ops registered.
6667 */
6668static void update_function_graph_func(void)
6669{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006670 struct ftrace_ops *op;
6671 bool do_test = false;
6672
6673 /*
6674 * The graph and global ops share the same set of functions
6675 * to test. If any other ops is on the list, then
6676 * the graph tracing needs to test if its the function
6677 * it should call.
6678 */
6679 do_for_each_ftrace_op(op, ftrace_ops_list) {
6680 if (op != &global_ops && op != &graph_ops &&
6681 op != &ftrace_list_end) {
6682 do_test = true;
6683 /* in double loop, break out with goto */
6684 goto out;
6685 }
6686 } while_for_each_ftrace_op(op);
6687 out:
6688 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006689 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006690 else
6691 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006692}
6693
Mathias Krause8275f692014-03-30 15:31:50 +02006694static struct notifier_block ftrace_suspend_notifier = {
6695 .notifier_call = ftrace_suspend_notifier_call,
6696};
6697
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006698int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6699 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006700{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006701 int ret = 0;
6702
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006703 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006704
Steven Rostedt05ce5812009-03-24 00:18:31 -04006705 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04006706 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04006707 ret = -EBUSY;
6708 goto out;
6709 }
6710
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006711 register_pm_notifier(&ftrace_suspend_notifier);
6712
Steven Rostedt597af812009-04-03 15:24:12 -04006713 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006714 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006715 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04006716 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006717 goto out;
6718 }
Steven Rostedte53a6312008-11-26 00:16:25 -05006719
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006720 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006721
6722 /*
6723 * Update the indirect function to the entryfunc, and the
6724 * function that gets called to the entry_test first. Then
6725 * call the update fgraph entry function to determine if
6726 * the entryfunc should be called directly or not.
6727 */
6728 __ftrace_graph_entry = entryfunc;
6729 ftrace_graph_entry = ftrace_graph_entry_test;
6730 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05006731
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006732 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006733out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006734 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006735 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006736}
6737
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006738void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006739{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006740 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006741
Steven Rostedt597af812009-04-03 15:24:12 -04006742 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006743 goto out;
6744
Steven Rostedt597af812009-04-03 15:24:12 -04006745 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006746 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006747 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006748 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006749 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006750 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04006751 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006752
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006753#ifdef CONFIG_DYNAMIC_FTRACE
6754 /*
6755 * Function graph does not allocate the trampoline, but
6756 * other global_ops do. We need to reset the ALLOC_TRAMP flag
6757 * if one was used.
6758 */
6759 global_ops.trampoline = save_global_trampoline;
6760 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
6761 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
6762#endif
6763
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006764 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006765 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006766}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006767
Steven Rostedt868baf02011-02-10 21:26:13 -05006768static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6769
6770static void
6771graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6772{
6773 atomic_set(&t->tracing_graph_pause, 0);
6774 atomic_set(&t->trace_overrun, 0);
6775 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006776 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05006777 smp_wmb();
6778 t->ret_stack = ret_stack;
6779}
6780
6781/*
6782 * Allocate a return stack for the idle task. May be the first
6783 * time through, or it may be done by CPU hotplug online.
6784 */
6785void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6786{
6787 t->curr_ret_stack = -1;
6788 /*
6789 * The idle task has no parent, it either has its own
6790 * stack or no stack at all.
6791 */
6792 if (t->ret_stack)
6793 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6794
6795 if (ftrace_graph_active) {
6796 struct ftrace_ret_stack *ret_stack;
6797
6798 ret_stack = per_cpu(idle_ret_stack, cpu);
6799 if (!ret_stack) {
6800 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6801 * sizeof(struct ftrace_ret_stack),
6802 GFP_KERNEL);
6803 if (!ret_stack)
6804 return;
6805 per_cpu(idle_ret_stack, cpu) = ret_stack;
6806 }
6807 graph_init_task(t, ret_stack);
6808 }
6809}
6810
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006811/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006812void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006813{
Steven Rostedt84047e32009-06-02 16:51:55 -04006814 /* Make sure we do not use the parent ret_stack */
6815 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05006816 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04006817
Steven Rostedt597af812009-04-03 15:24:12 -04006818 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04006819 struct ftrace_ret_stack *ret_stack;
6820
6821 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006822 * sizeof(struct ftrace_ret_stack),
6823 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04006824 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006825 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05006826 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04006827 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006828}
6829
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006830void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006831{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006832 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6833
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006834 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006835 /* NULL must become visible to IRQs before we free it: */
6836 barrier();
6837
6838 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006839}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006840#endif