blob: 7efbc8aaf7f647dfc32bad4f98533d5ab2cd3273 [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 */
Steven Rostedt33dc9b12011-05-02 17:34:47 -040065#define FTRACE_HASH_DEFAULT_BITS 10
66#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050067
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090068#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040069#define INIT_OPS_HASH(opsname) \
70 .func_hash = &opsname.local_hash, \
71 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090072#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040073#define INIT_OPS_HASH(opsname)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090074#endif
75
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -050076enum {
77 FTRACE_MODIFY_ENABLE_FL = (1 << 0),
78 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
79};
80
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050081struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040082 .func = ftrace_stub,
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -050083 .flags = FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040084 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040085};
86
Steven Rostedt4eebcc82008-05-12 21:20:48 +020087/* ftrace_enabled is a method to turn ftrace on or off */
88int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020089static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020090
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040091/* Current function tracing op */
92struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050093/* What to set function_trace_op to */
94static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050095
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040096static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040097{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040098 struct trace_array *tr;
99
100 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
101 return false;
102
103 tr = ops->private;
104
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -0400105 return tr->function_pids != NULL || tr->function_no_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400106}
107
108static void ftrace_update_trampoline(struct ftrace_ops *ops);
109
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200110/*
111 * ftrace_disabled is set when an anomaly is discovered.
112 * ftrace_disabled is much stronger than ftrace_enabled.
113 */
114static int ftrace_disabled __read_mostly;
115
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500116DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200117
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500118struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200119ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500120struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200121
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122#if ARCH_SUPPORTS_FTRACE_OPS
123static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -0400124 struct ftrace_ops *op, struct ftrace_regs *fregs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400125#else
126/* See comment below, where ftrace_ops_list_func is defined */
127static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
128#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
129#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400130
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900131static inline void ftrace_ops_init(struct ftrace_ops *ops)
132{
133#ifdef CONFIG_DYNAMIC_FTRACE
134 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400135 mutex_init(&ops->local_hash.regex_lock);
136 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900137 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
138 }
139#endif
140}
141
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400142static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -0400143 struct ftrace_ops *op, struct ftrace_regs *fregs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500144{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400145 struct trace_array *tr = op->private;
Steven Rostedt (VMware)717e3f52020-03-19 23:40:40 -0400146 int pid;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400147
Steven Rostedt (VMware)717e3f52020-03-19 23:40:40 -0400148 if (tr) {
149 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
150 if (pid == FTRACE_PID_IGNORE)
151 return;
152 if (pid != FTRACE_PID_TRACE &&
153 pid != current->pid)
154 return;
155 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500156
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -0400157 op->saved_func(ip, parent_ip, op, fregs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500158}
159
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500160static void ftrace_sync_ipi(void *data)
161{
162 /* Probably not needed, but do it anyway */
163 smp_rmb();
164}
165
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100166static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
167{
168 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500169 * 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 +0100170 * then it needs to call the list anyway.
171 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +0200172 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
173 FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100174 return ftrace_ops_list_func;
175
176 return ftrace_ops_get_func(ops);
177}
178
Steven Rostedt2b499382011-05-03 22:49:52 -0400179static void update_ftrace_function(void)
180{
181 ftrace_func_t func;
182
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400183 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400184 * Prepare the ftrace_ops that the arch callback will use.
185 * If there's only one ftrace_ops registered, the ftrace_ops_list
186 * will point to the ops we want.
187 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800188 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
189 lockdep_is_held(&ftrace_lock));
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400190
191 /* If there's no ftrace_ops registered, just call the stub function */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800192 if (set_function_trace_op == &ftrace_list_end) {
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400193 func = ftrace_stub;
194
195 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400196 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400197 * recursion safe and not dynamic and the arch supports passing ops,
198 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400199 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800200 } else if (rcu_dereference_protected(ftrace_ops_list->next,
201 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100202 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400203
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400204 } else {
205 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500206 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400207 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400208 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400209
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400210 update_function_graph_func();
211
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500212 /* If there's no change, then do nothing more here */
213 if (ftrace_trace_function == func)
214 return;
215
216 /*
217 * If we are using the list function, it doesn't care
218 * about the function_trace_ops.
219 */
220 if (func == ftrace_ops_list_func) {
221 ftrace_trace_function = func;
222 /*
223 * Don't even bother setting function_trace_ops,
224 * it would be racy to do so anyway.
225 */
226 return;
227 }
228
229#ifndef CONFIG_DYNAMIC_FTRACE
230 /*
231 * For static tracing, we need to be a bit more careful.
232 * The function change takes affect immediately. Thus,
Qiujun Huangfdda88d2020-10-02 22:31:26 +0800233 * we need to coordinate the setting of the function_trace_ops
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500234 * with the setting of the ftrace_trace_function.
235 *
236 * Set the function to the list ops, which will call the
237 * function we want, albeit indirectly, but it handles the
238 * ftrace_ops and doesn't depend on function_trace_op.
239 */
240 ftrace_trace_function = ftrace_ops_list_func;
241 /*
242 * Make sure all CPUs see this. Yes this is slow, but static
243 * tracing is slow and nasty to have enabled.
244 */
Paul E. McKenneye5a971d2020-04-03 12:10:28 -0700245 synchronize_rcu_tasks_rude();
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500246 /* Now all cpus are using the list ops. */
247 function_trace_op = set_function_trace_op;
248 /* Make sure the function_trace_op is visible on all CPUs */
249 smp_wmb();
250 /* Nasty way to force a rmb on all cpus */
251 smp_call_function(ftrace_sync_ipi, NULL, 1);
252 /* OK, we are all set to update the ftrace_trace_function now! */
253#endif /* !CONFIG_DYNAMIC_FTRACE */
254
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400255 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400256}
257
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800258static void add_ftrace_ops(struct ftrace_ops __rcu **list,
259 struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200260{
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800261 rcu_assign_pointer(ops->next, *list);
262
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200263 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400264 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200265 * CPU might be walking that list. We need to make sure
266 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400267 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200268 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400269 rcu_assign_pointer(*list, ops);
270}
Steven Rostedt3d083392008-05-12 21:20:42 +0200271
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800272static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
273 struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400274{
275 struct ftrace_ops **p;
276
277 /*
278 * If we are removing the last function, then simply point
279 * to the ftrace_stub.
280 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800281 if (rcu_dereference_protected(*list,
282 lockdep_is_held(&ftrace_lock)) == ops &&
283 rcu_dereference_protected(ops->next,
284 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt2b499382011-05-03 22:49:52 -0400285 *list = &ftrace_list_end;
286 return 0;
287 }
288
289 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
290 if (*p == ops)
291 break;
292
293 if (*p != ops)
294 return -1;
295
296 *p = (*p)->next;
297 return 0;
298}
299
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400300static void ftrace_update_trampoline(struct ftrace_ops *ops);
301
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500302int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400303{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500304 if (ops->flags & FTRACE_OPS_FL_DELETED)
305 return -EINVAL;
306
Steven Rostedtb8489142011-05-04 09:27:52 -0400307 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
308 return -EBUSY;
309
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900310#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400311 /*
312 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
313 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
314 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
315 */
316 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
317 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
318 return -EINVAL;
319
320 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
321 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
322#endif
Miroslav Benes71624312019-10-16 13:33:13 +0200323 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
324 return -EBUSY;
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400325
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400326 if (!core_kernel_data((unsigned long)ops))
327 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
328
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500329 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400330
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400331 /* Always save the function, and reset at unregistering */
332 ops->saved_func = ops->func;
333
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400334 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400335 ops->func = ftrace_pid_func;
336
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400337 ftrace_update_trampoline(ops);
338
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400339 if (ftrace_enabled)
340 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200341
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200342 return 0;
343}
344
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500345int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200346{
Steven Rostedt2b499382011-05-03 22:49:52 -0400347 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200348
Steven Rostedtb8489142011-05-04 09:27:52 -0400349 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
350 return -EBUSY;
351
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500352 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400353
Steven Rostedt2b499382011-05-03 22:49:52 -0400354 if (ret < 0)
355 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400356
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400357 if (ftrace_enabled)
358 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200359
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400360 ops->func = ops->saved_func;
361
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500362 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200363}
364
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500365static void ftrace_update_pid_func(void)
366{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400367 struct ftrace_ops *op;
368
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400369 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500370 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900371 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500372
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400373 do_for_each_ftrace_op(op, ftrace_ops_list) {
374 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400375 op->func = ftrace_pids_enabled(op) ?
376 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400377 ftrace_update_trampoline(op);
378 }
379 } while_for_each_ftrace_op(op);
380
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400381 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500382}
383
Steven Rostedt493762f2009-03-23 17:12:36 -0400384#ifdef CONFIG_FUNCTION_PROFILER
385struct ftrace_profile {
386 struct hlist_node node;
387 unsigned long ip;
388 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400389#ifdef CONFIG_FUNCTION_GRAPH_TRACER
390 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400391 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400392#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400393};
394
395struct ftrace_profile_page {
396 struct ftrace_profile_page *next;
397 unsigned long index;
398 struct ftrace_profile records[];
399};
400
Steven Rostedtcafb1682009-03-24 20:50:39 -0400401struct ftrace_profile_stat {
402 atomic_t disabled;
403 struct hlist_head *hash;
404 struct ftrace_profile_page *pages;
405 struct ftrace_profile_page *start;
406 struct tracer_stat stat;
407};
408
Steven Rostedt493762f2009-03-23 17:12:36 -0400409#define PROFILE_RECORDS_SIZE \
410 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
411
412#define PROFILES_PER_PAGE \
413 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
414
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400415static int ftrace_profile_enabled __read_mostly;
416
417/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400418static DEFINE_MUTEX(ftrace_profile_lock);
419
Steven Rostedtcafb1682009-03-24 20:50:39 -0400420static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400421
Namhyung Kim20079eb2013-04-10 08:55:50 +0900422#define FTRACE_PROFILE_HASH_BITS 10
423#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400424
Steven Rostedt493762f2009-03-23 17:12:36 -0400425static void *
426function_stat_next(void *v, int idx)
427{
428 struct ftrace_profile *rec = v;
429 struct ftrace_profile_page *pg;
430
431 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
432
433 again:
Li Zefan0296e422009-06-26 11:15:37 +0800434 if (idx != 0)
435 rec++;
436
Steven Rostedt493762f2009-03-23 17:12:36 -0400437 if ((void *)rec >= (void *)&pg->records[pg->index]) {
438 pg = pg->next;
439 if (!pg)
440 return NULL;
441 rec = &pg->records[0];
442 if (!rec->counter)
443 goto again;
444 }
445
446 return rec;
447}
448
449static void *function_stat_start(struct tracer_stat *trace)
450{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400451 struct ftrace_profile_stat *stat =
452 container_of(trace, struct ftrace_profile_stat, stat);
453
454 if (!stat || !stat->start)
455 return NULL;
456
457 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400458}
459
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400460#ifdef CONFIG_FUNCTION_GRAPH_TRACER
461/* function graph compares on total time */
Andy Shevchenko80042c82019-10-07 16:56:56 +0300462static int function_stat_cmp(const void *p1, const void *p2)
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400463{
Andy Shevchenko80042c82019-10-07 16:56:56 +0300464 const struct ftrace_profile *a = p1;
465 const struct ftrace_profile *b = p2;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400466
467 if (a->time < b->time)
468 return -1;
469 if (a->time > b->time)
470 return 1;
471 else
472 return 0;
473}
474#else
475/* not function graph compares against hits */
Andy Shevchenko80042c82019-10-07 16:56:56 +0300476static int function_stat_cmp(const void *p1, const void *p2)
Steven Rostedt493762f2009-03-23 17:12:36 -0400477{
Andy Shevchenko80042c82019-10-07 16:56:56 +0300478 const struct ftrace_profile *a = p1;
479 const struct ftrace_profile *b = p2;
Steven Rostedt493762f2009-03-23 17:12:36 -0400480
481 if (a->counter < b->counter)
482 return -1;
483 if (a->counter > b->counter)
484 return 1;
485 else
486 return 0;
487}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400488#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400489
490static int function_stat_headers(struct seq_file *m)
491{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400492#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100493 seq_puts(m, " Function "
494 "Hit Time Avg s^2\n"
495 " -------- "
496 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400497#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100498 seq_puts(m, " Function Hit\n"
499 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400500#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400501 return 0;
502}
503
504static int function_stat_show(struct seq_file *m, void *v)
505{
506 struct ftrace_profile *rec = v;
507 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800508 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400509#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400510 static struct trace_seq s;
511 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400512 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400513#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800514 mutex_lock(&ftrace_profile_lock);
515
516 /* we raced with function_profile_reset() */
517 if (unlikely(rec->counter == 0)) {
518 ret = -EBUSY;
519 goto out;
520 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400521
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530522#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Wen Yange31f7932020-01-03 11:02:48 +0800523 avg = div64_ul(rec->time, rec->counter);
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530524 if (tracing_thresh && (avg < tracing_thresh))
525 goto out;
526#endif
527
Steven Rostedt493762f2009-03-23 17:12:36 -0400528 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400529 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400530
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400531#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100532 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400533
Chase Douglase330b3b2010-04-26 14:02:05 -0400534 /* Sample standard deviation (s^2) */
535 if (rec->counter <= 1)
536 stddev = 0;
537 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200538 /*
539 * Apply Welford's method:
540 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
541 */
542 stddev = rec->counter * rec->time_squared -
543 rec->time * rec->time;
544
Chase Douglase330b3b2010-04-26 14:02:05 -0400545 /*
546 * Divide only 1000 for ns^2 -> us^2 conversion.
547 * trace_print_graph_duration will divide 1000 again.
548 */
Wen Yange31f7932020-01-03 11:02:48 +0800549 stddev = div64_ul(stddev,
550 rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400551 }
552
Steven Rostedt34886c82009-03-25 21:00:47 -0400553 trace_seq_init(&s);
554 trace_print_graph_duration(rec->time, &s);
555 trace_seq_puts(&s, " ");
556 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400557 trace_seq_puts(&s, " ");
558 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400559 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400560#endif
561 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800562out:
563 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400564
Li Zefan3aaba202010-08-23 16:50:12 +0800565 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400566}
567
Steven Rostedtcafb1682009-03-24 20:50:39 -0400568static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400569{
570 struct ftrace_profile_page *pg;
571
Steven Rostedtcafb1682009-03-24 20:50:39 -0400572 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400573
574 while (pg) {
575 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
576 pg->index = 0;
577 pg = pg->next;
578 }
579
Steven Rostedtcafb1682009-03-24 20:50:39 -0400580 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400581 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
582}
583
Steven Rostedtcafb1682009-03-24 20:50:39 -0400584int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400585{
586 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400587 int functions;
588 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400589 int i;
590
591 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400592 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400593 return 0;
594
Steven Rostedtcafb1682009-03-24 20:50:39 -0400595 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
596 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400597 return -ENOMEM;
598
Steven Rostedt318e0a72009-03-25 20:06:34 -0400599#ifdef CONFIG_DYNAMIC_FTRACE
600 functions = ftrace_update_tot_cnt;
601#else
602 /*
603 * We do not know the number of functions that exist because
604 * dynamic tracing is what counts them. With past experience
605 * we have around 20K functions. That should be more than enough.
606 * It is highly unlikely we will execute every function in
607 * the kernel.
608 */
609 functions = 20000;
610#endif
611
Steven Rostedtcafb1682009-03-24 20:50:39 -0400612 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400613
Steven Rostedt318e0a72009-03-25 20:06:34 -0400614 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
615
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900616 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400617 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400618 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400619 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400620 pg = pg->next;
621 }
622
623 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400624
625 out_free:
626 pg = stat->start;
627 while (pg) {
628 unsigned long tmp = (unsigned long)pg;
629
630 pg = pg->next;
631 free_page(tmp);
632 }
633
Steven Rostedt318e0a72009-03-25 20:06:34 -0400634 stat->pages = NULL;
635 stat->start = NULL;
636
637 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400638}
639
Steven Rostedtcafb1682009-03-24 20:50:39 -0400640static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400641{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400642 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400643 int size;
644
Steven Rostedtcafb1682009-03-24 20:50:39 -0400645 stat = &per_cpu(ftrace_profile_stats, cpu);
646
647 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400648 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400650 return 0;
651 }
652
653 /*
654 * We are profiling all functions, but usually only a few thousand
655 * functions are hit. We'll make a hash of 1024 items.
656 */
657 size = FTRACE_PROFILE_HASH_SIZE;
658
Kees Cook6396bb22018-06-12 14:03:40 -0700659 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400660
Steven Rostedtcafb1682009-03-24 20:50:39 -0400661 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400662 return -ENOMEM;
663
Steven Rostedt318e0a72009-03-25 20:06:34 -0400664 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400665 if (ftrace_profile_pages_init(stat) < 0) {
666 kfree(stat->hash);
667 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400668 return -ENOMEM;
669 }
670
671 return 0;
672}
673
Steven Rostedtcafb1682009-03-24 20:50:39 -0400674static int ftrace_profile_init(void)
675{
676 int cpu;
677 int ret = 0;
678
Miao Xiec4602c12013-12-16 15:20:01 +0800679 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400680 ret = ftrace_profile_init_cpu(cpu);
681 if (ret)
682 break;
683 }
684
685 return ret;
686}
687
Steven Rostedt493762f2009-03-23 17:12:36 -0400688/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400689static struct ftrace_profile *
690ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400691{
692 struct ftrace_profile *rec;
693 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400694 unsigned long key;
695
Namhyung Kim20079eb2013-04-10 08:55:50 +0900696 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400698
699 if (hlist_empty(hhd))
700 return NULL;
701
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400702 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400703 if (rec->ip == ip)
704 return rec;
705 }
706
707 return NULL;
708}
709
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710static void ftrace_add_profile(struct ftrace_profile_stat *stat,
711 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400712{
713 unsigned long key;
714
Namhyung Kim20079eb2013-04-10 08:55:50 +0900715 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400717}
718
Steven Rostedt318e0a72009-03-25 20:06:34 -0400719/*
720 * The memory is already allocated, this simply finds a new record to use.
721 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400722static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400724{
725 struct ftrace_profile *rec = NULL;
726
Steven Rostedt318e0a72009-03-25 20:06:34 -0400727 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400729 goto out;
730
Steven Rostedt493762f2009-03-23 17:12:36 -0400731 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400732 * Try to find the function again since an NMI
733 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400734 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400736 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400737 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400738
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739 if (stat->pages->index == PROFILES_PER_PAGE) {
740 if (!stat->pages->next)
741 goto out;
742 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400743 }
744
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400746 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400747 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400748
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400751
752 return rec;
753}
754
Steven Rostedt493762f2009-03-23 17:12:36 -0400755static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400756function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -0400757 struct ftrace_ops *ops, struct ftrace_regs *fregs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400758{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400759 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400760 struct ftrace_profile *rec;
761 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400762
763 if (!ftrace_profile_enabled)
764 return;
765
Steven Rostedt493762f2009-03-23 17:12:36 -0400766 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767
Christoph Lameterbdffd892014-04-29 14:17:40 -0500768 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400769 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770 goto out;
771
772 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400773 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400774 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400775 if (!rec)
776 goto out;
777 }
778
779 rec->counter++;
780 out:
781 local_irq_restore(flags);
782}
783
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400784#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500785static bool fgraph_graph_time = true;
786
787void ftrace_graph_graph_time_control(bool enable)
788{
789 fgraph_graph_time = enable;
790}
791
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400792static int profile_graph_entry(struct ftrace_graph_ent *trace)
793{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500794 struct ftrace_ret_stack *ret_stack;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900795
Steven Rostedta1e2e312011-08-09 12:50:46 -0400796 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900797
Steven Rostedt (VMware)a8f0f9e2017-08-17 16:37:25 -0400798 /* If function graph is shutting down, ret_stack can be NULL */
799 if (!current->ret_stack)
800 return 0;
801
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500802 ret_stack = ftrace_graph_get_ret_stack(current, 0);
803 if (ret_stack)
804 ret_stack->subtime = 0;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900805
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400806 return 1;
807}
808
809static void profile_graph_return(struct ftrace_graph_ret *trace)
810{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500811 struct ftrace_ret_stack *ret_stack;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400812 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400813 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400814 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400816
817 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500818 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400819 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400820 goto out;
821
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400822 /* If the calltime was zero'd ignore it */
823 if (!trace->calltime)
824 goto out;
825
Steven Rostedta2a16d62009-03-24 23:17:58 -0400826 calltime = trace->rettime - trace->calltime;
827
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400828 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400829
830 /* Append this call time to the parent time to subtract */
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500831 ret_stack = ftrace_graph_get_ret_stack(current, 1);
832 if (ret_stack)
833 ret_stack->subtime += calltime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400834
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500835 ret_stack = ftrace_graph_get_ret_stack(current, 0);
836 if (ret_stack && ret_stack->subtime < calltime)
837 calltime -= ret_stack->subtime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400838 else
839 calltime = 0;
840 }
841
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400843 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400844 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400845 rec->time_squared += calltime * calltime;
846 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400847
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400849 local_irq_restore(flags);
850}
851
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500852static struct fgraph_ops fprofiler_ops = {
853 .entryfunc = &profile_graph_entry,
854 .retfunc = &profile_graph_return,
855};
856
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400857static int register_ftrace_profiler(void)
858{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500859 return register_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400860}
861
862static void unregister_ftrace_profiler(void)
863{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500864 unregister_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400865}
866#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100867static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400868 .func = function_profile_call,
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -0500869 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400870 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400871};
872
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400873static int register_ftrace_profiler(void)
874{
875 return register_ftrace_function(&ftrace_profile_ops);
876}
877
878static void unregister_ftrace_profiler(void)
879{
880 unregister_ftrace_function(&ftrace_profile_ops);
881}
882#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
883
Steven Rostedt493762f2009-03-23 17:12:36 -0400884static ssize_t
885ftrace_profile_write(struct file *filp, const char __user *ubuf,
886 size_t cnt, loff_t *ppos)
887{
888 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400889 int ret;
890
Peter Huewe22fe9b52011-06-07 21:58:27 +0200891 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
892 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400893 return ret;
894
895 val = !!val;
896
897 mutex_lock(&ftrace_profile_lock);
898 if (ftrace_profile_enabled ^ val) {
899 if (val) {
900 ret = ftrace_profile_init();
901 if (ret < 0) {
902 cnt = ret;
903 goto out;
904 }
905
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400906 ret = register_ftrace_profiler();
907 if (ret < 0) {
908 cnt = ret;
909 goto out;
910 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400911 ftrace_profile_enabled = 1;
912 } else {
913 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400914 /*
915 * unregister_ftrace_profiler calls stop_machine
Paul E. McKenney74401722018-11-06 18:44:52 -0800916 * so this acts like an synchronize_rcu.
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400917 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400918 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400919 }
920 }
921 out:
922 mutex_unlock(&ftrace_profile_lock);
923
Jiri Olsacf8517c2009-10-23 19:36:16 -0400924 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400925
926 return cnt;
927}
928
929static ssize_t
930ftrace_profile_read(struct file *filp, char __user *ubuf,
931 size_t cnt, loff_t *ppos)
932{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400933 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400934 int r;
935
936 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
937 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
938}
939
940static const struct file_operations ftrace_profile_fops = {
941 .open = tracing_open_generic,
942 .read = ftrace_profile_read,
943 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200944 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400945};
946
Steven Rostedtcafb1682009-03-24 20:50:39 -0400947/* used to initialize the real stat files */
948static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400949 .name = "functions",
950 .stat_start = function_stat_start,
951 .stat_next = function_stat_next,
952 .stat_cmp = function_stat_cmp,
953 .stat_headers = function_stat_headers,
954 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400955};
956
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500957static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400958{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400959 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400960 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400961 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400962 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400963 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400964
Steven Rostedtcafb1682009-03-24 20:50:39 -0400965 for_each_possible_cpu(cpu) {
966 stat = &per_cpu(ftrace_profile_stats, cpu);
967
Geliang Tang6363c6b2016-03-15 22:12:34 +0800968 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400969 if (!name) {
970 /*
971 * The files created are permanent, if something happens
972 * we still do not free memory.
973 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400974 WARN(1,
975 "Could not allocate stat file for cpu %d\n",
976 cpu);
977 return;
978 }
979 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400980 stat->stat.name = name;
981 ret = register_stat_tracer(&stat->stat);
982 if (ret) {
983 WARN(1,
984 "Could not register function stat for cpu %d\n",
985 cpu);
986 kfree(name);
987 return;
988 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400989 }
990
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500991 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -0400992 d_tracer, NULL, &ftrace_profile_fops);
993 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700994 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -0400995}
996
997#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500998static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400999{
1000}
1001#endif /* CONFIG_FUNCTION_PROFILER */
1002
Steven Rostedt3d083392008-05-12 21:20:42 +02001003#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001004
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001005static struct ftrace_ops *removed_ops;
1006
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001007/*
1008 * Set when doing a global update, like enabling all recs or disabling them.
1009 * It is not set when just updating a single ftrace_ops.
1010 */
1011static bool update_all_ops;
1012
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001013#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001014# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001015#endif
1016
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001017struct ftrace_func_probe {
1018 struct ftrace_probe_ops *probe_ops;
1019 struct ftrace_ops ops;
1020 struct trace_array *tr;
1021 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001022 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001023 int ref;
1024};
1025
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001026/*
1027 * We make these constant because no one should touch them,
1028 * but they are used as the default "empty hash", to avoid allocating
1029 * it all the time. These are in a read only section such that if
1030 * anyone does try to modify it, it will cause an exception.
1031 */
1032static const struct hlist_head empty_buckets[1];
1033static const struct ftrace_hash empty_hash = {
1034 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001035};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001036#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001037
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001038struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001039 .func = ftrace_stub,
1040 .local_hash.notrace_hash = EMPTY_HASH,
1041 .local_hash.filter_hash = EMPTY_HASH,
1042 INIT_OPS_HASH(global_ops)
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05001043 .flags = FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001044 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001045};
1046
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001047/*
Ingo Molnarf2cc0202021-03-23 18:49:35 +01001048 * Used by the stack unwinder to know about dynamic ftrace trampolines.
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001049 */
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001050struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001051{
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001052 struct ftrace_ops *op = NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001053
1054 /*
1055 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08001056 * they are freed after a synchronize_rcu().
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001057 */
1058 preempt_disable_notrace();
1059
1060 do_for_each_ftrace_op(op, ftrace_ops_list) {
1061 /*
1062 * This is to check for dynamically allocated trampolines.
1063 * Trampolines that are in kernel text will have
1064 * core_kernel_text() return true.
1065 */
1066 if (op->trampoline && op->trampoline_size)
1067 if (addr >= op->trampoline &&
1068 addr < op->trampoline + op->trampoline_size) {
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001069 preempt_enable_notrace();
1070 return op;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001071 }
1072 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001073 preempt_enable_notrace();
1074
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001075 return NULL;
1076}
1077
1078/*
1079 * This is used by __kernel_text_address() to return true if the
1080 * address is on a dynamically allocated trampoline that would
1081 * not return true for either core_kernel_text() or
1082 * is_module_text_address().
1083 */
1084bool is_ftrace_trampoline(unsigned long addr)
1085{
1086 return ftrace_ops_trampoline(addr) != NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001087}
1088
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001089struct ftrace_page {
1090 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001091 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001092 int index;
Linus Torvaldsdb425232021-04-01 16:14:17 -04001093 int order;
David Milleraa5e5ce2008-05-13 22:06:56 -07001094};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001095
Steven Rostedta7900872011-12-16 16:23:44 -05001096#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1097#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001098
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001099static struct ftrace_page *ftrace_pages_start;
1100static struct ftrace_page *ftrace_pages;
1101
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001102static __always_inline unsigned long
1103ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1104{
1105 if (hash->size_bits > 0)
1106 return hash_long(ip, hash->size_bits);
1107
1108 return 0;
1109}
1110
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001111/* Only use this function if ftrace_hash_empty() has already been tested */
1112static __always_inline struct ftrace_func_entry *
1113__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001114{
1115 unsigned long key;
1116 struct ftrace_func_entry *entry;
1117 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001118
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001119 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001120 hhd = &hash->buckets[key];
1121
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001122 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123 if (entry->ip == ip)
1124 return entry;
1125 }
1126 return NULL;
1127}
1128
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001129/**
1130 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1131 * @hash: The hash to look at
1132 * @ip: The instruction pointer to test
1133 *
1134 * Search a given @hash to see if a given instruction pointer (@ip)
1135 * exists in it.
1136 *
1137 * Returns the entry that holds the @ip if found. NULL otherwise.
1138 */
1139struct ftrace_func_entry *
1140ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1141{
1142 if (ftrace_hash_empty(hash))
1143 return NULL;
1144
1145 return __ftrace_lookup_ip(hash, ip);
1146}
1147
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001148static void __add_hash_entry(struct ftrace_hash *hash,
1149 struct ftrace_func_entry *entry)
1150{
1151 struct hlist_head *hhd;
1152 unsigned long key;
1153
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001154 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001155 hhd = &hash->buckets[key];
1156 hlist_add_head(&entry->hlist, hhd);
1157 hash->count++;
1158}
1159
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001160static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1161{
1162 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001163
1164 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1165 if (!entry)
1166 return -ENOMEM;
1167
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001168 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001169 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001170
1171 return 0;
1172}
1173
1174static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001175free_hash_entry(struct ftrace_hash *hash,
1176 struct ftrace_func_entry *entry)
1177{
1178 hlist_del(&entry->hlist);
1179 kfree(entry);
1180 hash->count--;
1181}
1182
1183static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184remove_hash_entry(struct ftrace_hash *hash,
1185 struct ftrace_func_entry *entry)
1186{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001187 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001188 hash->count--;
1189}
1190
1191static void ftrace_hash_clear(struct ftrace_hash *hash)
1192{
1193 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001194 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001195 struct ftrace_func_entry *entry;
1196 int size = 1 << hash->size_bits;
1197 int i;
1198
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001199 if (!hash->count)
1200 return;
1201
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001202 for (i = 0; i < size; i++) {
1203 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001204 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001205 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001206 }
1207 FTRACE_WARN_ON(hash->count);
1208}
1209
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001210static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1211{
1212 list_del(&ftrace_mod->list);
1213 kfree(ftrace_mod->module);
1214 kfree(ftrace_mod->func);
1215 kfree(ftrace_mod);
1216}
1217
1218static void clear_ftrace_mod_list(struct list_head *head)
1219{
1220 struct ftrace_mod_load *p, *n;
1221
1222 /* stack tracer isn't supported yet */
1223 if (!head)
1224 return;
1225
1226 mutex_lock(&ftrace_lock);
1227 list_for_each_entry_safe(p, n, head, list)
1228 free_ftrace_mod(p);
1229 mutex_unlock(&ftrace_lock);
1230}
1231
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001232static void free_ftrace_hash(struct ftrace_hash *hash)
1233{
1234 if (!hash || hash == EMPTY_HASH)
1235 return;
1236 ftrace_hash_clear(hash);
1237 kfree(hash->buckets);
1238 kfree(hash);
1239}
1240
Steven Rostedt07fd5512011-05-05 18:03:47 -04001241static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1242{
1243 struct ftrace_hash *hash;
1244
1245 hash = container_of(rcu, struct ftrace_hash, rcu);
1246 free_ftrace_hash(hash);
1247}
1248
1249static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1250{
1251 if (!hash || hash == EMPTY_HASH)
1252 return;
Paul E. McKenney74401722018-11-06 18:44:52 -08001253 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001254}
1255
Jiri Olsa5500fa52012-02-15 15:51:54 +01001256void ftrace_free_filter(struct ftrace_ops *ops)
1257{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001258 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001259 free_ftrace_hash(ops->func_hash->filter_hash);
1260 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001261}
1262
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001263static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1264{
1265 struct ftrace_hash *hash;
1266 int size;
1267
1268 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1269 if (!hash)
1270 return NULL;
1271
1272 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001273 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001274
1275 if (!hash->buckets) {
1276 kfree(hash);
1277 return NULL;
1278 }
1279
1280 hash->size_bits = size_bits;
1281
1282 return hash;
1283}
1284
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001285
1286static int ftrace_add_mod(struct trace_array *tr,
1287 const char *func, const char *module,
1288 int enable)
1289{
1290 struct ftrace_mod_load *ftrace_mod;
1291 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1292
1293 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1294 if (!ftrace_mod)
1295 return -ENOMEM;
1296
1297 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1298 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1299 ftrace_mod->enable = enable;
1300
1301 if (!ftrace_mod->func || !ftrace_mod->module)
1302 goto out_free;
1303
1304 list_add(&ftrace_mod->list, mod_head);
1305
1306 return 0;
1307
1308 out_free:
1309 free_ftrace_mod(ftrace_mod);
1310
1311 return -ENOMEM;
1312}
1313
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001314static struct ftrace_hash *
1315alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1316{
1317 struct ftrace_func_entry *entry;
1318 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319 int size;
1320 int ret;
1321 int i;
1322
1323 new_hash = alloc_ftrace_hash(size_bits);
1324 if (!new_hash)
1325 return NULL;
1326
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001327 if (hash)
1328 new_hash->flags = hash->flags;
1329
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001330 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001331 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001332 return new_hash;
1333
1334 size = 1 << hash->size_bits;
1335 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001336 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 ret = add_hash_entry(new_hash, entry->ip);
1338 if (ret < 0)
1339 goto free_hash;
1340 }
1341 }
1342
1343 FTRACE_WARN_ON(new_hash->count != hash->count);
1344
1345 return new_hash;
1346
1347 free_hash:
1348 free_ftrace_hash(new_hash);
1349 return NULL;
1350}
1351
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001352static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001353ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001354static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001355ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001356
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001357static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1358 struct ftrace_hash *new_hash);
1359
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001360static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001361{
1362 struct ftrace_func_entry *entry;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001363 struct ftrace_hash *new_hash;
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001364 struct hlist_head *hhd;
1365 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001366 int bits = 0;
1367 int i;
1368
1369 /*
Steven Rostedt (VMware)be493132020-10-05 20:21:14 -04001370 * Use around half the size (max bit of it), but
1371 * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001372 */
Steven Rostedt (VMware)be493132020-10-05 20:21:14 -04001373 bits = fls(size / 2);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001374
1375 /* Don't allocate too much */
1376 if (bits > FTRACE_HASH_MAX_BITS)
1377 bits = FTRACE_HASH_MAX_BITS;
1378
Steven Rostedt07fd5512011-05-05 18:03:47 -04001379 new_hash = alloc_ftrace_hash(bits);
1380 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001381 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001382
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001383 new_hash->flags = src->flags;
1384
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385 size = 1 << src->size_bits;
1386 for (i = 0; i < size; i++) {
1387 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001388 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001389 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001390 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001391 }
1392 }
Namhyung Kim3e278c02017-01-20 11:44:45 +09001393 return new_hash;
1394}
1395
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001396static struct ftrace_hash *
1397__ftrace_hash_move(struct ftrace_hash *src)
1398{
1399 int size = src->count;
1400
1401 /*
1402 * If the new source is empty, just return the empty_hash.
1403 */
1404 if (ftrace_hash_empty(src))
1405 return EMPTY_HASH;
1406
1407 return dup_hash(src, size);
1408}
1409
Namhyung Kim3e278c02017-01-20 11:44:45 +09001410static int
1411ftrace_hash_move(struct ftrace_ops *ops, int enable,
1412 struct ftrace_hash **dst, struct ftrace_hash *src)
1413{
1414 struct ftrace_hash *new_hash;
1415 int ret;
1416
1417 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1418 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1419 return -EINVAL;
1420
1421 new_hash = __ftrace_hash_move(src);
1422 if (!new_hash)
1423 return -ENOMEM;
1424
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001425 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1426 if (enable) {
1427 /* IPMODIFY should be updated only when filter_hash updating */
1428 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1429 if (ret < 0) {
1430 free_ftrace_hash(new_hash);
1431 return ret;
1432 }
1433 }
1434
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001435 /*
1436 * Remove the current set, update the hash and add
1437 * them back.
1438 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001439 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001440
Steven Rostedt07fd5512011-05-05 18:03:47 -04001441 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001442
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001443 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001444
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001445 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001446}
1447
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001448static bool hash_contains_ip(unsigned long ip,
1449 struct ftrace_ops_hash *hash)
1450{
1451 /*
1452 * The function record is a match if it exists in the filter
Qiujun Huangfdda88d2020-10-02 22:31:26 +08001453 * hash and not in the notrace hash. Note, an empty hash is
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001454 * considered a match for the filter hash, but an empty
1455 * notrace hash is considered not in the notrace hash.
1456 */
1457 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001458 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001459 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001460 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001461}
1462
Steven Rostedt265c8312009-02-13 12:43:56 -05001463/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001464 * Test the hashes for this ops to see if we want to call
1465 * the ops->func or not.
1466 *
1467 * It's a match if the ip is in the ops->filter_hash or
1468 * the filter_hash does not exist or is empty,
1469 * AND
1470 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001471 *
1472 * This needs to be called with preemption disabled as
Paul E. McKenney74401722018-11-06 18:44:52 -08001473 * the hashes are freed with call_rcu().
Steven Rostedtb8489142011-05-04 09:27:52 -04001474 */
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001475int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001476ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001477{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001478 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001479 int ret;
1480
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001481#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1482 /*
1483 * There's a small race when adding ops that the ftrace handler
1484 * that wants regs, may be called without them. We can not
1485 * allow that handler to be called if regs is NULL.
1486 */
1487 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1488 return 0;
1489#endif
1490
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001491 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1492 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001493
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001494 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001495 ret = 1;
1496 else
1497 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001498
1499 return ret;
1500}
1501
1502/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001503 * This is a double for. Do not use 'break' to break out of the loop,
1504 * you must use a goto.
1505 */
1506#define do_for_each_ftrace_rec(pg, rec) \
1507 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1508 int _____i; \
1509 for (_____i = 0; _____i < pg->index; _____i++) { \
1510 rec = &pg->records[_____i];
1511
1512#define while_for_each_ftrace_rec() \
1513 } \
1514 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301515
Steven Rostedt5855fea2011-12-16 19:27:42 -05001516
1517static int ftrace_cmp_recs(const void *a, const void *b)
1518{
Steven Rostedta650e022012-04-25 13:48:13 -04001519 const struct dyn_ftrace *key = a;
1520 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001521
Steven Rostedta650e022012-04-25 13:48:13 -04001522 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001523 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001524 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1525 return 1;
1526 return 0;
1527}
1528
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001529static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1530{
1531 struct ftrace_page *pg;
1532 struct dyn_ftrace *rec = NULL;
1533 struct dyn_ftrace key;
1534
1535 key.ip = start;
1536 key.flags = end; /* overload flags, as it is unsigned long */
1537
1538 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1539 if (end < pg->records[0].ip ||
1540 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1541 continue;
1542 rec = bsearch(&key, pg->records, pg->index,
1543 sizeof(struct dyn_ftrace),
1544 ftrace_cmp_recs);
Artem Savkovd9815bf2020-03-06 18:43:17 +01001545 if (rec)
1546 break;
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001547 }
1548 return rec;
1549}
1550
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001551/**
1552 * ftrace_location_range - return the first address of a traced location
1553 * if it touches the given ip range
1554 * @start: start of range to search.
1555 * @end: end of range to search (inclusive). @end points to the last byte
1556 * to check.
1557 *
1558 * Returns rec->ip if the related ftrace location is a least partly within
1559 * the given address range. That is, the first address of the instruction
1560 * that is either a NOP or call to the function tracer. It checks the ftrace
1561 * internal tables to determine if the address belongs or not.
1562 */
1563unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001564{
Steven Rostedta650e022012-04-25 13:48:13 -04001565 struct dyn_ftrace *rec;
Steven Rostedta650e022012-04-25 13:48:13 -04001566
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001567 rec = lookup_rec(start, end);
1568 if (rec)
1569 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001570
Steven Rostedt5855fea2011-12-16 19:27:42 -05001571 return 0;
1572}
1573
Steven Rostedtc88fd862011-08-16 09:53:39 -04001574/**
1575 * ftrace_location - return true if the ip giving is a traced location
1576 * @ip: the instruction pointer to check
1577 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001578 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001579 * That is, the instruction that is either a NOP or call to
1580 * the function tracer. It checks the ftrace internal tables to
1581 * determine if the address belongs or not.
1582 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001583unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001584{
Steven Rostedta650e022012-04-25 13:48:13 -04001585 return ftrace_location_range(ip, ip);
1586}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001587
Steven Rostedta650e022012-04-25 13:48:13 -04001588/**
1589 * ftrace_text_reserved - return true if range contains an ftrace location
1590 * @start: start of range to search
1591 * @end: end of range to search (inclusive). @end points to the last byte to check.
1592 *
1593 * Returns 1 if @start and @end contains a ftrace location.
1594 * That is, the instruction that is either a NOP or call to
1595 * the function tracer. It checks the ftrace internal tables to
1596 * determine if the address belongs or not.
1597 */
Sasha Levind88471c2013-01-09 18:09:20 -05001598int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001599{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001600 unsigned long ret;
1601
1602 ret = ftrace_location_range((unsigned long)start,
1603 (unsigned long)end);
1604
1605 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001606}
1607
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001608/* Test if ops registered to this rec needs regs */
1609static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1610{
1611 struct ftrace_ops *ops;
1612 bool keep_regs = false;
1613
1614 for (ops = ftrace_ops_list;
1615 ops != &ftrace_list_end; ops = ops->next) {
1616 /* pass rec in as regs to have non-NULL val */
1617 if (ftrace_ops_test(ops, rec->ip, rec)) {
1618 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1619 keep_regs = true;
1620 break;
1621 }
1622 }
1623 }
1624
1625 return keep_regs;
1626}
1627
Cheng Jiana1246922019-05-04 19:39:39 +08001628static struct ftrace_ops *
1629ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1630static struct ftrace_ops *
Naveen N. Rao4c75b0f2020-11-26 23:38:38 +05301631ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1632static struct ftrace_ops *
Cheng Jiana1246922019-05-04 19:39:39 +08001633ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1634
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001635static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001636 int filter_hash,
1637 bool inc)
1638{
1639 struct ftrace_hash *hash;
1640 struct ftrace_hash *other_hash;
1641 struct ftrace_page *pg;
1642 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001643 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001644 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001645 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001646
1647 /* Only update if the ops has been registered */
1648 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001649 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001650
1651 /*
1652 * In the filter_hash case:
1653 * If the count is zero, we update all records.
1654 * Otherwise we just update the items in the hash.
1655 *
1656 * In the notrace_hash case:
1657 * We enable the update in the hash.
1658 * As disabling notrace means enabling the tracing,
1659 * and enabling notrace means disabling, the inc variable
1660 * gets inversed.
1661 */
1662 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001663 hash = ops->func_hash->filter_hash;
1664 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001665 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001666 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001667 } else {
1668 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001669 hash = ops->func_hash->notrace_hash;
1670 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001671 /*
1672 * If the notrace hash has no items,
1673 * then there's nothing to do.
1674 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001675 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001676 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001677 }
1678
1679 do_for_each_ftrace_rec(pg, rec) {
1680 int in_other_hash = 0;
1681 int in_hash = 0;
1682 int match = 0;
1683
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001684 if (rec->flags & FTRACE_FL_DISABLED)
1685 continue;
1686
Steven Rostedted926f92011-05-03 13:25:24 -04001687 if (all) {
1688 /*
1689 * Only the filter_hash affects all records.
1690 * Update if the record is not in the notrace hash.
1691 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001692 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001693 match = 1;
1694 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001695 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1696 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001697
1698 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001699 * If filter_hash is set, we want to match all functions
1700 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001701 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001702 * If filter_hash is not set, then we are decrementing.
1703 * That means we match anything that is in the hash
1704 * and also in the other_hash. That is, we need to turn
1705 * off functions in the other hash because they are disabled
1706 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001707 */
1708 if (filter_hash && in_hash && !in_other_hash)
1709 match = 1;
1710 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001711 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001712 match = 1;
1713 }
1714 if (!match)
1715 continue;
1716
1717 if (inc) {
1718 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001719 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001720 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001721
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05001722 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1723 rec->flags |= FTRACE_FL_DIRECT;
1724
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001725 /*
1726 * If there's only a single callback registered to a
1727 * function, and the ops has a trampoline registered
1728 * for it, then we can call it directly.
1729 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001730 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001731 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001732 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001733 /*
1734 * If we are adding another function callback
1735 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001736 * custom trampoline in use, then we need to go
1737 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001738 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001739 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001740
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001741 /*
1742 * If any ops wants regs saved for this function
1743 * then all ops will get saved regs.
1744 */
1745 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1746 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001747 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001748 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001749 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001750 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001751
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001752 /*
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05001753 * Only the internal direct_ops should have the
1754 * DIRECT flag set. Thus, if it is removing a
1755 * function, then that function should no longer
1756 * be direct.
1757 */
1758 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1759 rec->flags &= ~FTRACE_FL_DIRECT;
1760
1761 /*
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001762 * If the rec had REGS enabled and the ops that is
1763 * being removed had REGS set, then see if there is
1764 * still any ops for this record that wants regs.
1765 * If not, we can stop recording them.
1766 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001767 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001768 rec->flags & FTRACE_FL_REGS &&
1769 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1770 if (!test_rec_ops_needs_regs(rec))
1771 rec->flags &= ~FTRACE_FL_REGS;
1772 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001773
1774 /*
Cheng Jiana1246922019-05-04 19:39:39 +08001775 * The TRAMP needs to be set only if rec count
1776 * is decremented to one, and the ops that is
1777 * left has a trampoline. As TRAMP can only be
1778 * enabled if there is only a single ops attached
1779 * to it.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001780 */
Cheng Jiana1246922019-05-04 19:39:39 +08001781 if (ftrace_rec_count(rec) == 1 &&
Naveen N. Rao4c75b0f2020-11-26 23:38:38 +05301782 ftrace_find_tramp_ops_any_other(rec, ops))
Cheng Jiana1246922019-05-04 19:39:39 +08001783 rec->flags |= FTRACE_FL_TRAMP;
1784 else
1785 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001786
1787 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001788 * flags will be cleared in ftrace_check_record()
1789 * if rec count is zero.
1790 */
Steven Rostedted926f92011-05-03 13:25:24 -04001791 }
1792 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001793
1794 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04001795 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001796
Steven Rostedted926f92011-05-03 13:25:24 -04001797 /* Shortcut, if we handled all records, we are done. */
1798 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001799 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001800 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001801
1802 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001803}
1804
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001805static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001806 int filter_hash)
1807{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001808 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001809}
1810
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001811static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001812 int filter_hash)
1813{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001814 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001815}
1816
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001817static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1818 int filter_hash, int inc)
1819{
1820 struct ftrace_ops *op;
1821
1822 __ftrace_hash_rec_update(ops, filter_hash, inc);
1823
1824 if (ops->func_hash != &global_ops.local_hash)
1825 return;
1826
1827 /*
1828 * If the ops shares the global_ops hash, then we need to update
1829 * all ops that are enabled and use this hash.
1830 */
1831 do_for_each_ftrace_op(op, ftrace_ops_list) {
1832 /* Already done */
1833 if (op == ops)
1834 continue;
1835 if (op->func_hash == &global_ops.local_hash)
1836 __ftrace_hash_rec_update(op, filter_hash, inc);
1837 } while_for_each_ftrace_op(op);
1838}
1839
1840static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1841 int filter_hash)
1842{
1843 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1844}
1845
1846static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1847 int filter_hash)
1848{
1849 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1850}
1851
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001852/*
1853 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1854 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1855 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1856 * Note that old_hash and new_hash has below meanings
1857 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1858 * - If the hash is EMPTY_HASH, it hits nothing
1859 * - Anything else hits the recs which match the hash entries.
1860 */
1861static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1862 struct ftrace_hash *old_hash,
1863 struct ftrace_hash *new_hash)
1864{
1865 struct ftrace_page *pg;
1866 struct dyn_ftrace *rec, *end = NULL;
1867 int in_old, in_new;
1868
1869 /* Only update if the ops has been registered */
1870 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1871 return 0;
1872
1873 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1874 return 0;
1875
1876 /*
1877 * Since the IPMODIFY is a very address sensitive action, we do not
1878 * allow ftrace_ops to set all functions to new hash.
1879 */
1880 if (!new_hash || !old_hash)
1881 return -EINVAL;
1882
1883 /* Update rec->flags */
1884 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001885
1886 if (rec->flags & FTRACE_FL_DISABLED)
1887 continue;
1888
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001889 /* We need to update only differences of filter_hash */
1890 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1891 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1892 if (in_old == in_new)
1893 continue;
1894
1895 if (in_new) {
1896 /* New entries must ensure no others are using it */
1897 if (rec->flags & FTRACE_FL_IPMODIFY)
1898 goto rollback;
1899 rec->flags |= FTRACE_FL_IPMODIFY;
1900 } else /* Removed entry */
1901 rec->flags &= ~FTRACE_FL_IPMODIFY;
1902 } while_for_each_ftrace_rec();
1903
1904 return 0;
1905
1906rollback:
1907 end = rec;
1908
1909 /* Roll back what we did above */
1910 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001911
1912 if (rec->flags & FTRACE_FL_DISABLED)
1913 continue;
1914
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001915 if (rec == end)
1916 goto err_out;
1917
1918 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1919 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1920 if (in_old == in_new)
1921 continue;
1922
1923 if (in_new)
1924 rec->flags &= ~FTRACE_FL_IPMODIFY;
1925 else
1926 rec->flags |= FTRACE_FL_IPMODIFY;
1927 } while_for_each_ftrace_rec();
1928
1929err_out:
1930 return -EBUSY;
1931}
1932
1933static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1934{
1935 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1936
1937 if (ftrace_hash_empty(hash))
1938 hash = NULL;
1939
1940 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1941}
1942
1943/* Disabling always succeeds */
1944static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1945{
1946 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1947
1948 if (ftrace_hash_empty(hash))
1949 hash = NULL;
1950
1951 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1952}
1953
1954static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1955 struct ftrace_hash *new_hash)
1956{
1957 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1958
1959 if (ftrace_hash_empty(old_hash))
1960 old_hash = NULL;
1961
1962 if (ftrace_hash_empty(new_hash))
1963 new_hash = NULL;
1964
1965 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1966}
1967
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001968static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001969{
Steven Rostedt (VMware)6c141332021-06-07 21:39:08 -04001970 char ins[MCOUNT_INSN_SIZE];
Steven Rostedt05736a42008-09-22 14:55:47 -07001971 int i;
1972
Steven Rostedt (VMware)6c141332021-06-07 21:39:08 -04001973 if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
1974 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1975 return;
1976 }
1977
Steven Rostedt05736a42008-09-22 14:55:47 -07001978 printk(KERN_CONT "%s", fmt);
1979
1980 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
Steven Rostedt (VMware)6c141332021-06-07 21:39:08 -04001981 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
Steven Rostedt05736a42008-09-22 14:55:47 -07001982}
1983
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001984enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001985const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001986
1987static void print_bug_type(void)
1988{
1989 switch (ftrace_bug_type) {
1990 case FTRACE_BUG_UNKNOWN:
1991 break;
1992 case FTRACE_BUG_INIT:
1993 pr_info("Initializing ftrace call sites\n");
1994 break;
1995 case FTRACE_BUG_NOP:
1996 pr_info("Setting ftrace call site to NOP\n");
1997 break;
1998 case FTRACE_BUG_CALL:
1999 pr_info("Setting ftrace call site to call ftrace function\n");
2000 break;
2001 case FTRACE_BUG_UPDATE:
2002 pr_info("Updating ftrace call site to call a different ftrace function\n");
2003 break;
2004 }
2005}
2006
Steven Rostedtc88fd862011-08-16 09:53:39 -04002007/**
2008 * ftrace_bug - report and shutdown function tracer
2009 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002010 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04002011 *
2012 * The arch code that enables or disables the function tracing
2013 * can call ftrace_bug() when it has detected a problem in
2014 * modifying the code. @failed should be one of either:
2015 * EFAULT - if the problem happens on reading the @ip address
2016 * EINVAL - if what is read at @ip is not what was expected
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05302017 * EPERM - if the problem happens on writing to the @ip address
Steven Rostedtc88fd862011-08-16 09:53:39 -04002018 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002019void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002020{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002021 unsigned long ip = rec ? rec->ip : 0;
2022
Cheng Jianc143b772020-05-15 10:08:28 +00002023 pr_info("------------[ ftrace bug ]------------\n");
2024
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002025 switch (failed) {
2026 case -EFAULT:
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002027 pr_info("ftrace faulted on modifying ");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07002028 print_ip_sym(KERN_INFO, ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002029 break;
2030 case -EINVAL:
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002031 pr_info("ftrace failed to modify ");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07002032 print_ip_sym(KERN_INFO, ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002033 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002034 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002035 if (ftrace_expected) {
2036 print_ip_ins(" expected: ", ftrace_expected);
2037 pr_cont("\n");
2038 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002039 break;
2040 case -EPERM:
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002041 pr_info("ftrace faulted on writing ");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07002042 print_ip_sym(KERN_INFO, ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002043 break;
2044 default:
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002045 pr_info("ftrace faulted on unknown error ");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07002046 print_ip_sym(KERN_INFO, ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002047 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002048 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002049 if (rec) {
2050 struct ftrace_ops *ops = NULL;
2051
2052 pr_info("ftrace record flags: %lx\n", rec->flags);
2053 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2054 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2055 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2056 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002057 if (ops) {
2058 do {
2059 pr_cont("\ttramp: %pS (%pS)",
2060 (void *)ops->trampoline,
2061 (void *)ops->func);
2062 ops = ftrace_find_tramp_ops_next(rec, ops);
2063 } while (ops);
2064 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002065 pr_cont("\ttramp: ERROR!");
2066
2067 }
2068 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002069 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002070 }
Cheng Jianc143b772020-05-15 10:08:28 +00002071
2072 FTRACE_WARN_ON_ONCE(1);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002073}
2074
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002075static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002076{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002077 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002078
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002079 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2080
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002081 if (rec->flags & FTRACE_FL_DISABLED)
2082 return FTRACE_UPDATE_IGNORE;
2083
Steven Rostedt982c3502008-11-15 16:31:41 -05002084 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002085 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002086 *
Steven Rostedted926f92011-05-03 13:25:24 -04002087 * If the record has a ref count, then we need to enable it
2088 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002089 *
Steven Rostedted926f92011-05-03 13:25:24 -04002090 * Otherwise we make sure its disabled.
2091 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002092 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002093 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002094 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002095 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002096 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002097
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002098 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002099 * If enabling and the REGS flag does not match the REGS_EN, or
2100 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2101 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002102 * Same for direct calls.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002103 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002104 if (flag) {
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002105 if (!(rec->flags & FTRACE_FL_REGS) !=
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002106 !(rec->flags & FTRACE_FL_REGS_EN))
2107 flag |= FTRACE_FL_REGS;
2108
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002109 if (!(rec->flags & FTRACE_FL_TRAMP) !=
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002110 !(rec->flags & FTRACE_FL_TRAMP_EN))
2111 flag |= FTRACE_FL_TRAMP;
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002112
2113 /*
2114 * Direct calls are special, as count matters.
2115 * We must test the record for direct, if the
2116 * DIRECT and DIRECT_EN do not match, but only
2117 * if the count is 1. That's because, if the
2118 * count is something other than one, we do not
2119 * want the direct enabled (it will be done via the
2120 * direct helper). But if DIRECT_EN is set, and
2121 * the count is not one, we need to clear it.
2122 */
2123 if (ftrace_rec_count(rec) == 1) {
2124 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2125 !(rec->flags & FTRACE_FL_DIRECT_EN))
2126 flag |= FTRACE_FL_DIRECT;
2127 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2128 flag |= FTRACE_FL_DIRECT;
2129 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002130 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002131
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002132 /* If the state of this record hasn't changed, then do nothing */
2133 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002134 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002135
2136 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002137 /* Save off if rec is being enabled (for return value) */
2138 flag ^= rec->flags & FTRACE_FL_ENABLED;
2139
2140 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002141 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002142 if (flag & FTRACE_FL_REGS) {
2143 if (rec->flags & FTRACE_FL_REGS)
2144 rec->flags |= FTRACE_FL_REGS_EN;
2145 else
2146 rec->flags &= ~FTRACE_FL_REGS_EN;
2147 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002148 if (flag & FTRACE_FL_TRAMP) {
2149 if (rec->flags & FTRACE_FL_TRAMP)
2150 rec->flags |= FTRACE_FL_TRAMP_EN;
2151 else
2152 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2153 }
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04002154
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002155 if (flag & FTRACE_FL_DIRECT) {
2156 /*
2157 * If there's only one user (direct_ops helper)
2158 * then we can call the direct function
2159 * directly (no ftrace trampoline).
2160 */
2161 if (ftrace_rec_count(rec) == 1) {
2162 if (rec->flags & FTRACE_FL_DIRECT)
2163 rec->flags |= FTRACE_FL_DIRECT_EN;
2164 else
2165 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2166 } else {
2167 /*
2168 * Can only call directly if there's
2169 * only one callback to the function.
2170 */
2171 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2172 }
2173 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002174 }
2175
2176 /*
2177 * If this record is being updated from a nop, then
2178 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002179 * Otherwise,
2180 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002181 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002182 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002183 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002184 if (flag & FTRACE_FL_ENABLED) {
2185 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002186 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002187 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002188
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002189 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002190 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002191 }
2192
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002193 if (update) {
2194 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002195 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002196 rec->flags = 0;
2197 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002198 /*
2199 * Just disable the record, but keep the ops TRAMP
2200 * and REGS states. The _EN flags must be disabled though.
2201 */
2202 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002203 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002204 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002205
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002206 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002207 return FTRACE_UPDATE_MAKE_NOP;
2208}
2209
2210/**
2211 * ftrace_update_record, set a record that now is tracing or not
2212 * @rec: the record to update
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002213 * @enable: set to true if the record is tracing, false to force disable
Steven Rostedtc88fd862011-08-16 09:53:39 -04002214 *
2215 * The records that represent all functions that can be traced need
2216 * to be updated when tracing has been enabled.
2217 */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002218int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002219{
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002220 return ftrace_check_record(rec, enable, true);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002221}
2222
2223/**
2224 * ftrace_test_record, check if the record has been enabled or not
2225 * @rec: the record to test
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002226 * @enable: set to true to check if enabled, false if it is disabled
Steven Rostedtc88fd862011-08-16 09:53:39 -04002227 *
2228 * The arch code may need to test if a record is already set to
2229 * tracing to determine how to modify the function code that it
2230 * represents.
2231 */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002232int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002233{
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002234 return ftrace_check_record(rec, enable, false);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002235}
2236
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002237static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002238ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2239{
2240 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002241 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002242
2243 do_for_each_ftrace_op(op, ftrace_ops_list) {
2244
2245 if (!op->trampoline)
2246 continue;
2247
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002248 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002249 return op;
2250 } while_for_each_ftrace_op(op);
2251
2252 return NULL;
2253}
2254
2255static struct ftrace_ops *
Naveen N. Rao4c75b0f2020-11-26 23:38:38 +05302256ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2257{
2258 struct ftrace_ops *op;
2259 unsigned long ip = rec->ip;
2260
2261 do_for_each_ftrace_op(op, ftrace_ops_list) {
2262
2263 if (op == op_exclude || !op->trampoline)
2264 continue;
2265
2266 if (hash_contains_ip(ip, op->func_hash))
2267 return op;
2268 } while_for_each_ftrace_op(op);
2269
2270 return NULL;
2271}
2272
2273static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002274ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2275 struct ftrace_ops *op)
2276{
2277 unsigned long ip = rec->ip;
2278
2279 while_for_each_ftrace_op(op) {
2280
2281 if (!op->trampoline)
2282 continue;
2283
2284 if (hash_contains_ip(ip, op->func_hash))
2285 return op;
Kaitao Cheng026bb842020-05-29 22:12:14 +08002286 }
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002287
2288 return NULL;
2289}
2290
2291static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002292ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2293{
2294 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002295 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002296
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002297 /*
2298 * Need to check removed ops first.
2299 * If they are being removed, and this rec has a tramp,
2300 * and this rec is in the ops list, then it would be the
2301 * one with the tramp.
2302 */
2303 if (removed_ops) {
2304 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002305 return removed_ops;
2306 }
2307
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002308 /*
2309 * Need to find the current trampoline for a rec.
2310 * Now, a trampoline is only attached to a rec if there
2311 * was a single 'ops' attached to it. But this can be called
2312 * when we are adding another op to the rec or removing the
2313 * current one. Thus, if the op is being added, we can
2314 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002315 * yet.
2316 *
2317 * If an ops is being modified (hooking to different functions)
2318 * then we don't care about the new functions that are being
2319 * added, just the old ones (that are probably being removed).
2320 *
2321 * If we are adding an ops to a function that already is using
2322 * a trampoline, it needs to be removed (trampolines are only
2323 * for single ops connected), then an ops that is not being
2324 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002325 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002326 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002327
2328 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002329 continue;
2330
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002331 /*
2332 * If the ops is being added, it hasn't gotten to
2333 * the point to be removed from this tree yet.
2334 */
2335 if (op->flags & FTRACE_OPS_FL_ADDING)
2336 continue;
2337
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002338
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002339 /*
2340 * If the ops is being modified and is in the old
2341 * hash, then it is probably being removed from this
2342 * function.
2343 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002344 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2345 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002346 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002347 /*
2348 * If the ops is not being added or modified, and it's
2349 * in its normal filter hash, then this must be the one
2350 * we want!
2351 */
2352 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2353 hash_contains_ip(ip, op->func_hash))
2354 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002355
2356 } while_for_each_ftrace_op(op);
2357
2358 return NULL;
2359}
2360
2361static struct ftrace_ops *
2362ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2363{
2364 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002365 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002366
2367 do_for_each_ftrace_op(op, ftrace_ops_list) {
2368 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002369 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002370 return op;
2371 } while_for_each_ftrace_op(op);
2372
2373 return NULL;
2374}
2375
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002376#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2377/* Protected by rcu_tasks for reading, and direct_mutex for writing */
2378static struct ftrace_hash *direct_functions = EMPTY_HASH;
2379static DEFINE_MUTEX(direct_mutex);
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05002380int ftrace_direct_func_count;
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002381
2382/*
2383 * Search the direct_functions hash to see if the given instruction pointer
2384 * has a direct caller attached to it.
2385 */
Alexei Starovoitovff205762019-12-08 16:01:12 -08002386unsigned long ftrace_find_rec_direct(unsigned long ip)
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002387{
2388 struct ftrace_func_entry *entry;
2389
2390 entry = __ftrace_lookup_ip(direct_functions, ip);
2391 if (!entry)
2392 return 0;
2393
2394 return entry->direct;
2395}
2396
2397static void call_direct_funcs(unsigned long ip, unsigned long pip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04002398 struct ftrace_ops *ops, struct ftrace_regs *fregs)
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002399{
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04002400 struct pt_regs *regs = ftrace_get_regs(fregs);
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002401 unsigned long addr;
2402
Alexei Starovoitovff205762019-12-08 16:01:12 -08002403 addr = ftrace_find_rec_direct(ip);
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002404 if (!addr)
2405 return;
2406
2407 arch_ftrace_set_direct_caller(regs, addr);
2408}
2409
2410struct ftrace_ops direct_ops = {
2411 .func = call_direct_funcs,
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05002412 .flags = FTRACE_OPS_FL_IPMODIFY
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002413 | FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2414 | FTRACE_OPS_FL_PERMANENT,
Steven Rostedt (VMware)5da7cd12020-04-22 12:25:41 -04002415 /*
2416 * By declaring the main trampoline as this trampoline
2417 * it will never have one allocated for it. Allocated
2418 * trampolines should not call direct functions.
2419 * The direct_ops should only be called by the builtin
2420 * ftrace_regs_caller trampoline.
2421 */
2422 .trampoline = FTRACE_REGS_ADDR,
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002423};
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002424#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2425
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002426/**
2427 * ftrace_get_addr_new - Get the call address to set to
2428 * @rec: The ftrace record descriptor
2429 *
2430 * If the record has the FTRACE_FL_REGS set, that means that it
2431 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
Randy Dunlap5c8c2062020-08-06 20:32:59 -07002432 * is not set, then it wants to convert to the normal callback.
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002433 *
2434 * Returns the address of the trampoline to set to
2435 */
2436unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2437{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002438 struct ftrace_ops *ops;
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002439 unsigned long addr;
2440
2441 if ((rec->flags & FTRACE_FL_DIRECT) &&
2442 (ftrace_rec_count(rec) == 1)) {
Alexei Starovoitovff205762019-12-08 16:01:12 -08002443 addr = ftrace_find_rec_direct(rec->ip);
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002444 if (addr)
2445 return addr;
2446 WARN_ON_ONCE(1);
2447 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002448
2449 /* Trampolines take precedence over regs */
2450 if (rec->flags & FTRACE_FL_TRAMP) {
2451 ops = ftrace_find_tramp_ops_new(rec);
2452 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002453 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2454 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002455 /* Ftrace is shutting down, return anything */
2456 return (unsigned long)FTRACE_ADDR;
2457 }
2458 return ops->trampoline;
2459 }
2460
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002461 if (rec->flags & FTRACE_FL_REGS)
2462 return (unsigned long)FTRACE_REGS_ADDR;
2463 else
2464 return (unsigned long)FTRACE_ADDR;
2465}
2466
2467/**
2468 * ftrace_get_addr_curr - Get the call address that is already there
2469 * @rec: The ftrace record descriptor
2470 *
2471 * The FTRACE_FL_REGS_EN is set when the record already points to
2472 * a function that saves all the regs. Basically the '_EN' version
2473 * represents the current state of the function.
2474 *
2475 * Returns the address of the trampoline that is currently being called
2476 */
2477unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2478{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002479 struct ftrace_ops *ops;
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002480 unsigned long addr;
2481
2482 /* Direct calls take precedence over trampolines */
2483 if (rec->flags & FTRACE_FL_DIRECT_EN) {
Alexei Starovoitovff205762019-12-08 16:01:12 -08002484 addr = ftrace_find_rec_direct(rec->ip);
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05002485 if (addr)
2486 return addr;
2487 WARN_ON_ONCE(1);
2488 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002489
2490 /* Trampolines take precedence over regs */
2491 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2492 ops = ftrace_find_tramp_ops_curr(rec);
2493 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002494 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2495 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002496 /* Ftrace is shutting down, return anything */
2497 return (unsigned long)FTRACE_ADDR;
2498 }
2499 return ops->trampoline;
2500 }
2501
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002502 if (rec->flags & FTRACE_FL_REGS_EN)
2503 return (unsigned long)FTRACE_REGS_ADDR;
2504 else
2505 return (unsigned long)FTRACE_ADDR;
2506}
2507
Steven Rostedtc88fd862011-08-16 09:53:39 -04002508static int
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002509__ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002510{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002511 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002512 unsigned long ftrace_addr;
2513 int ret;
2514
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002515 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002516
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002517 /* This needs to be done before we call ftrace_update_record */
2518 ftrace_old_addr = ftrace_get_addr_curr(rec);
2519
2520 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002521
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002522 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2523
Steven Rostedtc88fd862011-08-16 09:53:39 -04002524 switch (ret) {
2525 case FTRACE_UPDATE_IGNORE:
2526 return 0;
2527
2528 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002529 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002530 return ftrace_make_call(rec, ftrace_addr);
2531
2532 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002533 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002534 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002535
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002536 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002537 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002538 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002539 }
2540
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05302541 return -1; /* unknown ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002542}
2543
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002544void __weak ftrace_replace_code(int mod_flags)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002545{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002546 struct dyn_ftrace *rec;
2547 struct ftrace_page *pg;
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002548 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002549 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002550 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002551
Steven Rostedt45a4a232011-04-21 23:16:46 -04002552 if (unlikely(ftrace_disabled))
2553 return;
2554
Steven Rostedt265c8312009-02-13 12:43:56 -05002555 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002556
2557 if (rec->flags & FTRACE_FL_DISABLED)
2558 continue;
2559
Steven Rostedte4f5d542012-04-27 09:13:18 -04002560 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002561 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002562 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002563 /* Stop processing */
2564 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002565 }
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002566 if (schedulable)
2567 cond_resched();
Steven Rostedt265c8312009-02-13 12:43:56 -05002568 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002569}
2570
Steven Rostedtc88fd862011-08-16 09:53:39 -04002571struct ftrace_rec_iter {
2572 struct ftrace_page *pg;
2573 int index;
2574};
2575
2576/**
2577 * ftrace_rec_iter_start, start up iterating over traced functions
2578 *
2579 * Returns an iterator handle that is used to iterate over all
2580 * the records that represent address locations where functions
2581 * are traced.
2582 *
2583 * May return NULL if no records are available.
2584 */
2585struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2586{
2587 /*
2588 * We only use a single iterator.
2589 * Protected by the ftrace_lock mutex.
2590 */
2591 static struct ftrace_rec_iter ftrace_rec_iter;
2592 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2593
2594 iter->pg = ftrace_pages_start;
2595 iter->index = 0;
2596
2597 /* Could have empty pages */
2598 while (iter->pg && !iter->pg->index)
2599 iter->pg = iter->pg->next;
2600
2601 if (!iter->pg)
2602 return NULL;
2603
2604 return iter;
2605}
2606
2607/**
2608 * ftrace_rec_iter_next, get the next record to process.
2609 * @iter: The handle to the iterator.
2610 *
2611 * Returns the next iterator after the given iterator @iter.
2612 */
2613struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2614{
2615 iter->index++;
2616
2617 if (iter->index >= iter->pg->index) {
2618 iter->pg = iter->pg->next;
2619 iter->index = 0;
2620
2621 /* Could have empty pages */
2622 while (iter->pg && !iter->pg->index)
2623 iter->pg = iter->pg->next;
2624 }
2625
2626 if (!iter->pg)
2627 return NULL;
2628
2629 return iter;
2630}
2631
2632/**
2633 * ftrace_rec_iter_record, get the record at the iterator location
2634 * @iter: The current iterator location
2635 *
2636 * Returns the record that the current @iter is at.
2637 */
2638struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2639{
2640 return &iter->pg->records[iter->index];
2641}
2642
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302643static int
Mark Rutlandfbf6c732019-10-16 17:51:10 +01002644ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002645{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002646 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002647
Steven Rostedt45a4a232011-04-21 23:16:46 -04002648 if (unlikely(ftrace_disabled))
2649 return 0;
2650
Mark Rutlandfbf6c732019-10-16 17:51:10 +01002651 ret = ftrace_init_nop(mod, rec);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002652 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002653 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002654 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302655 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002656 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302657 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002658}
2659
Steven Rostedt000ab692009-02-17 13:35:06 -05002660/*
2661 * archs can override this function if they must do something
2662 * before the modifying code is performed.
2663 */
2664int __weak ftrace_arch_code_modify_prepare(void)
2665{
2666 return 0;
2667}
2668
2669/*
2670 * archs can override this function if they must do something
2671 * after the modifying code is performed.
2672 */
2673int __weak ftrace_arch_code_modify_post_process(void)
2674{
2675 return 0;
2676}
2677
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002678void ftrace_modify_all_code(int command)
2679{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002680 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002681 int mod_flags = 0;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002682 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002683
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002684 if (command & FTRACE_MAY_SLEEP)
2685 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2686
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002687 /*
2688 * If the ftrace_caller calls a ftrace_ops func directly,
2689 * we need to make sure that it only traces functions it
2690 * expects to trace. When doing the switch of functions,
2691 * we need to update to the ftrace_ops_list_func first
2692 * before the transition between old and new calls are set,
2693 * as the ftrace_ops_list_func will check the ops hashes
2694 * to make sure the ops are having the right functions
2695 * traced.
2696 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002697 if (update) {
2698 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2699 if (FTRACE_WARN_ON(err))
2700 return;
2701 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002702
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002703 if (command & FTRACE_UPDATE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002704 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002705 else if (command & FTRACE_DISABLE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002706 ftrace_replace_code(mod_flags);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002707
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002708 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2709 function_trace_op = set_function_trace_op;
2710 smp_wmb();
2711 /* If irqs are disabled, we are in stop machine */
2712 if (!irqs_disabled())
2713 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002714 err = ftrace_update_ftrace_func(ftrace_trace_function);
2715 if (FTRACE_WARN_ON(err))
2716 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002717 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002718
2719 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002720 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002721 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002722 err = ftrace_disable_ftrace_graph_caller();
2723 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002724}
2725
Ingo Molnare309b412008-05-12 21:20:51 +02002726static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002727{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002728 int *command = data;
2729
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002730 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002731
Steven Rostedtc88fd862011-08-16 09:53:39 -04002732 return 0;
2733}
2734
2735/**
2736 * ftrace_run_stop_machine, go back to the stop machine method
2737 * @command: The command to tell ftrace what to do
2738 *
2739 * If an arch needs to fall back to the stop machine method, the
2740 * it can call this function.
2741 */
2742void ftrace_run_stop_machine(int command)
2743{
2744 stop_machine(__ftrace_modify_code, &command, NULL);
2745}
2746
2747/**
2748 * arch_ftrace_update_code, modify the code to trace or not trace
2749 * @command: The command that needs to be done
2750 *
2751 * Archs can override this function if it does not need to
2752 * run stop_machine() to modify code.
2753 */
2754void __weak arch_ftrace_update_code(int command)
2755{
2756 ftrace_run_stop_machine(command);
2757}
2758
2759static void ftrace_run_update_code(int command)
2760{
2761 int ret;
2762
2763 ret = ftrace_arch_code_modify_prepare();
2764 FTRACE_WARN_ON(ret);
2765 if (ret)
Petr Mladekd5b844a2019-06-27 10:13:34 +02002766 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002767
2768 /*
2769 * By default we use stop_machine() to modify the code.
2770 * But archs can do what ever they want as long as it
2771 * is safe. The stop_machine() is the safest, but also
2772 * produces the most overhead.
2773 */
2774 arch_ftrace_update_code(command);
2775
Steven Rostedt000ab692009-02-17 13:35:06 -05002776 ret = ftrace_arch_code_modify_post_process();
2777 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002778}
2779
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002780static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002781 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002782{
2783 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002784 ops->old_hash.filter_hash = old_hash->filter_hash;
2785 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002786 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002787 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002788 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002789 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2790}
2791
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002792static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002793static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002794
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002795void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2796{
2797}
2798
Adrian Hunterfc0ea792020-05-12 15:19:13 +03002799/* List of trace_ops that have allocated trampolines */
2800static LIST_HEAD(ftrace_ops_trampoline_list);
2801
2802static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
2803{
2804 lockdep_assert_held(&ftrace_lock);
2805 list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
2806}
2807
2808static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
2809{
2810 lockdep_assert_held(&ftrace_lock);
2811 list_del_rcu(&ops->list);
Adrian Hunter478ece92020-09-01 12:16:17 +03002812 synchronize_rcu();
Adrian Hunterfc0ea792020-05-12 15:19:13 +03002813}
2814
2815/*
2816 * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
2817 * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
2818 * not a module.
2819 */
2820#define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
2821#define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
2822
2823static void ftrace_trampoline_free(struct ftrace_ops *ops)
2824{
2825 if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03002826 ops->trampoline) {
Adrian Hunter548e1f62020-05-12 15:19:15 +03002827 /*
2828 * Record the text poke event before the ksymbol unregister
2829 * event.
2830 */
2831 perf_event_text_poke((void *)ops->trampoline,
2832 (void *)ops->trampoline,
2833 ops->trampoline_size, NULL, 0);
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03002834 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
2835 ops->trampoline, ops->trampoline_size,
2836 true, FTRACE_TRAMPOLINE_SYM);
2837 /* Remove from kallsyms after the perf events */
Adrian Hunterfc0ea792020-05-12 15:19:13 +03002838 ftrace_remove_trampoline_from_kallsyms(ops);
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03002839 }
Adrian Hunterfc0ea792020-05-12 15:19:13 +03002840
2841 arch_ftrace_trampoline_free(ops);
2842}
2843
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002844static void ftrace_startup_enable(int command)
2845{
2846 if (saved_ftrace_func != ftrace_trace_function) {
2847 saved_ftrace_func = ftrace_trace_function;
2848 command |= FTRACE_UPDATE_TRACE_FUNC;
2849 }
2850
2851 if (!command || !ftrace_enabled)
2852 return;
2853
2854 ftrace_run_update_code(command);
2855}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002856
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002857static void ftrace_startup_all(int command)
2858{
2859 update_all_ops = true;
2860 ftrace_startup_enable(command);
2861 update_all_ops = false;
2862}
2863
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002864int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002865{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002866 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002867
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002868 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002869 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002870
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002871 ret = __register_ftrace_function(ops);
2872 if (ret)
2873 return ret;
2874
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002875 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002876
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002877 /*
2878 * Note that ftrace probes uses this to start up
2879 * and modify functions it will probe. But we still
2880 * set the ADDING flag for modification, as probes
2881 * do not have trampolines. If they add them in the
2882 * future, then the probes will need to distinguish
2883 * between adding and updating probes.
2884 */
2885 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002886
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002887 ret = ftrace_hash_ipmodify_enable(ops);
2888 if (ret < 0) {
2889 /* Rollback registration process */
2890 __unregister_ftrace_function(ops);
2891 ftrace_start_up--;
2892 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Miroslav Benesd5e47502020-08-31 14:26:31 +02002893 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2894 ftrace_trampoline_free(ops);
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002895 return ret;
2896 }
2897
Jiri Olsa7f50d062016-03-16 15:34:33 +01002898 if (ftrace_hash_rec_enable(ops, 1))
2899 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002900
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002901 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002902
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002903 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2904
Steven Rostedta1cd6172011-05-23 15:24:25 -04002905 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002906}
2907
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002908int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002909{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002910 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002911
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002912 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002913 return -ENODEV;
2914
2915 ret = __unregister_ftrace_function(ops);
2916 if (ret)
2917 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002918
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002919 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002920 /*
2921 * Just warn in case of unbalance, no need to kill ftrace, it's not
2922 * critical but the ftrace_call callers may be never nopped again after
2923 * further ftrace uses.
2924 */
2925 WARN_ON_ONCE(ftrace_start_up < 0);
2926
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002927 /* Disabling ipmodify never fails */
2928 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002929
2930 if (ftrace_hash_rec_disable(ops, 1))
2931 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002932
Namhyung Kima737e6d2014-06-12 23:56:12 +09002933 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002934
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002935 if (saved_ftrace_func != ftrace_trace_function) {
2936 saved_ftrace_func = ftrace_trace_function;
2937 command |= FTRACE_UPDATE_TRACE_FUNC;
2938 }
2939
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002940 if (!command || !ftrace_enabled) {
2941 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002942 * If these are dynamic or per_cpu ops, they still
2943 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002944 * not currently active, we can just free them
2945 * without synchronizing all CPUs.
2946 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002947 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002948 goto free_ops;
2949
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002950 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002951 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002952
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002953 /*
2954 * If the ops uses a trampoline, then it needs to be
2955 * tested first on update.
2956 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002957 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002958 removed_ops = ops;
2959
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002960 /* The trampoline logic checks the old hashes */
2961 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2962 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2963
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002964 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002965
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002966 /*
2967 * If there's no more ops registered with ftrace, run a
2968 * sanity check to make sure all rec flags are cleared.
2969 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002970 if (rcu_dereference_protected(ftrace_ops_list,
2971 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002972 struct ftrace_page *pg;
2973 struct dyn_ftrace *rec;
2974
2975 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002976 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002977 pr_warn(" %pS flags:%lx\n",
2978 (void *)rec->ip, rec->flags);
2979 } while_for_each_ftrace_rec();
2980 }
2981
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002982 ops->old_hash.filter_hash = NULL;
2983 ops->old_hash.notrace_hash = NULL;
2984
2985 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002986 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002987
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002988 /*
2989 * Dynamic ops may be freed, we must make sure that all
2990 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002991 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002992 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002993 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002994 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002995 /*
2996 * We need to do a hard force of sched synchronization.
2997 * This is because we use preempt_disable() to do RCU, but
2998 * the function tracers can be called where RCU is not watching
2999 * (like before user_exit()). We can not rely on the RCU
3000 * infrastructure to do the synchronization, thus we must do it
3001 * ourselves.
3002 */
Paul E. McKenneye5a971d2020-04-03 12:10:28 -07003003 synchronize_rcu_tasks_rude();
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05003004
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04003005 /*
Qiujun Huangfdda88d2020-10-02 22:31:26 +08003006 * When the kernel is preemptive, tasks can be preempted
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04003007 * while on a ftrace trampoline. Just scheduling a task on
3008 * a CPU is not good enough to flush them. Calling
Ingo Molnarf2cc0202021-03-23 18:49:35 +01003009 * synchronize_rcu_tasks() will wait for those tasks to
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04003010 * execute and either schedule voluntarily or enter user space.
3011 */
Thomas Gleixner30c93702019-07-26 23:19:40 +02003012 if (IS_ENABLED(CONFIG_PREEMPTION))
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04003013 synchronize_rcu_tasks();
3014
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04003015 free_ops:
Adrian Hunterfc0ea792020-05-12 15:19:13 +03003016 ftrace_trampoline_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05003017 }
3018
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003019 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02003020}
3021
Ingo Molnare309b412008-05-12 21:20:51 +02003022static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003023{
Pratyush Anand1619dc32015-03-06 23:58:06 +05303024 int command;
3025
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003026 if (unlikely(ftrace_disabled))
3027 return;
3028
Steven Rostedtd61f82d2008-05-12 21:20:43 +02003029 /* Force update next time */
3030 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05003031 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05303032 if (ftrace_start_up) {
3033 command = FTRACE_UPDATE_CALLS;
3034 if (ftrace_graph_active)
3035 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05003036 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05303037 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003038}
3039
Ingo Molnare309b412008-05-12 21:20:51 +02003040static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003041{
Pratyush Anand1619dc32015-03-06 23:58:06 +05303042 int command;
3043
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003044 if (unlikely(ftrace_disabled))
3045 return;
3046
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05003047 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05303048 if (ftrace_start_up) {
3049 command = FTRACE_DISABLE_CALLS;
3050 if (ftrace_graph_active)
3051 command |= FTRACE_STOP_FUNC_RET;
3052 ftrace_run_update_code(command);
3053 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003054}
3055
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01003056static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02003057unsigned long ftrace_update_tot_cnt;
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04003058unsigned long ftrace_number_of_pages;
3059unsigned long ftrace_number_of_groups;
Steven Rostedt3d083392008-05-12 21:20:42 +02003060
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003061static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04003062{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003063 /*
3064 * Filter_hash being empty will default to trace module.
3065 * But notrace hash requires a test of individual module functions.
3066 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003067 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
3068 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003069}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04003070
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003071/*
3072 * Check if the current ops references the record.
3073 *
3074 * If the ops traces all functions, then it was already accounted for.
3075 * If the ops does not trace the current record function, skip it.
3076 * If the ops ignores the function via notrace filter, skip it.
3077 */
3078static inline bool
3079ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3080{
3081 /* If ops isn't enabled, ignore it */
3082 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003083 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003084
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003085 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003086 if (ops_traces_mod(ops))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003087 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003088
3089 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003090 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05003091 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003092 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003093
3094 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003095 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003096 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003097
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003098 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003099}
3100
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003101static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02003102{
Ilya Leoshkevich67ccddf2021-07-28 23:25:45 +02003103 bool init_nop = ftrace_need_init_nop();
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003104 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08003105 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01003106 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003107 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003108 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003109 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04003110
Ingo Molnar750ed1a2008-05-12 21:20:46 +02003111 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02003112
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003113 /*
3114 * When a module is loaded, this function is called to convert
3115 * the calls to mcount in its text to nops, and also to create
3116 * an entry in the ftrace data. Now, if ftrace is activated
3117 * after this call, but before the module sets its text to
3118 * read-only, the modification of enabling ftrace can fail if
3119 * the read-only is done while ftrace is converting the calls.
3120 * To prevent this, the module's records are set as disabled
3121 * and will be enabled after the call to set the module's text
3122 * to read-only.
3123 */
3124 if (mod)
3125 rec_flags |= FTRACE_FL_DISABLED;
3126
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003127 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05303128
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003129 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003130
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003131 /* If something went wrong, bail without enabling anything */
3132 if (unlikely(ftrace_disabled))
3133 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02003134
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003135 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003136 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05303137
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003138 /*
3139 * Do the initial record conversion from mcount jump
3140 * to the NOP instructions.
3141 */
Ilya Leoshkevich67ccddf2021-07-28 23:25:45 +02003142 if (init_nop && !ftrace_nop_initialize(mod, p))
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003143 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003144
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003145 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003146 }
Steven Rostedt3d083392008-05-12 21:20:42 +02003147 }
3148
Ingo Molnar750ed1a2008-05-12 21:20:46 +02003149 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02003150 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003151 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02003152
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003153 return 0;
3154}
3155
Steven Rostedta7900872011-12-16 16:23:44 -05003156static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003157{
Steven Rostedta7900872011-12-16 16:23:44 -05003158 int order;
Steven Rostedt (VMware)7ba031e2020-10-05 20:37:41 -04003159 int pages;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003160 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003161
Steven Rostedta7900872011-12-16 16:23:44 -05003162 if (WARN_ON(!count))
3163 return -EINVAL;
3164
Steven Rostedt (VMware)ceaaa122021-04-01 16:40:32 -04003165 /* We want to fill as much as possible, with no empty pages */
Wei Yangb40c6ea2020-08-31 11:11:02 +08003166 pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
Steven Rostedt (VMware)ceaaa122021-04-01 16:40:32 -04003167 order = fls(pages) - 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003168
Steven Rostedta7900872011-12-16 16:23:44 -05003169 again:
3170 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3171
3172 if (!pg->records) {
3173 /* if we can't allocate this size, try something smaller */
3174 if (!order)
3175 return -ENOMEM;
3176 order >>= 1;
3177 goto again;
3178 }
3179
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04003180 ftrace_number_of_pages += 1 << order;
3181 ftrace_number_of_groups++;
3182
Steven Rostedta7900872011-12-16 16:23:44 -05003183 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
Linus Torvaldsdb425232021-04-01 16:14:17 -04003184 pg->order = order;
Steven Rostedta7900872011-12-16 16:23:44 -05003185
3186 if (cnt > count)
3187 cnt = count;
3188
3189 return cnt;
3190}
3191
3192static struct ftrace_page *
3193ftrace_allocate_pages(unsigned long num_to_init)
3194{
3195 struct ftrace_page *start_pg;
3196 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003197 int cnt;
3198
3199 if (!num_to_init)
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05303200 return NULL;
Steven Rostedta7900872011-12-16 16:23:44 -05003201
3202 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3203 if (!pg)
3204 return NULL;
3205
3206 /*
3207 * Try to allocate as much as possible in one continues
3208 * location that fills in all of the space. We want to
3209 * waste as little space as possible.
3210 */
3211 for (;;) {
3212 cnt = ftrace_allocate_records(pg, num_to_init);
3213 if (cnt < 0)
3214 goto free_pages;
3215
3216 num_to_init -= cnt;
3217 if (!num_to_init)
3218 break;
3219
3220 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3221 if (!pg->next)
3222 goto free_pages;
3223
3224 pg = pg->next;
3225 }
3226
3227 return start_pg;
3228
3229 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003230 pg = start_pg;
3231 while (pg) {
Linus Torvaldsdb425232021-04-01 16:14:17 -04003232 if (pg->records) {
3233 free_pages((unsigned long)pg->records, pg->order);
3234 ftrace_number_of_pages -= 1 << pg->order;
3235 }
Steven Rostedta7900872011-12-16 16:23:44 -05003236 start_pg = pg->next;
3237 kfree(pg);
3238 pg = start_pg;
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04003239 ftrace_number_of_groups--;
Steven Rostedta7900872011-12-16 16:23:44 -05003240 }
3241 pr_info("ftrace: FAILED to allocate memory for functions\n");
3242 return NULL;
3243}
3244
Steven Rostedt5072c592008-05-12 21:20:43 +02003245#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3246
3247struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003248 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003249 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003250 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003251 struct ftrace_page *pg;
3252 struct dyn_ftrace *func;
3253 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003254 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003255 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003256 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003257 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003258 struct trace_array *tr;
3259 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003260 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003261 int idx;
3262 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003263};
3264
Ingo Molnare309b412008-05-12 21:20:51 +02003265static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003266t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003267{
3268 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003269 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003270 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003271 struct ftrace_hash *hash;
3272 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003273 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003274 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003275 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003276
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003277 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003278 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003279
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003280 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003281 return NULL;
3282
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003283 func_probes = &tr->func_probes;
3284 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003285 return NULL;
3286
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003287 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003288 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003289 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003290 }
3291
3292 if (iter->probe_entry)
3293 hnd = &iter->probe_entry->hlist;
3294
3295 hash = iter->probe->ops.func_hash->filter_hash;
Naveen N. Rao7bd46642019-07-04 20:04:41 +05303296
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04003297 /*
3298 * A probe being registered may temporarily have an empty hash
3299 * and it's at the end of the func_probes list.
3300 */
3301 if (!hash || hash == EMPTY_HASH)
Naveen N. Rao7bd46642019-07-04 20:04:41 +05303302 return NULL;
3303
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003304 size = 1 << hash->size_bits;
3305
3306 retry:
3307 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003308 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003309 return NULL;
3310 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003311 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003312 hash = iter->probe->ops.func_hash->filter_hash;
3313 size = 1 << hash->size_bits;
3314 iter->pidx = 0;
3315 }
3316
3317 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003318
3319 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003320 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003321 hnd = NULL;
3322 goto retry;
3323 }
3324
3325 if (!hnd)
3326 hnd = hhd->first;
3327 else {
3328 hnd = hnd->next;
3329 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003330 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003331 goto retry;
3332 }
3333 }
3334
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003335 if (WARN_ON_ONCE(!hnd))
3336 return NULL;
3337
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003338 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003339
3340 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003341}
3342
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003343static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003344{
3345 struct ftrace_iterator *iter = m->private;
3346 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003347 loff_t l;
3348
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003349 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003350 return NULL;
3351
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003352 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003353 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003354
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003355 iter->probe = NULL;
3356 iter->probe_entry = NULL;
3357 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003358 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003359 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003360 if (!p)
3361 break;
3362 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003363 if (!p)
3364 return NULL;
3365
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003366 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003367 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003368
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003369 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003370}
3371
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003372static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003373t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003374{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003375 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003376 struct ftrace_probe_ops *probe_ops;
3377 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003378
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003379 probe = iter->probe;
3380 probe_entry = iter->probe_entry;
3381
3382 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003383 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003384
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003385 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003386
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003387 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003388 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003389
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003390 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3391 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003392
3393 return 0;
3394}
3395
3396static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003397t_mod_next(struct seq_file *m, loff_t *pos)
3398{
3399 struct ftrace_iterator *iter = m->private;
3400 struct trace_array *tr = iter->tr;
3401
3402 (*pos)++;
3403 iter->pos = *pos;
3404
3405 iter->mod_list = iter->mod_list->next;
3406
3407 if (iter->mod_list == &tr->mod_trace ||
3408 iter->mod_list == &tr->mod_notrace) {
3409 iter->flags &= ~FTRACE_ITER_MOD;
3410 return NULL;
3411 }
3412
3413 iter->mod_pos = *pos;
3414
3415 return iter;
3416}
3417
3418static void *t_mod_start(struct seq_file *m, loff_t *pos)
3419{
3420 struct ftrace_iterator *iter = m->private;
3421 void *p = NULL;
3422 loff_t l;
3423
3424 if (iter->func_pos > *pos)
3425 return NULL;
3426
3427 iter->mod_pos = iter->func_pos;
3428
3429 /* probes are only available if tr is set */
3430 if (!iter->tr)
3431 return NULL;
3432
3433 for (l = 0; l <= (*pos - iter->func_pos); ) {
3434 p = t_mod_next(m, &l);
3435 if (!p)
3436 break;
3437 }
3438 if (!p) {
3439 iter->flags &= ~FTRACE_ITER_MOD;
3440 return t_probe_start(m, pos);
3441 }
3442
3443 /* Only set this if we have an item */
3444 iter->flags |= FTRACE_ITER_MOD;
3445
3446 return iter;
3447}
3448
3449static int
3450t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3451{
3452 struct ftrace_mod_load *ftrace_mod;
3453 struct trace_array *tr = iter->tr;
3454
3455 if (WARN_ON_ONCE(!iter->mod_list) ||
3456 iter->mod_list == &tr->mod_trace ||
3457 iter->mod_list == &tr->mod_notrace)
3458 return -EIO;
3459
3460 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3461
3462 if (ftrace_mod->func)
3463 seq_printf(m, "%s", ftrace_mod->func);
3464 else
3465 seq_putc(m, '*');
3466
3467 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3468
3469 return 0;
3470}
3471
3472static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003473t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003474{
3475 struct ftrace_iterator *iter = m->private;
3476 struct dyn_ftrace *rec = NULL;
3477
3478 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003479
Steven Rostedt5072c592008-05-12 21:20:43 +02003480 retry:
3481 if (iter->idx >= iter->pg->index) {
3482 if (iter->pg->next) {
3483 iter->pg = iter->pg->next;
3484 iter->idx = 0;
3485 goto retry;
3486 }
3487 } else {
3488 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003489 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3490 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003491
3492 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003493 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003494
Steven Rostedt5072c592008-05-12 21:20:43 +02003495 rec = NULL;
3496 goto retry;
3497 }
3498 }
3499
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003500 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003501 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003502
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003503 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003504 iter->func = rec;
3505
3506 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003507}
3508
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003509static void *
3510t_next(struct seq_file *m, void *v, loff_t *pos)
3511{
3512 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003513 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003514 void *ret;
3515
3516 if (unlikely(ftrace_disabled))
3517 return NULL;
3518
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003519 if (iter->flags & FTRACE_ITER_PROBE)
3520 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003521
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003522 if (iter->flags & FTRACE_ITER_MOD)
3523 return t_mod_next(m, pos);
3524
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003525 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003526 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003527 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003528 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003529 }
3530
3531 ret = t_func_next(m, pos);
3532
3533 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003534 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003535
3536 return ret;
3537}
3538
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003539static void reset_iter_read(struct ftrace_iterator *iter)
3540{
3541 iter->pos = 0;
3542 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003543 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003544}
3545
3546static void *t_start(struct seq_file *m, loff_t *pos)
3547{
3548 struct ftrace_iterator *iter = m->private;
3549 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003550 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003551
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003552 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003553
3554 if (unlikely(ftrace_disabled))
3555 return NULL;
3556
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003557 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003558 * If an lseek was done, then reset and start from beginning.
3559 */
3560 if (*pos < iter->pos)
3561 reset_iter_read(iter);
3562
3563 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003564 * For set_ftrace_filter reading, if we have the filter
3565 * off, we can short cut and just print out that all
3566 * functions are enabled.
3567 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003568 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3569 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003570 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003571 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003572 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003573 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003574 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003575 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003576 return iter;
3577 }
3578
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003579 if (iter->flags & FTRACE_ITER_MOD)
3580 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003581
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003582 /*
3583 * Unfortunately, we need to restart at ftrace_pages_start
3584 * every time we let go of the ftrace_mutex. This is because
3585 * those pointers can change without the lock.
3586 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003587 iter->pg = ftrace_pages_start;
3588 iter->idx = 0;
3589 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003590 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003591 if (!p)
3592 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003593 }
walimis5821e1b2008-11-15 15:19:06 +08003594
Steven Rostedt69a30832011-12-19 15:21:16 -05003595 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003596 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003597
3598 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003599}
3600
3601static void t_stop(struct seq_file *m, void *p)
3602{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003603 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003604}
3605
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003606void * __weak
3607arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3608{
3609 return NULL;
3610}
3611
3612static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3613 struct dyn_ftrace *rec)
3614{
3615 void *ptr;
3616
3617 ptr = arch_ftrace_trampoline_func(ops, rec);
3618 if (ptr)
3619 seq_printf(m, " ->%pS", ptr);
3620}
3621
Steven Rostedt5072c592008-05-12 21:20:43 +02003622static int t_show(struct seq_file *m, void *v)
3623{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003624 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003625 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003626
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003627 if (iter->flags & FTRACE_ITER_PROBE)
3628 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003629
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003630 if (iter->flags & FTRACE_ITER_MOD)
3631 return t_mod_show(m, iter);
3632
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003633 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003634 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003635 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003636 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003637 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003638 return 0;
3639 }
3640
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003641 rec = iter->func;
3642
Steven Rostedt5072c592008-05-12 21:20:43 +02003643 if (!rec)
3644 return 0;
3645
Steven Rostedt647bcd02011-05-03 14:39:21 -04003646 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003647 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003648 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003649
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05003650 seq_printf(m, " (%ld)%s%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003651 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003652 rec->flags & FTRACE_FL_REGS ? " R" : " ",
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05003653 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3654 rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003655 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003656 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003657 if (ops) {
3658 do {
3659 seq_printf(m, "\ttramp: %pS (%pS)",
3660 (void *)ops->trampoline,
3661 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003662 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003663 ops = ftrace_find_tramp_ops_next(rec, ops);
3664 } while (ops);
3665 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003666 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003667 } else {
3668 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003669 }
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05003670 if (rec->flags & FTRACE_FL_DIRECT) {
3671 unsigned long direct;
3672
Alexei Starovoitovff205762019-12-08 16:01:12 -08003673 direct = ftrace_find_rec_direct(rec->ip);
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05003674 if (direct)
3675 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3676 }
Kaitao Cheng026bb842020-05-29 22:12:14 +08003677 }
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003678
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003679 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003680
3681 return 0;
3682}
3683
James Morris88e9d342009-09-22 16:43:43 -07003684static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003685 .start = t_start,
3686 .next = t_next,
3687 .stop = t_stop,
3688 .show = t_show,
3689};
3690
Ingo Molnare309b412008-05-12 21:20:51 +02003691static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003692ftrace_avail_open(struct inode *inode, struct file *file)
3693{
3694 struct ftrace_iterator *iter;
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003695 int ret;
3696
3697 ret = security_locked_down(LOCKDOWN_TRACEFS);
3698 if (ret)
3699 return ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003700
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003701 if (unlikely(ftrace_disabled))
3702 return -ENODEV;
3703
Jiri Olsa50e18b92012-04-25 10:23:39 +02003704 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003705 if (!iter)
3706 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003707
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003708 iter->pg = ftrace_pages_start;
3709 iter->ops = &global_ops;
3710
3711 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003712}
3713
Steven Rostedt647bcd02011-05-03 14:39:21 -04003714static int
3715ftrace_enabled_open(struct inode *inode, struct file *file)
3716{
3717 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003718
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003719 /*
3720 * This shows us what functions are currently being
3721 * traced and by what. Not sure if we want lockdown
3722 * to hide such critical information for an admin.
3723 * Although, perhaps it can show information we don't
3724 * want people to see, but if something is tracing
3725 * something, we probably want to know about it.
3726 */
3727
Jiri Olsa50e18b92012-04-25 10:23:39 +02003728 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003729 if (!iter)
3730 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003731
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003732 iter->pg = ftrace_pages_start;
3733 iter->flags = FTRACE_ITER_ENABLED;
3734 iter->ops = &global_ops;
3735
3736 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003737}
3738
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003739/**
3740 * ftrace_regex_open - initialize function tracer filter files
3741 * @ops: The ftrace_ops that hold the hash filters
3742 * @flag: The type of filter to process
3743 * @inode: The inode, usually passed in to your open routine
3744 * @file: The file, usually passed in to your open routine
3745 *
3746 * ftrace_regex_open() initializes the filter files for the
3747 * @ops. Depending on @flag it may process the filter hash or
3748 * the notrace hash of @ops. With this called from the open
3749 * routine, you can use ftrace_filter_write() for the write
3750 * routine if @flag has FTRACE_ITER_FILTER set, or
3751 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c879e2013-12-21 17:39:40 -05003752 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003753 * release must call ftrace_regex_release().
3754 */
3755int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003756ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003757 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003758{
3759 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003760 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003761 struct list_head *mod_head;
3762 struct trace_array *tr = ops->private;
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003763 int ret = -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003764
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003765 ftrace_ops_init(ops);
3766
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003767 if (unlikely(ftrace_disabled))
3768 return -ENODEV;
3769
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04003770 if (tracing_check_open_get_tr(tr))
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003771 return -ENODEV;
3772
Steven Rostedt5072c592008-05-12 21:20:43 +02003773 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3774 if (!iter)
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003775 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003776
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003777 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3778 goto out;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003779
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003780 iter->ops = ops;
3781 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003782 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003783
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003784 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003785
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003786 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003787 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003788 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003789 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003790 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003791 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003792 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003793
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003794 iter->mod_list = mod_head;
3795
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003796 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003797 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3798
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003799 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003800 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003801 clear_ftrace_mod_list(mod_head);
3802 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003803 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003804 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003805
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003806 if (!iter->hash) {
3807 trace_parser_put(&iter->parser);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003808 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003809 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003810 } else
3811 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003812
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003813 ret = 0;
3814
Steven Rostedt5072c592008-05-12 21:20:43 +02003815 if (file->f_mode & FMODE_READ) {
3816 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003817
3818 ret = seq_open(file, &show_ftrace_seq_ops);
3819 if (!ret) {
3820 struct seq_file *m = file->private_data;
3821 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003822 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003823 /* Failed */
3824 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003825 trace_parser_put(&iter->parser);
Li Zefan79fe2492009-09-22 13:54:28 +08003826 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003827 } else
3828 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003829
3830 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003831 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003832
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003833 out:
3834 if (ret) {
3835 kfree(iter);
3836 if (tr)
3837 trace_array_put(tr);
3838 }
3839
Steven Rostedt5072c592008-05-12 21:20:43 +02003840 return ret;
3841}
3842
Steven Rostedt41c52c02008-05-22 11:46:33 -04003843static int
3844ftrace_filter_open(struct inode *inode, struct file *file)
3845{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003846 struct ftrace_ops *ops = inode->i_private;
3847
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003848 /* Checks for tracefs lockdown */
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003849 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003850 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003851 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003852}
3853
3854static int
3855ftrace_notrace_open(struct inode *inode, struct file *file)
3856{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003857 struct ftrace_ops *ops = inode->i_private;
3858
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003859 /* Checks for tracefs lockdown */
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003860 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003861 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003862}
3863
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003864/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3865struct ftrace_glob {
3866 char *search;
3867 unsigned len;
3868 int type;
3869};
3870
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003871/*
3872 * If symbols in an architecture don't correspond exactly to the user-visible
3873 * name of what they represent, it is possible to define this function to
3874 * perform the necessary adjustments.
3875*/
3876char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3877{
3878 return str;
3879}
3880
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003881static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003882{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003883 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003884 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003885
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003886 str = arch_ftrace_match_adjust(str, g->search);
3887
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003888 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003889 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003890 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003891 matched = 1;
3892 break;
3893 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003894 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003895 matched = 1;
3896 break;
3897 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003898 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003899 matched = 1;
3900 break;
3901 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003902 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003903 if (slen >= g->len &&
3904 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003905 matched = 1;
3906 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003907 case MATCH_GLOB:
3908 if (glob_match(g->search, str))
3909 matched = 1;
3910 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003911 }
3912
3913 return matched;
3914}
3915
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003916static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003917enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003918{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003919 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003920 int ret = 0;
3921
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003922 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003923 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003924 /* Do nothing if it doesn't exist */
3925 if (!entry)
3926 return 0;
3927
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003928 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003929 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003930 /* Do nothing if it exists */
3931 if (entry)
3932 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003933
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003934 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003935 }
3936 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003937}
3938
Steven Rostedt64e7c442009-02-13 17:08:48 -05003939static int
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003940add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3941 int clear_filter)
3942{
3943 long index = simple_strtoul(func_g->search, NULL, 0);
3944 struct ftrace_page *pg;
3945 struct dyn_ftrace *rec;
3946
3947 /* The index starts at 1 */
3948 if (--index < 0)
3949 return 0;
3950
3951 do_for_each_ftrace_rec(pg, rec) {
3952 if (pg->index <= index) {
3953 index -= pg->index;
3954 /* this is a double loop, break goes to the next page */
3955 break;
3956 }
3957 rec = &pg->records[index];
3958 enter_record(hash, rec, clear_filter);
3959 return 1;
3960 } while_for_each_ftrace_rec();
3961 return 0;
3962}
3963
3964static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003965ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3966 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003967{
3968 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003969 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003970
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003971 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3972
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003973 if (mod_g) {
3974 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3975
3976 /* blank module name to match all modules */
3977 if (!mod_g->len) {
3978 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003979 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003980 goto func_match;
3981 return 0;
3982 }
3983
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003984 /*
3985 * exclude_mod is set to trace everything but the given
3986 * module. If it is set and the module matches, then
3987 * return 0. If it is not set, and the module doesn't match
3988 * also return 0. Otherwise, check the function to see if
3989 * that matches.
3990 */
3991 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003992 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003993func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003994 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003995 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003996 return 1;
3997 }
3998
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003999 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004000}
4001
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004002static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004003match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004004{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004005 struct ftrace_page *pg;
4006 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004007 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004008 struct ftrace_glob mod_g = { .type = MATCH_FULL };
4009 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
4010 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08004011 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004012 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03004013 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004014
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004015 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004016 func_g.type = filter_parse_regex(func, len, &func_g.search,
4017 &clear_filter);
4018 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004019 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004020
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004021 if (mod) {
4022 mod_g.type = filter_parse_regex(mod, strlen(mod),
4023 &mod_g.search, &exclude_mod);
4024 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004025 }
4026
Steven Rostedt52baf112009-02-14 01:15:39 -05004027 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004028
4029 if (unlikely(ftrace_disabled))
4030 goto out_unlock;
4031
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05004032 if (func_g.type == MATCH_INDEX) {
4033 found = add_rec_by_index(hash, &func_g, clear_filter);
4034 goto out_unlock;
4035 }
4036
Steven Rostedt265c8312009-02-13 12:43:56 -05004037 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004038
4039 if (rec->flags & FTRACE_FL_DISABLED)
4040 continue;
4041
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004042 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004043 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004044 if (ret < 0) {
4045 found = ret;
4046 goto out_unlock;
4047 }
Li Zefan311d16d2009-12-08 11:15:11 +08004048 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05004049 }
4050 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004051 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05004052 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08004053
4054 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02004055}
4056
Steven Rostedt64e7c442009-02-13 17:08:48 -05004057static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004058ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004059{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004060 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004061}
4062
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004063static void ftrace_ops_update_code(struct ftrace_ops *ops,
4064 struct ftrace_ops_hash *old_hash)
4065{
4066 struct ftrace_ops *op;
4067
4068 if (!ftrace_enabled)
4069 return;
4070
4071 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4072 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4073 return;
4074 }
4075
4076 /*
4077 * If this is the shared global_ops filter, then we need to
4078 * check if there is another ops that shares it, is enabled.
4079 * If so, we still need to run the modify code.
4080 */
4081 if (ops->func_hash != &global_ops.local_hash)
4082 return;
4083
4084 do_for_each_ftrace_op(op, ftrace_ops_list) {
4085 if (op->func_hash == &global_ops.local_hash &&
4086 op->flags & FTRACE_OPS_FL_ENABLED) {
4087 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4088 /* Only need to do this once */
4089 return;
4090 }
4091 } while_for_each_ftrace_op(op);
4092}
4093
4094static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4095 struct ftrace_hash **orig_hash,
4096 struct ftrace_hash *hash,
4097 int enable)
4098{
4099 struct ftrace_ops_hash old_hash_ops;
4100 struct ftrace_hash *old_hash;
4101 int ret;
4102
4103 old_hash = *orig_hash;
4104 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4105 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4106 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4107 if (!ret) {
4108 ftrace_ops_update_code(ops, &old_hash_ops);
4109 free_ftrace_hash_rcu(old_hash);
4110 }
4111 return ret;
4112}
Steven Rostedt64e7c442009-02-13 17:08:48 -05004113
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004114static bool module_exists(const char *module)
4115{
4116 /* All modules have the symbol __this_module */
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01004117 static const char this_mod[] = "__this_module";
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02004118 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004119 unsigned long val;
4120 int n;
4121
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02004122 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004123
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02004124 if (n > sizeof(modname) - 1)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004125 return false;
4126
4127 val = module_kallsyms_lookup_name(modname);
4128 return val != 0;
4129}
4130
4131static int cache_mod(struct trace_array *tr,
4132 const char *func, char *module, int enable)
4133{
4134 struct ftrace_mod_load *ftrace_mod, *n;
4135 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4136 int ret;
4137
4138 mutex_lock(&ftrace_lock);
4139
4140 /* We do not cache inverse filters */
4141 if (func[0] == '!') {
4142 func++;
4143 ret = -EINVAL;
4144
4145 /* Look to remove this hash */
4146 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4147 if (strcmp(ftrace_mod->module, module) != 0)
4148 continue;
4149
4150 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03004151 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004152 (ftrace_mod->func &&
4153 strcmp(ftrace_mod->func, func) == 0)) {
4154 ret = 0;
4155 free_ftrace_mod(ftrace_mod);
4156 continue;
4157 }
4158 }
4159 goto out;
4160 }
4161
4162 ret = -EINVAL;
4163 /* We only care about modules that have not been loaded yet */
4164 if (module_exists(module))
4165 goto out;
4166
4167 /* Save this string off, and execute it when the module is loaded */
4168 ret = ftrace_add_mod(tr, func, module, enable);
4169 out:
4170 mutex_unlock(&ftrace_lock);
4171
4172 return ret;
4173}
4174
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04004175static int
4176ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4177 int reset, int enable);
4178
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004179#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04004180static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4181 char *mod, bool enable)
4182{
4183 struct ftrace_mod_load *ftrace_mod, *n;
4184 struct ftrace_hash **orig_hash, *new_hash;
4185 LIST_HEAD(process_mods);
4186 char *func;
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04004187
4188 mutex_lock(&ops->func_hash->regex_lock);
4189
4190 if (enable)
4191 orig_hash = &ops->func_hash->filter_hash;
4192 else
4193 orig_hash = &ops->func_hash->notrace_hash;
4194
4195 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4196 *orig_hash);
4197 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004198 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04004199
4200 mutex_lock(&ftrace_lock);
4201
4202 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4203
4204 if (strcmp(ftrace_mod->module, mod) != 0)
4205 continue;
4206
4207 if (ftrace_mod->func)
4208 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4209 else
4210 func = kstrdup("*", GFP_KERNEL);
4211
4212 if (!func) /* warn? */
4213 continue;
4214
Baokun Li3ecda642021-06-08 11:11:08 +08004215 list_move(&ftrace_mod->list, &process_mods);
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04004216
4217 /* Use the newly allocated func, as it may be "*" */
4218 kfree(ftrace_mod->func);
4219 ftrace_mod->func = func;
4220 }
4221
4222 mutex_unlock(&ftrace_lock);
4223
4224 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4225
4226 func = ftrace_mod->func;
4227
4228 /* Grabs ftrace_lock, which is why we have this extra step */
4229 match_records(new_hash, func, strlen(func), mod);
4230 free_ftrace_mod(ftrace_mod);
4231 }
4232
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004233 if (enable && list_empty(head))
4234 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4235
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04004236 mutex_lock(&ftrace_lock);
4237
Alex Shi045e2692020-11-06 22:54:46 +08004238 ftrace_hash_move_and_update_ops(ops, orig_hash,
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04004239 new_hash, enable);
4240 mutex_unlock(&ftrace_lock);
4241
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004242 out:
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04004243 mutex_unlock(&ops->func_hash->regex_lock);
4244
4245 free_ftrace_hash(new_hash);
4246}
4247
4248static void process_cached_mods(const char *mod_name)
4249{
4250 struct trace_array *tr;
4251 char *mod;
4252
4253 mod = kstrdup(mod_name, GFP_KERNEL);
4254 if (!mod)
4255 return;
4256
4257 mutex_lock(&trace_types_lock);
4258 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4259 if (!list_empty(&tr->mod_trace))
4260 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4261 if (!list_empty(&tr->mod_notrace))
4262 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4263 }
4264 mutex_unlock(&trace_types_lock);
4265
4266 kfree(mod);
4267}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004268#endif
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04004269
Steven Rostedtf6180772009-02-14 00:40:25 -05004270/*
4271 * We register the module command as a template to show others how
4272 * to register the a command as well.
4273 */
4274
4275static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004276ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004277 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004278{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004279 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004280 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004281
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004282 /* match_records() modifies func, and we need the original */
4283 func = kstrdup(func_orig, GFP_KERNEL);
4284 if (!func)
4285 return -ENOMEM;
4286
Steven Rostedtf6180772009-02-14 00:40:25 -05004287 /*
4288 * cmd == 'mod' because we only registered this func
4289 * for the 'mod' ftrace_func_command.
4290 * But if you register one func with multiple commands,
4291 * you can tell which command was used by the cmd
4292 * parameter.
4293 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004294 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004295 kfree(func);
4296
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004297 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004298 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004299 if (ret < 0)
4300 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004301 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004302}
4303
4304static struct ftrace_func_command ftrace_mod_cmd = {
4305 .name = "mod",
4306 .func = ftrace_mod_callback,
4307};
4308
4309static int __init ftrace_mod_cmd_init(void)
4310{
4311 return register_ftrace_command(&ftrace_mod_cmd);
4312}
Steven Rostedt6f415672012-10-05 12:13:07 -04004313core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004314
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004315static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04004316 struct ftrace_ops *op, struct ftrace_regs *fregs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004317{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004318 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004319 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004320
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004321 probe = container_of(op, struct ftrace_func_probe, ops);
4322 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004323
4324 /*
4325 * Disable preemption for these calls to prevent a RCU grace
4326 * period. This syncs the hash iteration and freeing of items
4327 * on the hash. rcu_read_lock is too dangerous here.
4328 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004329 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004330 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004331 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004332}
4333
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004334struct ftrace_func_map {
4335 struct ftrace_func_entry entry;
4336 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004337};
4338
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004339struct ftrace_func_mapper {
4340 struct ftrace_hash hash;
4341};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004342
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004343/**
4344 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4345 *
4346 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4347 */
4348struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004349{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004350 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004351
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004352 /*
4353 * The mapper is simply a ftrace_hash, but since the entries
4354 * in the hash are not ftrace_func_entry type, we define it
4355 * as a separate structure.
4356 */
4357 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4358 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004359}
4360
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004361/**
4362 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4363 * @mapper: The mapper that has the ip maps
4364 * @ip: the instruction pointer to find the data for
4365 *
4366 * Returns the data mapped to @ip if found otherwise NULL. The return
4367 * is actually the address of the mapper data pointer. The address is
4368 * returned for use cases where the data is no bigger than a long, and
4369 * the user can use the data pointer as its data instead of having to
4370 * allocate more memory for the reference.
4371 */
4372void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4373 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004374{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004375 struct ftrace_func_entry *entry;
4376 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004377
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004378 entry = ftrace_lookup_ip(&mapper->hash, ip);
4379 if (!entry)
4380 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004381
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004382 map = (struct ftrace_func_map *)entry;
4383 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004384}
4385
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004386/**
4387 * ftrace_func_mapper_add_ip - Map some data to an ip
4388 * @mapper: The mapper that has the ip maps
4389 * @ip: The instruction pointer address to map @data to
4390 * @data: The data to map to @ip
4391 *
Qiujun Huangfdda88d2020-10-02 22:31:26 +08004392 * Returns 0 on success otherwise an error.
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004393 */
4394int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4395 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004396{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004397 struct ftrace_func_entry *entry;
4398 struct ftrace_func_map *map;
4399
4400 entry = ftrace_lookup_ip(&mapper->hash, ip);
4401 if (entry)
4402 return -EBUSY;
4403
4404 map = kmalloc(sizeof(*map), GFP_KERNEL);
4405 if (!map)
4406 return -ENOMEM;
4407
4408 map->entry.ip = ip;
4409 map->data = data;
4410
4411 __add_hash_entry(&mapper->hash, &map->entry);
4412
4413 return 0;
4414}
4415
4416/**
4417 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4418 * @mapper: The mapper that has the ip maps
4419 * @ip: The instruction pointer address to remove the data from
4420 *
4421 * Returns the data if it is found, otherwise NULL.
4422 * Note, if the data pointer is used as the data itself, (see
4423 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4424 * if the data pointer was set to zero.
4425 */
4426void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4427 unsigned long ip)
4428{
4429 struct ftrace_func_entry *entry;
4430 struct ftrace_func_map *map;
4431 void *data;
4432
4433 entry = ftrace_lookup_ip(&mapper->hash, ip);
4434 if (!entry)
4435 return NULL;
4436
4437 map = (struct ftrace_func_map *)entry;
4438 data = map->data;
4439
4440 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004441 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004442
4443 return data;
4444}
4445
4446/**
4447 * free_ftrace_func_mapper - free a mapping of ips and data
4448 * @mapper: The mapper that has the ip maps
4449 * @free_func: A function to be called on each data item.
4450 *
4451 * This is used to free the function mapper. The @free_func is optional
4452 * and can be used if the data needs to be freed as well.
4453 */
4454void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4455 ftrace_mapper_func free_func)
4456{
4457 struct ftrace_func_entry *entry;
4458 struct ftrace_func_map *map;
4459 struct hlist_head *hhd;
Wei Li04e03d92019-06-06 11:17:54 +08004460 int size, i;
4461
4462 if (!mapper)
4463 return;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004464
4465 if (free_func && mapper->hash.count) {
Wei Li04e03d92019-06-06 11:17:54 +08004466 size = 1 << mapper->hash.size_bits;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004467 for (i = 0; i < size; i++) {
4468 hhd = &mapper->hash.buckets[i];
4469 hlist_for_each_entry(entry, hhd, hlist) {
4470 map = (struct ftrace_func_map *)entry;
4471 free_func(map);
4472 }
4473 }
4474 }
4475 free_ftrace_hash(&mapper->hash);
4476}
4477
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004478static void release_probe(struct ftrace_func_probe *probe)
4479{
4480 struct ftrace_probe_ops *probe_ops;
4481
4482 mutex_lock(&ftrace_lock);
4483
4484 WARN_ON(probe->ref <= 0);
4485
4486 /* Subtract the ref that was used to protect this instance */
4487 probe->ref--;
4488
4489 if (!probe->ref) {
4490 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004491 /*
4492 * Sending zero as ip tells probe_ops to free
4493 * the probe->data itself
4494 */
4495 if (probe_ops->free)
4496 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004497 list_del(&probe->list);
4498 kfree(probe);
4499 }
4500 mutex_unlock(&ftrace_lock);
4501}
4502
4503static void acquire_probe_locked(struct ftrace_func_probe *probe)
4504{
4505 /*
4506 * Add one ref to keep it from being freed when releasing the
4507 * ftrace_lock mutex.
4508 */
4509 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004510}
4511
Steven Rostedt59df055f2009-02-14 15:29:06 -05004512int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004513register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004514 struct ftrace_probe_ops *probe_ops,
4515 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004516{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004517 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004518 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004519 struct ftrace_hash **orig_hash;
4520 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004521 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004522 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004523 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004524 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004525 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004526
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004527 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004528 return -EINVAL;
4529
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004530 /* We do not support '!' for function probes */
4531 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004532 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004533
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004534
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004535 mutex_lock(&ftrace_lock);
4536 /* Check if the probe_ops is already registered */
4537 list_for_each_entry(probe, &tr->func_probes, list) {
4538 if (probe->probe_ops == probe_ops)
4539 break;
4540 }
4541 if (&probe->list == &tr->func_probes) {
4542 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4543 if (!probe) {
4544 mutex_unlock(&ftrace_lock);
4545 return -ENOMEM;
4546 }
4547 probe->probe_ops = probe_ops;
4548 probe->ops.func = function_trace_probe_call;
4549 probe->tr = tr;
4550 ftrace_ops_init(&probe->ops);
4551 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004552 }
4553
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004554 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004555
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004556 mutex_unlock(&ftrace_lock);
4557
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04004558 /*
4559 * Note, there's a small window here that the func_hash->filter_hash
Qiujun Huangfdda88d2020-10-02 22:31:26 +08004560 * may be NULL or empty. Need to be careful when reading the loop.
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04004561 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004562 mutex_lock(&probe->ops.func_hash->regex_lock);
4563
4564 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004565 old_hash = *orig_hash;
4566 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4567
Naveen N. Rao5b0022d2019-07-04 20:04:42 +05304568 if (!hash) {
4569 ret = -ENOMEM;
4570 goto out;
4571 }
4572
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004573 ret = ftrace_match_records(hash, glob, strlen(glob));
4574
4575 /* Nothing found? */
4576 if (!ret)
4577 ret = -EINVAL;
4578
4579 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004580 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004581
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004582 size = 1 << hash->size_bits;
4583 for (i = 0; i < size; i++) {
4584 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4585 if (ftrace_lookup_ip(old_hash, entry->ip))
4586 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004587 /*
4588 * The caller might want to do something special
4589 * for each function we find. We call the callback
4590 * to give the caller an opportunity to do so.
4591 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004592 if (probe_ops->init) {
4593 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004594 entry->ip, data,
4595 &probe->data);
4596 if (ret < 0) {
4597 if (probe_ops->free && count)
4598 probe_ops->free(probe_ops, tr,
4599 0, probe->data);
4600 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004601 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004602 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004603 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004604 count++;
4605 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004606 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004607
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004608 mutex_lock(&ftrace_lock);
4609
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004610 if (!count) {
4611 /* Nothing was added? */
4612 ret = -EINVAL;
4613 goto out_unlock;
4614 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004615
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004616 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4617 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004618 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004619 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004620
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004621 /* One ref for each new function traced */
4622 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004623
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004624 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4625 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004626
Steven Rostedt59df055f2009-02-14 15:29:06 -05004627 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004628 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004629
4630 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004631 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004632 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004633 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004634 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004635
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004636 release_probe(probe);
4637
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004638 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004639
4640 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004641 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004642 goto out_unlock;
4643
4644 /* Failed to do the move, need to call the free functions */
4645 for (i = 0; i < size; i++) {
4646 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4647 if (ftrace_lookup_ip(old_hash, entry->ip))
4648 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004649 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004650 }
4651 }
4652 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004653}
4654
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004655int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004656unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4657 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004658{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004659 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004660 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004661 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004662 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004663 struct ftrace_hash **orig_hash;
4664 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004665 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004666 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004667 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004668 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004669 int count = 0;
4670 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004671 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004672
Naveen N. Raocbab5672017-05-16 23:21:25 +05304673 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004674 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304675 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004676 int not;
4677
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004678 func_g.type = filter_parse_regex(glob, strlen(glob),
4679 &func_g.search, &not);
4680 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004681
Steven Rostedtb6887d72009-02-17 12:32:04 -05004682 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004683 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004684 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004685 }
4686
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004687 mutex_lock(&ftrace_lock);
4688 /* Check if the probe_ops is already registered */
4689 list_for_each_entry(probe, &tr->func_probes, list) {
4690 if (probe->probe_ops == probe_ops)
4691 break;
4692 }
4693 if (&probe->list == &tr->func_probes)
4694 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004695
4696 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004697 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4698 goto err_unlock_ftrace;
4699
4700 acquire_probe_locked(probe);
4701
4702 mutex_unlock(&ftrace_lock);
4703
4704 mutex_lock(&probe->ops.func_hash->regex_lock);
4705
4706 orig_hash = &probe->ops.func_hash->filter_hash;
4707 old_hash = *orig_hash;
4708
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004709 if (ftrace_hash_empty(old_hash))
4710 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004711
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004712 old_hash_ops.filter_hash = old_hash;
4713 /* Probes only have filters */
4714 old_hash_ops.notrace_hash = NULL;
4715
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004716 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004717 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004718 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004719 goto out_unlock;
4720
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004721 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004722
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004723 size = 1 << hash->size_bits;
4724 for (i = 0; i < size; i++) {
4725 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004726
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004727 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004728 kallsyms_lookup(entry->ip, NULL, NULL,
4729 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004730 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004731 continue;
4732 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004733 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004734 remove_hash_entry(hash, entry);
4735 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004736 }
4737 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004738
4739 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004740 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004741 ret = -EINVAL;
4742 goto out_unlock;
4743 }
4744
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004745 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004746
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004747 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004748
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004749 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004750
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004751 if (ftrace_hash_empty(hash))
4752 ftrace_shutdown(&probe->ops, 0);
4753
4754 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004755 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004756
4757 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004758 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004759 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004760 &old_hash_ops);
Paul E. McKenney74401722018-11-06 18:44:52 -08004761 synchronize_rcu();
Steven Rostedt (Red Hat)3296fc4e2014-07-24 15:33:41 -04004762
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004763 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4764 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004765 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004766 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004767 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004768 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004769 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004770
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004771 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004772 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004773 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004774
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004775 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004776
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004777 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004778
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004779 err_unlock_ftrace:
4780 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004781 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004782}
4783
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304784void clear_ftrace_function_probes(struct trace_array *tr)
4785{
4786 struct ftrace_func_probe *probe, *n;
4787
4788 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4789 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4790}
4791
Steven Rostedtf6180772009-02-14 00:40:25 -05004792static LIST_HEAD(ftrace_commands);
4793static DEFINE_MUTEX(ftrace_cmd_mutex);
4794
Tom Zanussi38de93a2013-10-24 08:34:18 -05004795/*
4796 * Currently we only register ftrace commands from __init, so mark this
4797 * __init too.
4798 */
4799__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004800{
4801 struct ftrace_func_command *p;
4802 int ret = 0;
4803
4804 mutex_lock(&ftrace_cmd_mutex);
4805 list_for_each_entry(p, &ftrace_commands, list) {
4806 if (strcmp(cmd->name, p->name) == 0) {
4807 ret = -EBUSY;
4808 goto out_unlock;
4809 }
4810 }
4811 list_add(&cmd->list, &ftrace_commands);
4812 out_unlock:
4813 mutex_unlock(&ftrace_cmd_mutex);
4814
4815 return ret;
4816}
4817
Tom Zanussi38de93a2013-10-24 08:34:18 -05004818/*
4819 * Currently we only unregister ftrace commands from __init, so mark
4820 * this __init too.
4821 */
4822__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004823{
4824 struct ftrace_func_command *p, *n;
4825 int ret = -ENODEV;
4826
4827 mutex_lock(&ftrace_cmd_mutex);
4828 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4829 if (strcmp(cmd->name, p->name) == 0) {
4830 ret = 0;
4831 list_del_init(&p->list);
4832 goto out_unlock;
4833 }
4834 }
4835 out_unlock:
4836 mutex_unlock(&ftrace_cmd_mutex);
4837
4838 return ret;
4839}
4840
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004841static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004842 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004843{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004844 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004845 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004846 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004847 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004848 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004849
4850 func = strsep(&next, ":");
4851
4852 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004853 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004854 if (!ret)
4855 ret = -EINVAL;
4856 if (ret < 0)
4857 return ret;
4858 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004859 }
4860
Steven Rostedtf6180772009-02-14 00:40:25 -05004861 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004862
4863 command = strsep(&next, ":");
4864
Steven Rostedtf6180772009-02-14 00:40:25 -05004865 mutex_lock(&ftrace_cmd_mutex);
4866 list_for_each_entry(p, &ftrace_commands, list) {
4867 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004868 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004869 goto out_unlock;
4870 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004871 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004872 out_unlock:
4873 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004874
Steven Rostedtf6180772009-02-14 00:40:25 -05004875 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004876}
4877
Ingo Molnare309b412008-05-12 21:20:51 +02004878static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004879ftrace_regex_write(struct file *file, const char __user *ubuf,
4880 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004881{
4882 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004883 struct trace_parser *parser;
4884 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004885
Li Zefan4ba79782009-09-22 13:52:20 +08004886 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004887 return 0;
4888
Steven Rostedt5072c592008-05-12 21:20:43 +02004889 if (file->f_mode & FMODE_READ) {
4890 struct seq_file *m = file->private_data;
4891 iter = m->private;
4892 } else
4893 iter = file->private_data;
4894
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004895 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004896 return -ENODEV;
4897
4898 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004899
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004900 parser = &iter->parser;
4901 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004902
Li Zefan4ba79782009-09-22 13:52:20 +08004903 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004904 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004905 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004906 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004907 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004908 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004909 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004910 }
4911
Steven Rostedt5072c592008-05-12 21:20:43 +02004912 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004913 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004914 return ret;
4915}
4916
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004917ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004918ftrace_filter_write(struct file *file, const char __user *ubuf,
4919 size_t cnt, loff_t *ppos)
4920{
4921 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4922}
4923
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004924ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004925ftrace_notrace_write(struct file *file, const char __user *ubuf,
4926 size_t cnt, loff_t *ppos)
4927{
4928 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4929}
4930
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004931static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004932ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4933{
4934 struct ftrace_func_entry *entry;
4935
4936 if (!ftrace_location(ip))
4937 return -EINVAL;
4938
4939 if (remove) {
4940 entry = ftrace_lookup_ip(hash, ip);
4941 if (!entry)
4942 return -ENOENT;
4943 free_hash_entry(hash, entry);
4944 return 0;
4945 }
4946
4947 return add_hash_entry(hash, ip);
4948}
4949
4950static int
4951ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4952 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004953{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004954 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004955 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004956 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004957
Steven Rostedt41c52c02008-05-22 11:46:33 -04004958 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004959 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004960
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004961 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004962
Steven Rostedtf45948e2011-05-02 12:29:25 -04004963 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004964 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004965 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004966 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004967
Wang Nanb972cc52014-07-15 08:40:20 +08004968 if (reset)
4969 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4970 else
4971 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4972
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004973 if (!hash) {
4974 ret = -ENOMEM;
4975 goto out_regex_unlock;
4976 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004977
Jiri Olsaac483c42012-01-02 10:04:14 +01004978 if (buf && !ftrace_match_records(hash, buf, len)) {
4979 ret = -EINVAL;
4980 goto out_regex_unlock;
4981 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004982 if (ip) {
4983 ret = ftrace_match_addr(hash, ip, remove);
4984 if (ret < 0)
4985 goto out_regex_unlock;
4986 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004987
4988 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004989 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004990 mutex_unlock(&ftrace_lock);
4991
Jiri Olsaac483c42012-01-02 10:04:14 +01004992 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004993 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004994
4995 free_ftrace_hash(hash);
4996 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004997}
4998
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004999static int
5000ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
5001 int reset, int enable)
5002{
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05305003 return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005004}
5005
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005006#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005007
5008struct ftrace_direct_func {
5009 struct list_head next;
5010 unsigned long addr;
5011 int count;
5012};
5013
5014static LIST_HEAD(ftrace_direct_funcs);
5015
5016/**
5017 * ftrace_find_direct_func - test an address if it is a registered direct caller
5018 * @addr: The address of a registered direct caller
5019 *
5020 * This searches to see if a ftrace direct caller has been registered
5021 * at a specific address, and if so, it returns a descriptor for it.
5022 *
5023 * This can be used by architecture code to see if an address is
5024 * a direct caller (trampoline) attached to a fentry/mcount location.
5025 * This is useful for the function_graph tracer, as it may need to
5026 * do adjustments if it traced a location that also has a direct
5027 * trampoline attached to it.
5028 */
5029struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
5030{
5031 struct ftrace_direct_func *entry;
5032 bool found = false;
5033
5034 /* May be called by fgraph trampoline (protected by rcu tasks) */
5035 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
5036 if (entry->addr == addr) {
5037 found = true;
5038 break;
5039 }
5040 }
5041 if (found)
5042 return entry;
5043
5044 return NULL;
5045}
5046
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005047static struct ftrace_direct_func *ftrace_alloc_direct_func(unsigned long addr)
5048{
5049 struct ftrace_direct_func *direct;
5050
5051 direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5052 if (!direct)
5053 return NULL;
5054 direct->addr = addr;
5055 direct->count = 0;
5056 list_add_rcu(&direct->next, &ftrace_direct_funcs);
5057 ftrace_direct_func_count++;
5058 return direct;
5059}
5060
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005061/**
5062 * register_ftrace_direct - Call a custom trampoline directly
5063 * @ip: The address of the nop at the beginning of a function
5064 * @addr: The address of the trampoline to call at @ip
5065 *
5066 * This is used to connect a direct call from the nop location (@ip)
5067 * at the start of ftrace traced functions. The location that it calls
5068 * (@addr) must be able to handle a direct call, and save the parameters
5069 * of the function being traced, and restore them (or inject new ones
5070 * if needed), before returning.
5071 *
5072 * Returns:
5073 * 0 on success
5074 * -EBUSY - Another direct function is already attached (there can be only one)
5075 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5076 * -ENOMEM - There was an allocation failure.
5077 */
5078int register_ftrace_direct(unsigned long ip, unsigned long addr)
5079{
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005080 struct ftrace_direct_func *direct;
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005081 struct ftrace_func_entry *entry;
5082 struct ftrace_hash *free_hash = NULL;
5083 struct dyn_ftrace *rec;
5084 int ret = -EBUSY;
5085
5086 mutex_lock(&direct_mutex);
5087
5088 /* See if there's a direct function at @ip already */
Alexei Starovoitovff205762019-12-08 16:01:12 -08005089 if (ftrace_find_rec_direct(ip))
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005090 goto out_unlock;
5091
5092 ret = -ENODEV;
5093 rec = lookup_rec(ip, ip);
5094 if (!rec)
5095 goto out_unlock;
5096
5097 /*
5098 * Check if the rec says it has a direct call but we didn't
5099 * find one earlier?
5100 */
5101 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5102 goto out_unlock;
5103
5104 /* Make sure the ip points to the exact record */
Steven Rostedt (VMware)406acdd2019-11-15 14:19:04 -05005105 if (ip != rec->ip) {
5106 ip = rec->ip;
5107 /* Need to check this ip for a direct. */
Alexei Starovoitovff205762019-12-08 16:01:12 -08005108 if (ftrace_find_rec_direct(ip))
Steven Rostedt (VMware)406acdd2019-11-15 14:19:04 -05005109 goto out_unlock;
5110 }
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005111
5112 ret = -ENOMEM;
5113 if (ftrace_hash_empty(direct_functions) ||
5114 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
5115 struct ftrace_hash *new_hash;
5116 int size = ftrace_hash_empty(direct_functions) ? 0 :
5117 direct_functions->count + 1;
5118
5119 if (size < 32)
5120 size = 32;
5121
5122 new_hash = dup_hash(direct_functions, size);
5123 if (!new_hash)
5124 goto out_unlock;
5125
5126 free_hash = direct_functions;
5127 direct_functions = new_hash;
5128 }
5129
5130 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
5131 if (!entry)
5132 goto out_unlock;
5133
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005134 direct = ftrace_find_direct_func(addr);
5135 if (!direct) {
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005136 direct = ftrace_alloc_direct_func(addr);
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005137 if (!direct) {
5138 kfree(entry);
5139 goto out_unlock;
5140 }
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005141 }
5142
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005143 entry->ip = ip;
5144 entry->direct = addr;
5145 __add_hash_entry(direct_functions, entry);
5146
5147 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5148 if (ret)
5149 remove_hash_entry(direct_functions, entry);
5150
5151 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5152 ret = register_ftrace_function(&direct_ops);
5153 if (ret)
5154 ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5155 }
5156
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005157 if (ret) {
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005158 kfree(entry);
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005159 if (!direct->count) {
5160 list_del_rcu(&direct->next);
5161 synchronize_rcu_tasks();
5162 kfree(direct);
5163 if (free_hash)
5164 free_ftrace_hash(free_hash);
5165 free_hash = NULL;
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05005166 ftrace_direct_func_count--;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005167 }
5168 } else {
Steven Rostedt (VMware)1c7f9b62019-11-15 14:13:20 -05005169 direct->count++;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005170 }
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005171 out_unlock:
5172 mutex_unlock(&direct_mutex);
5173
5174 if (free_hash) {
5175 synchronize_rcu_tasks();
5176 free_ftrace_hash(free_hash);
5177 }
5178
5179 return ret;
5180}
5181EXPORT_SYMBOL_GPL(register_ftrace_direct);
5182
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005183static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5184 struct dyn_ftrace **recp)
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005185{
5186 struct ftrace_func_entry *entry;
5187 struct dyn_ftrace *rec;
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005188
5189 rec = lookup_rec(*ip, *ip);
5190 if (!rec)
5191 return NULL;
5192
5193 entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5194 if (!entry) {
5195 WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5196 return NULL;
5197 }
5198
5199 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5200
5201 /* Passed in ip just needs to be on the call site */
5202 *ip = rec->ip;
5203
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005204 if (recp)
5205 *recp = rec;
5206
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005207 return entry;
5208}
5209
5210int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5211{
5212 struct ftrace_direct_func *direct;
5213 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005214 int ret = -ENODEV;
5215
5216 mutex_lock(&direct_mutex);
5217
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005218 entry = find_direct_entry(&ip, NULL);
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005219 if (!entry)
5220 goto out_unlock;
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005221
5222 if (direct_functions->count == 1)
5223 unregister_ftrace_function(&direct_ops);
5224
5225 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5226
5227 WARN_ON(ret);
5228
5229 remove_hash_entry(direct_functions, entry);
5230
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005231 direct = ftrace_find_direct_func(addr);
5232 if (!WARN_ON(!direct)) {
5233 /* This is the good path (see the ! before WARN) */
5234 direct->count--;
5235 WARN_ON(direct->count < 0);
5236 if (!direct->count) {
5237 list_del_rcu(&direct->next);
5238 synchronize_rcu_tasks();
5239 kfree(direct);
Steven Rostedt (VMware)353da872020-04-22 21:38:45 -04005240 kfree(entry);
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05005241 ftrace_direct_func_count--;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005242 }
5243 }
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005244 out_unlock:
5245 mutex_unlock(&direct_mutex);
5246
5247 return ret;
5248}
5249EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005250
5251static struct ftrace_ops stub_ops = {
5252 .func = ftrace_stub,
5253};
5254
5255/**
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005256 * ftrace_modify_direct_caller - modify ftrace nop directly
5257 * @entry: The ftrace hash entry of the direct helper for @rec
5258 * @rec: The record representing the function site to patch
5259 * @old_addr: The location that the site at @rec->ip currently calls
5260 * @new_addr: The location that the site at @rec->ip should call
5261 *
5262 * An architecture may overwrite this function to optimize the
5263 * changing of the direct callback on an ftrace nop location.
5264 * This is called with the ftrace_lock mutex held, and no other
5265 * ftrace callbacks are on the associated record (@rec). Thus,
5266 * it is safe to modify the ftrace record, where it should be
5267 * currently calling @old_addr directly, to call @new_addr.
5268 *
5269 * Safety checks should be made to make sure that the code at
5270 * @rec->ip is currently calling @old_addr. And this must
5271 * also update entry->direct to @new_addr.
5272 */
5273int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5274 struct dyn_ftrace *rec,
5275 unsigned long old_addr,
5276 unsigned long new_addr)
5277{
5278 unsigned long ip = rec->ip;
5279 int ret;
5280
5281 /*
5282 * The ftrace_lock was used to determine if the record
5283 * had more than one registered user to it. If it did,
5284 * we needed to prevent that from changing to do the quick
5285 * switch. But if it did not (only a direct caller was attached)
5286 * then this function is called. But this function can deal
5287 * with attached callers to the rec that we care about, and
5288 * since this function uses standard ftrace calls that take
5289 * the ftrace_lock mutex, we need to release it.
5290 */
5291 mutex_unlock(&ftrace_lock);
5292
5293 /*
5294 * By setting a stub function at the same address, we force
5295 * the code to call the iterator and the direct_ops helper.
5296 * This means that @ip does not call the direct call, and
5297 * we can simply modify it.
5298 */
5299 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5300 if (ret)
5301 goto out_lock;
5302
5303 ret = register_ftrace_function(&stub_ops);
5304 if (ret) {
5305 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5306 goto out_lock;
5307 }
5308
5309 entry->direct = new_addr;
5310
5311 /*
5312 * By removing the stub, we put back the direct call, calling
5313 * the @new_addr.
5314 */
5315 unregister_ftrace_function(&stub_ops);
5316 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5317
5318 out_lock:
5319 mutex_lock(&ftrace_lock);
5320
5321 return ret;
5322}
5323
5324/**
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005325 * modify_ftrace_direct - Modify an existing direct call to call something else
5326 * @ip: The instruction pointer to modify
5327 * @old_addr: The address that the current @ip calls directly
5328 * @new_addr: The address that the @ip should call
5329 *
5330 * This modifies a ftrace direct caller at an instruction pointer without
5331 * having to disable it first. The direct call will switch over to the
5332 * @new_addr without missing anything.
5333 *
5334 * Returns: zero on success. Non zero on error, which includes:
5335 * -ENODEV : the @ip given has no direct caller attached
5336 * -EINVAL : the @old_addr does not match the current direct caller
5337 */
5338int modify_ftrace_direct(unsigned long ip,
5339 unsigned long old_addr, unsigned long new_addr)
5340{
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005341 struct ftrace_direct_func *direct, *new_direct = NULL;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005342 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005343 struct dyn_ftrace *rec;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005344 int ret = -ENODEV;
5345
5346 mutex_lock(&direct_mutex);
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005347
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005348 mutex_lock(&ftrace_lock);
5349 entry = find_direct_entry(&ip, &rec);
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005350 if (!entry)
5351 goto out_unlock;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005352
5353 ret = -EINVAL;
5354 if (entry->direct != old_addr)
5355 goto out_unlock;
5356
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005357 direct = ftrace_find_direct_func(old_addr);
5358 if (WARN_ON(!direct))
5359 goto out_unlock;
5360 if (direct->count > 1) {
5361 ret = -ENOMEM;
5362 new_direct = ftrace_alloc_direct_func(new_addr);
5363 if (!new_direct)
5364 goto out_unlock;
5365 direct->count--;
5366 new_direct->count++;
5367 } else {
5368 direct->addr = new_addr;
5369 }
5370
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005371 /*
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005372 * If there's no other ftrace callback on the rec->ip location,
5373 * then it can be changed directly by the architecture.
5374 * If there is another caller, then we just need to change the
5375 * direct caller helper to point to @new_addr.
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005376 */
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005377 if (ftrace_rec_count(rec) == 1) {
5378 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5379 } else {
5380 entry->direct = new_addr;
5381 ret = 0;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005382 }
5383
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005384 if (unlikely(ret && new_direct)) {
5385 direct->count++;
5386 list_del_rcu(&new_direct->next);
5387 synchronize_rcu_tasks();
5388 kfree(new_direct);
5389 ftrace_direct_func_count--;
5390 }
5391
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005392 out_unlock:
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005393 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005394 mutex_unlock(&direct_mutex);
5395 return ret;
5396}
5397EXPORT_SYMBOL_GPL(modify_ftrace_direct);
Steven Rostedt (VMware)763e34e72019-11-08 13:07:06 -05005398#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5399
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005400/**
5401 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5402 * @ops - the ops to set the filter with
5403 * @ip - the address to add to or remove from the filter.
5404 * @remove - non zero to remove the ip from the filter
5405 * @reset - non zero to reset all filters before applying this filter.
5406 *
5407 * Filters denote which functions should be enabled when tracing is enabled
Ingo Molnarf2cc0202021-03-23 18:49:35 +01005408 * If @ip is NULL, it fails to update filter.
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005409 */
5410int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5411 int remove, int reset)
5412{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005413 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005414 return ftrace_set_addr(ops, ip, remove, reset, 1);
5415}
5416EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5417
Joel Fernandesd032ae82016-11-15 12:31:20 -08005418/**
5419 * ftrace_ops_set_global_filter - setup ops to use global filters
5420 * @ops - the ops which will use the global filters
5421 *
5422 * ftrace users who need global function trace filtering should call this.
5423 * It can set the global filter only if ops were not initialized before.
5424 */
5425void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5426{
5427 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5428 return;
5429
5430 ftrace_ops_init(ops);
5431 ops->func_hash = &global_ops.local_hash;
5432}
5433EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5434
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005435static int
5436ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5437 int reset, int enable)
5438{
5439 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
5440}
5441
Steven Rostedt77a2b372008-05-12 21:20:45 +02005442/**
5443 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04005444 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02005445 * @buf - the string that holds the function filter text.
5446 * @len - the length of the string.
5447 * @reset - non zero to reset all filters before applying this filter.
5448 *
5449 * Filters denote which functions should be enabled when tracing is enabled.
5450 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5451 */
Jiri Olsaac483c42012-01-02 10:04:14 +01005452int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04005453 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02005454{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005455 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01005456 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04005457}
Steven Rostedt936e0742011-05-05 22:54:01 -04005458EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005459
Steven Rostedt41c52c02008-05-22 11:46:33 -04005460/**
5461 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04005462 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04005463 * @buf - the string that holds the function notrace text.
5464 * @len - the length of the string.
5465 * @reset - non zero to reset all filters before applying this filter.
5466 *
5467 * Notrace Filters denote which functions should not be enabled when tracing
5468 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5469 * for tracing.
5470 */
Jiri Olsaac483c42012-01-02 10:04:14 +01005471int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04005472 int len, int reset)
5473{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005474 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01005475 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04005476}
5477EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5478/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08005479 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04005480 * @buf - the string that holds the function filter text.
5481 * @len - the length of the string.
5482 * @reset - non zero to reset all filters before applying this filter.
5483 *
5484 * Filters denote which functions should be enabled when tracing is enabled.
5485 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5486 */
5487void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5488{
5489 ftrace_set_regex(&global_ops, buf, len, reset, 1);
5490}
5491EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5492
5493/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08005494 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04005495 * @buf - the string that holds the function notrace text.
5496 * @len - the length of the string.
5497 * @reset - non zero to reset all filters before applying this filter.
5498 *
5499 * Notrace Filters denote which functions should not be enabled when tracing
5500 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5501 * for tracing.
5502 */
5503void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04005504{
Steven Rostedtf45948e2011-05-02 12:29:25 -04005505 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02005506}
Steven Rostedt936e0742011-05-05 22:54:01 -04005507EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02005508
Steven Rostedt2af15d62009-05-28 13:37:24 -04005509/*
5510 * command line interface to allow users to set filters on boot up.
5511 */
5512#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
5513static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5514static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5515
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04005516/* Used by function selftest to not test if filter is set */
5517bool ftrace_filter_param __initdata;
5518
Steven Rostedt2af15d62009-05-28 13:37:24 -04005519static int __init set_ftrace_notrace(char *str)
5520{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04005521 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08005522 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005523 return 1;
5524}
5525__setup("ftrace_notrace=", set_ftrace_notrace);
5526
5527static int __init set_ftrace_filter(char *str)
5528{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04005529 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08005530 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005531 return 1;
5532}
5533__setup("ftrace_filter=", set_ftrace_filter);
5534
Stefan Assmann369bc182009-10-12 22:17:21 +02005535#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08005536static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005537static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005538static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05005539
Stefan Assmann369bc182009-10-12 22:17:21 +02005540static int __init set_graph_function(char *str)
5541{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02005542 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02005543 return 1;
5544}
5545__setup("ftrace_graph_filter=", set_graph_function);
5546
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005547static int __init set_graph_notrace_function(char *str)
5548{
5549 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5550 return 1;
5551}
5552__setup("ftrace_graph_notrace=", set_graph_notrace_function);
5553
Todd Brandt65a50c62017-03-02 16:12:15 -08005554static int __init set_graph_max_depth_function(char *str)
5555{
5556 if (!str)
5557 return 0;
5558 fgraph_max_depth = simple_strtoul(str, NULL, 0);
5559 return 1;
5560}
5561__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5562
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005563static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02005564{
5565 int ret;
5566 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005567 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005568
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005569 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05005570 if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005571 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02005572
5573 while (buf) {
5574 func = strsep(&buf, ",");
5575 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005576 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02005577 if (ret)
5578 printk(KERN_DEBUG "ftrace: function %s not "
5579 "traceable\n", func);
5580 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005581
5582 if (enable)
5583 ftrace_graph_hash = hash;
5584 else
5585 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02005586}
5587#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5588
Steven Rostedt2a85a372011-12-19 21:57:44 -05005589void __init
5590ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04005591{
5592 char *func;
5593
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005594 ftrace_ops_init(ops);
5595
Steven Rostedt2af15d62009-05-28 13:37:24 -04005596 while (buf) {
5597 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04005598 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005599 }
5600}
5601
5602static void __init set_ftrace_early_filters(void)
5603{
5604 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005605 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005606 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005607 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005608#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5609 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005610 set_ftrace_early_graph(ftrace_graph_buf, 1);
5611 if (ftrace_graph_notrace_buf[0])
5612 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005613#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04005614}
5615
Steven Rostedtfc13cb02011-12-19 14:41:25 -05005616int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02005617{
5618 struct seq_file *m = (struct seq_file *)file->private_data;
5619 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005620 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005621 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04005622 int filter_hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02005623
Steven Rostedt5072c592008-05-12 21:20:43 +02005624 if (file->f_mode & FMODE_READ) {
5625 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02005626 seq_release(inode, file);
5627 } else
5628 iter = file->private_data;
5629
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005630 parser = &iter->parser;
5631 if (trace_parser_loaded(parser)) {
Steven Rostedt (VMware)8c9af472021-05-05 10:38:24 -04005632 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5633
5634 ftrace_process_regex(iter, parser->buffer,
5635 parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02005636 }
5637
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005638 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005639
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005640 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005641
Steven Rostedt058e2972011-04-29 22:35:33 -04005642 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04005643 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5644
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005645 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005646 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04005647 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005648 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5649 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005650 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005651
Steven Rostedt058e2972011-04-29 22:35:33 -04005652 mutex_lock(&ftrace_lock);
Alex Shi045e2692020-11-06 22:54:46 +08005653 ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04005654 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04005655 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04005656 } else {
5657 /* For read only, the hash is the ops hash */
5658 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04005659 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005660
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005661 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005662 free_ftrace_hash(iter->hash);
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04005663 if (iter->tr)
5664 trace_array_put(iter->tr);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005665 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04005666
Steven Rostedt5072c592008-05-12 21:20:43 +02005667 return 0;
5668}
5669
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005670static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005671 .open = ftrace_avail_open,
5672 .read = seq_read,
5673 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08005674 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02005675};
5676
Steven Rostedt647bcd02011-05-03 14:39:21 -04005677static const struct file_operations ftrace_enabled_fops = {
5678 .open = ftrace_enabled_open,
5679 .read = seq_read,
5680 .llseek = seq_lseek,
5681 .release = seq_release_private,
5682};
5683
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005684static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005685 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005686 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005687 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c879e2013-12-21 17:39:40 -05005688 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005689 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005690};
5691
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005692static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005693 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005694 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005695 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c879e2013-12-21 17:39:40 -05005696 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005697 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005698};
5699
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005700#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5701
5702static DEFINE_MUTEX(graph_lock);
5703
Amol Grover24a97292020-02-01 12:57:04 +05305704struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
Amol Groverfd0e6852020-02-05 11:27:02 +05305705struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005706
5707enum graph_filter_type {
5708 GRAPH_FILTER_NOTRACE = 0,
5709 GRAPH_FILTER_FUNCTION,
5710};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005711
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005712#define FTRACE_GRAPH_EMPTY ((void *)1)
5713
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005714struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005715 struct ftrace_hash *hash;
5716 struct ftrace_func_entry *entry;
5717 int idx; /* for hash table iteration */
5718 enum graph_filter_type type;
5719 struct ftrace_hash *new_hash;
5720 const struct seq_operations *seq_ops;
5721 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005722};
5723
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005724static void *
Li Zefan85951842009-06-24 09:54:00 +08005725__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005726{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005727 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005728 struct ftrace_func_entry *entry = fgd->entry;
5729 struct hlist_head *head;
5730 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005731
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005732 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005733 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005734
5735 if (entry) {
5736 hlist_for_each_entry_continue(entry, hlist) {
5737 fgd->entry = entry;
5738 return entry;
5739 }
5740
5741 idx++;
5742 }
5743
5744 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5745 head = &fgd->hash->buckets[i];
5746 hlist_for_each_entry(entry, head, hlist) {
5747 fgd->entry = entry;
5748 fgd->idx = i;
5749 return entry;
5750 }
5751 }
5752 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005753}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005754
Li Zefan85951842009-06-24 09:54:00 +08005755static void *
5756g_next(struct seq_file *m, void *v, loff_t *pos)
5757{
5758 (*pos)++;
5759 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005760}
5761
5762static void *g_start(struct seq_file *m, loff_t *pos)
5763{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005764 struct ftrace_graph_data *fgd = m->private;
5765
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005766 mutex_lock(&graph_lock);
5767
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005768 if (fgd->type == GRAPH_FILTER_FUNCTION)
5769 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5770 lockdep_is_held(&graph_lock));
5771 else
5772 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5773 lockdep_is_held(&graph_lock));
5774
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005775 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005776 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005777 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005778
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005779 fgd->idx = 0;
5780 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005781 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005782}
5783
5784static void g_stop(struct seq_file *m, void *p)
5785{
5786 mutex_unlock(&graph_lock);
5787}
5788
5789static int g_show(struct seq_file *m, void *v)
5790{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005791 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005792
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005793 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005794 return 0;
5795
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005796 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005797 struct ftrace_graph_data *fgd = m->private;
5798
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005799 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005800 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005801 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005802 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005803 return 0;
5804 }
5805
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005806 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005807
5808 return 0;
5809}
5810
James Morris88e9d342009-09-22 16:43:43 -07005811static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005812 .start = g_start,
5813 .next = g_next,
5814 .stop = g_stop,
5815 .show = g_show,
5816};
5817
5818static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005819__ftrace_graph_open(struct inode *inode, struct file *file,
5820 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005821{
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04005822 int ret;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005823 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005824
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04005825 ret = security_locked_down(LOCKDOWN_TRACEFS);
5826 if (ret)
5827 return ret;
5828
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005829 if (file->f_mode & FMODE_WRITE) {
5830 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5831
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005832 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5833 return -ENOMEM;
5834
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005835 if (file->f_flags & O_TRUNC)
5836 new_hash = alloc_ftrace_hash(size_bits);
5837 else
5838 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5839 fgd->hash);
5840 if (!new_hash) {
5841 ret = -ENOMEM;
5842 goto out;
5843 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005844 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005845
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005846 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005847 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005848 if (!ret) {
5849 struct seq_file *m = file->private_data;
5850 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005851 } else {
5852 /* Failed */
5853 free_ftrace_hash(new_hash);
5854 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005855 }
5856 } else
5857 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005858
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005859out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005860 if (ret < 0 && file->f_mode & FMODE_WRITE)
5861 trace_parser_put(&fgd->parser);
5862
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005863 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005864
5865 /*
5866 * All uses of fgd->hash must be taken with the graph_lock
5867 * held. The graph_lock is going to be released, so force
5868 * fgd->hash to be reinitialized when it is taken again.
5869 */
5870 fgd->hash = NULL;
5871
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005872 return ret;
5873}
5874
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005875static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005876ftrace_graph_open(struct inode *inode, struct file *file)
5877{
5878 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005879 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005880
5881 if (unlikely(ftrace_disabled))
5882 return -ENODEV;
5883
5884 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5885 if (fgd == NULL)
5886 return -ENOMEM;
5887
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005888 mutex_lock(&graph_lock);
5889
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005890 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5891 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005892 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005893 fgd->seq_ops = &ftrace_graph_seq_ops;
5894
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005895 ret = __ftrace_graph_open(inode, file, fgd);
5896 if (ret < 0)
5897 kfree(fgd);
5898
5899 mutex_unlock(&graph_lock);
5900 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005901}
5902
5903static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005904ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5905{
5906 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005907 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005908
5909 if (unlikely(ftrace_disabled))
5910 return -ENODEV;
5911
5912 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5913 if (fgd == NULL)
5914 return -ENOMEM;
5915
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005916 mutex_lock(&graph_lock);
5917
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005918 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5919 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005920 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005921 fgd->seq_ops = &ftrace_graph_seq_ops;
5922
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005923 ret = __ftrace_graph_open(inode, file, fgd);
5924 if (ret < 0)
5925 kfree(fgd);
5926
5927 mutex_unlock(&graph_lock);
5928 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005929}
5930
5931static int
Li Zefan87827112009-07-23 11:29:11 +08005932ftrace_graph_release(struct inode *inode, struct file *file)
5933{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005934 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005935 struct ftrace_hash *old_hash, *new_hash;
5936 struct trace_parser *parser;
5937 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005938
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005939 if (file->f_mode & FMODE_READ) {
5940 struct seq_file *m = file->private_data;
5941
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005942 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005943 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005944 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005945 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005946 }
5947
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005948
5949 if (file->f_mode & FMODE_WRITE) {
5950
5951 parser = &fgd->parser;
5952
5953 if (trace_parser_loaded((parser))) {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005954 ret = ftrace_graph_set_hash(fgd->new_hash,
5955 parser->buffer);
5956 }
5957
5958 trace_parser_put(parser);
5959
5960 new_hash = __ftrace_hash_move(fgd->new_hash);
5961 if (!new_hash) {
5962 ret = -ENOMEM;
5963 goto out;
5964 }
5965
5966 mutex_lock(&graph_lock);
5967
5968 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5969 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5970 lockdep_is_held(&graph_lock));
5971 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5972 } else {
5973 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5974 lockdep_is_held(&graph_lock));
5975 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5976 }
5977
5978 mutex_unlock(&graph_lock);
5979
Steven Rostedt (VMware)54a16ff2020-02-05 09:20:32 -05005980 /*
5981 * We need to do a hard force of sched synchronization.
5982 * This is because we use preempt_disable() to do RCU, but
5983 * the function tracers can be called where RCU is not watching
5984 * (like before user_exit()). We can not rely on the RCU
5985 * infrastructure to do the synchronization, thus we must do it
5986 * ourselves.
5987 */
Nicolas Saenz Julienne68e834982021-07-21 13:47:26 +02005988 if (old_hash != EMPTY_HASH)
5989 synchronize_rcu_tasks_rude();
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005990
5991 free_ftrace_hash(old_hash);
5992 }
5993
5994 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005995 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005996 kfree(fgd);
5997
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005998 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005999}
6000
6001static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006002ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006003{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03006004 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006005 struct dyn_ftrace *rec;
6006 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006007 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006008 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03006009 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006010
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01006011 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03006012 func_g.type = filter_parse_regex(buffer, strlen(buffer),
6013 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01006014
Dmitry Safonov3ba00922015-09-29 19:46:14 +03006015 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01006016
Steven Rostedt52baf112009-02-14 01:15:39 -05006017 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04006018
6019 if (unlikely(ftrace_disabled)) {
6020 mutex_unlock(&ftrace_lock);
6021 return -ENODEV;
6022 }
6023
Steven Rostedt265c8312009-02-13 12:43:56 -05006024 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006025
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05006026 if (rec->flags & FTRACE_FL_DISABLED)
6027 continue;
6028
Dmitry Safonov0b507e12015-09-29 19:46:15 +03006029 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006030 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006031
6032 if (!not) {
6033 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006034
6035 if (entry)
6036 continue;
6037 if (add_hash_entry(hash, rec->ip) < 0)
6038 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006039 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006040 if (entry) {
6041 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006042 fail = 0;
6043 }
6044 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006045 }
Steven Rostedt265c8312009-02-13 12:43:56 -05006046 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006047out:
Steven Rostedt52baf112009-02-14 01:15:39 -05006048 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006049
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006050 if (fail)
6051 return -EINVAL;
6052
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006053 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006054}
6055
6056static ssize_t
6057ftrace_graph_write(struct file *file, const char __user *ubuf,
6058 size_t cnt, loff_t *ppos)
6059{
Namhyung Kim6a101082013-10-14 17:24:25 +09006060 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006061 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006062 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006063
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006064 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006065 return 0;
6066
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05006067 /* Read mode uses seq functions */
6068 if (file->f_mode & FMODE_READ) {
6069 struct seq_file *m = file->private_data;
6070 fgd = m->private;
6071 }
6072
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006073 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02006074
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006075 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02006076
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006077 if (read >= 0 && trace_parser_loaded(parser) &&
6078 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09006079
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006080 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006081 parser->buffer);
6082 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006083 }
6084
Namhyung Kim6a101082013-10-14 17:24:25 +09006085 if (!ret)
6086 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08006087
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006088 return ret;
6089}
6090
6091static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08006092 .open = ftrace_graph_open,
6093 .read = seq_read,
6094 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c879e2013-12-21 17:39:40 -05006095 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08006096 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006097};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006098
6099static const struct file_operations ftrace_graph_notrace_fops = {
6100 .open = ftrace_graph_notrace_open,
6101 .read = seq_read,
6102 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c879e2013-12-21 17:39:40 -05006103 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006104 .release = ftrace_graph_release,
6105};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006106#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6107
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006108void ftrace_create_filter_files(struct ftrace_ops *ops,
6109 struct dentry *parent)
6110{
6111
6112 trace_create_file("set_ftrace_filter", 0644, parent,
6113 ops, &ftrace_filter_fops);
6114
6115 trace_create_file("set_ftrace_notrace", 0644, parent,
6116 ops, &ftrace_notrace_fops);
6117}
6118
6119/*
6120 * The name "destroy_filter_files" is really a misnomer. Although
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05306121 * in the future, it may actually delete the files, but this is
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006122 * really intended to make sure the ops passed in are disabled
6123 * and that when this function returns, the caller is free to
6124 * free the ops.
6125 *
6126 * The "destroy" name is only to match the "create" name that this
6127 * should be paired with.
6128 */
6129void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6130{
6131 mutex_lock(&ftrace_lock);
6132 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6133 ftrace_shutdown(ops, 0);
6134 ops->flags |= FTRACE_OPS_FL_DELETED;
Steven Rostedt (VMware)2840f842018-12-10 23:58:01 -05006135 ftrace_free_filter(ops);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006136 mutex_unlock(&ftrace_lock);
6137}
6138
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006139static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02006140{
Steven Rostedt5072c592008-05-12 21:20:43 +02006141
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006142 trace_create_file("available_filter_functions", 0444,
6143 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02006144
Steven Rostedt647bcd02011-05-03 14:39:21 -04006145 trace_create_file("enabled_functions", 0444,
6146 d_tracer, NULL, &ftrace_enabled_fops);
6147
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006148 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04006149
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006150#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Chen LinX1ce05002014-09-03 14:31:09 +08006151 trace_create_file("set_graph_function", 0644, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006152 NULL,
6153 &ftrace_graph_fops);
Chen LinX1ce05002014-09-03 14:31:09 +08006154 trace_create_file("set_graph_notrace", 0644, d_tracer,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006155 NULL,
6156 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006157#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6158
Steven Rostedt5072c592008-05-12 21:20:43 +02006159 return 0;
6160}
6161
Steven Rostedt9fd49322012-04-24 22:32:06 -04006162static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05006163{
Steven Rostedt9fd49322012-04-24 22:32:06 -04006164 const unsigned long *ipa = a;
6165 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05006166
Steven Rostedt9fd49322012-04-24 22:32:06 -04006167 if (*ipa > *ipb)
6168 return 1;
6169 if (*ipa < *ipb)
6170 return -1;
6171 return 0;
6172}
6173
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006174static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08006175 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006176 unsigned long *end)
6177{
Steven Rostedt706c81f2012-04-24 23:45:26 -04006178 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05006179 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04006180 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05006181 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006182 unsigned long *p;
6183 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04006184 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05006185 int ret = -ENOMEM;
6186
6187 count = end - start;
6188
6189 if (!count)
6190 return 0;
6191
Steven Rostedt9fd49322012-04-24 22:32:06 -04006192 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02006193 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04006194
Steven Rostedt706c81f2012-04-24 23:45:26 -04006195 start_pg = ftrace_allocate_pages(count);
6196 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05006197 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006198
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006199 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05006200
Steven Rostedt320823092011-12-16 14:42:37 -05006201 /*
6202 * Core and each module needs their own pages, as
6203 * modules will free them when they are removed.
6204 * Force a new page to be allocated for modules.
6205 */
Steven Rostedta7900872011-12-16 16:23:44 -05006206 if (!mod) {
6207 WARN_ON(ftrace_pages || ftrace_pages_start);
6208 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04006209 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05006210 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05006211 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05006212 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05006213
Steven Rostedta7900872011-12-16 16:23:44 -05006214 if (WARN_ON(ftrace_pages->next)) {
6215 /* Hmm, we have free pages? */
6216 while (ftrace_pages->next)
6217 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05006218 }
Steven Rostedta7900872011-12-16 16:23:44 -05006219
Steven Rostedt706c81f2012-04-24 23:45:26 -04006220 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05006221 }
6222
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006223 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04006224 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006225 while (p < end) {
Linus Torvaldsdb425232021-04-01 16:14:17 -04006226 unsigned long end_offset;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006227 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08006228 /*
6229 * Some architecture linkers will pad between
6230 * the different mcount_loc sections of different
6231 * object files to satisfy alignments.
6232 * Skip any NULL pointers.
6233 */
6234 if (!addr)
6235 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04006236
Linus Torvaldsdb425232021-04-01 16:14:17 -04006237 end_offset = (pg->index+1) * sizeof(pg->records[0]);
6238 if (end_offset > PAGE_SIZE << pg->order) {
Steven Rostedt706c81f2012-04-24 23:45:26 -04006239 /* We should have allocated enough */
6240 if (WARN_ON(!pg->next))
6241 break;
6242 pg = pg->next;
6243 }
6244
6245 rec = &pg->records[pg->index++];
6246 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006247 }
6248
Steven Rostedt706c81f2012-04-24 23:45:26 -04006249 /* We should have used all pages */
6250 WARN_ON(pg->next);
6251
6252 /* Assign the last page to ftrace_pages */
6253 ftrace_pages = pg;
6254
Steven Rostedta4f18ed2011-06-07 09:26:46 -04006255 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04006256 * We only need to disable interrupts on start up
6257 * because we are modifying code that an interrupt
6258 * may execute, and the modification is not atomic.
6259 * But for modules, nothing runs the code we modify
6260 * until we are finished with it, and there's no
6261 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04006262 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04006263 if (!mod)
6264 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006265 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04006266 if (!mod)
6267 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05006268 ret = 0;
6269 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006270 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006271
Steven Rostedta7900872011-12-16 16:23:44 -05006272 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006273}
6274
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006275struct ftrace_mod_func {
6276 struct list_head list;
6277 char *name;
6278 unsigned long ip;
6279 unsigned int size;
6280};
6281
6282struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006283 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006284 struct list_head list;
6285 struct module *mod;
6286 unsigned long start_addr;
6287 unsigned long end_addr;
6288 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006289 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006290};
6291
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006292static int ftrace_get_trampoline_kallsym(unsigned int symnum,
6293 unsigned long *value, char *type,
6294 char *name, char *module_name,
6295 int *exported)
6296{
6297 struct ftrace_ops *op;
6298
6299 list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
6300 if (!op->trampoline || symnum--)
6301 continue;
6302 *value = op->trampoline;
6303 *type = 't';
6304 strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
6305 strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
6306 *exported = 0;
6307 return 0;
6308 }
6309
6310 return -ERANGE;
6311}
6312
Steven Rostedt93eb6772009-04-15 13:24:06 -04006313#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05006314
6315#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6316
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006317static LIST_HEAD(ftrace_mod_maps);
6318
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006319static int referenced_filters(struct dyn_ftrace *rec)
6320{
6321 struct ftrace_ops *ops;
6322 int cnt = 0;
6323
6324 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
Chengming Zhou8a224ff2020-07-29 02:05:53 +08006325 if (ops_references_rec(ops, rec)) {
Chengming Zhouc5f51572020-07-29 02:05:54 +08006326 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
6327 continue;
6328 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
6329 continue;
Chengming Zhou8a224ff2020-07-29 02:05:53 +08006330 cnt++;
6331 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
6332 rec->flags |= FTRACE_FL_REGS;
Chengming Zhouc5f51572020-07-29 02:05:54 +08006333 if (cnt == 1 && ops->trampoline)
6334 rec->flags |= FTRACE_FL_TRAMP;
6335 else
6336 rec->flags &= ~FTRACE_FL_TRAMP;
Chengming Zhou8a224ff2020-07-29 02:05:53 +08006337 }
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006338 }
6339
6340 return cnt;
6341}
6342
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006343static void
6344clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6345{
6346 struct ftrace_func_entry *entry;
6347 struct dyn_ftrace *rec;
6348 int i;
6349
6350 if (ftrace_hash_empty(hash))
6351 return;
6352
6353 for (i = 0; i < pg->index; i++) {
6354 rec = &pg->records[i];
6355 entry = __ftrace_lookup_ip(hash, rec->ip);
6356 /*
6357 * Do not allow this rec to match again.
6358 * Yeah, it may waste some memory, but will be removed
6359 * if/when the hash is modified again.
6360 */
6361 if (entry)
6362 entry->ip = 0;
6363 }
6364}
6365
Ingo Molnarf2cc0202021-03-23 18:49:35 +01006366/* Clear any records from hashes */
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006367static void clear_mod_from_hashes(struct ftrace_page *pg)
6368{
6369 struct trace_array *tr;
6370
6371 mutex_lock(&trace_types_lock);
6372 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6373 if (!tr->ops || !tr->ops->func_hash)
6374 continue;
6375 mutex_lock(&tr->ops->func_hash->regex_lock);
6376 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6377 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6378 mutex_unlock(&tr->ops->func_hash->regex_lock);
6379 }
6380 mutex_unlock(&trace_types_lock);
6381}
6382
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006383static void ftrace_free_mod_map(struct rcu_head *rcu)
6384{
6385 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6386 struct ftrace_mod_func *mod_func;
6387 struct ftrace_mod_func *n;
6388
6389 /* All the contents of mod_map are now not visible to readers */
6390 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6391 kfree(mod_func->name);
6392 list_del(&mod_func->list);
6393 kfree(mod_func);
6394 }
6395
6396 kfree(mod_map);
6397}
6398
jolsa@redhat.come7247a12009-10-07 19:00:35 +02006399void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04006400{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006401 struct ftrace_mod_map *mod_map;
6402 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04006403 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05006404 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006405 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04006406 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04006407
Steven Rostedt93eb6772009-04-15 13:24:06 -04006408 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04006409
6410 if (ftrace_disabled)
6411 goto out_unlock;
6412
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006413 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6414 if (mod_map->mod == mod) {
6415 list_del_rcu(&mod_map->list);
Paul E. McKenney74401722018-11-06 18:44:52 -08006416 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006417 break;
6418 }
6419 }
6420
Steven Rostedt320823092011-12-16 14:42:37 -05006421 /*
6422 * Each module has its own ftrace_pages, remove
6423 * them from the list.
6424 */
6425 last_pg = &ftrace_pages_start;
6426 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6427 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05006428 if (within_module_core(rec->ip, mod) ||
6429 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04006430 /*
Steven Rostedt320823092011-12-16 14:42:37 -05006431 * As core pages are first, the first
6432 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04006433 */
Steven Rostedt320823092011-12-16 14:42:37 -05006434 if (WARN_ON(pg == ftrace_pages_start))
6435 goto out_unlock;
6436
6437 /* Check if we are deleting the last page */
6438 if (pg == ftrace_pages)
6439 ftrace_pages = next_to_ftrace_page(last_pg);
6440
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04006441 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05006442 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006443
6444 pg->next = tmp_page;
6445 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05006446 } else
6447 last_pg = &pg->next;
6448 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04006449 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04006450 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006451
6452 for (pg = tmp_page; pg; pg = tmp_page) {
6453
6454 /* Needs to be called outside of ftrace_lock */
6455 clear_mod_from_hashes(pg);
6456
Linus Torvaldsdb425232021-04-01 16:14:17 -04006457 if (pg->records) {
6458 free_pages((unsigned long)pg->records, pg->order);
6459 ftrace_number_of_pages -= 1 << pg->order;
6460 }
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006461 tmp_page = pg->next;
6462 kfree(pg);
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04006463 ftrace_number_of_groups--;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006464 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04006465}
6466
Jessica Yu7dcd1822016-02-16 17:32:33 -05006467void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006468{
6469 struct dyn_ftrace *rec;
6470 struct ftrace_page *pg;
6471
6472 mutex_lock(&ftrace_lock);
6473
6474 if (ftrace_disabled)
6475 goto out_unlock;
6476
6477 /*
6478 * If the tracing is enabled, go ahead and enable the record.
6479 *
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05306480 * The reason not to enable the record immediately is the
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006481 * inherent check of ftrace_make_nop/ftrace_make_call for
6482 * correct previous instructions. Making first the NOP
6483 * conversion puts the module to the correct state, thus
6484 * passing the ftrace_make_call check.
6485 *
6486 * We also delay this to after the module code already set the
6487 * text to read-only, as we now need to set it back to read-write
6488 * so that we can modify the text.
6489 */
6490 if (ftrace_start_up)
6491 ftrace_arch_code_modify_prepare();
6492
6493 do_for_each_ftrace_rec(pg, rec) {
6494 int cnt;
6495 /*
6496 * do_for_each_ftrace_rec() is a double loop.
6497 * module text shares the pg. If a record is
6498 * not part of this module, then skip this pg,
6499 * which the "break" will do.
6500 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05006501 if (!within_module_core(rec->ip, mod) &&
6502 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006503 break;
6504
6505 cnt = 0;
6506
6507 /*
6508 * When adding a module, we need to check if tracers are
6509 * currently enabled and if they are, and can trace this record,
6510 * we need to enable the module functions as well as update the
6511 * reference counts for those function records.
6512 */
6513 if (ftrace_start_up)
6514 cnt += referenced_filters(rec);
6515
Chengming Zhou8a224ff2020-07-29 02:05:53 +08006516 rec->flags &= ~FTRACE_FL_DISABLED;
6517 rec->flags += cnt;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006518
6519 if (ftrace_start_up && cnt) {
6520 int failed = __ftrace_replace_code(rec, 1);
6521 if (failed) {
6522 ftrace_bug(failed, rec);
6523 goto out_loop;
6524 }
6525 }
6526
6527 } while_for_each_ftrace_rec();
6528
6529 out_loop:
6530 if (ftrace_start_up)
6531 ftrace_arch_code_modify_post_process();
6532
6533 out_unlock:
6534 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8df2017-06-26 10:57:21 -04006535
6536 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006537}
6538
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04006539void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04006540{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05006541 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01006542 return;
6543
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05006544 ftrace_process_locs(mod, mod->ftrace_callsites,
6545 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05006546}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006547
6548static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6549 struct dyn_ftrace *rec)
6550{
6551 struct ftrace_mod_func *mod_func;
6552 unsigned long symsize;
6553 unsigned long offset;
6554 char str[KSYM_SYMBOL_LEN];
6555 char *modname;
6556 const char *ret;
6557
6558 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
6559 if (!ret)
6560 return;
6561
6562 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
6563 if (!mod_func)
6564 return;
6565
6566 mod_func->name = kstrdup(str, GFP_KERNEL);
6567 if (!mod_func->name) {
6568 kfree(mod_func);
6569 return;
6570 }
6571
6572 mod_func->ip = rec->ip - offset;
6573 mod_func->size = symsize;
6574
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006575 mod_map->num_funcs++;
6576
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006577 list_add_rcu(&mod_func->list, &mod_map->funcs);
6578}
6579
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006580static struct ftrace_mod_map *
6581allocate_ftrace_mod_map(struct module *mod,
6582 unsigned long start, unsigned long end)
6583{
6584 struct ftrace_mod_map *mod_map;
6585
6586 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
6587 if (!mod_map)
6588 return NULL;
6589
6590 mod_map->mod = mod;
6591 mod_map->start_addr = start;
6592 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006593 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006594
6595 INIT_LIST_HEAD_RCU(&mod_map->funcs);
6596
6597 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
6598
6599 return mod_map;
6600}
6601
6602static const char *
6603ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
6604 unsigned long addr, unsigned long *size,
6605 unsigned long *off, char *sym)
6606{
6607 struct ftrace_mod_func *found_func = NULL;
6608 struct ftrace_mod_func *mod_func;
6609
6610 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6611 if (addr >= mod_func->ip &&
6612 addr < mod_func->ip + mod_func->size) {
6613 found_func = mod_func;
6614 break;
6615 }
6616 }
6617
6618 if (found_func) {
6619 if (size)
6620 *size = found_func->size;
6621 if (off)
6622 *off = addr - found_func->ip;
6623 if (sym)
6624 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6625
6626 return found_func->name;
6627 }
6628
6629 return NULL;
6630}
6631
6632const char *
6633ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6634 unsigned long *off, char **modname, char *sym)
6635{
6636 struct ftrace_mod_map *mod_map;
6637 const char *ret = NULL;
6638
Paul E. McKenney74401722018-11-06 18:44:52 -08006639 /* mod_map is freed via call_rcu() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006640 preempt_disable();
6641 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6642 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6643 if (ret) {
6644 if (modname)
6645 *modname = mod_map->mod->name;
6646 break;
6647 }
6648 }
6649 preempt_enable();
6650
6651 return ret;
6652}
6653
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006654int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6655 char *type, char *name,
6656 char *module_name, int *exported)
6657{
6658 struct ftrace_mod_map *mod_map;
6659 struct ftrace_mod_func *mod_func;
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006660 int ret;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006661
6662 preempt_disable();
6663 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6664
6665 if (symnum >= mod_map->num_funcs) {
6666 symnum -= mod_map->num_funcs;
6667 continue;
6668 }
6669
6670 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6671 if (symnum > 1) {
6672 symnum--;
6673 continue;
6674 }
6675
6676 *value = mod_func->ip;
6677 *type = 'T';
6678 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6679 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6680 *exported = 1;
6681 preempt_enable();
6682 return 0;
6683 }
6684 WARN_ON(1);
6685 break;
6686 }
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006687 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6688 module_name, exported);
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006689 preempt_enable();
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006690 return ret;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006691}
6692
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006693#else
6694static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6695 struct dyn_ftrace *rec) { }
6696static inline struct ftrace_mod_map *
6697allocate_ftrace_mod_map(struct module *mod,
6698 unsigned long start, unsigned long end)
6699{
6700 return NULL;
6701}
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006702int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6703 char *type, char *name, char *module_name,
6704 int *exported)
6705{
6706 int ret;
6707
6708 preempt_disable();
6709 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6710 module_name, exported);
6711 preempt_enable();
6712 return ret;
6713}
Steven Rostedt93eb6772009-04-15 13:24:06 -04006714#endif /* CONFIG_MODULES */
6715
Joel Fernandes8715b102017-10-09 12:29:31 -07006716struct ftrace_init_func {
6717 struct list_head list;
6718 unsigned long ip;
6719};
6720
6721/* Clear any init ips from hashes */
6722static void
6723clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006724{
Joel Fernandes8715b102017-10-09 12:29:31 -07006725 struct ftrace_func_entry *entry;
6726
Changbin Du08468752019-09-10 22:33:36 +08006727 entry = ftrace_lookup_ip(hash, func->ip);
Joel Fernandes8715b102017-10-09 12:29:31 -07006728 /*
6729 * Do not allow this rec to match again.
6730 * Yeah, it may waste some memory, but will be removed
6731 * if/when the hash is modified again.
6732 */
6733 if (entry)
6734 entry->ip = 0;
6735}
6736
6737static void
6738clear_func_from_hashes(struct ftrace_init_func *func)
6739{
6740 struct trace_array *tr;
6741
6742 mutex_lock(&trace_types_lock);
6743 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6744 if (!tr->ops || !tr->ops->func_hash)
6745 continue;
6746 mutex_lock(&tr->ops->func_hash->regex_lock);
6747 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6748 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6749 mutex_unlock(&tr->ops->func_hash->regex_lock);
6750 }
6751 mutex_unlock(&trace_types_lock);
6752}
6753
6754static void add_to_clear_hash_list(struct list_head *clear_list,
6755 struct dyn_ftrace *rec)
6756{
6757 struct ftrace_init_func *func;
6758
6759 func = kmalloc(sizeof(*func), GFP_KERNEL);
6760 if (!func) {
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05006761 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
Joel Fernandes8715b102017-10-09 12:29:31 -07006762 return;
6763 }
6764
6765 func->ip = rec->ip;
6766 list_add(&func->list, clear_list);
6767}
6768
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006769void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006770{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006771 unsigned long start = (unsigned long)(start_ptr);
6772 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006773 struct ftrace_page **last_pg = &ftrace_pages_start;
6774 struct ftrace_page *pg;
6775 struct dyn_ftrace *rec;
6776 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006777 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07006778 struct ftrace_init_func *func, *func_next;
6779 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006780
Joel Fernandes8715b102017-10-09 12:29:31 -07006781 INIT_LIST_HEAD(&clear_hash);
6782
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006783 key.ip = start;
6784 key.flags = end; /* overload flags, as it is unsigned long */
6785
6786 mutex_lock(&ftrace_lock);
6787
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006788 /*
6789 * If we are freeing module init memory, then check if
6790 * any tracer is active. If so, we need to save a mapping of
6791 * the module functions being freed with the address.
6792 */
6793 if (mod && ftrace_ops_list != &ftrace_list_end)
6794 mod_map = allocate_ftrace_mod_map(mod, start, end);
6795
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006796 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6797 if (end < pg->records[0].ip ||
6798 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6799 continue;
6800 again:
6801 rec = bsearch(&key, pg->records, pg->index,
6802 sizeof(struct dyn_ftrace),
6803 ftrace_cmp_recs);
6804 if (!rec)
6805 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006806
Joel Fernandes8715b102017-10-09 12:29:31 -07006807 /* rec will be cleared from hashes after ftrace_lock unlock */
6808 add_to_clear_hash_list(&clear_hash, rec);
6809
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006810 if (mod_map)
6811 save_ftrace_mod_rec(mod_map, rec);
6812
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006813 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04006814 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006815 if (!pg->index) {
6816 *last_pg = pg->next;
Linus Torvaldsdb425232021-04-01 16:14:17 -04006817 if (pg->records) {
6818 free_pages((unsigned long)pg->records, pg->order);
6819 ftrace_number_of_pages -= 1 << pg->order;
6820 }
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04006821 ftrace_number_of_groups--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006822 kfree(pg);
6823 pg = container_of(last_pg, struct ftrace_page, next);
6824 if (!(*last_pg))
6825 ftrace_pages = pg;
6826 continue;
6827 }
6828 memmove(rec, rec + 1,
6829 (pg->index - (rec - pg->records)) * sizeof(*rec));
6830 /* More than one function may be in this block */
6831 goto again;
6832 }
6833 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07006834
6835 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6836 clear_func_from_hashes(func);
6837 kfree(func);
6838 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006839}
6840
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006841void __init ftrace_free_init_mem(void)
6842{
6843 void *start = (void *)(&__init_begin);
6844 void *end = (void *)(&__init_end);
6845
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006846 ftrace_free_mem(NULL, start, end);
Steven Rostedt93eb6772009-04-15 13:24:06 -04006847}
6848
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006849void __init ftrace_init(void)
6850{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006851 extern unsigned long __start_mcount_loc[];
6852 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006853 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006854 int ret;
6855
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006856 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006857 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006858 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01006859 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006860 goto failed;
6861
6862 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006863 if (!count) {
6864 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006865 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006866 }
6867
6868 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6869 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006870
6871 last_ftrace_enabled = ftrace_enabled = 1;
6872
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006873 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08006874 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006875 __stop_mcount_loc);
6876
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04006877 pr_info("ftrace: allocated %ld pages with %ld groups\n",
6878 ftrace_number_of_pages, ftrace_number_of_groups);
6879
Steven Rostedt2af15d62009-05-28 13:37:24 -04006880 set_ftrace_early_filters();
6881
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006882 return;
6883 failed:
6884 ftrace_disabled = 1;
6885}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006886
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006887/* Do nothing if arch does not support this */
6888void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6889{
6890}
6891
6892static void ftrace_update_trampoline(struct ftrace_ops *ops)
6893{
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006894 unsigned long trampoline = ops->trampoline;
6895
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006896 arch_ftrace_update_trampoline(ops);
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006897 if (ops->trampoline && ops->trampoline != trampoline &&
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03006898 (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
6899 /* Add to kallsyms before the perf events */
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006900 ftrace_add_trampoline_to_kallsyms(ops);
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03006901 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
6902 ops->trampoline, ops->trampoline_size, false,
6903 FTRACE_TRAMPOLINE_SYM);
Adrian Hunter548e1f62020-05-12 15:19:15 +03006904 /*
6905 * Record the perf text poke event after the ksymbol register
6906 * event.
6907 */
6908 perf_event_text_poke((void *)ops->trampoline, NULL, 0,
6909 (void *)ops->trampoline,
6910 ops->trampoline_size);
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03006911 }
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006912}
6913
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006914void ftrace_init_trace_array(struct trace_array *tr)
6915{
6916 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04006917 INIT_LIST_HEAD(&tr->mod_trace);
6918 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006919}
Steven Rostedt3d083392008-05-12 21:20:42 +02006920#else
Frederic Weisbecker0b6e4d562008-10-28 20:17:38 +01006921
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05006922struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04006923 .func = ftrace_stub,
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05006924 .flags = FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006925 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04006926};
6927
Frederic Weisbecker0b6e4d562008-10-28 20:17:38 +01006928static int __init ftrace_nodyn_init(void)
6929{
6930 ftrace_enabled = 1;
6931 return 0;
6932}
Steven Rostedt6f415672012-10-05 12:13:07 -04006933core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d562008-10-28 20:17:38 +01006934
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006935static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006936static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006937static inline void ftrace_startup_all(int command) { }
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006938
Ingo Molnarc7aafc52008-05-12 21:20:45 +02006939# define ftrace_startup_sysctl() do { } while (0)
6940# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04006941
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006942static void ftrace_update_trampoline(struct ftrace_ops *ops)
6943{
6944}
6945
Steven Rostedt3d083392008-05-12 21:20:42 +02006946#endif /* CONFIG_DYNAMIC_FTRACE */
6947
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006948__init void ftrace_init_global_array_ops(struct trace_array *tr)
6949{
6950 tr->ops = &global_ops;
6951 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006952 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006953}
6954
6955void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6956{
6957 /* If we filter on pids, update to use the pid function */
6958 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6959 if (WARN_ON(tr->ops->func != ftrace_stub))
6960 printk("ftrace ops had %pS for function\n",
6961 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006962 }
6963 tr->ops->func = func;
6964 tr->ops->private = tr;
6965}
6966
6967void ftrace_reset_array_ops(struct trace_array *tr)
6968{
6969 tr->ops->func = ftrace_stub;
6970}
6971
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006972static nokprobe_inline void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006973__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04006974 struct ftrace_ops *ignored, struct ftrace_regs *fregs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006975{
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04006976 struct pt_regs *regs = ftrace_get_regs(fregs);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006977 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006978 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04006979
Steven Rostedt (VMware)773c1672020-11-05 21:32:46 -05006980 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START, TRACE_LIST_MAX);
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006981 if (bit < 0)
6982 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04006983
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006984 /*
6985 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08006986 * they must be freed after a synchronize_rcu().
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006987 */
6988 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006989
Steven Rostedt0a016402012-11-02 17:03:03 -04006990 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (VMware)2fa717a2019-04-11 11:46:13 -04006991 /* Stub functions don't need to be called nor tested */
6992 if (op->flags & FTRACE_OPS_FL_STUB)
6993 continue;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006994 /*
6995 * Check the following for each ops before calling their func:
6996 * if RCU flag is set, then rcu_is_watching() must be true
6997 * if PER_CPU is set, then ftrace_function_local_disable()
6998 * must be false
6999 * Otherwise test if the ip matches the ops filter
7000 *
7001 * If any of the above fails then the op->func() is not executed.
7002 */
7003 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05007004 ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04007005 if (FTRACE_WARN_ON(!op->func)) {
7006 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007007 goto out;
7008 }
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007009 op->func(ip, parent_ip, op, fregs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007010 }
Steven Rostedt0a016402012-11-02 17:03:03 -04007011 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007012out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04007013 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04007014 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04007015}
7016
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007017/*
7018 * Some archs only support passing ip and parent_ip. Even though
7019 * the list function ignores the op parameter, we do not want any
7020 * C side effects, where a function is called without the caller
7021 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04007022 * Archs are to support both the regs and ftrace_ops at the same time.
7023 * If they support ftrace_ops, it is assumed they support regs.
7024 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09007025 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
7026 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04007027 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08007028 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007029 */
7030#if ARCH_SUPPORTS_FTRACE_OPS
7031static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007032 struct ftrace_ops *op, struct ftrace_regs *fregs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007033{
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007034 __ftrace_ops_list_func(ip, parent_ip, NULL, fregs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007035}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09007036NOKPROBE_SYMBOL(ftrace_ops_list_func);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007037#else
7038static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
7039{
Steven Rostedta1e2e312011-08-09 12:50:46 -04007040 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007041}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09007042NOKPROBE_SYMBOL(ftrace_ops_no_ops);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007043#endif
7044
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007045/*
7046 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05007047 * recursion, needs RCU protection and/or requires per cpu handling, then
7048 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007049 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05007050static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007051 struct ftrace_ops *op, struct ftrace_regs *fregs)
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007052{
7053 int bit;
7054
Steven Rostedt (VMware)773c1672020-11-05 21:32:46 -05007055 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START, TRACE_LIST_MAX);
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007056 if (bit < 0)
7057 return;
7058
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05007059 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007060
Steven Rostedt (VMware)b40341f2020-09-29 12:40:31 -04007061 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007062 op->func(ip, parent_ip, op, fregs);
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05007063
7064 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007065 trace_clear_recursion(bit);
7066}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09007067NOKPROBE_SYMBOL(ftrace_ops_assist_func);
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007068
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04007069/**
7070 * ftrace_ops_get_func - get the function a trampoline should call
7071 * @ops: the ops to get the function for
7072 *
7073 * Normally the mcount trampoline will call the ops->func, but there
7074 * are times that it should not. For example, if the ops does not
7075 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08007076 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04007077 *
7078 * Returns the function that the trampoline should call for @ops.
7079 */
7080ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
7081{
7082 /*
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05007083 * If the function does not handle recursion or needs to be RCU safe,
7084 * then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04007085 */
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05007086 if (ops->flags & (FTRACE_OPS_FL_RECURSION |
7087 FTRACE_OPS_FL_RCU))
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05007088 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04007089
7090 return ops->func;
7091}
7092
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007093static void
7094ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
7095 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05007096{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007097 struct trace_array *tr = data;
7098 struct trace_pid_list *pid_list;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007099 struct trace_pid_list *no_pid_list;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007100
7101 pid_list = rcu_dereference_sched(tr->function_pids);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007102 no_pid_list = rcu_dereference_sched(tr->function_no_pids);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007103
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007104 if (trace_ignore_this_task(pid_list, no_pid_list, next))
Steven Rostedt (VMware)717e3f52020-03-19 23:40:40 -04007105 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7106 FTRACE_PID_IGNORE);
7107 else
7108 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7109 next->pid);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007110}
7111
Namhyung Kim1e104862017-04-17 11:44:28 +09007112static void
7113ftrace_pid_follow_sched_process_fork(void *data,
7114 struct task_struct *self,
7115 struct task_struct *task)
7116{
7117 struct trace_pid_list *pid_list;
7118 struct trace_array *tr = data;
7119
7120 pid_list = rcu_dereference_sched(tr->function_pids);
7121 trace_filter_add_remove_task(pid_list, self, task);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007122
7123 pid_list = rcu_dereference_sched(tr->function_no_pids);
7124 trace_filter_add_remove_task(pid_list, self, task);
Namhyung Kim1e104862017-04-17 11:44:28 +09007125}
7126
7127static void
7128ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
7129{
7130 struct trace_pid_list *pid_list;
7131 struct trace_array *tr = data;
7132
7133 pid_list = rcu_dereference_sched(tr->function_pids);
7134 trace_filter_add_remove_task(pid_list, NULL, task);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007135
7136 pid_list = rcu_dereference_sched(tr->function_no_pids);
7137 trace_filter_add_remove_task(pid_list, NULL, task);
Namhyung Kim1e104862017-04-17 11:44:28 +09007138}
7139
7140void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
7141{
7142 if (enable) {
7143 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7144 tr);
Steven Rostedt (VMware)afcab632020-08-04 20:00:02 -04007145 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
Namhyung Kim1e104862017-04-17 11:44:28 +09007146 tr);
7147 } else {
7148 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7149 tr);
Steven Rostedt (VMware)afcab632020-08-04 20:00:02 -04007150 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
Namhyung Kim1e104862017-04-17 11:44:28 +09007151 tr);
7152 }
7153}
7154
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007155static void clear_ftrace_pids(struct trace_array *tr, int type)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007156{
7157 struct trace_pid_list *pid_list;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007158 struct trace_pid_list *no_pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05007159 int cpu;
7160
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007161 pid_list = rcu_dereference_protected(tr->function_pids,
7162 lockdep_is_held(&ftrace_lock));
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007163 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7164 lockdep_is_held(&ftrace_lock));
7165
7166 /* Make sure there's something to do */
Steven Rostedt (VMware)27683622020-03-25 19:51:19 -04007167 if (!pid_type_enabled(type, pid_list, no_pid_list))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007168 return;
7169
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007170 /* See if the pids still need to be checked after this */
Steven Rostedt (VMware)27683622020-03-25 19:51:19 -04007171 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007172 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7173 for_each_possible_cpu(cpu)
7174 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
7175 }
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007176
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007177 if (type & TRACE_PIDS)
7178 rcu_assign_pointer(tr->function_pids, NULL);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007179
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007180 if (type & TRACE_NO_PIDS)
7181 rcu_assign_pointer(tr->function_no_pids, NULL);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007182
7183 /* Wait till all users are no longer using pid filtering */
Paul E. McKenney74401722018-11-06 18:44:52 -08007184 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007185
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007186 if ((type & TRACE_PIDS) && pid_list)
7187 trace_free_pid_list(pid_list);
7188
7189 if ((type & TRACE_NO_PIDS) && no_pid_list)
7190 trace_free_pid_list(no_pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05007191}
7192
Namhyung Kimd879d0b2017-04-17 11:44:27 +09007193void ftrace_clear_pids(struct trace_array *tr)
7194{
7195 mutex_lock(&ftrace_lock);
7196
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007197 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
Namhyung Kimd879d0b2017-04-17 11:44:27 +09007198
7199 mutex_unlock(&ftrace_lock);
7200}
7201
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007202static void ftrace_pid_reset(struct trace_array *tr, int type)
Steven Rostedte32d8952008-12-04 00:26:41 -05007203{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007204 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007205 clear_ftrace_pids(tr, type);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007206
7207 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04007208 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007209
7210 mutex_unlock(&ftrace_lock);
7211}
7212
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007213/* Greater than any max PID */
7214#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7215
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007216static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007217 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007218{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007219 struct trace_pid_list *pid_list;
7220 struct trace_array *tr = m->private;
7221
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007222 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007223 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007224
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007225 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007226
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007227 if (!pid_list)
7228 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7229
7230 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007231}
7232
7233static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7234{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007235 struct trace_array *tr = m->private;
7236 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7237
Vasily Averine4075e82020-01-24 10:02:56 +03007238 if (v == FTRACE_NO_PIDS) {
7239 (*pos)++;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007240 return NULL;
Vasily Averine4075e82020-01-24 10:02:56 +03007241 }
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007242 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007243}
7244
7245static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007246 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007247{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007248 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007249 mutex_unlock(&ftrace_lock);
7250}
7251
7252static int fpid_show(struct seq_file *m, void *v)
7253{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007254 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01007255 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007256 return 0;
7257 }
7258
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007259 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007260}
7261
7262static const struct seq_operations ftrace_pid_sops = {
7263 .start = fpid_start,
7264 .next = fpid_next,
7265 .stop = fpid_stop,
7266 .show = fpid_show,
7267};
7268
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007269static void *fnpid_start(struct seq_file *m, loff_t *pos)
7270 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007271{
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007272 struct trace_pid_list *pid_list;
7273 struct trace_array *tr = m->private;
7274
7275 mutex_lock(&ftrace_lock);
7276 rcu_read_lock_sched();
7277
7278 pid_list = rcu_dereference_sched(tr->function_no_pids);
7279
7280 if (!pid_list)
7281 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7282
7283 return trace_pid_start(pid_list, pos);
7284}
7285
7286static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
7287{
7288 struct trace_array *tr = m->private;
7289 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
7290
7291 if (v == FTRACE_NO_PIDS) {
7292 (*pos)++;
7293 return NULL;
7294 }
7295 return trace_pid_next(pid_list, v, pos);
7296}
7297
7298static const struct seq_operations ftrace_no_pid_sops = {
7299 .start = fnpid_start,
7300 .next = fnpid_next,
7301 .stop = fpid_stop,
7302 .show = fpid_show,
7303};
7304
7305static int pid_open(struct inode *inode, struct file *file, int type)
7306{
7307 const struct seq_operations *seq_ops;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007308 struct trace_array *tr = inode->i_private;
7309 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007310 int ret = 0;
7311
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04007312 ret = tracing_check_open_get_tr(tr);
7313 if (ret)
7314 return ret;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007315
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007316 if ((file->f_mode & FMODE_WRITE) &&
7317 (file->f_flags & O_TRUNC))
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007318 ftrace_pid_reset(tr, type);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007319
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007320 switch (type) {
7321 case TRACE_PIDS:
7322 seq_ops = &ftrace_pid_sops;
7323 break;
7324 case TRACE_NO_PIDS:
7325 seq_ops = &ftrace_no_pid_sops;
7326 break;
Kaitao Cheng026bb842020-05-29 22:12:14 +08007327 default:
7328 trace_array_put(tr);
7329 WARN_ON_ONCE(1);
7330 return -EINVAL;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007331 }
7332
7333 ret = seq_open(file, seq_ops);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007334 if (ret < 0) {
7335 trace_array_put(tr);
7336 } else {
7337 m = file->private_data;
7338 /* copy tr over to seq ops */
7339 m->private = tr;
7340 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007341
7342 return ret;
7343}
7344
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007345static int
7346ftrace_pid_open(struct inode *inode, struct file *file)
7347{
7348 return pid_open(inode, file, TRACE_PIDS);
7349}
7350
7351static int
7352ftrace_no_pid_open(struct inode *inode, struct file *file)
7353{
7354 return pid_open(inode, file, TRACE_NO_PIDS);
7355}
7356
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007357static void ignore_task_cpu(void *data)
7358{
7359 struct trace_array *tr = data;
7360 struct trace_pid_list *pid_list;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007361 struct trace_pid_list *no_pid_list;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007362
7363 /*
7364 * This function is called by on_each_cpu() while the
7365 * event_mutex is held.
7366 */
7367 pid_list = rcu_dereference_protected(tr->function_pids,
7368 mutex_is_locked(&ftrace_lock));
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007369 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7370 mutex_is_locked(&ftrace_lock));
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007371
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007372 if (trace_ignore_this_task(pid_list, no_pid_list, current))
Steven Rostedt (VMware)717e3f52020-03-19 23:40:40 -04007373 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7374 FTRACE_PID_IGNORE);
7375 else
7376 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7377 current->pid);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007378}
7379
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007380static ssize_t
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007381pid_write(struct file *filp, const char __user *ubuf,
7382 size_t cnt, loff_t *ppos, int type)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007383{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007384 struct seq_file *m = filp->private_data;
7385 struct trace_array *tr = m->private;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007386 struct trace_pid_list *filtered_pids;
7387 struct trace_pid_list *other_pids;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007388 struct trace_pid_list *pid_list;
7389 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007390
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007391 if (!cnt)
7392 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007393
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007394 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007395
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007396 switch (type) {
7397 case TRACE_PIDS:
7398 filtered_pids = rcu_dereference_protected(tr->function_pids,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007399 lockdep_is_held(&ftrace_lock));
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007400 other_pids = rcu_dereference_protected(tr->function_no_pids,
7401 lockdep_is_held(&ftrace_lock));
7402 break;
7403 case TRACE_NO_PIDS:
7404 filtered_pids = rcu_dereference_protected(tr->function_no_pids,
7405 lockdep_is_held(&ftrace_lock));
7406 other_pids = rcu_dereference_protected(tr->function_pids,
7407 lockdep_is_held(&ftrace_lock));
7408 break;
Kaitao Cheng026bb842020-05-29 22:12:14 +08007409 default:
7410 ret = -EINVAL;
7411 WARN_ON_ONCE(1);
7412 goto out;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007413 }
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007414
7415 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7416 if (ret < 0)
7417 goto out;
7418
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007419 switch (type) {
7420 case TRACE_PIDS:
7421 rcu_assign_pointer(tr->function_pids, pid_list);
7422 break;
7423 case TRACE_NO_PIDS:
7424 rcu_assign_pointer(tr->function_no_pids, pid_list);
7425 break;
7426 }
7427
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007428
7429 if (filtered_pids) {
Paul E. McKenney74401722018-11-06 18:44:52 -08007430 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007431 trace_free_pid_list(filtered_pids);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007432 } else if (pid_list && !other_pids) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007433 /* Register a probe to set whether to ignore the tracing of a task */
7434 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7435 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007436
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007437 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007438 * Ignoring of pids is done at task switch. But we have to
7439 * check for those tasks that are currently running.
7440 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007441 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007442 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007443
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007444 ftrace_update_pid_func();
7445 ftrace_startup_all(0);
7446 out:
7447 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007448
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007449 if (ret > 0)
7450 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05007451
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007452 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007453}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007454
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007455static ssize_t
7456ftrace_pid_write(struct file *filp, const char __user *ubuf,
7457 size_t cnt, loff_t *ppos)
7458{
7459 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
7460}
7461
7462static ssize_t
7463ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
7464 size_t cnt, loff_t *ppos)
7465{
7466 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
7467}
7468
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007469static int
7470ftrace_pid_release(struct inode *inode, struct file *file)
7471{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007472 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007473
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007474 trace_array_put(tr);
7475
7476 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007477}
7478
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007479static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007480 .open = ftrace_pid_open,
7481 .write = ftrace_pid_write,
7482 .read = seq_read,
Steven Rostedt (Red Hat)098c879e2013-12-21 17:39:40 -05007483 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007484 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007485};
7486
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007487static const struct file_operations ftrace_no_pid_fops = {
7488 .open = ftrace_no_pid_open,
7489 .write = ftrace_no_pid_write,
7490 .read = seq_read,
7491 .llseek = tracing_lseek,
7492 .release = ftrace_pid_release,
7493};
7494
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007495void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007496{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007497 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007498 tr, &ftrace_pid_fops);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007499 trace_create_file("set_ftrace_notrace_pid", 0644, d_tracer,
7500 tr, &ftrace_no_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007501}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007502
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04007503void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
7504 struct dentry *d_tracer)
7505{
7506 /* Only the top level directory has the dyn_tracefs and profile */
7507 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
7508
7509 ftrace_init_dyn_tracefs(d_tracer);
7510 ftrace_profile_tracefs(d_tracer);
7511}
7512
Steven Rostedt3d083392008-05-12 21:20:42 +02007513/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04007514 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04007515 *
7516 * This function should be used by panic code. It stops ftrace
7517 * but in a not so nice way. If you need to simply kill ftrace
7518 * from a non-atomic section, use ftrace_kill.
7519 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04007520void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04007521{
7522 ftrace_disabled = 1;
7523 ftrace_enabled = 0;
Yisheng Xie5ccba642018-02-02 10:14:49 +08007524 ftrace_trace_function = ftrace_stub;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04007525}
7526
7527/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04007528 * Test if ftrace is dead or not.
7529 */
7530int ftrace_is_dead(void)
7531{
7532 return ftrace_disabled;
7533}
7534
7535/**
Steven Rostedt3d083392008-05-12 21:20:42 +02007536 * register_ftrace_function - register a function for profiling
7537 * @ops - ops structure that holds the function for profiling.
7538 *
7539 * Register a function to be called by all functions in the
7540 * kernel.
7541 *
7542 * Note: @ops->func and all the functions it calls must be labeled
7543 * with "notrace", otherwise it will go into a
7544 * recursive loop.
7545 */
7546int register_ftrace_function(struct ftrace_ops *ops)
7547{
Colin Ian King3b1a8f42021-07-21 13:09:15 +01007548 int ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02007549
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09007550 ftrace_ops_init(ops);
7551
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007552 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01007553
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05007554 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04007555
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007556 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02007557
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007558 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02007559}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04007560EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02007561
7562/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01007563 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02007564 * @ops - ops structure that holds the function to unregister
7565 *
7566 * Unregister a function that was added to be called by ftrace profiling.
7567 */
7568int unregister_ftrace_function(struct ftrace_ops *ops)
7569{
7570 int ret;
7571
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007572 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05007573 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007574 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007575
7576 return ret;
7577}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04007578EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007579
Miroslav Benes71624312019-10-16 13:33:13 +02007580static bool is_permanent_ops_registered(void)
7581{
7582 struct ftrace_ops *op;
7583
7584 do_for_each_ftrace_op(op, ftrace_ops_list) {
7585 if (op->flags & FTRACE_OPS_FL_PERMANENT)
7586 return true;
7587 } while_for_each_ftrace_op(op);
7588
7589 return false;
7590}
7591
Ingo Molnare309b412008-05-12 21:20:51 +02007592int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007593ftrace_enable_sysctl(struct ctl_table *table, int write,
Tobias Klauser54fa9ba2020-09-07 11:32:07 +02007594 void *buffer, size_t *lenp, loff_t *ppos)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007595{
Steven Rostedt45a4a232011-04-21 23:16:46 -04007596 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02007597
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007598 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007599
Steven Rostedt45a4a232011-04-21 23:16:46 -04007600 if (unlikely(ftrace_disabled))
7601 goto out;
7602
7603 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007604
Li Zefana32c7762009-06-26 16:55:51 +08007605 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007606 goto out;
7607
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007608 if (ftrace_enabled) {
7609
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007610 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08007611 if (rcu_dereference_protected(ftrace_ops_list,
7612 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01007613 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007614
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05007615 ftrace_startup_sysctl();
7616
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007617 } else {
Miroslav Benes71624312019-10-16 13:33:13 +02007618 if (is_permanent_ops_registered()) {
7619 ftrace_enabled = true;
7620 ret = -EBUSY;
7621 goto out;
7622 }
7623
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007624 /* stopping ftrace calls (just send to ftrace_stub) */
7625 ftrace_trace_function = ftrace_stub;
7626
7627 ftrace_shutdown_sysctl();
7628 }
7629
Miroslav Benes71624312019-10-16 13:33:13 +02007630 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007631 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007632 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02007633 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02007634}