blob: 52c89428b0db73cad33b7af6c390d2bbf3d2f1ea [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002/*
3 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 *
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 *
8 * Originally ported from the -rt patch by:
9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 *
11 * Based on code in the latency_tracer, that is:
12 *
13 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010014 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020015 */
16
Steven Rostedt3d083392008-05-12 21:20:42 +020017#include <linux/stop_machine.h>
18#include <linux/clocksource.h>
Ingo Molnar29930022017-02-08 18:51:36 +010019#include <linux/sched/task.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020020#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080022#include <linux/suspend.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050023#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020024#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010025#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020026#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050027#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040028#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010029#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020030#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020032#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050033#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020034#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050035#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080036#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020037
Steven Rostedtad8d75f2009-04-14 19:39:12 -040038#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040039
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -040040#include <asm/sections.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040041#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053042
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050043#include "ftrace_internal.h"
Steven Rostedt0706f1c2009-03-23 23:12:58 -040044#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040045#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020046
Steven Rostedt6912896e2008-10-23 09:33:03 -040047#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ({ \
49 int ___r = cond; \
50 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040051 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ___r; \
53 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040054
55#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ({ \
57 int ___r = cond; \
58 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040059 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040060 ___r; \
61 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040062
Steven Rostedt8fc0c702009-02-16 15:28:00 -050063/* hash bits for specific function selection */
64#define FTRACE_HASH_BITS 7
65#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040066#define FTRACE_HASH_DEFAULT_BITS 10
67#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050068
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090069#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040070#define INIT_OPS_HASH(opsname) \
71 .func_hash = &opsname.local_hash, \
72 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040073#define ASSIGN_OPS_HASH(opsname, val) \
74 .func_hash = val, \
75 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090076#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040077#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040078#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090079#endif
80
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050081struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040082 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040083 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040084 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040085};
86
Steven Rostedt4eebcc82008-05-12 21:20:48 +020087/* ftrace_enabled is a method to turn ftrace on or off */
88int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020089static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020090
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040091/* Current function tracing op */
92struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050093/* What to set function_trace_op to */
94static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050095
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040096static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040097{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040098 struct trace_array *tr;
99
100 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
101 return false;
102
103 tr = ops->private;
104
105 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400106}
107
108static void ftrace_update_trampoline(struct ftrace_ops *ops);
109
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200110/*
111 * ftrace_disabled is set when an anomaly is discovered.
112 * ftrace_disabled is much stronger than ftrace_enabled.
113 */
114static int ftrace_disabled __read_mostly;
115
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500116DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200117
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500118struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200119ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500120struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200121
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122#if ARCH_SUPPORTS_FTRACE_OPS
123static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400124 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400125#else
126/* See comment below, where ftrace_ops_list_func is defined */
127static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
128#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
129#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400130
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900131static inline void ftrace_ops_init(struct ftrace_ops *ops)
132{
133#ifdef CONFIG_DYNAMIC_FTRACE
134 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400135 mutex_init(&ops->local_hash.regex_lock);
136 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900137 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
138 }
139#endif
140}
141
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400142static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400143 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500144{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400145 struct trace_array *tr = op->private;
146
147 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500148 return;
149
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400150 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500151}
152
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500153static void ftrace_sync(struct work_struct *work)
154{
155 /*
156 * This function is just a stub to implement a hard force
157 * of synchronize_sched(). This requires synchronizing
158 * tasks even in userspace and idle.
159 *
160 * Yes, function tracing is rude.
161 */
162}
163
164static void ftrace_sync_ipi(void *data)
165{
166 /* Probably not needed, but do it anyway */
167 smp_rmb();
168}
169
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500170#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400171/* Both enabled by default (can be cleared by function_graph tracer flags */
172static bool fgraph_sleep_time = true;
173static bool fgraph_graph_time = true;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500174#endif
175
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100176static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
177{
178 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500179 * 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 +0100180 * then it needs to call the list anyway.
181 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +0200182 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
183 FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100184 return ftrace_ops_list_func;
185
186 return ftrace_ops_get_func(ops);
187}
188
Steven Rostedt2b499382011-05-03 22:49:52 -0400189static void update_ftrace_function(void)
190{
191 ftrace_func_t func;
192
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400193 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400194 * Prepare the ftrace_ops that the arch callback will use.
195 * If there's only one ftrace_ops registered, the ftrace_ops_list
196 * will point to the ops we want.
197 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800198 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
199 lockdep_is_held(&ftrace_lock));
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400200
201 /* If there's no ftrace_ops registered, just call the stub function */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800202 if (set_function_trace_op == &ftrace_list_end) {
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400203 func = ftrace_stub;
204
205 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400206 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400207 * recursion safe and not dynamic and the arch supports passing ops,
208 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400209 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800210 } else if (rcu_dereference_protected(ftrace_ops_list->next,
211 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100212 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400213
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400214 } else {
215 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500216 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400217 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400218 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400219
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400220 update_function_graph_func();
221
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500222 /* If there's no change, then do nothing more here */
223 if (ftrace_trace_function == func)
224 return;
225
226 /*
227 * If we are using the list function, it doesn't care
228 * about the function_trace_ops.
229 */
230 if (func == ftrace_ops_list_func) {
231 ftrace_trace_function = func;
232 /*
233 * Don't even bother setting function_trace_ops,
234 * it would be racy to do so anyway.
235 */
236 return;
237 }
238
239#ifndef CONFIG_DYNAMIC_FTRACE
240 /*
241 * For static tracing, we need to be a bit more careful.
242 * The function change takes affect immediately. Thus,
243 * we need to coorditate the setting of the function_trace_ops
244 * with the setting of the ftrace_trace_function.
245 *
246 * Set the function to the list ops, which will call the
247 * function we want, albeit indirectly, but it handles the
248 * ftrace_ops and doesn't depend on function_trace_op.
249 */
250 ftrace_trace_function = ftrace_ops_list_func;
251 /*
252 * Make sure all CPUs see this. Yes this is slow, but static
253 * tracing is slow and nasty to have enabled.
254 */
255 schedule_on_each_cpu(ftrace_sync);
256 /* Now all cpus are using the list ops. */
257 function_trace_op = set_function_trace_op;
258 /* Make sure the function_trace_op is visible on all CPUs */
259 smp_wmb();
260 /* Nasty way to force a rmb on all cpus */
261 smp_call_function(ftrace_sync_ipi, NULL, 1);
262 /* OK, we are all set to update the ftrace_trace_function now! */
263#endif /* !CONFIG_DYNAMIC_FTRACE */
264
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400265 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400266}
267
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800268static void add_ftrace_ops(struct ftrace_ops __rcu **list,
269 struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200270{
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800271 rcu_assign_pointer(ops->next, *list);
272
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200273 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400274 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200275 * CPU might be walking that list. We need to make sure
276 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400277 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200278 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400279 rcu_assign_pointer(*list, ops);
280}
Steven Rostedt3d083392008-05-12 21:20:42 +0200281
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800282static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
283 struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400284{
285 struct ftrace_ops **p;
286
287 /*
288 * If we are removing the last function, then simply point
289 * to the ftrace_stub.
290 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800291 if (rcu_dereference_protected(*list,
292 lockdep_is_held(&ftrace_lock)) == ops &&
293 rcu_dereference_protected(ops->next,
294 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt2b499382011-05-03 22:49:52 -0400295 *list = &ftrace_list_end;
296 return 0;
297 }
298
299 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
300 if (*p == ops)
301 break;
302
303 if (*p != ops)
304 return -1;
305
306 *p = (*p)->next;
307 return 0;
308}
309
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400310static void ftrace_update_trampoline(struct ftrace_ops *ops);
311
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500312int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400313{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500314 if (ops->flags & FTRACE_OPS_FL_DELETED)
315 return -EINVAL;
316
Steven Rostedtb8489142011-05-04 09:27:52 -0400317 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
318 return -EBUSY;
319
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900320#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400321 /*
322 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
323 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
324 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
325 */
326 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
327 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
328 return -EINVAL;
329
330 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
331 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
332#endif
333
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400334 if (!core_kernel_data((unsigned long)ops))
335 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
336
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500337 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400338
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400339 /* Always save the function, and reset at unregistering */
340 ops->saved_func = ops->func;
341
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400342 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400343 ops->func = ftrace_pid_func;
344
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400345 ftrace_update_trampoline(ops);
346
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400347 if (ftrace_enabled)
348 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200349
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200350 return 0;
351}
352
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500353int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200354{
Steven Rostedt2b499382011-05-03 22:49:52 -0400355 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200356
Steven Rostedtb8489142011-05-04 09:27:52 -0400357 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
358 return -EBUSY;
359
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500360 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400361
Steven Rostedt2b499382011-05-03 22:49:52 -0400362 if (ret < 0)
363 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400364
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400365 if (ftrace_enabled)
366 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200367
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400368 ops->func = ops->saved_func;
369
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500370 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200371}
372
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500373static void ftrace_update_pid_func(void)
374{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400375 struct ftrace_ops *op;
376
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400377 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500378 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900379 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500380
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400381 do_for_each_ftrace_op(op, ftrace_ops_list) {
382 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400383 op->func = ftrace_pids_enabled(op) ?
384 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400385 ftrace_update_trampoline(op);
386 }
387 } while_for_each_ftrace_op(op);
388
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400389 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500390}
391
Steven Rostedt493762f2009-03-23 17:12:36 -0400392#ifdef CONFIG_FUNCTION_PROFILER
393struct ftrace_profile {
394 struct hlist_node node;
395 unsigned long ip;
396 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400397#ifdef CONFIG_FUNCTION_GRAPH_TRACER
398 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400399 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400400#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400401};
402
403struct ftrace_profile_page {
404 struct ftrace_profile_page *next;
405 unsigned long index;
406 struct ftrace_profile records[];
407};
408
Steven Rostedtcafb1682009-03-24 20:50:39 -0400409struct ftrace_profile_stat {
410 atomic_t disabled;
411 struct hlist_head *hash;
412 struct ftrace_profile_page *pages;
413 struct ftrace_profile_page *start;
414 struct tracer_stat stat;
415};
416
Steven Rostedt493762f2009-03-23 17:12:36 -0400417#define PROFILE_RECORDS_SIZE \
418 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
419
420#define PROFILES_PER_PAGE \
421 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
422
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400423static int ftrace_profile_enabled __read_mostly;
424
425/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400426static DEFINE_MUTEX(ftrace_profile_lock);
427
Steven Rostedtcafb1682009-03-24 20:50:39 -0400428static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400429
Namhyung Kim20079eb2013-04-10 08:55:50 +0900430#define FTRACE_PROFILE_HASH_BITS 10
431#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400432
Steven Rostedt493762f2009-03-23 17:12:36 -0400433static void *
434function_stat_next(void *v, int idx)
435{
436 struct ftrace_profile *rec = v;
437 struct ftrace_profile_page *pg;
438
439 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
440
441 again:
Li Zefan0296e422009-06-26 11:15:37 +0800442 if (idx != 0)
443 rec++;
444
Steven Rostedt493762f2009-03-23 17:12:36 -0400445 if ((void *)rec >= (void *)&pg->records[pg->index]) {
446 pg = pg->next;
447 if (!pg)
448 return NULL;
449 rec = &pg->records[0];
450 if (!rec->counter)
451 goto again;
452 }
453
454 return rec;
455}
456
457static void *function_stat_start(struct tracer_stat *trace)
458{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400459 struct ftrace_profile_stat *stat =
460 container_of(trace, struct ftrace_profile_stat, stat);
461
462 if (!stat || !stat->start)
463 return NULL;
464
465 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400466}
467
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400468#ifdef CONFIG_FUNCTION_GRAPH_TRACER
469/* function graph compares on total time */
470static int function_stat_cmp(void *p1, void *p2)
471{
472 struct ftrace_profile *a = p1;
473 struct ftrace_profile *b = p2;
474
475 if (a->time < b->time)
476 return -1;
477 if (a->time > b->time)
478 return 1;
479 else
480 return 0;
481}
482#else
483/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400484static int function_stat_cmp(void *p1, void *p2)
485{
486 struct ftrace_profile *a = p1;
487 struct ftrace_profile *b = p2;
488
489 if (a->counter < b->counter)
490 return -1;
491 if (a->counter > b->counter)
492 return 1;
493 else
494 return 0;
495}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400496#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400497
498static int function_stat_headers(struct seq_file *m)
499{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400500#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100501 seq_puts(m, " Function "
502 "Hit Time Avg s^2\n"
503 " -------- "
504 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400505#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100506 seq_puts(m, " Function Hit\n"
507 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400508#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400509 return 0;
510}
511
512static int function_stat_show(struct seq_file *m, void *v)
513{
514 struct ftrace_profile *rec = v;
515 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800516 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400517#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400518 static struct trace_seq s;
519 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400520 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400521#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800522 mutex_lock(&ftrace_profile_lock);
523
524 /* we raced with function_profile_reset() */
525 if (unlikely(rec->counter == 0)) {
526 ret = -EBUSY;
527 goto out;
528 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400529
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530530#ifdef CONFIG_FUNCTION_GRAPH_TRACER
531 avg = rec->time;
532 do_div(avg, rec->counter);
533 if (tracing_thresh && (avg < tracing_thresh))
534 goto out;
535#endif
536
Steven Rostedt493762f2009-03-23 17:12:36 -0400537 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400538 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400539
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400540#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100541 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400542
Chase Douglase330b3b2010-04-26 14:02:05 -0400543 /* Sample standard deviation (s^2) */
544 if (rec->counter <= 1)
545 stddev = 0;
546 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200547 /*
548 * Apply Welford's method:
549 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
550 */
551 stddev = rec->counter * rec->time_squared -
552 rec->time * rec->time;
553
Chase Douglase330b3b2010-04-26 14:02:05 -0400554 /*
555 * Divide only 1000 for ns^2 -> us^2 conversion.
556 * trace_print_graph_duration will divide 1000 again.
557 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200558 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400559 }
560
Steven Rostedt34886c82009-03-25 21:00:47 -0400561 trace_seq_init(&s);
562 trace_print_graph_duration(rec->time, &s);
563 trace_seq_puts(&s, " ");
564 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400565 trace_seq_puts(&s, " ");
566 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400567 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400568#endif
569 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800570out:
571 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400572
Li Zefan3aaba202010-08-23 16:50:12 +0800573 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400574}
575
Steven Rostedtcafb1682009-03-24 20:50:39 -0400576static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400577{
578 struct ftrace_profile_page *pg;
579
Steven Rostedtcafb1682009-03-24 20:50:39 -0400580 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400581
582 while (pg) {
583 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
584 pg->index = 0;
585 pg = pg->next;
586 }
587
Steven Rostedtcafb1682009-03-24 20:50:39 -0400588 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400589 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
590}
591
Steven Rostedtcafb1682009-03-24 20:50:39 -0400592int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400593{
594 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400595 int functions;
596 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400597 int i;
598
599 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400600 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400601 return 0;
602
Steven Rostedtcafb1682009-03-24 20:50:39 -0400603 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
604 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400605 return -ENOMEM;
606
Steven Rostedt318e0a72009-03-25 20:06:34 -0400607#ifdef CONFIG_DYNAMIC_FTRACE
608 functions = ftrace_update_tot_cnt;
609#else
610 /*
611 * We do not know the number of functions that exist because
612 * dynamic tracing is what counts them. With past experience
613 * we have around 20K functions. That should be more than enough.
614 * It is highly unlikely we will execute every function in
615 * the kernel.
616 */
617 functions = 20000;
618#endif
619
Steven Rostedtcafb1682009-03-24 20:50:39 -0400620 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400621
Steven Rostedt318e0a72009-03-25 20:06:34 -0400622 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
623
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900624 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400625 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400626 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400627 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400628 pg = pg->next;
629 }
630
631 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400632
633 out_free:
634 pg = stat->start;
635 while (pg) {
636 unsigned long tmp = (unsigned long)pg;
637
638 pg = pg->next;
639 free_page(tmp);
640 }
641
Steven Rostedt318e0a72009-03-25 20:06:34 -0400642 stat->pages = NULL;
643 stat->start = NULL;
644
645 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400646}
647
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400649{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400650 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400651 int size;
652
Steven Rostedtcafb1682009-03-24 20:50:39 -0400653 stat = &per_cpu(ftrace_profile_stats, cpu);
654
655 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400656 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400657 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400658 return 0;
659 }
660
661 /*
662 * We are profiling all functions, but usually only a few thousand
663 * functions are hit. We'll make a hash of 1024 items.
664 */
665 size = FTRACE_PROFILE_HASH_SIZE;
666
Kees Cook6396bb22018-06-12 14:03:40 -0700667 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400670 return -ENOMEM;
671
Steven Rostedt318e0a72009-03-25 20:06:34 -0400672 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673 if (ftrace_profile_pages_init(stat) < 0) {
674 kfree(stat->hash);
675 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400676 return -ENOMEM;
677 }
678
679 return 0;
680}
681
Steven Rostedtcafb1682009-03-24 20:50:39 -0400682static int ftrace_profile_init(void)
683{
684 int cpu;
685 int ret = 0;
686
Miao Xiec4602c12013-12-16 15:20:01 +0800687 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400688 ret = ftrace_profile_init_cpu(cpu);
689 if (ret)
690 break;
691 }
692
693 return ret;
694}
695
Steven Rostedt493762f2009-03-23 17:12:36 -0400696/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697static struct ftrace_profile *
698ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400699{
700 struct ftrace_profile *rec;
701 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400702 unsigned long key;
703
Namhyung Kim20079eb2013-04-10 08:55:50 +0900704 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400705 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400706
707 if (hlist_empty(hhd))
708 return NULL;
709
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400710 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 if (rec->ip == ip)
712 return rec;
713 }
714
715 return NULL;
716}
717
Steven Rostedtcafb1682009-03-24 20:50:39 -0400718static void ftrace_add_profile(struct ftrace_profile_stat *stat,
719 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400720{
721 unsigned long key;
722
Namhyung Kim20079eb2013-04-10 08:55:50 +0900723 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400724 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400725}
726
Steven Rostedt318e0a72009-03-25 20:06:34 -0400727/*
728 * The memory is already allocated, this simply finds a new record to use.
729 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400730static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400731ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400732{
733 struct ftrace_profile *rec = NULL;
734
Steven Rostedt318e0a72009-03-25 20:06:34 -0400735 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400736 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400737 goto out;
738
Steven Rostedt493762f2009-03-23 17:12:36 -0400739 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400740 * Try to find the function again since an NMI
741 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400743 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400746
Steven Rostedtcafb1682009-03-24 20:50:39 -0400747 if (stat->pages->index == PROFILES_PER_PAGE) {
748 if (!stat->pages->next)
749 goto out;
750 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400751 }
752
Steven Rostedtcafb1682009-03-24 20:50:39 -0400753 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400754 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400755 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400756
Steven Rostedt493762f2009-03-23 17:12:36 -0400757 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400759
760 return rec;
761}
762
Steven Rostedt493762f2009-03-23 17:12:36 -0400763static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400764function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400765 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400766{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400768 struct ftrace_profile *rec;
769 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400770
771 if (!ftrace_profile_enabled)
772 return;
773
Steven Rostedt493762f2009-03-23 17:12:36 -0400774 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775
Christoph Lameterbdffd892014-04-29 14:17:40 -0500776 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400777 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400778 goto out;
779
780 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400781 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400782 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400783 if (!rec)
784 goto out;
785 }
786
787 rec->counter++;
788 out:
789 local_irq_restore(flags);
790}
791
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400792#ifdef CONFIG_FUNCTION_GRAPH_TRACER
793static int profile_graph_entry(struct ftrace_graph_ent *trace)
794{
Steven Rostedt (VMware)b1b35f22018-11-20 12:51:07 -0500795 int index = current->curr_ret_stack;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900796
Steven Rostedta1e2e312011-08-09 12:50:46 -0400797 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900798
Steven Rostedt (VMware)a8f0f9e2017-08-17 16:37:25 -0400799 /* If function graph is shutting down, ret_stack can be NULL */
800 if (!current->ret_stack)
801 return 0;
802
Namhyung Kim8861dd32016-08-31 11:55:29 +0900803 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
804 current->ret_stack[index].subtime = 0;
805
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400806 return 1;
807}
808
809static void profile_graph_return(struct ftrace_graph_ret *trace)
810{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400812 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400813 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400814 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400815
816 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500817 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400818 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 goto out;
820
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400821 /* If the calltime was zero'd ignore it */
822 if (!trace->calltime)
823 goto out;
824
Steven Rostedta2a16d62009-03-24 23:17:58 -0400825 calltime = trace->rettime - trace->calltime;
826
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400827 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400828 int index;
829
Steven Rostedt (VMware)b1b35f22018-11-20 12:51:07 -0500830 index = current->curr_ret_stack;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400831
832 /* Append this call time to the parent time to subtract */
833 if (index)
834 current->ret_stack[index - 1].subtime += calltime;
835
836 if (current->ret_stack[index].subtime < calltime)
837 calltime -= current->ret_stack[index].subtime;
838 else
839 calltime = 0;
840 }
841
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400843 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400844 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400845 rec->time_squared += calltime * calltime;
846 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400847
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400849 local_irq_restore(flags);
850}
851
852static int register_ftrace_profiler(void)
853{
854 return register_ftrace_graph(&profile_graph_return,
855 &profile_graph_entry);
856}
857
858static void unregister_ftrace_profiler(void)
859{
860 unregister_ftrace_graph();
861}
862#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100863static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400864 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900865 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400866 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400867};
868
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400869static int register_ftrace_profiler(void)
870{
871 return register_ftrace_function(&ftrace_profile_ops);
872}
873
874static void unregister_ftrace_profiler(void)
875{
876 unregister_ftrace_function(&ftrace_profile_ops);
877}
878#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
879
Steven Rostedt493762f2009-03-23 17:12:36 -0400880static ssize_t
881ftrace_profile_write(struct file *filp, const char __user *ubuf,
882 size_t cnt, loff_t *ppos)
883{
884 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400885 int ret;
886
Peter Huewe22fe9b52011-06-07 21:58:27 +0200887 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
888 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400889 return ret;
890
891 val = !!val;
892
893 mutex_lock(&ftrace_profile_lock);
894 if (ftrace_profile_enabled ^ val) {
895 if (val) {
896 ret = ftrace_profile_init();
897 if (ret < 0) {
898 cnt = ret;
899 goto out;
900 }
901
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400902 ret = register_ftrace_profiler();
903 if (ret < 0) {
904 cnt = ret;
905 goto out;
906 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400907 ftrace_profile_enabled = 1;
908 } else {
909 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400910 /*
911 * unregister_ftrace_profiler calls stop_machine
912 * so this acts like an synchronize_sched.
913 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400914 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400915 }
916 }
917 out:
918 mutex_unlock(&ftrace_profile_lock);
919
Jiri Olsacf8517c2009-10-23 19:36:16 -0400920 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400921
922 return cnt;
923}
924
925static ssize_t
926ftrace_profile_read(struct file *filp, char __user *ubuf,
927 size_t cnt, loff_t *ppos)
928{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400929 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400930 int r;
931
932 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
933 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
934}
935
936static const struct file_operations ftrace_profile_fops = {
937 .open = tracing_open_generic,
938 .read = ftrace_profile_read,
939 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200940 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400941};
942
Steven Rostedtcafb1682009-03-24 20:50:39 -0400943/* used to initialize the real stat files */
944static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400945 .name = "functions",
946 .stat_start = function_stat_start,
947 .stat_next = function_stat_next,
948 .stat_cmp = function_stat_cmp,
949 .stat_headers = function_stat_headers,
950 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400951};
952
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500953static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400954{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400955 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400956 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400957 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400958 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400959 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400960
Steven Rostedtcafb1682009-03-24 20:50:39 -0400961 for_each_possible_cpu(cpu) {
962 stat = &per_cpu(ftrace_profile_stats, cpu);
963
Geliang Tang6363c6b2016-03-15 22:12:34 +0800964 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400965 if (!name) {
966 /*
967 * The files created are permanent, if something happens
968 * we still do not free memory.
969 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400970 WARN(1,
971 "Could not allocate stat file for cpu %d\n",
972 cpu);
973 return;
974 }
975 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400976 stat->stat.name = name;
977 ret = register_stat_tracer(&stat->stat);
978 if (ret) {
979 WARN(1,
980 "Could not register function stat for cpu %d\n",
981 cpu);
982 kfree(name);
983 return;
984 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400985 }
986
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500987 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -0400988 d_tracer, NULL, &ftrace_profile_fops);
989 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700990 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -0400991}
992
993#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500994static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400995{
996}
997#endif /* CONFIG_FUNCTION_PROFILER */
998
Pratyush Anand1619dc32015-03-06 23:58:06 +0530999#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001000int ftrace_graph_active;
Pratyush Anand1619dc32015-03-06 23:58:06 +05301001#endif
1002
Steven Rostedt3d083392008-05-12 21:20:42 +02001003#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001004
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001005static struct ftrace_ops *removed_ops;
1006
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001007/*
1008 * Set when doing a global update, like enabling all recs or disabling them.
1009 * It is not set when just updating a single ftrace_ops.
1010 */
1011static bool update_all_ops;
1012
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001013#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001014# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001015#endif
1016
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001017struct ftrace_func_entry {
1018 struct hlist_node hlist;
1019 unsigned long ip;
1020};
1021
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001022struct ftrace_func_probe {
1023 struct ftrace_probe_ops *probe_ops;
1024 struct ftrace_ops ops;
1025 struct trace_array *tr;
1026 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001027 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001028 int ref;
1029};
1030
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001031/*
1032 * We make these constant because no one should touch them,
1033 * but they are used as the default "empty hash", to avoid allocating
1034 * it all the time. These are in a read only section such that if
1035 * anyone does try to modify it, it will cause an exception.
1036 */
1037static const struct hlist_head empty_buckets[1];
1038static const struct ftrace_hash empty_hash = {
1039 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001040};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001041#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001042
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001043struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001044 .func = ftrace_stub,
1045 .local_hash.notrace_hash = EMPTY_HASH,
1046 .local_hash.filter_hash = EMPTY_HASH,
1047 INIT_OPS_HASH(global_ops)
1048 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001049 FTRACE_OPS_FL_INITIALIZED |
1050 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001051};
1052
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001053/*
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001054 * Used by the stack undwinder to know about dynamic ftrace trampolines.
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001055 */
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001056struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001057{
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001058 struct ftrace_ops *op = NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001059
1060 /*
1061 * Some of the ops may be dynamically allocated,
1062 * they are freed after a synchronize_sched().
1063 */
1064 preempt_disable_notrace();
1065
1066 do_for_each_ftrace_op(op, ftrace_ops_list) {
1067 /*
1068 * This is to check for dynamically allocated trampolines.
1069 * Trampolines that are in kernel text will have
1070 * core_kernel_text() return true.
1071 */
1072 if (op->trampoline && op->trampoline_size)
1073 if (addr >= op->trampoline &&
1074 addr < op->trampoline + op->trampoline_size) {
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001075 preempt_enable_notrace();
1076 return op;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001077 }
1078 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001079 preempt_enable_notrace();
1080
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001081 return NULL;
1082}
1083
1084/*
1085 * This is used by __kernel_text_address() to return true if the
1086 * address is on a dynamically allocated trampoline that would
1087 * not return true for either core_kernel_text() or
1088 * is_module_text_address().
1089 */
1090bool is_ftrace_trampoline(unsigned long addr)
1091{
1092 return ftrace_ops_trampoline(addr) != NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001093}
1094
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001095struct ftrace_page {
1096 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001097 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001098 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001099 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001100};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101
Steven Rostedta7900872011-12-16 16:23:44 -05001102#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1103#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001104
1105/* estimate from running different kernels */
1106#define NR_TO_INIT 10000
1107
1108static struct ftrace_page *ftrace_pages_start;
1109static struct ftrace_page *ftrace_pages;
1110
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001111static __always_inline unsigned long
1112ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1113{
1114 if (hash->size_bits > 0)
1115 return hash_long(ip, hash->size_bits);
1116
1117 return 0;
1118}
1119
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001120/* Only use this function if ftrace_hash_empty() has already been tested */
1121static __always_inline struct ftrace_func_entry *
1122__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123{
1124 unsigned long key;
1125 struct ftrace_func_entry *entry;
1126 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001127
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001128 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001129 hhd = &hash->buckets[key];
1130
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001131 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001132 if (entry->ip == ip)
1133 return entry;
1134 }
1135 return NULL;
1136}
1137
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001138/**
1139 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1140 * @hash: The hash to look at
1141 * @ip: The instruction pointer to test
1142 *
1143 * Search a given @hash to see if a given instruction pointer (@ip)
1144 * exists in it.
1145 *
1146 * Returns the entry that holds the @ip if found. NULL otherwise.
1147 */
1148struct ftrace_func_entry *
1149ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1150{
1151 if (ftrace_hash_empty(hash))
1152 return NULL;
1153
1154 return __ftrace_lookup_ip(hash, ip);
1155}
1156
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001157static void __add_hash_entry(struct ftrace_hash *hash,
1158 struct ftrace_func_entry *entry)
1159{
1160 struct hlist_head *hhd;
1161 unsigned long key;
1162
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001163 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001164 hhd = &hash->buckets[key];
1165 hlist_add_head(&entry->hlist, hhd);
1166 hash->count++;
1167}
1168
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001169static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1170{
1171 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001172
1173 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1174 if (!entry)
1175 return -ENOMEM;
1176
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001177 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001178 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001179
1180 return 0;
1181}
1182
1183static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001184free_hash_entry(struct ftrace_hash *hash,
1185 struct ftrace_func_entry *entry)
1186{
1187 hlist_del(&entry->hlist);
1188 kfree(entry);
1189 hash->count--;
1190}
1191
1192static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001193remove_hash_entry(struct ftrace_hash *hash,
1194 struct ftrace_func_entry *entry)
1195{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001196 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001197 hash->count--;
1198}
1199
1200static void ftrace_hash_clear(struct ftrace_hash *hash)
1201{
1202 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001203 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001204 struct ftrace_func_entry *entry;
1205 int size = 1 << hash->size_bits;
1206 int i;
1207
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001208 if (!hash->count)
1209 return;
1210
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001211 for (i = 0; i < size; i++) {
1212 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001213 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001214 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001215 }
1216 FTRACE_WARN_ON(hash->count);
1217}
1218
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001219static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1220{
1221 list_del(&ftrace_mod->list);
1222 kfree(ftrace_mod->module);
1223 kfree(ftrace_mod->func);
1224 kfree(ftrace_mod);
1225}
1226
1227static void clear_ftrace_mod_list(struct list_head *head)
1228{
1229 struct ftrace_mod_load *p, *n;
1230
1231 /* stack tracer isn't supported yet */
1232 if (!head)
1233 return;
1234
1235 mutex_lock(&ftrace_lock);
1236 list_for_each_entry_safe(p, n, head, list)
1237 free_ftrace_mod(p);
1238 mutex_unlock(&ftrace_lock);
1239}
1240
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001241static void free_ftrace_hash(struct ftrace_hash *hash)
1242{
1243 if (!hash || hash == EMPTY_HASH)
1244 return;
1245 ftrace_hash_clear(hash);
1246 kfree(hash->buckets);
1247 kfree(hash);
1248}
1249
Steven Rostedt07fd5512011-05-05 18:03:47 -04001250static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1251{
1252 struct ftrace_hash *hash;
1253
1254 hash = container_of(rcu, struct ftrace_hash, rcu);
1255 free_ftrace_hash(hash);
1256}
1257
1258static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1259{
1260 if (!hash || hash == EMPTY_HASH)
1261 return;
1262 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1263}
1264
Jiri Olsa5500fa52012-02-15 15:51:54 +01001265void ftrace_free_filter(struct ftrace_ops *ops)
1266{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001267 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001268 free_ftrace_hash(ops->func_hash->filter_hash);
1269 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001270}
1271
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001272static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1273{
1274 struct ftrace_hash *hash;
1275 int size;
1276
1277 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1278 if (!hash)
1279 return NULL;
1280
1281 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001282 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001283
1284 if (!hash->buckets) {
1285 kfree(hash);
1286 return NULL;
1287 }
1288
1289 hash->size_bits = size_bits;
1290
1291 return hash;
1292}
1293
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001294
1295static int ftrace_add_mod(struct trace_array *tr,
1296 const char *func, const char *module,
1297 int enable)
1298{
1299 struct ftrace_mod_load *ftrace_mod;
1300 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1301
1302 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1303 if (!ftrace_mod)
1304 return -ENOMEM;
1305
1306 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1307 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1308 ftrace_mod->enable = enable;
1309
1310 if (!ftrace_mod->func || !ftrace_mod->module)
1311 goto out_free;
1312
1313 list_add(&ftrace_mod->list, mod_head);
1314
1315 return 0;
1316
1317 out_free:
1318 free_ftrace_mod(ftrace_mod);
1319
1320 return -ENOMEM;
1321}
1322
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001323static struct ftrace_hash *
1324alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1325{
1326 struct ftrace_func_entry *entry;
1327 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 int size;
1329 int ret;
1330 int i;
1331
1332 new_hash = alloc_ftrace_hash(size_bits);
1333 if (!new_hash)
1334 return NULL;
1335
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001336 if (hash)
1337 new_hash->flags = hash->flags;
1338
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001339 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001340 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001341 return new_hash;
1342
1343 size = 1 << hash->size_bits;
1344 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001345 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346 ret = add_hash_entry(new_hash, entry->ip);
1347 if (ret < 0)
1348 goto free_hash;
1349 }
1350 }
1351
1352 FTRACE_WARN_ON(new_hash->count != hash->count);
1353
1354 return new_hash;
1355
1356 free_hash:
1357 free_ftrace_hash(new_hash);
1358 return NULL;
1359}
1360
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001361static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001362ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001363static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001364ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001365
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001366static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1367 struct ftrace_hash *new_hash);
1368
Namhyung Kim3e278c02017-01-20 11:44:45 +09001369static struct ftrace_hash *
1370__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001371{
1372 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001373 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001374 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001375 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001376 int size = src->count;
1377 int bits = 0;
1378 int i;
1379
1380 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001381 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001382 */
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001383 if (ftrace_hash_empty(src))
Namhyung Kim3e278c02017-01-20 11:44:45 +09001384 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001386 /*
1387 * Make the hash size about 1/2 the # found
1388 */
1389 for (size /= 2; size; size >>= 1)
1390 bits++;
1391
1392 /* Don't allocate too much */
1393 if (bits > FTRACE_HASH_MAX_BITS)
1394 bits = FTRACE_HASH_MAX_BITS;
1395
Steven Rostedt07fd5512011-05-05 18:03:47 -04001396 new_hash = alloc_ftrace_hash(bits);
1397 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001398 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001400 new_hash->flags = src->flags;
1401
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001402 size = 1 << src->size_bits;
1403 for (i = 0; i < size; i++) {
1404 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001405 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001406 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001407 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001408 }
1409 }
1410
Namhyung Kim3e278c02017-01-20 11:44:45 +09001411 return new_hash;
1412}
1413
1414static int
1415ftrace_hash_move(struct ftrace_ops *ops, int enable,
1416 struct ftrace_hash **dst, struct ftrace_hash *src)
1417{
1418 struct ftrace_hash *new_hash;
1419 int ret;
1420
1421 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1422 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1423 return -EINVAL;
1424
1425 new_hash = __ftrace_hash_move(src);
1426 if (!new_hash)
1427 return -ENOMEM;
1428
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001429 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1430 if (enable) {
1431 /* IPMODIFY should be updated only when filter_hash updating */
1432 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1433 if (ret < 0) {
1434 free_ftrace_hash(new_hash);
1435 return ret;
1436 }
1437 }
1438
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001439 /*
1440 * Remove the current set, update the hash and add
1441 * them back.
1442 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001443 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001444
Steven Rostedt07fd5512011-05-05 18:03:47 -04001445 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001446
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001447 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001448
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001449 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001450}
1451
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001452static bool hash_contains_ip(unsigned long ip,
1453 struct ftrace_ops_hash *hash)
1454{
1455 /*
1456 * The function record is a match if it exists in the filter
1457 * hash and not in the notrace hash. Note, an emty hash is
1458 * considered a match for the filter hash, but an empty
1459 * notrace hash is considered not in the notrace hash.
1460 */
1461 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001462 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001463 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001464 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001465}
1466
Steven Rostedt265c8312009-02-13 12:43:56 -05001467/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001468 * Test the hashes for this ops to see if we want to call
1469 * the ops->func or not.
1470 *
1471 * It's a match if the ip is in the ops->filter_hash or
1472 * the filter_hash does not exist or is empty,
1473 * AND
1474 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001475 *
1476 * This needs to be called with preemption disabled as
1477 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001478 */
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001479int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001480ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001481{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001482 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001483 int ret;
1484
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001485#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1486 /*
1487 * There's a small race when adding ops that the ftrace handler
1488 * that wants regs, may be called without them. We can not
1489 * allow that handler to be called if regs is NULL.
1490 */
1491 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1492 return 0;
1493#endif
1494
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001495 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1496 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001497
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001498 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001499 ret = 1;
1500 else
1501 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001502
1503 return ret;
1504}
1505
1506/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001507 * This is a double for. Do not use 'break' to break out of the loop,
1508 * you must use a goto.
1509 */
1510#define do_for_each_ftrace_rec(pg, rec) \
1511 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1512 int _____i; \
1513 for (_____i = 0; _____i < pg->index; _____i++) { \
1514 rec = &pg->records[_____i];
1515
1516#define while_for_each_ftrace_rec() \
1517 } \
1518 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301519
Steven Rostedt5855fea2011-12-16 19:27:42 -05001520
1521static int ftrace_cmp_recs(const void *a, const void *b)
1522{
Steven Rostedta650e022012-04-25 13:48:13 -04001523 const struct dyn_ftrace *key = a;
1524 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001525
Steven Rostedta650e022012-04-25 13:48:13 -04001526 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001527 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001528 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1529 return 1;
1530 return 0;
1531}
1532
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001533/**
1534 * ftrace_location_range - return the first address of a traced location
1535 * if it touches the given ip range
1536 * @start: start of range to search.
1537 * @end: end of range to search (inclusive). @end points to the last byte
1538 * to check.
1539 *
1540 * Returns rec->ip if the related ftrace location is a least partly within
1541 * the given address range. That is, the first address of the instruction
1542 * that is either a NOP or call to the function tracer. It checks the ftrace
1543 * internal tables to determine if the address belongs or not.
1544 */
1545unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001546{
1547 struct ftrace_page *pg;
1548 struct dyn_ftrace *rec;
1549 struct dyn_ftrace key;
1550
1551 key.ip = start;
1552 key.flags = end; /* overload flags, as it is unsigned long */
1553
1554 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1555 if (end < pg->records[0].ip ||
1556 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1557 continue;
1558 rec = bsearch(&key, pg->records, pg->index,
1559 sizeof(struct dyn_ftrace),
1560 ftrace_cmp_recs);
1561 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001562 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001563 }
1564
Steven Rostedt5855fea2011-12-16 19:27:42 -05001565 return 0;
1566}
1567
Steven Rostedtc88fd862011-08-16 09:53:39 -04001568/**
1569 * ftrace_location - return true if the ip giving is a traced location
1570 * @ip: the instruction pointer to check
1571 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001572 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001573 * That is, the instruction that is either a NOP or call to
1574 * the function tracer. It checks the ftrace internal tables to
1575 * determine if the address belongs or not.
1576 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001577unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001578{
Steven Rostedta650e022012-04-25 13:48:13 -04001579 return ftrace_location_range(ip, ip);
1580}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001581
Steven Rostedta650e022012-04-25 13:48:13 -04001582/**
1583 * ftrace_text_reserved - return true if range contains an ftrace location
1584 * @start: start of range to search
1585 * @end: end of range to search (inclusive). @end points to the last byte to check.
1586 *
1587 * Returns 1 if @start and @end contains a ftrace location.
1588 * That is, the instruction that is either a NOP or call to
1589 * the function tracer. It checks the ftrace internal tables to
1590 * determine if the address belongs or not.
1591 */
Sasha Levind88471c2013-01-09 18:09:20 -05001592int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001593{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001594 unsigned long ret;
1595
1596 ret = ftrace_location_range((unsigned long)start,
1597 (unsigned long)end);
1598
1599 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001600}
1601
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001602/* Test if ops registered to this rec needs regs */
1603static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1604{
1605 struct ftrace_ops *ops;
1606 bool keep_regs = false;
1607
1608 for (ops = ftrace_ops_list;
1609 ops != &ftrace_list_end; ops = ops->next) {
1610 /* pass rec in as regs to have non-NULL val */
1611 if (ftrace_ops_test(ops, rec->ip, rec)) {
1612 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1613 keep_regs = true;
1614 break;
1615 }
1616 }
1617 }
1618
1619 return keep_regs;
1620}
1621
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001622static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001623 int filter_hash,
1624 bool inc)
1625{
1626 struct ftrace_hash *hash;
1627 struct ftrace_hash *other_hash;
1628 struct ftrace_page *pg;
1629 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001630 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001631 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001632 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001633
1634 /* Only update if the ops has been registered */
1635 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001636 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001637
1638 /*
1639 * In the filter_hash case:
1640 * If the count is zero, we update all records.
1641 * Otherwise we just update the items in the hash.
1642 *
1643 * In the notrace_hash case:
1644 * We enable the update in the hash.
1645 * As disabling notrace means enabling the tracing,
1646 * and enabling notrace means disabling, the inc variable
1647 * gets inversed.
1648 */
1649 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001650 hash = ops->func_hash->filter_hash;
1651 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001652 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001653 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001654 } else {
1655 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001656 hash = ops->func_hash->notrace_hash;
1657 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001658 /*
1659 * If the notrace hash has no items,
1660 * then there's nothing to do.
1661 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001662 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001663 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001664 }
1665
1666 do_for_each_ftrace_rec(pg, rec) {
1667 int in_other_hash = 0;
1668 int in_hash = 0;
1669 int match = 0;
1670
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001671 if (rec->flags & FTRACE_FL_DISABLED)
1672 continue;
1673
Steven Rostedted926f92011-05-03 13:25:24 -04001674 if (all) {
1675 /*
1676 * Only the filter_hash affects all records.
1677 * Update if the record is not in the notrace hash.
1678 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001679 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001680 match = 1;
1681 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001682 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1683 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001684
1685 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001686 * If filter_hash is set, we want to match all functions
1687 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001688 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001689 * If filter_hash is not set, then we are decrementing.
1690 * That means we match anything that is in the hash
1691 * and also in the other_hash. That is, we need to turn
1692 * off functions in the other hash because they are disabled
1693 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001694 */
1695 if (filter_hash && in_hash && !in_other_hash)
1696 match = 1;
1697 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001698 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001699 match = 1;
1700 }
1701 if (!match)
1702 continue;
1703
1704 if (inc) {
1705 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001706 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001707 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001708
1709 /*
1710 * If there's only a single callback registered to a
1711 * function, and the ops has a trampoline registered
1712 * for it, then we can call it directly.
1713 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001714 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001715 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001716 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001717 /*
1718 * If we are adding another function callback
1719 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001720 * custom trampoline in use, then we need to go
1721 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001722 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001723 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001724
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001725 /*
1726 * If any ops wants regs saved for this function
1727 * then all ops will get saved regs.
1728 */
1729 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1730 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001731 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001732 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001733 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001734 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001735
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001736 /*
1737 * If the rec had REGS enabled and the ops that is
1738 * being removed had REGS set, then see if there is
1739 * still any ops for this record that wants regs.
1740 * If not, we can stop recording them.
1741 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001742 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001743 rec->flags & FTRACE_FL_REGS &&
1744 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1745 if (!test_rec_ops_needs_regs(rec))
1746 rec->flags &= ~FTRACE_FL_REGS;
1747 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001748
1749 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001750 * If the rec had TRAMP enabled, then it needs to
1751 * be cleared. As TRAMP can only be enabled iff
1752 * there is only a single ops attached to it.
1753 * In otherwords, always disable it on decrementing.
1754 * In the future, we may set it if rec count is
1755 * decremented to one, and the ops that is left
1756 * has a trampoline.
1757 */
1758 rec->flags &= ~FTRACE_FL_TRAMP;
1759
1760 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001761 * flags will be cleared in ftrace_check_record()
1762 * if rec count is zero.
1763 */
Steven Rostedted926f92011-05-03 13:25:24 -04001764 }
1765 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001766
1767 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1768 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1769
Steven Rostedted926f92011-05-03 13:25:24 -04001770 /* Shortcut, if we handled all records, we are done. */
1771 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001772 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001773 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001774
1775 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001776}
1777
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001778static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001779 int filter_hash)
1780{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001781 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001782}
1783
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001784static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001785 int filter_hash)
1786{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001787 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001788}
1789
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001790static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1791 int filter_hash, int inc)
1792{
1793 struct ftrace_ops *op;
1794
1795 __ftrace_hash_rec_update(ops, filter_hash, inc);
1796
1797 if (ops->func_hash != &global_ops.local_hash)
1798 return;
1799
1800 /*
1801 * If the ops shares the global_ops hash, then we need to update
1802 * all ops that are enabled and use this hash.
1803 */
1804 do_for_each_ftrace_op(op, ftrace_ops_list) {
1805 /* Already done */
1806 if (op == ops)
1807 continue;
1808 if (op->func_hash == &global_ops.local_hash)
1809 __ftrace_hash_rec_update(op, filter_hash, inc);
1810 } while_for_each_ftrace_op(op);
1811}
1812
1813static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1814 int filter_hash)
1815{
1816 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1817}
1818
1819static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1820 int filter_hash)
1821{
1822 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1823}
1824
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001825/*
1826 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1827 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1828 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1829 * Note that old_hash and new_hash has below meanings
1830 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1831 * - If the hash is EMPTY_HASH, it hits nothing
1832 * - Anything else hits the recs which match the hash entries.
1833 */
1834static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1835 struct ftrace_hash *old_hash,
1836 struct ftrace_hash *new_hash)
1837{
1838 struct ftrace_page *pg;
1839 struct dyn_ftrace *rec, *end = NULL;
1840 int in_old, in_new;
1841
1842 /* Only update if the ops has been registered */
1843 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1844 return 0;
1845
1846 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1847 return 0;
1848
1849 /*
1850 * Since the IPMODIFY is a very address sensitive action, we do not
1851 * allow ftrace_ops to set all functions to new hash.
1852 */
1853 if (!new_hash || !old_hash)
1854 return -EINVAL;
1855
1856 /* Update rec->flags */
1857 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001858
1859 if (rec->flags & FTRACE_FL_DISABLED)
1860 continue;
1861
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001862 /* We need to update only differences of filter_hash */
1863 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1864 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1865 if (in_old == in_new)
1866 continue;
1867
1868 if (in_new) {
1869 /* New entries must ensure no others are using it */
1870 if (rec->flags & FTRACE_FL_IPMODIFY)
1871 goto rollback;
1872 rec->flags |= FTRACE_FL_IPMODIFY;
1873 } else /* Removed entry */
1874 rec->flags &= ~FTRACE_FL_IPMODIFY;
1875 } while_for_each_ftrace_rec();
1876
1877 return 0;
1878
1879rollback:
1880 end = rec;
1881
1882 /* Roll back what we did above */
1883 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001884
1885 if (rec->flags & FTRACE_FL_DISABLED)
1886 continue;
1887
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001888 if (rec == end)
1889 goto err_out;
1890
1891 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1892 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1893 if (in_old == in_new)
1894 continue;
1895
1896 if (in_new)
1897 rec->flags &= ~FTRACE_FL_IPMODIFY;
1898 else
1899 rec->flags |= FTRACE_FL_IPMODIFY;
1900 } while_for_each_ftrace_rec();
1901
1902err_out:
1903 return -EBUSY;
1904}
1905
1906static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1907{
1908 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1909
1910 if (ftrace_hash_empty(hash))
1911 hash = NULL;
1912
1913 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1914}
1915
1916/* Disabling always succeeds */
1917static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1918{
1919 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1920
1921 if (ftrace_hash_empty(hash))
1922 hash = NULL;
1923
1924 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1925}
1926
1927static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1928 struct ftrace_hash *new_hash)
1929{
1930 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1931
1932 if (ftrace_hash_empty(old_hash))
1933 old_hash = NULL;
1934
1935 if (ftrace_hash_empty(new_hash))
1936 new_hash = NULL;
1937
1938 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1939}
1940
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001941static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001942{
1943 int i;
1944
1945 printk(KERN_CONT "%s", fmt);
1946
1947 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1948 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1949}
1950
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001951static struct ftrace_ops *
1952ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001953static struct ftrace_ops *
1954ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001955
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001956enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001957const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001958
1959static void print_bug_type(void)
1960{
1961 switch (ftrace_bug_type) {
1962 case FTRACE_BUG_UNKNOWN:
1963 break;
1964 case FTRACE_BUG_INIT:
1965 pr_info("Initializing ftrace call sites\n");
1966 break;
1967 case FTRACE_BUG_NOP:
1968 pr_info("Setting ftrace call site to NOP\n");
1969 break;
1970 case FTRACE_BUG_CALL:
1971 pr_info("Setting ftrace call site to call ftrace function\n");
1972 break;
1973 case FTRACE_BUG_UPDATE:
1974 pr_info("Updating ftrace call site to call a different ftrace function\n");
1975 break;
1976 }
1977}
1978
Steven Rostedtc88fd862011-08-16 09:53:39 -04001979/**
1980 * ftrace_bug - report and shutdown function tracer
1981 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001982 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001983 *
1984 * The arch code that enables or disables the function tracing
1985 * can call ftrace_bug() when it has detected a problem in
1986 * modifying the code. @failed should be one of either:
1987 * EFAULT - if the problem happens on reading the @ip address
1988 * EINVAL - if what is read at @ip is not what was expected
1989 * EPERM - if the problem happens on writting to the @ip address
1990 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001991void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001992{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001993 unsigned long ip = rec ? rec->ip : 0;
1994
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001995 switch (failed) {
1996 case -EFAULT:
1997 FTRACE_WARN_ON_ONCE(1);
1998 pr_info("ftrace faulted on modifying ");
1999 print_ip_sym(ip);
2000 break;
2001 case -EINVAL:
2002 FTRACE_WARN_ON_ONCE(1);
2003 pr_info("ftrace failed to modify ");
2004 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002005 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002006 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002007 if (ftrace_expected) {
2008 print_ip_ins(" expected: ", ftrace_expected);
2009 pr_cont("\n");
2010 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002011 break;
2012 case -EPERM:
2013 FTRACE_WARN_ON_ONCE(1);
2014 pr_info("ftrace faulted on writing ");
2015 print_ip_sym(ip);
2016 break;
2017 default:
2018 FTRACE_WARN_ON_ONCE(1);
2019 pr_info("ftrace faulted on unknown error ");
2020 print_ip_sym(ip);
2021 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002022 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002023 if (rec) {
2024 struct ftrace_ops *ops = NULL;
2025
2026 pr_info("ftrace record flags: %lx\n", rec->flags);
2027 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2028 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2029 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2030 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002031 if (ops) {
2032 do {
2033 pr_cont("\ttramp: %pS (%pS)",
2034 (void *)ops->trampoline,
2035 (void *)ops->func);
2036 ops = ftrace_find_tramp_ops_next(rec, ops);
2037 } while (ops);
2038 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002039 pr_cont("\ttramp: ERROR!");
2040
2041 }
2042 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002043 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002044 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002045}
2046
Steven Rostedtc88fd862011-08-16 09:53:39 -04002047static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002048{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002049 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002050
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002051 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2052
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002053 if (rec->flags & FTRACE_FL_DISABLED)
2054 return FTRACE_UPDATE_IGNORE;
2055
Steven Rostedt982c3502008-11-15 16:31:41 -05002056 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002057 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002058 *
Steven Rostedted926f92011-05-03 13:25:24 -04002059 * If the record has a ref count, then we need to enable it
2060 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002061 *
Steven Rostedted926f92011-05-03 13:25:24 -04002062 * Otherwise we make sure its disabled.
2063 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002064 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002065 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002066 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002067 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002068 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002069
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002070 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002071 * If enabling and the REGS flag does not match the REGS_EN, or
2072 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2073 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002074 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002075 if (flag) {
2076 if (!(rec->flags & FTRACE_FL_REGS) !=
2077 !(rec->flags & FTRACE_FL_REGS_EN))
2078 flag |= FTRACE_FL_REGS;
2079
2080 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2081 !(rec->flags & FTRACE_FL_TRAMP_EN))
2082 flag |= FTRACE_FL_TRAMP;
2083 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002084
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002085 /* If the state of this record hasn't changed, then do nothing */
2086 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002087 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002088
2089 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002090 /* Save off if rec is being enabled (for return value) */
2091 flag ^= rec->flags & FTRACE_FL_ENABLED;
2092
2093 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002094 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002095 if (flag & FTRACE_FL_REGS) {
2096 if (rec->flags & FTRACE_FL_REGS)
2097 rec->flags |= FTRACE_FL_REGS_EN;
2098 else
2099 rec->flags &= ~FTRACE_FL_REGS_EN;
2100 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002101 if (flag & FTRACE_FL_TRAMP) {
2102 if (rec->flags & FTRACE_FL_TRAMP)
2103 rec->flags |= FTRACE_FL_TRAMP_EN;
2104 else
2105 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2106 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002107 }
2108
2109 /*
2110 * If this record is being updated from a nop, then
2111 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002112 * Otherwise,
2113 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002114 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002115 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002116 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002117 if (flag & FTRACE_FL_ENABLED) {
2118 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002119 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002120 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002121
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002122 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002123 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002124 }
2125
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002126 if (update) {
2127 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002128 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002129 rec->flags = 0;
2130 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002131 /*
2132 * Just disable the record, but keep the ops TRAMP
2133 * and REGS states. The _EN flags must be disabled though.
2134 */
2135 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2136 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002137 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002138
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002139 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002140 return FTRACE_UPDATE_MAKE_NOP;
2141}
2142
2143/**
2144 * ftrace_update_record, set a record that now is tracing or not
2145 * @rec: the record to update
2146 * @enable: set to 1 if the record is tracing, zero to force disable
2147 *
2148 * The records that represent all functions that can be traced need
2149 * to be updated when tracing has been enabled.
2150 */
2151int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2152{
2153 return ftrace_check_record(rec, enable, 1);
2154}
2155
2156/**
2157 * ftrace_test_record, check if the record has been enabled or not
2158 * @rec: the record to test
2159 * @enable: set to 1 to check if enabled, 0 if it is disabled
2160 *
2161 * The arch code may need to test if a record is already set to
2162 * tracing to determine how to modify the function code that it
2163 * represents.
2164 */
2165int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2166{
2167 return ftrace_check_record(rec, enable, 0);
2168}
2169
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002170static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002171ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2172{
2173 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002174 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002175
2176 do_for_each_ftrace_op(op, ftrace_ops_list) {
2177
2178 if (!op->trampoline)
2179 continue;
2180
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002181 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002182 return op;
2183 } while_for_each_ftrace_op(op);
2184
2185 return NULL;
2186}
2187
2188static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002189ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2190 struct ftrace_ops *op)
2191{
2192 unsigned long ip = rec->ip;
2193
2194 while_for_each_ftrace_op(op) {
2195
2196 if (!op->trampoline)
2197 continue;
2198
2199 if (hash_contains_ip(ip, op->func_hash))
2200 return op;
2201 }
2202
2203 return NULL;
2204}
2205
2206static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002207ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2208{
2209 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002210 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002211
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002212 /*
2213 * Need to check removed ops first.
2214 * If they are being removed, and this rec has a tramp,
2215 * and this rec is in the ops list, then it would be the
2216 * one with the tramp.
2217 */
2218 if (removed_ops) {
2219 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002220 return removed_ops;
2221 }
2222
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002223 /*
2224 * Need to find the current trampoline for a rec.
2225 * Now, a trampoline is only attached to a rec if there
2226 * was a single 'ops' attached to it. But this can be called
2227 * when we are adding another op to the rec or removing the
2228 * current one. Thus, if the op is being added, we can
2229 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002230 * yet.
2231 *
2232 * If an ops is being modified (hooking to different functions)
2233 * then we don't care about the new functions that are being
2234 * added, just the old ones (that are probably being removed).
2235 *
2236 * If we are adding an ops to a function that already is using
2237 * a trampoline, it needs to be removed (trampolines are only
2238 * for single ops connected), then an ops that is not being
2239 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002240 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002241 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002242
2243 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002244 continue;
2245
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002246 /*
2247 * If the ops is being added, it hasn't gotten to
2248 * the point to be removed from this tree yet.
2249 */
2250 if (op->flags & FTRACE_OPS_FL_ADDING)
2251 continue;
2252
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002253
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002254 /*
2255 * If the ops is being modified and is in the old
2256 * hash, then it is probably being removed from this
2257 * function.
2258 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002259 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2260 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002261 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002262 /*
2263 * If the ops is not being added or modified, and it's
2264 * in its normal filter hash, then this must be the one
2265 * we want!
2266 */
2267 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2268 hash_contains_ip(ip, op->func_hash))
2269 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002270
2271 } while_for_each_ftrace_op(op);
2272
2273 return NULL;
2274}
2275
2276static struct ftrace_ops *
2277ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2278{
2279 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002280 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002281
2282 do_for_each_ftrace_op(op, ftrace_ops_list) {
2283 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002284 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002285 return op;
2286 } while_for_each_ftrace_op(op);
2287
2288 return NULL;
2289}
2290
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002291/**
2292 * ftrace_get_addr_new - Get the call address to set to
2293 * @rec: The ftrace record descriptor
2294 *
2295 * If the record has the FTRACE_FL_REGS set, that means that it
2296 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2297 * is not not set, then it wants to convert to the normal callback.
2298 *
2299 * Returns the address of the trampoline to set to
2300 */
2301unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2302{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002303 struct ftrace_ops *ops;
2304
2305 /* Trampolines take precedence over regs */
2306 if (rec->flags & FTRACE_FL_TRAMP) {
2307 ops = ftrace_find_tramp_ops_new(rec);
2308 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002309 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2310 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002311 /* Ftrace is shutting down, return anything */
2312 return (unsigned long)FTRACE_ADDR;
2313 }
2314 return ops->trampoline;
2315 }
2316
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002317 if (rec->flags & FTRACE_FL_REGS)
2318 return (unsigned long)FTRACE_REGS_ADDR;
2319 else
2320 return (unsigned long)FTRACE_ADDR;
2321}
2322
2323/**
2324 * ftrace_get_addr_curr - Get the call address that is already there
2325 * @rec: The ftrace record descriptor
2326 *
2327 * The FTRACE_FL_REGS_EN is set when the record already points to
2328 * a function that saves all the regs. Basically the '_EN' version
2329 * represents the current state of the function.
2330 *
2331 * Returns the address of the trampoline that is currently being called
2332 */
2333unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2334{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002335 struct ftrace_ops *ops;
2336
2337 /* Trampolines take precedence over regs */
2338 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2339 ops = ftrace_find_tramp_ops_curr(rec);
2340 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002341 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2342 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002343 /* Ftrace is shutting down, return anything */
2344 return (unsigned long)FTRACE_ADDR;
2345 }
2346 return ops->trampoline;
2347 }
2348
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002349 if (rec->flags & FTRACE_FL_REGS_EN)
2350 return (unsigned long)FTRACE_REGS_ADDR;
2351 else
2352 return (unsigned long)FTRACE_ADDR;
2353}
2354
Steven Rostedtc88fd862011-08-16 09:53:39 -04002355static int
2356__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2357{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002358 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002359 unsigned long ftrace_addr;
2360 int ret;
2361
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002362 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002363
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002364 /* This needs to be done before we call ftrace_update_record */
2365 ftrace_old_addr = ftrace_get_addr_curr(rec);
2366
2367 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002368
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002369 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2370
Steven Rostedtc88fd862011-08-16 09:53:39 -04002371 switch (ret) {
2372 case FTRACE_UPDATE_IGNORE:
2373 return 0;
2374
2375 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002376 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002377 return ftrace_make_call(rec, ftrace_addr);
2378
2379 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002380 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002381 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002382
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002383 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002384 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002385 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002386 }
2387
2388 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002389}
2390
Steven Rostedte4f5d542012-04-27 09:13:18 -04002391void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002392{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002393 struct dyn_ftrace *rec;
2394 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002395 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002396
Steven Rostedt45a4a232011-04-21 23:16:46 -04002397 if (unlikely(ftrace_disabled))
2398 return;
2399
Steven Rostedt265c8312009-02-13 12:43:56 -05002400 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002401
2402 if (rec->flags & FTRACE_FL_DISABLED)
2403 continue;
2404
Steven Rostedte4f5d542012-04-27 09:13:18 -04002405 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002406 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002407 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002408 /* Stop processing */
2409 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002410 }
2411 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002412}
2413
Steven Rostedtc88fd862011-08-16 09:53:39 -04002414struct ftrace_rec_iter {
2415 struct ftrace_page *pg;
2416 int index;
2417};
2418
2419/**
2420 * ftrace_rec_iter_start, start up iterating over traced functions
2421 *
2422 * Returns an iterator handle that is used to iterate over all
2423 * the records that represent address locations where functions
2424 * are traced.
2425 *
2426 * May return NULL if no records are available.
2427 */
2428struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2429{
2430 /*
2431 * We only use a single iterator.
2432 * Protected by the ftrace_lock mutex.
2433 */
2434 static struct ftrace_rec_iter ftrace_rec_iter;
2435 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2436
2437 iter->pg = ftrace_pages_start;
2438 iter->index = 0;
2439
2440 /* Could have empty pages */
2441 while (iter->pg && !iter->pg->index)
2442 iter->pg = iter->pg->next;
2443
2444 if (!iter->pg)
2445 return NULL;
2446
2447 return iter;
2448}
2449
2450/**
2451 * ftrace_rec_iter_next, get the next record to process.
2452 * @iter: The handle to the iterator.
2453 *
2454 * Returns the next iterator after the given iterator @iter.
2455 */
2456struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2457{
2458 iter->index++;
2459
2460 if (iter->index >= iter->pg->index) {
2461 iter->pg = iter->pg->next;
2462 iter->index = 0;
2463
2464 /* Could have empty pages */
2465 while (iter->pg && !iter->pg->index)
2466 iter->pg = iter->pg->next;
2467 }
2468
2469 if (!iter->pg)
2470 return NULL;
2471
2472 return iter;
2473}
2474
2475/**
2476 * ftrace_rec_iter_record, get the record at the iterator location
2477 * @iter: The current iterator location
2478 *
2479 * Returns the record that the current @iter is at.
2480 */
2481struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2482{
2483 return &iter->pg->records[iter->index];
2484}
2485
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302486static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002487ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002488{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002489 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002490
Steven Rostedt45a4a232011-04-21 23:16:46 -04002491 if (unlikely(ftrace_disabled))
2492 return 0;
2493
Shaohua Li25aac9d2009-01-09 11:29:40 +08002494 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002495 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002496 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002497 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302498 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002499 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302500 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002501}
2502
Steven Rostedt000ab692009-02-17 13:35:06 -05002503/*
2504 * archs can override this function if they must do something
2505 * before the modifying code is performed.
2506 */
2507int __weak ftrace_arch_code_modify_prepare(void)
2508{
2509 return 0;
2510}
2511
2512/*
2513 * archs can override this function if they must do something
2514 * after the modifying code is performed.
2515 */
2516int __weak ftrace_arch_code_modify_post_process(void)
2517{
2518 return 0;
2519}
2520
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002521void ftrace_modify_all_code(int command)
2522{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002523 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002524 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002525
2526 /*
2527 * If the ftrace_caller calls a ftrace_ops func directly,
2528 * we need to make sure that it only traces functions it
2529 * expects to trace. When doing the switch of functions,
2530 * we need to update to the ftrace_ops_list_func first
2531 * before the transition between old and new calls are set,
2532 * as the ftrace_ops_list_func will check the ops hashes
2533 * to make sure the ops are having the right functions
2534 * traced.
2535 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002536 if (update) {
2537 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2538 if (FTRACE_WARN_ON(err))
2539 return;
2540 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002541
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002542 if (command & FTRACE_UPDATE_CALLS)
2543 ftrace_replace_code(1);
2544 else if (command & FTRACE_DISABLE_CALLS)
2545 ftrace_replace_code(0);
2546
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002547 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2548 function_trace_op = set_function_trace_op;
2549 smp_wmb();
2550 /* If irqs are disabled, we are in stop machine */
2551 if (!irqs_disabled())
2552 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002553 err = ftrace_update_ftrace_func(ftrace_trace_function);
2554 if (FTRACE_WARN_ON(err))
2555 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002556 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002557
2558 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002559 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002560 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002561 err = ftrace_disable_ftrace_graph_caller();
2562 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002563}
2564
Ingo Molnare309b412008-05-12 21:20:51 +02002565static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002566{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002567 int *command = data;
2568
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002569 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002570
Steven Rostedtc88fd862011-08-16 09:53:39 -04002571 return 0;
2572}
2573
2574/**
2575 * ftrace_run_stop_machine, go back to the stop machine method
2576 * @command: The command to tell ftrace what to do
2577 *
2578 * If an arch needs to fall back to the stop machine method, the
2579 * it can call this function.
2580 */
2581void ftrace_run_stop_machine(int command)
2582{
2583 stop_machine(__ftrace_modify_code, &command, NULL);
2584}
2585
2586/**
2587 * arch_ftrace_update_code, modify the code to trace or not trace
2588 * @command: The command that needs to be done
2589 *
2590 * Archs can override this function if it does not need to
2591 * run stop_machine() to modify code.
2592 */
2593void __weak arch_ftrace_update_code(int command)
2594{
2595 ftrace_run_stop_machine(command);
2596}
2597
2598static void ftrace_run_update_code(int command)
2599{
2600 int ret;
2601
2602 ret = ftrace_arch_code_modify_prepare();
2603 FTRACE_WARN_ON(ret);
2604 if (ret)
2605 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002606
2607 /*
2608 * By default we use stop_machine() to modify the code.
2609 * But archs can do what ever they want as long as it
2610 * is safe. The stop_machine() is the safest, but also
2611 * produces the most overhead.
2612 */
2613 arch_ftrace_update_code(command);
2614
Steven Rostedt000ab692009-02-17 13:35:06 -05002615 ret = ftrace_arch_code_modify_post_process();
2616 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002617}
2618
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002619static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002620 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002621{
2622 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002623 ops->old_hash.filter_hash = old_hash->filter_hash;
2624 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002625 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002626 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002627 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002628 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2629}
2630
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002631static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002632static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002633
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002634void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2635{
2636}
2637
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002638static void ftrace_startup_enable(int command)
2639{
2640 if (saved_ftrace_func != ftrace_trace_function) {
2641 saved_ftrace_func = ftrace_trace_function;
2642 command |= FTRACE_UPDATE_TRACE_FUNC;
2643 }
2644
2645 if (!command || !ftrace_enabled)
2646 return;
2647
2648 ftrace_run_update_code(command);
2649}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002650
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002651static void ftrace_startup_all(int command)
2652{
2653 update_all_ops = true;
2654 ftrace_startup_enable(command);
2655 update_all_ops = false;
2656}
2657
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002658int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002659{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002660 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002661
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002662 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002663 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002664
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002665 ret = __register_ftrace_function(ops);
2666 if (ret)
2667 return ret;
2668
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002669 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002670
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002671 /*
2672 * Note that ftrace probes uses this to start up
2673 * and modify functions it will probe. But we still
2674 * set the ADDING flag for modification, as probes
2675 * do not have trampolines. If they add them in the
2676 * future, then the probes will need to distinguish
2677 * between adding and updating probes.
2678 */
2679 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002680
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002681 ret = ftrace_hash_ipmodify_enable(ops);
2682 if (ret < 0) {
2683 /* Rollback registration process */
2684 __unregister_ftrace_function(ops);
2685 ftrace_start_up--;
2686 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2687 return ret;
2688 }
2689
Jiri Olsa7f50d062016-03-16 15:34:33 +01002690 if (ftrace_hash_rec_enable(ops, 1))
2691 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002692
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002693 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002694
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002695 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2696
Steven Rostedta1cd6172011-05-23 15:24:25 -04002697 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002698}
2699
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002700int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002701{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002702 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002703
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002704 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002705 return -ENODEV;
2706
2707 ret = __unregister_ftrace_function(ops);
2708 if (ret)
2709 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002710
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002711 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002712 /*
2713 * Just warn in case of unbalance, no need to kill ftrace, it's not
2714 * critical but the ftrace_call callers may be never nopped again after
2715 * further ftrace uses.
2716 */
2717 WARN_ON_ONCE(ftrace_start_up < 0);
2718
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002719 /* Disabling ipmodify never fails */
2720 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002721
2722 if (ftrace_hash_rec_disable(ops, 1))
2723 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002724
Namhyung Kima737e6d2014-06-12 23:56:12 +09002725 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002726
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002727 if (saved_ftrace_func != ftrace_trace_function) {
2728 saved_ftrace_func = ftrace_trace_function;
2729 command |= FTRACE_UPDATE_TRACE_FUNC;
2730 }
2731
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002732 if (!command || !ftrace_enabled) {
2733 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002734 * If these are dynamic or per_cpu ops, they still
2735 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002736 * not currently active, we can just free them
2737 * without synchronizing all CPUs.
2738 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002739 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002740 goto free_ops;
2741
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002742 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002743 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002744
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002745 /*
2746 * If the ops uses a trampoline, then it needs to be
2747 * tested first on update.
2748 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002749 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002750 removed_ops = ops;
2751
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002752 /* The trampoline logic checks the old hashes */
2753 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2754 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2755
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002756 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002757
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002758 /*
2759 * If there's no more ops registered with ftrace, run a
2760 * sanity check to make sure all rec flags are cleared.
2761 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002762 if (rcu_dereference_protected(ftrace_ops_list,
2763 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002764 struct ftrace_page *pg;
2765 struct dyn_ftrace *rec;
2766
2767 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002768 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002769 pr_warn(" %pS flags:%lx\n",
2770 (void *)rec->ip, rec->flags);
2771 } while_for_each_ftrace_rec();
2772 }
2773
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002774 ops->old_hash.filter_hash = NULL;
2775 ops->old_hash.notrace_hash = NULL;
2776
2777 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002778 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002779
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002780 /*
2781 * Dynamic ops may be freed, we must make sure that all
2782 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002783 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002784 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002785 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002786 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002787 /*
2788 * We need to do a hard force of sched synchronization.
2789 * This is because we use preempt_disable() to do RCU, but
2790 * the function tracers can be called where RCU is not watching
2791 * (like before user_exit()). We can not rely on the RCU
2792 * infrastructure to do the synchronization, thus we must do it
2793 * ourselves.
2794 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002795 schedule_on_each_cpu(ftrace_sync);
2796
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002797 /*
2798 * When the kernel is preeptive, tasks can be preempted
2799 * while on a ftrace trampoline. Just scheduling a task on
2800 * a CPU is not good enough to flush them. Calling
2801 * synchornize_rcu_tasks() will wait for those tasks to
2802 * execute and either schedule voluntarily or enter user space.
2803 */
2804 if (IS_ENABLED(CONFIG_PREEMPT))
2805 synchronize_rcu_tasks();
2806
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002807 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002808 arch_ftrace_trampoline_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002809 }
2810
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002811 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002812}
2813
Ingo Molnare309b412008-05-12 21:20:51 +02002814static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002815{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302816 int command;
2817
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002818 if (unlikely(ftrace_disabled))
2819 return;
2820
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002821 /* Force update next time */
2822 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002823 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302824 if (ftrace_start_up) {
2825 command = FTRACE_UPDATE_CALLS;
2826 if (ftrace_graph_active)
2827 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002828 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302829 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002830}
2831
Ingo Molnare309b412008-05-12 21:20:51 +02002832static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002833{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302834 int command;
2835
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002836 if (unlikely(ftrace_disabled))
2837 return;
2838
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002839 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302840 if (ftrace_start_up) {
2841 command = FTRACE_DISABLE_CALLS;
2842 if (ftrace_graph_active)
2843 command |= FTRACE_STOP_FUNC_RET;
2844 ftrace_run_update_code(command);
2845 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002846}
2847
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002848static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002849unsigned long ftrace_update_tot_cnt;
2850
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002851static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002852{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002853 /*
2854 * Filter_hash being empty will default to trace module.
2855 * But notrace hash requires a test of individual module functions.
2856 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002857 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2858 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002859}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002860
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002861/*
2862 * Check if the current ops references the record.
2863 *
2864 * If the ops traces all functions, then it was already accounted for.
2865 * If the ops does not trace the current record function, skip it.
2866 * If the ops ignores the function via notrace filter, skip it.
2867 */
2868static inline bool
2869ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2870{
2871 /* If ops isn't enabled, ignore it */
2872 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002873 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002874
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002875 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002876 if (ops_traces_mod(ops))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002877 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002878
2879 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002880 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002881 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002882 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002883
2884 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002885 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002886 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002887
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002888 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002889}
2890
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002891static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002892{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002893 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002894 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002895 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002896 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002897 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002898 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002899
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002900 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002901
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002902 /*
2903 * When a module is loaded, this function is called to convert
2904 * the calls to mcount in its text to nops, and also to create
2905 * an entry in the ftrace data. Now, if ftrace is activated
2906 * after this call, but before the module sets its text to
2907 * read-only, the modification of enabling ftrace can fail if
2908 * the read-only is done while ftrace is converting the calls.
2909 * To prevent this, the module's records are set as disabled
2910 * and will be enabled after the call to set the module's text
2911 * to read-only.
2912 */
2913 if (mod)
2914 rec_flags |= FTRACE_FL_DISABLED;
2915
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002916 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302917
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002918 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002919
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002920 /* If something went wrong, bail without enabling anything */
2921 if (unlikely(ftrace_disabled))
2922 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002923
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002924 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002925 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302926
Vasily Gorbik2f4df002018-08-06 15:17:46 +02002927#ifndef CC_USING_NOP_MCOUNT
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002928 /*
2929 * Do the initial record conversion from mcount jump
2930 * to the NOP instructions.
2931 */
2932 if (!ftrace_code_disable(mod, p))
2933 break;
Vasily Gorbik2f4df002018-08-06 15:17:46 +02002934#endif
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002935
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002936 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002937 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002938 }
2939
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002940 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002941 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002942 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002943
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002944 return 0;
2945}
2946
Steven Rostedta7900872011-12-16 16:23:44 -05002947static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002948{
Steven Rostedta7900872011-12-16 16:23:44 -05002949 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002950 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002951
Steven Rostedta7900872011-12-16 16:23:44 -05002952 if (WARN_ON(!count))
2953 return -EINVAL;
2954
2955 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002956
2957 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002958 * We want to fill as much as possible. No more than a page
2959 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002960 */
Steven Rostedta7900872011-12-16 16:23:44 -05002961 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2962 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002963
Steven Rostedta7900872011-12-16 16:23:44 -05002964 again:
2965 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2966
2967 if (!pg->records) {
2968 /* if we can't allocate this size, try something smaller */
2969 if (!order)
2970 return -ENOMEM;
2971 order >>= 1;
2972 goto again;
2973 }
2974
2975 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2976 pg->size = cnt;
2977
2978 if (cnt > count)
2979 cnt = count;
2980
2981 return cnt;
2982}
2983
2984static struct ftrace_page *
2985ftrace_allocate_pages(unsigned long num_to_init)
2986{
2987 struct ftrace_page *start_pg;
2988 struct ftrace_page *pg;
2989 int order;
2990 int cnt;
2991
2992 if (!num_to_init)
2993 return 0;
2994
2995 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2996 if (!pg)
2997 return NULL;
2998
2999 /*
3000 * Try to allocate as much as possible in one continues
3001 * location that fills in all of the space. We want to
3002 * waste as little space as possible.
3003 */
3004 for (;;) {
3005 cnt = ftrace_allocate_records(pg, num_to_init);
3006 if (cnt < 0)
3007 goto free_pages;
3008
3009 num_to_init -= cnt;
3010 if (!num_to_init)
3011 break;
3012
3013 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3014 if (!pg->next)
3015 goto free_pages;
3016
3017 pg = pg->next;
3018 }
3019
3020 return start_pg;
3021
3022 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003023 pg = start_pg;
3024 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003025 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3026 free_pages((unsigned long)pg->records, order);
3027 start_pg = pg->next;
3028 kfree(pg);
3029 pg = start_pg;
3030 }
3031 pr_info("ftrace: FAILED to allocate memory for functions\n");
3032 return NULL;
3033}
3034
Steven Rostedt5072c592008-05-12 21:20:43 +02003035#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3036
3037struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003038 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003039 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003040 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003041 struct ftrace_page *pg;
3042 struct dyn_ftrace *func;
3043 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003044 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003045 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003046 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003047 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003048 struct trace_array *tr;
3049 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003050 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003051 int idx;
3052 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003053};
3054
Ingo Molnare309b412008-05-12 21:20:51 +02003055static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003056t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003057{
3058 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003059 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003060 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003061 struct ftrace_hash *hash;
3062 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003063 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003064 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003065 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003066
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003067 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003068 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003069
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003070 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003071 return NULL;
3072
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003073 func_probes = &tr->func_probes;
3074 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003075 return NULL;
3076
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003077 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003078 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003079 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003080 }
3081
3082 if (iter->probe_entry)
3083 hnd = &iter->probe_entry->hlist;
3084
3085 hash = iter->probe->ops.func_hash->filter_hash;
3086 size = 1 << hash->size_bits;
3087
3088 retry:
3089 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003090 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003091 return NULL;
3092 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003093 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003094 hash = iter->probe->ops.func_hash->filter_hash;
3095 size = 1 << hash->size_bits;
3096 iter->pidx = 0;
3097 }
3098
3099 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003100
3101 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003102 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003103 hnd = NULL;
3104 goto retry;
3105 }
3106
3107 if (!hnd)
3108 hnd = hhd->first;
3109 else {
3110 hnd = hnd->next;
3111 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003112 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003113 goto retry;
3114 }
3115 }
3116
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003117 if (WARN_ON_ONCE(!hnd))
3118 return NULL;
3119
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003120 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003121
3122 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003123}
3124
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003125static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003126{
3127 struct ftrace_iterator *iter = m->private;
3128 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003129 loff_t l;
3130
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003131 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003132 return NULL;
3133
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003134 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003135 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003136
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003137 iter->probe = NULL;
3138 iter->probe_entry = NULL;
3139 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003140 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003141 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003142 if (!p)
3143 break;
3144 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003145 if (!p)
3146 return NULL;
3147
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003148 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003149 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003150
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003151 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003152}
3153
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003154static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003155t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003156{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003157 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003158 struct ftrace_probe_ops *probe_ops;
3159 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003160
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003161 probe = iter->probe;
3162 probe_entry = iter->probe_entry;
3163
3164 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003165 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003166
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003167 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003168
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003169 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003170 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003171
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003172 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3173 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003174
3175 return 0;
3176}
3177
3178static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003179t_mod_next(struct seq_file *m, loff_t *pos)
3180{
3181 struct ftrace_iterator *iter = m->private;
3182 struct trace_array *tr = iter->tr;
3183
3184 (*pos)++;
3185 iter->pos = *pos;
3186
3187 iter->mod_list = iter->mod_list->next;
3188
3189 if (iter->mod_list == &tr->mod_trace ||
3190 iter->mod_list == &tr->mod_notrace) {
3191 iter->flags &= ~FTRACE_ITER_MOD;
3192 return NULL;
3193 }
3194
3195 iter->mod_pos = *pos;
3196
3197 return iter;
3198}
3199
3200static void *t_mod_start(struct seq_file *m, loff_t *pos)
3201{
3202 struct ftrace_iterator *iter = m->private;
3203 void *p = NULL;
3204 loff_t l;
3205
3206 if (iter->func_pos > *pos)
3207 return NULL;
3208
3209 iter->mod_pos = iter->func_pos;
3210
3211 /* probes are only available if tr is set */
3212 if (!iter->tr)
3213 return NULL;
3214
3215 for (l = 0; l <= (*pos - iter->func_pos); ) {
3216 p = t_mod_next(m, &l);
3217 if (!p)
3218 break;
3219 }
3220 if (!p) {
3221 iter->flags &= ~FTRACE_ITER_MOD;
3222 return t_probe_start(m, pos);
3223 }
3224
3225 /* Only set this if we have an item */
3226 iter->flags |= FTRACE_ITER_MOD;
3227
3228 return iter;
3229}
3230
3231static int
3232t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3233{
3234 struct ftrace_mod_load *ftrace_mod;
3235 struct trace_array *tr = iter->tr;
3236
3237 if (WARN_ON_ONCE(!iter->mod_list) ||
3238 iter->mod_list == &tr->mod_trace ||
3239 iter->mod_list == &tr->mod_notrace)
3240 return -EIO;
3241
3242 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3243
3244 if (ftrace_mod->func)
3245 seq_printf(m, "%s", ftrace_mod->func);
3246 else
3247 seq_putc(m, '*');
3248
3249 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3250
3251 return 0;
3252}
3253
3254static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003255t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003256{
3257 struct ftrace_iterator *iter = m->private;
3258 struct dyn_ftrace *rec = NULL;
3259
3260 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003261
Steven Rostedt5072c592008-05-12 21:20:43 +02003262 retry:
3263 if (iter->idx >= iter->pg->index) {
3264 if (iter->pg->next) {
3265 iter->pg = iter->pg->next;
3266 iter->idx = 0;
3267 goto retry;
3268 }
3269 } else {
3270 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003271 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3272 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003273
3274 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003275 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003276
Steven Rostedt5072c592008-05-12 21:20:43 +02003277 rec = NULL;
3278 goto retry;
3279 }
3280 }
3281
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003282 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003283 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003284
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003285 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003286 iter->func = rec;
3287
3288 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003289}
3290
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003291static void *
3292t_next(struct seq_file *m, void *v, loff_t *pos)
3293{
3294 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003295 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003296 void *ret;
3297
3298 if (unlikely(ftrace_disabled))
3299 return NULL;
3300
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003301 if (iter->flags & FTRACE_ITER_PROBE)
3302 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003303
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003304 if (iter->flags & FTRACE_ITER_MOD)
3305 return t_mod_next(m, pos);
3306
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003307 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003308 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003309 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003310 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003311 }
3312
3313 ret = t_func_next(m, pos);
3314
3315 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003316 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003317
3318 return ret;
3319}
3320
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003321static void reset_iter_read(struct ftrace_iterator *iter)
3322{
3323 iter->pos = 0;
3324 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003325 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003326}
3327
3328static void *t_start(struct seq_file *m, loff_t *pos)
3329{
3330 struct ftrace_iterator *iter = m->private;
3331 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003332 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003333
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003334 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003335
3336 if (unlikely(ftrace_disabled))
3337 return NULL;
3338
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003339 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003340 * If an lseek was done, then reset and start from beginning.
3341 */
3342 if (*pos < iter->pos)
3343 reset_iter_read(iter);
3344
3345 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003346 * For set_ftrace_filter reading, if we have the filter
3347 * off, we can short cut and just print out that all
3348 * functions are enabled.
3349 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003350 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3351 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003352 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003353 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003354 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003355 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003356 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003357 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003358 return iter;
3359 }
3360
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003361 if (iter->flags & FTRACE_ITER_MOD)
3362 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003363
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003364 /*
3365 * Unfortunately, we need to restart at ftrace_pages_start
3366 * every time we let go of the ftrace_mutex. This is because
3367 * those pointers can change without the lock.
3368 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003369 iter->pg = ftrace_pages_start;
3370 iter->idx = 0;
3371 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003372 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003373 if (!p)
3374 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003375 }
walimis5821e1b2008-11-15 15:19:06 +08003376
Steven Rostedt69a30832011-12-19 15:21:16 -05003377 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003378 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003379
3380 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003381}
3382
3383static void t_stop(struct seq_file *m, void *p)
3384{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003385 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003386}
3387
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003388void * __weak
3389arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3390{
3391 return NULL;
3392}
3393
3394static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3395 struct dyn_ftrace *rec)
3396{
3397 void *ptr;
3398
3399 ptr = arch_ftrace_trampoline_func(ops, rec);
3400 if (ptr)
3401 seq_printf(m, " ->%pS", ptr);
3402}
3403
Steven Rostedt5072c592008-05-12 21:20:43 +02003404static int t_show(struct seq_file *m, void *v)
3405{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003406 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003407 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003408
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003409 if (iter->flags & FTRACE_ITER_PROBE)
3410 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003411
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003412 if (iter->flags & FTRACE_ITER_MOD)
3413 return t_mod_show(m, iter);
3414
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003415 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003416 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003417 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003418 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003419 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003420 return 0;
3421 }
3422
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003423 rec = iter->func;
3424
Steven Rostedt5072c592008-05-12 21:20:43 +02003425 if (!rec)
3426 return 0;
3427
Steven Rostedt647bcd02011-05-03 14:39:21 -04003428 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003429 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003430 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003431
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003432 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003433 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003434 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3435 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003436 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003437 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003438 if (ops) {
3439 do {
3440 seq_printf(m, "\ttramp: %pS (%pS)",
3441 (void *)ops->trampoline,
3442 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003443 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003444 ops = ftrace_find_tramp_ops_next(rec, ops);
3445 } while (ops);
3446 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003447 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003448 } else {
3449 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003450 }
3451 }
3452
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003453 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003454
3455 return 0;
3456}
3457
James Morris88e9d342009-09-22 16:43:43 -07003458static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003459 .start = t_start,
3460 .next = t_next,
3461 .stop = t_stop,
3462 .show = t_show,
3463};
3464
Ingo Molnare309b412008-05-12 21:20:51 +02003465static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003466ftrace_avail_open(struct inode *inode, struct file *file)
3467{
3468 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003469
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003470 if (unlikely(ftrace_disabled))
3471 return -ENODEV;
3472
Jiri Olsa50e18b92012-04-25 10:23:39 +02003473 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003474 if (!iter)
3475 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003476
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003477 iter->pg = ftrace_pages_start;
3478 iter->ops = &global_ops;
3479
3480 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003481}
3482
Steven Rostedt647bcd02011-05-03 14:39:21 -04003483static int
3484ftrace_enabled_open(struct inode *inode, struct file *file)
3485{
3486 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003487
Jiri Olsa50e18b92012-04-25 10:23:39 +02003488 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003489 if (!iter)
3490 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003491
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003492 iter->pg = ftrace_pages_start;
3493 iter->flags = FTRACE_ITER_ENABLED;
3494 iter->ops = &global_ops;
3495
3496 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003497}
3498
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003499/**
3500 * ftrace_regex_open - initialize function tracer filter files
3501 * @ops: The ftrace_ops that hold the hash filters
3502 * @flag: The type of filter to process
3503 * @inode: The inode, usually passed in to your open routine
3504 * @file: The file, usually passed in to your open routine
3505 *
3506 * ftrace_regex_open() initializes the filter files for the
3507 * @ops. Depending on @flag it may process the filter hash or
3508 * the notrace hash of @ops. With this called from the open
3509 * routine, you can use ftrace_filter_write() for the write
3510 * routine if @flag has FTRACE_ITER_FILTER set, or
3511 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003512 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003513 * release must call ftrace_regex_release().
3514 */
3515int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003516ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003517 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003518{
3519 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003520 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003521 struct list_head *mod_head;
3522 struct trace_array *tr = ops->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003523 int ret = 0;
3524
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003525 ftrace_ops_init(ops);
3526
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003527 if (unlikely(ftrace_disabled))
3528 return -ENODEV;
3529
Steven Rostedt5072c592008-05-12 21:20:43 +02003530 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3531 if (!iter)
3532 return -ENOMEM;
3533
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003534 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3535 kfree(iter);
3536 return -ENOMEM;
3537 }
3538
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003539 iter->ops = ops;
3540 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003541 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003542
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003543 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003544
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003545 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003546 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003547 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003548 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003549 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003550 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003551 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003552
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003553 iter->mod_list = mod_head;
3554
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003555 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003556 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3557
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003558 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003559 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003560 clear_ftrace_mod_list(mod_head);
3561 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003562 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003563 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003564
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003565 if (!iter->hash) {
3566 trace_parser_put(&iter->parser);
3567 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003568 ret = -ENOMEM;
3569 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003570 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003571 } else
3572 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003573
Steven Rostedt5072c592008-05-12 21:20:43 +02003574 if (file->f_mode & FMODE_READ) {
3575 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003576
3577 ret = seq_open(file, &show_ftrace_seq_ops);
3578 if (!ret) {
3579 struct seq_file *m = file->private_data;
3580 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003581 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003582 /* Failed */
3583 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003584 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003585 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003586 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003587 } else
3588 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003589
3590 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003591 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003592
3593 return ret;
3594}
3595
Steven Rostedt41c52c02008-05-22 11:46:33 -04003596static int
3597ftrace_filter_open(struct inode *inode, struct file *file)
3598{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003599 struct ftrace_ops *ops = inode->i_private;
3600
3601 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003602 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003603 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003604}
3605
3606static int
3607ftrace_notrace_open(struct inode *inode, struct file *file)
3608{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003609 struct ftrace_ops *ops = inode->i_private;
3610
3611 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003612 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003613}
3614
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003615/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3616struct ftrace_glob {
3617 char *search;
3618 unsigned len;
3619 int type;
3620};
3621
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003622/*
3623 * If symbols in an architecture don't correspond exactly to the user-visible
3624 * name of what they represent, it is possible to define this function to
3625 * perform the necessary adjustments.
3626*/
3627char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3628{
3629 return str;
3630}
3631
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003632static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003633{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003634 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003635 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003636
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003637 str = arch_ftrace_match_adjust(str, g->search);
3638
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003639 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003640 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003641 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003642 matched = 1;
3643 break;
3644 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003645 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003646 matched = 1;
3647 break;
3648 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003649 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003650 matched = 1;
3651 break;
3652 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003653 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003654 if (slen >= g->len &&
3655 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003656 matched = 1;
3657 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003658 case MATCH_GLOB:
3659 if (glob_match(g->search, str))
3660 matched = 1;
3661 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003662 }
3663
3664 return matched;
3665}
3666
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003667static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003668enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003669{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003670 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003671 int ret = 0;
3672
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003673 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003674 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003675 /* Do nothing if it doesn't exist */
3676 if (!entry)
3677 return 0;
3678
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003679 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003680 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003681 /* Do nothing if it exists */
3682 if (entry)
3683 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003684
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003685 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003686 }
3687 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003688}
3689
Steven Rostedt64e7c442009-02-13 17:08:48 -05003690static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003691ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3692 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003693{
3694 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003695 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003696
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003697 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3698
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003699 if (mod_g) {
3700 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3701
3702 /* blank module name to match all modules */
3703 if (!mod_g->len) {
3704 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003705 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003706 goto func_match;
3707 return 0;
3708 }
3709
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003710 /*
3711 * exclude_mod is set to trace everything but the given
3712 * module. If it is set and the module matches, then
3713 * return 0. If it is not set, and the module doesn't match
3714 * also return 0. Otherwise, check the function to see if
3715 * that matches.
3716 */
3717 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003718 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003719func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003720 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003721 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003722 return 1;
3723 }
3724
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003725 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003726}
3727
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003728static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003729match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003730{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003731 struct ftrace_page *pg;
3732 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003733 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003734 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3735 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3736 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003737 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003738 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03003739 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003740
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003741 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003742 func_g.type = filter_parse_regex(func, len, &func_g.search,
3743 &clear_filter);
3744 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003745 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003746
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003747 if (mod) {
3748 mod_g.type = filter_parse_regex(mod, strlen(mod),
3749 &mod_g.search, &exclude_mod);
3750 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003751 }
3752
Steven Rostedt52baf112009-02-14 01:15:39 -05003753 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003754
3755 if (unlikely(ftrace_disabled))
3756 goto out_unlock;
3757
Steven Rostedt265c8312009-02-13 12:43:56 -05003758 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003759
3760 if (rec->flags & FTRACE_FL_DISABLED)
3761 continue;
3762
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003763 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003764 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003765 if (ret < 0) {
3766 found = ret;
3767 goto out_unlock;
3768 }
Li Zefan311d16d2009-12-08 11:15:11 +08003769 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003770 }
3771 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003772 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003773 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003774
3775 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003776}
3777
Steven Rostedt64e7c442009-02-13 17:08:48 -05003778static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003779ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003780{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003781 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003782}
3783
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003784static void ftrace_ops_update_code(struct ftrace_ops *ops,
3785 struct ftrace_ops_hash *old_hash)
3786{
3787 struct ftrace_ops *op;
3788
3789 if (!ftrace_enabled)
3790 return;
3791
3792 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3793 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3794 return;
3795 }
3796
3797 /*
3798 * If this is the shared global_ops filter, then we need to
3799 * check if there is another ops that shares it, is enabled.
3800 * If so, we still need to run the modify code.
3801 */
3802 if (ops->func_hash != &global_ops.local_hash)
3803 return;
3804
3805 do_for_each_ftrace_op(op, ftrace_ops_list) {
3806 if (op->func_hash == &global_ops.local_hash &&
3807 op->flags & FTRACE_OPS_FL_ENABLED) {
3808 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3809 /* Only need to do this once */
3810 return;
3811 }
3812 } while_for_each_ftrace_op(op);
3813}
3814
3815static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3816 struct ftrace_hash **orig_hash,
3817 struct ftrace_hash *hash,
3818 int enable)
3819{
3820 struct ftrace_ops_hash old_hash_ops;
3821 struct ftrace_hash *old_hash;
3822 int ret;
3823
3824 old_hash = *orig_hash;
3825 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3826 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3827 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3828 if (!ret) {
3829 ftrace_ops_update_code(ops, &old_hash_ops);
3830 free_ftrace_hash_rcu(old_hash);
3831 }
3832 return ret;
3833}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003834
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003835static bool module_exists(const char *module)
3836{
3837 /* All modules have the symbol __this_module */
3838 const char this_mod[] = "__this_module";
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003839 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003840 unsigned long val;
3841 int n;
3842
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003843 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003844
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003845 if (n > sizeof(modname) - 1)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003846 return false;
3847
3848 val = module_kallsyms_lookup_name(modname);
3849 return val != 0;
3850}
3851
3852static int cache_mod(struct trace_array *tr,
3853 const char *func, char *module, int enable)
3854{
3855 struct ftrace_mod_load *ftrace_mod, *n;
3856 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3857 int ret;
3858
3859 mutex_lock(&ftrace_lock);
3860
3861 /* We do not cache inverse filters */
3862 if (func[0] == '!') {
3863 func++;
3864 ret = -EINVAL;
3865
3866 /* Look to remove this hash */
3867 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3868 if (strcmp(ftrace_mod->module, module) != 0)
3869 continue;
3870
3871 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03003872 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003873 (ftrace_mod->func &&
3874 strcmp(ftrace_mod->func, func) == 0)) {
3875 ret = 0;
3876 free_ftrace_mod(ftrace_mod);
3877 continue;
3878 }
3879 }
3880 goto out;
3881 }
3882
3883 ret = -EINVAL;
3884 /* We only care about modules that have not been loaded yet */
3885 if (module_exists(module))
3886 goto out;
3887
3888 /* Save this string off, and execute it when the module is loaded */
3889 ret = ftrace_add_mod(tr, func, module, enable);
3890 out:
3891 mutex_unlock(&ftrace_lock);
3892
3893 return ret;
3894}
3895
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003896static int
3897ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3898 int reset, int enable);
3899
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003900#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003901static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3902 char *mod, bool enable)
3903{
3904 struct ftrace_mod_load *ftrace_mod, *n;
3905 struct ftrace_hash **orig_hash, *new_hash;
3906 LIST_HEAD(process_mods);
3907 char *func;
3908 int ret;
3909
3910 mutex_lock(&ops->func_hash->regex_lock);
3911
3912 if (enable)
3913 orig_hash = &ops->func_hash->filter_hash;
3914 else
3915 orig_hash = &ops->func_hash->notrace_hash;
3916
3917 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3918 *orig_hash);
3919 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04003920 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003921
3922 mutex_lock(&ftrace_lock);
3923
3924 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3925
3926 if (strcmp(ftrace_mod->module, mod) != 0)
3927 continue;
3928
3929 if (ftrace_mod->func)
3930 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
3931 else
3932 func = kstrdup("*", GFP_KERNEL);
3933
3934 if (!func) /* warn? */
3935 continue;
3936
3937 list_del(&ftrace_mod->list);
3938 list_add(&ftrace_mod->list, &process_mods);
3939
3940 /* Use the newly allocated func, as it may be "*" */
3941 kfree(ftrace_mod->func);
3942 ftrace_mod->func = func;
3943 }
3944
3945 mutex_unlock(&ftrace_lock);
3946
3947 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
3948
3949 func = ftrace_mod->func;
3950
3951 /* Grabs ftrace_lock, which is why we have this extra step */
3952 match_records(new_hash, func, strlen(func), mod);
3953 free_ftrace_mod(ftrace_mod);
3954 }
3955
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04003956 if (enable && list_empty(head))
3957 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
3958
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003959 mutex_lock(&ftrace_lock);
3960
3961 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
3962 new_hash, enable);
3963 mutex_unlock(&ftrace_lock);
3964
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04003965 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003966 mutex_unlock(&ops->func_hash->regex_lock);
3967
3968 free_ftrace_hash(new_hash);
3969}
3970
3971static void process_cached_mods(const char *mod_name)
3972{
3973 struct trace_array *tr;
3974 char *mod;
3975
3976 mod = kstrdup(mod_name, GFP_KERNEL);
3977 if (!mod)
3978 return;
3979
3980 mutex_lock(&trace_types_lock);
3981 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
3982 if (!list_empty(&tr->mod_trace))
3983 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
3984 if (!list_empty(&tr->mod_notrace))
3985 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
3986 }
3987 mutex_unlock(&trace_types_lock);
3988
3989 kfree(mod);
3990}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003991#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003992
Steven Rostedtf6180772009-02-14 00:40:25 -05003993/*
3994 * We register the module command as a template to show others how
3995 * to register the a command as well.
3996 */
3997
3998static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003999ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004000 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004001{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004002 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004003 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004004
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004005 /* match_records() modifies func, and we need the original */
4006 func = kstrdup(func_orig, GFP_KERNEL);
4007 if (!func)
4008 return -ENOMEM;
4009
Steven Rostedtf6180772009-02-14 00:40:25 -05004010 /*
4011 * cmd == 'mod' because we only registered this func
4012 * for the 'mod' ftrace_func_command.
4013 * But if you register one func with multiple commands,
4014 * you can tell which command was used by the cmd
4015 * parameter.
4016 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004017 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004018 kfree(func);
4019
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004020 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004021 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004022 if (ret < 0)
4023 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004024 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004025}
4026
4027static struct ftrace_func_command ftrace_mod_cmd = {
4028 .name = "mod",
4029 .func = ftrace_mod_callback,
4030};
4031
4032static int __init ftrace_mod_cmd_init(void)
4033{
4034 return register_ftrace_command(&ftrace_mod_cmd);
4035}
Steven Rostedt6f415672012-10-05 12:13:07 -04004036core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004037
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004038static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004039 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004040{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004041 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004042 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004043
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004044 probe = container_of(op, struct ftrace_func_probe, ops);
4045 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004046
4047 /*
4048 * Disable preemption for these calls to prevent a RCU grace
4049 * period. This syncs the hash iteration and freeing of items
4050 * on the hash. rcu_read_lock is too dangerous here.
4051 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004052 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004053 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004054 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004055}
4056
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004057struct ftrace_func_map {
4058 struct ftrace_func_entry entry;
4059 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004060};
4061
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004062struct ftrace_func_mapper {
4063 struct ftrace_hash hash;
4064};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004065
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004066/**
4067 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4068 *
4069 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4070 */
4071struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004072{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004073 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004074
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004075 /*
4076 * The mapper is simply a ftrace_hash, but since the entries
4077 * in the hash are not ftrace_func_entry type, we define it
4078 * as a separate structure.
4079 */
4080 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4081 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004082}
4083
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004084/**
4085 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4086 * @mapper: The mapper that has the ip maps
4087 * @ip: the instruction pointer to find the data for
4088 *
4089 * Returns the data mapped to @ip if found otherwise NULL. The return
4090 * is actually the address of the mapper data pointer. The address is
4091 * returned for use cases where the data is no bigger than a long, and
4092 * the user can use the data pointer as its data instead of having to
4093 * allocate more memory for the reference.
4094 */
4095void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4096 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004097{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004098 struct ftrace_func_entry *entry;
4099 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004100
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004101 entry = ftrace_lookup_ip(&mapper->hash, ip);
4102 if (!entry)
4103 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004104
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004105 map = (struct ftrace_func_map *)entry;
4106 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004107}
4108
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004109/**
4110 * ftrace_func_mapper_add_ip - Map some data to an ip
4111 * @mapper: The mapper that has the ip maps
4112 * @ip: The instruction pointer address to map @data to
4113 * @data: The data to map to @ip
4114 *
4115 * Returns 0 on succes otherwise an error.
4116 */
4117int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4118 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004119{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004120 struct ftrace_func_entry *entry;
4121 struct ftrace_func_map *map;
4122
4123 entry = ftrace_lookup_ip(&mapper->hash, ip);
4124 if (entry)
4125 return -EBUSY;
4126
4127 map = kmalloc(sizeof(*map), GFP_KERNEL);
4128 if (!map)
4129 return -ENOMEM;
4130
4131 map->entry.ip = ip;
4132 map->data = data;
4133
4134 __add_hash_entry(&mapper->hash, &map->entry);
4135
4136 return 0;
4137}
4138
4139/**
4140 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4141 * @mapper: The mapper that has the ip maps
4142 * @ip: The instruction pointer address to remove the data from
4143 *
4144 * Returns the data if it is found, otherwise NULL.
4145 * Note, if the data pointer is used as the data itself, (see
4146 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4147 * if the data pointer was set to zero.
4148 */
4149void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4150 unsigned long ip)
4151{
4152 struct ftrace_func_entry *entry;
4153 struct ftrace_func_map *map;
4154 void *data;
4155
4156 entry = ftrace_lookup_ip(&mapper->hash, ip);
4157 if (!entry)
4158 return NULL;
4159
4160 map = (struct ftrace_func_map *)entry;
4161 data = map->data;
4162
4163 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004164 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004165
4166 return data;
4167}
4168
4169/**
4170 * free_ftrace_func_mapper - free a mapping of ips and data
4171 * @mapper: The mapper that has the ip maps
4172 * @free_func: A function to be called on each data item.
4173 *
4174 * This is used to free the function mapper. The @free_func is optional
4175 * and can be used if the data needs to be freed as well.
4176 */
4177void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4178 ftrace_mapper_func free_func)
4179{
4180 struct ftrace_func_entry *entry;
4181 struct ftrace_func_map *map;
4182 struct hlist_head *hhd;
4183 int size = 1 << mapper->hash.size_bits;
4184 int i;
4185
4186 if (free_func && mapper->hash.count) {
4187 for (i = 0; i < size; i++) {
4188 hhd = &mapper->hash.buckets[i];
4189 hlist_for_each_entry(entry, hhd, hlist) {
4190 map = (struct ftrace_func_map *)entry;
4191 free_func(map);
4192 }
4193 }
4194 }
4195 free_ftrace_hash(&mapper->hash);
4196}
4197
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004198static void release_probe(struct ftrace_func_probe *probe)
4199{
4200 struct ftrace_probe_ops *probe_ops;
4201
4202 mutex_lock(&ftrace_lock);
4203
4204 WARN_ON(probe->ref <= 0);
4205
4206 /* Subtract the ref that was used to protect this instance */
4207 probe->ref--;
4208
4209 if (!probe->ref) {
4210 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004211 /*
4212 * Sending zero as ip tells probe_ops to free
4213 * the probe->data itself
4214 */
4215 if (probe_ops->free)
4216 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004217 list_del(&probe->list);
4218 kfree(probe);
4219 }
4220 mutex_unlock(&ftrace_lock);
4221}
4222
4223static void acquire_probe_locked(struct ftrace_func_probe *probe)
4224{
4225 /*
4226 * Add one ref to keep it from being freed when releasing the
4227 * ftrace_lock mutex.
4228 */
4229 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004230}
4231
Steven Rostedt59df055f2009-02-14 15:29:06 -05004232int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004233register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004234 struct ftrace_probe_ops *probe_ops,
4235 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004236{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004237 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004238 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004239 struct ftrace_hash **orig_hash;
4240 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004241 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004242 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004243 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004244 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004245 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004246
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004247 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004248 return -EINVAL;
4249
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004250 /* We do not support '!' for function probes */
4251 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004252 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004253
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004254
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004255 mutex_lock(&ftrace_lock);
4256 /* Check if the probe_ops is already registered */
4257 list_for_each_entry(probe, &tr->func_probes, list) {
4258 if (probe->probe_ops == probe_ops)
4259 break;
4260 }
4261 if (&probe->list == &tr->func_probes) {
4262 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4263 if (!probe) {
4264 mutex_unlock(&ftrace_lock);
4265 return -ENOMEM;
4266 }
4267 probe->probe_ops = probe_ops;
4268 probe->ops.func = function_trace_probe_call;
4269 probe->tr = tr;
4270 ftrace_ops_init(&probe->ops);
4271 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004272 }
4273
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004274 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004275
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004276 mutex_unlock(&ftrace_lock);
4277
4278 mutex_lock(&probe->ops.func_hash->regex_lock);
4279
4280 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004281 old_hash = *orig_hash;
4282 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4283
4284 ret = ftrace_match_records(hash, glob, strlen(glob));
4285
4286 /* Nothing found? */
4287 if (!ret)
4288 ret = -EINVAL;
4289
4290 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004291 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004292
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004293 size = 1 << hash->size_bits;
4294 for (i = 0; i < size; i++) {
4295 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4296 if (ftrace_lookup_ip(old_hash, entry->ip))
4297 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004298 /*
4299 * The caller might want to do something special
4300 * for each function we find. We call the callback
4301 * to give the caller an opportunity to do so.
4302 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004303 if (probe_ops->init) {
4304 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004305 entry->ip, data,
4306 &probe->data);
4307 if (ret < 0) {
4308 if (probe_ops->free && count)
4309 probe_ops->free(probe_ops, tr,
4310 0, probe->data);
4311 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004312 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004313 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004314 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004315 count++;
4316 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004317 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004318
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004319 mutex_lock(&ftrace_lock);
4320
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004321 if (!count) {
4322 /* Nothing was added? */
4323 ret = -EINVAL;
4324 goto out_unlock;
4325 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004326
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004327 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4328 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004329 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004330 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004331
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004332 /* One ref for each new function traced */
4333 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004334
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004335 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4336 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004337
Steven Rostedt59df055f2009-02-14 15:29:06 -05004338 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004339 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004340
4341 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004342 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004343 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004344 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004345 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004346
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004347 release_probe(probe);
4348
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004349 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004350
4351 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004352 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004353 goto out_unlock;
4354
4355 /* Failed to do the move, need to call the free functions */
4356 for (i = 0; i < size; i++) {
4357 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4358 if (ftrace_lookup_ip(old_hash, entry->ip))
4359 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004360 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004361 }
4362 }
4363 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004364}
4365
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004366int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004367unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4368 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004369{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004370 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004371 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004372 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004373 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004374 struct ftrace_hash **orig_hash;
4375 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004376 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004377 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004378 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004379 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004380 int count = 0;
4381 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004382 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004383
Naveen N. Raocbab5672017-05-16 23:21:25 +05304384 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004385 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304386 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004387 int not;
4388
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004389 func_g.type = filter_parse_regex(glob, strlen(glob),
4390 &func_g.search, &not);
4391 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004392
Steven Rostedtb6887d72009-02-17 12:32:04 -05004393 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004394 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004395 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004396 }
4397
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004398 mutex_lock(&ftrace_lock);
4399 /* Check if the probe_ops is already registered */
4400 list_for_each_entry(probe, &tr->func_probes, list) {
4401 if (probe->probe_ops == probe_ops)
4402 break;
4403 }
4404 if (&probe->list == &tr->func_probes)
4405 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004406
4407 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004408 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4409 goto err_unlock_ftrace;
4410
4411 acquire_probe_locked(probe);
4412
4413 mutex_unlock(&ftrace_lock);
4414
4415 mutex_lock(&probe->ops.func_hash->regex_lock);
4416
4417 orig_hash = &probe->ops.func_hash->filter_hash;
4418 old_hash = *orig_hash;
4419
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004420 if (ftrace_hash_empty(old_hash))
4421 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004422
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004423 old_hash_ops.filter_hash = old_hash;
4424 /* Probes only have filters */
4425 old_hash_ops.notrace_hash = NULL;
4426
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004427 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004428 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004429 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004430 goto out_unlock;
4431
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004432 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004433
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004434 size = 1 << hash->size_bits;
4435 for (i = 0; i < size; i++) {
4436 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004437
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004438 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004439 kallsyms_lookup(entry->ip, NULL, NULL,
4440 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004441 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004442 continue;
4443 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004444 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004445 remove_hash_entry(hash, entry);
4446 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004447 }
4448 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004449
4450 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004451 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004452 ret = -EINVAL;
4453 goto out_unlock;
4454 }
4455
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004456 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004457
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004458 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004459
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004460 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004461
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004462 if (ftrace_hash_empty(hash))
4463 ftrace_shutdown(&probe->ops, 0);
4464
4465 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004466 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004467
4468 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004469 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004470 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004471 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004472 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004473
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004474 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4475 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004476 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004477 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004478 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004479 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004480 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004481
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004482 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004483 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004484 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004485
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004486 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004487
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004488 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004489
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004490 err_unlock_ftrace:
4491 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004492 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004493}
4494
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304495void clear_ftrace_function_probes(struct trace_array *tr)
4496{
4497 struct ftrace_func_probe *probe, *n;
4498
4499 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4500 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4501}
4502
Steven Rostedtf6180772009-02-14 00:40:25 -05004503static LIST_HEAD(ftrace_commands);
4504static DEFINE_MUTEX(ftrace_cmd_mutex);
4505
Tom Zanussi38de93a2013-10-24 08:34:18 -05004506/*
4507 * Currently we only register ftrace commands from __init, so mark this
4508 * __init too.
4509 */
4510__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004511{
4512 struct ftrace_func_command *p;
4513 int ret = 0;
4514
4515 mutex_lock(&ftrace_cmd_mutex);
4516 list_for_each_entry(p, &ftrace_commands, list) {
4517 if (strcmp(cmd->name, p->name) == 0) {
4518 ret = -EBUSY;
4519 goto out_unlock;
4520 }
4521 }
4522 list_add(&cmd->list, &ftrace_commands);
4523 out_unlock:
4524 mutex_unlock(&ftrace_cmd_mutex);
4525
4526 return ret;
4527}
4528
Tom Zanussi38de93a2013-10-24 08:34:18 -05004529/*
4530 * Currently we only unregister ftrace commands from __init, so mark
4531 * this __init too.
4532 */
4533__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004534{
4535 struct ftrace_func_command *p, *n;
4536 int ret = -ENODEV;
4537
4538 mutex_lock(&ftrace_cmd_mutex);
4539 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4540 if (strcmp(cmd->name, p->name) == 0) {
4541 ret = 0;
4542 list_del_init(&p->list);
4543 goto out_unlock;
4544 }
4545 }
4546 out_unlock:
4547 mutex_unlock(&ftrace_cmd_mutex);
4548
4549 return ret;
4550}
4551
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004552static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004553 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004554{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004555 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004556 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004557 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004558 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004559 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004560
4561 func = strsep(&next, ":");
4562
4563 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004564 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004565 if (!ret)
4566 ret = -EINVAL;
4567 if (ret < 0)
4568 return ret;
4569 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004570 }
4571
Steven Rostedtf6180772009-02-14 00:40:25 -05004572 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004573
4574 command = strsep(&next, ":");
4575
Steven Rostedtf6180772009-02-14 00:40:25 -05004576 mutex_lock(&ftrace_cmd_mutex);
4577 list_for_each_entry(p, &ftrace_commands, list) {
4578 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004579 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004580 goto out_unlock;
4581 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004582 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004583 out_unlock:
4584 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004585
Steven Rostedtf6180772009-02-14 00:40:25 -05004586 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004587}
4588
Ingo Molnare309b412008-05-12 21:20:51 +02004589static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004590ftrace_regex_write(struct file *file, const char __user *ubuf,
4591 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004592{
4593 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004594 struct trace_parser *parser;
4595 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004596
Li Zefan4ba79782009-09-22 13:52:20 +08004597 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004598 return 0;
4599
Steven Rostedt5072c592008-05-12 21:20:43 +02004600 if (file->f_mode & FMODE_READ) {
4601 struct seq_file *m = file->private_data;
4602 iter = m->private;
4603 } else
4604 iter = file->private_data;
4605
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004606 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004607 return -ENODEV;
4608
4609 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004610
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004611 parser = &iter->parser;
4612 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004613
Li Zefan4ba79782009-09-22 13:52:20 +08004614 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004615 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004616 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004617 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004618 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004619 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004620 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004621 }
4622
Steven Rostedt5072c592008-05-12 21:20:43 +02004623 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004624 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004625 return ret;
4626}
4627
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004628ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004629ftrace_filter_write(struct file *file, const char __user *ubuf,
4630 size_t cnt, loff_t *ppos)
4631{
4632 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4633}
4634
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004635ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004636ftrace_notrace_write(struct file *file, const char __user *ubuf,
4637 size_t cnt, loff_t *ppos)
4638{
4639 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4640}
4641
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004642static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004643ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4644{
4645 struct ftrace_func_entry *entry;
4646
4647 if (!ftrace_location(ip))
4648 return -EINVAL;
4649
4650 if (remove) {
4651 entry = ftrace_lookup_ip(hash, ip);
4652 if (!entry)
4653 return -ENOENT;
4654 free_hash_entry(hash, entry);
4655 return 0;
4656 }
4657
4658 return add_hash_entry(hash, ip);
4659}
4660
4661static int
4662ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4663 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004664{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004665 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004666 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004667 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004668
Steven Rostedt41c52c02008-05-22 11:46:33 -04004669 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004670 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004671
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004672 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004673
Steven Rostedtf45948e2011-05-02 12:29:25 -04004674 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004675 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004676 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004677 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004678
Wang Nanb972cc52014-07-15 08:40:20 +08004679 if (reset)
4680 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4681 else
4682 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4683
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004684 if (!hash) {
4685 ret = -ENOMEM;
4686 goto out_regex_unlock;
4687 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004688
Jiri Olsaac483c42012-01-02 10:04:14 +01004689 if (buf && !ftrace_match_records(hash, buf, len)) {
4690 ret = -EINVAL;
4691 goto out_regex_unlock;
4692 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004693 if (ip) {
4694 ret = ftrace_match_addr(hash, ip, remove);
4695 if (ret < 0)
4696 goto out_regex_unlock;
4697 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004698
4699 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004700 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004701 mutex_unlock(&ftrace_lock);
4702
Jiri Olsaac483c42012-01-02 10:04:14 +01004703 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004704 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004705
4706 free_ftrace_hash(hash);
4707 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004708}
4709
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004710static int
4711ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4712 int reset, int enable)
4713{
4714 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4715}
4716
4717/**
4718 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4719 * @ops - the ops to set the filter with
4720 * @ip - the address to add to or remove from the filter.
4721 * @remove - non zero to remove the ip from the filter
4722 * @reset - non zero to reset all filters before applying this filter.
4723 *
4724 * Filters denote which functions should be enabled when tracing is enabled
4725 * If @ip is NULL, it failes to update filter.
4726 */
4727int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4728 int remove, int reset)
4729{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004730 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004731 return ftrace_set_addr(ops, ip, remove, reset, 1);
4732}
4733EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4734
Joel Fernandesd032ae82016-11-15 12:31:20 -08004735/**
4736 * ftrace_ops_set_global_filter - setup ops to use global filters
4737 * @ops - the ops which will use the global filters
4738 *
4739 * ftrace users who need global function trace filtering should call this.
4740 * It can set the global filter only if ops were not initialized before.
4741 */
4742void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4743{
4744 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4745 return;
4746
4747 ftrace_ops_init(ops);
4748 ops->func_hash = &global_ops.local_hash;
4749}
4750EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4751
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004752static int
4753ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4754 int reset, int enable)
4755{
4756 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4757}
4758
Steven Rostedt77a2b372008-05-12 21:20:45 +02004759/**
4760 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004761 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004762 * @buf - the string that holds the function filter text.
4763 * @len - the length of the string.
4764 * @reset - non zero to reset all filters before applying this filter.
4765 *
4766 * Filters denote which functions should be enabled when tracing is enabled.
4767 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4768 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004769int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004770 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004771{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004772 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004773 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004774}
Steven Rostedt936e0742011-05-05 22:54:01 -04004775EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004776
Steven Rostedt41c52c02008-05-22 11:46:33 -04004777/**
4778 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004779 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004780 * @buf - the string that holds the function notrace text.
4781 * @len - the length of the string.
4782 * @reset - non zero to reset all filters before applying this filter.
4783 *
4784 * Notrace Filters denote which functions should not be enabled when tracing
4785 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4786 * for tracing.
4787 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004788int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004789 int len, int reset)
4790{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004791 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004792 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004793}
4794EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4795/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004796 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004797 * @buf - the string that holds the function filter text.
4798 * @len - the length of the string.
4799 * @reset - non zero to reset all filters before applying this filter.
4800 *
4801 * Filters denote which functions should be enabled when tracing is enabled.
4802 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4803 */
4804void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4805{
4806 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4807}
4808EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4809
4810/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004811 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004812 * @buf - the string that holds the function notrace text.
4813 * @len - the length of the string.
4814 * @reset - non zero to reset all filters before applying this filter.
4815 *
4816 * Notrace Filters denote which functions should not be enabled when tracing
4817 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4818 * for tracing.
4819 */
4820void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004821{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004822 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004823}
Steven Rostedt936e0742011-05-05 22:54:01 -04004824EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004825
Steven Rostedt2af15d62009-05-28 13:37:24 -04004826/*
4827 * command line interface to allow users to set filters on boot up.
4828 */
4829#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4830static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4831static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4832
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004833/* Used by function selftest to not test if filter is set */
4834bool ftrace_filter_param __initdata;
4835
Steven Rostedt2af15d62009-05-28 13:37:24 -04004836static int __init set_ftrace_notrace(char *str)
4837{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004838 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004839 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004840 return 1;
4841}
4842__setup("ftrace_notrace=", set_ftrace_notrace);
4843
4844static int __init set_ftrace_filter(char *str)
4845{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004846 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004847 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004848 return 1;
4849}
4850__setup("ftrace_filter=", set_ftrace_filter);
4851
Stefan Assmann369bc182009-10-12 22:17:21 +02004852#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004853static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004854static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004855static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004856
Stefan Assmann369bc182009-10-12 22:17:21 +02004857static int __init set_graph_function(char *str)
4858{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004859 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004860 return 1;
4861}
4862__setup("ftrace_graph_filter=", set_graph_function);
4863
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004864static int __init set_graph_notrace_function(char *str)
4865{
4866 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4867 return 1;
4868}
4869__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4870
Todd Brandt65a50c652017-03-02 16:12:15 -08004871static int __init set_graph_max_depth_function(char *str)
4872{
4873 if (!str)
4874 return 0;
4875 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4876 return 1;
4877}
4878__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4879
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004880static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004881{
4882 int ret;
4883 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004884 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004885
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004886 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4887 if (WARN_ON(!hash))
4888 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004889
4890 while (buf) {
4891 func = strsep(&buf, ",");
4892 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004893 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004894 if (ret)
4895 printk(KERN_DEBUG "ftrace: function %s not "
4896 "traceable\n", func);
4897 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004898
4899 if (enable)
4900 ftrace_graph_hash = hash;
4901 else
4902 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02004903}
4904#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4905
Steven Rostedt2a85a372011-12-19 21:57:44 -05004906void __init
4907ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004908{
4909 char *func;
4910
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004911 ftrace_ops_init(ops);
4912
Steven Rostedt2af15d62009-05-28 13:37:24 -04004913 while (buf) {
4914 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004915 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004916 }
4917}
4918
4919static void __init set_ftrace_early_filters(void)
4920{
4921 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004922 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004923 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004924 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004925#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4926 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004927 set_ftrace_early_graph(ftrace_graph_buf, 1);
4928 if (ftrace_graph_notrace_buf[0])
4929 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004930#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004931}
4932
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004933int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004934{
4935 struct seq_file *m = (struct seq_file *)file->private_data;
4936 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004937 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004938 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004939 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004940 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004941
Steven Rostedt5072c592008-05-12 21:20:43 +02004942 if (file->f_mode & FMODE_READ) {
4943 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004944 seq_release(inode, file);
4945 } else
4946 iter = file->private_data;
4947
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004948 parser = &iter->parser;
4949 if (trace_parser_loaded(parser)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004950 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004951 }
4952
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004953 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004954
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004955 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004956
Steven Rostedt058e2972011-04-29 22:35:33 -04004957 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004958 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4959
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004960 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004961 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04004962 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004963 iter->hash->flags |= FTRACE_HASH_FL_MOD;
4964 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004965 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004966
Steven Rostedt058e2972011-04-29 22:35:33 -04004967 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004968 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
4969 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04004970 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04004971 } else {
4972 /* For read only, the hash is the ops hash */
4973 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04004974 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004975
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004976 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004977 free_ftrace_hash(iter->hash);
4978 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004979
Steven Rostedt5072c592008-05-12 21:20:43 +02004980 return 0;
4981}
4982
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004983static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004984 .open = ftrace_avail_open,
4985 .read = seq_read,
4986 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004987 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004988};
4989
Steven Rostedt647bcd02011-05-03 14:39:21 -04004990static const struct file_operations ftrace_enabled_fops = {
4991 .open = ftrace_enabled_open,
4992 .read = seq_read,
4993 .llseek = seq_lseek,
4994 .release = seq_release_private,
4995};
4996
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004997static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004998 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004999 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005000 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005001 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005002 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005003};
5004
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005005static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005006 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005007 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005008 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005009 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005010 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005011};
5012
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005013#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5014
5015static DEFINE_MUTEX(graph_lock);
5016
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005017struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5018struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5019
5020enum graph_filter_type {
5021 GRAPH_FILTER_NOTRACE = 0,
5022 GRAPH_FILTER_FUNCTION,
5023};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005024
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005025#define FTRACE_GRAPH_EMPTY ((void *)1)
5026
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005027struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005028 struct ftrace_hash *hash;
5029 struct ftrace_func_entry *entry;
5030 int idx; /* for hash table iteration */
5031 enum graph_filter_type type;
5032 struct ftrace_hash *new_hash;
5033 const struct seq_operations *seq_ops;
5034 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005035};
5036
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005037static void *
Li Zefan85951842009-06-24 09:54:00 +08005038__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005039{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005040 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005041 struct ftrace_func_entry *entry = fgd->entry;
5042 struct hlist_head *head;
5043 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005044
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005045 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005046 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005047
5048 if (entry) {
5049 hlist_for_each_entry_continue(entry, hlist) {
5050 fgd->entry = entry;
5051 return entry;
5052 }
5053
5054 idx++;
5055 }
5056
5057 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5058 head = &fgd->hash->buckets[i];
5059 hlist_for_each_entry(entry, head, hlist) {
5060 fgd->entry = entry;
5061 fgd->idx = i;
5062 return entry;
5063 }
5064 }
5065 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005066}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005067
Li Zefan85951842009-06-24 09:54:00 +08005068static void *
5069g_next(struct seq_file *m, void *v, loff_t *pos)
5070{
5071 (*pos)++;
5072 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005073}
5074
5075static void *g_start(struct seq_file *m, loff_t *pos)
5076{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005077 struct ftrace_graph_data *fgd = m->private;
5078
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005079 mutex_lock(&graph_lock);
5080
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005081 if (fgd->type == GRAPH_FILTER_FUNCTION)
5082 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5083 lockdep_is_held(&graph_lock));
5084 else
5085 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5086 lockdep_is_held(&graph_lock));
5087
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005088 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005089 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005090 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005091
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005092 fgd->idx = 0;
5093 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005094 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005095}
5096
5097static void g_stop(struct seq_file *m, void *p)
5098{
5099 mutex_unlock(&graph_lock);
5100}
5101
5102static int g_show(struct seq_file *m, void *v)
5103{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005104 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005105
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005106 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005107 return 0;
5108
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005109 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005110 struct ftrace_graph_data *fgd = m->private;
5111
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005112 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005113 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005114 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005115 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005116 return 0;
5117 }
5118
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005119 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005120
5121 return 0;
5122}
5123
James Morris88e9d342009-09-22 16:43:43 -07005124static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005125 .start = g_start,
5126 .next = g_next,
5127 .stop = g_stop,
5128 .show = g_show,
5129};
5130
5131static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005132__ftrace_graph_open(struct inode *inode, struct file *file,
5133 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005134{
5135 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005136 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005137
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005138 if (file->f_mode & FMODE_WRITE) {
5139 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5140
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005141 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5142 return -ENOMEM;
5143
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005144 if (file->f_flags & O_TRUNC)
5145 new_hash = alloc_ftrace_hash(size_bits);
5146 else
5147 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5148 fgd->hash);
5149 if (!new_hash) {
5150 ret = -ENOMEM;
5151 goto out;
5152 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005153 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005154
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005155 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005156 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005157 if (!ret) {
5158 struct seq_file *m = file->private_data;
5159 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005160 } else {
5161 /* Failed */
5162 free_ftrace_hash(new_hash);
5163 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005164 }
5165 } else
5166 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005167
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005168out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005169 if (ret < 0 && file->f_mode & FMODE_WRITE)
5170 trace_parser_put(&fgd->parser);
5171
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005172 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005173
5174 /*
5175 * All uses of fgd->hash must be taken with the graph_lock
5176 * held. The graph_lock is going to be released, so force
5177 * fgd->hash to be reinitialized when it is taken again.
5178 */
5179 fgd->hash = NULL;
5180
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005181 return ret;
5182}
5183
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005184static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005185ftrace_graph_open(struct inode *inode, struct file *file)
5186{
5187 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005188 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005189
5190 if (unlikely(ftrace_disabled))
5191 return -ENODEV;
5192
5193 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5194 if (fgd == NULL)
5195 return -ENOMEM;
5196
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005197 mutex_lock(&graph_lock);
5198
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005199 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5200 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005201 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005202 fgd->seq_ops = &ftrace_graph_seq_ops;
5203
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005204 ret = __ftrace_graph_open(inode, file, fgd);
5205 if (ret < 0)
5206 kfree(fgd);
5207
5208 mutex_unlock(&graph_lock);
5209 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005210}
5211
5212static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005213ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5214{
5215 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005216 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005217
5218 if (unlikely(ftrace_disabled))
5219 return -ENODEV;
5220
5221 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5222 if (fgd == NULL)
5223 return -ENOMEM;
5224
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005225 mutex_lock(&graph_lock);
5226
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005227 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5228 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005229 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005230 fgd->seq_ops = &ftrace_graph_seq_ops;
5231
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005232 ret = __ftrace_graph_open(inode, file, fgd);
5233 if (ret < 0)
5234 kfree(fgd);
5235
5236 mutex_unlock(&graph_lock);
5237 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005238}
5239
5240static int
Li Zefan87827112009-07-23 11:29:11 +08005241ftrace_graph_release(struct inode *inode, struct file *file)
5242{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005243 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005244 struct ftrace_hash *old_hash, *new_hash;
5245 struct trace_parser *parser;
5246 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005247
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005248 if (file->f_mode & FMODE_READ) {
5249 struct seq_file *m = file->private_data;
5250
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005251 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005252 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005253 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005254 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005255 }
5256
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005257
5258 if (file->f_mode & FMODE_WRITE) {
5259
5260 parser = &fgd->parser;
5261
5262 if (trace_parser_loaded((parser))) {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005263 ret = ftrace_graph_set_hash(fgd->new_hash,
5264 parser->buffer);
5265 }
5266
5267 trace_parser_put(parser);
5268
5269 new_hash = __ftrace_hash_move(fgd->new_hash);
5270 if (!new_hash) {
5271 ret = -ENOMEM;
5272 goto out;
5273 }
5274
5275 mutex_lock(&graph_lock);
5276
5277 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5278 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5279 lockdep_is_held(&graph_lock));
5280 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5281 } else {
5282 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5283 lockdep_is_held(&graph_lock));
5284 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5285 }
5286
5287 mutex_unlock(&graph_lock);
5288
5289 /* Wait till all users are no longer using the old hash */
5290 synchronize_sched();
5291
5292 free_ftrace_hash(old_hash);
5293 }
5294
5295 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005296 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005297 kfree(fgd);
5298
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005299 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005300}
5301
5302static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005303ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005304{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005305 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005306 struct dyn_ftrace *rec;
5307 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005308 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005309 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005310 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005311
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005312 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005313 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5314 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005315
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005316 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005317
Steven Rostedt52baf112009-02-14 01:15:39 -05005318 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005319
5320 if (unlikely(ftrace_disabled)) {
5321 mutex_unlock(&ftrace_lock);
5322 return -ENODEV;
5323 }
5324
Steven Rostedt265c8312009-02-13 12:43:56 -05005325 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005326
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005327 if (rec->flags & FTRACE_FL_DISABLED)
5328 continue;
5329
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005330 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005331 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005332
5333 if (!not) {
5334 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005335
5336 if (entry)
5337 continue;
5338 if (add_hash_entry(hash, rec->ip) < 0)
5339 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005340 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005341 if (entry) {
5342 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005343 fail = 0;
5344 }
5345 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005346 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005347 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005348out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005349 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005350
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005351 if (fail)
5352 return -EINVAL;
5353
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005354 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005355}
5356
5357static ssize_t
5358ftrace_graph_write(struct file *file, const char __user *ubuf,
5359 size_t cnt, loff_t *ppos)
5360{
Namhyung Kim6a101082013-10-14 17:24:25 +09005361 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005362 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005363 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005364
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005365 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005366 return 0;
5367
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005368 /* Read mode uses seq functions */
5369 if (file->f_mode & FMODE_READ) {
5370 struct seq_file *m = file->private_data;
5371 fgd = m->private;
5372 }
5373
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005374 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005375
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005376 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005377
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005378 if (read >= 0 && trace_parser_loaded(parser) &&
5379 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005380
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005381 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005382 parser->buffer);
5383 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005384 }
5385
Namhyung Kim6a101082013-10-14 17:24:25 +09005386 if (!ret)
5387 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005388
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005389 return ret;
5390}
5391
5392static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005393 .open = ftrace_graph_open,
5394 .read = seq_read,
5395 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005396 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005397 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005398};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005399
5400static const struct file_operations ftrace_graph_notrace_fops = {
5401 .open = ftrace_graph_notrace_open,
5402 .read = seq_read,
5403 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005404 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005405 .release = ftrace_graph_release,
5406};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005407#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5408
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005409void ftrace_create_filter_files(struct ftrace_ops *ops,
5410 struct dentry *parent)
5411{
5412
5413 trace_create_file("set_ftrace_filter", 0644, parent,
5414 ops, &ftrace_filter_fops);
5415
5416 trace_create_file("set_ftrace_notrace", 0644, parent,
5417 ops, &ftrace_notrace_fops);
5418}
5419
5420/*
5421 * The name "destroy_filter_files" is really a misnomer. Although
5422 * in the future, it may actualy delete the files, but this is
5423 * really intended to make sure the ops passed in are disabled
5424 * and that when this function returns, the caller is free to
5425 * free the ops.
5426 *
5427 * The "destroy" name is only to match the "create" name that this
5428 * should be paired with.
5429 */
5430void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5431{
5432 mutex_lock(&ftrace_lock);
5433 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5434 ftrace_shutdown(ops, 0);
5435 ops->flags |= FTRACE_OPS_FL_DELETED;
5436 mutex_unlock(&ftrace_lock);
5437}
5438
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005439static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005440{
Steven Rostedt5072c592008-05-12 21:20:43 +02005441
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005442 trace_create_file("available_filter_functions", 0444,
5443 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005444
Steven Rostedt647bcd02011-05-03 14:39:21 -04005445 trace_create_file("enabled_functions", 0444,
5446 d_tracer, NULL, &ftrace_enabled_fops);
5447
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005448 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005449
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005450#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Chen LinX1ce05002014-09-03 14:31:09 +08005451 trace_create_file("set_graph_function", 0644, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005452 NULL,
5453 &ftrace_graph_fops);
Chen LinX1ce05002014-09-03 14:31:09 +08005454 trace_create_file("set_graph_notrace", 0644, d_tracer,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005455 NULL,
5456 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005457#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5458
Steven Rostedt5072c592008-05-12 21:20:43 +02005459 return 0;
5460}
5461
Steven Rostedt9fd49322012-04-24 22:32:06 -04005462static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005463{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005464 const unsigned long *ipa = a;
5465 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005466
Steven Rostedt9fd49322012-04-24 22:32:06 -04005467 if (*ipa > *ipb)
5468 return 1;
5469 if (*ipa < *ipb)
5470 return -1;
5471 return 0;
5472}
5473
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005474static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005475 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005476 unsigned long *end)
5477{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005478 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005479 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005480 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005481 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005482 unsigned long *p;
5483 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005484 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005485 int ret = -ENOMEM;
5486
5487 count = end - start;
5488
5489 if (!count)
5490 return 0;
5491
Steven Rostedt9fd49322012-04-24 22:32:06 -04005492 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005493 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005494
Steven Rostedt706c81f2012-04-24 23:45:26 -04005495 start_pg = ftrace_allocate_pages(count);
5496 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005497 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005498
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005499 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005500
Steven Rostedt320823092011-12-16 14:42:37 -05005501 /*
5502 * Core and each module needs their own pages, as
5503 * modules will free them when they are removed.
5504 * Force a new page to be allocated for modules.
5505 */
Steven Rostedta7900872011-12-16 16:23:44 -05005506 if (!mod) {
5507 WARN_ON(ftrace_pages || ftrace_pages_start);
5508 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005509 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005510 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005511 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005512 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005513
Steven Rostedta7900872011-12-16 16:23:44 -05005514 if (WARN_ON(ftrace_pages->next)) {
5515 /* Hmm, we have free pages? */
5516 while (ftrace_pages->next)
5517 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005518 }
Steven Rostedta7900872011-12-16 16:23:44 -05005519
Steven Rostedt706c81f2012-04-24 23:45:26 -04005520 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005521 }
5522
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005523 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005524 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005525 while (p < end) {
5526 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005527 /*
5528 * Some architecture linkers will pad between
5529 * the different mcount_loc sections of different
5530 * object files to satisfy alignments.
5531 * Skip any NULL pointers.
5532 */
5533 if (!addr)
5534 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005535
5536 if (pg->index == pg->size) {
5537 /* We should have allocated enough */
5538 if (WARN_ON(!pg->next))
5539 break;
5540 pg = pg->next;
5541 }
5542
5543 rec = &pg->records[pg->index++];
5544 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005545 }
5546
Steven Rostedt706c81f2012-04-24 23:45:26 -04005547 /* We should have used all pages */
5548 WARN_ON(pg->next);
5549
5550 /* Assign the last page to ftrace_pages */
5551 ftrace_pages = pg;
5552
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005553 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005554 * We only need to disable interrupts on start up
5555 * because we are modifying code that an interrupt
5556 * may execute, and the modification is not atomic.
5557 * But for modules, nothing runs the code we modify
5558 * until we are finished with it, and there's no
5559 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005560 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005561 if (!mod)
5562 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005563 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005564 if (!mod)
5565 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005566 ret = 0;
5567 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005568 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005569
Steven Rostedta7900872011-12-16 16:23:44 -05005570 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005571}
5572
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005573struct ftrace_mod_func {
5574 struct list_head list;
5575 char *name;
5576 unsigned long ip;
5577 unsigned int size;
5578};
5579
5580struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005581 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005582 struct list_head list;
5583 struct module *mod;
5584 unsigned long start_addr;
5585 unsigned long end_addr;
5586 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005587 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005588};
5589
Steven Rostedt93eb6772009-04-15 13:24:06 -04005590#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005591
5592#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5593
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005594static LIST_HEAD(ftrace_mod_maps);
5595
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005596static int referenced_filters(struct dyn_ftrace *rec)
5597{
5598 struct ftrace_ops *ops;
5599 int cnt = 0;
5600
5601 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5602 if (ops_references_rec(ops, rec))
5603 cnt++;
5604 }
5605
5606 return cnt;
5607}
5608
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005609static void
5610clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5611{
5612 struct ftrace_func_entry *entry;
5613 struct dyn_ftrace *rec;
5614 int i;
5615
5616 if (ftrace_hash_empty(hash))
5617 return;
5618
5619 for (i = 0; i < pg->index; i++) {
5620 rec = &pg->records[i];
5621 entry = __ftrace_lookup_ip(hash, rec->ip);
5622 /*
5623 * Do not allow this rec to match again.
5624 * Yeah, it may waste some memory, but will be removed
5625 * if/when the hash is modified again.
5626 */
5627 if (entry)
5628 entry->ip = 0;
5629 }
5630}
5631
5632/* Clear any records from hashs */
5633static void clear_mod_from_hashes(struct ftrace_page *pg)
5634{
5635 struct trace_array *tr;
5636
5637 mutex_lock(&trace_types_lock);
5638 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5639 if (!tr->ops || !tr->ops->func_hash)
5640 continue;
5641 mutex_lock(&tr->ops->func_hash->regex_lock);
5642 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5643 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5644 mutex_unlock(&tr->ops->func_hash->regex_lock);
5645 }
5646 mutex_unlock(&trace_types_lock);
5647}
5648
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005649static void ftrace_free_mod_map(struct rcu_head *rcu)
5650{
5651 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5652 struct ftrace_mod_func *mod_func;
5653 struct ftrace_mod_func *n;
5654
5655 /* All the contents of mod_map are now not visible to readers */
5656 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5657 kfree(mod_func->name);
5658 list_del(&mod_func->list);
5659 kfree(mod_func);
5660 }
5661
5662 kfree(mod_map);
5663}
5664
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005665void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005666{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005667 struct ftrace_mod_map *mod_map;
5668 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005669 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005670 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005671 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005672 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005673 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005674
Steven Rostedt93eb6772009-04-15 13:24:06 -04005675 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005676
5677 if (ftrace_disabled)
5678 goto out_unlock;
5679
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005680 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5681 if (mod_map->mod == mod) {
5682 list_del_rcu(&mod_map->list);
5683 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5684 break;
5685 }
5686 }
5687
Steven Rostedt320823092011-12-16 14:42:37 -05005688 /*
5689 * Each module has its own ftrace_pages, remove
5690 * them from the list.
5691 */
5692 last_pg = &ftrace_pages_start;
5693 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5694 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005695 if (within_module_core(rec->ip, mod) ||
5696 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005697 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005698 * As core pages are first, the first
5699 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005700 */
Steven Rostedt320823092011-12-16 14:42:37 -05005701 if (WARN_ON(pg == ftrace_pages_start))
5702 goto out_unlock;
5703
5704 /* Check if we are deleting the last page */
5705 if (pg == ftrace_pages)
5706 ftrace_pages = next_to_ftrace_page(last_pg);
5707
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04005708 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05005709 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005710
5711 pg->next = tmp_page;
5712 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005713 } else
5714 last_pg = &pg->next;
5715 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005716 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005717 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005718
5719 for (pg = tmp_page; pg; pg = tmp_page) {
5720
5721 /* Needs to be called outside of ftrace_lock */
5722 clear_mod_from_hashes(pg);
5723
5724 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5725 free_pages((unsigned long)pg->records, order);
5726 tmp_page = pg->next;
5727 kfree(pg);
5728 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04005729}
5730
Jessica Yu7dcd1822016-02-16 17:32:33 -05005731void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005732{
5733 struct dyn_ftrace *rec;
5734 struct ftrace_page *pg;
5735
5736 mutex_lock(&ftrace_lock);
5737
5738 if (ftrace_disabled)
5739 goto out_unlock;
5740
5741 /*
5742 * If the tracing is enabled, go ahead and enable the record.
5743 *
5744 * The reason not to enable the record immediatelly is the
5745 * inherent check of ftrace_make_nop/ftrace_make_call for
5746 * correct previous instructions. Making first the NOP
5747 * conversion puts the module to the correct state, thus
5748 * passing the ftrace_make_call check.
5749 *
5750 * We also delay this to after the module code already set the
5751 * text to read-only, as we now need to set it back to read-write
5752 * so that we can modify the text.
5753 */
5754 if (ftrace_start_up)
5755 ftrace_arch_code_modify_prepare();
5756
5757 do_for_each_ftrace_rec(pg, rec) {
5758 int cnt;
5759 /*
5760 * do_for_each_ftrace_rec() is a double loop.
5761 * module text shares the pg. If a record is
5762 * not part of this module, then skip this pg,
5763 * which the "break" will do.
5764 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005765 if (!within_module_core(rec->ip, mod) &&
5766 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005767 break;
5768
5769 cnt = 0;
5770
5771 /*
5772 * When adding a module, we need to check if tracers are
5773 * currently enabled and if they are, and can trace this record,
5774 * we need to enable the module functions as well as update the
5775 * reference counts for those function records.
5776 */
5777 if (ftrace_start_up)
5778 cnt += referenced_filters(rec);
5779
5780 /* This clears FTRACE_FL_DISABLED */
5781 rec->flags = cnt;
5782
5783 if (ftrace_start_up && cnt) {
5784 int failed = __ftrace_replace_code(rec, 1);
5785 if (failed) {
5786 ftrace_bug(failed, rec);
5787 goto out_loop;
5788 }
5789 }
5790
5791 } while_for_each_ftrace_rec();
5792
5793 out_loop:
5794 if (ftrace_start_up)
5795 ftrace_arch_code_modify_post_process();
5796
5797 out_unlock:
5798 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04005799
5800 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005801}
5802
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005803void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005804{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005805 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005806 return;
5807
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005808 ftrace_process_locs(mod, mod->ftrace_callsites,
5809 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005810}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005811
5812static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5813 struct dyn_ftrace *rec)
5814{
5815 struct ftrace_mod_func *mod_func;
5816 unsigned long symsize;
5817 unsigned long offset;
5818 char str[KSYM_SYMBOL_LEN];
5819 char *modname;
5820 const char *ret;
5821
5822 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5823 if (!ret)
5824 return;
5825
5826 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5827 if (!mod_func)
5828 return;
5829
5830 mod_func->name = kstrdup(str, GFP_KERNEL);
5831 if (!mod_func->name) {
5832 kfree(mod_func);
5833 return;
5834 }
5835
5836 mod_func->ip = rec->ip - offset;
5837 mod_func->size = symsize;
5838
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005839 mod_map->num_funcs++;
5840
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005841 list_add_rcu(&mod_func->list, &mod_map->funcs);
5842}
5843
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005844static struct ftrace_mod_map *
5845allocate_ftrace_mod_map(struct module *mod,
5846 unsigned long start, unsigned long end)
5847{
5848 struct ftrace_mod_map *mod_map;
5849
5850 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5851 if (!mod_map)
5852 return NULL;
5853
5854 mod_map->mod = mod;
5855 mod_map->start_addr = start;
5856 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005857 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005858
5859 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5860
5861 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5862
5863 return mod_map;
5864}
5865
5866static const char *
5867ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5868 unsigned long addr, unsigned long *size,
5869 unsigned long *off, char *sym)
5870{
5871 struct ftrace_mod_func *found_func = NULL;
5872 struct ftrace_mod_func *mod_func;
5873
5874 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5875 if (addr >= mod_func->ip &&
5876 addr < mod_func->ip + mod_func->size) {
5877 found_func = mod_func;
5878 break;
5879 }
5880 }
5881
5882 if (found_func) {
5883 if (size)
5884 *size = found_func->size;
5885 if (off)
5886 *off = addr - found_func->ip;
5887 if (sym)
5888 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5889
5890 return found_func->name;
5891 }
5892
5893 return NULL;
5894}
5895
5896const char *
5897ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5898 unsigned long *off, char **modname, char *sym)
5899{
5900 struct ftrace_mod_map *mod_map;
5901 const char *ret = NULL;
5902
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005903 /* mod_map is freed via call_rcu_sched() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005904 preempt_disable();
5905 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5906 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
5907 if (ret) {
5908 if (modname)
5909 *modname = mod_map->mod->name;
5910 break;
5911 }
5912 }
5913 preempt_enable();
5914
5915 return ret;
5916}
5917
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005918int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
5919 char *type, char *name,
5920 char *module_name, int *exported)
5921{
5922 struct ftrace_mod_map *mod_map;
5923 struct ftrace_mod_func *mod_func;
5924
5925 preempt_disable();
5926 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5927
5928 if (symnum >= mod_map->num_funcs) {
5929 symnum -= mod_map->num_funcs;
5930 continue;
5931 }
5932
5933 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5934 if (symnum > 1) {
5935 symnum--;
5936 continue;
5937 }
5938
5939 *value = mod_func->ip;
5940 *type = 'T';
5941 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
5942 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
5943 *exported = 1;
5944 preempt_enable();
5945 return 0;
5946 }
5947 WARN_ON(1);
5948 break;
5949 }
5950 preempt_enable();
5951 return -ERANGE;
5952}
5953
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005954#else
5955static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5956 struct dyn_ftrace *rec) { }
5957static inline struct ftrace_mod_map *
5958allocate_ftrace_mod_map(struct module *mod,
5959 unsigned long start, unsigned long end)
5960{
5961 return NULL;
5962}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005963#endif /* CONFIG_MODULES */
5964
Joel Fernandes8715b102017-10-09 12:29:31 -07005965struct ftrace_init_func {
5966 struct list_head list;
5967 unsigned long ip;
5968};
5969
5970/* Clear any init ips from hashes */
5971static void
5972clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005973{
Joel Fernandes8715b102017-10-09 12:29:31 -07005974 struct ftrace_func_entry *entry;
5975
5976 if (ftrace_hash_empty(hash))
5977 return;
5978
5979 entry = __ftrace_lookup_ip(hash, func->ip);
5980
5981 /*
5982 * Do not allow this rec to match again.
5983 * Yeah, it may waste some memory, but will be removed
5984 * if/when the hash is modified again.
5985 */
5986 if (entry)
5987 entry->ip = 0;
5988}
5989
5990static void
5991clear_func_from_hashes(struct ftrace_init_func *func)
5992{
5993 struct trace_array *tr;
5994
5995 mutex_lock(&trace_types_lock);
5996 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5997 if (!tr->ops || !tr->ops->func_hash)
5998 continue;
5999 mutex_lock(&tr->ops->func_hash->regex_lock);
6000 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6001 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6002 mutex_unlock(&tr->ops->func_hash->regex_lock);
6003 }
6004 mutex_unlock(&trace_types_lock);
6005}
6006
6007static void add_to_clear_hash_list(struct list_head *clear_list,
6008 struct dyn_ftrace *rec)
6009{
6010 struct ftrace_init_func *func;
6011
6012 func = kmalloc(sizeof(*func), GFP_KERNEL);
6013 if (!func) {
6014 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6015 return;
6016 }
6017
6018 func->ip = rec->ip;
6019 list_add(&func->list, clear_list);
6020}
6021
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006022void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006023{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006024 unsigned long start = (unsigned long)(start_ptr);
6025 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006026 struct ftrace_page **last_pg = &ftrace_pages_start;
6027 struct ftrace_page *pg;
6028 struct dyn_ftrace *rec;
6029 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006030 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07006031 struct ftrace_init_func *func, *func_next;
6032 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006033 int order;
6034
Joel Fernandes8715b102017-10-09 12:29:31 -07006035 INIT_LIST_HEAD(&clear_hash);
6036
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006037 key.ip = start;
6038 key.flags = end; /* overload flags, as it is unsigned long */
6039
6040 mutex_lock(&ftrace_lock);
6041
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006042 /*
6043 * If we are freeing module init memory, then check if
6044 * any tracer is active. If so, we need to save a mapping of
6045 * the module functions being freed with the address.
6046 */
6047 if (mod && ftrace_ops_list != &ftrace_list_end)
6048 mod_map = allocate_ftrace_mod_map(mod, start, end);
6049
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006050 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6051 if (end < pg->records[0].ip ||
6052 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6053 continue;
6054 again:
6055 rec = bsearch(&key, pg->records, pg->index,
6056 sizeof(struct dyn_ftrace),
6057 ftrace_cmp_recs);
6058 if (!rec)
6059 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006060
Joel Fernandes8715b102017-10-09 12:29:31 -07006061 /* rec will be cleared from hashes after ftrace_lock unlock */
6062 add_to_clear_hash_list(&clear_hash, rec);
6063
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006064 if (mod_map)
6065 save_ftrace_mod_rec(mod_map, rec);
6066
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006067 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04006068 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006069 if (!pg->index) {
6070 *last_pg = pg->next;
6071 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6072 free_pages((unsigned long)pg->records, order);
6073 kfree(pg);
6074 pg = container_of(last_pg, struct ftrace_page, next);
6075 if (!(*last_pg))
6076 ftrace_pages = pg;
6077 continue;
6078 }
6079 memmove(rec, rec + 1,
6080 (pg->index - (rec - pg->records)) * sizeof(*rec));
6081 /* More than one function may be in this block */
6082 goto again;
6083 }
6084 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07006085
6086 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6087 clear_func_from_hashes(func);
6088 kfree(func);
6089 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006090}
6091
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006092void __init ftrace_free_init_mem(void)
6093{
6094 void *start = (void *)(&__init_begin);
6095 void *end = (void *)(&__init_end);
6096
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006097 ftrace_free_mem(NULL, start, end);
Steven Rostedt93eb6772009-04-15 13:24:06 -04006098}
6099
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006100void __init ftrace_init(void)
6101{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006102 extern unsigned long __start_mcount_loc[];
6103 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006104 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006105 int ret;
6106
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006107 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006108 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006109 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01006110 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006111 goto failed;
6112
6113 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006114 if (!count) {
6115 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006116 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006117 }
6118
6119 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6120 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006121
6122 last_ftrace_enabled = ftrace_enabled = 1;
6123
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006124 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08006125 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006126 __stop_mcount_loc);
6127
Steven Rostedt2af15d62009-05-28 13:37:24 -04006128 set_ftrace_early_filters();
6129
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006130 return;
6131 failed:
6132 ftrace_disabled = 1;
6133}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006134
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006135/* Do nothing if arch does not support this */
6136void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6137{
6138}
6139
6140static void ftrace_update_trampoline(struct ftrace_ops *ops)
6141{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006142 arch_ftrace_update_trampoline(ops);
6143}
6144
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006145void ftrace_init_trace_array(struct trace_array *tr)
6146{
6147 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04006148 INIT_LIST_HEAD(&tr->mod_trace);
6149 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006150}
Steven Rostedt3d083392008-05-12 21:20:42 +02006151#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006152
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05006153struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04006154 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006155 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6156 FTRACE_OPS_FL_INITIALIZED |
6157 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04006158};
6159
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006160static int __init ftrace_nodyn_init(void)
6161{
6162 ftrace_enabled = 1;
6163 return 0;
6164}
Steven Rostedt6f415672012-10-05 12:13:07 -04006165core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006166
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006167static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006168static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006169static inline void ftrace_startup_all(int command) { }
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006170
Ingo Molnarc7aafc52008-05-12 21:20:45 +02006171# define ftrace_startup_sysctl() do { } while (0)
6172# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04006173
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006174static void ftrace_update_trampoline(struct ftrace_ops *ops)
6175{
6176}
6177
Steven Rostedt3d083392008-05-12 21:20:42 +02006178#endif /* CONFIG_DYNAMIC_FTRACE */
6179
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006180__init void ftrace_init_global_array_ops(struct trace_array *tr)
6181{
6182 tr->ops = &global_ops;
6183 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006184 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006185}
6186
6187void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6188{
6189 /* If we filter on pids, update to use the pid function */
6190 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6191 if (WARN_ON(tr->ops->func != ftrace_stub))
6192 printk("ftrace ops had %pS for function\n",
6193 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006194 }
6195 tr->ops->func = func;
6196 tr->ops->private = tr;
6197}
6198
6199void ftrace_reset_array_ops(struct trace_array *tr)
6200{
6201 tr->ops->func = ftrace_stub;
6202}
6203
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006204static inline void
6205__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006206 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006207{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006208 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006209 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04006210
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006211 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6212 if (bit < 0)
6213 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04006214
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006215 /*
6216 * Some of the ops may be dynamically allocated,
6217 * they must be freed after a synchronize_sched().
6218 */
6219 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006220
Steven Rostedt0a016402012-11-02 17:03:03 -04006221 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006222 /*
6223 * Check the following for each ops before calling their func:
6224 * if RCU flag is set, then rcu_is_watching() must be true
6225 * if PER_CPU is set, then ftrace_function_local_disable()
6226 * must be false
6227 * Otherwise test if the ip matches the ops filter
6228 *
6229 * If any of the above fails then the op->func() is not executed.
6230 */
6231 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006232 ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04006233 if (FTRACE_WARN_ON(!op->func)) {
6234 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006235 goto out;
6236 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04006237 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006238 }
Steven Rostedt0a016402012-11-02 17:03:03 -04006239 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006240out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006241 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006242 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04006243}
6244
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006245/*
6246 * Some archs only support passing ip and parent_ip. Even though
6247 * the list function ignores the op parameter, we do not want any
6248 * C side effects, where a function is called without the caller
6249 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006250 * Archs are to support both the regs and ftrace_ops at the same time.
6251 * If they support ftrace_ops, it is assumed they support regs.
6252 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09006253 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6254 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006255 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08006256 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006257 */
6258#if ARCH_SUPPORTS_FTRACE_OPS
6259static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006260 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006261{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006262 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006263}
6264#else
6265static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6266{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006267 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006268}
6269#endif
6270
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006271/*
6272 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006273 * recursion, needs RCU protection and/or requires per cpu handling, then
6274 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006275 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006276static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006277 struct ftrace_ops *op, struct pt_regs *regs)
6278{
6279 int bit;
6280
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006281 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6282 return;
6283
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006284 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6285 if (bit < 0)
6286 return;
6287
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006288 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006289
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006290 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006291
6292 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006293 trace_clear_recursion(bit);
6294}
6295
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006296/**
6297 * ftrace_ops_get_func - get the function a trampoline should call
6298 * @ops: the ops to get the function for
6299 *
6300 * Normally the mcount trampoline will call the ops->func, but there
6301 * are times that it should not. For example, if the ops does not
6302 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08006303 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006304 *
6305 * Returns the function that the trampoline should call for @ops.
6306 */
6307ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6308{
6309 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006310 * If the function does not handle recursion, needs to be RCU safe,
6311 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006312 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006313 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006314 ops->flags & FTRACE_OPS_FL_RCU)
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006315 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006316
6317 return ops->func;
6318}
6319
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006320static void
6321ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6322 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05006323{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006324 struct trace_array *tr = data;
6325 struct trace_pid_list *pid_list;
6326
6327 pid_list = rcu_dereference_sched(tr->function_pids);
6328
6329 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6330 trace_ignore_this_task(pid_list, next));
6331}
6332
Namhyung Kim1e104862017-04-17 11:44:28 +09006333static void
6334ftrace_pid_follow_sched_process_fork(void *data,
6335 struct task_struct *self,
6336 struct task_struct *task)
6337{
6338 struct trace_pid_list *pid_list;
6339 struct trace_array *tr = data;
6340
6341 pid_list = rcu_dereference_sched(tr->function_pids);
6342 trace_filter_add_remove_task(pid_list, self, task);
6343}
6344
6345static void
6346ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6347{
6348 struct trace_pid_list *pid_list;
6349 struct trace_array *tr = data;
6350
6351 pid_list = rcu_dereference_sched(tr->function_pids);
6352 trace_filter_add_remove_task(pid_list, NULL, task);
6353}
6354
6355void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6356{
6357 if (enable) {
6358 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6359 tr);
6360 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6361 tr);
6362 } else {
6363 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6364 tr);
6365 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6366 tr);
6367 }
6368}
6369
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006370static void clear_ftrace_pids(struct trace_array *tr)
6371{
6372 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05006373 int cpu;
6374
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006375 pid_list = rcu_dereference_protected(tr->function_pids,
6376 lockdep_is_held(&ftrace_lock));
6377 if (!pid_list)
6378 return;
6379
6380 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6381
6382 for_each_possible_cpu(cpu)
6383 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6384
6385 rcu_assign_pointer(tr->function_pids, NULL);
6386
6387 /* Wait till all users are no longer using pid filtering */
6388 synchronize_sched();
6389
6390 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05006391}
6392
Namhyung Kimd879d0b2017-04-17 11:44:27 +09006393void ftrace_clear_pids(struct trace_array *tr)
6394{
6395 mutex_lock(&ftrace_lock);
6396
6397 clear_ftrace_pids(tr);
6398
6399 mutex_unlock(&ftrace_lock);
6400}
6401
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006402static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05006403{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006404 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006405 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006406
6407 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006408 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006409
6410 mutex_unlock(&ftrace_lock);
6411}
6412
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006413/* Greater than any max PID */
6414#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6415
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006416static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006417 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006418{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006419 struct trace_pid_list *pid_list;
6420 struct trace_array *tr = m->private;
6421
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006422 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006423 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006424
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006425 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006426
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006427 if (!pid_list)
6428 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6429
6430 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006431}
6432
6433static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6434{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006435 struct trace_array *tr = m->private;
6436 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6437
6438 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006439 return NULL;
6440
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006441 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006442}
6443
6444static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006445 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006446{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006447 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006448 mutex_unlock(&ftrace_lock);
6449}
6450
6451static int fpid_show(struct seq_file *m, void *v)
6452{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006453 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006454 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006455 return 0;
6456 }
6457
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006458 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006459}
6460
6461static const struct seq_operations ftrace_pid_sops = {
6462 .start = fpid_start,
6463 .next = fpid_next,
6464 .stop = fpid_stop,
6465 .show = fpid_show,
6466};
6467
6468static int
6469ftrace_pid_open(struct inode *inode, struct file *file)
6470{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006471 struct trace_array *tr = inode->i_private;
6472 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006473 int ret = 0;
6474
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006475 if (trace_array_get(tr) < 0)
6476 return -ENODEV;
6477
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006478 if ((file->f_mode & FMODE_WRITE) &&
6479 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006480 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006481
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006482 ret = seq_open(file, &ftrace_pid_sops);
6483 if (ret < 0) {
6484 trace_array_put(tr);
6485 } else {
6486 m = file->private_data;
6487 /* copy tr over to seq ops */
6488 m->private = tr;
6489 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006490
6491 return ret;
6492}
6493
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006494static void ignore_task_cpu(void *data)
6495{
6496 struct trace_array *tr = data;
6497 struct trace_pid_list *pid_list;
6498
6499 /*
6500 * This function is called by on_each_cpu() while the
6501 * event_mutex is held.
6502 */
6503 pid_list = rcu_dereference_protected(tr->function_pids,
6504 mutex_is_locked(&ftrace_lock));
6505
6506 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6507 trace_ignore_this_task(pid_list, current));
6508}
6509
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006510static ssize_t
6511ftrace_pid_write(struct file *filp, const char __user *ubuf,
6512 size_t cnt, loff_t *ppos)
6513{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006514 struct seq_file *m = filp->private_data;
6515 struct trace_array *tr = m->private;
6516 struct trace_pid_list *filtered_pids = NULL;
6517 struct trace_pid_list *pid_list;
6518 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006519
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006520 if (!cnt)
6521 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006522
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006523 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006524
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006525 filtered_pids = rcu_dereference_protected(tr->function_pids,
6526 lockdep_is_held(&ftrace_lock));
6527
6528 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6529 if (ret < 0)
6530 goto out;
6531
6532 rcu_assign_pointer(tr->function_pids, pid_list);
6533
6534 if (filtered_pids) {
6535 synchronize_sched();
6536 trace_free_pid_list(filtered_pids);
6537 } else if (pid_list) {
6538 /* Register a probe to set whether to ignore the tracing of a task */
6539 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6540 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006541
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006542 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006543 * Ignoring of pids is done at task switch. But we have to
6544 * check for those tasks that are currently running.
6545 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006546 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006547 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006548
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006549 ftrace_update_pid_func();
6550 ftrace_startup_all(0);
6551 out:
6552 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006553
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006554 if (ret > 0)
6555 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05006556
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006557 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006558}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006559
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006560static int
6561ftrace_pid_release(struct inode *inode, struct file *file)
6562{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006563 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006564
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006565 trace_array_put(tr);
6566
6567 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006568}
6569
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006570static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006571 .open = ftrace_pid_open,
6572 .write = ftrace_pid_write,
6573 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006574 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006575 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006576};
6577
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006578void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006579{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006580 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006581 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006582}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006583
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04006584void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6585 struct dentry *d_tracer)
6586{
6587 /* Only the top level directory has the dyn_tracefs and profile */
6588 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6589
6590 ftrace_init_dyn_tracefs(d_tracer);
6591 ftrace_profile_tracefs(d_tracer);
6592}
6593
Steven Rostedt3d083392008-05-12 21:20:42 +02006594/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006595 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006596 *
6597 * This function should be used by panic code. It stops ftrace
6598 * but in a not so nice way. If you need to simply kill ftrace
6599 * from a non-atomic section, use ftrace_kill.
6600 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006601void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006602{
6603 ftrace_disabled = 1;
6604 ftrace_enabled = 0;
Yisheng Xie5ccba642018-02-02 10:14:49 +08006605 ftrace_trace_function = ftrace_stub;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006606}
6607
6608/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04006609 * Test if ftrace is dead or not.
6610 */
6611int ftrace_is_dead(void)
6612{
6613 return ftrace_disabled;
6614}
6615
6616/**
Steven Rostedt3d083392008-05-12 21:20:42 +02006617 * register_ftrace_function - register a function for profiling
6618 * @ops - ops structure that holds the function for profiling.
6619 *
6620 * Register a function to be called by all functions in the
6621 * kernel.
6622 *
6623 * Note: @ops->func and all the functions it calls must be labeled
6624 * with "notrace", otherwise it will go into a
6625 * recursive loop.
6626 */
6627int register_ftrace_function(struct ftrace_ops *ops)
6628{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006629 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006630
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09006631 ftrace_ops_init(ops);
6632
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006633 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006634
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006635 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04006636
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006637 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02006638
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006639 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02006640}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006641EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02006642
6643/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01006644 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02006645 * @ops - ops structure that holds the function to unregister
6646 *
6647 * Unregister a function that was added to be called by ftrace profiling.
6648 */
6649int unregister_ftrace_function(struct ftrace_ops *ops)
6650{
6651 int ret;
6652
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006653 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006654 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006655 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006656
6657 return ret;
6658}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006659EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006660
Ingo Molnare309b412008-05-12 21:20:51 +02006661int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006662ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006663 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006664 loff_t *ppos)
6665{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006666 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006667
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006668 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006669
Steven Rostedt45a4a232011-04-21 23:16:46 -04006670 if (unlikely(ftrace_disabled))
6671 goto out;
6672
6673 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006674
Li Zefana32c7762009-06-26 16:55:51 +08006675 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006676 goto out;
6677
Li Zefana32c7762009-06-26 16:55:51 +08006678 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006679
6680 if (ftrace_enabled) {
6681
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006682 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08006683 if (rcu_dereference_protected(ftrace_ops_list,
6684 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01006685 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006686
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006687 ftrace_startup_sysctl();
6688
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006689 } else {
6690 /* stopping ftrace calls (just send to ftrace_stub) */
6691 ftrace_trace_function = ftrace_stub;
6692
6693 ftrace_shutdown_sysctl();
6694 }
6695
6696 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006697 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006698 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006699}
Ingo Molnarf17845e2008-10-24 12:47:10 +02006700
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006701#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006702
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006703static struct ftrace_ops graph_ops = {
6704 .func = ftrace_stub,
6705 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6706 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006707 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006708 FTRACE_OPS_FL_STUB,
6709#ifdef FTRACE_GRAPH_TRAMP_ADDR
6710 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05006711 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006712#endif
6713 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6714};
6715
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006716void ftrace_graph_sleep_time_control(bool enable)
6717{
6718 fgraph_sleep_time = enable;
6719}
6720
6721void ftrace_graph_graph_time_control(bool enable)
6722{
6723 fgraph_graph_time = enable;
6724}
6725
Steven Rostedte49dc192008-12-02 23:50:05 -05006726int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6727{
6728 return 0;
6729}
6730
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006731/* The callbacks that hook a function */
6732trace_func_graph_ret_t ftrace_graph_return =
6733 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006734trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006735static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006736
6737/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6738static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6739{
6740 int i;
6741 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006742 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6743 struct task_struct *g, *t;
6744
6745 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
Kees Cook6da2ec52018-06-12 13:55:00 -07006746 ret_stack_list[i] =
6747 kmalloc_array(FTRACE_RETFUNC_DEPTH,
6748 sizeof(struct ftrace_ret_stack),
6749 GFP_KERNEL);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006750 if (!ret_stack_list[i]) {
6751 start = 0;
6752 end = i;
6753 ret = -ENOMEM;
6754 goto free;
6755 }
6756 }
6757
Soumya PN6112a302016-05-17 21:31:14 +05306758 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006759 do_each_thread(g, t) {
6760 if (start == end) {
6761 ret = -EAGAIN;
6762 goto unlock;
6763 }
6764
6765 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01006766 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006767 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04006768 t->curr_ret_stack = -1;
Steven Rostedt (VMware)39eb4562018-11-19 08:07:12 -05006769 t->curr_ret_depth = -1;
Steven Rostedt26c01622009-06-02 14:01:19 -04006770 /* Make sure the tasks see the -1 first: */
6771 smp_wmb();
6772 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006773 }
6774 } while_each_thread(g, t);
6775
6776unlock:
Soumya PN6112a302016-05-17 21:31:14 +05306777 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006778free:
6779 for (i = start; i < end; i++)
6780 kfree(ret_stack_list[i]);
6781 return ret;
6782}
6783
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006784static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02006785ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04006786 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006787{
6788 unsigned long long timestamp;
6789 int index;
6790
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006791 /*
6792 * Does the user want to count the time a function was asleep.
6793 * If so, do not update the time stamps.
6794 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006795 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006796 return;
6797
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006798 timestamp = trace_clock_local();
6799
6800 prev->ftrace_timestamp = timestamp;
6801
6802 /* only process tasks that we timestamped */
6803 if (!next->ftrace_timestamp)
6804 return;
6805
6806 /*
6807 * Update all the counters in next to make up for the
6808 * time next was sleeping.
6809 */
6810 timestamp -= next->ftrace_timestamp;
6811
6812 for (index = next->curr_ret_stack; index >= 0; index--)
6813 next->ret_stack[index].calltime += timestamp;
6814}
6815
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006816/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006817static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006818{
6819 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006820 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006821
Kees Cook6da2ec52018-06-12 13:55:00 -07006822 ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
6823 sizeof(struct ftrace_ret_stack *),
6824 GFP_KERNEL);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006825
6826 if (!ret_stack_list)
6827 return -ENOMEM;
6828
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006829 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04006830 for_each_online_cpu(cpu) {
6831 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05006832 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04006833 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006834
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006835 do {
6836 ret = alloc_retstack_tasklist(ret_stack_list);
6837 } while (ret == -EAGAIN);
6838
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006839 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04006840 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006841 if (ret)
6842 pr_info("ftrace_graph: Couldn't activate tracepoint"
6843 " probe to kernel_sched_switch\n");
6844 }
6845
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006846 kfree(ret_stack_list);
6847 return ret;
6848}
6849
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006850/*
6851 * Hibernation protection.
6852 * The state of the current task is too much unstable during
6853 * suspend/restore to disk. We want to protect against that.
6854 */
6855static int
6856ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6857 void *unused)
6858{
6859 switch (state) {
6860 case PM_HIBERNATION_PREPARE:
6861 pause_graph_tracing();
6862 break;
6863
6864 case PM_POST_HIBERNATION:
6865 unpause_graph_tracing();
6866 break;
6867 }
6868 return NOTIFY_DONE;
6869}
6870
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006871static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6872{
6873 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6874 return 0;
6875 return __ftrace_graph_entry(trace);
6876}
6877
6878/*
6879 * The function graph tracer should only trace the functions defined
6880 * by set_ftrace_filter and set_ftrace_notrace. If another function
6881 * tracer ops is registered, the graph tracer requires testing the
6882 * function against the global ops, and not just trace any function
6883 * that any ftrace_ops registered.
6884 */
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05006885void update_function_graph_func(void)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006886{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006887 struct ftrace_ops *op;
6888 bool do_test = false;
6889
6890 /*
6891 * The graph and global ops share the same set of functions
6892 * to test. If any other ops is on the list, then
6893 * the graph tracing needs to test if its the function
6894 * it should call.
6895 */
6896 do_for_each_ftrace_op(op, ftrace_ops_list) {
6897 if (op != &global_ops && op != &graph_ops &&
6898 op != &ftrace_list_end) {
6899 do_test = true;
6900 /* in double loop, break out with goto */
6901 goto out;
6902 }
6903 } while_for_each_ftrace_op(op);
6904 out:
6905 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006906 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006907 else
6908 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006909}
6910
Mathias Krause8275f692014-03-30 15:31:50 +02006911static struct notifier_block ftrace_suspend_notifier = {
6912 .notifier_call = ftrace_suspend_notifier_call,
6913};
6914
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006915int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6916 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006917{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006918 int ret = 0;
6919
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006920 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006921
Steven Rostedt05ce5812009-03-24 00:18:31 -04006922 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04006923 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04006924 ret = -EBUSY;
6925 goto out;
6926 }
6927
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006928 register_pm_notifier(&ftrace_suspend_notifier);
6929
Steven Rostedt597af812009-04-03 15:24:12 -04006930 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006931 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006932 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04006933 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006934 goto out;
6935 }
Steven Rostedte53a6312008-11-26 00:16:25 -05006936
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006937 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006938
6939 /*
6940 * Update the indirect function to the entryfunc, and the
6941 * function that gets called to the entry_test first. Then
6942 * call the update fgraph entry function to determine if
6943 * the entryfunc should be called directly or not.
6944 */
6945 __ftrace_graph_entry = entryfunc;
6946 ftrace_graph_entry = ftrace_graph_entry_test;
6947 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05006948
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006949 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006950out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006951 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006952 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006953}
6954
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006955void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006956{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006957 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006958
Steven Rostedt597af812009-04-03 15:24:12 -04006959 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006960 goto out;
6961
Steven Rostedt597af812009-04-03 15:24:12 -04006962 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006963 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006964 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006965 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006966 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006967 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04006968 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006969
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006970 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006971 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006972}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006973
Steven Rostedt868baf02011-02-10 21:26:13 -05006974static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6975
6976static void
6977graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6978{
6979 atomic_set(&t->tracing_graph_pause, 0);
6980 atomic_set(&t->trace_overrun, 0);
6981 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006982 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05006983 smp_wmb();
6984 t->ret_stack = ret_stack;
6985}
6986
6987/*
6988 * Allocate a return stack for the idle task. May be the first
6989 * time through, or it may be done by CPU hotplug online.
6990 */
6991void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6992{
6993 t->curr_ret_stack = -1;
Steven Rostedt (VMware)39eb4562018-11-19 08:07:12 -05006994 t->curr_ret_depth = -1;
Steven Rostedt868baf02011-02-10 21:26:13 -05006995 /*
6996 * The idle task has no parent, it either has its own
6997 * stack or no stack at all.
6998 */
6999 if (t->ret_stack)
7000 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7001
7002 if (ftrace_graph_active) {
7003 struct ftrace_ret_stack *ret_stack;
7004
7005 ret_stack = per_cpu(idle_ret_stack, cpu);
7006 if (!ret_stack) {
Kees Cook6da2ec52018-06-12 13:55:00 -07007007 ret_stack =
7008 kmalloc_array(FTRACE_RETFUNC_DEPTH,
7009 sizeof(struct ftrace_ret_stack),
7010 GFP_KERNEL);
Steven Rostedt868baf02011-02-10 21:26:13 -05007011 if (!ret_stack)
7012 return;
7013 per_cpu(idle_ret_stack, cpu) = ret_stack;
7014 }
7015 graph_init_task(t, ret_stack);
7016 }
7017}
7018
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007019/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01007020void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007021{
Steven Rostedt84047e32009-06-02 16:51:55 -04007022 /* Make sure we do not use the parent ret_stack */
7023 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05007024 t->curr_ret_stack = -1;
Steven Rostedt (VMware)39eb4562018-11-19 08:07:12 -05007025 t->curr_ret_depth = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04007026
Steven Rostedt597af812009-04-03 15:24:12 -04007027 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04007028 struct ftrace_ret_stack *ret_stack;
7029
Kees Cook6da2ec52018-06-12 13:55:00 -07007030 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
7031 sizeof(struct ftrace_ret_stack),
7032 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04007033 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007034 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05007035 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04007036 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007037}
7038
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01007039void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007040{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01007041 struct ftrace_ret_stack *ret_stack = t->ret_stack;
7042
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007043 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01007044 /* NULL must become visible to IRQs before we free it: */
7045 barrier();
7046
7047 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01007048}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01007049#endif