blob: b0e7f03919de9f42d6485b10780b625d525ec796 [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 Rostedt (VMware)17911ff2019-10-11 17:22:50 -040021#include <linux/security.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020022#include <linux/seq_file.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>
Masami Hiramatsufabe38a2019-02-24 01:50:20 +090037#include <linux/kprobes.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020038
Steven Rostedtad8d75f2009-04-14 19:39:12 -040039#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040040
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -040041#include <asm/sections.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040042#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053043
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050044#include "ftrace_internal.h"
Steven Rostedt0706f1c2009-03-23 23:12:58 -040045#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040046#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020047
Steven Rostedt6912896e2008-10-23 09:33:03 -040048#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040049 ({ \
50 int ___r = cond; \
51 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040052 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040053 ___r; \
54 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040055
56#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040057 ({ \
58 int ___r = cond; \
59 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040060 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040061 ___r; \
62 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040063
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064/* hash bits for specific function selection */
65#define FTRACE_HASH_BITS 7
66#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040067#define FTRACE_HASH_DEFAULT_BITS 10
68#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050069
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090070#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040071#define INIT_OPS_HASH(opsname) \
72 .func_hash = &opsname.local_hash, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090074#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040075#define INIT_OPS_HASH(opsname)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090076#endif
77
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -050078enum {
79 FTRACE_MODIFY_ENABLE_FL = (1 << 0),
80 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
81};
82
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050083struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040085 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040086 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040087};
88
Steven Rostedt4eebcc82008-05-12 21:20:48 +020089/* ftrace_enabled is a method to turn ftrace on or off */
90int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020091static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020092
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040093/* Current function tracing op */
94struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050095/* What to set function_trace_op to */
96static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050097
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040098static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040099{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400100 struct trace_array *tr;
101
102 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
103 return false;
104
105 tr = ops->private;
106
107 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400108}
109
110static void ftrace_update_trampoline(struct ftrace_ops *ops);
111
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200112/*
113 * ftrace_disabled is set when an anomaly is discovered.
114 * ftrace_disabled is much stronger than ftrace_enabled.
115 */
116static int ftrace_disabled __read_mostly;
117
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500118DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200119
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500120struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200121ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500122struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200123
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400124#if ARCH_SUPPORTS_FTRACE_OPS
125static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400126 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400127#else
128/* See comment below, where ftrace_ops_list_func is defined */
129static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
130#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
131#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400132
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900133static inline void ftrace_ops_init(struct ftrace_ops *ops)
134{
135#ifdef CONFIG_DYNAMIC_FTRACE
136 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400137 mutex_init(&ops->local_hash.regex_lock);
138 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900139 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
140 }
141#endif
142}
143
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400144static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400145 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500146{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400147 struct trace_array *tr = op->private;
148
149 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500150 return;
151
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400152 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500153}
154
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500155static void ftrace_sync(struct work_struct *work)
156{
157 /*
158 * This function is just a stub to implement a hard force
Paul E. McKenney74401722018-11-06 18:44:52 -0800159 * of synchronize_rcu(). This requires synchronizing
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500160 * tasks even in userspace and idle.
161 *
162 * Yes, function tracing is rude.
163 */
164}
165
166static void ftrace_sync_ipi(void *data)
167{
168 /* Probably not needed, but do it anyway */
169 smp_rmb();
170}
171
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100172static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
173{
174 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500175 * 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 +0100176 * then it needs to call the list anyway.
177 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +0200178 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
179 FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100180 return ftrace_ops_list_func;
181
182 return ftrace_ops_get_func(ops);
183}
184
Steven Rostedt2b499382011-05-03 22:49:52 -0400185static void update_ftrace_function(void)
186{
187 ftrace_func_t func;
188
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400189 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400190 * Prepare the ftrace_ops that the arch callback will use.
191 * If there's only one ftrace_ops registered, the ftrace_ops_list
192 * will point to the ops we want.
193 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800194 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
195 lockdep_is_held(&ftrace_lock));
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400196
197 /* If there's no ftrace_ops registered, just call the stub function */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800198 if (set_function_trace_op == &ftrace_list_end) {
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400199 func = ftrace_stub;
200
201 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400202 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400203 * recursion safe and not dynamic and the arch supports passing ops,
204 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400205 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800206 } else if (rcu_dereference_protected(ftrace_ops_list->next,
207 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100208 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400209
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400210 } else {
211 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500212 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400213 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400214 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400215
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400216 update_function_graph_func();
217
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500218 /* If there's no change, then do nothing more here */
219 if (ftrace_trace_function == func)
220 return;
221
222 /*
223 * If we are using the list function, it doesn't care
224 * about the function_trace_ops.
225 */
226 if (func == ftrace_ops_list_func) {
227 ftrace_trace_function = func;
228 /*
229 * Don't even bother setting function_trace_ops,
230 * it would be racy to do so anyway.
231 */
232 return;
233 }
234
235#ifndef CONFIG_DYNAMIC_FTRACE
236 /*
237 * For static tracing, we need to be a bit more careful.
238 * The function change takes affect immediately. Thus,
239 * we need to coorditate the setting of the function_trace_ops
240 * with the setting of the ftrace_trace_function.
241 *
242 * Set the function to the list ops, which will call the
243 * function we want, albeit indirectly, but it handles the
244 * ftrace_ops and doesn't depend on function_trace_op.
245 */
246 ftrace_trace_function = ftrace_ops_list_func;
247 /*
248 * Make sure all CPUs see this. Yes this is slow, but static
249 * tracing is slow and nasty to have enabled.
250 */
251 schedule_on_each_cpu(ftrace_sync);
252 /* Now all cpus are using the list ops. */
253 function_trace_op = set_function_trace_op;
254 /* Make sure the function_trace_op is visible on all CPUs */
255 smp_wmb();
256 /* Nasty way to force a rmb on all cpus */
257 smp_call_function(ftrace_sync_ipi, NULL, 1);
258 /* OK, we are all set to update the ftrace_trace_function now! */
259#endif /* !CONFIG_DYNAMIC_FTRACE */
260
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400261 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400262}
263
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800264static void add_ftrace_ops(struct ftrace_ops __rcu **list,
265 struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200266{
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800267 rcu_assign_pointer(ops->next, *list);
268
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200269 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400270 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200271 * CPU might be walking that list. We need to make sure
272 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400273 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200274 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400275 rcu_assign_pointer(*list, ops);
276}
Steven Rostedt3d083392008-05-12 21:20:42 +0200277
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800278static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
279 struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400280{
281 struct ftrace_ops **p;
282
283 /*
284 * If we are removing the last function, then simply point
285 * to the ftrace_stub.
286 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800287 if (rcu_dereference_protected(*list,
288 lockdep_is_held(&ftrace_lock)) == ops &&
289 rcu_dereference_protected(ops->next,
290 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt2b499382011-05-03 22:49:52 -0400291 *list = &ftrace_list_end;
292 return 0;
293 }
294
295 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
296 if (*p == ops)
297 break;
298
299 if (*p != ops)
300 return -1;
301
302 *p = (*p)->next;
303 return 0;
304}
305
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400306static void ftrace_update_trampoline(struct ftrace_ops *ops);
307
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500308int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400309{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500310 if (ops->flags & FTRACE_OPS_FL_DELETED)
311 return -EINVAL;
312
Steven Rostedtb8489142011-05-04 09:27:52 -0400313 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
314 return -EBUSY;
315
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900316#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400317 /*
318 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
319 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
320 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
321 */
322 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
323 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
324 return -EINVAL;
325
326 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
327 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
328#endif
Miroslav Benes71624312019-10-16 13:33:13 +0200329 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
330 return -EBUSY;
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400331
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400332 if (!core_kernel_data((unsigned long)ops))
333 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
334
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500335 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400336
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400337 /* Always save the function, and reset at unregistering */
338 ops->saved_func = ops->func;
339
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400340 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400341 ops->func = ftrace_pid_func;
342
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400343 ftrace_update_trampoline(ops);
344
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400345 if (ftrace_enabled)
346 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200347
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200348 return 0;
349}
350
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500351int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200352{
Steven Rostedt2b499382011-05-03 22:49:52 -0400353 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200354
Steven Rostedtb8489142011-05-04 09:27:52 -0400355 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
356 return -EBUSY;
357
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500358 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400359
Steven Rostedt2b499382011-05-03 22:49:52 -0400360 if (ret < 0)
361 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400362
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400363 if (ftrace_enabled)
364 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200365
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400366 ops->func = ops->saved_func;
367
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500368 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200369}
370
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500371static void ftrace_update_pid_func(void)
372{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400373 struct ftrace_ops *op;
374
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400375 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500376 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900377 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500378
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400379 do_for_each_ftrace_op(op, ftrace_ops_list) {
380 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400381 op->func = ftrace_pids_enabled(op) ?
382 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400383 ftrace_update_trampoline(op);
384 }
385 } while_for_each_ftrace_op(op);
386
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400387 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500388}
389
Steven Rostedt493762f2009-03-23 17:12:36 -0400390#ifdef CONFIG_FUNCTION_PROFILER
391struct ftrace_profile {
392 struct hlist_node node;
393 unsigned long ip;
394 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400395#ifdef CONFIG_FUNCTION_GRAPH_TRACER
396 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400397 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400398#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400399};
400
401struct ftrace_profile_page {
402 struct ftrace_profile_page *next;
403 unsigned long index;
404 struct ftrace_profile records[];
405};
406
Steven Rostedtcafb1682009-03-24 20:50:39 -0400407struct ftrace_profile_stat {
408 atomic_t disabled;
409 struct hlist_head *hash;
410 struct ftrace_profile_page *pages;
411 struct ftrace_profile_page *start;
412 struct tracer_stat stat;
413};
414
Steven Rostedt493762f2009-03-23 17:12:36 -0400415#define PROFILE_RECORDS_SIZE \
416 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
417
418#define PROFILES_PER_PAGE \
419 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
420
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400421static int ftrace_profile_enabled __read_mostly;
422
423/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400424static DEFINE_MUTEX(ftrace_profile_lock);
425
Steven Rostedtcafb1682009-03-24 20:50:39 -0400426static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400427
Namhyung Kim20079eb2013-04-10 08:55:50 +0900428#define FTRACE_PROFILE_HASH_BITS 10
429#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400430
Steven Rostedt493762f2009-03-23 17:12:36 -0400431static void *
432function_stat_next(void *v, int idx)
433{
434 struct ftrace_profile *rec = v;
435 struct ftrace_profile_page *pg;
436
437 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
438
439 again:
Li Zefan0296e422009-06-26 11:15:37 +0800440 if (idx != 0)
441 rec++;
442
Steven Rostedt493762f2009-03-23 17:12:36 -0400443 if ((void *)rec >= (void *)&pg->records[pg->index]) {
444 pg = pg->next;
445 if (!pg)
446 return NULL;
447 rec = &pg->records[0];
448 if (!rec->counter)
449 goto again;
450 }
451
452 return rec;
453}
454
455static void *function_stat_start(struct tracer_stat *trace)
456{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400457 struct ftrace_profile_stat *stat =
458 container_of(trace, struct ftrace_profile_stat, stat);
459
460 if (!stat || !stat->start)
461 return NULL;
462
463 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400464}
465
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400466#ifdef CONFIG_FUNCTION_GRAPH_TRACER
467/* function graph compares on total time */
468static int function_stat_cmp(void *p1, void *p2)
469{
470 struct ftrace_profile *a = p1;
471 struct ftrace_profile *b = p2;
472
473 if (a->time < b->time)
474 return -1;
475 if (a->time > b->time)
476 return 1;
477 else
478 return 0;
479}
480#else
481/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400482static int function_stat_cmp(void *p1, void *p2)
483{
484 struct ftrace_profile *a = p1;
485 struct ftrace_profile *b = p2;
486
487 if (a->counter < b->counter)
488 return -1;
489 if (a->counter > b->counter)
490 return 1;
491 else
492 return 0;
493}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400494#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400495
496static int function_stat_headers(struct seq_file *m)
497{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400498#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100499 seq_puts(m, " Function "
500 "Hit Time Avg s^2\n"
501 " -------- "
502 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400503#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100504 seq_puts(m, " Function Hit\n"
505 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400506#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400507 return 0;
508}
509
510static int function_stat_show(struct seq_file *m, void *v)
511{
512 struct ftrace_profile *rec = v;
513 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800514 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400515#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400516 static struct trace_seq s;
517 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400518 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400519#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800520 mutex_lock(&ftrace_profile_lock);
521
522 /* we raced with function_profile_reset() */
523 if (unlikely(rec->counter == 0)) {
524 ret = -EBUSY;
525 goto out;
526 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400527
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530528#ifdef CONFIG_FUNCTION_GRAPH_TRACER
529 avg = rec->time;
530 do_div(avg, rec->counter);
531 if (tracing_thresh && (avg < tracing_thresh))
532 goto out;
533#endif
534
Steven Rostedt493762f2009-03-23 17:12:36 -0400535 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400536 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400537
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400538#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100539 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400540
Chase Douglase330b3b2010-04-26 14:02:05 -0400541 /* Sample standard deviation (s^2) */
542 if (rec->counter <= 1)
543 stddev = 0;
544 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200545 /*
546 * Apply Welford's method:
547 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
548 */
549 stddev = rec->counter * rec->time_squared -
550 rec->time * rec->time;
551
Chase Douglase330b3b2010-04-26 14:02:05 -0400552 /*
553 * Divide only 1000 for ns^2 -> us^2 conversion.
554 * trace_print_graph_duration will divide 1000 again.
555 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200556 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400557 }
558
Steven Rostedt34886c82009-03-25 21:00:47 -0400559 trace_seq_init(&s);
560 trace_print_graph_duration(rec->time, &s);
561 trace_seq_puts(&s, " ");
562 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400563 trace_seq_puts(&s, " ");
564 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400565 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400566#endif
567 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800568out:
569 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400570
Li Zefan3aaba202010-08-23 16:50:12 +0800571 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400572}
573
Steven Rostedtcafb1682009-03-24 20:50:39 -0400574static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400575{
576 struct ftrace_profile_page *pg;
577
Steven Rostedtcafb1682009-03-24 20:50:39 -0400578 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400579
580 while (pg) {
581 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
582 pg->index = 0;
583 pg = pg->next;
584 }
585
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400587 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
588}
589
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400591{
592 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400593 int functions;
594 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400595 int i;
596
597 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400599 return 0;
600
Steven Rostedtcafb1682009-03-24 20:50:39 -0400601 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
602 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 return -ENOMEM;
604
Steven Rostedt318e0a72009-03-25 20:06:34 -0400605#ifdef CONFIG_DYNAMIC_FTRACE
606 functions = ftrace_update_tot_cnt;
607#else
608 /*
609 * We do not know the number of functions that exist because
610 * dynamic tracing is what counts them. With past experience
611 * we have around 20K functions. That should be more than enough.
612 * It is highly unlikely we will execute every function in
613 * the kernel.
614 */
615 functions = 20000;
616#endif
617
Steven Rostedtcafb1682009-03-24 20:50:39 -0400618 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400619
Steven Rostedt318e0a72009-03-25 20:06:34 -0400620 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
621
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900622 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400624 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400625 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400626 pg = pg->next;
627 }
628
629 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400630
631 out_free:
632 pg = stat->start;
633 while (pg) {
634 unsigned long tmp = (unsigned long)pg;
635
636 pg = pg->next;
637 free_page(tmp);
638 }
639
Steven Rostedt318e0a72009-03-25 20:06:34 -0400640 stat->pages = NULL;
641 stat->start = NULL;
642
643 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400644}
645
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400647{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400649 int size;
650
Steven Rostedtcafb1682009-03-24 20:50:39 -0400651 stat = &per_cpu(ftrace_profile_stats, cpu);
652
653 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400654 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400655 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400656 return 0;
657 }
658
659 /*
660 * We are profiling all functions, but usually only a few thousand
661 * functions are hit. We'll make a hash of 1024 items.
662 */
663 size = FTRACE_PROFILE_HASH_SIZE;
664
Kees Cook6396bb22018-06-12 14:03:40 -0700665 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400666
Steven Rostedtcafb1682009-03-24 20:50:39 -0400667 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400668 return -ENOMEM;
669
Steven Rostedt318e0a72009-03-25 20:06:34 -0400670 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400671 if (ftrace_profile_pages_init(stat) < 0) {
672 kfree(stat->hash);
673 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400674 return -ENOMEM;
675 }
676
677 return 0;
678}
679
Steven Rostedtcafb1682009-03-24 20:50:39 -0400680static int ftrace_profile_init(void)
681{
682 int cpu;
683 int ret = 0;
684
Miao Xiec4602c12013-12-16 15:20:01 +0800685 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686 ret = ftrace_profile_init_cpu(cpu);
687 if (ret)
688 break;
689 }
690
691 return ret;
692}
693
Steven Rostedt493762f2009-03-23 17:12:36 -0400694/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400695static struct ftrace_profile *
696ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400697{
698 struct ftrace_profile *rec;
699 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400700 unsigned long key;
701
Namhyung Kim20079eb2013-04-10 08:55:50 +0900702 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400704
705 if (hlist_empty(hhd))
706 return NULL;
707
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400708 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400709 if (rec->ip == ip)
710 return rec;
711 }
712
713 return NULL;
714}
715
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716static void ftrace_add_profile(struct ftrace_profile_stat *stat,
717 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400718{
719 unsigned long key;
720
Namhyung Kim20079eb2013-04-10 08:55:50 +0900721 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400723}
724
Steven Rostedt318e0a72009-03-25 20:06:34 -0400725/*
726 * The memory is already allocated, this simply finds a new record to use.
727 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400728static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400729ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400730{
731 struct ftrace_profile *rec = NULL;
732
Steven Rostedt318e0a72009-03-25 20:06:34 -0400733 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400734 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400735 goto out;
736
Steven Rostedt493762f2009-03-23 17:12:36 -0400737 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400738 * Try to find the function again since an NMI
739 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400740 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400743 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400744
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745 if (stat->pages->index == PROFILES_PER_PAGE) {
746 if (!stat->pages->next)
747 goto out;
748 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 }
750
Steven Rostedtcafb1682009-03-24 20:50:39 -0400751 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400752 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400753 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400754
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400756 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400757
758 return rec;
759}
760
Steven Rostedt493762f2009-03-23 17:12:36 -0400761static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400762function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400763 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400764{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400765 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400766 struct ftrace_profile *rec;
767 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400768
769 if (!ftrace_profile_enabled)
770 return;
771
Steven Rostedt493762f2009-03-23 17:12:36 -0400772 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773
Christoph Lameterbdffd892014-04-29 14:17:40 -0500774 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400775 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400776 goto out;
777
778 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400779 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400780 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400781 if (!rec)
782 goto out;
783 }
784
785 rec->counter++;
786 out:
787 local_irq_restore(flags);
788}
789
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400790#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500791static bool fgraph_graph_time = true;
792
793void ftrace_graph_graph_time_control(bool enable)
794{
795 fgraph_graph_time = enable;
796}
797
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400798static int profile_graph_entry(struct ftrace_graph_ent *trace)
799{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500800 struct ftrace_ret_stack *ret_stack;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900801
Steven Rostedta1e2e312011-08-09 12:50:46 -0400802 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900803
Steven Rostedt (VMware)a8f0f9e2017-08-17 16:37:25 -0400804 /* If function graph is shutting down, ret_stack can be NULL */
805 if (!current->ret_stack)
806 return 0;
807
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500808 ret_stack = ftrace_graph_get_ret_stack(current, 0);
809 if (ret_stack)
810 ret_stack->subtime = 0;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900811
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400812 return 1;
813}
814
815static void profile_graph_return(struct ftrace_graph_ret *trace)
816{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500817 struct ftrace_ret_stack *ret_stack;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400818 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400819 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400820 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400822
823 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500824 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400825 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400826 goto out;
827
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400828 /* If the calltime was zero'd ignore it */
829 if (!trace->calltime)
830 goto out;
831
Steven Rostedta2a16d62009-03-24 23:17:58 -0400832 calltime = trace->rettime - trace->calltime;
833
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400834 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400835
836 /* Append this call time to the parent time to subtract */
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500837 ret_stack = ftrace_graph_get_ret_stack(current, 1);
838 if (ret_stack)
839 ret_stack->subtime += calltime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400840
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500841 ret_stack = ftrace_graph_get_ret_stack(current, 0);
842 if (ret_stack && ret_stack->subtime < calltime)
843 calltime -= ret_stack->subtime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400844 else
845 calltime = 0;
846 }
847
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400849 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400850 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400851 rec->time_squared += calltime * calltime;
852 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400853
Steven Rostedtcafb1682009-03-24 20:50:39 -0400854 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400855 local_irq_restore(flags);
856}
857
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500858static struct fgraph_ops fprofiler_ops = {
859 .entryfunc = &profile_graph_entry,
860 .retfunc = &profile_graph_return,
861};
862
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400863static int register_ftrace_profiler(void)
864{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500865 return register_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400866}
867
868static void unregister_ftrace_profiler(void)
869{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500870 unregister_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400871}
872#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100873static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400874 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900875 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400876 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400877};
878
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400879static int register_ftrace_profiler(void)
880{
881 return register_ftrace_function(&ftrace_profile_ops);
882}
883
884static void unregister_ftrace_profiler(void)
885{
886 unregister_ftrace_function(&ftrace_profile_ops);
887}
888#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
889
Steven Rostedt493762f2009-03-23 17:12:36 -0400890static ssize_t
891ftrace_profile_write(struct file *filp, const char __user *ubuf,
892 size_t cnt, loff_t *ppos)
893{
894 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400895 int ret;
896
Peter Huewe22fe9b52011-06-07 21:58:27 +0200897 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
898 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400899 return ret;
900
901 val = !!val;
902
903 mutex_lock(&ftrace_profile_lock);
904 if (ftrace_profile_enabled ^ val) {
905 if (val) {
906 ret = ftrace_profile_init();
907 if (ret < 0) {
908 cnt = ret;
909 goto out;
910 }
911
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400912 ret = register_ftrace_profiler();
913 if (ret < 0) {
914 cnt = ret;
915 goto out;
916 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400917 ftrace_profile_enabled = 1;
918 } else {
919 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400920 /*
921 * unregister_ftrace_profiler calls stop_machine
Paul E. McKenney74401722018-11-06 18:44:52 -0800922 * so this acts like an synchronize_rcu.
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400923 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400924 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400925 }
926 }
927 out:
928 mutex_unlock(&ftrace_profile_lock);
929
Jiri Olsacf8517c2009-10-23 19:36:16 -0400930 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400931
932 return cnt;
933}
934
935static ssize_t
936ftrace_profile_read(struct file *filp, char __user *ubuf,
937 size_t cnt, loff_t *ppos)
938{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400939 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400940 int r;
941
942 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
943 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
944}
945
946static const struct file_operations ftrace_profile_fops = {
947 .open = tracing_open_generic,
948 .read = ftrace_profile_read,
949 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200950 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400951};
952
Steven Rostedtcafb1682009-03-24 20:50:39 -0400953/* used to initialize the real stat files */
954static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400955 .name = "functions",
956 .stat_start = function_stat_start,
957 .stat_next = function_stat_next,
958 .stat_cmp = function_stat_cmp,
959 .stat_headers = function_stat_headers,
960 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400961};
962
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500963static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400964{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400965 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400967 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400968 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400969 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400970
Steven Rostedtcafb1682009-03-24 20:50:39 -0400971 for_each_possible_cpu(cpu) {
972 stat = &per_cpu(ftrace_profile_stats, cpu);
973
Geliang Tang6363c6b2016-03-15 22:12:34 +0800974 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400975 if (!name) {
976 /*
977 * The files created are permanent, if something happens
978 * we still do not free memory.
979 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400980 WARN(1,
981 "Could not allocate stat file for cpu %d\n",
982 cpu);
983 return;
984 }
985 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400986 stat->stat.name = name;
987 ret = register_stat_tracer(&stat->stat);
988 if (ret) {
989 WARN(1,
990 "Could not register function stat for cpu %d\n",
991 cpu);
992 kfree(name);
993 return;
994 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400995 }
996
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500997 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -0400998 d_tracer, NULL, &ftrace_profile_fops);
999 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001000 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001001}
1002
1003#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001004static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001005{
1006}
1007#endif /* CONFIG_FUNCTION_PROFILER */
1008
Steven Rostedt3d083392008-05-12 21:20:42 +02001009#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001010
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001011static struct ftrace_ops *removed_ops;
1012
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001013/*
1014 * Set when doing a global update, like enabling all recs or disabling them.
1015 * It is not set when just updating a single ftrace_ops.
1016 */
1017static bool update_all_ops;
1018
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001019#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001020# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001021#endif
1022
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001023struct ftrace_func_entry {
1024 struct hlist_node hlist;
1025 unsigned long ip;
1026};
1027
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001028struct ftrace_func_probe {
1029 struct ftrace_probe_ops *probe_ops;
1030 struct ftrace_ops ops;
1031 struct trace_array *tr;
1032 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001033 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001034 int ref;
1035};
1036
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001037/*
1038 * We make these constant because no one should touch them,
1039 * but they are used as the default "empty hash", to avoid allocating
1040 * it all the time. These are in a read only section such that if
1041 * anyone does try to modify it, it will cause an exception.
1042 */
1043static const struct hlist_head empty_buckets[1];
1044static const struct ftrace_hash empty_hash = {
1045 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001046};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001047#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001048
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001049struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001050 .func = ftrace_stub,
1051 .local_hash.notrace_hash = EMPTY_HASH,
1052 .local_hash.filter_hash = EMPTY_HASH,
1053 INIT_OPS_HASH(global_ops)
1054 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001055 FTRACE_OPS_FL_INITIALIZED |
1056 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001057};
1058
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001059/*
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001060 * Used by the stack undwinder to know about dynamic ftrace trampolines.
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001061 */
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001062struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001063{
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001064 struct ftrace_ops *op = NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001065
1066 /*
1067 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08001068 * they are freed after a synchronize_rcu().
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001069 */
1070 preempt_disable_notrace();
1071
1072 do_for_each_ftrace_op(op, ftrace_ops_list) {
1073 /*
1074 * This is to check for dynamically allocated trampolines.
1075 * Trampolines that are in kernel text will have
1076 * core_kernel_text() return true.
1077 */
1078 if (op->trampoline && op->trampoline_size)
1079 if (addr >= op->trampoline &&
1080 addr < op->trampoline + op->trampoline_size) {
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001081 preempt_enable_notrace();
1082 return op;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001083 }
1084 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001085 preempt_enable_notrace();
1086
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001087 return NULL;
1088}
1089
1090/*
1091 * This is used by __kernel_text_address() to return true if the
1092 * address is on a dynamically allocated trampoline that would
1093 * not return true for either core_kernel_text() or
1094 * is_module_text_address().
1095 */
1096bool is_ftrace_trampoline(unsigned long addr)
1097{
1098 return ftrace_ops_trampoline(addr) != NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001099}
1100
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101struct ftrace_page {
1102 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001103 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001104 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001105 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001106};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001107
Steven Rostedta7900872011-12-16 16:23:44 -05001108#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1109#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001110
1111/* estimate from running different kernels */
1112#define NR_TO_INIT 10000
1113
1114static struct ftrace_page *ftrace_pages_start;
1115static struct ftrace_page *ftrace_pages;
1116
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001117static __always_inline unsigned long
1118ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1119{
1120 if (hash->size_bits > 0)
1121 return hash_long(ip, hash->size_bits);
1122
1123 return 0;
1124}
1125
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001126/* Only use this function if ftrace_hash_empty() has already been tested */
1127static __always_inline struct ftrace_func_entry *
1128__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001129{
1130 unsigned long key;
1131 struct ftrace_func_entry *entry;
1132 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001133
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001134 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001135 hhd = &hash->buckets[key];
1136
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001137 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001138 if (entry->ip == ip)
1139 return entry;
1140 }
1141 return NULL;
1142}
1143
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001144/**
1145 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1146 * @hash: The hash to look at
1147 * @ip: The instruction pointer to test
1148 *
1149 * Search a given @hash to see if a given instruction pointer (@ip)
1150 * exists in it.
1151 *
1152 * Returns the entry that holds the @ip if found. NULL otherwise.
1153 */
1154struct ftrace_func_entry *
1155ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1156{
1157 if (ftrace_hash_empty(hash))
1158 return NULL;
1159
1160 return __ftrace_lookup_ip(hash, ip);
1161}
1162
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001163static void __add_hash_entry(struct ftrace_hash *hash,
1164 struct ftrace_func_entry *entry)
1165{
1166 struct hlist_head *hhd;
1167 unsigned long key;
1168
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001169 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001170 hhd = &hash->buckets[key];
1171 hlist_add_head(&entry->hlist, hhd);
1172 hash->count++;
1173}
1174
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001175static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1176{
1177 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178
1179 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1180 if (!entry)
1181 return -ENOMEM;
1182
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001183 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001184 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001185
1186 return 0;
1187}
1188
1189static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001190free_hash_entry(struct ftrace_hash *hash,
1191 struct ftrace_func_entry *entry)
1192{
1193 hlist_del(&entry->hlist);
1194 kfree(entry);
1195 hash->count--;
1196}
1197
1198static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199remove_hash_entry(struct ftrace_hash *hash,
1200 struct ftrace_func_entry *entry)
1201{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001202 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001203 hash->count--;
1204}
1205
1206static void ftrace_hash_clear(struct ftrace_hash *hash)
1207{
1208 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210 struct ftrace_func_entry *entry;
1211 int size = 1 << hash->size_bits;
1212 int i;
1213
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001214 if (!hash->count)
1215 return;
1216
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001217 for (i = 0; i < size; i++) {
1218 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001219 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001220 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001221 }
1222 FTRACE_WARN_ON(hash->count);
1223}
1224
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001225static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1226{
1227 list_del(&ftrace_mod->list);
1228 kfree(ftrace_mod->module);
1229 kfree(ftrace_mod->func);
1230 kfree(ftrace_mod);
1231}
1232
1233static void clear_ftrace_mod_list(struct list_head *head)
1234{
1235 struct ftrace_mod_load *p, *n;
1236
1237 /* stack tracer isn't supported yet */
1238 if (!head)
1239 return;
1240
1241 mutex_lock(&ftrace_lock);
1242 list_for_each_entry_safe(p, n, head, list)
1243 free_ftrace_mod(p);
1244 mutex_unlock(&ftrace_lock);
1245}
1246
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001247static void free_ftrace_hash(struct ftrace_hash *hash)
1248{
1249 if (!hash || hash == EMPTY_HASH)
1250 return;
1251 ftrace_hash_clear(hash);
1252 kfree(hash->buckets);
1253 kfree(hash);
1254}
1255
Steven Rostedt07fd5512011-05-05 18:03:47 -04001256static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1257{
1258 struct ftrace_hash *hash;
1259
1260 hash = container_of(rcu, struct ftrace_hash, rcu);
1261 free_ftrace_hash(hash);
1262}
1263
1264static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1265{
1266 if (!hash || hash == EMPTY_HASH)
1267 return;
Paul E. McKenney74401722018-11-06 18:44:52 -08001268 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001269}
1270
Jiri Olsa5500fa52012-02-15 15:51:54 +01001271void ftrace_free_filter(struct ftrace_ops *ops)
1272{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001273 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001274 free_ftrace_hash(ops->func_hash->filter_hash);
1275 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001276}
1277
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001278static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1279{
1280 struct ftrace_hash *hash;
1281 int size;
1282
1283 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1284 if (!hash)
1285 return NULL;
1286
1287 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001288 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001289
1290 if (!hash->buckets) {
1291 kfree(hash);
1292 return NULL;
1293 }
1294
1295 hash->size_bits = size_bits;
1296
1297 return hash;
1298}
1299
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001300
1301static int ftrace_add_mod(struct trace_array *tr,
1302 const char *func, const char *module,
1303 int enable)
1304{
1305 struct ftrace_mod_load *ftrace_mod;
1306 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1307
1308 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1309 if (!ftrace_mod)
1310 return -ENOMEM;
1311
1312 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1313 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1314 ftrace_mod->enable = enable;
1315
1316 if (!ftrace_mod->func || !ftrace_mod->module)
1317 goto out_free;
1318
1319 list_add(&ftrace_mod->list, mod_head);
1320
1321 return 0;
1322
1323 out_free:
1324 free_ftrace_mod(ftrace_mod);
1325
1326 return -ENOMEM;
1327}
1328
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001329static struct ftrace_hash *
1330alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1331{
1332 struct ftrace_func_entry *entry;
1333 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001334 int size;
1335 int ret;
1336 int i;
1337
1338 new_hash = alloc_ftrace_hash(size_bits);
1339 if (!new_hash)
1340 return NULL;
1341
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001342 if (hash)
1343 new_hash->flags = hash->flags;
1344
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001345 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001346 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001347 return new_hash;
1348
1349 size = 1 << hash->size_bits;
1350 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001351 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001352 ret = add_hash_entry(new_hash, entry->ip);
1353 if (ret < 0)
1354 goto free_hash;
1355 }
1356 }
1357
1358 FTRACE_WARN_ON(new_hash->count != hash->count);
1359
1360 return new_hash;
1361
1362 free_hash:
1363 free_ftrace_hash(new_hash);
1364 return NULL;
1365}
1366
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001367static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001368ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001369static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001370ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001371
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001372static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1373 struct ftrace_hash *new_hash);
1374
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001375static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001376{
1377 struct ftrace_func_entry *entry;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001378 struct ftrace_hash *new_hash;
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001379 struct hlist_head *hhd;
1380 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001381 int bits = 0;
1382 int i;
1383
1384 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385 * Make the hash size about 1/2 the # found
1386 */
1387 for (size /= 2; size; size >>= 1)
1388 bits++;
1389
1390 /* Don't allocate too much */
1391 if (bits > FTRACE_HASH_MAX_BITS)
1392 bits = FTRACE_HASH_MAX_BITS;
1393
Steven Rostedt07fd5512011-05-05 18:03:47 -04001394 new_hash = alloc_ftrace_hash(bits);
1395 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001396 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001397
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001398 new_hash->flags = src->flags;
1399
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001400 size = 1 << src->size_bits;
1401 for (i = 0; i < size; i++) {
1402 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001403 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001404 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001405 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001406 }
1407 }
Namhyung Kim3e278c02017-01-20 11:44:45 +09001408 return new_hash;
1409}
1410
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001411static struct ftrace_hash *
1412__ftrace_hash_move(struct ftrace_hash *src)
1413{
1414 int size = src->count;
1415
1416 /*
1417 * If the new source is empty, just return the empty_hash.
1418 */
1419 if (ftrace_hash_empty(src))
1420 return EMPTY_HASH;
1421
1422 return dup_hash(src, size);
1423}
1424
Namhyung Kim3e278c02017-01-20 11:44:45 +09001425static int
1426ftrace_hash_move(struct ftrace_ops *ops, int enable,
1427 struct ftrace_hash **dst, struct ftrace_hash *src)
1428{
1429 struct ftrace_hash *new_hash;
1430 int ret;
1431
1432 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1433 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1434 return -EINVAL;
1435
1436 new_hash = __ftrace_hash_move(src);
1437 if (!new_hash)
1438 return -ENOMEM;
1439
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001440 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1441 if (enable) {
1442 /* IPMODIFY should be updated only when filter_hash updating */
1443 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1444 if (ret < 0) {
1445 free_ftrace_hash(new_hash);
1446 return ret;
1447 }
1448 }
1449
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001450 /*
1451 * Remove the current set, update the hash and add
1452 * them back.
1453 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001454 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001455
Steven Rostedt07fd5512011-05-05 18:03:47 -04001456 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001457
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001458 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001459
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001460 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001461}
1462
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001463static bool hash_contains_ip(unsigned long ip,
1464 struct ftrace_ops_hash *hash)
1465{
1466 /*
1467 * The function record is a match if it exists in the filter
1468 * hash and not in the notrace hash. Note, an emty hash is
1469 * considered a match for the filter hash, but an empty
1470 * notrace hash is considered not in the notrace hash.
1471 */
1472 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001473 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001474 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001475 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001476}
1477
Steven Rostedt265c8312009-02-13 12:43:56 -05001478/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001479 * Test the hashes for this ops to see if we want to call
1480 * the ops->func or not.
1481 *
1482 * It's a match if the ip is in the ops->filter_hash or
1483 * the filter_hash does not exist or is empty,
1484 * AND
1485 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001486 *
1487 * This needs to be called with preemption disabled as
Paul E. McKenney74401722018-11-06 18:44:52 -08001488 * the hashes are freed with call_rcu().
Steven Rostedtb8489142011-05-04 09:27:52 -04001489 */
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001490int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001491ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001492{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001493 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001494 int ret;
1495
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001496#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1497 /*
1498 * There's a small race when adding ops that the ftrace handler
1499 * that wants regs, may be called without them. We can not
1500 * allow that handler to be called if regs is NULL.
1501 */
1502 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1503 return 0;
1504#endif
1505
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001506 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1507 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001508
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001509 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001510 ret = 1;
1511 else
1512 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001513
1514 return ret;
1515}
1516
1517/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001518 * This is a double for. Do not use 'break' to break out of the loop,
1519 * you must use a goto.
1520 */
1521#define do_for_each_ftrace_rec(pg, rec) \
1522 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1523 int _____i; \
1524 for (_____i = 0; _____i < pg->index; _____i++) { \
1525 rec = &pg->records[_____i];
1526
1527#define while_for_each_ftrace_rec() \
1528 } \
1529 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301530
Steven Rostedt5855fea2011-12-16 19:27:42 -05001531
1532static int ftrace_cmp_recs(const void *a, const void *b)
1533{
Steven Rostedta650e022012-04-25 13:48:13 -04001534 const struct dyn_ftrace *key = a;
1535 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001536
Steven Rostedta650e022012-04-25 13:48:13 -04001537 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001538 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001539 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1540 return 1;
1541 return 0;
1542}
1543
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001544static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1545{
1546 struct ftrace_page *pg;
1547 struct dyn_ftrace *rec = NULL;
1548 struct dyn_ftrace key;
1549
1550 key.ip = start;
1551 key.flags = end; /* overload flags, as it is unsigned long */
1552
1553 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1554 if (end < pg->records[0].ip ||
1555 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1556 continue;
1557 rec = bsearch(&key, pg->records, pg->index,
1558 sizeof(struct dyn_ftrace),
1559 ftrace_cmp_recs);
1560 }
1561 return rec;
1562}
1563
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001564/**
1565 * ftrace_location_range - return the first address of a traced location
1566 * if it touches the given ip range
1567 * @start: start of range to search.
1568 * @end: end of range to search (inclusive). @end points to the last byte
1569 * to check.
1570 *
1571 * Returns rec->ip if the related ftrace location is a least partly within
1572 * the given address range. That is, the first address of the instruction
1573 * that is either a NOP or call to the function tracer. It checks the ftrace
1574 * internal tables to determine if the address belongs or not.
1575 */
1576unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001577{
Steven Rostedta650e022012-04-25 13:48:13 -04001578 struct dyn_ftrace *rec;
Steven Rostedta650e022012-04-25 13:48:13 -04001579
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001580 rec = lookup_rec(start, end);
1581 if (rec)
1582 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001583
Steven Rostedt5855fea2011-12-16 19:27:42 -05001584 return 0;
1585}
1586
Steven Rostedtc88fd862011-08-16 09:53:39 -04001587/**
1588 * ftrace_location - return true if the ip giving is a traced location
1589 * @ip: the instruction pointer to check
1590 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001591 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001592 * That is, the instruction that is either a NOP or call to
1593 * the function tracer. It checks the ftrace internal tables to
1594 * determine if the address belongs or not.
1595 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001596unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001597{
Steven Rostedta650e022012-04-25 13:48:13 -04001598 return ftrace_location_range(ip, ip);
1599}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001600
Steven Rostedta650e022012-04-25 13:48:13 -04001601/**
1602 * ftrace_text_reserved - return true if range contains an ftrace location
1603 * @start: start of range to search
1604 * @end: end of range to search (inclusive). @end points to the last byte to check.
1605 *
1606 * Returns 1 if @start and @end contains a ftrace location.
1607 * That is, the instruction that is either a NOP or call to
1608 * the function tracer. It checks the ftrace internal tables to
1609 * determine if the address belongs or not.
1610 */
Sasha Levind88471c2013-01-09 18:09:20 -05001611int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001612{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001613 unsigned long ret;
1614
1615 ret = ftrace_location_range((unsigned long)start,
1616 (unsigned long)end);
1617
1618 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001619}
1620
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001621/* Test if ops registered to this rec needs regs */
1622static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1623{
1624 struct ftrace_ops *ops;
1625 bool keep_regs = false;
1626
1627 for (ops = ftrace_ops_list;
1628 ops != &ftrace_list_end; ops = ops->next) {
1629 /* pass rec in as regs to have non-NULL val */
1630 if (ftrace_ops_test(ops, rec->ip, rec)) {
1631 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1632 keep_regs = true;
1633 break;
1634 }
1635 }
1636 }
1637
1638 return keep_regs;
1639}
1640
Cheng Jiana1246922019-05-04 19:39:39 +08001641static struct ftrace_ops *
1642ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1643static struct ftrace_ops *
1644ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1645
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001646static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001647 int filter_hash,
1648 bool inc)
1649{
1650 struct ftrace_hash *hash;
1651 struct ftrace_hash *other_hash;
1652 struct ftrace_page *pg;
1653 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001654 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001655 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001656 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001657
1658 /* Only update if the ops has been registered */
1659 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001660 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001661
1662 /*
1663 * In the filter_hash case:
1664 * If the count is zero, we update all records.
1665 * Otherwise we just update the items in the hash.
1666 *
1667 * In the notrace_hash case:
1668 * We enable the update in the hash.
1669 * As disabling notrace means enabling the tracing,
1670 * and enabling notrace means disabling, the inc variable
1671 * gets inversed.
1672 */
1673 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001674 hash = ops->func_hash->filter_hash;
1675 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001676 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001677 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001678 } else {
1679 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001680 hash = ops->func_hash->notrace_hash;
1681 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001682 /*
1683 * If the notrace hash has no items,
1684 * then there's nothing to do.
1685 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001686 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001687 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001688 }
1689
1690 do_for_each_ftrace_rec(pg, rec) {
1691 int in_other_hash = 0;
1692 int in_hash = 0;
1693 int match = 0;
1694
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001695 if (rec->flags & FTRACE_FL_DISABLED)
1696 continue;
1697
Steven Rostedted926f92011-05-03 13:25:24 -04001698 if (all) {
1699 /*
1700 * Only the filter_hash affects all records.
1701 * Update if the record is not in the notrace hash.
1702 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001703 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001704 match = 1;
1705 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001706 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1707 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001708
1709 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001710 * If filter_hash is set, we want to match all functions
1711 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001712 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001713 * If filter_hash is not set, then we are decrementing.
1714 * That means we match anything that is in the hash
1715 * and also in the other_hash. That is, we need to turn
1716 * off functions in the other hash because they are disabled
1717 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001718 */
1719 if (filter_hash && in_hash && !in_other_hash)
1720 match = 1;
1721 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001722 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001723 match = 1;
1724 }
1725 if (!match)
1726 continue;
1727
1728 if (inc) {
1729 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001730 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001731 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001732
1733 /*
1734 * If there's only a single callback registered to a
1735 * function, and the ops has a trampoline registered
1736 * for it, then we can call it directly.
1737 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001738 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001739 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001740 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001741 /*
1742 * If we are adding another function callback
1743 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001744 * custom trampoline in use, then we need to go
1745 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001746 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001747 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001748
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001749 /*
1750 * If any ops wants regs saved for this function
1751 * then all ops will get saved regs.
1752 */
1753 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1754 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001755 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001756 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001757 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001758 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001759
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001760 /*
1761 * If the rec had REGS enabled and the ops that is
1762 * being removed had REGS set, then see if there is
1763 * still any ops for this record that wants regs.
1764 * If not, we can stop recording them.
1765 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001766 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001767 rec->flags & FTRACE_FL_REGS &&
1768 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1769 if (!test_rec_ops_needs_regs(rec))
1770 rec->flags &= ~FTRACE_FL_REGS;
1771 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001772
1773 /*
Cheng Jiana1246922019-05-04 19:39:39 +08001774 * The TRAMP needs to be set only if rec count
1775 * is decremented to one, and the ops that is
1776 * left has a trampoline. As TRAMP can only be
1777 * enabled if there is only a single ops attached
1778 * to it.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001779 */
Cheng Jiana1246922019-05-04 19:39:39 +08001780 if (ftrace_rec_count(rec) == 1 &&
1781 ftrace_find_tramp_ops_any(rec))
1782 rec->flags |= FTRACE_FL_TRAMP;
1783 else
1784 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001785
1786 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001787 * flags will be cleared in ftrace_check_record()
1788 * if rec count is zero.
1789 */
Steven Rostedted926f92011-05-03 13:25:24 -04001790 }
1791 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001792
1793 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04001794 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001795
Steven Rostedted926f92011-05-03 13:25:24 -04001796 /* Shortcut, if we handled all records, we are done. */
1797 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001798 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001799 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001800
1801 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001802}
1803
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001804static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001805 int filter_hash)
1806{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001807 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001808}
1809
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001810static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001811 int filter_hash)
1812{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001813 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001814}
1815
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001816static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1817 int filter_hash, int inc)
1818{
1819 struct ftrace_ops *op;
1820
1821 __ftrace_hash_rec_update(ops, filter_hash, inc);
1822
1823 if (ops->func_hash != &global_ops.local_hash)
1824 return;
1825
1826 /*
1827 * If the ops shares the global_ops hash, then we need to update
1828 * all ops that are enabled and use this hash.
1829 */
1830 do_for_each_ftrace_op(op, ftrace_ops_list) {
1831 /* Already done */
1832 if (op == ops)
1833 continue;
1834 if (op->func_hash == &global_ops.local_hash)
1835 __ftrace_hash_rec_update(op, filter_hash, inc);
1836 } while_for_each_ftrace_op(op);
1837}
1838
1839static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1840 int filter_hash)
1841{
1842 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1843}
1844
1845static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1846 int filter_hash)
1847{
1848 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1849}
1850
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001851/*
1852 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1853 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1854 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1855 * Note that old_hash and new_hash has below meanings
1856 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1857 * - If the hash is EMPTY_HASH, it hits nothing
1858 * - Anything else hits the recs which match the hash entries.
1859 */
1860static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1861 struct ftrace_hash *old_hash,
1862 struct ftrace_hash *new_hash)
1863{
1864 struct ftrace_page *pg;
1865 struct dyn_ftrace *rec, *end = NULL;
1866 int in_old, in_new;
1867
1868 /* Only update if the ops has been registered */
1869 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1870 return 0;
1871
1872 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1873 return 0;
1874
1875 /*
1876 * Since the IPMODIFY is a very address sensitive action, we do not
1877 * allow ftrace_ops to set all functions to new hash.
1878 */
1879 if (!new_hash || !old_hash)
1880 return -EINVAL;
1881
1882 /* Update rec->flags */
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 /* We need to update only differences of filter_hash */
1889 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1890 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1891 if (in_old == in_new)
1892 continue;
1893
1894 if (in_new) {
1895 /* New entries must ensure no others are using it */
1896 if (rec->flags & FTRACE_FL_IPMODIFY)
1897 goto rollback;
1898 rec->flags |= FTRACE_FL_IPMODIFY;
1899 } else /* Removed entry */
1900 rec->flags &= ~FTRACE_FL_IPMODIFY;
1901 } while_for_each_ftrace_rec();
1902
1903 return 0;
1904
1905rollback:
1906 end = rec;
1907
1908 /* Roll back what we did above */
1909 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001910
1911 if (rec->flags & FTRACE_FL_DISABLED)
1912 continue;
1913
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001914 if (rec == end)
1915 goto err_out;
1916
1917 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1918 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1919 if (in_old == in_new)
1920 continue;
1921
1922 if (in_new)
1923 rec->flags &= ~FTRACE_FL_IPMODIFY;
1924 else
1925 rec->flags |= FTRACE_FL_IPMODIFY;
1926 } while_for_each_ftrace_rec();
1927
1928err_out:
1929 return -EBUSY;
1930}
1931
1932static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1933{
1934 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1935
1936 if (ftrace_hash_empty(hash))
1937 hash = NULL;
1938
1939 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1940}
1941
1942/* Disabling always succeeds */
1943static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1944{
1945 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1946
1947 if (ftrace_hash_empty(hash))
1948 hash = NULL;
1949
1950 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1951}
1952
1953static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1954 struct ftrace_hash *new_hash)
1955{
1956 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1957
1958 if (ftrace_hash_empty(old_hash))
1959 old_hash = NULL;
1960
1961 if (ftrace_hash_empty(new_hash))
1962 new_hash = NULL;
1963
1964 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1965}
1966
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001967static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001968{
1969 int i;
1970
1971 printk(KERN_CONT "%s", fmt);
1972
1973 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1974 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1975}
1976
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001977enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001978const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001979
1980static void print_bug_type(void)
1981{
1982 switch (ftrace_bug_type) {
1983 case FTRACE_BUG_UNKNOWN:
1984 break;
1985 case FTRACE_BUG_INIT:
1986 pr_info("Initializing ftrace call sites\n");
1987 break;
1988 case FTRACE_BUG_NOP:
1989 pr_info("Setting ftrace call site to NOP\n");
1990 break;
1991 case FTRACE_BUG_CALL:
1992 pr_info("Setting ftrace call site to call ftrace function\n");
1993 break;
1994 case FTRACE_BUG_UPDATE:
1995 pr_info("Updating ftrace call site to call a different ftrace function\n");
1996 break;
1997 }
1998}
1999
Steven Rostedtc88fd862011-08-16 09:53:39 -04002000/**
2001 * ftrace_bug - report and shutdown function tracer
2002 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002003 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04002004 *
2005 * The arch code that enables or disables the function tracing
2006 * can call ftrace_bug() when it has detected a problem in
2007 * modifying the code. @failed should be one of either:
2008 * EFAULT - if the problem happens on reading the @ip address
2009 * EINVAL - if what is read at @ip is not what was expected
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05302010 * EPERM - if the problem happens on writing to the @ip address
Steven Rostedtc88fd862011-08-16 09:53:39 -04002011 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002012void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002013{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002014 unsigned long ip = rec ? rec->ip : 0;
2015
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002016 switch (failed) {
2017 case -EFAULT:
2018 FTRACE_WARN_ON_ONCE(1);
2019 pr_info("ftrace faulted on modifying ");
2020 print_ip_sym(ip);
2021 break;
2022 case -EINVAL:
2023 FTRACE_WARN_ON_ONCE(1);
2024 pr_info("ftrace failed to modify ");
2025 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002026 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002027 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002028 if (ftrace_expected) {
2029 print_ip_ins(" expected: ", ftrace_expected);
2030 pr_cont("\n");
2031 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002032 break;
2033 case -EPERM:
2034 FTRACE_WARN_ON_ONCE(1);
2035 pr_info("ftrace faulted on writing ");
2036 print_ip_sym(ip);
2037 break;
2038 default:
2039 FTRACE_WARN_ON_ONCE(1);
2040 pr_info("ftrace faulted on unknown error ");
2041 print_ip_sym(ip);
2042 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002043 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002044 if (rec) {
2045 struct ftrace_ops *ops = NULL;
2046
2047 pr_info("ftrace record flags: %lx\n", rec->flags);
2048 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2049 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2050 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2051 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002052 if (ops) {
2053 do {
2054 pr_cont("\ttramp: %pS (%pS)",
2055 (void *)ops->trampoline,
2056 (void *)ops->func);
2057 ops = ftrace_find_tramp_ops_next(rec, ops);
2058 } while (ops);
2059 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002060 pr_cont("\ttramp: ERROR!");
2061
2062 }
2063 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002064 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002065 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002066}
2067
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002068static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002069{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002070 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002071
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002072 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2073
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002074 if (rec->flags & FTRACE_FL_DISABLED)
2075 return FTRACE_UPDATE_IGNORE;
2076
Steven Rostedt982c3502008-11-15 16:31:41 -05002077 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002078 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002079 *
Steven Rostedted926f92011-05-03 13:25:24 -04002080 * If the record has a ref count, then we need to enable it
2081 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002082 *
Steven Rostedted926f92011-05-03 13:25:24 -04002083 * Otherwise we make sure its disabled.
2084 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002085 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002086 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002087 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002088 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002089 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002090
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002091 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002092 * If enabling and the REGS flag does not match the REGS_EN, or
2093 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2094 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002095 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002096 if (flag) {
2097 if (!(rec->flags & FTRACE_FL_REGS) !=
2098 !(rec->flags & FTRACE_FL_REGS_EN))
2099 flag |= FTRACE_FL_REGS;
2100
2101 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2102 !(rec->flags & FTRACE_FL_TRAMP_EN))
2103 flag |= FTRACE_FL_TRAMP;
2104 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002105
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002106 /* If the state of this record hasn't changed, then do nothing */
2107 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002108 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002109
2110 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002111 /* Save off if rec is being enabled (for return value) */
2112 flag ^= rec->flags & FTRACE_FL_ENABLED;
2113
2114 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002115 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002116 if (flag & FTRACE_FL_REGS) {
2117 if (rec->flags & FTRACE_FL_REGS)
2118 rec->flags |= FTRACE_FL_REGS_EN;
2119 else
2120 rec->flags &= ~FTRACE_FL_REGS_EN;
2121 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002122 if (flag & FTRACE_FL_TRAMP) {
2123 if (rec->flags & FTRACE_FL_TRAMP)
2124 rec->flags |= FTRACE_FL_TRAMP_EN;
2125 else
2126 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2127 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002128 }
2129
2130 /*
2131 * If this record is being updated from a nop, then
2132 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002133 * Otherwise,
2134 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002135 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002136 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002137 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002138 if (flag & FTRACE_FL_ENABLED) {
2139 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002140 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002141 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002142
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002143 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002144 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002145 }
2146
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002147 if (update) {
2148 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002149 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002150 rec->flags = 0;
2151 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002152 /*
2153 * Just disable the record, but keep the ops TRAMP
2154 * and REGS states. The _EN flags must be disabled though.
2155 */
2156 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2157 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002158 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002159
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002160 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002161 return FTRACE_UPDATE_MAKE_NOP;
2162}
2163
2164/**
2165 * ftrace_update_record, set a record that now is tracing or not
2166 * @rec: the record to update
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002167 * @enable: set to true if the record is tracing, false to force disable
Steven Rostedtc88fd862011-08-16 09:53:39 -04002168 *
2169 * The records that represent all functions that can be traced need
2170 * to be updated when tracing has been enabled.
2171 */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002172int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002173{
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002174 return ftrace_check_record(rec, enable, true);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002175}
2176
2177/**
2178 * ftrace_test_record, check if the record has been enabled or not
2179 * @rec: the record to test
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002180 * @enable: set to true to check if enabled, false if it is disabled
Steven Rostedtc88fd862011-08-16 09:53:39 -04002181 *
2182 * The arch code may need to test if a record is already set to
2183 * tracing to determine how to modify the function code that it
2184 * represents.
2185 */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002186int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002187{
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002188 return ftrace_check_record(rec, enable, false);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002189}
2190
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002191static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002192ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2193{
2194 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002195 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002196
2197 do_for_each_ftrace_op(op, ftrace_ops_list) {
2198
2199 if (!op->trampoline)
2200 continue;
2201
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002202 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002203 return op;
2204 } while_for_each_ftrace_op(op);
2205
2206 return NULL;
2207}
2208
2209static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002210ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2211 struct ftrace_ops *op)
2212{
2213 unsigned long ip = rec->ip;
2214
2215 while_for_each_ftrace_op(op) {
2216
2217 if (!op->trampoline)
2218 continue;
2219
2220 if (hash_contains_ip(ip, op->func_hash))
2221 return op;
2222 }
2223
2224 return NULL;
2225}
2226
2227static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002228ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2229{
2230 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002231 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002232
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002233 /*
2234 * Need to check removed ops first.
2235 * If they are being removed, and this rec has a tramp,
2236 * and this rec is in the ops list, then it would be the
2237 * one with the tramp.
2238 */
2239 if (removed_ops) {
2240 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002241 return removed_ops;
2242 }
2243
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002244 /*
2245 * Need to find the current trampoline for a rec.
2246 * Now, a trampoline is only attached to a rec if there
2247 * was a single 'ops' attached to it. But this can be called
2248 * when we are adding another op to the rec or removing the
2249 * current one. Thus, if the op is being added, we can
2250 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002251 * yet.
2252 *
2253 * If an ops is being modified (hooking to different functions)
2254 * then we don't care about the new functions that are being
2255 * added, just the old ones (that are probably being removed).
2256 *
2257 * If we are adding an ops to a function that already is using
2258 * a trampoline, it needs to be removed (trampolines are only
2259 * for single ops connected), then an ops that is not being
2260 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002261 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002262 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002263
2264 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002265 continue;
2266
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002267 /*
2268 * If the ops is being added, it hasn't gotten to
2269 * the point to be removed from this tree yet.
2270 */
2271 if (op->flags & FTRACE_OPS_FL_ADDING)
2272 continue;
2273
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002274
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002275 /*
2276 * If the ops is being modified and is in the old
2277 * hash, then it is probably being removed from this
2278 * function.
2279 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002280 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2281 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002282 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002283 /*
2284 * If the ops is not being added or modified, and it's
2285 * in its normal filter hash, then this must be the one
2286 * we want!
2287 */
2288 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2289 hash_contains_ip(ip, op->func_hash))
2290 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002291
2292 } while_for_each_ftrace_op(op);
2293
2294 return NULL;
2295}
2296
2297static struct ftrace_ops *
2298ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2299{
2300 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002301 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002302
2303 do_for_each_ftrace_op(op, ftrace_ops_list) {
2304 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002305 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002306 return op;
2307 } while_for_each_ftrace_op(op);
2308
2309 return NULL;
2310}
2311
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002312/**
2313 * ftrace_get_addr_new - Get the call address to set to
2314 * @rec: The ftrace record descriptor
2315 *
2316 * If the record has the FTRACE_FL_REGS set, that means that it
2317 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2318 * is not not set, then it wants to convert to the normal callback.
2319 *
2320 * Returns the address of the trampoline to set to
2321 */
2322unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2323{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002324 struct ftrace_ops *ops;
2325
2326 /* Trampolines take precedence over regs */
2327 if (rec->flags & FTRACE_FL_TRAMP) {
2328 ops = ftrace_find_tramp_ops_new(rec);
2329 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002330 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2331 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002332 /* Ftrace is shutting down, return anything */
2333 return (unsigned long)FTRACE_ADDR;
2334 }
2335 return ops->trampoline;
2336 }
2337
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002338 if (rec->flags & FTRACE_FL_REGS)
2339 return (unsigned long)FTRACE_REGS_ADDR;
2340 else
2341 return (unsigned long)FTRACE_ADDR;
2342}
2343
2344/**
2345 * ftrace_get_addr_curr - Get the call address that is already there
2346 * @rec: The ftrace record descriptor
2347 *
2348 * The FTRACE_FL_REGS_EN is set when the record already points to
2349 * a function that saves all the regs. Basically the '_EN' version
2350 * represents the current state of the function.
2351 *
2352 * Returns the address of the trampoline that is currently being called
2353 */
2354unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2355{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002356 struct ftrace_ops *ops;
2357
2358 /* Trampolines take precedence over regs */
2359 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2360 ops = ftrace_find_tramp_ops_curr(rec);
2361 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002362 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2363 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002364 /* Ftrace is shutting down, return anything */
2365 return (unsigned long)FTRACE_ADDR;
2366 }
2367 return ops->trampoline;
2368 }
2369
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002370 if (rec->flags & FTRACE_FL_REGS_EN)
2371 return (unsigned long)FTRACE_REGS_ADDR;
2372 else
2373 return (unsigned long)FTRACE_ADDR;
2374}
2375
Steven Rostedtc88fd862011-08-16 09:53:39 -04002376static int
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002377__ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002378{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002379 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002380 unsigned long ftrace_addr;
2381 int ret;
2382
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002383 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002384
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002385 /* This needs to be done before we call ftrace_update_record */
2386 ftrace_old_addr = ftrace_get_addr_curr(rec);
2387
2388 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002389
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002390 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2391
Steven Rostedtc88fd862011-08-16 09:53:39 -04002392 switch (ret) {
2393 case FTRACE_UPDATE_IGNORE:
2394 return 0;
2395
2396 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002397 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002398 return ftrace_make_call(rec, ftrace_addr);
2399
2400 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002401 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002402 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002403
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002404 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002405 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002406 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002407 }
2408
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05302409 return -1; /* unknown ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002410}
2411
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002412void __weak ftrace_replace_code(int mod_flags)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002413{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002414 struct dyn_ftrace *rec;
2415 struct ftrace_page *pg;
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002416 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002417 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002418 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002419
Steven Rostedt45a4a232011-04-21 23:16:46 -04002420 if (unlikely(ftrace_disabled))
2421 return;
2422
Steven Rostedt265c8312009-02-13 12:43:56 -05002423 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002424
2425 if (rec->flags & FTRACE_FL_DISABLED)
2426 continue;
2427
Steven Rostedte4f5d542012-04-27 09:13:18 -04002428 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002429 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002430 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002431 /* Stop processing */
2432 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002433 }
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002434 if (schedulable)
2435 cond_resched();
Steven Rostedt265c8312009-02-13 12:43:56 -05002436 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002437}
2438
Steven Rostedtc88fd862011-08-16 09:53:39 -04002439struct ftrace_rec_iter {
2440 struct ftrace_page *pg;
2441 int index;
2442};
2443
2444/**
2445 * ftrace_rec_iter_start, start up iterating over traced functions
2446 *
2447 * Returns an iterator handle that is used to iterate over all
2448 * the records that represent address locations where functions
2449 * are traced.
2450 *
2451 * May return NULL if no records are available.
2452 */
2453struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2454{
2455 /*
2456 * We only use a single iterator.
2457 * Protected by the ftrace_lock mutex.
2458 */
2459 static struct ftrace_rec_iter ftrace_rec_iter;
2460 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2461
2462 iter->pg = ftrace_pages_start;
2463 iter->index = 0;
2464
2465 /* Could have empty pages */
2466 while (iter->pg && !iter->pg->index)
2467 iter->pg = iter->pg->next;
2468
2469 if (!iter->pg)
2470 return NULL;
2471
2472 return iter;
2473}
2474
2475/**
2476 * ftrace_rec_iter_next, get the next record to process.
2477 * @iter: The handle to the iterator.
2478 *
2479 * Returns the next iterator after the given iterator @iter.
2480 */
2481struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2482{
2483 iter->index++;
2484
2485 if (iter->index >= iter->pg->index) {
2486 iter->pg = iter->pg->next;
2487 iter->index = 0;
2488
2489 /* Could have empty pages */
2490 while (iter->pg && !iter->pg->index)
2491 iter->pg = iter->pg->next;
2492 }
2493
2494 if (!iter->pg)
2495 return NULL;
2496
2497 return iter;
2498}
2499
2500/**
2501 * ftrace_rec_iter_record, get the record at the iterator location
2502 * @iter: The current iterator location
2503 *
2504 * Returns the record that the current @iter is at.
2505 */
2506struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2507{
2508 return &iter->pg->records[iter->index];
2509}
2510
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302511static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002512ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002513{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002514 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002515
Steven Rostedt45a4a232011-04-21 23:16:46 -04002516 if (unlikely(ftrace_disabled))
2517 return 0;
2518
Shaohua Li25aac9d2009-01-09 11:29:40 +08002519 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002520 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002521 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002522 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302523 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002524 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302525 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002526}
2527
Steven Rostedt000ab692009-02-17 13:35:06 -05002528/*
2529 * archs can override this function if they must do something
2530 * before the modifying code is performed.
2531 */
2532int __weak ftrace_arch_code_modify_prepare(void)
2533{
2534 return 0;
2535}
2536
2537/*
2538 * archs can override this function if they must do something
2539 * after the modifying code is performed.
2540 */
2541int __weak ftrace_arch_code_modify_post_process(void)
2542{
2543 return 0;
2544}
2545
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002546void ftrace_modify_all_code(int command)
2547{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002548 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002549 int mod_flags = 0;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002550 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002551
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002552 if (command & FTRACE_MAY_SLEEP)
2553 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2554
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002555 /*
2556 * If the ftrace_caller calls a ftrace_ops func directly,
2557 * we need to make sure that it only traces functions it
2558 * expects to trace. When doing the switch of functions,
2559 * we need to update to the ftrace_ops_list_func first
2560 * before the transition between old and new calls are set,
2561 * as the ftrace_ops_list_func will check the ops hashes
2562 * to make sure the ops are having the right functions
2563 * traced.
2564 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002565 if (update) {
2566 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2567 if (FTRACE_WARN_ON(err))
2568 return;
2569 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002570
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002571 if (command & FTRACE_UPDATE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002572 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002573 else if (command & FTRACE_DISABLE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002574 ftrace_replace_code(mod_flags);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002575
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002576 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2577 function_trace_op = set_function_trace_op;
2578 smp_wmb();
2579 /* If irqs are disabled, we are in stop machine */
2580 if (!irqs_disabled())
2581 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002582 err = ftrace_update_ftrace_func(ftrace_trace_function);
2583 if (FTRACE_WARN_ON(err))
2584 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002585 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002586
2587 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002588 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002589 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002590 err = ftrace_disable_ftrace_graph_caller();
2591 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002592}
2593
Ingo Molnare309b412008-05-12 21:20:51 +02002594static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002595{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002596 int *command = data;
2597
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002598 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002599
Steven Rostedtc88fd862011-08-16 09:53:39 -04002600 return 0;
2601}
2602
2603/**
2604 * ftrace_run_stop_machine, go back to the stop machine method
2605 * @command: The command to tell ftrace what to do
2606 *
2607 * If an arch needs to fall back to the stop machine method, the
2608 * it can call this function.
2609 */
2610void ftrace_run_stop_machine(int command)
2611{
2612 stop_machine(__ftrace_modify_code, &command, NULL);
2613}
2614
2615/**
2616 * arch_ftrace_update_code, modify the code to trace or not trace
2617 * @command: The command that needs to be done
2618 *
2619 * Archs can override this function if it does not need to
2620 * run stop_machine() to modify code.
2621 */
2622void __weak arch_ftrace_update_code(int command)
2623{
2624 ftrace_run_stop_machine(command);
2625}
2626
2627static void ftrace_run_update_code(int command)
2628{
2629 int ret;
2630
2631 ret = ftrace_arch_code_modify_prepare();
2632 FTRACE_WARN_ON(ret);
2633 if (ret)
Petr Mladekd5b844a2019-06-27 10:13:34 +02002634 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002635
2636 /*
2637 * By default we use stop_machine() to modify the code.
2638 * But archs can do what ever they want as long as it
2639 * is safe. The stop_machine() is the safest, but also
2640 * produces the most overhead.
2641 */
2642 arch_ftrace_update_code(command);
2643
Steven Rostedt000ab692009-02-17 13:35:06 -05002644 ret = ftrace_arch_code_modify_post_process();
2645 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002646}
2647
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002648static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002649 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002650{
2651 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002652 ops->old_hash.filter_hash = old_hash->filter_hash;
2653 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002654 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002655 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002656 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002657 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2658}
2659
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002660static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002661static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002662
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002663void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2664{
2665}
2666
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002667static void ftrace_startup_enable(int command)
2668{
2669 if (saved_ftrace_func != ftrace_trace_function) {
2670 saved_ftrace_func = ftrace_trace_function;
2671 command |= FTRACE_UPDATE_TRACE_FUNC;
2672 }
2673
2674 if (!command || !ftrace_enabled)
2675 return;
2676
2677 ftrace_run_update_code(command);
2678}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002679
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002680static void ftrace_startup_all(int command)
2681{
2682 update_all_ops = true;
2683 ftrace_startup_enable(command);
2684 update_all_ops = false;
2685}
2686
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002687int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002688{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002689 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002690
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002691 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002692 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002693
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002694 ret = __register_ftrace_function(ops);
2695 if (ret)
2696 return ret;
2697
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002698 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002699
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002700 /*
2701 * Note that ftrace probes uses this to start up
2702 * and modify functions it will probe. But we still
2703 * set the ADDING flag for modification, as probes
2704 * do not have trampolines. If they add them in the
2705 * future, then the probes will need to distinguish
2706 * between adding and updating probes.
2707 */
2708 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002709
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002710 ret = ftrace_hash_ipmodify_enable(ops);
2711 if (ret < 0) {
2712 /* Rollback registration process */
2713 __unregister_ftrace_function(ops);
2714 ftrace_start_up--;
2715 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2716 return ret;
2717 }
2718
Jiri Olsa7f50d062016-03-16 15:34:33 +01002719 if (ftrace_hash_rec_enable(ops, 1))
2720 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002721
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002722 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002723
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002724 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2725
Steven Rostedta1cd6172011-05-23 15:24:25 -04002726 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002727}
2728
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002729int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002730{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002731 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002732
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002733 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002734 return -ENODEV;
2735
2736 ret = __unregister_ftrace_function(ops);
2737 if (ret)
2738 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002739
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002740 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002741 /*
2742 * Just warn in case of unbalance, no need to kill ftrace, it's not
2743 * critical but the ftrace_call callers may be never nopped again after
2744 * further ftrace uses.
2745 */
2746 WARN_ON_ONCE(ftrace_start_up < 0);
2747
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002748 /* Disabling ipmodify never fails */
2749 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002750
2751 if (ftrace_hash_rec_disable(ops, 1))
2752 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002753
Namhyung Kima737e6d2014-06-12 23:56:12 +09002754 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002755
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002756 if (saved_ftrace_func != ftrace_trace_function) {
2757 saved_ftrace_func = ftrace_trace_function;
2758 command |= FTRACE_UPDATE_TRACE_FUNC;
2759 }
2760
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002761 if (!command || !ftrace_enabled) {
2762 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002763 * If these are dynamic or per_cpu ops, they still
2764 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002765 * not currently active, we can just free them
2766 * without synchronizing all CPUs.
2767 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002768 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002769 goto free_ops;
2770
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002771 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002772 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002773
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002774 /*
2775 * If the ops uses a trampoline, then it needs to be
2776 * tested first on update.
2777 */
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 removed_ops = ops;
2780
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002781 /* The trampoline logic checks the old hashes */
2782 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2783 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2784
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002785 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002786
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002787 /*
2788 * If there's no more ops registered with ftrace, run a
2789 * sanity check to make sure all rec flags are cleared.
2790 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002791 if (rcu_dereference_protected(ftrace_ops_list,
2792 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002793 struct ftrace_page *pg;
2794 struct dyn_ftrace *rec;
2795
2796 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002797 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002798 pr_warn(" %pS flags:%lx\n",
2799 (void *)rec->ip, rec->flags);
2800 } while_for_each_ftrace_rec();
2801 }
2802
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002803 ops->old_hash.filter_hash = NULL;
2804 ops->old_hash.notrace_hash = NULL;
2805
2806 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002807 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002808
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002809 /*
2810 * Dynamic ops may be freed, we must make sure that all
2811 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002812 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002813 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002814 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002815 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002816 /*
2817 * We need to do a hard force of sched synchronization.
2818 * This is because we use preempt_disable() to do RCU, but
2819 * the function tracers can be called where RCU is not watching
2820 * (like before user_exit()). We can not rely on the RCU
2821 * infrastructure to do the synchronization, thus we must do it
2822 * ourselves.
2823 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002824 schedule_on_each_cpu(ftrace_sync);
2825
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002826 /*
2827 * When the kernel is preeptive, tasks can be preempted
2828 * while on a ftrace trampoline. Just scheduling a task on
2829 * a CPU is not good enough to flush them. Calling
2830 * synchornize_rcu_tasks() will wait for those tasks to
2831 * execute and either schedule voluntarily or enter user space.
2832 */
Thomas Gleixner30c93702019-07-26 23:19:40 +02002833 if (IS_ENABLED(CONFIG_PREEMPTION))
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002834 synchronize_rcu_tasks();
2835
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002836 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002837 arch_ftrace_trampoline_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002838 }
2839
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002840 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002841}
2842
Ingo Molnare309b412008-05-12 21:20:51 +02002843static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002844{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302845 int command;
2846
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002847 if (unlikely(ftrace_disabled))
2848 return;
2849
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002850 /* Force update next time */
2851 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002852 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302853 if (ftrace_start_up) {
2854 command = FTRACE_UPDATE_CALLS;
2855 if (ftrace_graph_active)
2856 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002857 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302858 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002859}
2860
Ingo Molnare309b412008-05-12 21:20:51 +02002861static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002862{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302863 int command;
2864
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002865 if (unlikely(ftrace_disabled))
2866 return;
2867
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002868 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302869 if (ftrace_start_up) {
2870 command = FTRACE_DISABLE_CALLS;
2871 if (ftrace_graph_active)
2872 command |= FTRACE_STOP_FUNC_RET;
2873 ftrace_run_update_code(command);
2874 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002875}
2876
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002877static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002878unsigned long ftrace_update_tot_cnt;
2879
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002880static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002881{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002882 /*
2883 * Filter_hash being empty will default to trace module.
2884 * But notrace hash requires a test of individual module functions.
2885 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002886 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2887 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002888}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002889
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002890/*
2891 * Check if the current ops references the record.
2892 *
2893 * If the ops traces all functions, then it was already accounted for.
2894 * If the ops does not trace the current record function, skip it.
2895 * If the ops ignores the function via notrace filter, skip it.
2896 */
2897static inline bool
2898ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2899{
2900 /* If ops isn't enabled, ignore it */
2901 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002902 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002903
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002904 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002905 if (ops_traces_mod(ops))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002906 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002907
2908 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002909 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002910 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002911 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002912
2913 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002914 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002915 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002916
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002917 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002918}
2919
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002920static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002921{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002922 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002923 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002924 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002925 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002926 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002927 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002928
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002929 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002930
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002931 /*
2932 * When a module is loaded, this function is called to convert
2933 * the calls to mcount in its text to nops, and also to create
2934 * an entry in the ftrace data. Now, if ftrace is activated
2935 * after this call, but before the module sets its text to
2936 * read-only, the modification of enabling ftrace can fail if
2937 * the read-only is done while ftrace is converting the calls.
2938 * To prevent this, the module's records are set as disabled
2939 * and will be enabled after the call to set the module's text
2940 * to read-only.
2941 */
2942 if (mod)
2943 rec_flags |= FTRACE_FL_DISABLED;
2944
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002945 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302946
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002947 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002948
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002949 /* If something went wrong, bail without enabling anything */
2950 if (unlikely(ftrace_disabled))
2951 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002952
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002953 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002954 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302955
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002956 /*
2957 * Do the initial record conversion from mcount jump
2958 * to the NOP instructions.
2959 */
Vasily Gorbikcbdaeaf2019-06-05 13:11:58 +02002960 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
2961 !ftrace_code_disable(mod, p))
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002962 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002963
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002964 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002965 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002966 }
2967
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002968 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002969 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002970 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002971
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002972 return 0;
2973}
2974
Steven Rostedta7900872011-12-16 16:23:44 -05002975static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002976{
Steven Rostedta7900872011-12-16 16:23:44 -05002977 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002978 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002979
Steven Rostedta7900872011-12-16 16:23:44 -05002980 if (WARN_ON(!count))
2981 return -EINVAL;
2982
2983 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002984
2985 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002986 * We want to fill as much as possible. No more than a page
2987 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002988 */
Steven Rostedta7900872011-12-16 16:23:44 -05002989 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2990 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002991
Steven Rostedta7900872011-12-16 16:23:44 -05002992 again:
2993 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2994
2995 if (!pg->records) {
2996 /* if we can't allocate this size, try something smaller */
2997 if (!order)
2998 return -ENOMEM;
2999 order >>= 1;
3000 goto again;
3001 }
3002
3003 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3004 pg->size = cnt;
3005
3006 if (cnt > count)
3007 cnt = count;
3008
3009 return cnt;
3010}
3011
3012static struct ftrace_page *
3013ftrace_allocate_pages(unsigned long num_to_init)
3014{
3015 struct ftrace_page *start_pg;
3016 struct ftrace_page *pg;
3017 int order;
3018 int cnt;
3019
3020 if (!num_to_init)
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05303021 return NULL;
Steven Rostedta7900872011-12-16 16:23:44 -05003022
3023 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3024 if (!pg)
3025 return NULL;
3026
3027 /*
3028 * Try to allocate as much as possible in one continues
3029 * location that fills in all of the space. We want to
3030 * waste as little space as possible.
3031 */
3032 for (;;) {
3033 cnt = ftrace_allocate_records(pg, num_to_init);
3034 if (cnt < 0)
3035 goto free_pages;
3036
3037 num_to_init -= cnt;
3038 if (!num_to_init)
3039 break;
3040
3041 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3042 if (!pg->next)
3043 goto free_pages;
3044
3045 pg = pg->next;
3046 }
3047
3048 return start_pg;
3049
3050 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003051 pg = start_pg;
3052 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003053 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3054 free_pages((unsigned long)pg->records, order);
3055 start_pg = pg->next;
3056 kfree(pg);
3057 pg = start_pg;
3058 }
3059 pr_info("ftrace: FAILED to allocate memory for functions\n");
3060 return NULL;
3061}
3062
Steven Rostedt5072c592008-05-12 21:20:43 +02003063#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3064
3065struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003066 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003067 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003068 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003069 struct ftrace_page *pg;
3070 struct dyn_ftrace *func;
3071 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003072 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003073 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003074 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003075 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003076 struct trace_array *tr;
3077 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003078 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003079 int idx;
3080 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003081};
3082
Ingo Molnare309b412008-05-12 21:20:51 +02003083static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003084t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003085{
3086 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003087 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003088 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003089 struct ftrace_hash *hash;
3090 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003091 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003092 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003093 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003094
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003095 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003096 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003097
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003098 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003099 return NULL;
3100
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003101 func_probes = &tr->func_probes;
3102 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003103 return NULL;
3104
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003105 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003106 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003107 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003108 }
3109
3110 if (iter->probe_entry)
3111 hnd = &iter->probe_entry->hlist;
3112
3113 hash = iter->probe->ops.func_hash->filter_hash;
Naveen N. Rao7bd46642019-07-04 20:04:41 +05303114
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04003115 /*
3116 * A probe being registered may temporarily have an empty hash
3117 * and it's at the end of the func_probes list.
3118 */
3119 if (!hash || hash == EMPTY_HASH)
Naveen N. Rao7bd46642019-07-04 20:04:41 +05303120 return NULL;
3121
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003122 size = 1 << hash->size_bits;
3123
3124 retry:
3125 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003126 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003127 return NULL;
3128 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003129 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003130 hash = iter->probe->ops.func_hash->filter_hash;
3131 size = 1 << hash->size_bits;
3132 iter->pidx = 0;
3133 }
3134
3135 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003136
3137 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003138 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003139 hnd = NULL;
3140 goto retry;
3141 }
3142
3143 if (!hnd)
3144 hnd = hhd->first;
3145 else {
3146 hnd = hnd->next;
3147 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003148 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003149 goto retry;
3150 }
3151 }
3152
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003153 if (WARN_ON_ONCE(!hnd))
3154 return NULL;
3155
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003156 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003157
3158 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003159}
3160
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003161static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003162{
3163 struct ftrace_iterator *iter = m->private;
3164 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003165 loff_t l;
3166
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003167 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003168 return NULL;
3169
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003170 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003171 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003172
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003173 iter->probe = NULL;
3174 iter->probe_entry = NULL;
3175 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003176 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003177 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003178 if (!p)
3179 break;
3180 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003181 if (!p)
3182 return NULL;
3183
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003184 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003185 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003186
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003187 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003188}
3189
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003190static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003191t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003192{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003193 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003194 struct ftrace_probe_ops *probe_ops;
3195 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003196
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003197 probe = iter->probe;
3198 probe_entry = iter->probe_entry;
3199
3200 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003201 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003202
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003203 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003204
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003205 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003206 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003207
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003208 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3209 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003210
3211 return 0;
3212}
3213
3214static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003215t_mod_next(struct seq_file *m, loff_t *pos)
3216{
3217 struct ftrace_iterator *iter = m->private;
3218 struct trace_array *tr = iter->tr;
3219
3220 (*pos)++;
3221 iter->pos = *pos;
3222
3223 iter->mod_list = iter->mod_list->next;
3224
3225 if (iter->mod_list == &tr->mod_trace ||
3226 iter->mod_list == &tr->mod_notrace) {
3227 iter->flags &= ~FTRACE_ITER_MOD;
3228 return NULL;
3229 }
3230
3231 iter->mod_pos = *pos;
3232
3233 return iter;
3234}
3235
3236static void *t_mod_start(struct seq_file *m, loff_t *pos)
3237{
3238 struct ftrace_iterator *iter = m->private;
3239 void *p = NULL;
3240 loff_t l;
3241
3242 if (iter->func_pos > *pos)
3243 return NULL;
3244
3245 iter->mod_pos = iter->func_pos;
3246
3247 /* probes are only available if tr is set */
3248 if (!iter->tr)
3249 return NULL;
3250
3251 for (l = 0; l <= (*pos - iter->func_pos); ) {
3252 p = t_mod_next(m, &l);
3253 if (!p)
3254 break;
3255 }
3256 if (!p) {
3257 iter->flags &= ~FTRACE_ITER_MOD;
3258 return t_probe_start(m, pos);
3259 }
3260
3261 /* Only set this if we have an item */
3262 iter->flags |= FTRACE_ITER_MOD;
3263
3264 return iter;
3265}
3266
3267static int
3268t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3269{
3270 struct ftrace_mod_load *ftrace_mod;
3271 struct trace_array *tr = iter->tr;
3272
3273 if (WARN_ON_ONCE(!iter->mod_list) ||
3274 iter->mod_list == &tr->mod_trace ||
3275 iter->mod_list == &tr->mod_notrace)
3276 return -EIO;
3277
3278 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3279
3280 if (ftrace_mod->func)
3281 seq_printf(m, "%s", ftrace_mod->func);
3282 else
3283 seq_putc(m, '*');
3284
3285 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3286
3287 return 0;
3288}
3289
3290static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003291t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003292{
3293 struct ftrace_iterator *iter = m->private;
3294 struct dyn_ftrace *rec = NULL;
3295
3296 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003297
Steven Rostedt5072c592008-05-12 21:20:43 +02003298 retry:
3299 if (iter->idx >= iter->pg->index) {
3300 if (iter->pg->next) {
3301 iter->pg = iter->pg->next;
3302 iter->idx = 0;
3303 goto retry;
3304 }
3305 } else {
3306 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003307 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3308 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003309
3310 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003311 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003312
Steven Rostedt5072c592008-05-12 21:20:43 +02003313 rec = NULL;
3314 goto retry;
3315 }
3316 }
3317
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003318 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003319 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003320
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003321 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003322 iter->func = rec;
3323
3324 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003325}
3326
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003327static void *
3328t_next(struct seq_file *m, void *v, loff_t *pos)
3329{
3330 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003331 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003332 void *ret;
3333
3334 if (unlikely(ftrace_disabled))
3335 return NULL;
3336
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003337 if (iter->flags & FTRACE_ITER_PROBE)
3338 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003339
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003340 if (iter->flags & FTRACE_ITER_MOD)
3341 return t_mod_next(m, pos);
3342
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003343 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003344 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003345 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003346 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003347 }
3348
3349 ret = t_func_next(m, pos);
3350
3351 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003352 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003353
3354 return ret;
3355}
3356
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003357static void reset_iter_read(struct ftrace_iterator *iter)
3358{
3359 iter->pos = 0;
3360 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003361 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003362}
3363
3364static void *t_start(struct seq_file *m, loff_t *pos)
3365{
3366 struct ftrace_iterator *iter = m->private;
3367 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003368 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003369
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003370 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003371
3372 if (unlikely(ftrace_disabled))
3373 return NULL;
3374
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003375 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003376 * If an lseek was done, then reset and start from beginning.
3377 */
3378 if (*pos < iter->pos)
3379 reset_iter_read(iter);
3380
3381 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003382 * For set_ftrace_filter reading, if we have the filter
3383 * off, we can short cut and just print out that all
3384 * functions are enabled.
3385 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003386 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3387 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003388 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003389 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003390 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003391 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003392 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003393 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003394 return iter;
3395 }
3396
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003397 if (iter->flags & FTRACE_ITER_MOD)
3398 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003399
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003400 /*
3401 * Unfortunately, we need to restart at ftrace_pages_start
3402 * every time we let go of the ftrace_mutex. This is because
3403 * those pointers can change without the lock.
3404 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003405 iter->pg = ftrace_pages_start;
3406 iter->idx = 0;
3407 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003408 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003409 if (!p)
3410 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003411 }
walimis5821e1b2008-11-15 15:19:06 +08003412
Steven Rostedt69a30832011-12-19 15:21:16 -05003413 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003414 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003415
3416 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003417}
3418
3419static void t_stop(struct seq_file *m, void *p)
3420{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003421 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003422}
3423
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003424void * __weak
3425arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3426{
3427 return NULL;
3428}
3429
3430static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3431 struct dyn_ftrace *rec)
3432{
3433 void *ptr;
3434
3435 ptr = arch_ftrace_trampoline_func(ops, rec);
3436 if (ptr)
3437 seq_printf(m, " ->%pS", ptr);
3438}
3439
Steven Rostedt5072c592008-05-12 21:20:43 +02003440static int t_show(struct seq_file *m, void *v)
3441{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003442 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003443 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003444
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003445 if (iter->flags & FTRACE_ITER_PROBE)
3446 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003447
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003448 if (iter->flags & FTRACE_ITER_MOD)
3449 return t_mod_show(m, iter);
3450
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003451 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003452 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003453 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003454 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003455 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003456 return 0;
3457 }
3458
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003459 rec = iter->func;
3460
Steven Rostedt5072c592008-05-12 21:20:43 +02003461 if (!rec)
3462 return 0;
3463
Steven Rostedt647bcd02011-05-03 14:39:21 -04003464 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003465 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003466 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003467
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003468 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003469 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003470 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3471 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003472 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003473 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003474 if (ops) {
3475 do {
3476 seq_printf(m, "\ttramp: %pS (%pS)",
3477 (void *)ops->trampoline,
3478 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003479 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003480 ops = ftrace_find_tramp_ops_next(rec, ops);
3481 } while (ops);
3482 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003483 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003484 } else {
3485 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003486 }
3487 }
3488
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003489 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003490
3491 return 0;
3492}
3493
James Morris88e9d342009-09-22 16:43:43 -07003494static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003495 .start = t_start,
3496 .next = t_next,
3497 .stop = t_stop,
3498 .show = t_show,
3499};
3500
Ingo Molnare309b412008-05-12 21:20:51 +02003501static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003502ftrace_avail_open(struct inode *inode, struct file *file)
3503{
3504 struct ftrace_iterator *iter;
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003505 int ret;
3506
3507 ret = security_locked_down(LOCKDOWN_TRACEFS);
3508 if (ret)
3509 return ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003510
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003511 if (unlikely(ftrace_disabled))
3512 return -ENODEV;
3513
Jiri Olsa50e18b92012-04-25 10:23:39 +02003514 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003515 if (!iter)
3516 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003517
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003518 iter->pg = ftrace_pages_start;
3519 iter->ops = &global_ops;
3520
3521 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003522}
3523
Steven Rostedt647bcd02011-05-03 14:39:21 -04003524static int
3525ftrace_enabled_open(struct inode *inode, struct file *file)
3526{
3527 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003528
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003529 /*
3530 * This shows us what functions are currently being
3531 * traced and by what. Not sure if we want lockdown
3532 * to hide such critical information for an admin.
3533 * Although, perhaps it can show information we don't
3534 * want people to see, but if something is tracing
3535 * something, we probably want to know about it.
3536 */
3537
Jiri Olsa50e18b92012-04-25 10:23:39 +02003538 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003539 if (!iter)
3540 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003541
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003542 iter->pg = ftrace_pages_start;
3543 iter->flags = FTRACE_ITER_ENABLED;
3544 iter->ops = &global_ops;
3545
3546 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003547}
3548
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003549/**
3550 * ftrace_regex_open - initialize function tracer filter files
3551 * @ops: The ftrace_ops that hold the hash filters
3552 * @flag: The type of filter to process
3553 * @inode: The inode, usually passed in to your open routine
3554 * @file: The file, usually passed in to your open routine
3555 *
3556 * ftrace_regex_open() initializes the filter files for the
3557 * @ops. Depending on @flag it may process the filter hash or
3558 * the notrace hash of @ops. With this called from the open
3559 * routine, you can use ftrace_filter_write() for the write
3560 * routine if @flag has FTRACE_ITER_FILTER set, or
3561 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003562 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003563 * release must call ftrace_regex_release().
3564 */
3565int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003566ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003567 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003568{
3569 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003570 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003571 struct list_head *mod_head;
3572 struct trace_array *tr = ops->private;
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003573 int ret = -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003574
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003575 ftrace_ops_init(ops);
3576
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003577 if (unlikely(ftrace_disabled))
3578 return -ENODEV;
3579
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04003580 if (tracing_check_open_get_tr(tr))
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003581 return -ENODEV;
3582
Steven Rostedt5072c592008-05-12 21:20:43 +02003583 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3584 if (!iter)
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003585 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003586
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003587 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3588 goto out;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003589
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003590 iter->ops = ops;
3591 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003592 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003593
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003594 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003595
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003596 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003597 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003598 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003599 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003600 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003601 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003602 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003603
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003604 iter->mod_list = mod_head;
3605
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003606 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003607 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3608
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003609 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003610 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003611 clear_ftrace_mod_list(mod_head);
3612 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003613 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003614 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003615
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003616 if (!iter->hash) {
3617 trace_parser_put(&iter->parser);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003618 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003619 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003620 } else
3621 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003622
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003623 ret = 0;
3624
Steven Rostedt5072c592008-05-12 21:20:43 +02003625 if (file->f_mode & FMODE_READ) {
3626 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003627
3628 ret = seq_open(file, &show_ftrace_seq_ops);
3629 if (!ret) {
3630 struct seq_file *m = file->private_data;
3631 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003632 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003633 /* Failed */
3634 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003635 trace_parser_put(&iter->parser);
Li Zefan79fe2492009-09-22 13:54:28 +08003636 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003637 } else
3638 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003639
3640 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003641 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003642
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003643 out:
3644 if (ret) {
3645 kfree(iter);
3646 if (tr)
3647 trace_array_put(tr);
3648 }
3649
Steven Rostedt5072c592008-05-12 21:20:43 +02003650 return ret;
3651}
3652
Steven Rostedt41c52c02008-05-22 11:46:33 -04003653static int
3654ftrace_filter_open(struct inode *inode, struct file *file)
3655{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003656 struct ftrace_ops *ops = inode->i_private;
3657
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003658 /* Checks for tracefs lockdown */
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003659 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003660 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003661 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003662}
3663
3664static int
3665ftrace_notrace_open(struct inode *inode, struct file *file)
3666{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003667 struct ftrace_ops *ops = inode->i_private;
3668
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003669 /* Checks for tracefs lockdown */
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003670 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003671 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003672}
3673
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003674/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3675struct ftrace_glob {
3676 char *search;
3677 unsigned len;
3678 int type;
3679};
3680
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003681/*
3682 * If symbols in an architecture don't correspond exactly to the user-visible
3683 * name of what they represent, it is possible to define this function to
3684 * perform the necessary adjustments.
3685*/
3686char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3687{
3688 return str;
3689}
3690
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003691static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003692{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003693 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003694 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003695
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003696 str = arch_ftrace_match_adjust(str, g->search);
3697
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003698 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003699 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003700 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003701 matched = 1;
3702 break;
3703 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003704 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003705 matched = 1;
3706 break;
3707 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003708 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003709 matched = 1;
3710 break;
3711 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003712 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003713 if (slen >= g->len &&
3714 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003715 matched = 1;
3716 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003717 case MATCH_GLOB:
3718 if (glob_match(g->search, str))
3719 matched = 1;
3720 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003721 }
3722
3723 return matched;
3724}
3725
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003726static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003727enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003728{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003729 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003730 int ret = 0;
3731
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003732 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003733 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003734 /* Do nothing if it doesn't exist */
3735 if (!entry)
3736 return 0;
3737
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003738 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003739 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003740 /* Do nothing if it exists */
3741 if (entry)
3742 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003743
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003744 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003745 }
3746 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003747}
3748
Steven Rostedt64e7c442009-02-13 17:08:48 -05003749static int
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003750add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3751 int clear_filter)
3752{
3753 long index = simple_strtoul(func_g->search, NULL, 0);
3754 struct ftrace_page *pg;
3755 struct dyn_ftrace *rec;
3756
3757 /* The index starts at 1 */
3758 if (--index < 0)
3759 return 0;
3760
3761 do_for_each_ftrace_rec(pg, rec) {
3762 if (pg->index <= index) {
3763 index -= pg->index;
3764 /* this is a double loop, break goes to the next page */
3765 break;
3766 }
3767 rec = &pg->records[index];
3768 enter_record(hash, rec, clear_filter);
3769 return 1;
3770 } while_for_each_ftrace_rec();
3771 return 0;
3772}
3773
3774static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003775ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3776 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003777{
3778 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003779 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003780
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003781 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3782
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003783 if (mod_g) {
3784 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3785
3786 /* blank module name to match all modules */
3787 if (!mod_g->len) {
3788 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003789 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003790 goto func_match;
3791 return 0;
3792 }
3793
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003794 /*
3795 * exclude_mod is set to trace everything but the given
3796 * module. If it is set and the module matches, then
3797 * return 0. If it is not set, and the module doesn't match
3798 * also return 0. Otherwise, check the function to see if
3799 * that matches.
3800 */
3801 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003802 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003803func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003804 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003805 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003806 return 1;
3807 }
3808
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003809 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003810}
3811
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003812static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003813match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003814{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003815 struct ftrace_page *pg;
3816 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003817 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003818 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3819 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3820 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003821 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003822 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03003823 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003824
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003825 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003826 func_g.type = filter_parse_regex(func, len, &func_g.search,
3827 &clear_filter);
3828 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003829 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003830
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003831 if (mod) {
3832 mod_g.type = filter_parse_regex(mod, strlen(mod),
3833 &mod_g.search, &exclude_mod);
3834 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003835 }
3836
Steven Rostedt52baf112009-02-14 01:15:39 -05003837 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003838
3839 if (unlikely(ftrace_disabled))
3840 goto out_unlock;
3841
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003842 if (func_g.type == MATCH_INDEX) {
3843 found = add_rec_by_index(hash, &func_g, clear_filter);
3844 goto out_unlock;
3845 }
3846
Steven Rostedt265c8312009-02-13 12:43:56 -05003847 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003848
3849 if (rec->flags & FTRACE_FL_DISABLED)
3850 continue;
3851
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003852 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003853 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003854 if (ret < 0) {
3855 found = ret;
3856 goto out_unlock;
3857 }
Li Zefan311d16d2009-12-08 11:15:11 +08003858 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003859 }
3860 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003861 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003862 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003863
3864 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003865}
3866
Steven Rostedt64e7c442009-02-13 17:08:48 -05003867static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003868ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003869{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003870 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003871}
3872
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003873static void ftrace_ops_update_code(struct ftrace_ops *ops,
3874 struct ftrace_ops_hash *old_hash)
3875{
3876 struct ftrace_ops *op;
3877
3878 if (!ftrace_enabled)
3879 return;
3880
3881 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3882 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3883 return;
3884 }
3885
3886 /*
3887 * If this is the shared global_ops filter, then we need to
3888 * check if there is another ops that shares it, is enabled.
3889 * If so, we still need to run the modify code.
3890 */
3891 if (ops->func_hash != &global_ops.local_hash)
3892 return;
3893
3894 do_for_each_ftrace_op(op, ftrace_ops_list) {
3895 if (op->func_hash == &global_ops.local_hash &&
3896 op->flags & FTRACE_OPS_FL_ENABLED) {
3897 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3898 /* Only need to do this once */
3899 return;
3900 }
3901 } while_for_each_ftrace_op(op);
3902}
3903
3904static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3905 struct ftrace_hash **orig_hash,
3906 struct ftrace_hash *hash,
3907 int enable)
3908{
3909 struct ftrace_ops_hash old_hash_ops;
3910 struct ftrace_hash *old_hash;
3911 int ret;
3912
3913 old_hash = *orig_hash;
3914 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3915 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3916 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3917 if (!ret) {
3918 ftrace_ops_update_code(ops, &old_hash_ops);
3919 free_ftrace_hash_rcu(old_hash);
3920 }
3921 return ret;
3922}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003923
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003924static bool module_exists(const char *module)
3925{
3926 /* All modules have the symbol __this_module */
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01003927 static const char this_mod[] = "__this_module";
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003928 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003929 unsigned long val;
3930 int n;
3931
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003932 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003933
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003934 if (n > sizeof(modname) - 1)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003935 return false;
3936
3937 val = module_kallsyms_lookup_name(modname);
3938 return val != 0;
3939}
3940
3941static int cache_mod(struct trace_array *tr,
3942 const char *func, char *module, int enable)
3943{
3944 struct ftrace_mod_load *ftrace_mod, *n;
3945 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3946 int ret;
3947
3948 mutex_lock(&ftrace_lock);
3949
3950 /* We do not cache inverse filters */
3951 if (func[0] == '!') {
3952 func++;
3953 ret = -EINVAL;
3954
3955 /* Look to remove this hash */
3956 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3957 if (strcmp(ftrace_mod->module, module) != 0)
3958 continue;
3959
3960 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03003961 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003962 (ftrace_mod->func &&
3963 strcmp(ftrace_mod->func, func) == 0)) {
3964 ret = 0;
3965 free_ftrace_mod(ftrace_mod);
3966 continue;
3967 }
3968 }
3969 goto out;
3970 }
3971
3972 ret = -EINVAL;
3973 /* We only care about modules that have not been loaded yet */
3974 if (module_exists(module))
3975 goto out;
3976
3977 /* Save this string off, and execute it when the module is loaded */
3978 ret = ftrace_add_mod(tr, func, module, enable);
3979 out:
3980 mutex_unlock(&ftrace_lock);
3981
3982 return ret;
3983}
3984
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003985static int
3986ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3987 int reset, int enable);
3988
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003989#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003990static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3991 char *mod, bool enable)
3992{
3993 struct ftrace_mod_load *ftrace_mod, *n;
3994 struct ftrace_hash **orig_hash, *new_hash;
3995 LIST_HEAD(process_mods);
3996 char *func;
3997 int ret;
3998
3999 mutex_lock(&ops->func_hash->regex_lock);
4000
4001 if (enable)
4002 orig_hash = &ops->func_hash->filter_hash;
4003 else
4004 orig_hash = &ops->func_hash->notrace_hash;
4005
4006 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4007 *orig_hash);
4008 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004009 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004010
4011 mutex_lock(&ftrace_lock);
4012
4013 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4014
4015 if (strcmp(ftrace_mod->module, mod) != 0)
4016 continue;
4017
4018 if (ftrace_mod->func)
4019 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4020 else
4021 func = kstrdup("*", GFP_KERNEL);
4022
4023 if (!func) /* warn? */
4024 continue;
4025
4026 list_del(&ftrace_mod->list);
4027 list_add(&ftrace_mod->list, &process_mods);
4028
4029 /* Use the newly allocated func, as it may be "*" */
4030 kfree(ftrace_mod->func);
4031 ftrace_mod->func = func;
4032 }
4033
4034 mutex_unlock(&ftrace_lock);
4035
4036 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4037
4038 func = ftrace_mod->func;
4039
4040 /* Grabs ftrace_lock, which is why we have this extra step */
4041 match_records(new_hash, func, strlen(func), mod);
4042 free_ftrace_mod(ftrace_mod);
4043 }
4044
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004045 if (enable && list_empty(head))
4046 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4047
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004048 mutex_lock(&ftrace_lock);
4049
4050 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4051 new_hash, enable);
4052 mutex_unlock(&ftrace_lock);
4053
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004054 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004055 mutex_unlock(&ops->func_hash->regex_lock);
4056
4057 free_ftrace_hash(new_hash);
4058}
4059
4060static void process_cached_mods(const char *mod_name)
4061{
4062 struct trace_array *tr;
4063 char *mod;
4064
4065 mod = kstrdup(mod_name, GFP_KERNEL);
4066 if (!mod)
4067 return;
4068
4069 mutex_lock(&trace_types_lock);
4070 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4071 if (!list_empty(&tr->mod_trace))
4072 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4073 if (!list_empty(&tr->mod_notrace))
4074 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4075 }
4076 mutex_unlock(&trace_types_lock);
4077
4078 kfree(mod);
4079}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004080#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004081
Steven Rostedtf6180772009-02-14 00:40:25 -05004082/*
4083 * We register the module command as a template to show others how
4084 * to register the a command as well.
4085 */
4086
4087static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004088ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004089 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004090{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004091 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004092 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004093
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004094 /* match_records() modifies func, and we need the original */
4095 func = kstrdup(func_orig, GFP_KERNEL);
4096 if (!func)
4097 return -ENOMEM;
4098
Steven Rostedtf6180772009-02-14 00:40:25 -05004099 /*
4100 * cmd == 'mod' because we only registered this func
4101 * for the 'mod' ftrace_func_command.
4102 * But if you register one func with multiple commands,
4103 * you can tell which command was used by the cmd
4104 * parameter.
4105 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004106 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004107 kfree(func);
4108
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004109 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004110 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004111 if (ret < 0)
4112 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004113 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004114}
4115
4116static struct ftrace_func_command ftrace_mod_cmd = {
4117 .name = "mod",
4118 .func = ftrace_mod_callback,
4119};
4120
4121static int __init ftrace_mod_cmd_init(void)
4122{
4123 return register_ftrace_command(&ftrace_mod_cmd);
4124}
Steven Rostedt6f415672012-10-05 12:13:07 -04004125core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004126
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004127static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004128 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004129{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004130 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004131 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004132
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004133 probe = container_of(op, struct ftrace_func_probe, ops);
4134 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004135
4136 /*
4137 * Disable preemption for these calls to prevent a RCU grace
4138 * period. This syncs the hash iteration and freeing of items
4139 * on the hash. rcu_read_lock is too dangerous here.
4140 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004141 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004142 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004143 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004144}
4145
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004146struct ftrace_func_map {
4147 struct ftrace_func_entry entry;
4148 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004149};
4150
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004151struct ftrace_func_mapper {
4152 struct ftrace_hash hash;
4153};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004154
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004155/**
4156 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4157 *
4158 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4159 */
4160struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004161{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004162 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004163
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004164 /*
4165 * The mapper is simply a ftrace_hash, but since the entries
4166 * in the hash are not ftrace_func_entry type, we define it
4167 * as a separate structure.
4168 */
4169 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4170 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004171}
4172
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004173/**
4174 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4175 * @mapper: The mapper that has the ip maps
4176 * @ip: the instruction pointer to find the data for
4177 *
4178 * Returns the data mapped to @ip if found otherwise NULL. The return
4179 * is actually the address of the mapper data pointer. The address is
4180 * returned for use cases where the data is no bigger than a long, and
4181 * the user can use the data pointer as its data instead of having to
4182 * allocate more memory for the reference.
4183 */
4184void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4185 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004186{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004187 struct ftrace_func_entry *entry;
4188 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004189
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004190 entry = ftrace_lookup_ip(&mapper->hash, ip);
4191 if (!entry)
4192 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004193
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004194 map = (struct ftrace_func_map *)entry;
4195 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004196}
4197
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004198/**
4199 * ftrace_func_mapper_add_ip - Map some data to an ip
4200 * @mapper: The mapper that has the ip maps
4201 * @ip: The instruction pointer address to map @data to
4202 * @data: The data to map to @ip
4203 *
4204 * Returns 0 on succes otherwise an error.
4205 */
4206int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4207 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004208{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004209 struct ftrace_func_entry *entry;
4210 struct ftrace_func_map *map;
4211
4212 entry = ftrace_lookup_ip(&mapper->hash, ip);
4213 if (entry)
4214 return -EBUSY;
4215
4216 map = kmalloc(sizeof(*map), GFP_KERNEL);
4217 if (!map)
4218 return -ENOMEM;
4219
4220 map->entry.ip = ip;
4221 map->data = data;
4222
4223 __add_hash_entry(&mapper->hash, &map->entry);
4224
4225 return 0;
4226}
4227
4228/**
4229 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4230 * @mapper: The mapper that has the ip maps
4231 * @ip: The instruction pointer address to remove the data from
4232 *
4233 * Returns the data if it is found, otherwise NULL.
4234 * Note, if the data pointer is used as the data itself, (see
4235 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4236 * if the data pointer was set to zero.
4237 */
4238void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4239 unsigned long ip)
4240{
4241 struct ftrace_func_entry *entry;
4242 struct ftrace_func_map *map;
4243 void *data;
4244
4245 entry = ftrace_lookup_ip(&mapper->hash, ip);
4246 if (!entry)
4247 return NULL;
4248
4249 map = (struct ftrace_func_map *)entry;
4250 data = map->data;
4251
4252 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004253 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004254
4255 return data;
4256}
4257
4258/**
4259 * free_ftrace_func_mapper - free a mapping of ips and data
4260 * @mapper: The mapper that has the ip maps
4261 * @free_func: A function to be called on each data item.
4262 *
4263 * This is used to free the function mapper. The @free_func is optional
4264 * and can be used if the data needs to be freed as well.
4265 */
4266void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4267 ftrace_mapper_func free_func)
4268{
4269 struct ftrace_func_entry *entry;
4270 struct ftrace_func_map *map;
4271 struct hlist_head *hhd;
Wei Li04e03d92019-06-06 11:17:54 +08004272 int size, i;
4273
4274 if (!mapper)
4275 return;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004276
4277 if (free_func && mapper->hash.count) {
Wei Li04e03d92019-06-06 11:17:54 +08004278 size = 1 << mapper->hash.size_bits;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004279 for (i = 0; i < size; i++) {
4280 hhd = &mapper->hash.buckets[i];
4281 hlist_for_each_entry(entry, hhd, hlist) {
4282 map = (struct ftrace_func_map *)entry;
4283 free_func(map);
4284 }
4285 }
4286 }
4287 free_ftrace_hash(&mapper->hash);
4288}
4289
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004290static void release_probe(struct ftrace_func_probe *probe)
4291{
4292 struct ftrace_probe_ops *probe_ops;
4293
4294 mutex_lock(&ftrace_lock);
4295
4296 WARN_ON(probe->ref <= 0);
4297
4298 /* Subtract the ref that was used to protect this instance */
4299 probe->ref--;
4300
4301 if (!probe->ref) {
4302 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004303 /*
4304 * Sending zero as ip tells probe_ops to free
4305 * the probe->data itself
4306 */
4307 if (probe_ops->free)
4308 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004309 list_del(&probe->list);
4310 kfree(probe);
4311 }
4312 mutex_unlock(&ftrace_lock);
4313}
4314
4315static void acquire_probe_locked(struct ftrace_func_probe *probe)
4316{
4317 /*
4318 * Add one ref to keep it from being freed when releasing the
4319 * ftrace_lock mutex.
4320 */
4321 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004322}
4323
Steven Rostedt59df055f2009-02-14 15:29:06 -05004324int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004325register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004326 struct ftrace_probe_ops *probe_ops,
4327 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004328{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004329 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004330 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004331 struct ftrace_hash **orig_hash;
4332 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004333 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004334 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004335 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004336 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004337 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004338
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004339 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004340 return -EINVAL;
4341
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004342 /* We do not support '!' for function probes */
4343 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004344 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004345
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004346
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004347 mutex_lock(&ftrace_lock);
4348 /* Check if the probe_ops is already registered */
4349 list_for_each_entry(probe, &tr->func_probes, list) {
4350 if (probe->probe_ops == probe_ops)
4351 break;
4352 }
4353 if (&probe->list == &tr->func_probes) {
4354 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4355 if (!probe) {
4356 mutex_unlock(&ftrace_lock);
4357 return -ENOMEM;
4358 }
4359 probe->probe_ops = probe_ops;
4360 probe->ops.func = function_trace_probe_call;
4361 probe->tr = tr;
4362 ftrace_ops_init(&probe->ops);
4363 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004364 }
4365
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004366 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004367
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004368 mutex_unlock(&ftrace_lock);
4369
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04004370 /*
4371 * Note, there's a small window here that the func_hash->filter_hash
4372 * may be NULL or empty. Need to be carefule when reading the loop.
4373 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004374 mutex_lock(&probe->ops.func_hash->regex_lock);
4375
4376 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004377 old_hash = *orig_hash;
4378 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4379
Naveen N. Rao5b0022d2019-07-04 20:04:42 +05304380 if (!hash) {
4381 ret = -ENOMEM;
4382 goto out;
4383 }
4384
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004385 ret = ftrace_match_records(hash, glob, strlen(glob));
4386
4387 /* Nothing found? */
4388 if (!ret)
4389 ret = -EINVAL;
4390
4391 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004392 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004393
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004394 size = 1 << hash->size_bits;
4395 for (i = 0; i < size; i++) {
4396 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4397 if (ftrace_lookup_ip(old_hash, entry->ip))
4398 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004399 /*
4400 * The caller might want to do something special
4401 * for each function we find. We call the callback
4402 * to give the caller an opportunity to do so.
4403 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004404 if (probe_ops->init) {
4405 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004406 entry->ip, data,
4407 &probe->data);
4408 if (ret < 0) {
4409 if (probe_ops->free && count)
4410 probe_ops->free(probe_ops, tr,
4411 0, probe->data);
4412 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004413 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004414 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004415 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004416 count++;
4417 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004418 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004419
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004420 mutex_lock(&ftrace_lock);
4421
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004422 if (!count) {
4423 /* Nothing was added? */
4424 ret = -EINVAL;
4425 goto out_unlock;
4426 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004427
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004428 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4429 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004430 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004431 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004432
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004433 /* One ref for each new function traced */
4434 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004435
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004436 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4437 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004438
Steven Rostedt59df055f2009-02-14 15:29:06 -05004439 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004440 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004441
4442 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004443 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004444 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004445 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004446 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004447
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004448 release_probe(probe);
4449
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004450 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004451
4452 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004453 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004454 goto out_unlock;
4455
4456 /* Failed to do the move, need to call the free functions */
4457 for (i = 0; i < size; i++) {
4458 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4459 if (ftrace_lookup_ip(old_hash, entry->ip))
4460 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004461 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004462 }
4463 }
4464 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004465}
4466
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004467int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004468unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4469 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004470{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004471 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004472 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004473 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004474 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004475 struct ftrace_hash **orig_hash;
4476 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004477 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004478 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004479 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004480 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004481 int count = 0;
4482 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004483 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004484
Naveen N. Raocbab5672017-05-16 23:21:25 +05304485 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004486 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304487 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004488 int not;
4489
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004490 func_g.type = filter_parse_regex(glob, strlen(glob),
4491 &func_g.search, &not);
4492 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004493
Steven Rostedtb6887d72009-02-17 12:32:04 -05004494 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004495 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004496 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004497 }
4498
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004499 mutex_lock(&ftrace_lock);
4500 /* Check if the probe_ops is already registered */
4501 list_for_each_entry(probe, &tr->func_probes, list) {
4502 if (probe->probe_ops == probe_ops)
4503 break;
4504 }
4505 if (&probe->list == &tr->func_probes)
4506 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004507
4508 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004509 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4510 goto err_unlock_ftrace;
4511
4512 acquire_probe_locked(probe);
4513
4514 mutex_unlock(&ftrace_lock);
4515
4516 mutex_lock(&probe->ops.func_hash->regex_lock);
4517
4518 orig_hash = &probe->ops.func_hash->filter_hash;
4519 old_hash = *orig_hash;
4520
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004521 if (ftrace_hash_empty(old_hash))
4522 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004523
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004524 old_hash_ops.filter_hash = old_hash;
4525 /* Probes only have filters */
4526 old_hash_ops.notrace_hash = NULL;
4527
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004528 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004529 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004530 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004531 goto out_unlock;
4532
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004533 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004534
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004535 size = 1 << hash->size_bits;
4536 for (i = 0; i < size; i++) {
4537 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004538
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004539 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004540 kallsyms_lookup(entry->ip, NULL, NULL,
4541 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004542 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004543 continue;
4544 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004545 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004546 remove_hash_entry(hash, entry);
4547 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004548 }
4549 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004550
4551 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004552 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004553 ret = -EINVAL;
4554 goto out_unlock;
4555 }
4556
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004557 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004558
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004559 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004560
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004561 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004562
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004563 if (ftrace_hash_empty(hash))
4564 ftrace_shutdown(&probe->ops, 0);
4565
4566 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004567 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004568
4569 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004570 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004571 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004572 &old_hash_ops);
Paul E. McKenney74401722018-11-06 18:44:52 -08004573 synchronize_rcu();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004574
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004575 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4576 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004577 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004578 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004579 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004580 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004581 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004582
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004583 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004584 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004585 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004586
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004587 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004588
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004589 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004590
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004591 err_unlock_ftrace:
4592 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004593 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004594}
4595
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304596void clear_ftrace_function_probes(struct trace_array *tr)
4597{
4598 struct ftrace_func_probe *probe, *n;
4599
4600 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4601 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4602}
4603
Steven Rostedtf6180772009-02-14 00:40:25 -05004604static LIST_HEAD(ftrace_commands);
4605static DEFINE_MUTEX(ftrace_cmd_mutex);
4606
Tom Zanussi38de93a2013-10-24 08:34:18 -05004607/*
4608 * Currently we only register ftrace commands from __init, so mark this
4609 * __init too.
4610 */
4611__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004612{
4613 struct ftrace_func_command *p;
4614 int ret = 0;
4615
4616 mutex_lock(&ftrace_cmd_mutex);
4617 list_for_each_entry(p, &ftrace_commands, list) {
4618 if (strcmp(cmd->name, p->name) == 0) {
4619 ret = -EBUSY;
4620 goto out_unlock;
4621 }
4622 }
4623 list_add(&cmd->list, &ftrace_commands);
4624 out_unlock:
4625 mutex_unlock(&ftrace_cmd_mutex);
4626
4627 return ret;
4628}
4629
Tom Zanussi38de93a2013-10-24 08:34:18 -05004630/*
4631 * Currently we only unregister ftrace commands from __init, so mark
4632 * this __init too.
4633 */
4634__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004635{
4636 struct ftrace_func_command *p, *n;
4637 int ret = -ENODEV;
4638
4639 mutex_lock(&ftrace_cmd_mutex);
4640 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4641 if (strcmp(cmd->name, p->name) == 0) {
4642 ret = 0;
4643 list_del_init(&p->list);
4644 goto out_unlock;
4645 }
4646 }
4647 out_unlock:
4648 mutex_unlock(&ftrace_cmd_mutex);
4649
4650 return ret;
4651}
4652
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004653static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004654 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004655{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004656 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004657 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004658 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004659 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004660 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004661
4662 func = strsep(&next, ":");
4663
4664 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004665 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004666 if (!ret)
4667 ret = -EINVAL;
4668 if (ret < 0)
4669 return ret;
4670 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004671 }
4672
Steven Rostedtf6180772009-02-14 00:40:25 -05004673 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004674
4675 command = strsep(&next, ":");
4676
Steven Rostedtf6180772009-02-14 00:40:25 -05004677 mutex_lock(&ftrace_cmd_mutex);
4678 list_for_each_entry(p, &ftrace_commands, list) {
4679 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004680 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004681 goto out_unlock;
4682 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004683 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004684 out_unlock:
4685 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004686
Steven Rostedtf6180772009-02-14 00:40:25 -05004687 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004688}
4689
Ingo Molnare309b412008-05-12 21:20:51 +02004690static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004691ftrace_regex_write(struct file *file, const char __user *ubuf,
4692 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004693{
4694 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004695 struct trace_parser *parser;
4696 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004697
Li Zefan4ba79782009-09-22 13:52:20 +08004698 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004699 return 0;
4700
Steven Rostedt5072c592008-05-12 21:20:43 +02004701 if (file->f_mode & FMODE_READ) {
4702 struct seq_file *m = file->private_data;
4703 iter = m->private;
4704 } else
4705 iter = file->private_data;
4706
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004707 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004708 return -ENODEV;
4709
4710 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004711
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004712 parser = &iter->parser;
4713 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004714
Li Zefan4ba79782009-09-22 13:52:20 +08004715 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004716 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004717 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004718 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004719 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004720 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004721 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004722 }
4723
Steven Rostedt5072c592008-05-12 21:20:43 +02004724 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004725 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004726 return ret;
4727}
4728
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004729ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004730ftrace_filter_write(struct file *file, const char __user *ubuf,
4731 size_t cnt, loff_t *ppos)
4732{
4733 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4734}
4735
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004736ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004737ftrace_notrace_write(struct file *file, const char __user *ubuf,
4738 size_t cnt, loff_t *ppos)
4739{
4740 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4741}
4742
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004743static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004744ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4745{
4746 struct ftrace_func_entry *entry;
4747
4748 if (!ftrace_location(ip))
4749 return -EINVAL;
4750
4751 if (remove) {
4752 entry = ftrace_lookup_ip(hash, ip);
4753 if (!entry)
4754 return -ENOENT;
4755 free_hash_entry(hash, entry);
4756 return 0;
4757 }
4758
4759 return add_hash_entry(hash, ip);
4760}
4761
4762static int
4763ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4764 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004765{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004766 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004767 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004768 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004769
Steven Rostedt41c52c02008-05-22 11:46:33 -04004770 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004771 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004772
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004773 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004774
Steven Rostedtf45948e2011-05-02 12:29:25 -04004775 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004776 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004777 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004778 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004779
Wang Nanb972cc52014-07-15 08:40:20 +08004780 if (reset)
4781 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4782 else
4783 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4784
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004785 if (!hash) {
4786 ret = -ENOMEM;
4787 goto out_regex_unlock;
4788 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004789
Jiri Olsaac483c42012-01-02 10:04:14 +01004790 if (buf && !ftrace_match_records(hash, buf, len)) {
4791 ret = -EINVAL;
4792 goto out_regex_unlock;
4793 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004794 if (ip) {
4795 ret = ftrace_match_addr(hash, ip, remove);
4796 if (ret < 0)
4797 goto out_regex_unlock;
4798 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004799
4800 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004801 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004802 mutex_unlock(&ftrace_lock);
4803
Jiri Olsaac483c42012-01-02 10:04:14 +01004804 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004805 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004806
4807 free_ftrace_hash(hash);
4808 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004809}
4810
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004811static int
4812ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4813 int reset, int enable)
4814{
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05304815 return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004816}
4817
4818/**
4819 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4820 * @ops - the ops to set the filter with
4821 * @ip - the address to add to or remove from the filter.
4822 * @remove - non zero to remove the ip from the filter
4823 * @reset - non zero to reset all filters before applying this filter.
4824 *
4825 * Filters denote which functions should be enabled when tracing is enabled
4826 * If @ip is NULL, it failes to update filter.
4827 */
4828int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4829 int remove, int reset)
4830{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004831 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004832 return ftrace_set_addr(ops, ip, remove, reset, 1);
4833}
4834EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4835
Joel Fernandesd032ae82016-11-15 12:31:20 -08004836/**
4837 * ftrace_ops_set_global_filter - setup ops to use global filters
4838 * @ops - the ops which will use the global filters
4839 *
4840 * ftrace users who need global function trace filtering should call this.
4841 * It can set the global filter only if ops were not initialized before.
4842 */
4843void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4844{
4845 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4846 return;
4847
4848 ftrace_ops_init(ops);
4849 ops->func_hash = &global_ops.local_hash;
4850}
4851EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4852
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004853static int
4854ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4855 int reset, int enable)
4856{
4857 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4858}
4859
Steven Rostedt77a2b372008-05-12 21:20:45 +02004860/**
4861 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004862 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004863 * @buf - the string that holds the function filter text.
4864 * @len - the length of the string.
4865 * @reset - non zero to reset all filters before applying this filter.
4866 *
4867 * Filters denote which functions should be enabled when tracing is enabled.
4868 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4869 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004870int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004871 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004872{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004873 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004874 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004875}
Steven Rostedt936e0742011-05-05 22:54:01 -04004876EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004877
Steven Rostedt41c52c02008-05-22 11:46:33 -04004878/**
4879 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004880 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004881 * @buf - the string that holds the function notrace text.
4882 * @len - the length of the string.
4883 * @reset - non zero to reset all filters before applying this filter.
4884 *
4885 * Notrace Filters denote which functions should not be enabled when tracing
4886 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4887 * for tracing.
4888 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004889int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004890 int len, int reset)
4891{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004892 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004893 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004894}
4895EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4896/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004897 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004898 * @buf - the string that holds the function filter text.
4899 * @len - the length of the string.
4900 * @reset - non zero to reset all filters before applying this filter.
4901 *
4902 * Filters denote which functions should be enabled when tracing is enabled.
4903 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4904 */
4905void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4906{
4907 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4908}
4909EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4910
4911/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004912 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004913 * @buf - the string that holds the function notrace text.
4914 * @len - the length of the string.
4915 * @reset - non zero to reset all filters before applying this filter.
4916 *
4917 * Notrace Filters denote which functions should not be enabled when tracing
4918 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4919 * for tracing.
4920 */
4921void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004922{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004923 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004924}
Steven Rostedt936e0742011-05-05 22:54:01 -04004925EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004926
Steven Rostedt2af15d62009-05-28 13:37:24 -04004927/*
4928 * command line interface to allow users to set filters on boot up.
4929 */
4930#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4931static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4932static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4933
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004934/* Used by function selftest to not test if filter is set */
4935bool ftrace_filter_param __initdata;
4936
Steven Rostedt2af15d62009-05-28 13:37:24 -04004937static int __init set_ftrace_notrace(char *str)
4938{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004939 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004940 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004941 return 1;
4942}
4943__setup("ftrace_notrace=", set_ftrace_notrace);
4944
4945static int __init set_ftrace_filter(char *str)
4946{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004947 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004948 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004949 return 1;
4950}
4951__setup("ftrace_filter=", set_ftrace_filter);
4952
Stefan Assmann369bc182009-10-12 22:17:21 +02004953#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004954static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004955static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004956static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004957
Stefan Assmann369bc182009-10-12 22:17:21 +02004958static int __init set_graph_function(char *str)
4959{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004960 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004961 return 1;
4962}
4963__setup("ftrace_graph_filter=", set_graph_function);
4964
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004965static int __init set_graph_notrace_function(char *str)
4966{
4967 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4968 return 1;
4969}
4970__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4971
Todd Brandt65a50c62017-03-02 16:12:15 -08004972static int __init set_graph_max_depth_function(char *str)
4973{
4974 if (!str)
4975 return 0;
4976 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4977 return 1;
4978}
4979__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4980
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004981static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004982{
4983 int ret;
4984 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004985 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004986
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004987 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4988 if (WARN_ON(!hash))
4989 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004990
4991 while (buf) {
4992 func = strsep(&buf, ",");
4993 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004994 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004995 if (ret)
4996 printk(KERN_DEBUG "ftrace: function %s not "
4997 "traceable\n", func);
4998 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004999
5000 if (enable)
5001 ftrace_graph_hash = hash;
5002 else
5003 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02005004}
5005#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5006
Steven Rostedt2a85a372011-12-19 21:57:44 -05005007void __init
5008ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04005009{
5010 char *func;
5011
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005012 ftrace_ops_init(ops);
5013
Steven Rostedt2af15d62009-05-28 13:37:24 -04005014 while (buf) {
5015 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04005016 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005017 }
5018}
5019
5020static void __init set_ftrace_early_filters(void)
5021{
5022 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005023 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005024 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005025 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005026#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5027 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005028 set_ftrace_early_graph(ftrace_graph_buf, 1);
5029 if (ftrace_graph_notrace_buf[0])
5030 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005031#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04005032}
5033
Steven Rostedtfc13cb02011-12-19 14:41:25 -05005034int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02005035{
5036 struct seq_file *m = (struct seq_file *)file->private_data;
5037 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005038 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005039 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04005040 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005041 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02005042
Steven Rostedt5072c592008-05-12 21:20:43 +02005043 if (file->f_mode & FMODE_READ) {
5044 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02005045 seq_release(inode, file);
5046 } else
5047 iter = file->private_data;
5048
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005049 parser = &iter->parser;
5050 if (trace_parser_loaded(parser)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005051 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02005052 }
5053
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005054 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005055
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005056 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005057
Steven Rostedt058e2972011-04-29 22:35:33 -04005058 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04005059 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5060
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005061 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005062 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04005063 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005064 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5065 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005066 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005067
Steven Rostedt058e2972011-04-29 22:35:33 -04005068 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04005069 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5070 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04005071 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04005072 } else {
5073 /* For read only, the hash is the ops hash */
5074 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04005075 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005076
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005077 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005078 free_ftrace_hash(iter->hash);
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04005079 if (iter->tr)
5080 trace_array_put(iter->tr);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005081 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04005082
Steven Rostedt5072c592008-05-12 21:20:43 +02005083 return 0;
5084}
5085
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005086static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005087 .open = ftrace_avail_open,
5088 .read = seq_read,
5089 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08005090 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02005091};
5092
Steven Rostedt647bcd02011-05-03 14:39:21 -04005093static const struct file_operations ftrace_enabled_fops = {
5094 .open = ftrace_enabled_open,
5095 .read = seq_read,
5096 .llseek = seq_lseek,
5097 .release = seq_release_private,
5098};
5099
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005100static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005101 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005102 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005103 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005104 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005105 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005106};
5107
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005108static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005109 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005110 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005111 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005112 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005113 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005114};
5115
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005116#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5117
5118static DEFINE_MUTEX(graph_lock);
5119
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005120struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5121struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5122
5123enum graph_filter_type {
5124 GRAPH_FILTER_NOTRACE = 0,
5125 GRAPH_FILTER_FUNCTION,
5126};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005127
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005128#define FTRACE_GRAPH_EMPTY ((void *)1)
5129
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005130struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005131 struct ftrace_hash *hash;
5132 struct ftrace_func_entry *entry;
5133 int idx; /* for hash table iteration */
5134 enum graph_filter_type type;
5135 struct ftrace_hash *new_hash;
5136 const struct seq_operations *seq_ops;
5137 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005138};
5139
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005140static void *
Li Zefan85951842009-06-24 09:54:00 +08005141__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005142{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005143 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005144 struct ftrace_func_entry *entry = fgd->entry;
5145 struct hlist_head *head;
5146 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005147
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005148 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005149 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005150
5151 if (entry) {
5152 hlist_for_each_entry_continue(entry, hlist) {
5153 fgd->entry = entry;
5154 return entry;
5155 }
5156
5157 idx++;
5158 }
5159
5160 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5161 head = &fgd->hash->buckets[i];
5162 hlist_for_each_entry(entry, head, hlist) {
5163 fgd->entry = entry;
5164 fgd->idx = i;
5165 return entry;
5166 }
5167 }
5168 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005169}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005170
Li Zefan85951842009-06-24 09:54:00 +08005171static void *
5172g_next(struct seq_file *m, void *v, loff_t *pos)
5173{
5174 (*pos)++;
5175 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005176}
5177
5178static void *g_start(struct seq_file *m, loff_t *pos)
5179{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005180 struct ftrace_graph_data *fgd = m->private;
5181
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005182 mutex_lock(&graph_lock);
5183
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005184 if (fgd->type == GRAPH_FILTER_FUNCTION)
5185 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5186 lockdep_is_held(&graph_lock));
5187 else
5188 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5189 lockdep_is_held(&graph_lock));
5190
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005191 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005192 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005193 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005194
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005195 fgd->idx = 0;
5196 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005197 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005198}
5199
5200static void g_stop(struct seq_file *m, void *p)
5201{
5202 mutex_unlock(&graph_lock);
5203}
5204
5205static int g_show(struct seq_file *m, void *v)
5206{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005207 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005208
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005209 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005210 return 0;
5211
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005212 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005213 struct ftrace_graph_data *fgd = m->private;
5214
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005215 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005216 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005217 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005218 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005219 return 0;
5220 }
5221
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005222 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005223
5224 return 0;
5225}
5226
James Morris88e9d342009-09-22 16:43:43 -07005227static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005228 .start = g_start,
5229 .next = g_next,
5230 .stop = g_stop,
5231 .show = g_show,
5232};
5233
5234static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005235__ftrace_graph_open(struct inode *inode, struct file *file,
5236 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005237{
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04005238 int ret;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005239 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005240
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04005241 ret = security_locked_down(LOCKDOWN_TRACEFS);
5242 if (ret)
5243 return ret;
5244
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005245 if (file->f_mode & FMODE_WRITE) {
5246 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5247
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005248 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5249 return -ENOMEM;
5250
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005251 if (file->f_flags & O_TRUNC)
5252 new_hash = alloc_ftrace_hash(size_bits);
5253 else
5254 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5255 fgd->hash);
5256 if (!new_hash) {
5257 ret = -ENOMEM;
5258 goto out;
5259 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005260 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005261
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005262 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005263 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005264 if (!ret) {
5265 struct seq_file *m = file->private_data;
5266 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005267 } else {
5268 /* Failed */
5269 free_ftrace_hash(new_hash);
5270 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005271 }
5272 } else
5273 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005274
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005275out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005276 if (ret < 0 && file->f_mode & FMODE_WRITE)
5277 trace_parser_put(&fgd->parser);
5278
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005279 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005280
5281 /*
5282 * All uses of fgd->hash must be taken with the graph_lock
5283 * held. The graph_lock is going to be released, so force
5284 * fgd->hash to be reinitialized when it is taken again.
5285 */
5286 fgd->hash = NULL;
5287
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005288 return ret;
5289}
5290
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005291static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005292ftrace_graph_open(struct inode *inode, struct file *file)
5293{
5294 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005295 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005296
5297 if (unlikely(ftrace_disabled))
5298 return -ENODEV;
5299
5300 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5301 if (fgd == NULL)
5302 return -ENOMEM;
5303
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005304 mutex_lock(&graph_lock);
5305
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005306 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5307 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005308 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005309 fgd->seq_ops = &ftrace_graph_seq_ops;
5310
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005311 ret = __ftrace_graph_open(inode, file, fgd);
5312 if (ret < 0)
5313 kfree(fgd);
5314
5315 mutex_unlock(&graph_lock);
5316 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005317}
5318
5319static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005320ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5321{
5322 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005323 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005324
5325 if (unlikely(ftrace_disabled))
5326 return -ENODEV;
5327
5328 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5329 if (fgd == NULL)
5330 return -ENOMEM;
5331
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005332 mutex_lock(&graph_lock);
5333
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005334 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5335 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005336 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005337 fgd->seq_ops = &ftrace_graph_seq_ops;
5338
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005339 ret = __ftrace_graph_open(inode, file, fgd);
5340 if (ret < 0)
5341 kfree(fgd);
5342
5343 mutex_unlock(&graph_lock);
5344 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005345}
5346
5347static int
Li Zefan87827112009-07-23 11:29:11 +08005348ftrace_graph_release(struct inode *inode, struct file *file)
5349{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005350 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005351 struct ftrace_hash *old_hash, *new_hash;
5352 struct trace_parser *parser;
5353 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005354
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005355 if (file->f_mode & FMODE_READ) {
5356 struct seq_file *m = file->private_data;
5357
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005358 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005359 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005360 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005361 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005362 }
5363
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005364
5365 if (file->f_mode & FMODE_WRITE) {
5366
5367 parser = &fgd->parser;
5368
5369 if (trace_parser_loaded((parser))) {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005370 ret = ftrace_graph_set_hash(fgd->new_hash,
5371 parser->buffer);
5372 }
5373
5374 trace_parser_put(parser);
5375
5376 new_hash = __ftrace_hash_move(fgd->new_hash);
5377 if (!new_hash) {
5378 ret = -ENOMEM;
5379 goto out;
5380 }
5381
5382 mutex_lock(&graph_lock);
5383
5384 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5385 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5386 lockdep_is_held(&graph_lock));
5387 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5388 } else {
5389 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5390 lockdep_is_held(&graph_lock));
5391 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5392 }
5393
5394 mutex_unlock(&graph_lock);
5395
5396 /* Wait till all users are no longer using the old hash */
Paul E. McKenney74401722018-11-06 18:44:52 -08005397 synchronize_rcu();
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005398
5399 free_ftrace_hash(old_hash);
5400 }
5401
5402 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005403 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005404 kfree(fgd);
5405
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005406 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005407}
5408
5409static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005410ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005411{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005412 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005413 struct dyn_ftrace *rec;
5414 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005415 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005416 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005417 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005418
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005419 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005420 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5421 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005422
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005423 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005424
Steven Rostedt52baf112009-02-14 01:15:39 -05005425 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005426
5427 if (unlikely(ftrace_disabled)) {
5428 mutex_unlock(&ftrace_lock);
5429 return -ENODEV;
5430 }
5431
Steven Rostedt265c8312009-02-13 12:43:56 -05005432 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005433
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005434 if (rec->flags & FTRACE_FL_DISABLED)
5435 continue;
5436
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005437 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005438 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005439
5440 if (!not) {
5441 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005442
5443 if (entry)
5444 continue;
5445 if (add_hash_entry(hash, rec->ip) < 0)
5446 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005447 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005448 if (entry) {
5449 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005450 fail = 0;
5451 }
5452 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005453 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005454 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005455out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005456 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005457
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005458 if (fail)
5459 return -EINVAL;
5460
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005461 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005462}
5463
5464static ssize_t
5465ftrace_graph_write(struct file *file, const char __user *ubuf,
5466 size_t cnt, loff_t *ppos)
5467{
Namhyung Kim6a101082013-10-14 17:24:25 +09005468 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005469 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005470 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005471
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005472 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005473 return 0;
5474
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005475 /* Read mode uses seq functions */
5476 if (file->f_mode & FMODE_READ) {
5477 struct seq_file *m = file->private_data;
5478 fgd = m->private;
5479 }
5480
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005481 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005482
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005483 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005484
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005485 if (read >= 0 && trace_parser_loaded(parser) &&
5486 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005487
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005488 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005489 parser->buffer);
5490 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005491 }
5492
Namhyung Kim6a101082013-10-14 17:24:25 +09005493 if (!ret)
5494 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005495
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005496 return ret;
5497}
5498
5499static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005500 .open = ftrace_graph_open,
5501 .read = seq_read,
5502 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005503 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005504 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005505};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005506
5507static const struct file_operations ftrace_graph_notrace_fops = {
5508 .open = ftrace_graph_notrace_open,
5509 .read = seq_read,
5510 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005511 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005512 .release = ftrace_graph_release,
5513};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005514#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5515
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005516void ftrace_create_filter_files(struct ftrace_ops *ops,
5517 struct dentry *parent)
5518{
5519
5520 trace_create_file("set_ftrace_filter", 0644, parent,
5521 ops, &ftrace_filter_fops);
5522
5523 trace_create_file("set_ftrace_notrace", 0644, parent,
5524 ops, &ftrace_notrace_fops);
5525}
5526
5527/*
5528 * The name "destroy_filter_files" is really a misnomer. Although
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05305529 * in the future, it may actually delete the files, but this is
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005530 * really intended to make sure the ops passed in are disabled
5531 * and that when this function returns, the caller is free to
5532 * free the ops.
5533 *
5534 * The "destroy" name is only to match the "create" name that this
5535 * should be paired with.
5536 */
5537void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5538{
5539 mutex_lock(&ftrace_lock);
5540 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5541 ftrace_shutdown(ops, 0);
5542 ops->flags |= FTRACE_OPS_FL_DELETED;
Steven Rostedt (VMware)2840f842018-12-10 23:58:01 -05005543 ftrace_free_filter(ops);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005544 mutex_unlock(&ftrace_lock);
5545}
5546
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005547static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005548{
Steven Rostedt5072c592008-05-12 21:20:43 +02005549
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005550 trace_create_file("available_filter_functions", 0444,
5551 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005552
Steven Rostedt647bcd02011-05-03 14:39:21 -04005553 trace_create_file("enabled_functions", 0444,
5554 d_tracer, NULL, &ftrace_enabled_fops);
5555
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005556 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005557
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005558#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Chen LinX1ce05002014-09-03 14:31:09 +08005559 trace_create_file("set_graph_function", 0644, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005560 NULL,
5561 &ftrace_graph_fops);
Chen LinX1ce05002014-09-03 14:31:09 +08005562 trace_create_file("set_graph_notrace", 0644, d_tracer,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005563 NULL,
5564 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005565#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5566
Steven Rostedt5072c592008-05-12 21:20:43 +02005567 return 0;
5568}
5569
Steven Rostedt9fd49322012-04-24 22:32:06 -04005570static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005571{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005572 const unsigned long *ipa = a;
5573 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005574
Steven Rostedt9fd49322012-04-24 22:32:06 -04005575 if (*ipa > *ipb)
5576 return 1;
5577 if (*ipa < *ipb)
5578 return -1;
5579 return 0;
5580}
5581
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005582static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005583 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005584 unsigned long *end)
5585{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005586 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005587 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005588 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005589 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005590 unsigned long *p;
5591 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005592 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005593 int ret = -ENOMEM;
5594
5595 count = end - start;
5596
5597 if (!count)
5598 return 0;
5599
Steven Rostedt9fd49322012-04-24 22:32:06 -04005600 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005601 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005602
Steven Rostedt706c81f2012-04-24 23:45:26 -04005603 start_pg = ftrace_allocate_pages(count);
5604 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005605 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005606
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005607 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005608
Steven Rostedt320823092011-12-16 14:42:37 -05005609 /*
5610 * Core and each module needs their own pages, as
5611 * modules will free them when they are removed.
5612 * Force a new page to be allocated for modules.
5613 */
Steven Rostedta7900872011-12-16 16:23:44 -05005614 if (!mod) {
5615 WARN_ON(ftrace_pages || ftrace_pages_start);
5616 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005617 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005618 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005619 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005620 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005621
Steven Rostedta7900872011-12-16 16:23:44 -05005622 if (WARN_ON(ftrace_pages->next)) {
5623 /* Hmm, we have free pages? */
5624 while (ftrace_pages->next)
5625 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005626 }
Steven Rostedta7900872011-12-16 16:23:44 -05005627
Steven Rostedt706c81f2012-04-24 23:45:26 -04005628 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005629 }
5630
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005631 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005632 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005633 while (p < end) {
5634 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005635 /*
5636 * Some architecture linkers will pad between
5637 * the different mcount_loc sections of different
5638 * object files to satisfy alignments.
5639 * Skip any NULL pointers.
5640 */
5641 if (!addr)
5642 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005643
5644 if (pg->index == pg->size) {
5645 /* We should have allocated enough */
5646 if (WARN_ON(!pg->next))
5647 break;
5648 pg = pg->next;
5649 }
5650
5651 rec = &pg->records[pg->index++];
5652 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005653 }
5654
Steven Rostedt706c81f2012-04-24 23:45:26 -04005655 /* We should have used all pages */
5656 WARN_ON(pg->next);
5657
5658 /* Assign the last page to ftrace_pages */
5659 ftrace_pages = pg;
5660
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005661 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005662 * We only need to disable interrupts on start up
5663 * because we are modifying code that an interrupt
5664 * may execute, and the modification is not atomic.
5665 * But for modules, nothing runs the code we modify
5666 * until we are finished with it, and there's no
5667 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005668 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005669 if (!mod)
5670 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005671 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005672 if (!mod)
5673 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005674 ret = 0;
5675 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005676 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005677
Steven Rostedta7900872011-12-16 16:23:44 -05005678 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005679}
5680
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005681struct ftrace_mod_func {
5682 struct list_head list;
5683 char *name;
5684 unsigned long ip;
5685 unsigned int size;
5686};
5687
5688struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005689 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005690 struct list_head list;
5691 struct module *mod;
5692 unsigned long start_addr;
5693 unsigned long end_addr;
5694 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005695 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005696};
5697
Steven Rostedt93eb6772009-04-15 13:24:06 -04005698#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005699
5700#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5701
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005702static LIST_HEAD(ftrace_mod_maps);
5703
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005704static int referenced_filters(struct dyn_ftrace *rec)
5705{
5706 struct ftrace_ops *ops;
5707 int cnt = 0;
5708
5709 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5710 if (ops_references_rec(ops, rec))
5711 cnt++;
5712 }
5713
5714 return cnt;
5715}
5716
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005717static void
5718clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5719{
5720 struct ftrace_func_entry *entry;
5721 struct dyn_ftrace *rec;
5722 int i;
5723
5724 if (ftrace_hash_empty(hash))
5725 return;
5726
5727 for (i = 0; i < pg->index; i++) {
5728 rec = &pg->records[i];
5729 entry = __ftrace_lookup_ip(hash, rec->ip);
5730 /*
5731 * Do not allow this rec to match again.
5732 * Yeah, it may waste some memory, but will be removed
5733 * if/when the hash is modified again.
5734 */
5735 if (entry)
5736 entry->ip = 0;
5737 }
5738}
5739
5740/* Clear any records from hashs */
5741static void clear_mod_from_hashes(struct ftrace_page *pg)
5742{
5743 struct trace_array *tr;
5744
5745 mutex_lock(&trace_types_lock);
5746 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5747 if (!tr->ops || !tr->ops->func_hash)
5748 continue;
5749 mutex_lock(&tr->ops->func_hash->regex_lock);
5750 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5751 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5752 mutex_unlock(&tr->ops->func_hash->regex_lock);
5753 }
5754 mutex_unlock(&trace_types_lock);
5755}
5756
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005757static void ftrace_free_mod_map(struct rcu_head *rcu)
5758{
5759 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5760 struct ftrace_mod_func *mod_func;
5761 struct ftrace_mod_func *n;
5762
5763 /* All the contents of mod_map are now not visible to readers */
5764 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5765 kfree(mod_func->name);
5766 list_del(&mod_func->list);
5767 kfree(mod_func);
5768 }
5769
5770 kfree(mod_map);
5771}
5772
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005773void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005774{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005775 struct ftrace_mod_map *mod_map;
5776 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005777 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005778 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005779 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005780 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005781 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005782
Steven Rostedt93eb6772009-04-15 13:24:06 -04005783 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005784
5785 if (ftrace_disabled)
5786 goto out_unlock;
5787
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005788 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5789 if (mod_map->mod == mod) {
5790 list_del_rcu(&mod_map->list);
Paul E. McKenney74401722018-11-06 18:44:52 -08005791 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005792 break;
5793 }
5794 }
5795
Steven Rostedt320823092011-12-16 14:42:37 -05005796 /*
5797 * Each module has its own ftrace_pages, remove
5798 * them from the list.
5799 */
5800 last_pg = &ftrace_pages_start;
5801 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5802 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005803 if (within_module_core(rec->ip, mod) ||
5804 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005805 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005806 * As core pages are first, the first
5807 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005808 */
Steven Rostedt320823092011-12-16 14:42:37 -05005809 if (WARN_ON(pg == ftrace_pages_start))
5810 goto out_unlock;
5811
5812 /* Check if we are deleting the last page */
5813 if (pg == ftrace_pages)
5814 ftrace_pages = next_to_ftrace_page(last_pg);
5815
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04005816 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05005817 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005818
5819 pg->next = tmp_page;
5820 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005821 } else
5822 last_pg = &pg->next;
5823 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005824 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005825 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005826
5827 for (pg = tmp_page; pg; pg = tmp_page) {
5828
5829 /* Needs to be called outside of ftrace_lock */
5830 clear_mod_from_hashes(pg);
5831
5832 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5833 free_pages((unsigned long)pg->records, order);
5834 tmp_page = pg->next;
5835 kfree(pg);
5836 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04005837}
5838
Jessica Yu7dcd1822016-02-16 17:32:33 -05005839void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005840{
5841 struct dyn_ftrace *rec;
5842 struct ftrace_page *pg;
5843
5844 mutex_lock(&ftrace_lock);
5845
5846 if (ftrace_disabled)
5847 goto out_unlock;
5848
5849 /*
5850 * If the tracing is enabled, go ahead and enable the record.
5851 *
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05305852 * The reason not to enable the record immediately is the
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005853 * inherent check of ftrace_make_nop/ftrace_make_call for
5854 * correct previous instructions. Making first the NOP
5855 * conversion puts the module to the correct state, thus
5856 * passing the ftrace_make_call check.
5857 *
5858 * We also delay this to after the module code already set the
5859 * text to read-only, as we now need to set it back to read-write
5860 * so that we can modify the text.
5861 */
5862 if (ftrace_start_up)
5863 ftrace_arch_code_modify_prepare();
5864
5865 do_for_each_ftrace_rec(pg, rec) {
5866 int cnt;
5867 /*
5868 * do_for_each_ftrace_rec() is a double loop.
5869 * module text shares the pg. If a record is
5870 * not part of this module, then skip this pg,
5871 * which the "break" will do.
5872 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005873 if (!within_module_core(rec->ip, mod) &&
5874 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005875 break;
5876
5877 cnt = 0;
5878
5879 /*
5880 * When adding a module, we need to check if tracers are
5881 * currently enabled and if they are, and can trace this record,
5882 * we need to enable the module functions as well as update the
5883 * reference counts for those function records.
5884 */
5885 if (ftrace_start_up)
5886 cnt += referenced_filters(rec);
5887
5888 /* This clears FTRACE_FL_DISABLED */
5889 rec->flags = cnt;
5890
5891 if (ftrace_start_up && cnt) {
5892 int failed = __ftrace_replace_code(rec, 1);
5893 if (failed) {
5894 ftrace_bug(failed, rec);
5895 goto out_loop;
5896 }
5897 }
5898
5899 } while_for_each_ftrace_rec();
5900
5901 out_loop:
5902 if (ftrace_start_up)
5903 ftrace_arch_code_modify_post_process();
5904
5905 out_unlock:
5906 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04005907
5908 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005909}
5910
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005911void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005912{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005913 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005914 return;
5915
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005916 ftrace_process_locs(mod, mod->ftrace_callsites,
5917 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005918}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005919
5920static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5921 struct dyn_ftrace *rec)
5922{
5923 struct ftrace_mod_func *mod_func;
5924 unsigned long symsize;
5925 unsigned long offset;
5926 char str[KSYM_SYMBOL_LEN];
5927 char *modname;
5928 const char *ret;
5929
5930 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5931 if (!ret)
5932 return;
5933
5934 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5935 if (!mod_func)
5936 return;
5937
5938 mod_func->name = kstrdup(str, GFP_KERNEL);
5939 if (!mod_func->name) {
5940 kfree(mod_func);
5941 return;
5942 }
5943
5944 mod_func->ip = rec->ip - offset;
5945 mod_func->size = symsize;
5946
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005947 mod_map->num_funcs++;
5948
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005949 list_add_rcu(&mod_func->list, &mod_map->funcs);
5950}
5951
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005952static struct ftrace_mod_map *
5953allocate_ftrace_mod_map(struct module *mod,
5954 unsigned long start, unsigned long end)
5955{
5956 struct ftrace_mod_map *mod_map;
5957
5958 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5959 if (!mod_map)
5960 return NULL;
5961
5962 mod_map->mod = mod;
5963 mod_map->start_addr = start;
5964 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005965 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005966
5967 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5968
5969 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5970
5971 return mod_map;
5972}
5973
5974static const char *
5975ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5976 unsigned long addr, unsigned long *size,
5977 unsigned long *off, char *sym)
5978{
5979 struct ftrace_mod_func *found_func = NULL;
5980 struct ftrace_mod_func *mod_func;
5981
5982 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5983 if (addr >= mod_func->ip &&
5984 addr < mod_func->ip + mod_func->size) {
5985 found_func = mod_func;
5986 break;
5987 }
5988 }
5989
5990 if (found_func) {
5991 if (size)
5992 *size = found_func->size;
5993 if (off)
5994 *off = addr - found_func->ip;
5995 if (sym)
5996 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5997
5998 return found_func->name;
5999 }
6000
6001 return NULL;
6002}
6003
6004const char *
6005ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6006 unsigned long *off, char **modname, char *sym)
6007{
6008 struct ftrace_mod_map *mod_map;
6009 const char *ret = NULL;
6010
Paul E. McKenney74401722018-11-06 18:44:52 -08006011 /* mod_map is freed via call_rcu() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006012 preempt_disable();
6013 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6014 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6015 if (ret) {
6016 if (modname)
6017 *modname = mod_map->mod->name;
6018 break;
6019 }
6020 }
6021 preempt_enable();
6022
6023 return ret;
6024}
6025
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006026int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6027 char *type, char *name,
6028 char *module_name, int *exported)
6029{
6030 struct ftrace_mod_map *mod_map;
6031 struct ftrace_mod_func *mod_func;
6032
6033 preempt_disable();
6034 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6035
6036 if (symnum >= mod_map->num_funcs) {
6037 symnum -= mod_map->num_funcs;
6038 continue;
6039 }
6040
6041 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6042 if (symnum > 1) {
6043 symnum--;
6044 continue;
6045 }
6046
6047 *value = mod_func->ip;
6048 *type = 'T';
6049 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6050 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6051 *exported = 1;
6052 preempt_enable();
6053 return 0;
6054 }
6055 WARN_ON(1);
6056 break;
6057 }
6058 preempt_enable();
6059 return -ERANGE;
6060}
6061
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006062#else
6063static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6064 struct dyn_ftrace *rec) { }
6065static inline struct ftrace_mod_map *
6066allocate_ftrace_mod_map(struct module *mod,
6067 unsigned long start, unsigned long end)
6068{
6069 return NULL;
6070}
Steven Rostedt93eb6772009-04-15 13:24:06 -04006071#endif /* CONFIG_MODULES */
6072
Joel Fernandes8715b102017-10-09 12:29:31 -07006073struct ftrace_init_func {
6074 struct list_head list;
6075 unsigned long ip;
6076};
6077
6078/* Clear any init ips from hashes */
6079static void
6080clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006081{
Joel Fernandes8715b102017-10-09 12:29:31 -07006082 struct ftrace_func_entry *entry;
6083
Changbin Du08468752019-09-10 22:33:36 +08006084 entry = ftrace_lookup_ip(hash, func->ip);
Joel Fernandes8715b102017-10-09 12:29:31 -07006085 /*
6086 * Do not allow this rec to match again.
6087 * Yeah, it may waste some memory, but will be removed
6088 * if/when the hash is modified again.
6089 */
6090 if (entry)
6091 entry->ip = 0;
6092}
6093
6094static void
6095clear_func_from_hashes(struct ftrace_init_func *func)
6096{
6097 struct trace_array *tr;
6098
6099 mutex_lock(&trace_types_lock);
6100 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6101 if (!tr->ops || !tr->ops->func_hash)
6102 continue;
6103 mutex_lock(&tr->ops->func_hash->regex_lock);
6104 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6105 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6106 mutex_unlock(&tr->ops->func_hash->regex_lock);
6107 }
6108 mutex_unlock(&trace_types_lock);
6109}
6110
6111static void add_to_clear_hash_list(struct list_head *clear_list,
6112 struct dyn_ftrace *rec)
6113{
6114 struct ftrace_init_func *func;
6115
6116 func = kmalloc(sizeof(*func), GFP_KERNEL);
6117 if (!func) {
6118 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6119 return;
6120 }
6121
6122 func->ip = rec->ip;
6123 list_add(&func->list, clear_list);
6124}
6125
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006126void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006127{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006128 unsigned long start = (unsigned long)(start_ptr);
6129 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006130 struct ftrace_page **last_pg = &ftrace_pages_start;
6131 struct ftrace_page *pg;
6132 struct dyn_ftrace *rec;
6133 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006134 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07006135 struct ftrace_init_func *func, *func_next;
6136 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006137 int order;
6138
Joel Fernandes8715b102017-10-09 12:29:31 -07006139 INIT_LIST_HEAD(&clear_hash);
6140
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006141 key.ip = start;
6142 key.flags = end; /* overload flags, as it is unsigned long */
6143
6144 mutex_lock(&ftrace_lock);
6145
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006146 /*
6147 * If we are freeing module init memory, then check if
6148 * any tracer is active. If so, we need to save a mapping of
6149 * the module functions being freed with the address.
6150 */
6151 if (mod && ftrace_ops_list != &ftrace_list_end)
6152 mod_map = allocate_ftrace_mod_map(mod, start, end);
6153
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006154 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6155 if (end < pg->records[0].ip ||
6156 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6157 continue;
6158 again:
6159 rec = bsearch(&key, pg->records, pg->index,
6160 sizeof(struct dyn_ftrace),
6161 ftrace_cmp_recs);
6162 if (!rec)
6163 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006164
Joel Fernandes8715b102017-10-09 12:29:31 -07006165 /* rec will be cleared from hashes after ftrace_lock unlock */
6166 add_to_clear_hash_list(&clear_hash, rec);
6167
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006168 if (mod_map)
6169 save_ftrace_mod_rec(mod_map, rec);
6170
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006171 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04006172 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006173 if (!pg->index) {
6174 *last_pg = pg->next;
6175 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6176 free_pages((unsigned long)pg->records, order);
6177 kfree(pg);
6178 pg = container_of(last_pg, struct ftrace_page, next);
6179 if (!(*last_pg))
6180 ftrace_pages = pg;
6181 continue;
6182 }
6183 memmove(rec, rec + 1,
6184 (pg->index - (rec - pg->records)) * sizeof(*rec));
6185 /* More than one function may be in this block */
6186 goto again;
6187 }
6188 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07006189
6190 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6191 clear_func_from_hashes(func);
6192 kfree(func);
6193 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006194}
6195
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006196void __init ftrace_free_init_mem(void)
6197{
6198 void *start = (void *)(&__init_begin);
6199 void *end = (void *)(&__init_end);
6200
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006201 ftrace_free_mem(NULL, start, end);
Steven Rostedt93eb6772009-04-15 13:24:06 -04006202}
6203
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006204void __init ftrace_init(void)
6205{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006206 extern unsigned long __start_mcount_loc[];
6207 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006208 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006209 int ret;
6210
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006211 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006212 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006213 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01006214 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006215 goto failed;
6216
6217 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006218 if (!count) {
6219 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006220 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006221 }
6222
6223 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6224 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006225
6226 last_ftrace_enabled = ftrace_enabled = 1;
6227
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006228 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08006229 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006230 __stop_mcount_loc);
6231
Steven Rostedt2af15d62009-05-28 13:37:24 -04006232 set_ftrace_early_filters();
6233
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006234 return;
6235 failed:
6236 ftrace_disabled = 1;
6237}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006238
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006239/* Do nothing if arch does not support this */
6240void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6241{
6242}
6243
6244static void ftrace_update_trampoline(struct ftrace_ops *ops)
6245{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006246 arch_ftrace_update_trampoline(ops);
6247}
6248
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006249void ftrace_init_trace_array(struct trace_array *tr)
6250{
6251 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04006252 INIT_LIST_HEAD(&tr->mod_trace);
6253 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006254}
Steven Rostedt3d083392008-05-12 21:20:42 +02006255#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006256
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05006257struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04006258 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006259 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6260 FTRACE_OPS_FL_INITIALIZED |
6261 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04006262};
6263
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006264static int __init ftrace_nodyn_init(void)
6265{
6266 ftrace_enabled = 1;
6267 return 0;
6268}
Steven Rostedt6f415672012-10-05 12:13:07 -04006269core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006270
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006271static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006272static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006273static inline void ftrace_startup_all(int command) { }
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006274
Ingo Molnarc7aafc52008-05-12 21:20:45 +02006275# define ftrace_startup_sysctl() do { } while (0)
6276# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04006277
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006278static void ftrace_update_trampoline(struct ftrace_ops *ops)
6279{
6280}
6281
Steven Rostedt3d083392008-05-12 21:20:42 +02006282#endif /* CONFIG_DYNAMIC_FTRACE */
6283
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006284__init void ftrace_init_global_array_ops(struct trace_array *tr)
6285{
6286 tr->ops = &global_ops;
6287 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006288 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006289}
6290
6291void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6292{
6293 /* If we filter on pids, update to use the pid function */
6294 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6295 if (WARN_ON(tr->ops->func != ftrace_stub))
6296 printk("ftrace ops had %pS for function\n",
6297 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006298 }
6299 tr->ops->func = func;
6300 tr->ops->private = tr;
6301}
6302
6303void ftrace_reset_array_ops(struct trace_array *tr)
6304{
6305 tr->ops->func = ftrace_stub;
6306}
6307
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006308static nokprobe_inline void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006309__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006310 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006311{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006312 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006313 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04006314
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006315 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6316 if (bit < 0)
6317 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04006318
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006319 /*
6320 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08006321 * they must be freed after a synchronize_rcu().
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006322 */
6323 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006324
Steven Rostedt0a016402012-11-02 17:03:03 -04006325 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (VMware)2fa717a2019-04-11 11:46:13 -04006326 /* Stub functions don't need to be called nor tested */
6327 if (op->flags & FTRACE_OPS_FL_STUB)
6328 continue;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006329 /*
6330 * Check the following for each ops before calling their func:
6331 * if RCU flag is set, then rcu_is_watching() must be true
6332 * if PER_CPU is set, then ftrace_function_local_disable()
6333 * must be false
6334 * Otherwise test if the ip matches the ops filter
6335 *
6336 * If any of the above fails then the op->func() is not executed.
6337 */
6338 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006339 ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04006340 if (FTRACE_WARN_ON(!op->func)) {
6341 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006342 goto out;
6343 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04006344 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006345 }
Steven Rostedt0a016402012-11-02 17:03:03 -04006346 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006347out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006348 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006349 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04006350}
6351
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006352/*
6353 * Some archs only support passing ip and parent_ip. Even though
6354 * the list function ignores the op parameter, we do not want any
6355 * C side effects, where a function is called without the caller
6356 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006357 * Archs are to support both the regs and ftrace_ops at the same time.
6358 * If they support ftrace_ops, it is assumed they support regs.
6359 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09006360 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6361 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006362 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08006363 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006364 */
6365#if ARCH_SUPPORTS_FTRACE_OPS
6366static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006367 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006368{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006369 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006370}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006371NOKPROBE_SYMBOL(ftrace_ops_list_func);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006372#else
6373static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6374{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006375 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006376}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006377NOKPROBE_SYMBOL(ftrace_ops_no_ops);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006378#endif
6379
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006380/*
6381 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006382 * recursion, needs RCU protection and/or requires per cpu handling, then
6383 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006384 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006385static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006386 struct ftrace_ops *op, struct pt_regs *regs)
6387{
6388 int bit;
6389
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006390 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6391 return;
6392
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006393 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6394 if (bit < 0)
6395 return;
6396
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006397 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006398
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006399 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006400
6401 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006402 trace_clear_recursion(bit);
6403}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006404NOKPROBE_SYMBOL(ftrace_ops_assist_func);
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006405
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006406/**
6407 * ftrace_ops_get_func - get the function a trampoline should call
6408 * @ops: the ops to get the function for
6409 *
6410 * Normally the mcount trampoline will call the ops->func, but there
6411 * are times that it should not. For example, if the ops does not
6412 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08006413 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006414 *
6415 * Returns the function that the trampoline should call for @ops.
6416 */
6417ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6418{
6419 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006420 * If the function does not handle recursion, needs to be RCU safe,
6421 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006422 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006423 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006424 ops->flags & FTRACE_OPS_FL_RCU)
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006425 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006426
6427 return ops->func;
6428}
6429
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006430static void
6431ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6432 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05006433{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006434 struct trace_array *tr = data;
6435 struct trace_pid_list *pid_list;
6436
6437 pid_list = rcu_dereference_sched(tr->function_pids);
6438
6439 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6440 trace_ignore_this_task(pid_list, next));
6441}
6442
Namhyung Kim1e104862017-04-17 11:44:28 +09006443static void
6444ftrace_pid_follow_sched_process_fork(void *data,
6445 struct task_struct *self,
6446 struct task_struct *task)
6447{
6448 struct trace_pid_list *pid_list;
6449 struct trace_array *tr = data;
6450
6451 pid_list = rcu_dereference_sched(tr->function_pids);
6452 trace_filter_add_remove_task(pid_list, self, task);
6453}
6454
6455static void
6456ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6457{
6458 struct trace_pid_list *pid_list;
6459 struct trace_array *tr = data;
6460
6461 pid_list = rcu_dereference_sched(tr->function_pids);
6462 trace_filter_add_remove_task(pid_list, NULL, task);
6463}
6464
6465void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6466{
6467 if (enable) {
6468 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6469 tr);
6470 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6471 tr);
6472 } else {
6473 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6474 tr);
6475 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6476 tr);
6477 }
6478}
6479
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006480static void clear_ftrace_pids(struct trace_array *tr)
6481{
6482 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05006483 int cpu;
6484
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006485 pid_list = rcu_dereference_protected(tr->function_pids,
6486 lockdep_is_held(&ftrace_lock));
6487 if (!pid_list)
6488 return;
6489
6490 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6491
6492 for_each_possible_cpu(cpu)
6493 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6494
6495 rcu_assign_pointer(tr->function_pids, NULL);
6496
6497 /* Wait till all users are no longer using pid filtering */
Paul E. McKenney74401722018-11-06 18:44:52 -08006498 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006499
6500 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05006501}
6502
Namhyung Kimd879d0b2017-04-17 11:44:27 +09006503void ftrace_clear_pids(struct trace_array *tr)
6504{
6505 mutex_lock(&ftrace_lock);
6506
6507 clear_ftrace_pids(tr);
6508
6509 mutex_unlock(&ftrace_lock);
6510}
6511
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006512static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05006513{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006514 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006515 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006516
6517 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006518 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006519
6520 mutex_unlock(&ftrace_lock);
6521}
6522
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006523/* Greater than any max PID */
6524#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6525
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006526static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006527 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006528{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006529 struct trace_pid_list *pid_list;
6530 struct trace_array *tr = m->private;
6531
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006532 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006533 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006534
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006535 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006536
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006537 if (!pid_list)
6538 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6539
6540 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006541}
6542
6543static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6544{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006545 struct trace_array *tr = m->private;
6546 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6547
6548 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006549 return NULL;
6550
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006551 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006552}
6553
6554static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006555 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006556{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006557 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006558 mutex_unlock(&ftrace_lock);
6559}
6560
6561static int fpid_show(struct seq_file *m, void *v)
6562{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006563 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006564 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006565 return 0;
6566 }
6567
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006568 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006569}
6570
6571static const struct seq_operations ftrace_pid_sops = {
6572 .start = fpid_start,
6573 .next = fpid_next,
6574 .stop = fpid_stop,
6575 .show = fpid_show,
6576};
6577
6578static int
6579ftrace_pid_open(struct inode *inode, struct file *file)
6580{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006581 struct trace_array *tr = inode->i_private;
6582 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006583 int ret = 0;
6584
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04006585 ret = tracing_check_open_get_tr(tr);
6586 if (ret)
6587 return ret;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006588
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006589 if ((file->f_mode & FMODE_WRITE) &&
6590 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006591 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006592
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006593 ret = seq_open(file, &ftrace_pid_sops);
6594 if (ret < 0) {
6595 trace_array_put(tr);
6596 } else {
6597 m = file->private_data;
6598 /* copy tr over to seq ops */
6599 m->private = tr;
6600 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006601
6602 return ret;
6603}
6604
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006605static void ignore_task_cpu(void *data)
6606{
6607 struct trace_array *tr = data;
6608 struct trace_pid_list *pid_list;
6609
6610 /*
6611 * This function is called by on_each_cpu() while the
6612 * event_mutex is held.
6613 */
6614 pid_list = rcu_dereference_protected(tr->function_pids,
6615 mutex_is_locked(&ftrace_lock));
6616
6617 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6618 trace_ignore_this_task(pid_list, current));
6619}
6620
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006621static ssize_t
6622ftrace_pid_write(struct file *filp, const char __user *ubuf,
6623 size_t cnt, loff_t *ppos)
6624{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006625 struct seq_file *m = filp->private_data;
6626 struct trace_array *tr = m->private;
6627 struct trace_pid_list *filtered_pids = NULL;
6628 struct trace_pid_list *pid_list;
6629 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006630
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006631 if (!cnt)
6632 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006633
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006634 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006635
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006636 filtered_pids = rcu_dereference_protected(tr->function_pids,
6637 lockdep_is_held(&ftrace_lock));
6638
6639 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6640 if (ret < 0)
6641 goto out;
6642
6643 rcu_assign_pointer(tr->function_pids, pid_list);
6644
6645 if (filtered_pids) {
Paul E. McKenney74401722018-11-06 18:44:52 -08006646 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006647 trace_free_pid_list(filtered_pids);
6648 } else if (pid_list) {
6649 /* Register a probe to set whether to ignore the tracing of a task */
6650 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6651 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006652
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006653 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006654 * Ignoring of pids is done at task switch. But we have to
6655 * check for those tasks that are currently running.
6656 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006657 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006658 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006659
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006660 ftrace_update_pid_func();
6661 ftrace_startup_all(0);
6662 out:
6663 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006664
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006665 if (ret > 0)
6666 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05006667
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006668 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006669}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006670
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006671static int
6672ftrace_pid_release(struct inode *inode, struct file *file)
6673{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006674 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006675
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006676 trace_array_put(tr);
6677
6678 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006679}
6680
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006681static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006682 .open = ftrace_pid_open,
6683 .write = ftrace_pid_write,
6684 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006685 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006686 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006687};
6688
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006689void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006690{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006691 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006692 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006693}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006694
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04006695void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6696 struct dentry *d_tracer)
6697{
6698 /* Only the top level directory has the dyn_tracefs and profile */
6699 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6700
6701 ftrace_init_dyn_tracefs(d_tracer);
6702 ftrace_profile_tracefs(d_tracer);
6703}
6704
Steven Rostedt3d083392008-05-12 21:20:42 +02006705/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006706 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006707 *
6708 * This function should be used by panic code. It stops ftrace
6709 * but in a not so nice way. If you need to simply kill ftrace
6710 * from a non-atomic section, use ftrace_kill.
6711 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006712void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006713{
6714 ftrace_disabled = 1;
6715 ftrace_enabled = 0;
Yisheng Xie5ccba642018-02-02 10:14:49 +08006716 ftrace_trace_function = ftrace_stub;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006717}
6718
6719/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04006720 * Test if ftrace is dead or not.
6721 */
6722int ftrace_is_dead(void)
6723{
6724 return ftrace_disabled;
6725}
6726
6727/**
Steven Rostedt3d083392008-05-12 21:20:42 +02006728 * register_ftrace_function - register a function for profiling
6729 * @ops - ops structure that holds the function for profiling.
6730 *
6731 * Register a function to be called by all functions in the
6732 * kernel.
6733 *
6734 * Note: @ops->func and all the functions it calls must be labeled
6735 * with "notrace", otherwise it will go into a
6736 * recursive loop.
6737 */
6738int register_ftrace_function(struct ftrace_ops *ops)
6739{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006740 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006741
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09006742 ftrace_ops_init(ops);
6743
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006744 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006745
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006746 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04006747
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006748 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02006749
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006750 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02006751}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006752EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02006753
6754/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01006755 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02006756 * @ops - ops structure that holds the function to unregister
6757 *
6758 * Unregister a function that was added to be called by ftrace profiling.
6759 */
6760int unregister_ftrace_function(struct ftrace_ops *ops)
6761{
6762 int ret;
6763
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006764 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006765 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006766 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006767
6768 return ret;
6769}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006770EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006771
Miroslav Benes71624312019-10-16 13:33:13 +02006772static bool is_permanent_ops_registered(void)
6773{
6774 struct ftrace_ops *op;
6775
6776 do_for_each_ftrace_op(op, ftrace_ops_list) {
6777 if (op->flags & FTRACE_OPS_FL_PERMANENT)
6778 return true;
6779 } while_for_each_ftrace_op(op);
6780
6781 return false;
6782}
6783
Ingo Molnare309b412008-05-12 21:20:51 +02006784int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006785ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006786 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006787 loff_t *ppos)
6788{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006789 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006790
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006791 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006792
Steven Rostedt45a4a232011-04-21 23:16:46 -04006793 if (unlikely(ftrace_disabled))
6794 goto out;
6795
6796 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006797
Li Zefana32c7762009-06-26 16:55:51 +08006798 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006799 goto out;
6800
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006801 if (ftrace_enabled) {
6802
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006803 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08006804 if (rcu_dereference_protected(ftrace_ops_list,
6805 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01006806 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006807
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006808 ftrace_startup_sysctl();
6809
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006810 } else {
Miroslav Benes71624312019-10-16 13:33:13 +02006811 if (is_permanent_ops_registered()) {
6812 ftrace_enabled = true;
6813 ret = -EBUSY;
6814 goto out;
6815 }
6816
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006817 /* stopping ftrace calls (just send to ftrace_stub) */
6818 ftrace_trace_function = ftrace_stub;
6819
6820 ftrace_shutdown_sysctl();
6821 }
6822
Miroslav Benes71624312019-10-16 13:33:13 +02006823 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006824 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006825 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006826 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006827}