blob: e0a98225666b53f50c5b66da8e5666799ab3d77f [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
Steven Rostedt (VMware)a8f0f9e2017-08-17 16:37:25 -0400892 /* If function graph is shutting down, ret_stack can be NULL */
893 if (!current->ret_stack)
894 return 0;
895
Namhyung Kim8861dd32016-08-31 11:55:29 +0900896 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
897 current->ret_stack[index].subtime = 0;
898
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400899 return 1;
900}
901
902static void profile_graph_return(struct ftrace_graph_ret *trace)
903{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400904 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400905 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400906 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400907 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400908
909 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500910 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400911 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400912 goto out;
913
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400914 /* If the calltime was zero'd ignore it */
915 if (!trace->calltime)
916 goto out;
917
Steven Rostedta2a16d62009-03-24 23:17:58 -0400918 calltime = trace->rettime - trace->calltime;
919
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400920 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400921 int index;
922
923 index = trace->depth;
924
925 /* Append this call time to the parent time to subtract */
926 if (index)
927 current->ret_stack[index - 1].subtime += calltime;
928
929 if (current->ret_stack[index].subtime < calltime)
930 calltime -= current->ret_stack[index].subtime;
931 else
932 calltime = 0;
933 }
934
Steven Rostedtcafb1682009-03-24 20:50:39 -0400935 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400936 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400937 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400938 rec->time_squared += calltime * calltime;
939 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400940
Steven Rostedtcafb1682009-03-24 20:50:39 -0400941 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400942 local_irq_restore(flags);
943}
944
945static int register_ftrace_profiler(void)
946{
947 return register_ftrace_graph(&profile_graph_return,
948 &profile_graph_entry);
949}
950
951static void unregister_ftrace_profiler(void)
952{
953 unregister_ftrace_graph();
954}
955#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100956static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400957 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900958 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400959 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400960};
961
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400962static int register_ftrace_profiler(void)
963{
964 return register_ftrace_function(&ftrace_profile_ops);
965}
966
967static void unregister_ftrace_profiler(void)
968{
969 unregister_ftrace_function(&ftrace_profile_ops);
970}
971#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
972
Steven Rostedt493762f2009-03-23 17:12:36 -0400973static ssize_t
974ftrace_profile_write(struct file *filp, const char __user *ubuf,
975 size_t cnt, loff_t *ppos)
976{
977 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400978 int ret;
979
Peter Huewe22fe9b52011-06-07 21:58:27 +0200980 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
981 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400982 return ret;
983
984 val = !!val;
985
986 mutex_lock(&ftrace_profile_lock);
987 if (ftrace_profile_enabled ^ val) {
988 if (val) {
989 ret = ftrace_profile_init();
990 if (ret < 0) {
991 cnt = ret;
992 goto out;
993 }
994
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400995 ret = register_ftrace_profiler();
996 if (ret < 0) {
997 cnt = ret;
998 goto out;
999 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001000 ftrace_profile_enabled = 1;
1001 } else {
1002 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -04001003 /*
1004 * unregister_ftrace_profiler calls stop_machine
1005 * so this acts like an synchronize_sched.
1006 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001007 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001008 }
1009 }
1010 out:
1011 mutex_unlock(&ftrace_profile_lock);
1012
Jiri Olsacf8517c2009-10-23 19:36:16 -04001013 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001014
1015 return cnt;
1016}
1017
1018static ssize_t
1019ftrace_profile_read(struct file *filp, char __user *ubuf,
1020 size_t cnt, loff_t *ppos)
1021{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001022 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001023 int r;
1024
1025 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1026 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1027}
1028
1029static const struct file_operations ftrace_profile_fops = {
1030 .open = tracing_open_generic,
1031 .read = ftrace_profile_read,
1032 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001033 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001034};
1035
Steven Rostedtcafb1682009-03-24 20:50:39 -04001036/* used to initialize the real stat files */
1037static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001038 .name = "functions",
1039 .stat_start = function_stat_start,
1040 .stat_next = function_stat_next,
1041 .stat_cmp = function_stat_cmp,
1042 .stat_headers = function_stat_headers,
1043 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001044};
1045
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001046static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001047{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001048 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001049 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001050 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001051 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001052 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001053
Steven Rostedtcafb1682009-03-24 20:50:39 -04001054 for_each_possible_cpu(cpu) {
1055 stat = &per_cpu(ftrace_profile_stats, cpu);
1056
Geliang Tang6363c6b2016-03-15 22:12:34 +08001057 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -04001058 if (!name) {
1059 /*
1060 * The files created are permanent, if something happens
1061 * we still do not free memory.
1062 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001063 WARN(1,
1064 "Could not allocate stat file for cpu %d\n",
1065 cpu);
1066 return;
1067 }
1068 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001069 stat->stat.name = name;
1070 ret = register_stat_tracer(&stat->stat);
1071 if (ret) {
1072 WARN(1,
1073 "Could not register function stat for cpu %d\n",
1074 cpu);
1075 kfree(name);
1076 return;
1077 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001078 }
1079
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001080 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001081 d_tracer, NULL, &ftrace_profile_fops);
1082 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001083 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001084}
1085
1086#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001087static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001088{
1089}
1090#endif /* CONFIG_FUNCTION_PROFILER */
1091
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001092static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1093
Pratyush Anand1619dc32015-03-06 23:58:06 +05301094#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1095static int ftrace_graph_active;
1096#else
1097# define ftrace_graph_active 0
1098#endif
1099
Steven Rostedt3d083392008-05-12 21:20:42 +02001100#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001101
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001102static struct ftrace_ops *removed_ops;
1103
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001104/*
1105 * Set when doing a global update, like enabling all recs or disabling them.
1106 * It is not set when just updating a single ftrace_ops.
1107 */
1108static bool update_all_ops;
1109
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001110#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001111# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001112#endif
1113
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001114struct ftrace_func_entry {
1115 struct hlist_node hlist;
1116 unsigned long ip;
1117};
1118
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001119struct ftrace_func_probe {
1120 struct ftrace_probe_ops *probe_ops;
1121 struct ftrace_ops ops;
1122 struct trace_array *tr;
1123 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001124 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001125 int ref;
1126};
1127
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001128/*
1129 * We make these constant because no one should touch them,
1130 * but they are used as the default "empty hash", to avoid allocating
1131 * it all the time. These are in a read only section such that if
1132 * anyone does try to modify it, it will cause an exception.
1133 */
1134static const struct hlist_head empty_buckets[1];
1135static const struct ftrace_hash empty_hash = {
1136 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001137};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001138#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001139
Steven Rostedt2b499382011-05-03 22:49:52 -04001140static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001141 .func = ftrace_stub,
1142 .local_hash.notrace_hash = EMPTY_HASH,
1143 .local_hash.filter_hash = EMPTY_HASH,
1144 INIT_OPS_HASH(global_ops)
1145 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001146 FTRACE_OPS_FL_INITIALIZED |
1147 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001148};
1149
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001150/*
1151 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001152 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001153 * not return true for either core_kernel_text() or
1154 * is_module_text_address().
1155 */
1156bool is_ftrace_trampoline(unsigned long addr)
1157{
1158 struct ftrace_ops *op;
1159 bool ret = false;
1160
1161 /*
1162 * Some of the ops may be dynamically allocated,
1163 * they are freed after a synchronize_sched().
1164 */
1165 preempt_disable_notrace();
1166
1167 do_for_each_ftrace_op(op, ftrace_ops_list) {
1168 /*
1169 * This is to check for dynamically allocated trampolines.
1170 * Trampolines that are in kernel text will have
1171 * core_kernel_text() return true.
1172 */
1173 if (op->trampoline && op->trampoline_size)
1174 if (addr >= op->trampoline &&
1175 addr < op->trampoline + op->trampoline_size) {
1176 ret = true;
1177 goto out;
1178 }
1179 } while_for_each_ftrace_op(op);
1180
1181 out:
1182 preempt_enable_notrace();
1183
1184 return ret;
1185}
1186
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001187struct ftrace_page {
1188 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001189 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001190 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001191 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001192};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001193
Steven Rostedta7900872011-12-16 16:23:44 -05001194#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1195#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001196
1197/* estimate from running different kernels */
1198#define NR_TO_INIT 10000
1199
1200static struct ftrace_page *ftrace_pages_start;
1201static struct ftrace_page *ftrace_pages;
1202
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001203static __always_inline unsigned long
1204ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1205{
1206 if (hash->size_bits > 0)
1207 return hash_long(ip, hash->size_bits);
1208
1209 return 0;
1210}
1211
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001212/* Only use this function if ftrace_hash_empty() has already been tested */
1213static __always_inline struct ftrace_func_entry *
1214__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001215{
1216 unsigned long key;
1217 struct ftrace_func_entry *entry;
1218 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001219
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001220 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001221 hhd = &hash->buckets[key];
1222
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001223 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001224 if (entry->ip == ip)
1225 return entry;
1226 }
1227 return NULL;
1228}
1229
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001230/**
1231 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1232 * @hash: The hash to look at
1233 * @ip: The instruction pointer to test
1234 *
1235 * Search a given @hash to see if a given instruction pointer (@ip)
1236 * exists in it.
1237 *
1238 * Returns the entry that holds the @ip if found. NULL otherwise.
1239 */
1240struct ftrace_func_entry *
1241ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1242{
1243 if (ftrace_hash_empty(hash))
1244 return NULL;
1245
1246 return __ftrace_lookup_ip(hash, ip);
1247}
1248
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001249static void __add_hash_entry(struct ftrace_hash *hash,
1250 struct ftrace_func_entry *entry)
1251{
1252 struct hlist_head *hhd;
1253 unsigned long key;
1254
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001255 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001256 hhd = &hash->buckets[key];
1257 hlist_add_head(&entry->hlist, hhd);
1258 hash->count++;
1259}
1260
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001261static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1262{
1263 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001264
1265 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1266 if (!entry)
1267 return -ENOMEM;
1268
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001269 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001270 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001271
1272 return 0;
1273}
1274
1275static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276free_hash_entry(struct ftrace_hash *hash,
1277 struct ftrace_func_entry *entry)
1278{
1279 hlist_del(&entry->hlist);
1280 kfree(entry);
1281 hash->count--;
1282}
1283
1284static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001285remove_hash_entry(struct ftrace_hash *hash,
1286 struct ftrace_func_entry *entry)
1287{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001288 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001289 hash->count--;
1290}
1291
1292static void ftrace_hash_clear(struct ftrace_hash *hash)
1293{
1294 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001295 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001296 struct ftrace_func_entry *entry;
1297 int size = 1 << hash->size_bits;
1298 int i;
1299
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001300 if (!hash->count)
1301 return;
1302
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001303 for (i = 0; i < size; i++) {
1304 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001305 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001306 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001307 }
1308 FTRACE_WARN_ON(hash->count);
1309}
1310
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001311static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1312{
1313 list_del(&ftrace_mod->list);
1314 kfree(ftrace_mod->module);
1315 kfree(ftrace_mod->func);
1316 kfree(ftrace_mod);
1317}
1318
1319static void clear_ftrace_mod_list(struct list_head *head)
1320{
1321 struct ftrace_mod_load *p, *n;
1322
1323 /* stack tracer isn't supported yet */
1324 if (!head)
1325 return;
1326
1327 mutex_lock(&ftrace_lock);
1328 list_for_each_entry_safe(p, n, head, list)
1329 free_ftrace_mod(p);
1330 mutex_unlock(&ftrace_lock);
1331}
1332
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333static void free_ftrace_hash(struct ftrace_hash *hash)
1334{
1335 if (!hash || hash == EMPTY_HASH)
1336 return;
1337 ftrace_hash_clear(hash);
1338 kfree(hash->buckets);
1339 kfree(hash);
1340}
1341
Steven Rostedt07fd5512011-05-05 18:03:47 -04001342static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1343{
1344 struct ftrace_hash *hash;
1345
1346 hash = container_of(rcu, struct ftrace_hash, rcu);
1347 free_ftrace_hash(hash);
1348}
1349
1350static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1351{
1352 if (!hash || hash == EMPTY_HASH)
1353 return;
1354 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1355}
1356
Jiri Olsa5500fa52012-02-15 15:51:54 +01001357void ftrace_free_filter(struct ftrace_ops *ops)
1358{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001359 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001360 free_ftrace_hash(ops->func_hash->filter_hash);
1361 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001362}
1363
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001364static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1365{
1366 struct ftrace_hash *hash;
1367 int size;
1368
1369 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1370 if (!hash)
1371 return NULL;
1372
1373 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001374 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001375
1376 if (!hash->buckets) {
1377 kfree(hash);
1378 return NULL;
1379 }
1380
1381 hash->size_bits = size_bits;
1382
1383 return hash;
1384}
1385
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001386
1387static int ftrace_add_mod(struct trace_array *tr,
1388 const char *func, const char *module,
1389 int enable)
1390{
1391 struct ftrace_mod_load *ftrace_mod;
1392 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1393
1394 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1395 if (!ftrace_mod)
1396 return -ENOMEM;
1397
1398 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1399 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1400 ftrace_mod->enable = enable;
1401
1402 if (!ftrace_mod->func || !ftrace_mod->module)
1403 goto out_free;
1404
1405 list_add(&ftrace_mod->list, mod_head);
1406
1407 return 0;
1408
1409 out_free:
1410 free_ftrace_mod(ftrace_mod);
1411
1412 return -ENOMEM;
1413}
1414
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001415static struct ftrace_hash *
1416alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1417{
1418 struct ftrace_func_entry *entry;
1419 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001420 int size;
1421 int ret;
1422 int i;
1423
1424 new_hash = alloc_ftrace_hash(size_bits);
1425 if (!new_hash)
1426 return NULL;
1427
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001428 if (hash)
1429 new_hash->flags = hash->flags;
1430
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001431 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001432 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001433 return new_hash;
1434
1435 size = 1 << hash->size_bits;
1436 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001437 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001438 ret = add_hash_entry(new_hash, entry->ip);
1439 if (ret < 0)
1440 goto free_hash;
1441 }
1442 }
1443
1444 FTRACE_WARN_ON(new_hash->count != hash->count);
1445
1446 return new_hash;
1447
1448 free_hash:
1449 free_ftrace_hash(new_hash);
1450 return NULL;
1451}
1452
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001453static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001454ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001455static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001456ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001457
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001458static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1459 struct ftrace_hash *new_hash);
1460
Namhyung Kim3e278c02017-01-20 11:44:45 +09001461static struct ftrace_hash *
1462__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001463{
1464 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001465 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001466 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001467 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001468 int size = src->count;
1469 int bits = 0;
1470 int i;
1471
1472 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001473 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001474 */
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001475 if (ftrace_hash_empty(src))
Namhyung Kim3e278c02017-01-20 11:44:45 +09001476 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001477
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001478 /*
1479 * Make the hash size about 1/2 the # found
1480 */
1481 for (size /= 2; size; size >>= 1)
1482 bits++;
1483
1484 /* Don't allocate too much */
1485 if (bits > FTRACE_HASH_MAX_BITS)
1486 bits = FTRACE_HASH_MAX_BITS;
1487
Steven Rostedt07fd5512011-05-05 18:03:47 -04001488 new_hash = alloc_ftrace_hash(bits);
1489 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001490 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001491
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001492 new_hash->flags = src->flags;
1493
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001494 size = 1 << src->size_bits;
1495 for (i = 0; i < size; i++) {
1496 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001497 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001498 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001499 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001500 }
1501 }
1502
Namhyung Kim3e278c02017-01-20 11:44:45 +09001503 return new_hash;
1504}
1505
1506static int
1507ftrace_hash_move(struct ftrace_ops *ops, int enable,
1508 struct ftrace_hash **dst, struct ftrace_hash *src)
1509{
1510 struct ftrace_hash *new_hash;
1511 int ret;
1512
1513 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1514 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1515 return -EINVAL;
1516
1517 new_hash = __ftrace_hash_move(src);
1518 if (!new_hash)
1519 return -ENOMEM;
1520
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001521 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1522 if (enable) {
1523 /* IPMODIFY should be updated only when filter_hash updating */
1524 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1525 if (ret < 0) {
1526 free_ftrace_hash(new_hash);
1527 return ret;
1528 }
1529 }
1530
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001531 /*
1532 * Remove the current set, update the hash and add
1533 * them back.
1534 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001535 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001536
Steven Rostedt07fd5512011-05-05 18:03:47 -04001537 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001538
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001539 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001540
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001541 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001542}
1543
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001544static bool hash_contains_ip(unsigned long ip,
1545 struct ftrace_ops_hash *hash)
1546{
1547 /*
1548 * The function record is a match if it exists in the filter
1549 * hash and not in the notrace hash. Note, an emty hash is
1550 * considered a match for the filter hash, but an empty
1551 * notrace hash is considered not in the notrace hash.
1552 */
1553 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001554 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001555 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001556 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001557}
1558
Steven Rostedt265c8312009-02-13 12:43:56 -05001559/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001560 * Test the hashes for this ops to see if we want to call
1561 * the ops->func or not.
1562 *
1563 * It's a match if the ip is in the ops->filter_hash or
1564 * the filter_hash does not exist or is empty,
1565 * AND
1566 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001567 *
1568 * This needs to be called with preemption disabled as
1569 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001570 */
1571static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001572ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001573{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001574 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001575 int ret;
1576
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001577#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1578 /*
1579 * There's a small race when adding ops that the ftrace handler
1580 * that wants regs, may be called without them. We can not
1581 * allow that handler to be called if regs is NULL.
1582 */
1583 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1584 return 0;
1585#endif
1586
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001587 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1588 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001589
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001590 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001591 ret = 1;
1592 else
1593 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001594
1595 return ret;
1596}
1597
1598/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001599 * This is a double for. Do not use 'break' to break out of the loop,
1600 * you must use a goto.
1601 */
1602#define do_for_each_ftrace_rec(pg, rec) \
1603 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1604 int _____i; \
1605 for (_____i = 0; _____i < pg->index; _____i++) { \
1606 rec = &pg->records[_____i];
1607
1608#define while_for_each_ftrace_rec() \
1609 } \
1610 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301611
Steven Rostedt5855fea2011-12-16 19:27:42 -05001612
1613static int ftrace_cmp_recs(const void *a, const void *b)
1614{
Steven Rostedta650e022012-04-25 13:48:13 -04001615 const struct dyn_ftrace *key = a;
1616 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001617
Steven Rostedta650e022012-04-25 13:48:13 -04001618 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001619 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001620 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1621 return 1;
1622 return 0;
1623}
1624
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001625/**
1626 * ftrace_location_range - return the first address of a traced location
1627 * if it touches the given ip range
1628 * @start: start of range to search.
1629 * @end: end of range to search (inclusive). @end points to the last byte
1630 * to check.
1631 *
1632 * Returns rec->ip if the related ftrace location is a least partly within
1633 * the given address range. That is, the first address of the instruction
1634 * that is either a NOP or call to the function tracer. It checks the ftrace
1635 * internal tables to determine if the address belongs or not.
1636 */
1637unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001638{
1639 struct ftrace_page *pg;
1640 struct dyn_ftrace *rec;
1641 struct dyn_ftrace key;
1642
1643 key.ip = start;
1644 key.flags = end; /* overload flags, as it is unsigned long */
1645
1646 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1647 if (end < pg->records[0].ip ||
1648 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1649 continue;
1650 rec = bsearch(&key, pg->records, pg->index,
1651 sizeof(struct dyn_ftrace),
1652 ftrace_cmp_recs);
1653 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001654 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001655 }
1656
Steven Rostedt5855fea2011-12-16 19:27:42 -05001657 return 0;
1658}
1659
Steven Rostedtc88fd862011-08-16 09:53:39 -04001660/**
1661 * ftrace_location - return true if the ip giving is a traced location
1662 * @ip: the instruction pointer to check
1663 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001664 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001665 * That is, the instruction that is either a NOP or call to
1666 * the function tracer. It checks the ftrace internal tables to
1667 * determine if the address belongs or not.
1668 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001669unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001670{
Steven Rostedta650e022012-04-25 13:48:13 -04001671 return ftrace_location_range(ip, ip);
1672}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001673
Steven Rostedta650e022012-04-25 13:48:13 -04001674/**
1675 * ftrace_text_reserved - return true if range contains an ftrace location
1676 * @start: start of range to search
1677 * @end: end of range to search (inclusive). @end points to the last byte to check.
1678 *
1679 * Returns 1 if @start and @end contains a ftrace location.
1680 * That is, the instruction that is either a NOP or call to
1681 * the function tracer. It checks the ftrace internal tables to
1682 * determine if the address belongs or not.
1683 */
Sasha Levind88471c2013-01-09 18:09:20 -05001684int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001685{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001686 unsigned long ret;
1687
1688 ret = ftrace_location_range((unsigned long)start,
1689 (unsigned long)end);
1690
1691 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001692}
1693
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001694/* Test if ops registered to this rec needs regs */
1695static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1696{
1697 struct ftrace_ops *ops;
1698 bool keep_regs = false;
1699
1700 for (ops = ftrace_ops_list;
1701 ops != &ftrace_list_end; ops = ops->next) {
1702 /* pass rec in as regs to have non-NULL val */
1703 if (ftrace_ops_test(ops, rec->ip, rec)) {
1704 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1705 keep_regs = true;
1706 break;
1707 }
1708 }
1709 }
1710
1711 return keep_regs;
1712}
1713
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001714static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001715 int filter_hash,
1716 bool inc)
1717{
1718 struct ftrace_hash *hash;
1719 struct ftrace_hash *other_hash;
1720 struct ftrace_page *pg;
1721 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001722 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001723 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001724 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001725
1726 /* Only update if the ops has been registered */
1727 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001728 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001729
1730 /*
1731 * In the filter_hash case:
1732 * If the count is zero, we update all records.
1733 * Otherwise we just update the items in the hash.
1734 *
1735 * In the notrace_hash case:
1736 * We enable the update in the hash.
1737 * As disabling notrace means enabling the tracing,
1738 * and enabling notrace means disabling, the inc variable
1739 * gets inversed.
1740 */
1741 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001742 hash = ops->func_hash->filter_hash;
1743 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001744 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001745 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001746 } else {
1747 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001748 hash = ops->func_hash->notrace_hash;
1749 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001750 /*
1751 * If the notrace hash has no items,
1752 * then there's nothing to do.
1753 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001754 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001755 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001756 }
1757
1758 do_for_each_ftrace_rec(pg, rec) {
1759 int in_other_hash = 0;
1760 int in_hash = 0;
1761 int match = 0;
1762
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001763 if (rec->flags & FTRACE_FL_DISABLED)
1764 continue;
1765
Steven Rostedted926f92011-05-03 13:25:24 -04001766 if (all) {
1767 /*
1768 * Only the filter_hash affects all records.
1769 * Update if the record is not in the notrace hash.
1770 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001771 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001772 match = 1;
1773 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001774 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1775 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001776
1777 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001778 * If filter_hash is set, we want to match all functions
1779 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001780 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001781 * If filter_hash is not set, then we are decrementing.
1782 * That means we match anything that is in the hash
1783 * and also in the other_hash. That is, we need to turn
1784 * off functions in the other hash because they are disabled
1785 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001786 */
1787 if (filter_hash && in_hash && !in_other_hash)
1788 match = 1;
1789 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001790 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001791 match = 1;
1792 }
1793 if (!match)
1794 continue;
1795
1796 if (inc) {
1797 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001798 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001799 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001800
1801 /*
1802 * If there's only a single callback registered to a
1803 * function, and the ops has a trampoline registered
1804 * for it, then we can call it directly.
1805 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001806 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001807 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001808 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001809 /*
1810 * If we are adding another function callback
1811 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001812 * custom trampoline in use, then we need to go
1813 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001814 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001815 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001816
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001817 /*
1818 * If any ops wants regs saved for this function
1819 * then all ops will get saved regs.
1820 */
1821 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1822 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001823 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001824 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001825 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001826 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001827
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001828 /*
1829 * If the rec had REGS enabled and the ops that is
1830 * being removed had REGS set, then see if there is
1831 * still any ops for this record that wants regs.
1832 * If not, we can stop recording them.
1833 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001834 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001835 rec->flags & FTRACE_FL_REGS &&
1836 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1837 if (!test_rec_ops_needs_regs(rec))
1838 rec->flags &= ~FTRACE_FL_REGS;
1839 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001840
1841 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001842 * If the rec had TRAMP enabled, then it needs to
1843 * be cleared. As TRAMP can only be enabled iff
1844 * there is only a single ops attached to it.
1845 * In otherwords, always disable it on decrementing.
1846 * In the future, we may set it if rec count is
1847 * decremented to one, and the ops that is left
1848 * has a trampoline.
1849 */
1850 rec->flags &= ~FTRACE_FL_TRAMP;
1851
1852 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001853 * flags will be cleared in ftrace_check_record()
1854 * if rec count is zero.
1855 */
Steven Rostedted926f92011-05-03 13:25:24 -04001856 }
1857 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001858
1859 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1860 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1861
Steven Rostedted926f92011-05-03 13:25:24 -04001862 /* Shortcut, if we handled all records, we are done. */
1863 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001864 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001865 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001866
1867 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001868}
1869
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001870static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001871 int filter_hash)
1872{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001873 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001874}
1875
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001876static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001877 int filter_hash)
1878{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001879 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001880}
1881
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001882static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1883 int filter_hash, int inc)
1884{
1885 struct ftrace_ops *op;
1886
1887 __ftrace_hash_rec_update(ops, filter_hash, inc);
1888
1889 if (ops->func_hash != &global_ops.local_hash)
1890 return;
1891
1892 /*
1893 * If the ops shares the global_ops hash, then we need to update
1894 * all ops that are enabled and use this hash.
1895 */
1896 do_for_each_ftrace_op(op, ftrace_ops_list) {
1897 /* Already done */
1898 if (op == ops)
1899 continue;
1900 if (op->func_hash == &global_ops.local_hash)
1901 __ftrace_hash_rec_update(op, filter_hash, inc);
1902 } while_for_each_ftrace_op(op);
1903}
1904
1905static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1906 int filter_hash)
1907{
1908 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1909}
1910
1911static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1912 int filter_hash)
1913{
1914 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1915}
1916
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001917/*
1918 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1919 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1920 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1921 * Note that old_hash and new_hash has below meanings
1922 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1923 * - If the hash is EMPTY_HASH, it hits nothing
1924 * - Anything else hits the recs which match the hash entries.
1925 */
1926static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1927 struct ftrace_hash *old_hash,
1928 struct ftrace_hash *new_hash)
1929{
1930 struct ftrace_page *pg;
1931 struct dyn_ftrace *rec, *end = NULL;
1932 int in_old, in_new;
1933
1934 /* Only update if the ops has been registered */
1935 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1936 return 0;
1937
1938 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1939 return 0;
1940
1941 /*
1942 * Since the IPMODIFY is a very address sensitive action, we do not
1943 * allow ftrace_ops to set all functions to new hash.
1944 */
1945 if (!new_hash || !old_hash)
1946 return -EINVAL;
1947
1948 /* Update rec->flags */
1949 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001950
1951 if (rec->flags & FTRACE_FL_DISABLED)
1952 continue;
1953
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001954 /* We need to update only differences of filter_hash */
1955 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1956 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1957 if (in_old == in_new)
1958 continue;
1959
1960 if (in_new) {
1961 /* New entries must ensure no others are using it */
1962 if (rec->flags & FTRACE_FL_IPMODIFY)
1963 goto rollback;
1964 rec->flags |= FTRACE_FL_IPMODIFY;
1965 } else /* Removed entry */
1966 rec->flags &= ~FTRACE_FL_IPMODIFY;
1967 } while_for_each_ftrace_rec();
1968
1969 return 0;
1970
1971rollback:
1972 end = rec;
1973
1974 /* Roll back what we did above */
1975 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001976
1977 if (rec->flags & FTRACE_FL_DISABLED)
1978 continue;
1979
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001980 if (rec == end)
1981 goto err_out;
1982
1983 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1984 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1985 if (in_old == in_new)
1986 continue;
1987
1988 if (in_new)
1989 rec->flags &= ~FTRACE_FL_IPMODIFY;
1990 else
1991 rec->flags |= FTRACE_FL_IPMODIFY;
1992 } while_for_each_ftrace_rec();
1993
1994err_out:
1995 return -EBUSY;
1996}
1997
1998static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1999{
2000 struct ftrace_hash *hash = ops->func_hash->filter_hash;
2001
2002 if (ftrace_hash_empty(hash))
2003 hash = NULL;
2004
2005 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
2006}
2007
2008/* Disabling always succeeds */
2009static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
2010{
2011 struct ftrace_hash *hash = ops->func_hash->filter_hash;
2012
2013 if (ftrace_hash_empty(hash))
2014 hash = NULL;
2015
2016 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
2017}
2018
2019static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
2020 struct ftrace_hash *new_hash)
2021{
2022 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
2023
2024 if (ftrace_hash_empty(old_hash))
2025 old_hash = NULL;
2026
2027 if (ftrace_hash_empty(new_hash))
2028 new_hash = NULL;
2029
2030 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
2031}
2032
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002033static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07002034{
2035 int i;
2036
2037 printk(KERN_CONT "%s", fmt);
2038
2039 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
2040 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
2041}
2042
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002043static struct ftrace_ops *
2044ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002045static struct ftrace_ops *
2046ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002047
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002048enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002049const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002050
2051static void print_bug_type(void)
2052{
2053 switch (ftrace_bug_type) {
2054 case FTRACE_BUG_UNKNOWN:
2055 break;
2056 case FTRACE_BUG_INIT:
2057 pr_info("Initializing ftrace call sites\n");
2058 break;
2059 case FTRACE_BUG_NOP:
2060 pr_info("Setting ftrace call site to NOP\n");
2061 break;
2062 case FTRACE_BUG_CALL:
2063 pr_info("Setting ftrace call site to call ftrace function\n");
2064 break;
2065 case FTRACE_BUG_UPDATE:
2066 pr_info("Updating ftrace call site to call a different ftrace function\n");
2067 break;
2068 }
2069}
2070
Steven Rostedtc88fd862011-08-16 09:53:39 -04002071/**
2072 * ftrace_bug - report and shutdown function tracer
2073 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002074 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04002075 *
2076 * The arch code that enables or disables the function tracing
2077 * can call ftrace_bug() when it has detected a problem in
2078 * modifying the code. @failed should be one of either:
2079 * EFAULT - if the problem happens on reading the @ip address
2080 * EINVAL - if what is read at @ip is not what was expected
2081 * EPERM - if the problem happens on writting to the @ip address
2082 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002083void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002084{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002085 unsigned long ip = rec ? rec->ip : 0;
2086
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002087 switch (failed) {
2088 case -EFAULT:
2089 FTRACE_WARN_ON_ONCE(1);
2090 pr_info("ftrace faulted on modifying ");
2091 print_ip_sym(ip);
2092 break;
2093 case -EINVAL:
2094 FTRACE_WARN_ON_ONCE(1);
2095 pr_info("ftrace failed to modify ");
2096 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002097 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002098 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002099 if (ftrace_expected) {
2100 print_ip_ins(" expected: ", ftrace_expected);
2101 pr_cont("\n");
2102 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002103 break;
2104 case -EPERM:
2105 FTRACE_WARN_ON_ONCE(1);
2106 pr_info("ftrace faulted on writing ");
2107 print_ip_sym(ip);
2108 break;
2109 default:
2110 FTRACE_WARN_ON_ONCE(1);
2111 pr_info("ftrace faulted on unknown error ");
2112 print_ip_sym(ip);
2113 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002114 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002115 if (rec) {
2116 struct ftrace_ops *ops = NULL;
2117
2118 pr_info("ftrace record flags: %lx\n", rec->flags);
2119 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2120 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2121 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2122 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002123 if (ops) {
2124 do {
2125 pr_cont("\ttramp: %pS (%pS)",
2126 (void *)ops->trampoline,
2127 (void *)ops->func);
2128 ops = ftrace_find_tramp_ops_next(rec, ops);
2129 } while (ops);
2130 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002131 pr_cont("\ttramp: ERROR!");
2132
2133 }
2134 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002135 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002136 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002137}
2138
Steven Rostedtc88fd862011-08-16 09:53:39 -04002139static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002140{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002141 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002142
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002143 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2144
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002145 if (rec->flags & FTRACE_FL_DISABLED)
2146 return FTRACE_UPDATE_IGNORE;
2147
Steven Rostedt982c3502008-11-15 16:31:41 -05002148 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002149 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002150 *
Steven Rostedted926f92011-05-03 13:25:24 -04002151 * If the record has a ref count, then we need to enable it
2152 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002153 *
Steven Rostedted926f92011-05-03 13:25:24 -04002154 * Otherwise we make sure its disabled.
2155 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002156 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002157 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002158 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002159 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002160 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002161
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002162 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002163 * If enabling and the REGS flag does not match the REGS_EN, or
2164 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2165 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002166 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002167 if (flag) {
2168 if (!(rec->flags & FTRACE_FL_REGS) !=
2169 !(rec->flags & FTRACE_FL_REGS_EN))
2170 flag |= FTRACE_FL_REGS;
2171
2172 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2173 !(rec->flags & FTRACE_FL_TRAMP_EN))
2174 flag |= FTRACE_FL_TRAMP;
2175 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002176
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002177 /* If the state of this record hasn't changed, then do nothing */
2178 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002179 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002180
2181 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002182 /* Save off if rec is being enabled (for return value) */
2183 flag ^= rec->flags & FTRACE_FL_ENABLED;
2184
2185 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002186 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002187 if (flag & FTRACE_FL_REGS) {
2188 if (rec->flags & FTRACE_FL_REGS)
2189 rec->flags |= FTRACE_FL_REGS_EN;
2190 else
2191 rec->flags &= ~FTRACE_FL_REGS_EN;
2192 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002193 if (flag & FTRACE_FL_TRAMP) {
2194 if (rec->flags & FTRACE_FL_TRAMP)
2195 rec->flags |= FTRACE_FL_TRAMP_EN;
2196 else
2197 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2198 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002199 }
2200
2201 /*
2202 * If this record is being updated from a nop, then
2203 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002204 * Otherwise,
2205 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002206 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002207 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002208 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002209 if (flag & FTRACE_FL_ENABLED) {
2210 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002211 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002212 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002213
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002214 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002215 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002216 }
2217
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002218 if (update) {
2219 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002220 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002221 rec->flags = 0;
2222 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002223 /*
2224 * Just disable the record, but keep the ops TRAMP
2225 * and REGS states. The _EN flags must be disabled though.
2226 */
2227 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2228 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002229 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002230
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002231 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002232 return FTRACE_UPDATE_MAKE_NOP;
2233}
2234
2235/**
2236 * ftrace_update_record, set a record that now is tracing or not
2237 * @rec: the record to update
2238 * @enable: set to 1 if the record is tracing, zero to force disable
2239 *
2240 * The records that represent all functions that can be traced need
2241 * to be updated when tracing has been enabled.
2242 */
2243int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2244{
2245 return ftrace_check_record(rec, enable, 1);
2246}
2247
2248/**
2249 * ftrace_test_record, check if the record has been enabled or not
2250 * @rec: the record to test
2251 * @enable: set to 1 to check if enabled, 0 if it is disabled
2252 *
2253 * The arch code may need to test if a record is already set to
2254 * tracing to determine how to modify the function code that it
2255 * represents.
2256 */
2257int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2258{
2259 return ftrace_check_record(rec, enable, 0);
2260}
2261
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002262static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002263ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2264{
2265 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002266 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002267
2268 do_for_each_ftrace_op(op, ftrace_ops_list) {
2269
2270 if (!op->trampoline)
2271 continue;
2272
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002273 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002274 return op;
2275 } while_for_each_ftrace_op(op);
2276
2277 return NULL;
2278}
2279
2280static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002281ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2282 struct ftrace_ops *op)
2283{
2284 unsigned long ip = rec->ip;
2285
2286 while_for_each_ftrace_op(op) {
2287
2288 if (!op->trampoline)
2289 continue;
2290
2291 if (hash_contains_ip(ip, op->func_hash))
2292 return op;
2293 }
2294
2295 return NULL;
2296}
2297
2298static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002299ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2300{
2301 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002302 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002303
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002304 /*
2305 * Need to check removed ops first.
2306 * If they are being removed, and this rec has a tramp,
2307 * and this rec is in the ops list, then it would be the
2308 * one with the tramp.
2309 */
2310 if (removed_ops) {
2311 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002312 return removed_ops;
2313 }
2314
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002315 /*
2316 * Need to find the current trampoline for a rec.
2317 * Now, a trampoline is only attached to a rec if there
2318 * was a single 'ops' attached to it. But this can be called
2319 * when we are adding another op to the rec or removing the
2320 * current one. Thus, if the op is being added, we can
2321 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002322 * yet.
2323 *
2324 * If an ops is being modified (hooking to different functions)
2325 * then we don't care about the new functions that are being
2326 * added, just the old ones (that are probably being removed).
2327 *
2328 * If we are adding an ops to a function that already is using
2329 * a trampoline, it needs to be removed (trampolines are only
2330 * for single ops connected), then an ops that is not being
2331 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002332 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002333 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002334
2335 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002336 continue;
2337
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002338 /*
2339 * If the ops is being added, it hasn't gotten to
2340 * the point to be removed from this tree yet.
2341 */
2342 if (op->flags & FTRACE_OPS_FL_ADDING)
2343 continue;
2344
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002345
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002346 /*
2347 * If the ops is being modified and is in the old
2348 * hash, then it is probably being removed from this
2349 * function.
2350 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002351 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2352 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002353 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002354 /*
2355 * If the ops is not being added or modified, and it's
2356 * in its normal filter hash, then this must be the one
2357 * we want!
2358 */
2359 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2360 hash_contains_ip(ip, op->func_hash))
2361 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002362
2363 } while_for_each_ftrace_op(op);
2364
2365 return NULL;
2366}
2367
2368static struct ftrace_ops *
2369ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2370{
2371 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002372 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002373
2374 do_for_each_ftrace_op(op, ftrace_ops_list) {
2375 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002376 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002377 return op;
2378 } while_for_each_ftrace_op(op);
2379
2380 return NULL;
2381}
2382
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002383/**
2384 * ftrace_get_addr_new - Get the call address to set to
2385 * @rec: The ftrace record descriptor
2386 *
2387 * If the record has the FTRACE_FL_REGS set, that means that it
2388 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2389 * is not not set, then it wants to convert to the normal callback.
2390 *
2391 * Returns the address of the trampoline to set to
2392 */
2393unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2394{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002395 struct ftrace_ops *ops;
2396
2397 /* Trampolines take precedence over regs */
2398 if (rec->flags & FTRACE_FL_TRAMP) {
2399 ops = ftrace_find_tramp_ops_new(rec);
2400 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002401 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2402 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002403 /* Ftrace is shutting down, return anything */
2404 return (unsigned long)FTRACE_ADDR;
2405 }
2406 return ops->trampoline;
2407 }
2408
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002409 if (rec->flags & FTRACE_FL_REGS)
2410 return (unsigned long)FTRACE_REGS_ADDR;
2411 else
2412 return (unsigned long)FTRACE_ADDR;
2413}
2414
2415/**
2416 * ftrace_get_addr_curr - Get the call address that is already there
2417 * @rec: The ftrace record descriptor
2418 *
2419 * The FTRACE_FL_REGS_EN is set when the record already points to
2420 * a function that saves all the regs. Basically the '_EN' version
2421 * represents the current state of the function.
2422 *
2423 * Returns the address of the trampoline that is currently being called
2424 */
2425unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2426{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002427 struct ftrace_ops *ops;
2428
2429 /* Trampolines take precedence over regs */
2430 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2431 ops = ftrace_find_tramp_ops_curr(rec);
2432 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002433 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2434 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002435 /* Ftrace is shutting down, return anything */
2436 return (unsigned long)FTRACE_ADDR;
2437 }
2438 return ops->trampoline;
2439 }
2440
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002441 if (rec->flags & FTRACE_FL_REGS_EN)
2442 return (unsigned long)FTRACE_REGS_ADDR;
2443 else
2444 return (unsigned long)FTRACE_ADDR;
2445}
2446
Steven Rostedtc88fd862011-08-16 09:53:39 -04002447static int
2448__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2449{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002450 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002451 unsigned long ftrace_addr;
2452 int ret;
2453
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002454 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002455
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002456 /* This needs to be done before we call ftrace_update_record */
2457 ftrace_old_addr = ftrace_get_addr_curr(rec);
2458
2459 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002460
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002461 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2462
Steven Rostedtc88fd862011-08-16 09:53:39 -04002463 switch (ret) {
2464 case FTRACE_UPDATE_IGNORE:
2465 return 0;
2466
2467 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002468 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002469 return ftrace_make_call(rec, ftrace_addr);
2470
2471 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002472 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002473 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002474
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002475 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002476 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002477 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002478 }
2479
2480 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002481}
2482
Steven Rostedte4f5d542012-04-27 09:13:18 -04002483void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002484{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002485 struct dyn_ftrace *rec;
2486 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002487 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002488
Steven Rostedt45a4a232011-04-21 23:16:46 -04002489 if (unlikely(ftrace_disabled))
2490 return;
2491
Steven Rostedt265c8312009-02-13 12:43:56 -05002492 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002493
2494 if (rec->flags & FTRACE_FL_DISABLED)
2495 continue;
2496
Steven Rostedte4f5d542012-04-27 09:13:18 -04002497 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002498 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002499 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002500 /* Stop processing */
2501 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002502 }
2503 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002504}
2505
Steven Rostedtc88fd862011-08-16 09:53:39 -04002506struct ftrace_rec_iter {
2507 struct ftrace_page *pg;
2508 int index;
2509};
2510
2511/**
2512 * ftrace_rec_iter_start, start up iterating over traced functions
2513 *
2514 * Returns an iterator handle that is used to iterate over all
2515 * the records that represent address locations where functions
2516 * are traced.
2517 *
2518 * May return NULL if no records are available.
2519 */
2520struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2521{
2522 /*
2523 * We only use a single iterator.
2524 * Protected by the ftrace_lock mutex.
2525 */
2526 static struct ftrace_rec_iter ftrace_rec_iter;
2527 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2528
2529 iter->pg = ftrace_pages_start;
2530 iter->index = 0;
2531
2532 /* Could have empty pages */
2533 while (iter->pg && !iter->pg->index)
2534 iter->pg = iter->pg->next;
2535
2536 if (!iter->pg)
2537 return NULL;
2538
2539 return iter;
2540}
2541
2542/**
2543 * ftrace_rec_iter_next, get the next record to process.
2544 * @iter: The handle to the iterator.
2545 *
2546 * Returns the next iterator after the given iterator @iter.
2547 */
2548struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2549{
2550 iter->index++;
2551
2552 if (iter->index >= iter->pg->index) {
2553 iter->pg = iter->pg->next;
2554 iter->index = 0;
2555
2556 /* Could have empty pages */
2557 while (iter->pg && !iter->pg->index)
2558 iter->pg = iter->pg->next;
2559 }
2560
2561 if (!iter->pg)
2562 return NULL;
2563
2564 return iter;
2565}
2566
2567/**
2568 * ftrace_rec_iter_record, get the record at the iterator location
2569 * @iter: The current iterator location
2570 *
2571 * Returns the record that the current @iter is at.
2572 */
2573struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2574{
2575 return &iter->pg->records[iter->index];
2576}
2577
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302578static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002579ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002580{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002581 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002582
Steven Rostedt45a4a232011-04-21 23:16:46 -04002583 if (unlikely(ftrace_disabled))
2584 return 0;
2585
Shaohua Li25aac9d2009-01-09 11:29:40 +08002586 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002587 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002588 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002589 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302590 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002591 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302592 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002593}
2594
Steven Rostedt000ab692009-02-17 13:35:06 -05002595/*
2596 * archs can override this function if they must do something
2597 * before the modifying code is performed.
2598 */
2599int __weak ftrace_arch_code_modify_prepare(void)
2600{
2601 return 0;
2602}
2603
2604/*
2605 * archs can override this function if they must do something
2606 * after the modifying code is performed.
2607 */
2608int __weak ftrace_arch_code_modify_post_process(void)
2609{
2610 return 0;
2611}
2612
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002613void ftrace_modify_all_code(int command)
2614{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002615 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002616 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002617
2618 /*
2619 * If the ftrace_caller calls a ftrace_ops func directly,
2620 * we need to make sure that it only traces functions it
2621 * expects to trace. When doing the switch of functions,
2622 * we need to update to the ftrace_ops_list_func first
2623 * before the transition between old and new calls are set,
2624 * as the ftrace_ops_list_func will check the ops hashes
2625 * to make sure the ops are having the right functions
2626 * traced.
2627 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002628 if (update) {
2629 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2630 if (FTRACE_WARN_ON(err))
2631 return;
2632 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002633
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002634 if (command & FTRACE_UPDATE_CALLS)
2635 ftrace_replace_code(1);
2636 else if (command & FTRACE_DISABLE_CALLS)
2637 ftrace_replace_code(0);
2638
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002639 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2640 function_trace_op = set_function_trace_op;
2641 smp_wmb();
2642 /* If irqs are disabled, we are in stop machine */
2643 if (!irqs_disabled())
2644 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002645 err = ftrace_update_ftrace_func(ftrace_trace_function);
2646 if (FTRACE_WARN_ON(err))
2647 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002648 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002649
2650 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002651 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002652 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002653 err = ftrace_disable_ftrace_graph_caller();
2654 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002655}
2656
Ingo Molnare309b412008-05-12 21:20:51 +02002657static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002658{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002659 int *command = data;
2660
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002661 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002662
Steven Rostedtc88fd862011-08-16 09:53:39 -04002663 return 0;
2664}
2665
2666/**
2667 * ftrace_run_stop_machine, go back to the stop machine method
2668 * @command: The command to tell ftrace what to do
2669 *
2670 * If an arch needs to fall back to the stop machine method, the
2671 * it can call this function.
2672 */
2673void ftrace_run_stop_machine(int command)
2674{
2675 stop_machine(__ftrace_modify_code, &command, NULL);
2676}
2677
2678/**
2679 * arch_ftrace_update_code, modify the code to trace or not trace
2680 * @command: The command that needs to be done
2681 *
2682 * Archs can override this function if it does not need to
2683 * run stop_machine() to modify code.
2684 */
2685void __weak arch_ftrace_update_code(int command)
2686{
2687 ftrace_run_stop_machine(command);
2688}
2689
2690static void ftrace_run_update_code(int command)
2691{
2692 int ret;
2693
2694 ret = ftrace_arch_code_modify_prepare();
2695 FTRACE_WARN_ON(ret);
2696 if (ret)
2697 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002698
2699 /*
2700 * By default we use stop_machine() to modify the code.
2701 * But archs can do what ever they want as long as it
2702 * is safe. The stop_machine() is the safest, but also
2703 * produces the most overhead.
2704 */
2705 arch_ftrace_update_code(command);
2706
Steven Rostedt000ab692009-02-17 13:35:06 -05002707 ret = ftrace_arch_code_modify_post_process();
2708 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002709}
2710
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002711static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002712 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002713{
2714 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002715 ops->old_hash.filter_hash = old_hash->filter_hash;
2716 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002717 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002718 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002719 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002720 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2721}
2722
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002723static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002724static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002725
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002726void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2727{
2728}
2729
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002730static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002731{
2732 free_percpu(ops->disabled);
2733}
2734
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002735static void ftrace_startup_enable(int command)
2736{
2737 if (saved_ftrace_func != ftrace_trace_function) {
2738 saved_ftrace_func = ftrace_trace_function;
2739 command |= FTRACE_UPDATE_TRACE_FUNC;
2740 }
2741
2742 if (!command || !ftrace_enabled)
2743 return;
2744
2745 ftrace_run_update_code(command);
2746}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002747
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002748static void ftrace_startup_all(int command)
2749{
2750 update_all_ops = true;
2751 ftrace_startup_enable(command);
2752 update_all_ops = false;
2753}
2754
Steven Rostedta1cd6172011-05-23 15:24:25 -04002755static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002756{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002757 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002758
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002759 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002760 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002761
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002762 ret = __register_ftrace_function(ops);
2763 if (ret)
2764 return ret;
2765
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002766 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002767
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002768 /*
2769 * Note that ftrace probes uses this to start up
2770 * and modify functions it will probe. But we still
2771 * set the ADDING flag for modification, as probes
2772 * do not have trampolines. If they add them in the
2773 * future, then the probes will need to distinguish
2774 * between adding and updating probes.
2775 */
2776 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002777
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002778 ret = ftrace_hash_ipmodify_enable(ops);
2779 if (ret < 0) {
2780 /* Rollback registration process */
2781 __unregister_ftrace_function(ops);
2782 ftrace_start_up--;
2783 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2784 return ret;
2785 }
2786
Jiri Olsa7f50d062016-03-16 15:34:33 +01002787 if (ftrace_hash_rec_enable(ops, 1))
2788 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002789
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002790 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002791
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002792 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2793
Steven Rostedta1cd6172011-05-23 15:24:25 -04002794 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002795}
2796
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002797static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002798{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002799 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002800
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002801 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002802 return -ENODEV;
2803
2804 ret = __unregister_ftrace_function(ops);
2805 if (ret)
2806 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002807
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002808 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002809 /*
2810 * Just warn in case of unbalance, no need to kill ftrace, it's not
2811 * critical but the ftrace_call callers may be never nopped again after
2812 * further ftrace uses.
2813 */
2814 WARN_ON_ONCE(ftrace_start_up < 0);
2815
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002816 /* Disabling ipmodify never fails */
2817 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002818
2819 if (ftrace_hash_rec_disable(ops, 1))
2820 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002821
Namhyung Kima737e6d2014-06-12 23:56:12 +09002822 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002823
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002824 if (saved_ftrace_func != ftrace_trace_function) {
2825 saved_ftrace_func = ftrace_trace_function;
2826 command |= FTRACE_UPDATE_TRACE_FUNC;
2827 }
2828
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002829 if (!command || !ftrace_enabled) {
2830 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002831 * If these are dynamic or per_cpu ops, they still
2832 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002833 * not currently active, we can just free them
2834 * without synchronizing all CPUs.
2835 */
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002836 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2837 goto free_ops;
2838
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002839 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002840 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002841
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002842 /*
2843 * If the ops uses a trampoline, then it needs to be
2844 * tested first on update.
2845 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002846 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002847 removed_ops = ops;
2848
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002849 /* The trampoline logic checks the old hashes */
2850 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2851 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2852
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002853 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002854
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002855 /*
2856 * If there's no more ops registered with ftrace, run a
2857 * sanity check to make sure all rec flags are cleared.
2858 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002859 if (rcu_dereference_protected(ftrace_ops_list,
2860 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002861 struct ftrace_page *pg;
2862 struct dyn_ftrace *rec;
2863
2864 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002865 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002866 pr_warn(" %pS flags:%lx\n",
2867 (void *)rec->ip, rec->flags);
2868 } while_for_each_ftrace_rec();
2869 }
2870
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002871 ops->old_hash.filter_hash = NULL;
2872 ops->old_hash.notrace_hash = NULL;
2873
2874 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002875 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002876
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002877 /*
2878 * Dynamic ops may be freed, we must make sure that all
2879 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002880 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002881 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002882 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002883 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002884 /*
2885 * We need to do a hard force of sched synchronization.
2886 * This is because we use preempt_disable() to do RCU, but
2887 * the function tracers can be called where RCU is not watching
2888 * (like before user_exit()). We can not rely on the RCU
2889 * infrastructure to do the synchronization, thus we must do it
2890 * ourselves.
2891 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002892 schedule_on_each_cpu(ftrace_sync);
2893
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002894 /*
2895 * When the kernel is preeptive, tasks can be preempted
2896 * while on a ftrace trampoline. Just scheduling a task on
2897 * a CPU is not good enough to flush them. Calling
2898 * synchornize_rcu_tasks() will wait for those tasks to
2899 * execute and either schedule voluntarily or enter user space.
2900 */
2901 if (IS_ENABLED(CONFIG_PREEMPT))
2902 synchronize_rcu_tasks();
2903
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002904 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002905 arch_ftrace_trampoline_free(ops);
2906
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002907 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2908 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002909 }
2910
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002911 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002912}
2913
Ingo Molnare309b412008-05-12 21:20:51 +02002914static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002915{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302916 int command;
2917
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002918 if (unlikely(ftrace_disabled))
2919 return;
2920
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002921 /* Force update next time */
2922 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002923 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302924 if (ftrace_start_up) {
2925 command = FTRACE_UPDATE_CALLS;
2926 if (ftrace_graph_active)
2927 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002928 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302929 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002930}
2931
Ingo Molnare309b412008-05-12 21:20:51 +02002932static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002933{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302934 int command;
2935
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002936 if (unlikely(ftrace_disabled))
2937 return;
2938
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002939 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302940 if (ftrace_start_up) {
2941 command = FTRACE_DISABLE_CALLS;
2942 if (ftrace_graph_active)
2943 command |= FTRACE_STOP_FUNC_RET;
2944 ftrace_run_update_code(command);
2945 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002946}
2947
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002948static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002949unsigned long ftrace_update_tot_cnt;
2950
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002951static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002952{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002953 /*
2954 * Filter_hash being empty will default to trace module.
2955 * But notrace hash requires a test of individual module functions.
2956 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002957 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2958 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002959}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002960
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002961/*
2962 * Check if the current ops references the record.
2963 *
2964 * If the ops traces all functions, then it was already accounted for.
2965 * If the ops does not trace the current record function, skip it.
2966 * If the ops ignores the function via notrace filter, skip it.
2967 */
2968static inline bool
2969ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2970{
2971 /* If ops isn't enabled, ignore it */
2972 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2973 return 0;
2974
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002975 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002976 if (ops_traces_mod(ops))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002977 return 1;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002978
2979 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002980 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002981 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002982 return 0;
2983
2984 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002985 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002986 return 0;
2987
2988 return 1;
2989}
2990
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002991static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002992{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002993 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002994 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002995 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002996 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002997 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002998 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002999
Ingo Molnar750ed1a2008-05-12 21:20:46 +02003000 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02003001
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003002 /*
3003 * When a module is loaded, this function is called to convert
3004 * the calls to mcount in its text to nops, and also to create
3005 * an entry in the ftrace data. Now, if ftrace is activated
3006 * after this call, but before the module sets its text to
3007 * read-only, the modification of enabling ftrace can fail if
3008 * the read-only is done while ftrace is converting the calls.
3009 * To prevent this, the module's records are set as disabled
3010 * and will be enabled after the call to set the module's text
3011 * to read-only.
3012 */
3013 if (mod)
3014 rec_flags |= FTRACE_FL_DISABLED;
3015
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003016 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05303017
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003018 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003019
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003020 /* If something went wrong, bail without enabling anything */
3021 if (unlikely(ftrace_disabled))
3022 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02003023
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003024 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003025 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05303026
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003027 /*
3028 * Do the initial record conversion from mcount jump
3029 * to the NOP instructions.
3030 */
3031 if (!ftrace_code_disable(mod, p))
3032 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003033
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003034 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003035 }
Steven Rostedt3d083392008-05-12 21:20:42 +02003036 }
3037
Ingo Molnar750ed1a2008-05-12 21:20:46 +02003038 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02003039 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003040 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02003041
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003042 return 0;
3043}
3044
Steven Rostedta7900872011-12-16 16:23:44 -05003045static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003046{
Steven Rostedta7900872011-12-16 16:23:44 -05003047 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003048 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003049
Steven Rostedta7900872011-12-16 16:23:44 -05003050 if (WARN_ON(!count))
3051 return -EINVAL;
3052
3053 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003054
3055 /*
Steven Rostedta7900872011-12-16 16:23:44 -05003056 * We want to fill as much as possible. No more than a page
3057 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003058 */
Steven Rostedta7900872011-12-16 16:23:44 -05003059 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3060 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003061
Steven Rostedta7900872011-12-16 16:23:44 -05003062 again:
3063 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3064
3065 if (!pg->records) {
3066 /* if we can't allocate this size, try something smaller */
3067 if (!order)
3068 return -ENOMEM;
3069 order >>= 1;
3070 goto again;
3071 }
3072
3073 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3074 pg->size = cnt;
3075
3076 if (cnt > count)
3077 cnt = count;
3078
3079 return cnt;
3080}
3081
3082static struct ftrace_page *
3083ftrace_allocate_pages(unsigned long num_to_init)
3084{
3085 struct ftrace_page *start_pg;
3086 struct ftrace_page *pg;
3087 int order;
3088 int cnt;
3089
3090 if (!num_to_init)
3091 return 0;
3092
3093 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3094 if (!pg)
3095 return NULL;
3096
3097 /*
3098 * Try to allocate as much as possible in one continues
3099 * location that fills in all of the space. We want to
3100 * waste as little space as possible.
3101 */
3102 for (;;) {
3103 cnt = ftrace_allocate_records(pg, num_to_init);
3104 if (cnt < 0)
3105 goto free_pages;
3106
3107 num_to_init -= cnt;
3108 if (!num_to_init)
3109 break;
3110
3111 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3112 if (!pg->next)
3113 goto free_pages;
3114
3115 pg = pg->next;
3116 }
3117
3118 return start_pg;
3119
3120 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003121 pg = start_pg;
3122 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003123 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3124 free_pages((unsigned long)pg->records, order);
3125 start_pg = pg->next;
3126 kfree(pg);
3127 pg = start_pg;
3128 }
3129 pr_info("ftrace: FAILED to allocate memory for functions\n");
3130 return NULL;
3131}
3132
Steven Rostedt5072c592008-05-12 21:20:43 +02003133#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3134
3135struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003136 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003137 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003138 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003139 struct ftrace_page *pg;
3140 struct dyn_ftrace *func;
3141 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003142 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003143 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003144 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003145 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003146 struct trace_array *tr;
3147 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003148 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003149 int idx;
3150 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003151};
3152
Ingo Molnare309b412008-05-12 21:20:51 +02003153static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003154t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003155{
3156 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003157 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003158 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003159 struct ftrace_hash *hash;
3160 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003161 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003162 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003163 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003164
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003165 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003166 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003167
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003168 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003169 return NULL;
3170
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003171 func_probes = &tr->func_probes;
3172 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003173 return NULL;
3174
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003175 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003176 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003177 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003178 }
3179
3180 if (iter->probe_entry)
3181 hnd = &iter->probe_entry->hlist;
3182
3183 hash = iter->probe->ops.func_hash->filter_hash;
3184 size = 1 << hash->size_bits;
3185
3186 retry:
3187 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003188 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003189 return NULL;
3190 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003191 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003192 hash = iter->probe->ops.func_hash->filter_hash;
3193 size = 1 << hash->size_bits;
3194 iter->pidx = 0;
3195 }
3196
3197 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003198
3199 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003200 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003201 hnd = NULL;
3202 goto retry;
3203 }
3204
3205 if (!hnd)
3206 hnd = hhd->first;
3207 else {
3208 hnd = hnd->next;
3209 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003210 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003211 goto retry;
3212 }
3213 }
3214
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003215 if (WARN_ON_ONCE(!hnd))
3216 return NULL;
3217
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003218 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003219
3220 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003221}
3222
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003223static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003224{
3225 struct ftrace_iterator *iter = m->private;
3226 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003227 loff_t l;
3228
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003229 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003230 return NULL;
3231
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003232 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003233 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003234
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003235 iter->probe = NULL;
3236 iter->probe_entry = NULL;
3237 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003238 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003239 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003240 if (!p)
3241 break;
3242 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003243 if (!p)
3244 return NULL;
3245
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003246 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003247 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003248
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003249 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003250}
3251
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003252static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003253t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003254{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003255 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003256 struct ftrace_probe_ops *probe_ops;
3257 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003258
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003259 probe = iter->probe;
3260 probe_entry = iter->probe_entry;
3261
3262 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003263 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003264
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003265 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003266
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003267 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003268 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003269
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003270 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3271 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003272
3273 return 0;
3274}
3275
3276static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003277t_mod_next(struct seq_file *m, loff_t *pos)
3278{
3279 struct ftrace_iterator *iter = m->private;
3280 struct trace_array *tr = iter->tr;
3281
3282 (*pos)++;
3283 iter->pos = *pos;
3284
3285 iter->mod_list = iter->mod_list->next;
3286
3287 if (iter->mod_list == &tr->mod_trace ||
3288 iter->mod_list == &tr->mod_notrace) {
3289 iter->flags &= ~FTRACE_ITER_MOD;
3290 return NULL;
3291 }
3292
3293 iter->mod_pos = *pos;
3294
3295 return iter;
3296}
3297
3298static void *t_mod_start(struct seq_file *m, loff_t *pos)
3299{
3300 struct ftrace_iterator *iter = m->private;
3301 void *p = NULL;
3302 loff_t l;
3303
3304 if (iter->func_pos > *pos)
3305 return NULL;
3306
3307 iter->mod_pos = iter->func_pos;
3308
3309 /* probes are only available if tr is set */
3310 if (!iter->tr)
3311 return NULL;
3312
3313 for (l = 0; l <= (*pos - iter->func_pos); ) {
3314 p = t_mod_next(m, &l);
3315 if (!p)
3316 break;
3317 }
3318 if (!p) {
3319 iter->flags &= ~FTRACE_ITER_MOD;
3320 return t_probe_start(m, pos);
3321 }
3322
3323 /* Only set this if we have an item */
3324 iter->flags |= FTRACE_ITER_MOD;
3325
3326 return iter;
3327}
3328
3329static int
3330t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3331{
3332 struct ftrace_mod_load *ftrace_mod;
3333 struct trace_array *tr = iter->tr;
3334
3335 if (WARN_ON_ONCE(!iter->mod_list) ||
3336 iter->mod_list == &tr->mod_trace ||
3337 iter->mod_list == &tr->mod_notrace)
3338 return -EIO;
3339
3340 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3341
3342 if (ftrace_mod->func)
3343 seq_printf(m, "%s", ftrace_mod->func);
3344 else
3345 seq_putc(m, '*');
3346
3347 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3348
3349 return 0;
3350}
3351
3352static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003353t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003354{
3355 struct ftrace_iterator *iter = m->private;
3356 struct dyn_ftrace *rec = NULL;
3357
3358 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003359
Steven Rostedt5072c592008-05-12 21:20:43 +02003360 retry:
3361 if (iter->idx >= iter->pg->index) {
3362 if (iter->pg->next) {
3363 iter->pg = iter->pg->next;
3364 iter->idx = 0;
3365 goto retry;
3366 }
3367 } else {
3368 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003369 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3370 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003371
3372 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003373 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003374
Steven Rostedt5072c592008-05-12 21:20:43 +02003375 rec = NULL;
3376 goto retry;
3377 }
3378 }
3379
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003380 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003381 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003382
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003383 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003384 iter->func = rec;
3385
3386 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003387}
3388
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003389static void *
3390t_next(struct seq_file *m, void *v, loff_t *pos)
3391{
3392 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003393 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003394 void *ret;
3395
3396 if (unlikely(ftrace_disabled))
3397 return NULL;
3398
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003399 if (iter->flags & FTRACE_ITER_PROBE)
3400 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003401
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003402 if (iter->flags & FTRACE_ITER_MOD)
3403 return t_mod_next(m, pos);
3404
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003405 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003406 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003407 (*pos)++;
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
3411 ret = t_func_next(m, pos);
3412
3413 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003414 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003415
3416 return ret;
3417}
3418
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003419static void reset_iter_read(struct ftrace_iterator *iter)
3420{
3421 iter->pos = 0;
3422 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003423 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003424}
3425
3426static void *t_start(struct seq_file *m, loff_t *pos)
3427{
3428 struct ftrace_iterator *iter = m->private;
3429 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003430 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003431
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003432 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003433
3434 if (unlikely(ftrace_disabled))
3435 return NULL;
3436
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003437 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003438 * If an lseek was done, then reset and start from beginning.
3439 */
3440 if (*pos < iter->pos)
3441 reset_iter_read(iter);
3442
3443 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003444 * For set_ftrace_filter reading, if we have the filter
3445 * off, we can short cut and just print out that all
3446 * functions are enabled.
3447 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003448 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3449 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003450 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003451 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003452 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003453 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003454 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003455 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003456 return iter;
3457 }
3458
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003459 if (iter->flags & FTRACE_ITER_MOD)
3460 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003461
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003462 /*
3463 * Unfortunately, we need to restart at ftrace_pages_start
3464 * every time we let go of the ftrace_mutex. This is because
3465 * those pointers can change without the lock.
3466 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003467 iter->pg = ftrace_pages_start;
3468 iter->idx = 0;
3469 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003470 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003471 if (!p)
3472 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003473 }
walimis5821e1b2008-11-15 15:19:06 +08003474
Steven Rostedt69a30832011-12-19 15:21:16 -05003475 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003476 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003477
3478 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003479}
3480
3481static void t_stop(struct seq_file *m, void *p)
3482{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003483 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003484}
3485
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003486void * __weak
3487arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3488{
3489 return NULL;
3490}
3491
3492static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3493 struct dyn_ftrace *rec)
3494{
3495 void *ptr;
3496
3497 ptr = arch_ftrace_trampoline_func(ops, rec);
3498 if (ptr)
3499 seq_printf(m, " ->%pS", ptr);
3500}
3501
Steven Rostedt5072c592008-05-12 21:20:43 +02003502static int t_show(struct seq_file *m, void *v)
3503{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003504 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003505 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003506
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003507 if (iter->flags & FTRACE_ITER_PROBE)
3508 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003509
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003510 if (iter->flags & FTRACE_ITER_MOD)
3511 return t_mod_show(m, iter);
3512
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003513 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003514 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003515 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003516 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003517 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003518 return 0;
3519 }
3520
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003521 rec = iter->func;
3522
Steven Rostedt5072c592008-05-12 21:20:43 +02003523 if (!rec)
3524 return 0;
3525
Steven Rostedt647bcd02011-05-03 14:39:21 -04003526 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003527 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003528 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003529
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003530 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003531 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003532 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3533 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003534 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003535 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003536 if (ops) {
3537 do {
3538 seq_printf(m, "\ttramp: %pS (%pS)",
3539 (void *)ops->trampoline,
3540 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003541 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003542 ops = ftrace_find_tramp_ops_next(rec, ops);
3543 } while (ops);
3544 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003545 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003546 } else {
3547 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003548 }
3549 }
3550
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003551 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003552
3553 return 0;
3554}
3555
James Morris88e9d342009-09-22 16:43:43 -07003556static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003557 .start = t_start,
3558 .next = t_next,
3559 .stop = t_stop,
3560 .show = t_show,
3561};
3562
Ingo Molnare309b412008-05-12 21:20:51 +02003563static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003564ftrace_avail_open(struct inode *inode, struct file *file)
3565{
3566 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003567
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003568 if (unlikely(ftrace_disabled))
3569 return -ENODEV;
3570
Jiri Olsa50e18b92012-04-25 10:23:39 +02003571 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003572 if (!iter)
3573 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003574
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003575 iter->pg = ftrace_pages_start;
3576 iter->ops = &global_ops;
3577
3578 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003579}
3580
Steven Rostedt647bcd02011-05-03 14:39:21 -04003581static int
3582ftrace_enabled_open(struct inode *inode, struct file *file)
3583{
3584 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003585
Jiri Olsa50e18b92012-04-25 10:23:39 +02003586 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003587 if (!iter)
3588 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003589
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003590 iter->pg = ftrace_pages_start;
3591 iter->flags = FTRACE_ITER_ENABLED;
3592 iter->ops = &global_ops;
3593
3594 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003595}
3596
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003597/**
3598 * ftrace_regex_open - initialize function tracer filter files
3599 * @ops: The ftrace_ops that hold the hash filters
3600 * @flag: The type of filter to process
3601 * @inode: The inode, usually passed in to your open routine
3602 * @file: The file, usually passed in to your open routine
3603 *
3604 * ftrace_regex_open() initializes the filter files for the
3605 * @ops. Depending on @flag it may process the filter hash or
3606 * the notrace hash of @ops. With this called from the open
3607 * routine, you can use ftrace_filter_write() for the write
3608 * routine if @flag has FTRACE_ITER_FILTER set, or
3609 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003610 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003611 * release must call ftrace_regex_release().
3612 */
3613int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003614ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003615 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003616{
3617 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003618 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003619 struct list_head *mod_head;
3620 struct trace_array *tr = ops->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003621 int ret = 0;
3622
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003623 ftrace_ops_init(ops);
3624
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003625 if (unlikely(ftrace_disabled))
3626 return -ENODEV;
3627
Steven Rostedt5072c592008-05-12 21:20:43 +02003628 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3629 if (!iter)
3630 return -ENOMEM;
3631
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003632 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3633 kfree(iter);
3634 return -ENOMEM;
3635 }
3636
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003637 iter->ops = ops;
3638 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003639 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003640
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003641 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003642
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003643 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003644 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003645 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003646 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003647 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003648 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003649 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003650
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003651 iter->mod_list = mod_head;
3652
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003653 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003654 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3655
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003656 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003657 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003658 clear_ftrace_mod_list(mod_head);
3659 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003660 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003661 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003662
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003663 if (!iter->hash) {
3664 trace_parser_put(&iter->parser);
3665 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003666 ret = -ENOMEM;
3667 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003668 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003669 } else
3670 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003671
Steven Rostedt5072c592008-05-12 21:20:43 +02003672 if (file->f_mode & FMODE_READ) {
3673 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003674
3675 ret = seq_open(file, &show_ftrace_seq_ops);
3676 if (!ret) {
3677 struct seq_file *m = file->private_data;
3678 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003679 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003680 /* Failed */
3681 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003682 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003683 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003684 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003685 } else
3686 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003687
3688 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003689 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003690
3691 return ret;
3692}
3693
Steven Rostedt41c52c02008-05-22 11:46:33 -04003694static int
3695ftrace_filter_open(struct inode *inode, struct file *file)
3696{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003697 struct ftrace_ops *ops = inode->i_private;
3698
3699 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003700 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003701 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003702}
3703
3704static int
3705ftrace_notrace_open(struct inode *inode, struct file *file)
3706{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003707 struct ftrace_ops *ops = inode->i_private;
3708
3709 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003710 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003711}
3712
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003713/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3714struct ftrace_glob {
3715 char *search;
3716 unsigned len;
3717 int type;
3718};
3719
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003720/*
3721 * If symbols in an architecture don't correspond exactly to the user-visible
3722 * name of what they represent, it is possible to define this function to
3723 * perform the necessary adjustments.
3724*/
3725char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3726{
3727 return str;
3728}
3729
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003730static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003731{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003732 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003733 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003734
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003735 str = arch_ftrace_match_adjust(str, g->search);
3736
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003737 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003738 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003739 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003740 matched = 1;
3741 break;
3742 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003743 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003744 matched = 1;
3745 break;
3746 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003747 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003748 matched = 1;
3749 break;
3750 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003751 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003752 if (slen >= g->len &&
3753 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003754 matched = 1;
3755 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003756 case MATCH_GLOB:
3757 if (glob_match(g->search, str))
3758 matched = 1;
3759 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003760 }
3761
3762 return matched;
3763}
3764
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003765static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003766enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003767{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003768 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003769 int ret = 0;
3770
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003771 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003772 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003773 /* Do nothing if it doesn't exist */
3774 if (!entry)
3775 return 0;
3776
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003777 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003778 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003779 /* Do nothing if it exists */
3780 if (entry)
3781 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003782
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003783 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003784 }
3785 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003786}
3787
Steven Rostedt64e7c442009-02-13 17:08:48 -05003788static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003789ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3790 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003791{
3792 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003793 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003794
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003795 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3796
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003797 if (mod_g) {
3798 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3799
3800 /* blank module name to match all modules */
3801 if (!mod_g->len) {
3802 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003803 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003804 goto func_match;
3805 return 0;
3806 }
3807
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003808 /*
3809 * exclude_mod is set to trace everything but the given
3810 * module. If it is set and the module matches, then
3811 * return 0. If it is not set, and the module doesn't match
3812 * also return 0. Otherwise, check the function to see if
3813 * that matches.
3814 */
3815 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003816 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003817func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003818 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003819 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003820 return 1;
3821 }
3822
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003823 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003824}
3825
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003826static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003827match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003828{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003829 struct ftrace_page *pg;
3830 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003831 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003832 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3833 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3834 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003835 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003836 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03003837 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003838
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003839 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003840 func_g.type = filter_parse_regex(func, len, &func_g.search,
3841 &clear_filter);
3842 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003843 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003844
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003845 if (mod) {
3846 mod_g.type = filter_parse_regex(mod, strlen(mod),
3847 &mod_g.search, &exclude_mod);
3848 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003849 }
3850
Steven Rostedt52baf112009-02-14 01:15:39 -05003851 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003852
3853 if (unlikely(ftrace_disabled))
3854 goto out_unlock;
3855
Steven Rostedt265c8312009-02-13 12:43:56 -05003856 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003857
3858 if (rec->flags & FTRACE_FL_DISABLED)
3859 continue;
3860
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003861 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003862 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003863 if (ret < 0) {
3864 found = ret;
3865 goto out_unlock;
3866 }
Li Zefan311d16d2009-12-08 11:15:11 +08003867 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003868 }
3869 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003870 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003871 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003872
3873 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003874}
3875
Steven Rostedt64e7c442009-02-13 17:08:48 -05003876static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003877ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003878{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003879 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003880}
3881
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003882static void ftrace_ops_update_code(struct ftrace_ops *ops,
3883 struct ftrace_ops_hash *old_hash)
3884{
3885 struct ftrace_ops *op;
3886
3887 if (!ftrace_enabled)
3888 return;
3889
3890 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3891 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3892 return;
3893 }
3894
3895 /*
3896 * If this is the shared global_ops filter, then we need to
3897 * check if there is another ops that shares it, is enabled.
3898 * If so, we still need to run the modify code.
3899 */
3900 if (ops->func_hash != &global_ops.local_hash)
3901 return;
3902
3903 do_for_each_ftrace_op(op, ftrace_ops_list) {
3904 if (op->func_hash == &global_ops.local_hash &&
3905 op->flags & FTRACE_OPS_FL_ENABLED) {
3906 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3907 /* Only need to do this once */
3908 return;
3909 }
3910 } while_for_each_ftrace_op(op);
3911}
3912
3913static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3914 struct ftrace_hash **orig_hash,
3915 struct ftrace_hash *hash,
3916 int enable)
3917{
3918 struct ftrace_ops_hash old_hash_ops;
3919 struct ftrace_hash *old_hash;
3920 int ret;
3921
3922 old_hash = *orig_hash;
3923 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3924 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3925 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3926 if (!ret) {
3927 ftrace_ops_update_code(ops, &old_hash_ops);
3928 free_ftrace_hash_rcu(old_hash);
3929 }
3930 return ret;
3931}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003932
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003933static bool module_exists(const char *module)
3934{
3935 /* All modules have the symbol __this_module */
3936 const char this_mod[] = "__this_module";
3937 const int modname_size = MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 1;
3938 char modname[modname_size + 1];
3939 unsigned long val;
3940 int n;
3941
3942 n = snprintf(modname, modname_size + 1, "%s:%s", module, this_mod);
3943
3944 if (n > modname_size)
3945 return false;
3946
3947 val = module_kallsyms_lookup_name(modname);
3948 return val != 0;
3949}
3950
3951static int cache_mod(struct trace_array *tr,
3952 const char *func, char *module, int enable)
3953{
3954 struct ftrace_mod_load *ftrace_mod, *n;
3955 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3956 int ret;
3957
3958 mutex_lock(&ftrace_lock);
3959
3960 /* We do not cache inverse filters */
3961 if (func[0] == '!') {
3962 func++;
3963 ret = -EINVAL;
3964
3965 /* Look to remove this hash */
3966 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3967 if (strcmp(ftrace_mod->module, module) != 0)
3968 continue;
3969
3970 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03003971 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003972 (ftrace_mod->func &&
3973 strcmp(ftrace_mod->func, func) == 0)) {
3974 ret = 0;
3975 free_ftrace_mod(ftrace_mod);
3976 continue;
3977 }
3978 }
3979 goto out;
3980 }
3981
3982 ret = -EINVAL;
3983 /* We only care about modules that have not been loaded yet */
3984 if (module_exists(module))
3985 goto out;
3986
3987 /* Save this string off, and execute it when the module is loaded */
3988 ret = ftrace_add_mod(tr, func, module, enable);
3989 out:
3990 mutex_unlock(&ftrace_lock);
3991
3992 return ret;
3993}
3994
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003995static int
3996ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3997 int reset, int enable);
3998
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003999#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004000static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4001 char *mod, bool enable)
4002{
4003 struct ftrace_mod_load *ftrace_mod, *n;
4004 struct ftrace_hash **orig_hash, *new_hash;
4005 LIST_HEAD(process_mods);
4006 char *func;
4007 int ret;
4008
4009 mutex_lock(&ops->func_hash->regex_lock);
4010
4011 if (enable)
4012 orig_hash = &ops->func_hash->filter_hash;
4013 else
4014 orig_hash = &ops->func_hash->notrace_hash;
4015
4016 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4017 *orig_hash);
4018 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004019 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004020
4021 mutex_lock(&ftrace_lock);
4022
4023 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4024
4025 if (strcmp(ftrace_mod->module, mod) != 0)
4026 continue;
4027
4028 if (ftrace_mod->func)
4029 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4030 else
4031 func = kstrdup("*", GFP_KERNEL);
4032
4033 if (!func) /* warn? */
4034 continue;
4035
4036 list_del(&ftrace_mod->list);
4037 list_add(&ftrace_mod->list, &process_mods);
4038
4039 /* Use the newly allocated func, as it may be "*" */
4040 kfree(ftrace_mod->func);
4041 ftrace_mod->func = func;
4042 }
4043
4044 mutex_unlock(&ftrace_lock);
4045
4046 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4047
4048 func = ftrace_mod->func;
4049
4050 /* Grabs ftrace_lock, which is why we have this extra step */
4051 match_records(new_hash, func, strlen(func), mod);
4052 free_ftrace_mod(ftrace_mod);
4053 }
4054
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004055 if (enable && list_empty(head))
4056 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4057
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004058 mutex_lock(&ftrace_lock);
4059
4060 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4061 new_hash, enable);
4062 mutex_unlock(&ftrace_lock);
4063
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004064 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004065 mutex_unlock(&ops->func_hash->regex_lock);
4066
4067 free_ftrace_hash(new_hash);
4068}
4069
4070static void process_cached_mods(const char *mod_name)
4071{
4072 struct trace_array *tr;
4073 char *mod;
4074
4075 mod = kstrdup(mod_name, GFP_KERNEL);
4076 if (!mod)
4077 return;
4078
4079 mutex_lock(&trace_types_lock);
4080 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4081 if (!list_empty(&tr->mod_trace))
4082 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4083 if (!list_empty(&tr->mod_notrace))
4084 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4085 }
4086 mutex_unlock(&trace_types_lock);
4087
4088 kfree(mod);
4089}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004090#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004091
Steven Rostedtf6180772009-02-14 00:40:25 -05004092/*
4093 * We register the module command as a template to show others how
4094 * to register the a command as well.
4095 */
4096
4097static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004098ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004099 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004100{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004101 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004102 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004103
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004104 /* match_records() modifies func, and we need the original */
4105 func = kstrdup(func_orig, GFP_KERNEL);
4106 if (!func)
4107 return -ENOMEM;
4108
Steven Rostedtf6180772009-02-14 00:40:25 -05004109 /*
4110 * cmd == 'mod' because we only registered this func
4111 * for the 'mod' ftrace_func_command.
4112 * But if you register one func with multiple commands,
4113 * you can tell which command was used by the cmd
4114 * parameter.
4115 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004116 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004117 kfree(func);
4118
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004119 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004120 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004121 if (ret < 0)
4122 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004123 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004124}
4125
4126static struct ftrace_func_command ftrace_mod_cmd = {
4127 .name = "mod",
4128 .func = ftrace_mod_callback,
4129};
4130
4131static int __init ftrace_mod_cmd_init(void)
4132{
4133 return register_ftrace_command(&ftrace_mod_cmd);
4134}
Steven Rostedt6f415672012-10-05 12:13:07 -04004135core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004136
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004137static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004138 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004139{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004140 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004141 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004142
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004143 probe = container_of(op, struct ftrace_func_probe, ops);
4144 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004145
4146 /*
4147 * Disable preemption for these calls to prevent a RCU grace
4148 * period. This syncs the hash iteration and freeing of items
4149 * on the hash. rcu_read_lock is too dangerous here.
4150 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004151 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004152 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004153 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004154}
4155
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004156struct ftrace_func_map {
4157 struct ftrace_func_entry entry;
4158 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004159};
4160
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004161struct ftrace_func_mapper {
4162 struct ftrace_hash hash;
4163};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004164
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004165/**
4166 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4167 *
4168 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4169 */
4170struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004171{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004172 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004173
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004174 /*
4175 * The mapper is simply a ftrace_hash, but since the entries
4176 * in the hash are not ftrace_func_entry type, we define it
4177 * as a separate structure.
4178 */
4179 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4180 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004181}
4182
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004183/**
4184 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4185 * @mapper: The mapper that has the ip maps
4186 * @ip: the instruction pointer to find the data for
4187 *
4188 * Returns the data mapped to @ip if found otherwise NULL. The return
4189 * is actually the address of the mapper data pointer. The address is
4190 * returned for use cases where the data is no bigger than a long, and
4191 * the user can use the data pointer as its data instead of having to
4192 * allocate more memory for the reference.
4193 */
4194void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4195 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004196{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004197 struct ftrace_func_entry *entry;
4198 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004199
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004200 entry = ftrace_lookup_ip(&mapper->hash, ip);
4201 if (!entry)
4202 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004203
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004204 map = (struct ftrace_func_map *)entry;
4205 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004206}
4207
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004208/**
4209 * ftrace_func_mapper_add_ip - Map some data to an ip
4210 * @mapper: The mapper that has the ip maps
4211 * @ip: The instruction pointer address to map @data to
4212 * @data: The data to map to @ip
4213 *
4214 * Returns 0 on succes otherwise an error.
4215 */
4216int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4217 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004218{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004219 struct ftrace_func_entry *entry;
4220 struct ftrace_func_map *map;
4221
4222 entry = ftrace_lookup_ip(&mapper->hash, ip);
4223 if (entry)
4224 return -EBUSY;
4225
4226 map = kmalloc(sizeof(*map), GFP_KERNEL);
4227 if (!map)
4228 return -ENOMEM;
4229
4230 map->entry.ip = ip;
4231 map->data = data;
4232
4233 __add_hash_entry(&mapper->hash, &map->entry);
4234
4235 return 0;
4236}
4237
4238/**
4239 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4240 * @mapper: The mapper that has the ip maps
4241 * @ip: The instruction pointer address to remove the data from
4242 *
4243 * Returns the data if it is found, otherwise NULL.
4244 * Note, if the data pointer is used as the data itself, (see
4245 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4246 * if the data pointer was set to zero.
4247 */
4248void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4249 unsigned long ip)
4250{
4251 struct ftrace_func_entry *entry;
4252 struct ftrace_func_map *map;
4253 void *data;
4254
4255 entry = ftrace_lookup_ip(&mapper->hash, ip);
4256 if (!entry)
4257 return NULL;
4258
4259 map = (struct ftrace_func_map *)entry;
4260 data = map->data;
4261
4262 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004263 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004264
4265 return data;
4266}
4267
4268/**
4269 * free_ftrace_func_mapper - free a mapping of ips and data
4270 * @mapper: The mapper that has the ip maps
4271 * @free_func: A function to be called on each data item.
4272 *
4273 * This is used to free the function mapper. The @free_func is optional
4274 * and can be used if the data needs to be freed as well.
4275 */
4276void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4277 ftrace_mapper_func free_func)
4278{
4279 struct ftrace_func_entry *entry;
4280 struct ftrace_func_map *map;
4281 struct hlist_head *hhd;
4282 int size = 1 << mapper->hash.size_bits;
4283 int i;
4284
4285 if (free_func && mapper->hash.count) {
4286 for (i = 0; i < size; i++) {
4287 hhd = &mapper->hash.buckets[i];
4288 hlist_for_each_entry(entry, hhd, hlist) {
4289 map = (struct ftrace_func_map *)entry;
4290 free_func(map);
4291 }
4292 }
4293 }
4294 free_ftrace_hash(&mapper->hash);
4295}
4296
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004297static void release_probe(struct ftrace_func_probe *probe)
4298{
4299 struct ftrace_probe_ops *probe_ops;
4300
4301 mutex_lock(&ftrace_lock);
4302
4303 WARN_ON(probe->ref <= 0);
4304
4305 /* Subtract the ref that was used to protect this instance */
4306 probe->ref--;
4307
4308 if (!probe->ref) {
4309 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004310 /*
4311 * Sending zero as ip tells probe_ops to free
4312 * the probe->data itself
4313 */
4314 if (probe_ops->free)
4315 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004316 list_del(&probe->list);
4317 kfree(probe);
4318 }
4319 mutex_unlock(&ftrace_lock);
4320}
4321
4322static void acquire_probe_locked(struct ftrace_func_probe *probe)
4323{
4324 /*
4325 * Add one ref to keep it from being freed when releasing the
4326 * ftrace_lock mutex.
4327 */
4328 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004329}
4330
Steven Rostedt59df055f2009-02-14 15:29:06 -05004331int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004332register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004333 struct ftrace_probe_ops *probe_ops,
4334 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004335{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004336 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004337 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004338 struct ftrace_hash **orig_hash;
4339 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004340 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004341 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004342 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004343 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004344 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004345
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004346 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004347 return -EINVAL;
4348
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004349 /* We do not support '!' for function probes */
4350 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004351 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004352
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004353
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004354 mutex_lock(&ftrace_lock);
4355 /* Check if the probe_ops is already registered */
4356 list_for_each_entry(probe, &tr->func_probes, list) {
4357 if (probe->probe_ops == probe_ops)
4358 break;
4359 }
4360 if (&probe->list == &tr->func_probes) {
4361 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4362 if (!probe) {
4363 mutex_unlock(&ftrace_lock);
4364 return -ENOMEM;
4365 }
4366 probe->probe_ops = probe_ops;
4367 probe->ops.func = function_trace_probe_call;
4368 probe->tr = tr;
4369 ftrace_ops_init(&probe->ops);
4370 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004371 }
4372
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004373 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004374
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004375 mutex_unlock(&ftrace_lock);
4376
4377 mutex_lock(&probe->ops.func_hash->regex_lock);
4378
4379 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004380 old_hash = *orig_hash;
4381 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4382
4383 ret = ftrace_match_records(hash, glob, strlen(glob));
4384
4385 /* Nothing found? */
4386 if (!ret)
4387 ret = -EINVAL;
4388
4389 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004390 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004391
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004392 size = 1 << hash->size_bits;
4393 for (i = 0; i < size; i++) {
4394 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4395 if (ftrace_lookup_ip(old_hash, entry->ip))
4396 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004397 /*
4398 * The caller might want to do something special
4399 * for each function we find. We call the callback
4400 * to give the caller an opportunity to do so.
4401 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004402 if (probe_ops->init) {
4403 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004404 entry->ip, data,
4405 &probe->data);
4406 if (ret < 0) {
4407 if (probe_ops->free && count)
4408 probe_ops->free(probe_ops, tr,
4409 0, probe->data);
4410 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004411 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004412 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004413 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004414 count++;
4415 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004416 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004417
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004418 mutex_lock(&ftrace_lock);
4419
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004420 if (!count) {
4421 /* Nothing was added? */
4422 ret = -EINVAL;
4423 goto out_unlock;
4424 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004425
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004426 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4427 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004428 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004429 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004430
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004431 /* One ref for each new function traced */
4432 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004433
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004434 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4435 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004436
Steven Rostedt59df055f2009-02-14 15:29:06 -05004437 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004438 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004439
4440 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004441 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004442 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004443 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004444 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004445
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004446 release_probe(probe);
4447
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004448 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004449
4450 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004451 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004452 goto out_unlock;
4453
4454 /* Failed to do the move, need to call the free functions */
4455 for (i = 0; i < size; i++) {
4456 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4457 if (ftrace_lookup_ip(old_hash, entry->ip))
4458 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004459 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004460 }
4461 }
4462 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004463}
4464
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004465int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004466unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4467 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004468{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004469 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004470 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004471 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004472 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004473 struct ftrace_hash **orig_hash;
4474 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004475 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004476 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004477 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004478 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004479 int count = 0;
4480 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004481 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004482
Naveen N. Raocbab5672017-05-16 23:21:25 +05304483 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004484 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304485 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004486 int not;
4487
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004488 func_g.type = filter_parse_regex(glob, strlen(glob),
4489 &func_g.search, &not);
4490 func_g.len = strlen(func_g.search);
4491 func_g.search = glob;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004492
Steven Rostedtb6887d72009-02-17 12:32:04 -05004493 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004494 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004495 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004496 }
4497
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004498 mutex_lock(&ftrace_lock);
4499 /* Check if the probe_ops is already registered */
4500 list_for_each_entry(probe, &tr->func_probes, list) {
4501 if (probe->probe_ops == probe_ops)
4502 break;
4503 }
4504 if (&probe->list == &tr->func_probes)
4505 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004506
4507 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004508 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4509 goto err_unlock_ftrace;
4510
4511 acquire_probe_locked(probe);
4512
4513 mutex_unlock(&ftrace_lock);
4514
4515 mutex_lock(&probe->ops.func_hash->regex_lock);
4516
4517 orig_hash = &probe->ops.func_hash->filter_hash;
4518 old_hash = *orig_hash;
4519
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004520 if (ftrace_hash_empty(old_hash))
4521 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004522
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004523 old_hash_ops.filter_hash = old_hash;
4524 /* Probes only have filters */
4525 old_hash_ops.notrace_hash = NULL;
4526
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004527 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004528 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004529 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004530 goto out_unlock;
4531
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004532 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004533
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004534 size = 1 << hash->size_bits;
4535 for (i = 0; i < size; i++) {
4536 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004537
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004538 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004539 kallsyms_lookup(entry->ip, NULL, NULL,
4540 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004541 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004542 continue;
4543 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004544 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004545 remove_hash_entry(hash, entry);
4546 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004547 }
4548 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004549
4550 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004551 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004552 ret = -EINVAL;
4553 goto out_unlock;
4554 }
4555
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004556 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004557
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004558 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004559
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004560 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004561
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004562 if (ftrace_hash_empty(hash))
4563 ftrace_shutdown(&probe->ops, 0);
4564
4565 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004566 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004567
4568 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004569 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004570 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004571 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004572 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004573
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004574 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4575 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004576 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004577 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004578 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004579 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004580 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004581
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004582 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004583 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004584 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004585
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004586 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004587
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004588 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004589
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004590 err_unlock_ftrace:
4591 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004592 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004593}
4594
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304595void clear_ftrace_function_probes(struct trace_array *tr)
4596{
4597 struct ftrace_func_probe *probe, *n;
4598
4599 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4600 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4601}
4602
Steven Rostedtf6180772009-02-14 00:40:25 -05004603static LIST_HEAD(ftrace_commands);
4604static DEFINE_MUTEX(ftrace_cmd_mutex);
4605
Tom Zanussi38de93a2013-10-24 08:34:18 -05004606/*
4607 * Currently we only register ftrace commands from __init, so mark this
4608 * __init too.
4609 */
4610__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004611{
4612 struct ftrace_func_command *p;
4613 int ret = 0;
4614
4615 mutex_lock(&ftrace_cmd_mutex);
4616 list_for_each_entry(p, &ftrace_commands, list) {
4617 if (strcmp(cmd->name, p->name) == 0) {
4618 ret = -EBUSY;
4619 goto out_unlock;
4620 }
4621 }
4622 list_add(&cmd->list, &ftrace_commands);
4623 out_unlock:
4624 mutex_unlock(&ftrace_cmd_mutex);
4625
4626 return ret;
4627}
4628
Tom Zanussi38de93a2013-10-24 08:34:18 -05004629/*
4630 * Currently we only unregister ftrace commands from __init, so mark
4631 * this __init too.
4632 */
4633__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004634{
4635 struct ftrace_func_command *p, *n;
4636 int ret = -ENODEV;
4637
4638 mutex_lock(&ftrace_cmd_mutex);
4639 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4640 if (strcmp(cmd->name, p->name) == 0) {
4641 ret = 0;
4642 list_del_init(&p->list);
4643 goto out_unlock;
4644 }
4645 }
4646 out_unlock:
4647 mutex_unlock(&ftrace_cmd_mutex);
4648
4649 return ret;
4650}
4651
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004652static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004653 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004654{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004655 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004656 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004657 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004658 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004659 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004660
4661 func = strsep(&next, ":");
4662
4663 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004664 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004665 if (!ret)
4666 ret = -EINVAL;
4667 if (ret < 0)
4668 return ret;
4669 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004670 }
4671
Steven Rostedtf6180772009-02-14 00:40:25 -05004672 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004673
4674 command = strsep(&next, ":");
4675
Steven Rostedtf6180772009-02-14 00:40:25 -05004676 mutex_lock(&ftrace_cmd_mutex);
4677 list_for_each_entry(p, &ftrace_commands, list) {
4678 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004679 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004680 goto out_unlock;
4681 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004682 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004683 out_unlock:
4684 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004685
Steven Rostedtf6180772009-02-14 00:40:25 -05004686 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004687}
4688
Ingo Molnare309b412008-05-12 21:20:51 +02004689static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004690ftrace_regex_write(struct file *file, const char __user *ubuf,
4691 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004692{
4693 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004694 struct trace_parser *parser;
4695 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004696
Li Zefan4ba79782009-09-22 13:52:20 +08004697 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004698 return 0;
4699
Steven Rostedt5072c592008-05-12 21:20:43 +02004700 if (file->f_mode & FMODE_READ) {
4701 struct seq_file *m = file->private_data;
4702 iter = m->private;
4703 } else
4704 iter = file->private_data;
4705
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004706 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004707 return -ENODEV;
4708
4709 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004710
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004711 parser = &iter->parser;
4712 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004713
Li Zefan4ba79782009-09-22 13:52:20 +08004714 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004715 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004716 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004717 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004718 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004719 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004720 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004721 }
4722
Steven Rostedt5072c592008-05-12 21:20:43 +02004723 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004724 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004725 return ret;
4726}
4727
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004728ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004729ftrace_filter_write(struct file *file, const char __user *ubuf,
4730 size_t cnt, loff_t *ppos)
4731{
4732 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4733}
4734
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004735ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004736ftrace_notrace_write(struct file *file, const char __user *ubuf,
4737 size_t cnt, loff_t *ppos)
4738{
4739 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4740}
4741
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004742static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004743ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4744{
4745 struct ftrace_func_entry *entry;
4746
4747 if (!ftrace_location(ip))
4748 return -EINVAL;
4749
4750 if (remove) {
4751 entry = ftrace_lookup_ip(hash, ip);
4752 if (!entry)
4753 return -ENOENT;
4754 free_hash_entry(hash, entry);
4755 return 0;
4756 }
4757
4758 return add_hash_entry(hash, ip);
4759}
4760
4761static int
4762ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4763 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004764{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004765 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004766 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004767 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004768
Steven Rostedt41c52c02008-05-22 11:46:33 -04004769 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004770 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004771
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004772 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004773
Steven Rostedtf45948e2011-05-02 12:29:25 -04004774 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004775 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004776 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004777 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004778
Wang Nanb972cc52014-07-15 08:40:20 +08004779 if (reset)
4780 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4781 else
4782 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4783
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004784 if (!hash) {
4785 ret = -ENOMEM;
4786 goto out_regex_unlock;
4787 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004788
Jiri Olsaac483c42012-01-02 10:04:14 +01004789 if (buf && !ftrace_match_records(hash, buf, len)) {
4790 ret = -EINVAL;
4791 goto out_regex_unlock;
4792 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004793 if (ip) {
4794 ret = ftrace_match_addr(hash, ip, remove);
4795 if (ret < 0)
4796 goto out_regex_unlock;
4797 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004798
4799 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004800 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004801 mutex_unlock(&ftrace_lock);
4802
Jiri Olsaac483c42012-01-02 10:04:14 +01004803 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004804 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004805
4806 free_ftrace_hash(hash);
4807 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004808}
4809
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004810static int
4811ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4812 int reset, int enable)
4813{
4814 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4815}
4816
4817/**
4818 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4819 * @ops - the ops to set the filter with
4820 * @ip - the address to add to or remove from the filter.
4821 * @remove - non zero to remove the ip from the filter
4822 * @reset - non zero to reset all filters before applying this filter.
4823 *
4824 * Filters denote which functions should be enabled when tracing is enabled
4825 * If @ip is NULL, it failes to update filter.
4826 */
4827int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4828 int remove, int reset)
4829{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004830 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004831 return ftrace_set_addr(ops, ip, remove, reset, 1);
4832}
4833EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4834
Joel Fernandesd032ae82016-11-15 12:31:20 -08004835/**
4836 * ftrace_ops_set_global_filter - setup ops to use global filters
4837 * @ops - the ops which will use the global filters
4838 *
4839 * ftrace users who need global function trace filtering should call this.
4840 * It can set the global filter only if ops were not initialized before.
4841 */
4842void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4843{
4844 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4845 return;
4846
4847 ftrace_ops_init(ops);
4848 ops->func_hash = &global_ops.local_hash;
4849}
4850EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4851
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004852static int
4853ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4854 int reset, int enable)
4855{
4856 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4857}
4858
Steven Rostedt77a2b372008-05-12 21:20:45 +02004859/**
4860 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004861 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004862 * @buf - the string that holds the function filter text.
4863 * @len - the length of the string.
4864 * @reset - non zero to reset all filters before applying this filter.
4865 *
4866 * Filters denote which functions should be enabled when tracing is enabled.
4867 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4868 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004869int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004870 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004871{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004872 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004873 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004874}
Steven Rostedt936e0742011-05-05 22:54:01 -04004875EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004876
Steven Rostedt41c52c02008-05-22 11:46:33 -04004877/**
4878 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004879 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004880 * @buf - the string that holds the function notrace text.
4881 * @len - the length of the string.
4882 * @reset - non zero to reset all filters before applying this filter.
4883 *
4884 * Notrace Filters denote which functions should not be enabled when tracing
4885 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4886 * for tracing.
4887 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004888int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004889 int len, int reset)
4890{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004891 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004892 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004893}
4894EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4895/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004896 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004897 * @buf - the string that holds the function filter text.
4898 * @len - the length of the string.
4899 * @reset - non zero to reset all filters before applying this filter.
4900 *
4901 * Filters denote which functions should be enabled when tracing is enabled.
4902 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4903 */
4904void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4905{
4906 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4907}
4908EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4909
4910/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004911 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004912 * @buf - the string that holds the function notrace text.
4913 * @len - the length of the string.
4914 * @reset - non zero to reset all filters before applying this filter.
4915 *
4916 * Notrace Filters denote which functions should not be enabled when tracing
4917 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4918 * for tracing.
4919 */
4920void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004921{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004922 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004923}
Steven Rostedt936e0742011-05-05 22:54:01 -04004924EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004925
Steven Rostedt2af15d62009-05-28 13:37:24 -04004926/*
4927 * command line interface to allow users to set filters on boot up.
4928 */
4929#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4930static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4931static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4932
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004933/* Used by function selftest to not test if filter is set */
4934bool ftrace_filter_param __initdata;
4935
Steven Rostedt2af15d62009-05-28 13:37:24 -04004936static int __init set_ftrace_notrace(char *str)
4937{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004938 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004939 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004940 return 1;
4941}
4942__setup("ftrace_notrace=", set_ftrace_notrace);
4943
4944static int __init set_ftrace_filter(char *str)
4945{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004946 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004947 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004948 return 1;
4949}
4950__setup("ftrace_filter=", set_ftrace_filter);
4951
Stefan Assmann369bc182009-10-12 22:17:21 +02004952#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004953static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004954static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004955static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004956
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004957static unsigned long save_global_trampoline;
4958static unsigned long save_global_flags;
4959
Stefan Assmann369bc182009-10-12 22:17:21 +02004960static int __init set_graph_function(char *str)
4961{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004962 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004963 return 1;
4964}
4965__setup("ftrace_graph_filter=", set_graph_function);
4966
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004967static int __init set_graph_notrace_function(char *str)
4968{
4969 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4970 return 1;
4971}
4972__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4973
Todd Brandt65a50c652017-03-02 16:12:15 -08004974static int __init set_graph_max_depth_function(char *str)
4975{
4976 if (!str)
4977 return 0;
4978 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4979 return 1;
4980}
4981__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4982
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004983static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004984{
4985 int ret;
4986 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004987 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004988
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004989 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4990 if (WARN_ON(!hash))
4991 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004992
4993 while (buf) {
4994 func = strsep(&buf, ",");
4995 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004996 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004997 if (ret)
4998 printk(KERN_DEBUG "ftrace: function %s not "
4999 "traceable\n", func);
5000 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005001
5002 if (enable)
5003 ftrace_graph_hash = hash;
5004 else
5005 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02005006}
5007#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5008
Steven Rostedt2a85a372011-12-19 21:57:44 -05005009void __init
5010ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04005011{
5012 char *func;
5013
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005014 ftrace_ops_init(ops);
5015
Steven Rostedt2af15d62009-05-28 13:37:24 -04005016 while (buf) {
5017 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04005018 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005019 }
5020}
5021
5022static void __init set_ftrace_early_filters(void)
5023{
5024 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005025 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005026 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005027 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005028#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5029 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005030 set_ftrace_early_graph(ftrace_graph_buf, 1);
5031 if (ftrace_graph_notrace_buf[0])
5032 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005033#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04005034}
5035
Steven Rostedtfc13cb02011-12-19 14:41:25 -05005036int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02005037{
5038 struct seq_file *m = (struct seq_file *)file->private_data;
5039 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005040 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005041 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04005042 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005043 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02005044
Steven Rostedt5072c592008-05-12 21:20:43 +02005045 if (file->f_mode & FMODE_READ) {
5046 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02005047 seq_release(inode, file);
5048 } else
5049 iter = file->private_data;
5050
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005051 parser = &iter->parser;
5052 if (trace_parser_loaded(parser)) {
5053 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005054 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02005055 }
5056
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005057 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005058
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005059 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005060
Steven Rostedt058e2972011-04-29 22:35:33 -04005061 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04005062 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5063
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005064 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005065 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04005066 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005067 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5068 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005069 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005070
Steven Rostedt058e2972011-04-29 22:35:33 -04005071 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04005072 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5073 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04005074 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04005075 } else {
5076 /* For read only, the hash is the ops hash */
5077 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04005078 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005079
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005080 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005081 free_ftrace_hash(iter->hash);
5082 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04005083
Steven Rostedt5072c592008-05-12 21:20:43 +02005084 return 0;
5085}
5086
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005087static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005088 .open = ftrace_avail_open,
5089 .read = seq_read,
5090 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08005091 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02005092};
5093
Steven Rostedt647bcd02011-05-03 14:39:21 -04005094static const struct file_operations ftrace_enabled_fops = {
5095 .open = ftrace_enabled_open,
5096 .read = seq_read,
5097 .llseek = seq_lseek,
5098 .release = seq_release_private,
5099};
5100
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005101static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005102 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005103 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005104 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005105 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005106 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005107};
5108
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005109static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005110 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005111 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005112 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005113 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005114 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005115};
5116
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005117#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5118
5119static DEFINE_MUTEX(graph_lock);
5120
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005121struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5122struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5123
5124enum graph_filter_type {
5125 GRAPH_FILTER_NOTRACE = 0,
5126 GRAPH_FILTER_FUNCTION,
5127};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005128
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005129#define FTRACE_GRAPH_EMPTY ((void *)1)
5130
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005131struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005132 struct ftrace_hash *hash;
5133 struct ftrace_func_entry *entry;
5134 int idx; /* for hash table iteration */
5135 enum graph_filter_type type;
5136 struct ftrace_hash *new_hash;
5137 const struct seq_operations *seq_ops;
5138 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005139};
5140
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005141static void *
Li Zefan85951842009-06-24 09:54:00 +08005142__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005143{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005144 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005145 struct ftrace_func_entry *entry = fgd->entry;
5146 struct hlist_head *head;
5147 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005148
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005149 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005150 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005151
5152 if (entry) {
5153 hlist_for_each_entry_continue(entry, hlist) {
5154 fgd->entry = entry;
5155 return entry;
5156 }
5157
5158 idx++;
5159 }
5160
5161 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5162 head = &fgd->hash->buckets[i];
5163 hlist_for_each_entry(entry, head, hlist) {
5164 fgd->entry = entry;
5165 fgd->idx = i;
5166 return entry;
5167 }
5168 }
5169 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005170}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005171
Li Zefan85951842009-06-24 09:54:00 +08005172static void *
5173g_next(struct seq_file *m, void *v, loff_t *pos)
5174{
5175 (*pos)++;
5176 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005177}
5178
5179static void *g_start(struct seq_file *m, loff_t *pos)
5180{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005181 struct ftrace_graph_data *fgd = m->private;
5182
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005183 mutex_lock(&graph_lock);
5184
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005185 if (fgd->type == GRAPH_FILTER_FUNCTION)
5186 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5187 lockdep_is_held(&graph_lock));
5188 else
5189 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5190 lockdep_is_held(&graph_lock));
5191
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005192 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005193 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005194 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005195
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005196 fgd->idx = 0;
5197 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005198 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005199}
5200
5201static void g_stop(struct seq_file *m, void *p)
5202{
5203 mutex_unlock(&graph_lock);
5204}
5205
5206static int g_show(struct seq_file *m, void *v)
5207{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005208 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005209
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005210 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005211 return 0;
5212
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005213 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005214 struct ftrace_graph_data *fgd = m->private;
5215
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005216 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005217 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005218 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005219 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005220 return 0;
5221 }
5222
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005223 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005224
5225 return 0;
5226}
5227
James Morris88e9d342009-09-22 16:43:43 -07005228static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005229 .start = g_start,
5230 .next = g_next,
5231 .stop = g_stop,
5232 .show = g_show,
5233};
5234
5235static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005236__ftrace_graph_open(struct inode *inode, struct file *file,
5237 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005238{
5239 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005240 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005241
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005242 if (file->f_mode & FMODE_WRITE) {
5243 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5244
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005245 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5246 return -ENOMEM;
5247
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005248 if (file->f_flags & O_TRUNC)
5249 new_hash = alloc_ftrace_hash(size_bits);
5250 else
5251 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5252 fgd->hash);
5253 if (!new_hash) {
5254 ret = -ENOMEM;
5255 goto out;
5256 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005257 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005258
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005259 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005260 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005261 if (!ret) {
5262 struct seq_file *m = file->private_data;
5263 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005264 } else {
5265 /* Failed */
5266 free_ftrace_hash(new_hash);
5267 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005268 }
5269 } else
5270 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005271
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005272out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005273 if (ret < 0 && file->f_mode & FMODE_WRITE)
5274 trace_parser_put(&fgd->parser);
5275
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005276 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005277
5278 /*
5279 * All uses of fgd->hash must be taken with the graph_lock
5280 * held. The graph_lock is going to be released, so force
5281 * fgd->hash to be reinitialized when it is taken again.
5282 */
5283 fgd->hash = NULL;
5284
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005285 return ret;
5286}
5287
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005288static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005289ftrace_graph_open(struct inode *inode, struct file *file)
5290{
5291 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005292 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005293
5294 if (unlikely(ftrace_disabled))
5295 return -ENODEV;
5296
5297 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5298 if (fgd == NULL)
5299 return -ENOMEM;
5300
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005301 mutex_lock(&graph_lock);
5302
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005303 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5304 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005305 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005306 fgd->seq_ops = &ftrace_graph_seq_ops;
5307
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005308 ret = __ftrace_graph_open(inode, file, fgd);
5309 if (ret < 0)
5310 kfree(fgd);
5311
5312 mutex_unlock(&graph_lock);
5313 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005314}
5315
5316static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005317ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5318{
5319 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005320 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005321
5322 if (unlikely(ftrace_disabled))
5323 return -ENODEV;
5324
5325 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5326 if (fgd == NULL)
5327 return -ENOMEM;
5328
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005329 mutex_lock(&graph_lock);
5330
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005331 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5332 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005333 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005334 fgd->seq_ops = &ftrace_graph_seq_ops;
5335
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005336 ret = __ftrace_graph_open(inode, file, fgd);
5337 if (ret < 0)
5338 kfree(fgd);
5339
5340 mutex_unlock(&graph_lock);
5341 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005342}
5343
5344static int
Li Zefan87827112009-07-23 11:29:11 +08005345ftrace_graph_release(struct inode *inode, struct file *file)
5346{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005347 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005348 struct ftrace_hash *old_hash, *new_hash;
5349 struct trace_parser *parser;
5350 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005351
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005352 if (file->f_mode & FMODE_READ) {
5353 struct seq_file *m = file->private_data;
5354
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005355 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005356 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005357 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005358 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005359 }
5360
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005361
5362 if (file->f_mode & FMODE_WRITE) {
5363
5364 parser = &fgd->parser;
5365
5366 if (trace_parser_loaded((parser))) {
5367 parser->buffer[parser->idx] = 0;
5368 ret = ftrace_graph_set_hash(fgd->new_hash,
5369 parser->buffer);
5370 }
5371
5372 trace_parser_put(parser);
5373
5374 new_hash = __ftrace_hash_move(fgd->new_hash);
5375 if (!new_hash) {
5376 ret = -ENOMEM;
5377 goto out;
5378 }
5379
5380 mutex_lock(&graph_lock);
5381
5382 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5383 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5384 lockdep_is_held(&graph_lock));
5385 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5386 } else {
5387 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5388 lockdep_is_held(&graph_lock));
5389 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5390 }
5391
5392 mutex_unlock(&graph_lock);
5393
5394 /* Wait till all users are no longer using the old hash */
5395 synchronize_sched();
5396
5397 free_ftrace_hash(old_hash);
5398 }
5399
5400 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005401 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005402 kfree(fgd);
5403
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005404 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005405}
5406
5407static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005408ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005409{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005410 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005411 struct dyn_ftrace *rec;
5412 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005413 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005414 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005415 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005416
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005417 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005418 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5419 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005420
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005421 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005422
Steven Rostedt52baf112009-02-14 01:15:39 -05005423 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005424
5425 if (unlikely(ftrace_disabled)) {
5426 mutex_unlock(&ftrace_lock);
5427 return -ENODEV;
5428 }
5429
Steven Rostedt265c8312009-02-13 12:43:56 -05005430 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005431
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005432 if (rec->flags & FTRACE_FL_DISABLED)
5433 continue;
5434
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005435 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005436 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005437
5438 if (!not) {
5439 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005440
5441 if (entry)
5442 continue;
5443 if (add_hash_entry(hash, rec->ip) < 0)
5444 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005445 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005446 if (entry) {
5447 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005448 fail = 0;
5449 }
5450 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005451 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005452 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005453out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005454 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005455
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005456 if (fail)
5457 return -EINVAL;
5458
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005459 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005460}
5461
5462static ssize_t
5463ftrace_graph_write(struct file *file, const char __user *ubuf,
5464 size_t cnt, loff_t *ppos)
5465{
Namhyung Kim6a101082013-10-14 17:24:25 +09005466 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005467 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005468 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005469
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005470 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005471 return 0;
5472
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005473 /* Read mode uses seq functions */
5474 if (file->f_mode & FMODE_READ) {
5475 struct seq_file *m = file->private_data;
5476 fgd = m->private;
5477 }
5478
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005479 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005480
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005481 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005482
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005483 if (read >= 0 && trace_parser_loaded(parser) &&
5484 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005485
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005486 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005487 parser->buffer);
5488 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005489 }
5490
Namhyung Kim6a101082013-10-14 17:24:25 +09005491 if (!ret)
5492 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005493
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005494 return ret;
5495}
5496
5497static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005498 .open = ftrace_graph_open,
5499 .read = seq_read,
5500 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005501 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005502 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005503};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005504
5505static const struct file_operations ftrace_graph_notrace_fops = {
5506 .open = ftrace_graph_notrace_open,
5507 .read = seq_read,
5508 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005509 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005510 .release = ftrace_graph_release,
5511};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005512#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5513
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005514void ftrace_create_filter_files(struct ftrace_ops *ops,
5515 struct dentry *parent)
5516{
5517
5518 trace_create_file("set_ftrace_filter", 0644, parent,
5519 ops, &ftrace_filter_fops);
5520
5521 trace_create_file("set_ftrace_notrace", 0644, parent,
5522 ops, &ftrace_notrace_fops);
5523}
5524
5525/*
5526 * The name "destroy_filter_files" is really a misnomer. Although
5527 * in the future, it may actualy delete the files, but this is
5528 * really intended to make sure the ops passed in are disabled
5529 * and that when this function returns, the caller is free to
5530 * free the ops.
5531 *
5532 * The "destroy" name is only to match the "create" name that this
5533 * should be paired with.
5534 */
5535void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5536{
5537 mutex_lock(&ftrace_lock);
5538 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5539 ftrace_shutdown(ops, 0);
5540 ops->flags |= FTRACE_OPS_FL_DELETED;
5541 mutex_unlock(&ftrace_lock);
5542}
5543
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005544static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005545{
Steven Rostedt5072c592008-05-12 21:20:43 +02005546
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005547 trace_create_file("available_filter_functions", 0444,
5548 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005549
Steven Rostedt647bcd02011-05-03 14:39:21 -04005550 trace_create_file("enabled_functions", 0444,
5551 d_tracer, NULL, &ftrace_enabled_fops);
5552
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005553 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005554
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005555#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005556 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005557 NULL,
5558 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005559 trace_create_file("set_graph_notrace", 0444, d_tracer,
5560 NULL,
5561 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005562#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5563
Steven Rostedt5072c592008-05-12 21:20:43 +02005564 return 0;
5565}
5566
Steven Rostedt9fd49322012-04-24 22:32:06 -04005567static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005568{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005569 const unsigned long *ipa = a;
5570 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005571
Steven Rostedt9fd49322012-04-24 22:32:06 -04005572 if (*ipa > *ipb)
5573 return 1;
5574 if (*ipa < *ipb)
5575 return -1;
5576 return 0;
5577}
5578
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005579static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005580 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005581 unsigned long *end)
5582{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005583 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005584 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005585 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005586 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005587 unsigned long *p;
5588 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005589 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005590 int ret = -ENOMEM;
5591
5592 count = end - start;
5593
5594 if (!count)
5595 return 0;
5596
Steven Rostedt9fd49322012-04-24 22:32:06 -04005597 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005598 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005599
Steven Rostedt706c81f2012-04-24 23:45:26 -04005600 start_pg = ftrace_allocate_pages(count);
5601 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005602 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005603
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005604 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005605
Steven Rostedt320823092011-12-16 14:42:37 -05005606 /*
5607 * Core and each module needs their own pages, as
5608 * modules will free them when they are removed.
5609 * Force a new page to be allocated for modules.
5610 */
Steven Rostedta7900872011-12-16 16:23:44 -05005611 if (!mod) {
5612 WARN_ON(ftrace_pages || ftrace_pages_start);
5613 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005614 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005615 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005616 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005617 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005618
Steven Rostedta7900872011-12-16 16:23:44 -05005619 if (WARN_ON(ftrace_pages->next)) {
5620 /* Hmm, we have free pages? */
5621 while (ftrace_pages->next)
5622 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005623 }
Steven Rostedta7900872011-12-16 16:23:44 -05005624
Steven Rostedt706c81f2012-04-24 23:45:26 -04005625 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005626 }
5627
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005628 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005629 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005630 while (p < end) {
5631 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005632 /*
5633 * Some architecture linkers will pad between
5634 * the different mcount_loc sections of different
5635 * object files to satisfy alignments.
5636 * Skip any NULL pointers.
5637 */
5638 if (!addr)
5639 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005640
5641 if (pg->index == pg->size) {
5642 /* We should have allocated enough */
5643 if (WARN_ON(!pg->next))
5644 break;
5645 pg = pg->next;
5646 }
5647
5648 rec = &pg->records[pg->index++];
5649 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005650 }
5651
Steven Rostedt706c81f2012-04-24 23:45:26 -04005652 /* We should have used all pages */
5653 WARN_ON(pg->next);
5654
5655 /* Assign the last page to ftrace_pages */
5656 ftrace_pages = pg;
5657
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005658 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005659 * We only need to disable interrupts on start up
5660 * because we are modifying code that an interrupt
5661 * may execute, and the modification is not atomic.
5662 * But for modules, nothing runs the code we modify
5663 * until we are finished with it, and there's no
5664 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005665 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005666 if (!mod)
5667 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005668 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005669 if (!mod)
5670 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005671 ret = 0;
5672 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005673 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005674
Steven Rostedta7900872011-12-16 16:23:44 -05005675 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005676}
5677
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005678struct ftrace_mod_func {
5679 struct list_head list;
5680 char *name;
5681 unsigned long ip;
5682 unsigned int size;
5683};
5684
5685struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005686 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005687 struct list_head list;
5688 struct module *mod;
5689 unsigned long start_addr;
5690 unsigned long end_addr;
5691 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005692 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005693};
5694
Steven Rostedt93eb6772009-04-15 13:24:06 -04005695#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005696
5697#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5698
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005699static LIST_HEAD(ftrace_mod_maps);
5700
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005701static int referenced_filters(struct dyn_ftrace *rec)
5702{
5703 struct ftrace_ops *ops;
5704 int cnt = 0;
5705
5706 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5707 if (ops_references_rec(ops, rec))
5708 cnt++;
5709 }
5710
5711 return cnt;
5712}
5713
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005714static void
5715clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5716{
5717 struct ftrace_func_entry *entry;
5718 struct dyn_ftrace *rec;
5719 int i;
5720
5721 if (ftrace_hash_empty(hash))
5722 return;
5723
5724 for (i = 0; i < pg->index; i++) {
5725 rec = &pg->records[i];
5726 entry = __ftrace_lookup_ip(hash, rec->ip);
5727 /*
5728 * Do not allow this rec to match again.
5729 * Yeah, it may waste some memory, but will be removed
5730 * if/when the hash is modified again.
5731 */
5732 if (entry)
5733 entry->ip = 0;
5734 }
5735}
5736
5737/* Clear any records from hashs */
5738static void clear_mod_from_hashes(struct ftrace_page *pg)
5739{
5740 struct trace_array *tr;
5741
5742 mutex_lock(&trace_types_lock);
5743 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5744 if (!tr->ops || !tr->ops->func_hash)
5745 continue;
5746 mutex_lock(&tr->ops->func_hash->regex_lock);
5747 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5748 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5749 mutex_unlock(&tr->ops->func_hash->regex_lock);
5750 }
5751 mutex_unlock(&trace_types_lock);
5752}
5753
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005754static void ftrace_free_mod_map(struct rcu_head *rcu)
5755{
5756 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5757 struct ftrace_mod_func *mod_func;
5758 struct ftrace_mod_func *n;
5759
5760 /* All the contents of mod_map are now not visible to readers */
5761 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5762 kfree(mod_func->name);
5763 list_del(&mod_func->list);
5764 kfree(mod_func);
5765 }
5766
5767 kfree(mod_map);
5768}
5769
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005770void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005771{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005772 struct ftrace_mod_map *mod_map;
5773 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005774 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005775 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005776 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005777 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005778 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005779
Steven Rostedt93eb6772009-04-15 13:24:06 -04005780 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005781
5782 if (ftrace_disabled)
5783 goto out_unlock;
5784
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005785 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5786 if (mod_map->mod == mod) {
5787 list_del_rcu(&mod_map->list);
5788 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5789 break;
5790 }
5791 }
5792
Steven Rostedt320823092011-12-16 14:42:37 -05005793 /*
5794 * Each module has its own ftrace_pages, remove
5795 * them from the list.
5796 */
5797 last_pg = &ftrace_pages_start;
5798 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5799 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005800 if (within_module_core(rec->ip, mod) ||
5801 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005802 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005803 * As core pages are first, the first
5804 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005805 */
Steven Rostedt320823092011-12-16 14:42:37 -05005806 if (WARN_ON(pg == ftrace_pages_start))
5807 goto out_unlock;
5808
5809 /* Check if we are deleting the last page */
5810 if (pg == ftrace_pages)
5811 ftrace_pages = next_to_ftrace_page(last_pg);
5812
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04005813 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05005814 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005815
5816 pg->next = tmp_page;
5817 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005818 } else
5819 last_pg = &pg->next;
5820 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005821 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005822 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005823
5824 for (pg = tmp_page; pg; pg = tmp_page) {
5825
5826 /* Needs to be called outside of ftrace_lock */
5827 clear_mod_from_hashes(pg);
5828
5829 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5830 free_pages((unsigned long)pg->records, order);
5831 tmp_page = pg->next;
5832 kfree(pg);
5833 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04005834}
5835
Jessica Yu7dcd1822016-02-16 17:32:33 -05005836void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005837{
5838 struct dyn_ftrace *rec;
5839 struct ftrace_page *pg;
5840
5841 mutex_lock(&ftrace_lock);
5842
5843 if (ftrace_disabled)
5844 goto out_unlock;
5845
5846 /*
5847 * If the tracing is enabled, go ahead and enable the record.
5848 *
5849 * The reason not to enable the record immediatelly is the
5850 * inherent check of ftrace_make_nop/ftrace_make_call for
5851 * correct previous instructions. Making first the NOP
5852 * conversion puts the module to the correct state, thus
5853 * passing the ftrace_make_call check.
5854 *
5855 * We also delay this to after the module code already set the
5856 * text to read-only, as we now need to set it back to read-write
5857 * so that we can modify the text.
5858 */
5859 if (ftrace_start_up)
5860 ftrace_arch_code_modify_prepare();
5861
5862 do_for_each_ftrace_rec(pg, rec) {
5863 int cnt;
5864 /*
5865 * do_for_each_ftrace_rec() is a double loop.
5866 * module text shares the pg. If a record is
5867 * not part of this module, then skip this pg,
5868 * which the "break" will do.
5869 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005870 if (!within_module_core(rec->ip, mod) &&
5871 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005872 break;
5873
5874 cnt = 0;
5875
5876 /*
5877 * When adding a module, we need to check if tracers are
5878 * currently enabled and if they are, and can trace this record,
5879 * we need to enable the module functions as well as update the
5880 * reference counts for those function records.
5881 */
5882 if (ftrace_start_up)
5883 cnt += referenced_filters(rec);
5884
5885 /* This clears FTRACE_FL_DISABLED */
5886 rec->flags = cnt;
5887
5888 if (ftrace_start_up && cnt) {
5889 int failed = __ftrace_replace_code(rec, 1);
5890 if (failed) {
5891 ftrace_bug(failed, rec);
5892 goto out_loop;
5893 }
5894 }
5895
5896 } while_for_each_ftrace_rec();
5897
5898 out_loop:
5899 if (ftrace_start_up)
5900 ftrace_arch_code_modify_post_process();
5901
5902 out_unlock:
5903 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04005904
5905 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005906}
5907
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005908void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005909{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005910 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005911 return;
5912
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005913 ftrace_process_locs(mod, mod->ftrace_callsites,
5914 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005915}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005916
5917static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5918 struct dyn_ftrace *rec)
5919{
5920 struct ftrace_mod_func *mod_func;
5921 unsigned long symsize;
5922 unsigned long offset;
5923 char str[KSYM_SYMBOL_LEN];
5924 char *modname;
5925 const char *ret;
5926
5927 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5928 if (!ret)
5929 return;
5930
5931 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5932 if (!mod_func)
5933 return;
5934
5935 mod_func->name = kstrdup(str, GFP_KERNEL);
5936 if (!mod_func->name) {
5937 kfree(mod_func);
5938 return;
5939 }
5940
5941 mod_func->ip = rec->ip - offset;
5942 mod_func->size = symsize;
5943
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005944 mod_map->num_funcs++;
5945
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005946 list_add_rcu(&mod_func->list, &mod_map->funcs);
5947}
5948
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005949static struct ftrace_mod_map *
5950allocate_ftrace_mod_map(struct module *mod,
5951 unsigned long start, unsigned long end)
5952{
5953 struct ftrace_mod_map *mod_map;
5954
5955 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5956 if (!mod_map)
5957 return NULL;
5958
5959 mod_map->mod = mod;
5960 mod_map->start_addr = start;
5961 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005962 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005963
5964 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5965
5966 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5967
5968 return mod_map;
5969}
5970
5971static const char *
5972ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5973 unsigned long addr, unsigned long *size,
5974 unsigned long *off, char *sym)
5975{
5976 struct ftrace_mod_func *found_func = NULL;
5977 struct ftrace_mod_func *mod_func;
5978
5979 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5980 if (addr >= mod_func->ip &&
5981 addr < mod_func->ip + mod_func->size) {
5982 found_func = mod_func;
5983 break;
5984 }
5985 }
5986
5987 if (found_func) {
5988 if (size)
5989 *size = found_func->size;
5990 if (off)
5991 *off = addr - found_func->ip;
5992 if (sym)
5993 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5994
5995 return found_func->name;
5996 }
5997
5998 return NULL;
5999}
6000
6001const char *
6002ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6003 unsigned long *off, char **modname, char *sym)
6004{
6005 struct ftrace_mod_map *mod_map;
6006 const char *ret = NULL;
6007
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006008 /* mod_map is freed via call_rcu_sched() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006009 preempt_disable();
6010 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6011 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6012 if (ret) {
6013 if (modname)
6014 *modname = mod_map->mod->name;
6015 break;
6016 }
6017 }
6018 preempt_enable();
6019
6020 return ret;
6021}
6022
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006023int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6024 char *type, char *name,
6025 char *module_name, int *exported)
6026{
6027 struct ftrace_mod_map *mod_map;
6028 struct ftrace_mod_func *mod_func;
6029
6030 preempt_disable();
6031 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6032
6033 if (symnum >= mod_map->num_funcs) {
6034 symnum -= mod_map->num_funcs;
6035 continue;
6036 }
6037
6038 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6039 if (symnum > 1) {
6040 symnum--;
6041 continue;
6042 }
6043
6044 *value = mod_func->ip;
6045 *type = 'T';
6046 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6047 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6048 *exported = 1;
6049 preempt_enable();
6050 return 0;
6051 }
6052 WARN_ON(1);
6053 break;
6054 }
6055 preempt_enable();
6056 return -ERANGE;
6057}
6058
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006059#else
6060static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6061 struct dyn_ftrace *rec) { }
6062static inline struct ftrace_mod_map *
6063allocate_ftrace_mod_map(struct module *mod,
6064 unsigned long start, unsigned long end)
6065{
6066 return NULL;
6067}
Steven Rostedt93eb6772009-04-15 13:24:06 -04006068#endif /* CONFIG_MODULES */
6069
Joel Fernandes8715b102017-10-09 12:29:31 -07006070struct ftrace_init_func {
6071 struct list_head list;
6072 unsigned long ip;
6073};
6074
6075/* Clear any init ips from hashes */
6076static void
6077clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6078{
6079 struct ftrace_func_entry *entry;
6080
6081 if (ftrace_hash_empty(hash))
6082 return;
6083
6084 entry = __ftrace_lookup_ip(hash, func->ip);
6085
6086 /*
6087 * Do not allow this rec to match again.
6088 * Yeah, it may waste some memory, but will be removed
6089 * if/when the hash is modified again.
6090 */
6091 if (entry)
6092 entry->ip = 0;
6093}
6094
6095static void
6096clear_func_from_hashes(struct ftrace_init_func *func)
6097{
6098 struct trace_array *tr;
6099
6100 mutex_lock(&trace_types_lock);
6101 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6102 if (!tr->ops || !tr->ops->func_hash)
6103 continue;
6104 mutex_lock(&tr->ops->func_hash->regex_lock);
6105 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6106 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6107 mutex_unlock(&tr->ops->func_hash->regex_lock);
6108 }
6109 mutex_unlock(&trace_types_lock);
6110}
6111
6112static void add_to_clear_hash_list(struct list_head *clear_list,
6113 struct dyn_ftrace *rec)
6114{
6115 struct ftrace_init_func *func;
6116
6117 func = kmalloc(sizeof(*func), GFP_KERNEL);
6118 if (!func) {
6119 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6120 return;
6121 }
6122
6123 func->ip = rec->ip;
6124 list_add(&func->list, clear_list);
6125}
6126
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006127void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006128{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006129 unsigned long start = (unsigned long)(start_ptr);
6130 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006131 struct ftrace_page **last_pg = &ftrace_pages_start;
6132 struct ftrace_page *pg;
6133 struct dyn_ftrace *rec;
6134 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006135 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07006136 struct ftrace_init_func *func, *func_next;
6137 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006138 int order;
6139
Joel Fernandes8715b102017-10-09 12:29:31 -07006140 INIT_LIST_HEAD(&clear_hash);
6141
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006142 key.ip = start;
6143 key.flags = end; /* overload flags, as it is unsigned long */
6144
6145 mutex_lock(&ftrace_lock);
6146
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006147 /*
6148 * If we are freeing module init memory, then check if
6149 * any tracer is active. If so, we need to save a mapping of
6150 * the module functions being freed with the address.
6151 */
6152 if (mod && ftrace_ops_list != &ftrace_list_end)
6153 mod_map = allocate_ftrace_mod_map(mod, start, end);
6154
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006155 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6156 if (end < pg->records[0].ip ||
6157 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6158 continue;
6159 again:
6160 rec = bsearch(&key, pg->records, pg->index,
6161 sizeof(struct dyn_ftrace),
6162 ftrace_cmp_recs);
6163 if (!rec)
6164 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006165
Joel Fernandes8715b102017-10-09 12:29:31 -07006166 /* rec will be cleared from hashes after ftrace_lock unlock */
6167 add_to_clear_hash_list(&clear_hash, rec);
6168
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006169 if (mod_map)
6170 save_ftrace_mod_rec(mod_map, rec);
6171
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006172 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04006173 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006174 if (!pg->index) {
6175 *last_pg = pg->next;
6176 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6177 free_pages((unsigned long)pg->records, order);
6178 kfree(pg);
6179 pg = container_of(last_pg, struct ftrace_page, next);
6180 if (!(*last_pg))
6181 ftrace_pages = pg;
6182 continue;
6183 }
6184 memmove(rec, rec + 1,
6185 (pg->index - (rec - pg->records)) * sizeof(*rec));
6186 /* More than one function may be in this block */
6187 goto again;
6188 }
6189 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07006190
6191 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6192 clear_func_from_hashes(func);
6193 kfree(func);
6194 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006195}
6196
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006197void __init ftrace_free_init_mem(void)
6198{
6199 void *start = (void *)(&__init_begin);
6200 void *end = (void *)(&__init_end);
6201
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006202 ftrace_free_mem(NULL, start, end);
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006203}
6204
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006205void __init ftrace_init(void)
6206{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006207 extern unsigned long __start_mcount_loc[];
6208 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006209 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006210 int ret;
6211
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006212 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006213 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006214 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01006215 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006216 goto failed;
6217
6218 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006219 if (!count) {
6220 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006221 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006222 }
6223
6224 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6225 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006226
6227 last_ftrace_enabled = ftrace_enabled = 1;
6228
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006229 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08006230 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006231 __stop_mcount_loc);
6232
Steven Rostedt2af15d62009-05-28 13:37:24 -04006233 set_ftrace_early_filters();
6234
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006235 return;
6236 failed:
6237 ftrace_disabled = 1;
6238}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006239
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006240/* Do nothing if arch does not support this */
6241void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6242{
6243}
6244
6245static void ftrace_update_trampoline(struct ftrace_ops *ops)
6246{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006247 arch_ftrace_update_trampoline(ops);
6248}
6249
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006250void ftrace_init_trace_array(struct trace_array *tr)
6251{
6252 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04006253 INIT_LIST_HEAD(&tr->mod_trace);
6254 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006255}
Steven Rostedt3d083392008-05-12 21:20:42 +02006256#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006257
Steven Rostedt2b499382011-05-03 22:49:52 -04006258static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04006259 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006260 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6261 FTRACE_OPS_FL_INITIALIZED |
6262 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04006263};
6264
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006265static int __init ftrace_nodyn_init(void)
6266{
6267 ftrace_enabled = 1;
6268 return 0;
6269}
Steven Rostedt6f415672012-10-05 12:13:07 -04006270core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006271
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006272static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006273static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006274static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05006275/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006276# define ftrace_startup(ops, command) \
6277 ({ \
6278 int ___ret = __register_ftrace_function(ops); \
6279 if (!___ret) \
6280 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
6281 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04006282 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05006283# define ftrace_shutdown(ops, command) \
6284 ({ \
6285 int ___ret = __unregister_ftrace_function(ops); \
6286 if (!___ret) \
6287 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
6288 ___ret; \
6289 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006290
Ingo Molnarc7aafc52008-05-12 21:20:45 +02006291# define ftrace_startup_sysctl() do { } while (0)
6292# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04006293
6294static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04006295ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006296{
6297 return 1;
6298}
6299
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006300static void ftrace_update_trampoline(struct ftrace_ops *ops)
6301{
6302}
6303
Steven Rostedt3d083392008-05-12 21:20:42 +02006304#endif /* CONFIG_DYNAMIC_FTRACE */
6305
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006306__init void ftrace_init_global_array_ops(struct trace_array *tr)
6307{
6308 tr->ops = &global_ops;
6309 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006310 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006311}
6312
6313void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6314{
6315 /* If we filter on pids, update to use the pid function */
6316 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6317 if (WARN_ON(tr->ops->func != ftrace_stub))
6318 printk("ftrace ops had %pS for function\n",
6319 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006320 }
6321 tr->ops->func = func;
6322 tr->ops->private = tr;
6323}
6324
6325void ftrace_reset_array_ops(struct trace_array *tr)
6326{
6327 tr->ops->func = ftrace_stub;
6328}
6329
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006330static inline void
6331__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006332 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006333{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006334 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006335 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04006336
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006337 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6338 if (bit < 0)
6339 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04006340
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006341 /*
6342 * Some of the ops may be dynamically allocated,
6343 * they must be freed after a synchronize_sched().
6344 */
6345 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006346
Steven Rostedt0a016402012-11-02 17:03:03 -04006347 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006348 /*
6349 * Check the following for each ops before calling their func:
6350 * if RCU flag is set, then rcu_is_watching() must be true
6351 * if PER_CPU is set, then ftrace_function_local_disable()
6352 * must be false
6353 * Otherwise test if the ip matches the ops filter
6354 *
6355 * If any of the above fails then the op->func() is not executed.
6356 */
6357 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6358 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
6359 !ftrace_function_local_disabled(op)) &&
6360 ftrace_ops_test(op, ip, regs)) {
6361
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04006362 if (FTRACE_WARN_ON(!op->func)) {
6363 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006364 goto out;
6365 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04006366 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006367 }
Steven Rostedt0a016402012-11-02 17:03:03 -04006368 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006369out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006370 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006371 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04006372}
6373
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006374/*
6375 * Some archs only support passing ip and parent_ip. Even though
6376 * the list function ignores the op parameter, we do not want any
6377 * C side effects, where a function is called without the caller
6378 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006379 * Archs are to support both the regs and ftrace_ops at the same time.
6380 * If they support ftrace_ops, it is assumed they support regs.
6381 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09006382 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6383 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006384 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08006385 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006386 */
6387#if ARCH_SUPPORTS_FTRACE_OPS
6388static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006389 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006390{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006391 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006392}
6393#else
6394static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6395{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006396 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006397}
6398#endif
6399
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006400/*
6401 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006402 * recursion, needs RCU protection and/or requires per cpu handling, then
6403 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006404 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006405static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006406 struct ftrace_ops *op, struct pt_regs *regs)
6407{
6408 int bit;
6409
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006410 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6411 return;
6412
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006413 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6414 if (bit < 0)
6415 return;
6416
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006417 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006418
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006419 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
6420 !ftrace_function_local_disabled(op)) {
6421 op->func(ip, parent_ip, op, regs);
6422 }
6423
6424 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006425 trace_clear_recursion(bit);
6426}
6427
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006428/**
6429 * ftrace_ops_get_func - get the function a trampoline should call
6430 * @ops: the ops to get the function for
6431 *
6432 * Normally the mcount trampoline will call the ops->func, but there
6433 * are times that it should not. For example, if the ops does not
6434 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08006435 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006436 *
6437 * Returns the function that the trampoline should call for @ops.
6438 */
6439ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6440{
6441 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006442 * If the function does not handle recursion, needs to be RCU safe,
6443 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006444 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006445 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6446 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
6447 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006448
6449 return ops->func;
6450}
6451
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006452static void
6453ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6454 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05006455{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006456 struct trace_array *tr = data;
6457 struct trace_pid_list *pid_list;
6458
6459 pid_list = rcu_dereference_sched(tr->function_pids);
6460
6461 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6462 trace_ignore_this_task(pid_list, next));
6463}
6464
Namhyung Kim1e104862017-04-17 11:44:28 +09006465static void
6466ftrace_pid_follow_sched_process_fork(void *data,
6467 struct task_struct *self,
6468 struct task_struct *task)
6469{
6470 struct trace_pid_list *pid_list;
6471 struct trace_array *tr = data;
6472
6473 pid_list = rcu_dereference_sched(tr->function_pids);
6474 trace_filter_add_remove_task(pid_list, self, task);
6475}
6476
6477static void
6478ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6479{
6480 struct trace_pid_list *pid_list;
6481 struct trace_array *tr = data;
6482
6483 pid_list = rcu_dereference_sched(tr->function_pids);
6484 trace_filter_add_remove_task(pid_list, NULL, task);
6485}
6486
6487void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6488{
6489 if (enable) {
6490 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6491 tr);
6492 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6493 tr);
6494 } else {
6495 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6496 tr);
6497 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6498 tr);
6499 }
6500}
6501
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006502static void clear_ftrace_pids(struct trace_array *tr)
6503{
6504 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05006505 int cpu;
6506
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006507 pid_list = rcu_dereference_protected(tr->function_pids,
6508 lockdep_is_held(&ftrace_lock));
6509 if (!pid_list)
6510 return;
6511
6512 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6513
6514 for_each_possible_cpu(cpu)
6515 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6516
6517 rcu_assign_pointer(tr->function_pids, NULL);
6518
6519 /* Wait till all users are no longer using pid filtering */
6520 synchronize_sched();
6521
6522 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05006523}
6524
Namhyung Kimd879d0b2017-04-17 11:44:27 +09006525void ftrace_clear_pids(struct trace_array *tr)
6526{
6527 mutex_lock(&ftrace_lock);
6528
6529 clear_ftrace_pids(tr);
6530
6531 mutex_unlock(&ftrace_lock);
6532}
6533
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006534static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05006535{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006536 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006537 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006538
6539 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006540 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006541
6542 mutex_unlock(&ftrace_lock);
6543}
6544
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006545/* Greater than any max PID */
6546#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6547
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006548static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006549 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006550{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006551 struct trace_pid_list *pid_list;
6552 struct trace_array *tr = m->private;
6553
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006554 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006555 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006556
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006557 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006558
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006559 if (!pid_list)
6560 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6561
6562 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006563}
6564
6565static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6566{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006567 struct trace_array *tr = m->private;
6568 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6569
6570 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006571 return NULL;
6572
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006573 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006574}
6575
6576static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006577 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006578{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006579 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006580 mutex_unlock(&ftrace_lock);
6581}
6582
6583static int fpid_show(struct seq_file *m, void *v)
6584{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006585 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006586 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006587 return 0;
6588 }
6589
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006590 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006591}
6592
6593static const struct seq_operations ftrace_pid_sops = {
6594 .start = fpid_start,
6595 .next = fpid_next,
6596 .stop = fpid_stop,
6597 .show = fpid_show,
6598};
6599
6600static int
6601ftrace_pid_open(struct inode *inode, struct file *file)
6602{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006603 struct trace_array *tr = inode->i_private;
6604 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006605 int ret = 0;
6606
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006607 if (trace_array_get(tr) < 0)
6608 return -ENODEV;
6609
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006610 if ((file->f_mode & FMODE_WRITE) &&
6611 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006612 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006613
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006614 ret = seq_open(file, &ftrace_pid_sops);
6615 if (ret < 0) {
6616 trace_array_put(tr);
6617 } else {
6618 m = file->private_data;
6619 /* copy tr over to seq ops */
6620 m->private = tr;
6621 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006622
6623 return ret;
6624}
6625
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006626static void ignore_task_cpu(void *data)
6627{
6628 struct trace_array *tr = data;
6629 struct trace_pid_list *pid_list;
6630
6631 /*
6632 * This function is called by on_each_cpu() while the
6633 * event_mutex is held.
6634 */
6635 pid_list = rcu_dereference_protected(tr->function_pids,
6636 mutex_is_locked(&ftrace_lock));
6637
6638 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6639 trace_ignore_this_task(pid_list, current));
6640}
6641
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006642static ssize_t
6643ftrace_pid_write(struct file *filp, const char __user *ubuf,
6644 size_t cnt, loff_t *ppos)
6645{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006646 struct seq_file *m = filp->private_data;
6647 struct trace_array *tr = m->private;
6648 struct trace_pid_list *filtered_pids = NULL;
6649 struct trace_pid_list *pid_list;
6650 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006651
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006652 if (!cnt)
6653 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006654
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006655 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006656
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006657 filtered_pids = rcu_dereference_protected(tr->function_pids,
6658 lockdep_is_held(&ftrace_lock));
6659
6660 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6661 if (ret < 0)
6662 goto out;
6663
6664 rcu_assign_pointer(tr->function_pids, pid_list);
6665
6666 if (filtered_pids) {
6667 synchronize_sched();
6668 trace_free_pid_list(filtered_pids);
6669 } else if (pid_list) {
6670 /* Register a probe to set whether to ignore the tracing of a task */
6671 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6672 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006673
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006674 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006675 * Ignoring of pids is done at task switch. But we have to
6676 * check for those tasks that are currently running.
6677 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006678 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006679 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006680
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006681 ftrace_update_pid_func();
6682 ftrace_startup_all(0);
6683 out:
6684 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006685
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006686 if (ret > 0)
6687 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05006688
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006689 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006690}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006691
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006692static int
6693ftrace_pid_release(struct inode *inode, struct file *file)
6694{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006695 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006696
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006697 trace_array_put(tr);
6698
6699 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006700}
6701
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006702static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006703 .open = ftrace_pid_open,
6704 .write = ftrace_pid_write,
6705 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006706 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006707 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006708};
6709
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006710void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006711{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006712 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006713 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006714}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006715
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04006716void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6717 struct dentry *d_tracer)
6718{
6719 /* Only the top level directory has the dyn_tracefs and profile */
6720 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6721
6722 ftrace_init_dyn_tracefs(d_tracer);
6723 ftrace_profile_tracefs(d_tracer);
6724}
6725
Steven Rostedt3d083392008-05-12 21:20:42 +02006726/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006727 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006728 *
6729 * This function should be used by panic code. It stops ftrace
6730 * but in a not so nice way. If you need to simply kill ftrace
6731 * from a non-atomic section, use ftrace_kill.
6732 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006733void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006734{
6735 ftrace_disabled = 1;
6736 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006737 clear_ftrace_function();
6738}
6739
6740/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04006741 * Test if ftrace is dead or not.
6742 */
6743int ftrace_is_dead(void)
6744{
6745 return ftrace_disabled;
6746}
6747
6748/**
Steven Rostedt3d083392008-05-12 21:20:42 +02006749 * register_ftrace_function - register a function for profiling
6750 * @ops - ops structure that holds the function for profiling.
6751 *
6752 * Register a function to be called by all functions in the
6753 * kernel.
6754 *
6755 * Note: @ops->func and all the functions it calls must be labeled
6756 * with "notrace", otherwise it will go into a
6757 * recursive loop.
6758 */
6759int register_ftrace_function(struct ftrace_ops *ops)
6760{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006761 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006762
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09006763 ftrace_ops_init(ops);
6764
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006765 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006766
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006767 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04006768
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006769 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02006770
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006771 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02006772}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006773EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02006774
6775/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01006776 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02006777 * @ops - ops structure that holds the function to unregister
6778 *
6779 * Unregister a function that was added to be called by ftrace profiling.
6780 */
6781int unregister_ftrace_function(struct ftrace_ops *ops)
6782{
6783 int ret;
6784
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006785 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006786 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006787 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006788
6789 return ret;
6790}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006791EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006792
Ingo Molnare309b412008-05-12 21:20:51 +02006793int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006794ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006795 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006796 loff_t *ppos)
6797{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006798 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006799
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006800 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006801
Steven Rostedt45a4a232011-04-21 23:16:46 -04006802 if (unlikely(ftrace_disabled))
6803 goto out;
6804
6805 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006806
Li Zefana32c7762009-06-26 16:55:51 +08006807 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006808 goto out;
6809
Li Zefana32c7762009-06-26 16:55:51 +08006810 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006811
6812 if (ftrace_enabled) {
6813
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006814 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08006815 if (rcu_dereference_protected(ftrace_ops_list,
6816 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01006817 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006818
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006819 ftrace_startup_sysctl();
6820
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006821 } else {
6822 /* stopping ftrace calls (just send to ftrace_stub) */
6823 ftrace_trace_function = ftrace_stub;
6824
6825 ftrace_shutdown_sysctl();
6826 }
6827
6828 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006829 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006830 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006831}
Ingo Molnarf17845e2008-10-24 12:47:10 +02006832
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006833#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006834
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006835static struct ftrace_ops graph_ops = {
6836 .func = ftrace_stub,
6837 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6838 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006839 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006840 FTRACE_OPS_FL_STUB,
6841#ifdef FTRACE_GRAPH_TRAMP_ADDR
6842 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05006843 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006844#endif
6845 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6846};
6847
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006848void ftrace_graph_sleep_time_control(bool enable)
6849{
6850 fgraph_sleep_time = enable;
6851}
6852
6853void ftrace_graph_graph_time_control(bool enable)
6854{
6855 fgraph_graph_time = enable;
6856}
6857
Steven Rostedte49dc192008-12-02 23:50:05 -05006858int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6859{
6860 return 0;
6861}
6862
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006863/* The callbacks that hook a function */
6864trace_func_graph_ret_t ftrace_graph_return =
6865 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006866trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006867static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006868
6869/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6870static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6871{
6872 int i;
6873 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006874 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6875 struct task_struct *g, *t;
6876
6877 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6878 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
6879 * sizeof(struct ftrace_ret_stack),
6880 GFP_KERNEL);
6881 if (!ret_stack_list[i]) {
6882 start = 0;
6883 end = i;
6884 ret = -ENOMEM;
6885 goto free;
6886 }
6887 }
6888
Soumya PN6112a302016-05-17 21:31:14 +05306889 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006890 do_each_thread(g, t) {
6891 if (start == end) {
6892 ret = -EAGAIN;
6893 goto unlock;
6894 }
6895
6896 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01006897 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006898 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04006899 t->curr_ret_stack = -1;
6900 /* Make sure the tasks see the -1 first: */
6901 smp_wmb();
6902 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006903 }
6904 } while_each_thread(g, t);
6905
6906unlock:
Soumya PN6112a302016-05-17 21:31:14 +05306907 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006908free:
6909 for (i = start; i < end; i++)
6910 kfree(ret_stack_list[i]);
6911 return ret;
6912}
6913
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006914static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02006915ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04006916 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006917{
6918 unsigned long long timestamp;
6919 int index;
6920
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006921 /*
6922 * Does the user want to count the time a function was asleep.
6923 * If so, do not update the time stamps.
6924 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006925 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006926 return;
6927
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006928 timestamp = trace_clock_local();
6929
6930 prev->ftrace_timestamp = timestamp;
6931
6932 /* only process tasks that we timestamped */
6933 if (!next->ftrace_timestamp)
6934 return;
6935
6936 /*
6937 * Update all the counters in next to make up for the
6938 * time next was sleeping.
6939 */
6940 timestamp -= next->ftrace_timestamp;
6941
6942 for (index = next->curr_ret_stack; index >= 0; index--)
6943 next->ret_stack[index].calltime += timestamp;
6944}
6945
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006946/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006947static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006948{
6949 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006950 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006951
6952 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
6953 sizeof(struct ftrace_ret_stack *),
6954 GFP_KERNEL);
6955
6956 if (!ret_stack_list)
6957 return -ENOMEM;
6958
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006959 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04006960 for_each_online_cpu(cpu) {
6961 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05006962 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04006963 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006964
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006965 do {
6966 ret = alloc_retstack_tasklist(ret_stack_list);
6967 } while (ret == -EAGAIN);
6968
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006969 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04006970 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006971 if (ret)
6972 pr_info("ftrace_graph: Couldn't activate tracepoint"
6973 " probe to kernel_sched_switch\n");
6974 }
6975
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006976 kfree(ret_stack_list);
6977 return ret;
6978}
6979
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006980/*
6981 * Hibernation protection.
6982 * The state of the current task is too much unstable during
6983 * suspend/restore to disk. We want to protect against that.
6984 */
6985static int
6986ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6987 void *unused)
6988{
6989 switch (state) {
6990 case PM_HIBERNATION_PREPARE:
6991 pause_graph_tracing();
6992 break;
6993
6994 case PM_POST_HIBERNATION:
6995 unpause_graph_tracing();
6996 break;
6997 }
6998 return NOTIFY_DONE;
6999}
7000
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05007001static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
7002{
7003 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
7004 return 0;
7005 return __ftrace_graph_entry(trace);
7006}
7007
7008/*
7009 * The function graph tracer should only trace the functions defined
7010 * by set_ftrace_filter and set_ftrace_notrace. If another function
7011 * tracer ops is registered, the graph tracer requires testing the
7012 * function against the global ops, and not just trace any function
7013 * that any ftrace_ops registered.
7014 */
7015static void update_function_graph_func(void)
7016{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04007017 struct ftrace_ops *op;
7018 bool do_test = false;
7019
7020 /*
7021 * The graph and global ops share the same set of functions
7022 * to test. If any other ops is on the list, then
7023 * the graph tracing needs to test if its the function
7024 * it should call.
7025 */
7026 do_for_each_ftrace_op(op, ftrace_ops_list) {
7027 if (op != &global_ops && op != &graph_ops &&
7028 op != &ftrace_list_end) {
7029 do_test = true;
7030 /* in double loop, break out with goto */
7031 goto out;
7032 }
7033 } while_for_each_ftrace_op(op);
7034 out:
7035 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05007036 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04007037 else
7038 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05007039}
7040
Mathias Krause8275f692014-03-30 15:31:50 +02007041static struct notifier_block ftrace_suspend_notifier = {
7042 .notifier_call = ftrace_suspend_notifier_call,
7043};
7044
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01007045int register_ftrace_graph(trace_func_graph_ret_t retfunc,
7046 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01007047{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01007048 int ret = 0;
7049
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007050 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01007051
Steven Rostedt05ce5812009-03-24 00:18:31 -04007052 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04007053 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04007054 ret = -EBUSY;
7055 goto out;
7056 }
7057
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08007058 register_pm_notifier(&ftrace_suspend_notifier);
7059
Steven Rostedt597af812009-04-03 15:24:12 -04007060 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01007061 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007062 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04007063 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007064 goto out;
7065 }
Steven Rostedte53a6312008-11-26 00:16:25 -05007066
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01007067 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05007068
7069 /*
7070 * Update the indirect function to the entryfunc, and the
7071 * function that gets called to the entry_test first. Then
7072 * call the update fgraph entry function to determine if
7073 * the entryfunc should be called directly or not.
7074 */
7075 __ftrace_graph_entry = entryfunc;
7076 ftrace_graph_entry = ftrace_graph_entry_test;
7077 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05007078
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04007079 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01007080out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007081 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01007082 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01007083}
7084
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01007085void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01007086{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007087 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01007088
Steven Rostedt597af812009-04-03 15:24:12 -04007089 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04007090 goto out;
7091
Steven Rostedt597af812009-04-03 15:24:12 -04007092 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01007093 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05007094 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05007095 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04007096 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08007097 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04007098 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01007099
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04007100#ifdef CONFIG_DYNAMIC_FTRACE
7101 /*
7102 * Function graph does not allocate the trampoline, but
7103 * other global_ops do. We need to reset the ALLOC_TRAMP flag
7104 * if one was used.
7105 */
7106 global_ops.trampoline = save_global_trampoline;
7107 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
7108 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
7109#endif
7110
Steven Rostedt2aad1b72009-03-30 11:11:28 -04007111 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007112 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01007113}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007114
Steven Rostedt868baf02011-02-10 21:26:13 -05007115static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
7116
7117static void
7118graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
7119{
7120 atomic_set(&t->tracing_graph_pause, 0);
7121 atomic_set(&t->trace_overrun, 0);
7122 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03007123 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05007124 smp_wmb();
7125 t->ret_stack = ret_stack;
7126}
7127
7128/*
7129 * Allocate a return stack for the idle task. May be the first
7130 * time through, or it may be done by CPU hotplug online.
7131 */
7132void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
7133{
7134 t->curr_ret_stack = -1;
7135 /*
7136 * The idle task has no parent, it either has its own
7137 * stack or no stack at all.
7138 */
7139 if (t->ret_stack)
7140 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7141
7142 if (ftrace_graph_active) {
7143 struct ftrace_ret_stack *ret_stack;
7144
7145 ret_stack = per_cpu(idle_ret_stack, cpu);
7146 if (!ret_stack) {
7147 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
7148 * sizeof(struct ftrace_ret_stack),
7149 GFP_KERNEL);
7150 if (!ret_stack)
7151 return;
7152 per_cpu(idle_ret_stack, cpu) = ret_stack;
7153 }
7154 graph_init_task(t, ret_stack);
7155 }
7156}
7157
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007158/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01007159void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007160{
Steven Rostedt84047e32009-06-02 16:51:55 -04007161 /* Make sure we do not use the parent ret_stack */
7162 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05007163 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04007164
Steven Rostedt597af812009-04-03 15:24:12 -04007165 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04007166 struct ftrace_ret_stack *ret_stack;
7167
7168 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007169 * sizeof(struct ftrace_ret_stack),
7170 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04007171 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007172 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05007173 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04007174 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007175}
7176
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01007177void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007178{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01007179 struct ftrace_ret_stack *ret_stack = t->ret_stack;
7180
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007181 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01007182 /* NULL must become visible to IRQs before we free it: */
7183 barrier();
7184
7185 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007186}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01007187#endif