blob: f9feb197b2daaf348622665924795a3ae88e4fda [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 Rostedt (VMware)34cdd182020-06-17 16:56:16 -0400122/* Defined by vmlinux.lds.h see the commment above arch_ftrace_ops_list_func for details */
123void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
124 struct ftrace_ops *op, struct ftrace_regs *fregs);
Steven Rostedtb8489142011-05-04 09:27:52 -0400125
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900126static inline void ftrace_ops_init(struct ftrace_ops *ops)
127{
128#ifdef CONFIG_DYNAMIC_FTRACE
129 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400130 mutex_init(&ops->local_hash.regex_lock);
131 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900132 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
133 }
134#endif
135}
136
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400137static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -0400138 struct ftrace_ops *op, struct ftrace_regs *fregs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500139{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400140 struct trace_array *tr = op->private;
Steven Rostedt (VMware)717e3f52020-03-19 23:40:40 -0400141 int pid;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400142
Steven Rostedt (VMware)717e3f52020-03-19 23:40:40 -0400143 if (tr) {
144 pid = this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid);
145 if (pid == FTRACE_PID_IGNORE)
146 return;
147 if (pid != FTRACE_PID_TRACE &&
148 pid != current->pid)
149 return;
150 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500151
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -0400152 op->saved_func(ip, parent_ip, op, fregs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500153}
154
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500155static void ftrace_sync_ipi(void *data)
156{
157 /* Probably not needed, but do it anyway */
158 smp_rmb();
159}
160
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100161static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
162{
163 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500164 * 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 +0100165 * then it needs to call the list anyway.
166 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +0200167 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
168 FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100169 return ftrace_ops_list_func;
170
171 return ftrace_ops_get_func(ops);
172}
173
Steven Rostedt2b499382011-05-03 22:49:52 -0400174static void update_ftrace_function(void)
175{
176 ftrace_func_t func;
177
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400178 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400179 * Prepare the ftrace_ops that the arch callback will use.
180 * If there's only one ftrace_ops registered, the ftrace_ops_list
181 * will point to the ops we want.
182 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800183 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
184 lockdep_is_held(&ftrace_lock));
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400185
186 /* If there's no ftrace_ops registered, just call the stub function */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800187 if (set_function_trace_op == &ftrace_list_end) {
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400188 func = ftrace_stub;
189
190 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400191 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400192 * recursion safe and not dynamic and the arch supports passing ops,
193 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400194 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800195 } else if (rcu_dereference_protected(ftrace_ops_list->next,
196 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100197 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400198
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400199 } else {
200 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500201 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400202 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400203 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400204
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400205 update_function_graph_func();
206
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500207 /* If there's no change, then do nothing more here */
208 if (ftrace_trace_function == func)
209 return;
210
211 /*
212 * If we are using the list function, it doesn't care
213 * about the function_trace_ops.
214 */
215 if (func == ftrace_ops_list_func) {
216 ftrace_trace_function = func;
217 /*
218 * Don't even bother setting function_trace_ops,
219 * it would be racy to do so anyway.
220 */
221 return;
222 }
223
224#ifndef CONFIG_DYNAMIC_FTRACE
225 /*
226 * For static tracing, we need to be a bit more careful.
227 * The function change takes affect immediately. Thus,
Qiujun Huangfdda88d2020-10-02 22:31:26 +0800228 * we need to coordinate the setting of the function_trace_ops
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500229 * with the setting of the ftrace_trace_function.
230 *
231 * Set the function to the list ops, which will call the
232 * function we want, albeit indirectly, but it handles the
233 * ftrace_ops and doesn't depend on function_trace_op.
234 */
235 ftrace_trace_function = ftrace_ops_list_func;
236 /*
237 * Make sure all CPUs see this. Yes this is slow, but static
238 * tracing is slow and nasty to have enabled.
239 */
Paul E. McKenneye5a971d2020-04-03 12:10:28 -0700240 synchronize_rcu_tasks_rude();
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500241 /* Now all cpus are using the list ops. */
242 function_trace_op = set_function_trace_op;
243 /* Make sure the function_trace_op is visible on all CPUs */
244 smp_wmb();
245 /* Nasty way to force a rmb on all cpus */
246 smp_call_function(ftrace_sync_ipi, NULL, 1);
247 /* OK, we are all set to update the ftrace_trace_function now! */
248#endif /* !CONFIG_DYNAMIC_FTRACE */
249
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400250 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400251}
252
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800253static void add_ftrace_ops(struct ftrace_ops __rcu **list,
254 struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200255{
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800256 rcu_assign_pointer(ops->next, *list);
257
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200258 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400259 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200260 * CPU might be walking that list. We need to make sure
261 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400262 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200263 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400264 rcu_assign_pointer(*list, ops);
265}
Steven Rostedt3d083392008-05-12 21:20:42 +0200266
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800267static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
268 struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400269{
270 struct ftrace_ops **p;
271
272 /*
273 * If we are removing the last function, then simply point
274 * to the ftrace_stub.
275 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800276 if (rcu_dereference_protected(*list,
277 lockdep_is_held(&ftrace_lock)) == ops &&
278 rcu_dereference_protected(ops->next,
279 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt2b499382011-05-03 22:49:52 -0400280 *list = &ftrace_list_end;
281 return 0;
282 }
283
284 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
285 if (*p == ops)
286 break;
287
288 if (*p != ops)
289 return -1;
290
291 *p = (*p)->next;
292 return 0;
293}
294
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400295static void ftrace_update_trampoline(struct ftrace_ops *ops);
296
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500297int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400298{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500299 if (ops->flags & FTRACE_OPS_FL_DELETED)
300 return -EINVAL;
301
Steven Rostedtb8489142011-05-04 09:27:52 -0400302 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
303 return -EBUSY;
304
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900305#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400306 /*
307 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
308 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
309 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
310 */
311 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
312 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
313 return -EINVAL;
314
315 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
316 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
317#endif
Miroslav Benes71624312019-10-16 13:33:13 +0200318 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
319 return -EBUSY;
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400320
Kefeng Wanga20deb32021-11-08 18:33:51 -0800321 if (!is_kernel_core_data((unsigned long)ops))
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400322 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
323
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500324 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400325
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400326 /* Always save the function, and reset at unregistering */
327 ops->saved_func = ops->func;
328
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400329 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400330 ops->func = ftrace_pid_func;
331
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400332 ftrace_update_trampoline(ops);
333
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400334 if (ftrace_enabled)
335 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200336
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200337 return 0;
338}
339
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500340int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200341{
Steven Rostedt2b499382011-05-03 22:49:52 -0400342 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200343
Steven Rostedtb8489142011-05-04 09:27:52 -0400344 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
345 return -EBUSY;
346
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500347 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400348
Steven Rostedt2b499382011-05-03 22:49:52 -0400349 if (ret < 0)
350 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400351
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400352 if (ftrace_enabled)
353 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200354
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400355 ops->func = ops->saved_func;
356
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500357 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200358}
359
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500360static void ftrace_update_pid_func(void)
361{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400362 struct ftrace_ops *op;
363
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400364 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500365 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900366 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500367
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400368 do_for_each_ftrace_op(op, ftrace_ops_list) {
369 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400370 op->func = ftrace_pids_enabled(op) ?
371 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400372 ftrace_update_trampoline(op);
373 }
374 } while_for_each_ftrace_op(op);
375
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400376 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500377}
378
Steven Rostedt493762f2009-03-23 17:12:36 -0400379#ifdef CONFIG_FUNCTION_PROFILER
380struct ftrace_profile {
381 struct hlist_node node;
382 unsigned long ip;
383 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400384#ifdef CONFIG_FUNCTION_GRAPH_TRACER
385 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400386 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400387#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400388};
389
390struct ftrace_profile_page {
391 struct ftrace_profile_page *next;
392 unsigned long index;
393 struct ftrace_profile records[];
394};
395
Steven Rostedtcafb1682009-03-24 20:50:39 -0400396struct ftrace_profile_stat {
397 atomic_t disabled;
398 struct hlist_head *hash;
399 struct ftrace_profile_page *pages;
400 struct ftrace_profile_page *start;
401 struct tracer_stat stat;
402};
403
Steven Rostedt493762f2009-03-23 17:12:36 -0400404#define PROFILE_RECORDS_SIZE \
405 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
406
407#define PROFILES_PER_PAGE \
408 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
409
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400410static int ftrace_profile_enabled __read_mostly;
411
412/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400413static DEFINE_MUTEX(ftrace_profile_lock);
414
Steven Rostedtcafb1682009-03-24 20:50:39 -0400415static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400416
Namhyung Kim20079eb2013-04-10 08:55:50 +0900417#define FTRACE_PROFILE_HASH_BITS 10
418#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400419
Steven Rostedt493762f2009-03-23 17:12:36 -0400420static void *
421function_stat_next(void *v, int idx)
422{
423 struct ftrace_profile *rec = v;
424 struct ftrace_profile_page *pg;
425
426 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
427
428 again:
Li Zefan0296e422009-06-26 11:15:37 +0800429 if (idx != 0)
430 rec++;
431
Steven Rostedt493762f2009-03-23 17:12:36 -0400432 if ((void *)rec >= (void *)&pg->records[pg->index]) {
433 pg = pg->next;
434 if (!pg)
435 return NULL;
436 rec = &pg->records[0];
437 if (!rec->counter)
438 goto again;
439 }
440
441 return rec;
442}
443
444static void *function_stat_start(struct tracer_stat *trace)
445{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400446 struct ftrace_profile_stat *stat =
447 container_of(trace, struct ftrace_profile_stat, stat);
448
449 if (!stat || !stat->start)
450 return NULL;
451
452 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400453}
454
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400455#ifdef CONFIG_FUNCTION_GRAPH_TRACER
456/* function graph compares on total time */
Andy Shevchenko80042c82019-10-07 16:56:56 +0300457static int function_stat_cmp(const void *p1, const void *p2)
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400458{
Andy Shevchenko80042c82019-10-07 16:56:56 +0300459 const struct ftrace_profile *a = p1;
460 const struct ftrace_profile *b = p2;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400461
462 if (a->time < b->time)
463 return -1;
464 if (a->time > b->time)
465 return 1;
466 else
467 return 0;
468}
469#else
470/* not function graph compares against hits */
Andy Shevchenko80042c82019-10-07 16:56:56 +0300471static int function_stat_cmp(const void *p1, const void *p2)
Steven Rostedt493762f2009-03-23 17:12:36 -0400472{
Andy Shevchenko80042c82019-10-07 16:56:56 +0300473 const struct ftrace_profile *a = p1;
474 const struct ftrace_profile *b = p2;
Steven Rostedt493762f2009-03-23 17:12:36 -0400475
476 if (a->counter < b->counter)
477 return -1;
478 if (a->counter > b->counter)
479 return 1;
480 else
481 return 0;
482}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400483#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400484
485static int function_stat_headers(struct seq_file *m)
486{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400487#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100488 seq_puts(m, " Function "
489 "Hit Time Avg s^2\n"
490 " -------- "
491 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400492#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100493 seq_puts(m, " Function Hit\n"
494 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400495#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400496 return 0;
497}
498
499static int function_stat_show(struct seq_file *m, void *v)
500{
501 struct ftrace_profile *rec = v;
502 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800503 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400504#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400505 static struct trace_seq s;
506 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400507 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400508#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800509 mutex_lock(&ftrace_profile_lock);
510
511 /* we raced with function_profile_reset() */
512 if (unlikely(rec->counter == 0)) {
513 ret = -EBUSY;
514 goto out;
515 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400516
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530517#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Wen Yange31f7932020-01-03 11:02:48 +0800518 avg = div64_ul(rec->time, rec->counter);
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530519 if (tracing_thresh && (avg < tracing_thresh))
520 goto out;
521#endif
522
Steven Rostedt493762f2009-03-23 17:12:36 -0400523 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400524 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400525
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400526#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100527 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400528
Chase Douglase330b3b2010-04-26 14:02:05 -0400529 /* Sample standard deviation (s^2) */
530 if (rec->counter <= 1)
531 stddev = 0;
532 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200533 /*
534 * Apply Welford's method:
535 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
536 */
537 stddev = rec->counter * rec->time_squared -
538 rec->time * rec->time;
539
Chase Douglase330b3b2010-04-26 14:02:05 -0400540 /*
541 * Divide only 1000 for ns^2 -> us^2 conversion.
542 * trace_print_graph_duration will divide 1000 again.
543 */
Wen Yange31f7932020-01-03 11:02:48 +0800544 stddev = div64_ul(stddev,
545 rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400546 }
547
Steven Rostedt34886c82009-03-25 21:00:47 -0400548 trace_seq_init(&s);
549 trace_print_graph_duration(rec->time, &s);
550 trace_seq_puts(&s, " ");
551 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400552 trace_seq_puts(&s, " ");
553 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400554 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400555#endif
556 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800557out:
558 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400559
Li Zefan3aaba202010-08-23 16:50:12 +0800560 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400561}
562
Steven Rostedtcafb1682009-03-24 20:50:39 -0400563static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400564{
565 struct ftrace_profile_page *pg;
566
Steven Rostedtcafb1682009-03-24 20:50:39 -0400567 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400568
569 while (pg) {
570 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
571 pg->index = 0;
572 pg = pg->next;
573 }
574
Steven Rostedtcafb1682009-03-24 20:50:39 -0400575 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400576 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
577}
578
chongjiapeng172f7ba2021-10-19 18:48:54 +0800579static int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400580{
581 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400582 int functions;
583 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400584 int i;
585
586 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400587 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400588 return 0;
589
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
591 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400592 return -ENOMEM;
593
Steven Rostedt318e0a72009-03-25 20:06:34 -0400594#ifdef CONFIG_DYNAMIC_FTRACE
595 functions = ftrace_update_tot_cnt;
596#else
597 /*
598 * We do not know the number of functions that exist because
599 * dynamic tracing is what counts them. With past experience
600 * we have around 20K functions. That should be more than enough.
601 * It is highly unlikely we will execute every function in
602 * the kernel.
603 */
604 functions = 20000;
605#endif
606
Steven Rostedtcafb1682009-03-24 20:50:39 -0400607 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400608
Steven Rostedt318e0a72009-03-25 20:06:34 -0400609 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
610
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900611 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400612 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400613 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400614 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400615 pg = pg->next;
616 }
617
618 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400619
620 out_free:
621 pg = stat->start;
622 while (pg) {
623 unsigned long tmp = (unsigned long)pg;
624
625 pg = pg->next;
626 free_page(tmp);
627 }
628
Steven Rostedt318e0a72009-03-25 20:06:34 -0400629 stat->pages = NULL;
630 stat->start = NULL;
631
632 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400633}
634
Steven Rostedtcafb1682009-03-24 20:50:39 -0400635static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400636{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400637 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400638 int size;
639
Steven Rostedtcafb1682009-03-24 20:50:39 -0400640 stat = &per_cpu(ftrace_profile_stats, cpu);
641
642 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400643 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400644 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400645 return 0;
646 }
647
648 /*
649 * We are profiling all functions, but usually only a few thousand
650 * functions are hit. We'll make a hash of 1024 items.
651 */
652 size = FTRACE_PROFILE_HASH_SIZE;
653
Kees Cook6396bb22018-06-12 14:03:40 -0700654 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400655
Steven Rostedtcafb1682009-03-24 20:50:39 -0400656 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400657 return -ENOMEM;
658
Steven Rostedt318e0a72009-03-25 20:06:34 -0400659 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400660 if (ftrace_profile_pages_init(stat) < 0) {
661 kfree(stat->hash);
662 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400663 return -ENOMEM;
664 }
665
666 return 0;
667}
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669static int ftrace_profile_init(void)
670{
671 int cpu;
672 int ret = 0;
673
Miao Xiec4602c12013-12-16 15:20:01 +0800674 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675 ret = ftrace_profile_init_cpu(cpu);
676 if (ret)
677 break;
678 }
679
680 return ret;
681}
682
Steven Rostedt493762f2009-03-23 17:12:36 -0400683/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400684static struct ftrace_profile *
685ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400686{
687 struct ftrace_profile *rec;
688 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400689 unsigned long key;
690
Namhyung Kim20079eb2013-04-10 08:55:50 +0900691 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400693
694 if (hlist_empty(hhd))
695 return NULL;
696
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400697 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400698 if (rec->ip == ip)
699 return rec;
700 }
701
702 return NULL;
703}
704
Steven Rostedtcafb1682009-03-24 20:50:39 -0400705static void ftrace_add_profile(struct ftrace_profile_stat *stat,
706 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400707{
708 unsigned long key;
709
Namhyung Kim20079eb2013-04-10 08:55:50 +0900710 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400712}
713
Steven Rostedt318e0a72009-03-25 20:06:34 -0400714/*
715 * The memory is already allocated, this simply finds a new record to use.
716 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400717static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400718ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400719{
720 struct ftrace_profile *rec = NULL;
721
Steven Rostedt318e0a72009-03-25 20:06:34 -0400722 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400723 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 goto out;
725
Steven Rostedt493762f2009-03-23 17:12:36 -0400726 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400727 * Try to find the function again since an NMI
728 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400729 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400730 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400731 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400732 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400733
Steven Rostedtcafb1682009-03-24 20:50:39 -0400734 if (stat->pages->index == PROFILES_PER_PAGE) {
735 if (!stat->pages->next)
736 goto out;
737 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400738 }
739
Steven Rostedtcafb1682009-03-24 20:50:39 -0400740 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400741 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400742 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400743
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400746
747 return rec;
748}
749
Steven Rostedt493762f2009-03-23 17:12:36 -0400750static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400751function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -0400752 struct ftrace_ops *ops, struct ftrace_regs *fregs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400753{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 struct ftrace_profile *rec;
756 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400757
758 if (!ftrace_profile_enabled)
759 return;
760
Steven Rostedt493762f2009-03-23 17:12:36 -0400761 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762
Christoph Lameterbdffd892014-04-29 14:17:40 -0500763 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400764 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400765 goto out;
766
767 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400768 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400769 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400770 if (!rec)
771 goto out;
772 }
773
774 rec->counter++;
775 out:
776 local_irq_restore(flags);
777}
778
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400779#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500780static bool fgraph_graph_time = true;
781
782void ftrace_graph_graph_time_control(bool enable)
783{
784 fgraph_graph_time = enable;
785}
786
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400787static int profile_graph_entry(struct ftrace_graph_ent *trace)
788{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500789 struct ftrace_ret_stack *ret_stack;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900790
Steven Rostedta1e2e312011-08-09 12:50:46 -0400791 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900792
Steven Rostedt (VMware)a8f0f9e2017-08-17 16:37:25 -0400793 /* If function graph is shutting down, ret_stack can be NULL */
794 if (!current->ret_stack)
795 return 0;
796
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500797 ret_stack = ftrace_graph_get_ret_stack(current, 0);
798 if (ret_stack)
799 ret_stack->subtime = 0;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900800
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400801 return 1;
802}
803
804static void profile_graph_return(struct ftrace_graph_ret *trace)
805{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500806 struct ftrace_ret_stack *ret_stack;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400808 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400809 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400811
812 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500813 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400814 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 goto out;
816
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400817 /* If the calltime was zero'd ignore it */
818 if (!trace->calltime)
819 goto out;
820
Steven Rostedta2a16d62009-03-24 23:17:58 -0400821 calltime = trace->rettime - trace->calltime;
822
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400823 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400824
825 /* Append this call time to the parent time to subtract */
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500826 ret_stack = ftrace_graph_get_ret_stack(current, 1);
827 if (ret_stack)
828 ret_stack->subtime += calltime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400829
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500830 ret_stack = ftrace_graph_get_ret_stack(current, 0);
831 if (ret_stack && ret_stack->subtime < calltime)
832 calltime -= ret_stack->subtime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400833 else
834 calltime = 0;
835 }
836
Steven Rostedtcafb1682009-03-24 20:50:39 -0400837 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400838 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400839 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400840 rec->time_squared += calltime * calltime;
841 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400842
Steven Rostedtcafb1682009-03-24 20:50:39 -0400843 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400844 local_irq_restore(flags);
845}
846
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500847static struct fgraph_ops fprofiler_ops = {
848 .entryfunc = &profile_graph_entry,
849 .retfunc = &profile_graph_return,
850};
851
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400852static int register_ftrace_profiler(void)
853{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500854 return register_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400855}
856
857static void unregister_ftrace_profiler(void)
858{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500859 unregister_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400860}
861#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100862static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400863 .func = function_profile_call,
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -0500864 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400865 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400866};
867
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400868static int register_ftrace_profiler(void)
869{
870 return register_ftrace_function(&ftrace_profile_ops);
871}
872
873static void unregister_ftrace_profiler(void)
874{
875 unregister_ftrace_function(&ftrace_profile_ops);
876}
877#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
878
Steven Rostedt493762f2009-03-23 17:12:36 -0400879static ssize_t
880ftrace_profile_write(struct file *filp, const char __user *ubuf,
881 size_t cnt, loff_t *ppos)
882{
883 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400884 int ret;
885
Peter Huewe22fe9b52011-06-07 21:58:27 +0200886 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
887 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400888 return ret;
889
890 val = !!val;
891
892 mutex_lock(&ftrace_profile_lock);
893 if (ftrace_profile_enabled ^ val) {
894 if (val) {
895 ret = ftrace_profile_init();
896 if (ret < 0) {
897 cnt = ret;
898 goto out;
899 }
900
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400901 ret = register_ftrace_profiler();
902 if (ret < 0) {
903 cnt = ret;
904 goto out;
905 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400906 ftrace_profile_enabled = 1;
907 } else {
908 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400909 /*
910 * unregister_ftrace_profiler calls stop_machine
Paul E. McKenney74401722018-11-06 18:44:52 -0800911 * so this acts like an synchronize_rcu.
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400912 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400913 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400914 }
915 }
916 out:
917 mutex_unlock(&ftrace_profile_lock);
918
Jiri Olsacf8517c2009-10-23 19:36:16 -0400919 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400920
921 return cnt;
922}
923
924static ssize_t
925ftrace_profile_read(struct file *filp, char __user *ubuf,
926 size_t cnt, loff_t *ppos)
927{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400928 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400929 int r;
930
931 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
932 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
933}
934
935static const struct file_operations ftrace_profile_fops = {
936 .open = tracing_open_generic,
937 .read = ftrace_profile_read,
938 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200939 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400940};
941
Steven Rostedtcafb1682009-03-24 20:50:39 -0400942/* used to initialize the real stat files */
943static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400944 .name = "functions",
945 .stat_start = function_stat_start,
946 .stat_next = function_stat_next,
947 .stat_cmp = function_stat_cmp,
948 .stat_headers = function_stat_headers,
949 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400950};
951
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500952static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400953{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400954 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400955 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400956 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400957 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400958 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400959
Steven Rostedtcafb1682009-03-24 20:50:39 -0400960 for_each_possible_cpu(cpu) {
961 stat = &per_cpu(ftrace_profile_stats, cpu);
962
Geliang Tang6363c6b2016-03-15 22:12:34 +0800963 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400964 if (!name) {
965 /*
966 * The files created are permanent, if something happens
967 * we still do not free memory.
968 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400969 WARN(1,
970 "Could not allocate stat file for cpu %d\n",
971 cpu);
972 return;
973 }
974 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400975 stat->stat.name = name;
976 ret = register_stat_tracer(&stat->stat);
977 if (ret) {
978 WARN(1,
979 "Could not register function stat for cpu %d\n",
980 cpu);
981 kfree(name);
982 return;
983 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400984 }
985
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -0400986 entry = tracefs_create_file("function_profile_enabled",
987 TRACE_MODE_WRITE, d_tracer, NULL,
988 &ftrace_profile_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -0400989 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700990 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -0400991}
992
993#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500994static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400995{
996}
997#endif /* CONFIG_FUNCTION_PROFILER */
998
Steven Rostedt3d083392008-05-12 21:20:42 +0200999#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001000
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001001static struct ftrace_ops *removed_ops;
1002
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001003/*
1004 * Set when doing a global update, like enabling all recs or disabling them.
1005 * It is not set when just updating a single ftrace_ops.
1006 */
1007static bool update_all_ops;
1008
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001009#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001010# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001011#endif
1012
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001013struct ftrace_func_probe {
1014 struct ftrace_probe_ops *probe_ops;
1015 struct ftrace_ops ops;
1016 struct trace_array *tr;
1017 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001018 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001019 int ref;
1020};
1021
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001022/*
1023 * We make these constant because no one should touch them,
1024 * but they are used as the default "empty hash", to avoid allocating
1025 * it all the time. These are in a read only section such that if
1026 * anyone does try to modify it, it will cause an exception.
1027 */
1028static const struct hlist_head empty_buckets[1];
1029static const struct ftrace_hash empty_hash = {
1030 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001031};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001032#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001033
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001034struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001035 .func = ftrace_stub,
1036 .local_hash.notrace_hash = EMPTY_HASH,
1037 .local_hash.filter_hash = EMPTY_HASH,
1038 INIT_OPS_HASH(global_ops)
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05001039 .flags = FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001040 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001041};
1042
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001043/*
Ingo Molnarf2cc0202021-03-23 18:49:35 +01001044 * Used by the stack unwinder to know about dynamic ftrace trampolines.
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001045 */
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001046struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001047{
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001048 struct ftrace_ops *op = NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001049
1050 /*
1051 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08001052 * they are freed after a synchronize_rcu().
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001053 */
1054 preempt_disable_notrace();
1055
1056 do_for_each_ftrace_op(op, ftrace_ops_list) {
1057 /*
1058 * This is to check for dynamically allocated trampolines.
1059 * Trampolines that are in kernel text will have
1060 * core_kernel_text() return true.
1061 */
1062 if (op->trampoline && op->trampoline_size)
1063 if (addr >= op->trampoline &&
1064 addr < op->trampoline + op->trampoline_size) {
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001065 preempt_enable_notrace();
1066 return op;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001067 }
1068 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001069 preempt_enable_notrace();
1070
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001071 return NULL;
1072}
1073
1074/*
1075 * This is used by __kernel_text_address() to return true if the
1076 * address is on a dynamically allocated trampoline that would
1077 * not return true for either core_kernel_text() or
1078 * is_module_text_address().
1079 */
1080bool is_ftrace_trampoline(unsigned long addr)
1081{
1082 return ftrace_ops_trampoline(addr) != NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001083}
1084
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001085struct ftrace_page {
1086 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001087 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001088 int index;
Linus Torvaldsdb425232021-04-01 16:14:17 -04001089 int order;
David Milleraa5e5ce2008-05-13 22:06:56 -07001090};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001091
Steven Rostedta7900872011-12-16 16:23:44 -05001092#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1093#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001094
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001095static struct ftrace_page *ftrace_pages_start;
1096static struct ftrace_page *ftrace_pages;
1097
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001098static __always_inline unsigned long
1099ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1100{
1101 if (hash->size_bits > 0)
1102 return hash_long(ip, hash->size_bits);
1103
1104 return 0;
1105}
1106
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001107/* Only use this function if ftrace_hash_empty() has already been tested */
1108static __always_inline struct ftrace_func_entry *
1109__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001110{
1111 unsigned long key;
1112 struct ftrace_func_entry *entry;
1113 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001114
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001115 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001116 hhd = &hash->buckets[key];
1117
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001118 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001119 if (entry->ip == ip)
1120 return entry;
1121 }
1122 return NULL;
1123}
1124
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001125/**
1126 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1127 * @hash: The hash to look at
1128 * @ip: The instruction pointer to test
1129 *
1130 * Search a given @hash to see if a given instruction pointer (@ip)
1131 * exists in it.
1132 *
1133 * Returns the entry that holds the @ip if found. NULL otherwise.
1134 */
1135struct ftrace_func_entry *
1136ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1137{
1138 if (ftrace_hash_empty(hash))
1139 return NULL;
1140
1141 return __ftrace_lookup_ip(hash, ip);
1142}
1143
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001144static void __add_hash_entry(struct ftrace_hash *hash,
1145 struct ftrace_func_entry *entry)
1146{
1147 struct hlist_head *hhd;
1148 unsigned long key;
1149
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001150 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001151 hhd = &hash->buckets[key];
1152 hlist_add_head(&entry->hlist, hhd);
1153 hash->count++;
1154}
1155
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001156static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1157{
1158 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001159
1160 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1161 if (!entry)
1162 return -ENOMEM;
1163
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001164 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001165 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001166
1167 return 0;
1168}
1169
1170static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001171free_hash_entry(struct ftrace_hash *hash,
1172 struct ftrace_func_entry *entry)
1173{
1174 hlist_del(&entry->hlist);
1175 kfree(entry);
1176 hash->count--;
1177}
1178
1179static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001180remove_hash_entry(struct ftrace_hash *hash,
1181 struct ftrace_func_entry *entry)
1182{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001183 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184 hash->count--;
1185}
1186
1187static void ftrace_hash_clear(struct ftrace_hash *hash)
1188{
1189 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001190 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001191 struct ftrace_func_entry *entry;
1192 int size = 1 << hash->size_bits;
1193 int i;
1194
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001195 if (!hash->count)
1196 return;
1197
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001198 for (i = 0; i < size; i++) {
1199 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001200 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001201 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001202 }
1203 FTRACE_WARN_ON(hash->count);
1204}
1205
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001206static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1207{
1208 list_del(&ftrace_mod->list);
1209 kfree(ftrace_mod->module);
1210 kfree(ftrace_mod->func);
1211 kfree(ftrace_mod);
1212}
1213
1214static void clear_ftrace_mod_list(struct list_head *head)
1215{
1216 struct ftrace_mod_load *p, *n;
1217
1218 /* stack tracer isn't supported yet */
1219 if (!head)
1220 return;
1221
1222 mutex_lock(&ftrace_lock);
1223 list_for_each_entry_safe(p, n, head, list)
1224 free_ftrace_mod(p);
1225 mutex_unlock(&ftrace_lock);
1226}
1227
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001228static void free_ftrace_hash(struct ftrace_hash *hash)
1229{
1230 if (!hash || hash == EMPTY_HASH)
1231 return;
1232 ftrace_hash_clear(hash);
1233 kfree(hash->buckets);
1234 kfree(hash);
1235}
1236
Steven Rostedt07fd5512011-05-05 18:03:47 -04001237static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1238{
1239 struct ftrace_hash *hash;
1240
1241 hash = container_of(rcu, struct ftrace_hash, rcu);
1242 free_ftrace_hash(hash);
1243}
1244
1245static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1246{
1247 if (!hash || hash == EMPTY_HASH)
1248 return;
Paul E. McKenney74401722018-11-06 18:44:52 -08001249 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001250}
1251
Jiri Olsa5500fa52012-02-15 15:51:54 +01001252void ftrace_free_filter(struct ftrace_ops *ops)
1253{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001254 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001255 free_ftrace_hash(ops->func_hash->filter_hash);
1256 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001257}
1258
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001259static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1260{
1261 struct ftrace_hash *hash;
1262 int size;
1263
1264 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1265 if (!hash)
1266 return NULL;
1267
1268 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001269 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001270
1271 if (!hash->buckets) {
1272 kfree(hash);
1273 return NULL;
1274 }
1275
1276 hash->size_bits = size_bits;
1277
1278 return hash;
1279}
1280
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001281
1282static int ftrace_add_mod(struct trace_array *tr,
1283 const char *func, const char *module,
1284 int enable)
1285{
1286 struct ftrace_mod_load *ftrace_mod;
1287 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1288
1289 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1290 if (!ftrace_mod)
1291 return -ENOMEM;
1292
1293 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1294 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1295 ftrace_mod->enable = enable;
1296
1297 if (!ftrace_mod->func || !ftrace_mod->module)
1298 goto out_free;
1299
1300 list_add(&ftrace_mod->list, mod_head);
1301
1302 return 0;
1303
1304 out_free:
1305 free_ftrace_mod(ftrace_mod);
1306
1307 return -ENOMEM;
1308}
1309
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001310static struct ftrace_hash *
1311alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1312{
1313 struct ftrace_func_entry *entry;
1314 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001315 int size;
1316 int ret;
1317 int i;
1318
1319 new_hash = alloc_ftrace_hash(size_bits);
1320 if (!new_hash)
1321 return NULL;
1322
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001323 if (hash)
1324 new_hash->flags = hash->flags;
1325
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001327 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 return new_hash;
1329
1330 size = 1 << hash->size_bits;
1331 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001332 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333 ret = add_hash_entry(new_hash, entry->ip);
1334 if (ret < 0)
1335 goto free_hash;
1336 }
1337 }
1338
1339 FTRACE_WARN_ON(new_hash->count != hash->count);
1340
1341 return new_hash;
1342
1343 free_hash:
1344 free_ftrace_hash(new_hash);
1345 return NULL;
1346}
1347
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001348static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001349ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001350static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001351ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001352
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001353static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1354 struct ftrace_hash *new_hash);
1355
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001356static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001357{
1358 struct ftrace_func_entry *entry;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001359 struct ftrace_hash *new_hash;
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001360 struct hlist_head *hhd;
1361 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001362 int bits = 0;
1363 int i;
1364
1365 /*
Steven Rostedt (VMware)be493132020-10-05 20:21:14 -04001366 * Use around half the size (max bit of it), but
1367 * a minimum of 2 is fine (as size of 0 or 1 both give 1 for bits).
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001368 */
Steven Rostedt (VMware)be493132020-10-05 20:21:14 -04001369 bits = fls(size / 2);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001370
1371 /* Don't allocate too much */
1372 if (bits > FTRACE_HASH_MAX_BITS)
1373 bits = FTRACE_HASH_MAX_BITS;
1374
Steven Rostedt07fd5512011-05-05 18:03:47 -04001375 new_hash = alloc_ftrace_hash(bits);
1376 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001377 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001378
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001379 new_hash->flags = src->flags;
1380
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001381 size = 1 << src->size_bits;
1382 for (i = 0; i < size; i++) {
1383 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001384 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001386 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001387 }
1388 }
Namhyung Kim3e278c02017-01-20 11:44:45 +09001389 return new_hash;
1390}
1391
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001392static struct ftrace_hash *
1393__ftrace_hash_move(struct ftrace_hash *src)
1394{
1395 int size = src->count;
1396
1397 /*
1398 * If the new source is empty, just return the empty_hash.
1399 */
1400 if (ftrace_hash_empty(src))
1401 return EMPTY_HASH;
1402
1403 return dup_hash(src, size);
1404}
1405
Namhyung Kim3e278c02017-01-20 11:44:45 +09001406static int
1407ftrace_hash_move(struct ftrace_ops *ops, int enable,
1408 struct ftrace_hash **dst, struct ftrace_hash *src)
1409{
1410 struct ftrace_hash *new_hash;
1411 int ret;
1412
1413 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1414 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1415 return -EINVAL;
1416
1417 new_hash = __ftrace_hash_move(src);
1418 if (!new_hash)
1419 return -ENOMEM;
1420
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001421 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1422 if (enable) {
1423 /* IPMODIFY should be updated only when filter_hash updating */
1424 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1425 if (ret < 0) {
1426 free_ftrace_hash(new_hash);
1427 return ret;
1428 }
1429 }
1430
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001431 /*
1432 * Remove the current set, update the hash and add
1433 * them back.
1434 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001435 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001436
Steven Rostedt07fd5512011-05-05 18:03:47 -04001437 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001438
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001439 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001440
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001441 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001442}
1443
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001444static bool hash_contains_ip(unsigned long ip,
1445 struct ftrace_ops_hash *hash)
1446{
1447 /*
1448 * The function record is a match if it exists in the filter
Qiujun Huangfdda88d2020-10-02 22:31:26 +08001449 * hash and not in the notrace hash. Note, an empty hash is
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001450 * considered a match for the filter hash, but an empty
1451 * notrace hash is considered not in the notrace hash.
1452 */
1453 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001454 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001455 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001456 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001457}
1458
Steven Rostedt265c8312009-02-13 12:43:56 -05001459/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001460 * Test the hashes for this ops to see if we want to call
1461 * the ops->func or not.
1462 *
1463 * It's a match if the ip is in the ops->filter_hash or
1464 * the filter_hash does not exist or is empty,
1465 * AND
1466 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001467 *
1468 * This needs to be called with preemption disabled as
Paul E. McKenney74401722018-11-06 18:44:52 -08001469 * the hashes are freed with call_rcu().
Steven Rostedtb8489142011-05-04 09:27:52 -04001470 */
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001471int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001472ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001473{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001474 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001475 int ret;
1476
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001477#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1478 /*
1479 * There's a small race when adding ops that the ftrace handler
1480 * that wants regs, may be called without them. We can not
1481 * allow that handler to be called if regs is NULL.
1482 */
1483 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1484 return 0;
1485#endif
1486
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001487 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1488 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001489
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001490 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001491 ret = 1;
1492 else
1493 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001494
1495 return ret;
1496}
1497
1498/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001499 * This is a double for. Do not use 'break' to break out of the loop,
1500 * you must use a goto.
1501 */
1502#define do_for_each_ftrace_rec(pg, rec) \
1503 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1504 int _____i; \
1505 for (_____i = 0; _____i < pg->index; _____i++) { \
1506 rec = &pg->records[_____i];
1507
1508#define while_for_each_ftrace_rec() \
1509 } \
1510 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301511
Steven Rostedt5855fea2011-12-16 19:27:42 -05001512
1513static int ftrace_cmp_recs(const void *a, const void *b)
1514{
Steven Rostedta650e022012-04-25 13:48:13 -04001515 const struct dyn_ftrace *key = a;
1516 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001517
Steven Rostedta650e022012-04-25 13:48:13 -04001518 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001519 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001520 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1521 return 1;
1522 return 0;
1523}
1524
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001525static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1526{
1527 struct ftrace_page *pg;
1528 struct dyn_ftrace *rec = NULL;
1529 struct dyn_ftrace key;
1530
1531 key.ip = start;
1532 key.flags = end; /* overload flags, as it is unsigned long */
1533
1534 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1535 if (end < pg->records[0].ip ||
1536 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1537 continue;
1538 rec = bsearch(&key, pg->records, pg->index,
1539 sizeof(struct dyn_ftrace),
1540 ftrace_cmp_recs);
Artem Savkovd9815bf2020-03-06 18:43:17 +01001541 if (rec)
1542 break;
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001543 }
1544 return rec;
1545}
1546
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001547/**
1548 * ftrace_location_range - return the first address of a traced location
1549 * if it touches the given ip range
1550 * @start: start of range to search.
1551 * @end: end of range to search (inclusive). @end points to the last byte
1552 * to check.
1553 *
1554 * Returns rec->ip if the related ftrace location is a least partly within
1555 * the given address range. That is, the first address of the instruction
1556 * that is either a NOP or call to the function tracer. It checks the ftrace
1557 * internal tables to determine if the address belongs or not.
1558 */
1559unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001560{
Steven Rostedta650e022012-04-25 13:48:13 -04001561 struct dyn_ftrace *rec;
Steven Rostedta650e022012-04-25 13:48:13 -04001562
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001563 rec = lookup_rec(start, end);
1564 if (rec)
1565 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001566
Steven Rostedt5855fea2011-12-16 19:27:42 -05001567 return 0;
1568}
1569
Steven Rostedtc88fd862011-08-16 09:53:39 -04001570/**
1571 * ftrace_location - return true if the ip giving is a traced location
1572 * @ip: the instruction pointer to check
1573 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001574 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001575 * That is, the instruction that is either a NOP or call to
1576 * the function tracer. It checks the ftrace internal tables to
1577 * determine if the address belongs or not.
1578 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001579unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001580{
Steven Rostedta650e022012-04-25 13:48:13 -04001581 return ftrace_location_range(ip, ip);
1582}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001583
Steven Rostedta650e022012-04-25 13:48:13 -04001584/**
1585 * ftrace_text_reserved - return true if range contains an ftrace location
1586 * @start: start of range to search
1587 * @end: end of range to search (inclusive). @end points to the last byte to check.
1588 *
1589 * Returns 1 if @start and @end contains a ftrace location.
1590 * That is, the instruction that is either a NOP or call to
1591 * the function tracer. It checks the ftrace internal tables to
1592 * determine if the address belongs or not.
1593 */
Sasha Levind88471c2013-01-09 18:09:20 -05001594int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001595{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001596 unsigned long ret;
1597
1598 ret = ftrace_location_range((unsigned long)start,
1599 (unsigned long)end);
1600
1601 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001602}
1603
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001604/* Test if ops registered to this rec needs regs */
1605static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1606{
1607 struct ftrace_ops *ops;
1608 bool keep_regs = false;
1609
1610 for (ops = ftrace_ops_list;
1611 ops != &ftrace_list_end; ops = ops->next) {
1612 /* pass rec in as regs to have non-NULL val */
1613 if (ftrace_ops_test(ops, rec->ip, rec)) {
1614 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1615 keep_regs = true;
1616 break;
1617 }
1618 }
1619 }
1620
1621 return keep_regs;
1622}
1623
Cheng Jiana1246922019-05-04 19:39:39 +08001624static struct ftrace_ops *
1625ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1626static struct ftrace_ops *
Naveen N. Rao4c75b0f2020-11-26 23:38:38 +05301627ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1628static struct ftrace_ops *
Cheng Jiana1246922019-05-04 19:39:39 +08001629ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1630
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001631static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001632 int filter_hash,
1633 bool inc)
1634{
1635 struct ftrace_hash *hash;
1636 struct ftrace_hash *other_hash;
1637 struct ftrace_page *pg;
1638 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001639 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001640 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001641 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001642
1643 /* Only update if the ops has been registered */
1644 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001645 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001646
1647 /*
1648 * In the filter_hash case:
1649 * If the count is zero, we update all records.
1650 * Otherwise we just update the items in the hash.
1651 *
1652 * In the notrace_hash case:
1653 * We enable the update in the hash.
1654 * As disabling notrace means enabling the tracing,
1655 * and enabling notrace means disabling, the inc variable
1656 * gets inversed.
1657 */
1658 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001659 hash = ops->func_hash->filter_hash;
1660 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001661 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001662 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001663 } else {
1664 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001665 hash = ops->func_hash->notrace_hash;
1666 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001667 /*
1668 * If the notrace hash has no items,
1669 * then there's nothing to do.
1670 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001671 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001672 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001673 }
1674
1675 do_for_each_ftrace_rec(pg, rec) {
1676 int in_other_hash = 0;
1677 int in_hash = 0;
1678 int match = 0;
1679
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001680 if (rec->flags & FTRACE_FL_DISABLED)
1681 continue;
1682
Steven Rostedted926f92011-05-03 13:25:24 -04001683 if (all) {
1684 /*
1685 * Only the filter_hash affects all records.
1686 * Update if the record is not in the notrace hash.
1687 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001688 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001689 match = 1;
1690 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001691 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1692 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001693
1694 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001695 * If filter_hash is set, we want to match all functions
1696 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001697 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001698 * If filter_hash is not set, then we are decrementing.
1699 * That means we match anything that is in the hash
1700 * and also in the other_hash. That is, we need to turn
1701 * off functions in the other hash because they are disabled
1702 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001703 */
1704 if (filter_hash && in_hash && !in_other_hash)
1705 match = 1;
1706 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001707 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001708 match = 1;
1709 }
1710 if (!match)
1711 continue;
1712
1713 if (inc) {
1714 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001715 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001716 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001717
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05001718 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1719 rec->flags |= FTRACE_FL_DIRECT;
1720
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001721 /*
1722 * If there's only a single callback registered to a
1723 * function, and the ops has a trampoline registered
1724 * for it, then we can call it directly.
1725 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001726 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001727 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001728 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001729 /*
1730 * If we are adding another function callback
1731 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001732 * custom trampoline in use, then we need to go
1733 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001734 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001735 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001736
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001737 /*
1738 * If any ops wants regs saved for this function
1739 * then all ops will get saved regs.
1740 */
1741 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1742 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001743 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001744 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001745 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001746 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001747
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001748 /*
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05001749 * Only the internal direct_ops should have the
1750 * DIRECT flag set. Thus, if it is removing a
1751 * function, then that function should no longer
1752 * be direct.
1753 */
1754 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1755 rec->flags &= ~FTRACE_FL_DIRECT;
1756
1757 /*
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001758 * If the rec had REGS enabled and the ops that is
1759 * being removed had REGS set, then see if there is
1760 * still any ops for this record that wants regs.
1761 * If not, we can stop recording them.
1762 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001763 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001764 rec->flags & FTRACE_FL_REGS &&
1765 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1766 if (!test_rec_ops_needs_regs(rec))
1767 rec->flags &= ~FTRACE_FL_REGS;
1768 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001769
1770 /*
Cheng Jiana1246922019-05-04 19:39:39 +08001771 * The TRAMP needs to be set only if rec count
1772 * is decremented to one, and the ops that is
1773 * left has a trampoline. As TRAMP can only be
1774 * enabled if there is only a single ops attached
1775 * to it.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001776 */
Cheng Jiana1246922019-05-04 19:39:39 +08001777 if (ftrace_rec_count(rec) == 1 &&
Naveen N. Rao4c75b0f2020-11-26 23:38:38 +05301778 ftrace_find_tramp_ops_any_other(rec, ops))
Cheng Jiana1246922019-05-04 19:39:39 +08001779 rec->flags |= FTRACE_FL_TRAMP;
1780 else
1781 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001782
1783 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001784 * flags will be cleared in ftrace_check_record()
1785 * if rec count is zero.
1786 */
Steven Rostedted926f92011-05-03 13:25:24 -04001787 }
1788 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001789
1790 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04001791 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001792
Steven Rostedted926f92011-05-03 13:25:24 -04001793 /* Shortcut, if we handled all records, we are done. */
1794 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001795 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001796 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001797
1798 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001799}
1800
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001801static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001802 int filter_hash)
1803{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001804 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001805}
1806
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001807static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001808 int filter_hash)
1809{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001810 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001811}
1812
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001813static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1814 int filter_hash, int inc)
1815{
1816 struct ftrace_ops *op;
1817
1818 __ftrace_hash_rec_update(ops, filter_hash, inc);
1819
1820 if (ops->func_hash != &global_ops.local_hash)
1821 return;
1822
1823 /*
1824 * If the ops shares the global_ops hash, then we need to update
1825 * all ops that are enabled and use this hash.
1826 */
1827 do_for_each_ftrace_op(op, ftrace_ops_list) {
1828 /* Already done */
1829 if (op == ops)
1830 continue;
1831 if (op->func_hash == &global_ops.local_hash)
1832 __ftrace_hash_rec_update(op, filter_hash, inc);
1833 } while_for_each_ftrace_op(op);
1834}
1835
1836static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1837 int filter_hash)
1838{
1839 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1840}
1841
1842static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1843 int filter_hash)
1844{
1845 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1846}
1847
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001848/*
1849 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1850 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1851 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1852 * Note that old_hash and new_hash has below meanings
1853 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1854 * - If the hash is EMPTY_HASH, it hits nothing
1855 * - Anything else hits the recs which match the hash entries.
1856 */
1857static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1858 struct ftrace_hash *old_hash,
1859 struct ftrace_hash *new_hash)
1860{
1861 struct ftrace_page *pg;
1862 struct dyn_ftrace *rec, *end = NULL;
1863 int in_old, in_new;
1864
1865 /* Only update if the ops has been registered */
1866 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1867 return 0;
1868
1869 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1870 return 0;
1871
1872 /*
1873 * Since the IPMODIFY is a very address sensitive action, we do not
1874 * allow ftrace_ops to set all functions to new hash.
1875 */
1876 if (!new_hash || !old_hash)
1877 return -EINVAL;
1878
1879 /* Update rec->flags */
1880 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001881
1882 if (rec->flags & FTRACE_FL_DISABLED)
1883 continue;
1884
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001885 /* We need to update only differences of filter_hash */
1886 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1887 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1888 if (in_old == in_new)
1889 continue;
1890
1891 if (in_new) {
1892 /* New entries must ensure no others are using it */
1893 if (rec->flags & FTRACE_FL_IPMODIFY)
1894 goto rollback;
1895 rec->flags |= FTRACE_FL_IPMODIFY;
1896 } else /* Removed entry */
1897 rec->flags &= ~FTRACE_FL_IPMODIFY;
1898 } while_for_each_ftrace_rec();
1899
1900 return 0;
1901
1902rollback:
1903 end = rec;
1904
1905 /* Roll back what we did above */
1906 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001907
1908 if (rec->flags & FTRACE_FL_DISABLED)
1909 continue;
1910
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001911 if (rec == end)
1912 goto err_out;
1913
1914 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1915 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1916 if (in_old == in_new)
1917 continue;
1918
1919 if (in_new)
1920 rec->flags &= ~FTRACE_FL_IPMODIFY;
1921 else
1922 rec->flags |= FTRACE_FL_IPMODIFY;
1923 } while_for_each_ftrace_rec();
1924
1925err_out:
1926 return -EBUSY;
1927}
1928
1929static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1930{
1931 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1932
1933 if (ftrace_hash_empty(hash))
1934 hash = NULL;
1935
1936 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1937}
1938
1939/* Disabling always succeeds */
1940static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1941{
1942 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1943
1944 if (ftrace_hash_empty(hash))
1945 hash = NULL;
1946
1947 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1948}
1949
1950static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1951 struct ftrace_hash *new_hash)
1952{
1953 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1954
1955 if (ftrace_hash_empty(old_hash))
1956 old_hash = NULL;
1957
1958 if (ftrace_hash_empty(new_hash))
1959 new_hash = NULL;
1960
1961 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1962}
1963
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001964static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001965{
Steven Rostedt (VMware)6c141332021-06-07 21:39:08 -04001966 char ins[MCOUNT_INSN_SIZE];
Steven Rostedt05736a42008-09-22 14:55:47 -07001967 int i;
1968
Steven Rostedt (VMware)6c141332021-06-07 21:39:08 -04001969 if (copy_from_kernel_nofault(ins, p, MCOUNT_INSN_SIZE)) {
1970 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1971 return;
1972 }
1973
Steven Rostedt05736a42008-09-22 14:55:47 -07001974 printk(KERN_CONT "%s", fmt);
1975
1976 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
Steven Rostedt (VMware)6c141332021-06-07 21:39:08 -04001977 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
Steven Rostedt05736a42008-09-22 14:55:47 -07001978}
1979
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001980enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001981const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001982
1983static void print_bug_type(void)
1984{
1985 switch (ftrace_bug_type) {
1986 case FTRACE_BUG_UNKNOWN:
1987 break;
1988 case FTRACE_BUG_INIT:
1989 pr_info("Initializing ftrace call sites\n");
1990 break;
1991 case FTRACE_BUG_NOP:
1992 pr_info("Setting ftrace call site to NOP\n");
1993 break;
1994 case FTRACE_BUG_CALL:
1995 pr_info("Setting ftrace call site to call ftrace function\n");
1996 break;
1997 case FTRACE_BUG_UPDATE:
1998 pr_info("Updating ftrace call site to call a different ftrace function\n");
1999 break;
2000 }
2001}
2002
Steven Rostedtc88fd862011-08-16 09:53:39 -04002003/**
2004 * ftrace_bug - report and shutdown function tracer
2005 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002006 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04002007 *
2008 * The arch code that enables or disables the function tracing
2009 * can call ftrace_bug() when it has detected a problem in
2010 * modifying the code. @failed should be one of either:
2011 * EFAULT - if the problem happens on reading the @ip address
2012 * EINVAL - if what is read at @ip is not what was expected
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05302013 * EPERM - if the problem happens on writing to the @ip address
Steven Rostedtc88fd862011-08-16 09:53:39 -04002014 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002015void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002016{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002017 unsigned long ip = rec ? rec->ip : 0;
2018
Cheng Jianc143b772020-05-15 10:08:28 +00002019 pr_info("------------[ ftrace bug ]------------\n");
2020
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002021 switch (failed) {
2022 case -EFAULT:
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002023 pr_info("ftrace faulted on modifying ");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07002024 print_ip_sym(KERN_INFO, ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002025 break;
2026 case -EINVAL:
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002027 pr_info("ftrace failed to modify ");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07002028 print_ip_sym(KERN_INFO, ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002029 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002030 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002031 if (ftrace_expected) {
2032 print_ip_ins(" expected: ", ftrace_expected);
2033 pr_cont("\n");
2034 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002035 break;
2036 case -EPERM:
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002037 pr_info("ftrace faulted on writing ");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07002038 print_ip_sym(KERN_INFO, ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002039 break;
2040 default:
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002041 pr_info("ftrace faulted on unknown error ");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07002042 print_ip_sym(KERN_INFO, ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002043 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002044 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002045 if (rec) {
2046 struct ftrace_ops *ops = NULL;
2047
2048 pr_info("ftrace record flags: %lx\n", rec->flags);
2049 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2050 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2051 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2052 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002053 if (ops) {
2054 do {
2055 pr_cont("\ttramp: %pS (%pS)",
2056 (void *)ops->trampoline,
2057 (void *)ops->func);
2058 ops = ftrace_find_tramp_ops_next(rec, ops);
2059 } while (ops);
2060 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002061 pr_cont("\ttramp: ERROR!");
2062
2063 }
2064 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002065 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002066 }
Cheng Jianc143b772020-05-15 10:08:28 +00002067
2068 FTRACE_WARN_ON_ONCE(1);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002069}
2070
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002071static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002072{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002073 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002074
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002075 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2076
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002077 if (rec->flags & FTRACE_FL_DISABLED)
2078 return FTRACE_UPDATE_IGNORE;
2079
Steven Rostedt982c3502008-11-15 16:31:41 -05002080 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002081 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002082 *
Steven Rostedted926f92011-05-03 13:25:24 -04002083 * If the record has a ref count, then we need to enable it
2084 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002085 *
Steven Rostedted926f92011-05-03 13:25:24 -04002086 * Otherwise we make sure its disabled.
2087 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002088 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002089 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002090 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002091 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002092 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002093
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002094 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002095 * If enabling and the REGS flag does not match the REGS_EN, or
2096 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2097 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002098 * Same for direct calls.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002099 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002100 if (flag) {
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002101 if (!(rec->flags & FTRACE_FL_REGS) !=
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002102 !(rec->flags & FTRACE_FL_REGS_EN))
2103 flag |= FTRACE_FL_REGS;
2104
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002105 if (!(rec->flags & FTRACE_FL_TRAMP) !=
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002106 !(rec->flags & FTRACE_FL_TRAMP_EN))
2107 flag |= FTRACE_FL_TRAMP;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002108
2109 /*
2110 * Direct calls are special, as count matters.
2111 * We must test the record for direct, if the
2112 * DIRECT and DIRECT_EN do not match, but only
2113 * if the count is 1. That's because, if the
2114 * count is something other than one, we do not
2115 * want the direct enabled (it will be done via the
2116 * direct helper). But if DIRECT_EN is set, and
2117 * the count is not one, we need to clear it.
2118 */
2119 if (ftrace_rec_count(rec) == 1) {
2120 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2121 !(rec->flags & FTRACE_FL_DIRECT_EN))
2122 flag |= FTRACE_FL_DIRECT;
2123 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2124 flag |= FTRACE_FL_DIRECT;
2125 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002126 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002127
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002128 /* If the state of this record hasn't changed, then do nothing */
2129 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002130 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002131
2132 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002133 /* Save off if rec is being enabled (for return value) */
2134 flag ^= rec->flags & FTRACE_FL_ENABLED;
2135
2136 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002137 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002138 if (flag & FTRACE_FL_REGS) {
2139 if (rec->flags & FTRACE_FL_REGS)
2140 rec->flags |= FTRACE_FL_REGS_EN;
2141 else
2142 rec->flags &= ~FTRACE_FL_REGS_EN;
2143 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002144 if (flag & FTRACE_FL_TRAMP) {
2145 if (rec->flags & FTRACE_FL_TRAMP)
2146 rec->flags |= FTRACE_FL_TRAMP_EN;
2147 else
2148 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2149 }
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04002150
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002151 if (flag & FTRACE_FL_DIRECT) {
2152 /*
2153 * If there's only one user (direct_ops helper)
2154 * then we can call the direct function
2155 * directly (no ftrace trampoline).
2156 */
2157 if (ftrace_rec_count(rec) == 1) {
2158 if (rec->flags & FTRACE_FL_DIRECT)
2159 rec->flags |= FTRACE_FL_DIRECT_EN;
2160 else
2161 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2162 } else {
2163 /*
2164 * Can only call directly if there's
2165 * only one callback to the function.
2166 */
2167 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2168 }
2169 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002170 }
2171
2172 /*
2173 * If this record is being updated from a nop, then
2174 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002175 * Otherwise,
2176 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002177 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002178 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002179 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002180 if (flag & FTRACE_FL_ENABLED) {
2181 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002182 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002183 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002184
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002185 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002186 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002187 }
2188
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002189 if (update) {
2190 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002191 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002192 rec->flags = 0;
2193 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002194 /*
2195 * Just disable the record, but keep the ops TRAMP
2196 * and REGS states. The _EN flags must be disabled though.
2197 */
2198 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002199 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002200 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002201
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002202 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002203 return FTRACE_UPDATE_MAKE_NOP;
2204}
2205
2206/**
Steven Rostedt (VMware)61307222021-10-29 09:52:23 -04002207 * ftrace_update_record - set a record that now is tracing or not
Steven Rostedtc88fd862011-08-16 09:53:39 -04002208 * @rec: the record to update
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002209 * @enable: set to true if the record is tracing, false to force disable
Steven Rostedtc88fd862011-08-16 09:53:39 -04002210 *
2211 * The records that represent all functions that can be traced need
2212 * to be updated when tracing has been enabled.
2213 */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002214int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002215{
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002216 return ftrace_check_record(rec, enable, true);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002217}
2218
2219/**
Steven Rostedt (VMware)61307222021-10-29 09:52:23 -04002220 * ftrace_test_record - check if the record has been enabled or not
Steven Rostedtc88fd862011-08-16 09:53:39 -04002221 * @rec: the record to test
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002222 * @enable: set to true to check if enabled, false if it is disabled
Steven Rostedtc88fd862011-08-16 09:53:39 -04002223 *
2224 * The arch code may need to test if a record is already set to
2225 * tracing to determine how to modify the function code that it
2226 * represents.
2227 */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002228int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002229{
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002230 return ftrace_check_record(rec, enable, false);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002231}
2232
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002233static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002234ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2235{
2236 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002237 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002238
2239 do_for_each_ftrace_op(op, ftrace_ops_list) {
2240
2241 if (!op->trampoline)
2242 continue;
2243
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002244 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002245 return op;
2246 } while_for_each_ftrace_op(op);
2247
2248 return NULL;
2249}
2250
2251static struct ftrace_ops *
Naveen N. Rao4c75b0f2020-11-26 23:38:38 +05302252ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2253{
2254 struct ftrace_ops *op;
2255 unsigned long ip = rec->ip;
2256
2257 do_for_each_ftrace_op(op, ftrace_ops_list) {
2258
2259 if (op == op_exclude || !op->trampoline)
2260 continue;
2261
2262 if (hash_contains_ip(ip, op->func_hash))
2263 return op;
2264 } while_for_each_ftrace_op(op);
2265
2266 return NULL;
2267}
2268
2269static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002270ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2271 struct ftrace_ops *op)
2272{
2273 unsigned long ip = rec->ip;
2274
2275 while_for_each_ftrace_op(op) {
2276
2277 if (!op->trampoline)
2278 continue;
2279
2280 if (hash_contains_ip(ip, op->func_hash))
2281 return op;
Kaitao Cheng026bb842020-05-29 22:12:14 +08002282 }
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002283
2284 return NULL;
2285}
2286
2287static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002288ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2289{
2290 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002291 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002292
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002293 /*
2294 * Need to check removed ops first.
2295 * If they are being removed, and this rec has a tramp,
2296 * and this rec is in the ops list, then it would be the
2297 * one with the tramp.
2298 */
2299 if (removed_ops) {
2300 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002301 return removed_ops;
2302 }
2303
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002304 /*
2305 * Need to find the current trampoline for a rec.
2306 * Now, a trampoline is only attached to a rec if there
2307 * was a single 'ops' attached to it. But this can be called
2308 * when we are adding another op to the rec or removing the
2309 * current one. Thus, if the op is being added, we can
2310 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002311 * yet.
2312 *
2313 * If an ops is being modified (hooking to different functions)
2314 * then we don't care about the new functions that are being
2315 * added, just the old ones (that are probably being removed).
2316 *
2317 * If we are adding an ops to a function that already is using
2318 * a trampoline, it needs to be removed (trampolines are only
2319 * for single ops connected), then an ops that is not being
2320 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002321 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002322 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002323
2324 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002325 continue;
2326
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002327 /*
2328 * If the ops is being added, it hasn't gotten to
2329 * the point to be removed from this tree yet.
2330 */
2331 if (op->flags & FTRACE_OPS_FL_ADDING)
2332 continue;
2333
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002334
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002335 /*
2336 * If the ops is being modified and is in the old
2337 * hash, then it is probably being removed from this
2338 * function.
2339 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002340 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2341 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002342 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002343 /*
2344 * If the ops is not being added or modified, and it's
2345 * in its normal filter hash, then this must be the one
2346 * we want!
2347 */
2348 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2349 hash_contains_ip(ip, op->func_hash))
2350 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002351
2352 } while_for_each_ftrace_op(op);
2353
2354 return NULL;
2355}
2356
2357static struct ftrace_ops *
2358ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2359{
2360 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002361 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002362
2363 do_for_each_ftrace_op(op, ftrace_ops_list) {
2364 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002365 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002366 return op;
2367 } while_for_each_ftrace_op(op);
2368
2369 return NULL;
2370}
2371
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002372#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2373/* Protected by rcu_tasks for reading, and direct_mutex for writing */
2374static struct ftrace_hash *direct_functions = EMPTY_HASH;
2375static DEFINE_MUTEX(direct_mutex);
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05002376int ftrace_direct_func_count;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002377
2378/*
2379 * Search the direct_functions hash to see if the given instruction pointer
2380 * has a direct caller attached to it.
2381 */
Alexei Starovoitovff205762019-12-08 16:01:12 -08002382unsigned long ftrace_find_rec_direct(unsigned long ip)
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002383{
2384 struct ftrace_func_entry *entry;
2385
2386 entry = __ftrace_lookup_ip(direct_functions, ip);
2387 if (!entry)
2388 return 0;
2389
2390 return entry->direct;
2391}
2392
Jiri Olsa1904a812021-10-08 11:13:33 +02002393static struct ftrace_func_entry*
2394ftrace_add_rec_direct(unsigned long ip, unsigned long addr,
2395 struct ftrace_hash **free_hash)
2396{
2397 struct ftrace_func_entry *entry;
2398
2399 if (ftrace_hash_empty(direct_functions) ||
2400 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
2401 struct ftrace_hash *new_hash;
2402 int size = ftrace_hash_empty(direct_functions) ? 0 :
2403 direct_functions->count + 1;
2404
2405 if (size < 32)
2406 size = 32;
2407
2408 new_hash = dup_hash(direct_functions, size);
2409 if (!new_hash)
2410 return NULL;
2411
2412 *free_hash = direct_functions;
2413 direct_functions = new_hash;
2414 }
2415
2416 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2417 if (!entry)
2418 return NULL;
2419
2420 entry->ip = ip;
2421 entry->direct = addr;
2422 __add_hash_entry(direct_functions, entry);
2423 return entry;
2424}
2425
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002426static void call_direct_funcs(unsigned long ip, unsigned long pip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04002427 struct ftrace_ops *ops, struct ftrace_regs *fregs)
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002428{
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04002429 struct pt_regs *regs = ftrace_get_regs(fregs);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002430 unsigned long addr;
2431
Alexei Starovoitovff205762019-12-08 16:01:12 -08002432 addr = ftrace_find_rec_direct(ip);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002433 if (!addr)
2434 return;
2435
2436 arch_ftrace_set_direct_caller(regs, addr);
2437}
2438
2439struct ftrace_ops direct_ops = {
2440 .func = call_direct_funcs,
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05002441 .flags = FTRACE_OPS_FL_IPMODIFY
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002442 | FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2443 | FTRACE_OPS_FL_PERMANENT,
Steven Rostedt (VMware)5da7cd12020-04-22 12:25:41 -04002444 /*
2445 * By declaring the main trampoline as this trampoline
2446 * it will never have one allocated for it. Allocated
2447 * trampolines should not call direct functions.
2448 * The direct_ops should only be called by the builtin
2449 * ftrace_regs_caller trampoline.
2450 */
2451 .trampoline = FTRACE_REGS_ADDR,
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002452};
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002453#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2454
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002455/**
2456 * ftrace_get_addr_new - Get the call address to set to
2457 * @rec: The ftrace record descriptor
2458 *
2459 * If the record has the FTRACE_FL_REGS set, that means that it
2460 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
Randy Dunlap5c8c2062020-08-06 20:32:59 -07002461 * is not set, then it wants to convert to the normal callback.
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002462 *
2463 * Returns the address of the trampoline to set to
2464 */
2465unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2466{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002467 struct ftrace_ops *ops;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002468 unsigned long addr;
2469
2470 if ((rec->flags & FTRACE_FL_DIRECT) &&
2471 (ftrace_rec_count(rec) == 1)) {
Alexei Starovoitovff205762019-12-08 16:01:12 -08002472 addr = ftrace_find_rec_direct(rec->ip);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002473 if (addr)
2474 return addr;
2475 WARN_ON_ONCE(1);
2476 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002477
2478 /* Trampolines take precedence over regs */
2479 if (rec->flags & FTRACE_FL_TRAMP) {
2480 ops = ftrace_find_tramp_ops_new(rec);
2481 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002482 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2483 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002484 /* Ftrace is shutting down, return anything */
2485 return (unsigned long)FTRACE_ADDR;
2486 }
2487 return ops->trampoline;
2488 }
2489
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002490 if (rec->flags & FTRACE_FL_REGS)
2491 return (unsigned long)FTRACE_REGS_ADDR;
2492 else
2493 return (unsigned long)FTRACE_ADDR;
2494}
2495
2496/**
2497 * ftrace_get_addr_curr - Get the call address that is already there
2498 * @rec: The ftrace record descriptor
2499 *
2500 * The FTRACE_FL_REGS_EN is set when the record already points to
2501 * a function that saves all the regs. Basically the '_EN' version
2502 * represents the current state of the function.
2503 *
2504 * Returns the address of the trampoline that is currently being called
2505 */
2506unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2507{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002508 struct ftrace_ops *ops;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002509 unsigned long addr;
2510
2511 /* Direct calls take precedence over trampolines */
2512 if (rec->flags & FTRACE_FL_DIRECT_EN) {
Alexei Starovoitovff205762019-12-08 16:01:12 -08002513 addr = ftrace_find_rec_direct(rec->ip);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002514 if (addr)
2515 return addr;
2516 WARN_ON_ONCE(1);
2517 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002518
2519 /* Trampolines take precedence over regs */
2520 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2521 ops = ftrace_find_tramp_ops_curr(rec);
2522 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002523 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2524 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002525 /* Ftrace is shutting down, return anything */
2526 return (unsigned long)FTRACE_ADDR;
2527 }
2528 return ops->trampoline;
2529 }
2530
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002531 if (rec->flags & FTRACE_FL_REGS_EN)
2532 return (unsigned long)FTRACE_REGS_ADDR;
2533 else
2534 return (unsigned long)FTRACE_ADDR;
2535}
2536
Steven Rostedtc88fd862011-08-16 09:53:39 -04002537static int
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002538__ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002539{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002540 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002541 unsigned long ftrace_addr;
2542 int ret;
2543
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002544 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002545
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002546 /* This needs to be done before we call ftrace_update_record */
2547 ftrace_old_addr = ftrace_get_addr_curr(rec);
2548
2549 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002550
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002551 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2552
Steven Rostedtc88fd862011-08-16 09:53:39 -04002553 switch (ret) {
2554 case FTRACE_UPDATE_IGNORE:
2555 return 0;
2556
2557 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002558 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002559 return ftrace_make_call(rec, ftrace_addr);
2560
2561 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002562 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002563 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002564
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002565 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002566 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002567 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002568 }
2569
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05302570 return -1; /* unknown ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002571}
2572
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002573void __weak ftrace_replace_code(int mod_flags)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002574{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002575 struct dyn_ftrace *rec;
2576 struct ftrace_page *pg;
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002577 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002578 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002579 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002580
Steven Rostedt45a4a232011-04-21 23:16:46 -04002581 if (unlikely(ftrace_disabled))
2582 return;
2583
Steven Rostedt265c8312009-02-13 12:43:56 -05002584 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002585
2586 if (rec->flags & FTRACE_FL_DISABLED)
2587 continue;
2588
Steven Rostedte4f5d542012-04-27 09:13:18 -04002589 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002590 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002591 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002592 /* Stop processing */
2593 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002594 }
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002595 if (schedulable)
2596 cond_resched();
Steven Rostedt265c8312009-02-13 12:43:56 -05002597 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002598}
2599
Steven Rostedtc88fd862011-08-16 09:53:39 -04002600struct ftrace_rec_iter {
2601 struct ftrace_page *pg;
2602 int index;
2603};
2604
2605/**
Steven Rostedt (VMware)61307222021-10-29 09:52:23 -04002606 * ftrace_rec_iter_start - start up iterating over traced functions
Steven Rostedtc88fd862011-08-16 09:53:39 -04002607 *
2608 * Returns an iterator handle that is used to iterate over all
2609 * the records that represent address locations where functions
2610 * are traced.
2611 *
2612 * May return NULL if no records are available.
2613 */
2614struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2615{
2616 /*
2617 * We only use a single iterator.
2618 * Protected by the ftrace_lock mutex.
2619 */
2620 static struct ftrace_rec_iter ftrace_rec_iter;
2621 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2622
2623 iter->pg = ftrace_pages_start;
2624 iter->index = 0;
2625
2626 /* Could have empty pages */
2627 while (iter->pg && !iter->pg->index)
2628 iter->pg = iter->pg->next;
2629
2630 if (!iter->pg)
2631 return NULL;
2632
2633 return iter;
2634}
2635
2636/**
Steven Rostedt (VMware)61307222021-10-29 09:52:23 -04002637 * ftrace_rec_iter_next - get the next record to process.
Steven Rostedtc88fd862011-08-16 09:53:39 -04002638 * @iter: The handle to the iterator.
2639 *
2640 * Returns the next iterator after the given iterator @iter.
2641 */
2642struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2643{
2644 iter->index++;
2645
2646 if (iter->index >= iter->pg->index) {
2647 iter->pg = iter->pg->next;
2648 iter->index = 0;
2649
2650 /* Could have empty pages */
2651 while (iter->pg && !iter->pg->index)
2652 iter->pg = iter->pg->next;
2653 }
2654
2655 if (!iter->pg)
2656 return NULL;
2657
2658 return iter;
2659}
2660
2661/**
Steven Rostedt (VMware)61307222021-10-29 09:52:23 -04002662 * ftrace_rec_iter_record - get the record at the iterator location
Steven Rostedtc88fd862011-08-16 09:53:39 -04002663 * @iter: The current iterator location
2664 *
2665 * Returns the record that the current @iter is at.
2666 */
2667struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2668{
2669 return &iter->pg->records[iter->index];
2670}
2671
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302672static int
Mark Rutlandfbf6c732019-10-16 17:51:10 +01002673ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002674{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002675 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002676
Steven Rostedt45a4a232011-04-21 23:16:46 -04002677 if (unlikely(ftrace_disabled))
2678 return 0;
2679
Mark Rutlandfbf6c732019-10-16 17:51:10 +01002680 ret = ftrace_init_nop(mod, rec);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002681 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002682 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002683 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302684 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002685 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302686 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002687}
2688
Steven Rostedt000ab692009-02-17 13:35:06 -05002689/*
2690 * archs can override this function if they must do something
2691 * before the modifying code is performed.
2692 */
2693int __weak ftrace_arch_code_modify_prepare(void)
2694{
2695 return 0;
2696}
2697
2698/*
2699 * archs can override this function if they must do something
2700 * after the modifying code is performed.
2701 */
2702int __weak ftrace_arch_code_modify_post_process(void)
2703{
2704 return 0;
2705}
2706
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002707void ftrace_modify_all_code(int command)
2708{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002709 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002710 int mod_flags = 0;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002711 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002712
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002713 if (command & FTRACE_MAY_SLEEP)
2714 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2715
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002716 /*
2717 * If the ftrace_caller calls a ftrace_ops func directly,
2718 * we need to make sure that it only traces functions it
2719 * expects to trace. When doing the switch of functions,
2720 * we need to update to the ftrace_ops_list_func first
2721 * before the transition between old and new calls are set,
2722 * as the ftrace_ops_list_func will check the ops hashes
2723 * to make sure the ops are having the right functions
2724 * traced.
2725 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002726 if (update) {
2727 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2728 if (FTRACE_WARN_ON(err))
2729 return;
2730 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002731
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002732 if (command & FTRACE_UPDATE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002733 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002734 else if (command & FTRACE_DISABLE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002735 ftrace_replace_code(mod_flags);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002736
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002737 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2738 function_trace_op = set_function_trace_op;
2739 smp_wmb();
2740 /* If irqs are disabled, we are in stop machine */
2741 if (!irqs_disabled())
2742 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002743 err = ftrace_update_ftrace_func(ftrace_trace_function);
2744 if (FTRACE_WARN_ON(err))
2745 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002746 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002747
2748 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002749 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002750 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002751 err = ftrace_disable_ftrace_graph_caller();
2752 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002753}
2754
Ingo Molnare309b412008-05-12 21:20:51 +02002755static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002756{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002757 int *command = data;
2758
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002759 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002760
Steven Rostedtc88fd862011-08-16 09:53:39 -04002761 return 0;
2762}
2763
2764/**
Steven Rostedt (VMware)61307222021-10-29 09:52:23 -04002765 * ftrace_run_stop_machine - go back to the stop machine method
Steven Rostedtc88fd862011-08-16 09:53:39 -04002766 * @command: The command to tell ftrace what to do
2767 *
2768 * If an arch needs to fall back to the stop machine method, the
2769 * it can call this function.
2770 */
2771void ftrace_run_stop_machine(int command)
2772{
2773 stop_machine(__ftrace_modify_code, &command, NULL);
2774}
2775
2776/**
Steven Rostedt (VMware)61307222021-10-29 09:52:23 -04002777 * arch_ftrace_update_code - modify the code to trace or not trace
Steven Rostedtc88fd862011-08-16 09:53:39 -04002778 * @command: The command that needs to be done
2779 *
2780 * Archs can override this function if it does not need to
2781 * run stop_machine() to modify code.
2782 */
2783void __weak arch_ftrace_update_code(int command)
2784{
2785 ftrace_run_stop_machine(command);
2786}
2787
2788static void ftrace_run_update_code(int command)
2789{
2790 int ret;
2791
2792 ret = ftrace_arch_code_modify_prepare();
2793 FTRACE_WARN_ON(ret);
2794 if (ret)
Petr Mladekd5b844a2019-06-27 10:13:34 +02002795 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002796
2797 /*
2798 * By default we use stop_machine() to modify the code.
2799 * But archs can do what ever they want as long as it
2800 * is safe. The stop_machine() is the safest, but also
2801 * produces the most overhead.
2802 */
2803 arch_ftrace_update_code(command);
2804
Steven Rostedt000ab692009-02-17 13:35:06 -05002805 ret = ftrace_arch_code_modify_post_process();
2806 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002807}
2808
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002809static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002810 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002811{
2812 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002813 ops->old_hash.filter_hash = old_hash->filter_hash;
2814 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002815 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002816 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002817 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002818 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2819}
2820
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002821static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002822static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002823
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002824void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2825{
2826}
2827
Adrian Hunterfc0ea792020-05-12 15:19:13 +03002828/* List of trace_ops that have allocated trampolines */
2829static LIST_HEAD(ftrace_ops_trampoline_list);
2830
2831static void ftrace_add_trampoline_to_kallsyms(struct ftrace_ops *ops)
2832{
2833 lockdep_assert_held(&ftrace_lock);
2834 list_add_rcu(&ops->list, &ftrace_ops_trampoline_list);
2835}
2836
2837static void ftrace_remove_trampoline_from_kallsyms(struct ftrace_ops *ops)
2838{
2839 lockdep_assert_held(&ftrace_lock);
2840 list_del_rcu(&ops->list);
Adrian Hunter478ece92020-09-01 12:16:17 +03002841 synchronize_rcu();
Adrian Hunterfc0ea792020-05-12 15:19:13 +03002842}
2843
2844/*
2845 * "__builtin__ftrace" is used as a module name in /proc/kallsyms for symbols
2846 * for pages allocated for ftrace purposes, even though "__builtin__ftrace" is
2847 * not a module.
2848 */
2849#define FTRACE_TRAMPOLINE_MOD "__builtin__ftrace"
2850#define FTRACE_TRAMPOLINE_SYM "ftrace_trampoline"
2851
2852static void ftrace_trampoline_free(struct ftrace_ops *ops)
2853{
2854 if (ops && (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP) &&
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03002855 ops->trampoline) {
Adrian Hunter548e1f62020-05-12 15:19:15 +03002856 /*
2857 * Record the text poke event before the ksymbol unregister
2858 * event.
2859 */
2860 perf_event_text_poke((void *)ops->trampoline,
2861 (void *)ops->trampoline,
2862 ops->trampoline_size, NULL, 0);
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03002863 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
2864 ops->trampoline, ops->trampoline_size,
2865 true, FTRACE_TRAMPOLINE_SYM);
2866 /* Remove from kallsyms after the perf events */
Adrian Hunterfc0ea792020-05-12 15:19:13 +03002867 ftrace_remove_trampoline_from_kallsyms(ops);
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03002868 }
Adrian Hunterfc0ea792020-05-12 15:19:13 +03002869
2870 arch_ftrace_trampoline_free(ops);
2871}
2872
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002873static void ftrace_startup_enable(int command)
2874{
2875 if (saved_ftrace_func != ftrace_trace_function) {
2876 saved_ftrace_func = ftrace_trace_function;
2877 command |= FTRACE_UPDATE_TRACE_FUNC;
2878 }
2879
2880 if (!command || !ftrace_enabled)
2881 return;
2882
2883 ftrace_run_update_code(command);
2884}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002885
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002886static void ftrace_startup_all(int command)
2887{
2888 update_all_ops = true;
2889 ftrace_startup_enable(command);
2890 update_all_ops = false;
2891}
2892
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002893int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002894{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002895 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002896
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002897 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002898 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002899
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002900 ret = __register_ftrace_function(ops);
2901 if (ret)
2902 return ret;
2903
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002904 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002905
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002906 /*
2907 * Note that ftrace probes uses this to start up
2908 * and modify functions it will probe. But we still
2909 * set the ADDING flag for modification, as probes
2910 * do not have trampolines. If they add them in the
2911 * future, then the probes will need to distinguish
2912 * between adding and updating probes.
2913 */
2914 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002915
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002916 ret = ftrace_hash_ipmodify_enable(ops);
2917 if (ret < 0) {
2918 /* Rollback registration process */
2919 __unregister_ftrace_function(ops);
2920 ftrace_start_up--;
2921 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Miroslav Benesd5e47502020-08-31 14:26:31 +02002922 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2923 ftrace_trampoline_free(ops);
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002924 return ret;
2925 }
2926
Jiri Olsa7f50d062016-03-16 15:34:33 +01002927 if (ftrace_hash_rec_enable(ops, 1))
2928 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002929
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002930 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002931
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002932 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2933
Steven Rostedta1cd6172011-05-23 15:24:25 -04002934 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002935}
2936
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002937int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002938{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002939 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002940
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002941 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002942 return -ENODEV;
2943
2944 ret = __unregister_ftrace_function(ops);
2945 if (ret)
2946 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002947
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002948 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002949 /*
2950 * Just warn in case of unbalance, no need to kill ftrace, it's not
2951 * critical but the ftrace_call callers may be never nopped again after
2952 * further ftrace uses.
2953 */
2954 WARN_ON_ONCE(ftrace_start_up < 0);
2955
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002956 /* Disabling ipmodify never fails */
2957 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002958
2959 if (ftrace_hash_rec_disable(ops, 1))
2960 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002961
Namhyung Kima737e6d2014-06-12 23:56:12 +09002962 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002963
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002964 if (saved_ftrace_func != ftrace_trace_function) {
2965 saved_ftrace_func = ftrace_trace_function;
2966 command |= FTRACE_UPDATE_TRACE_FUNC;
2967 }
2968
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002969 if (!command || !ftrace_enabled) {
2970 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002971 * If these are dynamic or per_cpu ops, they still
2972 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002973 * not currently active, we can just free them
2974 * without synchronizing all CPUs.
2975 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002976 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002977 goto free_ops;
2978
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002979 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002980 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002981
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002982 /*
2983 * If the ops uses a trampoline, then it needs to be
2984 * tested first on update.
2985 */
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 removed_ops = ops;
2988
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002989 /* The trampoline logic checks the old hashes */
2990 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2991 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2992
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002993 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002994
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002995 /*
2996 * If there's no more ops registered with ftrace, run a
2997 * sanity check to make sure all rec flags are cleared.
2998 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002999 if (rcu_dereference_protected(ftrace_ops_list,
3000 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04003001 struct ftrace_page *pg;
3002 struct dyn_ftrace *rec;
3003
3004 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08003005 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04003006 pr_warn(" %pS flags:%lx\n",
3007 (void *)rec->ip, rec->flags);
3008 } while_for_each_ftrace_rec();
3009 }
3010
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04003011 ops->old_hash.filter_hash = NULL;
3012 ops->old_hash.notrace_hash = NULL;
3013
3014 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04003015 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04003016
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05003017 /*
3018 * Dynamic ops may be freed, we must make sure that all
3019 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05003020 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05003021 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05003022 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02003023 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04003024 /*
3025 * We need to do a hard force of sched synchronization.
3026 * This is because we use preempt_disable() to do RCU, but
3027 * the function tracers can be called where RCU is not watching
3028 * (like before user_exit()). We can not rely on the RCU
3029 * infrastructure to do the synchronization, thus we must do it
3030 * ourselves.
3031 */
Paul E. McKenneye5a971d2020-04-03 12:10:28 -07003032 synchronize_rcu_tasks_rude();
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05003033
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04003034 /*
Qiujun Huangfdda88d2020-10-02 22:31:26 +08003035 * When the kernel is preemptive, tasks can be preempted
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04003036 * while on a ftrace trampoline. Just scheduling a task on
3037 * a CPU is not good enough to flush them. Calling
Ingo Molnarf2cc0202021-03-23 18:49:35 +01003038 * synchronize_rcu_tasks() will wait for those tasks to
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04003039 * execute and either schedule voluntarily or enter user space.
3040 */
Thomas Gleixner30c93702019-07-26 23:19:40 +02003041 if (IS_ENABLED(CONFIG_PREEMPTION))
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04003042 synchronize_rcu_tasks();
3043
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04003044 free_ops:
Adrian Hunterfc0ea792020-05-12 15:19:13 +03003045 ftrace_trampoline_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05003046 }
3047
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003048 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02003049}
3050
Ingo Molnare309b412008-05-12 21:20:51 +02003051static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003052{
Pratyush Anand1619dc32015-03-06 23:58:06 +05303053 int command;
3054
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003055 if (unlikely(ftrace_disabled))
3056 return;
3057
Steven Rostedtd61f82d2008-05-12 21:20:43 +02003058 /* Force update next time */
3059 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05003060 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05303061 if (ftrace_start_up) {
3062 command = FTRACE_UPDATE_CALLS;
3063 if (ftrace_graph_active)
3064 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05003065 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05303066 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003067}
3068
Ingo Molnare309b412008-05-12 21:20:51 +02003069static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003070{
Pratyush Anand1619dc32015-03-06 23:58:06 +05303071 int command;
3072
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003073 if (unlikely(ftrace_disabled))
3074 return;
3075
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05003076 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05303077 if (ftrace_start_up) {
3078 command = FTRACE_DISABLE_CALLS;
3079 if (ftrace_graph_active)
3080 command |= FTRACE_STOP_FUNC_RET;
3081 ftrace_run_update_code(command);
3082 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02003083}
3084
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01003085static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02003086unsigned long ftrace_update_tot_cnt;
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04003087unsigned long ftrace_number_of_pages;
3088unsigned long ftrace_number_of_groups;
Steven Rostedt3d083392008-05-12 21:20:42 +02003089
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003090static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04003091{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003092 /*
3093 * Filter_hash being empty will default to trace module.
3094 * But notrace hash requires a test of individual module functions.
3095 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003096 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
3097 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003098}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04003099
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003100/*
3101 * Check if the current ops references the record.
3102 *
3103 * If the ops traces all functions, then it was already accounted for.
3104 * If the ops does not trace the current record function, skip it.
3105 * If the ops ignores the function via notrace filter, skip it.
3106 */
3107static inline bool
3108ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3109{
3110 /* If ops isn't enabled, ignore it */
3111 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003112 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003113
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003114 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003115 if (ops_traces_mod(ops))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003116 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003117
3118 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003119 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05003120 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003121 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003122
3123 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003124 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003125 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003126
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003127 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003128}
3129
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003130static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02003131{
Ilya Leoshkevich67ccddf2021-07-28 23:25:45 +02003132 bool init_nop = ftrace_need_init_nop();
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003133 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08003134 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01003135 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003136 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003137 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003138 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04003139
Ingo Molnar750ed1a2008-05-12 21:20:46 +02003140 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02003141
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003142 /*
3143 * When a module is loaded, this function is called to convert
3144 * the calls to mcount in its text to nops, and also to create
3145 * an entry in the ftrace data. Now, if ftrace is activated
3146 * after this call, but before the module sets its text to
3147 * read-only, the modification of enabling ftrace can fail if
3148 * the read-only is done while ftrace is converting the calls.
3149 * To prevent this, the module's records are set as disabled
3150 * and will be enabled after the call to set the module's text
3151 * to read-only.
3152 */
3153 if (mod)
3154 rec_flags |= FTRACE_FL_DISABLED;
3155
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003156 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05303157
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003158 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003159
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003160 /* If something went wrong, bail without enabling anything */
3161 if (unlikely(ftrace_disabled))
3162 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02003163
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003164 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003165 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05303166
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003167 /*
3168 * Do the initial record conversion from mcount jump
3169 * to the NOP instructions.
3170 */
Ilya Leoshkevich67ccddf2021-07-28 23:25:45 +02003171 if (init_nop && !ftrace_nop_initialize(mod, p))
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003172 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003173
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003174 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003175 }
Steven Rostedt3d083392008-05-12 21:20:42 +02003176 }
3177
Ingo Molnar750ed1a2008-05-12 21:20:46 +02003178 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02003179 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003180 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02003181
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003182 return 0;
3183}
3184
Steven Rostedta7900872011-12-16 16:23:44 -05003185static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003186{
Steven Rostedta7900872011-12-16 16:23:44 -05003187 int order;
Steven Rostedt (VMware)7ba031e2020-10-05 20:37:41 -04003188 int pages;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003189 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003190
Steven Rostedta7900872011-12-16 16:23:44 -05003191 if (WARN_ON(!count))
3192 return -EINVAL;
3193
Steven Rostedt (VMware)ceaaa122021-04-01 16:40:32 -04003194 /* We want to fill as much as possible, with no empty pages */
Wei Yangb40c6ea2020-08-31 11:11:02 +08003195 pages = DIV_ROUND_UP(count, ENTRIES_PER_PAGE);
Steven Rostedt (VMware)ceaaa122021-04-01 16:40:32 -04003196 order = fls(pages) - 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003197
Steven Rostedta7900872011-12-16 16:23:44 -05003198 again:
3199 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3200
3201 if (!pg->records) {
3202 /* if we can't allocate this size, try something smaller */
3203 if (!order)
3204 return -ENOMEM;
3205 order >>= 1;
3206 goto again;
3207 }
3208
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04003209 ftrace_number_of_pages += 1 << order;
3210 ftrace_number_of_groups++;
3211
Steven Rostedta7900872011-12-16 16:23:44 -05003212 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
Linus Torvaldsdb425232021-04-01 16:14:17 -04003213 pg->order = order;
Steven Rostedta7900872011-12-16 16:23:44 -05003214
3215 if (cnt > count)
3216 cnt = count;
3217
3218 return cnt;
3219}
3220
3221static struct ftrace_page *
3222ftrace_allocate_pages(unsigned long num_to_init)
3223{
3224 struct ftrace_page *start_pg;
3225 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003226 int cnt;
3227
3228 if (!num_to_init)
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05303229 return NULL;
Steven Rostedta7900872011-12-16 16:23:44 -05003230
3231 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3232 if (!pg)
3233 return NULL;
3234
3235 /*
3236 * Try to allocate as much as possible in one continues
3237 * location that fills in all of the space. We want to
3238 * waste as little space as possible.
3239 */
3240 for (;;) {
3241 cnt = ftrace_allocate_records(pg, num_to_init);
3242 if (cnt < 0)
3243 goto free_pages;
3244
3245 num_to_init -= cnt;
3246 if (!num_to_init)
3247 break;
3248
3249 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3250 if (!pg->next)
3251 goto free_pages;
3252
3253 pg = pg->next;
3254 }
3255
3256 return start_pg;
3257
3258 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003259 pg = start_pg;
3260 while (pg) {
Linus Torvaldsdb425232021-04-01 16:14:17 -04003261 if (pg->records) {
3262 free_pages((unsigned long)pg->records, pg->order);
3263 ftrace_number_of_pages -= 1 << pg->order;
3264 }
Steven Rostedta7900872011-12-16 16:23:44 -05003265 start_pg = pg->next;
3266 kfree(pg);
3267 pg = start_pg;
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04003268 ftrace_number_of_groups--;
Steven Rostedta7900872011-12-16 16:23:44 -05003269 }
3270 pr_info("ftrace: FAILED to allocate memory for functions\n");
3271 return NULL;
3272}
3273
Steven Rostedt5072c592008-05-12 21:20:43 +02003274#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3275
3276struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003277 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003278 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003279 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003280 struct ftrace_page *pg;
3281 struct dyn_ftrace *func;
3282 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003283 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003284 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003285 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003286 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003287 struct trace_array *tr;
3288 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003289 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003290 int idx;
3291 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003292};
3293
Ingo Molnare309b412008-05-12 21:20:51 +02003294static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003295t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003296{
3297 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003298 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003299 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003300 struct ftrace_hash *hash;
3301 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003302 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003303 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003304 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003305
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003306 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003307 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003308
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003309 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003310 return NULL;
3311
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003312 func_probes = &tr->func_probes;
3313 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003314 return NULL;
3315
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003316 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003317 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003318 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003319 }
3320
3321 if (iter->probe_entry)
3322 hnd = &iter->probe_entry->hlist;
3323
3324 hash = iter->probe->ops.func_hash->filter_hash;
Naveen N. Rao7bd46642019-07-04 20:04:41 +05303325
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04003326 /*
3327 * A probe being registered may temporarily have an empty hash
3328 * and it's at the end of the func_probes list.
3329 */
3330 if (!hash || hash == EMPTY_HASH)
Naveen N. Rao7bd46642019-07-04 20:04:41 +05303331 return NULL;
3332
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003333 size = 1 << hash->size_bits;
3334
3335 retry:
3336 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003337 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003338 return NULL;
3339 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003340 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003341 hash = iter->probe->ops.func_hash->filter_hash;
3342 size = 1 << hash->size_bits;
3343 iter->pidx = 0;
3344 }
3345
3346 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003347
3348 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003349 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003350 hnd = NULL;
3351 goto retry;
3352 }
3353
3354 if (!hnd)
3355 hnd = hhd->first;
3356 else {
3357 hnd = hnd->next;
3358 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003359 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003360 goto retry;
3361 }
3362 }
3363
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003364 if (WARN_ON_ONCE(!hnd))
3365 return NULL;
3366
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003367 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003368
3369 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003370}
3371
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003372static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003373{
3374 struct ftrace_iterator *iter = m->private;
3375 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003376 loff_t l;
3377
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003378 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003379 return NULL;
3380
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003381 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003382 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003383
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003384 iter->probe = NULL;
3385 iter->probe_entry = NULL;
3386 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003387 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003388 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003389 if (!p)
3390 break;
3391 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003392 if (!p)
3393 return NULL;
3394
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003395 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003396 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003397
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003398 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003399}
3400
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003401static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003402t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003403{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003404 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003405 struct ftrace_probe_ops *probe_ops;
3406 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003407
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003408 probe = iter->probe;
3409 probe_entry = iter->probe_entry;
3410
3411 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003412 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003413
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003414 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003415
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003416 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003417 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003418
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003419 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3420 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003421
3422 return 0;
3423}
3424
3425static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003426t_mod_next(struct seq_file *m, loff_t *pos)
3427{
3428 struct ftrace_iterator *iter = m->private;
3429 struct trace_array *tr = iter->tr;
3430
3431 (*pos)++;
3432 iter->pos = *pos;
3433
3434 iter->mod_list = iter->mod_list->next;
3435
3436 if (iter->mod_list == &tr->mod_trace ||
3437 iter->mod_list == &tr->mod_notrace) {
3438 iter->flags &= ~FTRACE_ITER_MOD;
3439 return NULL;
3440 }
3441
3442 iter->mod_pos = *pos;
3443
3444 return iter;
3445}
3446
3447static void *t_mod_start(struct seq_file *m, loff_t *pos)
3448{
3449 struct ftrace_iterator *iter = m->private;
3450 void *p = NULL;
3451 loff_t l;
3452
3453 if (iter->func_pos > *pos)
3454 return NULL;
3455
3456 iter->mod_pos = iter->func_pos;
3457
3458 /* probes are only available if tr is set */
3459 if (!iter->tr)
3460 return NULL;
3461
3462 for (l = 0; l <= (*pos - iter->func_pos); ) {
3463 p = t_mod_next(m, &l);
3464 if (!p)
3465 break;
3466 }
3467 if (!p) {
3468 iter->flags &= ~FTRACE_ITER_MOD;
3469 return t_probe_start(m, pos);
3470 }
3471
3472 /* Only set this if we have an item */
3473 iter->flags |= FTRACE_ITER_MOD;
3474
3475 return iter;
3476}
3477
3478static int
3479t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3480{
3481 struct ftrace_mod_load *ftrace_mod;
3482 struct trace_array *tr = iter->tr;
3483
3484 if (WARN_ON_ONCE(!iter->mod_list) ||
3485 iter->mod_list == &tr->mod_trace ||
3486 iter->mod_list == &tr->mod_notrace)
3487 return -EIO;
3488
3489 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3490
3491 if (ftrace_mod->func)
3492 seq_printf(m, "%s", ftrace_mod->func);
3493 else
3494 seq_putc(m, '*');
3495
3496 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3497
3498 return 0;
3499}
3500
3501static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003502t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003503{
3504 struct ftrace_iterator *iter = m->private;
3505 struct dyn_ftrace *rec = NULL;
3506
3507 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003508
Steven Rostedt5072c592008-05-12 21:20:43 +02003509 retry:
3510 if (iter->idx >= iter->pg->index) {
3511 if (iter->pg->next) {
3512 iter->pg = iter->pg->next;
3513 iter->idx = 0;
3514 goto retry;
3515 }
3516 } else {
3517 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003518 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3519 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003520
3521 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003522 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003523
Steven Rostedt5072c592008-05-12 21:20:43 +02003524 rec = NULL;
3525 goto retry;
3526 }
3527 }
3528
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003529 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003530 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003531
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003532 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003533 iter->func = rec;
3534
3535 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003536}
3537
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003538static void *
3539t_next(struct seq_file *m, void *v, loff_t *pos)
3540{
3541 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003542 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003543 void *ret;
3544
3545 if (unlikely(ftrace_disabled))
3546 return NULL;
3547
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003548 if (iter->flags & FTRACE_ITER_PROBE)
3549 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003550
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003551 if (iter->flags & FTRACE_ITER_MOD)
3552 return t_mod_next(m, pos);
3553
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003554 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003555 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003556 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003557 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003558 }
3559
3560 ret = t_func_next(m, pos);
3561
3562 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003563 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003564
3565 return ret;
3566}
3567
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003568static void reset_iter_read(struct ftrace_iterator *iter)
3569{
3570 iter->pos = 0;
3571 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003572 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003573}
3574
3575static void *t_start(struct seq_file *m, loff_t *pos)
3576{
3577 struct ftrace_iterator *iter = m->private;
3578 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003579 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003580
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003581 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003582
3583 if (unlikely(ftrace_disabled))
3584 return NULL;
3585
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003586 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003587 * If an lseek was done, then reset and start from beginning.
3588 */
3589 if (*pos < iter->pos)
3590 reset_iter_read(iter);
3591
3592 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003593 * For set_ftrace_filter reading, if we have the filter
3594 * off, we can short cut and just print out that all
3595 * functions are enabled.
3596 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003597 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3598 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003599 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003600 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003601 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003602 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003603 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003604 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003605 return iter;
3606 }
3607
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003608 if (iter->flags & FTRACE_ITER_MOD)
3609 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003610
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003611 /*
3612 * Unfortunately, we need to restart at ftrace_pages_start
3613 * every time we let go of the ftrace_mutex. This is because
3614 * those pointers can change without the lock.
3615 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003616 iter->pg = ftrace_pages_start;
3617 iter->idx = 0;
3618 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003619 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003620 if (!p)
3621 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003622 }
walimis5821e1b2008-11-15 15:19:06 +08003623
Steven Rostedt69a30832011-12-19 15:21:16 -05003624 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003625 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003626
3627 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003628}
3629
3630static void t_stop(struct seq_file *m, void *p)
3631{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003632 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003633}
3634
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003635void * __weak
3636arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3637{
3638 return NULL;
3639}
3640
3641static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3642 struct dyn_ftrace *rec)
3643{
3644 void *ptr;
3645
3646 ptr = arch_ftrace_trampoline_func(ops, rec);
3647 if (ptr)
3648 seq_printf(m, " ->%pS", ptr);
3649}
3650
Steven Rostedt5072c592008-05-12 21:20:43 +02003651static int t_show(struct seq_file *m, void *v)
3652{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003653 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003654 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003655
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003656 if (iter->flags & FTRACE_ITER_PROBE)
3657 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003658
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003659 if (iter->flags & FTRACE_ITER_MOD)
3660 return t_mod_show(m, iter);
3661
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003662 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003663 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003664 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003665 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003666 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003667 return 0;
3668 }
3669
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003670 rec = iter->func;
3671
Steven Rostedt5072c592008-05-12 21:20:43 +02003672 if (!rec)
3673 return 0;
3674
Steven Rostedt647bcd02011-05-03 14:39:21 -04003675 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003676 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003677 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003678
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05003679 seq_printf(m, " (%ld)%s%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003680 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003681 rec->flags & FTRACE_FL_REGS ? " R" : " ",
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05003682 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3683 rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003684 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003685 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003686 if (ops) {
3687 do {
3688 seq_printf(m, "\ttramp: %pS (%pS)",
3689 (void *)ops->trampoline,
3690 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003691 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003692 ops = ftrace_find_tramp_ops_next(rec, ops);
3693 } while (ops);
3694 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003695 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003696 } else {
3697 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003698 }
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05003699 if (rec->flags & FTRACE_FL_DIRECT) {
3700 unsigned long direct;
3701
Alexei Starovoitovff205762019-12-08 16:01:12 -08003702 direct = ftrace_find_rec_direct(rec->ip);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05003703 if (direct)
3704 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3705 }
Kaitao Cheng026bb842020-05-29 22:12:14 +08003706 }
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003707
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003708 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003709
3710 return 0;
3711}
3712
James Morris88e9d342009-09-22 16:43:43 -07003713static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003714 .start = t_start,
3715 .next = t_next,
3716 .stop = t_stop,
3717 .show = t_show,
3718};
3719
Ingo Molnare309b412008-05-12 21:20:51 +02003720static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003721ftrace_avail_open(struct inode *inode, struct file *file)
3722{
3723 struct ftrace_iterator *iter;
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003724 int ret;
3725
3726 ret = security_locked_down(LOCKDOWN_TRACEFS);
3727 if (ret)
3728 return ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003729
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003730 if (unlikely(ftrace_disabled))
3731 return -ENODEV;
3732
Jiri Olsa50e18b92012-04-25 10:23:39 +02003733 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003734 if (!iter)
3735 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003736
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003737 iter->pg = ftrace_pages_start;
3738 iter->ops = &global_ops;
3739
3740 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003741}
3742
Steven Rostedt647bcd02011-05-03 14:39:21 -04003743static int
3744ftrace_enabled_open(struct inode *inode, struct file *file)
3745{
3746 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003747
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003748 /*
3749 * This shows us what functions are currently being
3750 * traced and by what. Not sure if we want lockdown
3751 * to hide such critical information for an admin.
3752 * Although, perhaps it can show information we don't
3753 * want people to see, but if something is tracing
3754 * something, we probably want to know about it.
3755 */
3756
Jiri Olsa50e18b92012-04-25 10:23:39 +02003757 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003758 if (!iter)
3759 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003760
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003761 iter->pg = ftrace_pages_start;
3762 iter->flags = FTRACE_ITER_ENABLED;
3763 iter->ops = &global_ops;
3764
3765 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003766}
3767
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003768/**
3769 * ftrace_regex_open - initialize function tracer filter files
3770 * @ops: The ftrace_ops that hold the hash filters
3771 * @flag: The type of filter to process
3772 * @inode: The inode, usually passed in to your open routine
3773 * @file: The file, usually passed in to your open routine
3774 *
3775 * ftrace_regex_open() initializes the filter files for the
3776 * @ops. Depending on @flag it may process the filter hash or
3777 * the notrace hash of @ops. With this called from the open
3778 * routine, you can use ftrace_filter_write() for the write
3779 * routine if @flag has FTRACE_ITER_FILTER set, or
3780 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003781 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003782 * release must call ftrace_regex_release().
3783 */
3784int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003785ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003786 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003787{
3788 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003789 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003790 struct list_head *mod_head;
3791 struct trace_array *tr = ops->private;
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003792 int ret = -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003793
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003794 ftrace_ops_init(ops);
3795
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003796 if (unlikely(ftrace_disabled))
3797 return -ENODEV;
3798
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04003799 if (tracing_check_open_get_tr(tr))
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003800 return -ENODEV;
3801
Steven Rostedt5072c592008-05-12 21:20:43 +02003802 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3803 if (!iter)
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003804 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003805
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003806 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3807 goto out;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003808
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003809 iter->ops = ops;
3810 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003811 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003812
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003813 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003814
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003815 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003816 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003817 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003818 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003819 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003820 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003821 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003822
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003823 iter->mod_list = mod_head;
3824
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003825 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003826 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3827
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003828 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003829 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003830 clear_ftrace_mod_list(mod_head);
3831 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003832 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003833 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003834
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003835 if (!iter->hash) {
3836 trace_parser_put(&iter->parser);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003837 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003838 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003839 } else
3840 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003841
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003842 ret = 0;
3843
Steven Rostedt5072c592008-05-12 21:20:43 +02003844 if (file->f_mode & FMODE_READ) {
3845 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003846
3847 ret = seq_open(file, &show_ftrace_seq_ops);
3848 if (!ret) {
3849 struct seq_file *m = file->private_data;
3850 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003851 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003852 /* Failed */
3853 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003854 trace_parser_put(&iter->parser);
Li Zefan79fe2492009-09-22 13:54:28 +08003855 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003856 } else
3857 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003858
3859 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003860 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003861
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003862 out:
3863 if (ret) {
3864 kfree(iter);
3865 if (tr)
3866 trace_array_put(tr);
3867 }
3868
Steven Rostedt5072c592008-05-12 21:20:43 +02003869 return ret;
3870}
3871
Steven Rostedt41c52c02008-05-22 11:46:33 -04003872static int
3873ftrace_filter_open(struct inode *inode, struct file *file)
3874{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003875 struct ftrace_ops *ops = inode->i_private;
3876
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003877 /* Checks for tracefs lockdown */
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003878 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003879 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003880 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003881}
3882
3883static int
3884ftrace_notrace_open(struct inode *inode, struct file *file)
3885{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003886 struct ftrace_ops *ops = inode->i_private;
3887
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003888 /* Checks for tracefs lockdown */
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003889 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003890 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003891}
3892
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003893/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3894struct ftrace_glob {
3895 char *search;
3896 unsigned len;
3897 int type;
3898};
3899
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003900/*
3901 * If symbols in an architecture don't correspond exactly to the user-visible
3902 * name of what they represent, it is possible to define this function to
3903 * perform the necessary adjustments.
3904*/
3905char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3906{
3907 return str;
3908}
3909
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003910static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003911{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003912 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003913 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003914
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003915 str = arch_ftrace_match_adjust(str, g->search);
3916
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003917 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003918 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003919 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003920 matched = 1;
3921 break;
3922 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003923 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003924 matched = 1;
3925 break;
3926 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003927 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003928 matched = 1;
3929 break;
3930 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003931 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003932 if (slen >= g->len &&
3933 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003934 matched = 1;
3935 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003936 case MATCH_GLOB:
3937 if (glob_match(g->search, str))
3938 matched = 1;
3939 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003940 }
3941
3942 return matched;
3943}
3944
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003945static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003946enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003947{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003948 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003949 int ret = 0;
3950
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003951 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003952 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003953 /* Do nothing if it doesn't exist */
3954 if (!entry)
3955 return 0;
3956
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003957 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003958 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003959 /* Do nothing if it exists */
3960 if (entry)
3961 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003962
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003963 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003964 }
3965 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003966}
3967
Steven Rostedt64e7c442009-02-13 17:08:48 -05003968static int
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003969add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3970 int clear_filter)
3971{
3972 long index = simple_strtoul(func_g->search, NULL, 0);
3973 struct ftrace_page *pg;
3974 struct dyn_ftrace *rec;
3975
3976 /* The index starts at 1 */
3977 if (--index < 0)
3978 return 0;
3979
3980 do_for_each_ftrace_rec(pg, rec) {
3981 if (pg->index <= index) {
3982 index -= pg->index;
3983 /* this is a double loop, break goes to the next page */
3984 break;
3985 }
3986 rec = &pg->records[index];
3987 enter_record(hash, rec, clear_filter);
3988 return 1;
3989 } while_for_each_ftrace_rec();
3990 return 0;
3991}
3992
3993static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003994ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3995 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003996{
3997 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003998 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003999
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004000 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
4001
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004002 if (mod_g) {
4003 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
4004
4005 /* blank module name to match all modules */
4006 if (!mod_g->len) {
4007 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04004008 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004009 goto func_match;
4010 return 0;
4011 }
4012
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04004013 /*
4014 * exclude_mod is set to trace everything but the given
4015 * module. If it is set and the module matches, then
4016 * return 0. If it is not set, and the module doesn't match
4017 * also return 0. Otherwise, check the function to see if
4018 * that matches.
4019 */
4020 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004021 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004022func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004023 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004024 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004025 return 1;
4026 }
4027
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004028 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004029}
4030
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004031static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004032match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004033{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004034 struct ftrace_page *pg;
4035 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004036 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004037 struct ftrace_glob mod_g = { .type = MATCH_FULL };
4038 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
4039 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08004040 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004041 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03004042 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004043
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004044 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004045 func_g.type = filter_parse_regex(func, len, &func_g.search,
4046 &clear_filter);
4047 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004048 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004049
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004050 if (mod) {
4051 mod_g.type = filter_parse_regex(mod, strlen(mod),
4052 &mod_g.search, &exclude_mod);
4053 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05004054 }
4055
Steven Rostedt52baf112009-02-14 01:15:39 -05004056 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004057
4058 if (unlikely(ftrace_disabled))
4059 goto out_unlock;
4060
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05004061 if (func_g.type == MATCH_INDEX) {
4062 found = add_rec_by_index(hash, &func_g, clear_filter);
4063 goto out_unlock;
4064 }
4065
Steven Rostedt265c8312009-02-13 12:43:56 -05004066 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004067
4068 if (rec->flags & FTRACE_FL_DISABLED)
4069 continue;
4070
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004071 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004072 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004073 if (ret < 0) {
4074 found = ret;
4075 goto out_unlock;
4076 }
Li Zefan311d16d2009-12-08 11:15:11 +08004077 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05004078 }
4079 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004080 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05004081 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08004082
4083 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02004084}
4085
Steven Rostedt64e7c442009-02-13 17:08:48 -05004086static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004087ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004088{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004089 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004090}
4091
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004092static void ftrace_ops_update_code(struct ftrace_ops *ops,
4093 struct ftrace_ops_hash *old_hash)
4094{
4095 struct ftrace_ops *op;
4096
4097 if (!ftrace_enabled)
4098 return;
4099
4100 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4101 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4102 return;
4103 }
4104
4105 /*
4106 * If this is the shared global_ops filter, then we need to
4107 * check if there is another ops that shares it, is enabled.
4108 * If so, we still need to run the modify code.
4109 */
4110 if (ops->func_hash != &global_ops.local_hash)
4111 return;
4112
4113 do_for_each_ftrace_op(op, ftrace_ops_list) {
4114 if (op->func_hash == &global_ops.local_hash &&
4115 op->flags & FTRACE_OPS_FL_ENABLED) {
4116 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4117 /* Only need to do this once */
4118 return;
4119 }
4120 } while_for_each_ftrace_op(op);
4121}
4122
4123static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4124 struct ftrace_hash **orig_hash,
4125 struct ftrace_hash *hash,
4126 int enable)
4127{
4128 struct ftrace_ops_hash old_hash_ops;
4129 struct ftrace_hash *old_hash;
4130 int ret;
4131
4132 old_hash = *orig_hash;
4133 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4134 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4135 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4136 if (!ret) {
4137 ftrace_ops_update_code(ops, &old_hash_ops);
4138 free_ftrace_hash_rcu(old_hash);
4139 }
4140 return ret;
4141}
Steven Rostedt64e7c442009-02-13 17:08:48 -05004142
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004143static bool module_exists(const char *module)
4144{
4145 /* All modules have the symbol __this_module */
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01004146 static const char this_mod[] = "__this_module";
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02004147 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004148 unsigned long val;
4149 int n;
4150
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02004151 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004152
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02004153 if (n > sizeof(modname) - 1)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004154 return false;
4155
4156 val = module_kallsyms_lookup_name(modname);
4157 return val != 0;
4158}
4159
4160static int cache_mod(struct trace_array *tr,
4161 const char *func, char *module, int enable)
4162{
4163 struct ftrace_mod_load *ftrace_mod, *n;
4164 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4165 int ret;
4166
4167 mutex_lock(&ftrace_lock);
4168
4169 /* We do not cache inverse filters */
4170 if (func[0] == '!') {
4171 func++;
4172 ret = -EINVAL;
4173
4174 /* Look to remove this hash */
4175 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4176 if (strcmp(ftrace_mod->module, module) != 0)
4177 continue;
4178
4179 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03004180 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004181 (ftrace_mod->func &&
4182 strcmp(ftrace_mod->func, func) == 0)) {
4183 ret = 0;
4184 free_ftrace_mod(ftrace_mod);
4185 continue;
4186 }
4187 }
4188 goto out;
4189 }
4190
4191 ret = -EINVAL;
4192 /* We only care about modules that have not been loaded yet */
4193 if (module_exists(module))
4194 goto out;
4195
4196 /* Save this string off, and execute it when the module is loaded */
4197 ret = ftrace_add_mod(tr, func, module, enable);
4198 out:
4199 mutex_unlock(&ftrace_lock);
4200
4201 return ret;
4202}
4203
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004204static int
4205ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4206 int reset, int enable);
4207
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004208#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004209static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4210 char *mod, bool enable)
4211{
4212 struct ftrace_mod_load *ftrace_mod, *n;
4213 struct ftrace_hash **orig_hash, *new_hash;
4214 LIST_HEAD(process_mods);
4215 char *func;
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004216
4217 mutex_lock(&ops->func_hash->regex_lock);
4218
4219 if (enable)
4220 orig_hash = &ops->func_hash->filter_hash;
4221 else
4222 orig_hash = &ops->func_hash->notrace_hash;
4223
4224 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4225 *orig_hash);
4226 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004227 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004228
4229 mutex_lock(&ftrace_lock);
4230
4231 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4232
4233 if (strcmp(ftrace_mod->module, mod) != 0)
4234 continue;
4235
4236 if (ftrace_mod->func)
4237 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4238 else
4239 func = kstrdup("*", GFP_KERNEL);
4240
4241 if (!func) /* warn? */
4242 continue;
4243
Baokun Li3ecda642021-06-08 11:11:08 +08004244 list_move(&ftrace_mod->list, &process_mods);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004245
4246 /* Use the newly allocated func, as it may be "*" */
4247 kfree(ftrace_mod->func);
4248 ftrace_mod->func = func;
4249 }
4250
4251 mutex_unlock(&ftrace_lock);
4252
4253 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4254
4255 func = ftrace_mod->func;
4256
4257 /* Grabs ftrace_lock, which is why we have this extra step */
4258 match_records(new_hash, func, strlen(func), mod);
4259 free_ftrace_mod(ftrace_mod);
4260 }
4261
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004262 if (enable && list_empty(head))
4263 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4264
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004265 mutex_lock(&ftrace_lock);
4266
Alex Shi045e2692020-11-06 22:54:46 +08004267 ftrace_hash_move_and_update_ops(ops, orig_hash,
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004268 new_hash, enable);
4269 mutex_unlock(&ftrace_lock);
4270
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004271 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004272 mutex_unlock(&ops->func_hash->regex_lock);
4273
4274 free_ftrace_hash(new_hash);
4275}
4276
4277static void process_cached_mods(const char *mod_name)
4278{
4279 struct trace_array *tr;
4280 char *mod;
4281
4282 mod = kstrdup(mod_name, GFP_KERNEL);
4283 if (!mod)
4284 return;
4285
4286 mutex_lock(&trace_types_lock);
4287 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4288 if (!list_empty(&tr->mod_trace))
4289 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4290 if (!list_empty(&tr->mod_notrace))
4291 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4292 }
4293 mutex_unlock(&trace_types_lock);
4294
4295 kfree(mod);
4296}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004297#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004298
Steven Rostedtf6180772009-02-14 00:40:25 -05004299/*
4300 * We register the module command as a template to show others how
4301 * to register the a command as well.
4302 */
4303
4304static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004305ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004306 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004307{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004308 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004309 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004310
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004311 /* match_records() modifies func, and we need the original */
4312 func = kstrdup(func_orig, GFP_KERNEL);
4313 if (!func)
4314 return -ENOMEM;
4315
Steven Rostedtf6180772009-02-14 00:40:25 -05004316 /*
4317 * cmd == 'mod' because we only registered this func
4318 * for the 'mod' ftrace_func_command.
4319 * But if you register one func with multiple commands,
4320 * you can tell which command was used by the cmd
4321 * parameter.
4322 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004323 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004324 kfree(func);
4325
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004326 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004327 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004328 if (ret < 0)
4329 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004330 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004331}
4332
4333static struct ftrace_func_command ftrace_mod_cmd = {
4334 .name = "mod",
4335 .func = ftrace_mod_callback,
4336};
4337
4338static int __init ftrace_mod_cmd_init(void)
4339{
4340 return register_ftrace_command(&ftrace_mod_cmd);
4341}
Steven Rostedt6f415672012-10-05 12:13:07 -04004342core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004343
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004344static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04004345 struct ftrace_ops *op, struct ftrace_regs *fregs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004346{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004347 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004348 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004349
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004350 probe = container_of(op, struct ftrace_func_probe, ops);
4351 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004352
4353 /*
4354 * Disable preemption for these calls to prevent a RCU grace
4355 * period. This syncs the hash iteration and freeing of items
4356 * on the hash. rcu_read_lock is too dangerous here.
4357 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004358 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004359 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004360 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004361}
4362
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004363struct ftrace_func_map {
4364 struct ftrace_func_entry entry;
4365 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004366};
4367
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004368struct ftrace_func_mapper {
4369 struct ftrace_hash hash;
4370};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004371
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004372/**
4373 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4374 *
4375 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4376 */
4377struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004378{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004379 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004380
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004381 /*
4382 * The mapper is simply a ftrace_hash, but since the entries
4383 * in the hash are not ftrace_func_entry type, we define it
4384 * as a separate structure.
4385 */
4386 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4387 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004388}
4389
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004390/**
4391 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4392 * @mapper: The mapper that has the ip maps
4393 * @ip: the instruction pointer to find the data for
4394 *
4395 * Returns the data mapped to @ip if found otherwise NULL. The return
4396 * is actually the address of the mapper data pointer. The address is
4397 * returned for use cases where the data is no bigger than a long, and
4398 * the user can use the data pointer as its data instead of having to
4399 * allocate more memory for the reference.
4400 */
4401void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4402 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004403{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004404 struct ftrace_func_entry *entry;
4405 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004406
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004407 entry = ftrace_lookup_ip(&mapper->hash, ip);
4408 if (!entry)
4409 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004410
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004411 map = (struct ftrace_func_map *)entry;
4412 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004413}
4414
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004415/**
4416 * ftrace_func_mapper_add_ip - Map some data to an ip
4417 * @mapper: The mapper that has the ip maps
4418 * @ip: The instruction pointer address to map @data to
4419 * @data: The data to map to @ip
4420 *
Qiujun Huangfdda88d2020-10-02 22:31:26 +08004421 * Returns 0 on success otherwise an error.
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004422 */
4423int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4424 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004425{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004426 struct ftrace_func_entry *entry;
4427 struct ftrace_func_map *map;
4428
4429 entry = ftrace_lookup_ip(&mapper->hash, ip);
4430 if (entry)
4431 return -EBUSY;
4432
4433 map = kmalloc(sizeof(*map), GFP_KERNEL);
4434 if (!map)
4435 return -ENOMEM;
4436
4437 map->entry.ip = ip;
4438 map->data = data;
4439
4440 __add_hash_entry(&mapper->hash, &map->entry);
4441
4442 return 0;
4443}
4444
4445/**
4446 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4447 * @mapper: The mapper that has the ip maps
4448 * @ip: The instruction pointer address to remove the data from
4449 *
4450 * Returns the data if it is found, otherwise NULL.
4451 * Note, if the data pointer is used as the data itself, (see
4452 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4453 * if the data pointer was set to zero.
4454 */
4455void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4456 unsigned long ip)
4457{
4458 struct ftrace_func_entry *entry;
4459 struct ftrace_func_map *map;
4460 void *data;
4461
4462 entry = ftrace_lookup_ip(&mapper->hash, ip);
4463 if (!entry)
4464 return NULL;
4465
4466 map = (struct ftrace_func_map *)entry;
4467 data = map->data;
4468
4469 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004470 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004471
4472 return data;
4473}
4474
4475/**
4476 * free_ftrace_func_mapper - free a mapping of ips and data
4477 * @mapper: The mapper that has the ip maps
4478 * @free_func: A function to be called on each data item.
4479 *
4480 * This is used to free the function mapper. The @free_func is optional
4481 * and can be used if the data needs to be freed as well.
4482 */
4483void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4484 ftrace_mapper_func free_func)
4485{
4486 struct ftrace_func_entry *entry;
4487 struct ftrace_func_map *map;
4488 struct hlist_head *hhd;
Wei Li04e03d92019-06-06 11:17:54 +08004489 int size, i;
4490
4491 if (!mapper)
4492 return;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004493
4494 if (free_func && mapper->hash.count) {
Wei Li04e03d92019-06-06 11:17:54 +08004495 size = 1 << mapper->hash.size_bits;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004496 for (i = 0; i < size; i++) {
4497 hhd = &mapper->hash.buckets[i];
4498 hlist_for_each_entry(entry, hhd, hlist) {
4499 map = (struct ftrace_func_map *)entry;
4500 free_func(map);
4501 }
4502 }
4503 }
4504 free_ftrace_hash(&mapper->hash);
4505}
4506
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004507static void release_probe(struct ftrace_func_probe *probe)
4508{
4509 struct ftrace_probe_ops *probe_ops;
4510
4511 mutex_lock(&ftrace_lock);
4512
4513 WARN_ON(probe->ref <= 0);
4514
4515 /* Subtract the ref that was used to protect this instance */
4516 probe->ref--;
4517
4518 if (!probe->ref) {
4519 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004520 /*
4521 * Sending zero as ip tells probe_ops to free
4522 * the probe->data itself
4523 */
4524 if (probe_ops->free)
4525 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004526 list_del(&probe->list);
4527 kfree(probe);
4528 }
4529 mutex_unlock(&ftrace_lock);
4530}
4531
4532static void acquire_probe_locked(struct ftrace_func_probe *probe)
4533{
4534 /*
4535 * Add one ref to keep it from being freed when releasing the
4536 * ftrace_lock mutex.
4537 */
4538 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004539}
4540
Steven Rostedt59df055f2009-02-14 15:29:06 -05004541int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004542register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004543 struct ftrace_probe_ops *probe_ops,
4544 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004545{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004546 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004547 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004548 struct ftrace_hash **orig_hash;
4549 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004550 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004551 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004552 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004553 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004554 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004555
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004556 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004557 return -EINVAL;
4558
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004559 /* We do not support '!' for function probes */
4560 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004561 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004562
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004563
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004564 mutex_lock(&ftrace_lock);
4565 /* Check if the probe_ops is already registered */
4566 list_for_each_entry(probe, &tr->func_probes, list) {
4567 if (probe->probe_ops == probe_ops)
4568 break;
4569 }
4570 if (&probe->list == &tr->func_probes) {
4571 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4572 if (!probe) {
4573 mutex_unlock(&ftrace_lock);
4574 return -ENOMEM;
4575 }
4576 probe->probe_ops = probe_ops;
4577 probe->ops.func = function_trace_probe_call;
4578 probe->tr = tr;
4579 ftrace_ops_init(&probe->ops);
4580 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004581 }
4582
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004583 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004584
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004585 mutex_unlock(&ftrace_lock);
4586
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04004587 /*
4588 * Note, there's a small window here that the func_hash->filter_hash
Qiujun Huangfdda88d2020-10-02 22:31:26 +08004589 * may be NULL or empty. Need to be careful when reading the loop.
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04004590 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004591 mutex_lock(&probe->ops.func_hash->regex_lock);
4592
4593 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004594 old_hash = *orig_hash;
4595 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4596
Naveen N. Rao5b0022d2019-07-04 20:04:42 +05304597 if (!hash) {
4598 ret = -ENOMEM;
4599 goto out;
4600 }
4601
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004602 ret = ftrace_match_records(hash, glob, strlen(glob));
4603
4604 /* Nothing found? */
4605 if (!ret)
4606 ret = -EINVAL;
4607
4608 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004609 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004610
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004611 size = 1 << hash->size_bits;
4612 for (i = 0; i < size; i++) {
4613 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4614 if (ftrace_lookup_ip(old_hash, entry->ip))
4615 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004616 /*
4617 * The caller might want to do something special
4618 * for each function we find. We call the callback
4619 * to give the caller an opportunity to do so.
4620 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004621 if (probe_ops->init) {
4622 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004623 entry->ip, data,
4624 &probe->data);
4625 if (ret < 0) {
4626 if (probe_ops->free && count)
4627 probe_ops->free(probe_ops, tr,
4628 0, probe->data);
4629 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004630 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004631 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004632 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004633 count++;
4634 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004635 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004636
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004637 mutex_lock(&ftrace_lock);
4638
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004639 if (!count) {
4640 /* Nothing was added? */
4641 ret = -EINVAL;
4642 goto out_unlock;
4643 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004644
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004645 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4646 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004647 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004648 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004649
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004650 /* One ref for each new function traced */
4651 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004652
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004653 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4654 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004655
Steven Rostedt59df055f2009-02-14 15:29:06 -05004656 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004657 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004658
4659 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004660 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004661 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004662 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004663 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004664
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004665 release_probe(probe);
4666
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004667 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004668
4669 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004670 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004671 goto out_unlock;
4672
4673 /* Failed to do the move, need to call the free functions */
4674 for (i = 0; i < size; i++) {
4675 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4676 if (ftrace_lookup_ip(old_hash, entry->ip))
4677 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004678 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004679 }
4680 }
4681 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004682}
4683
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004684int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004685unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4686 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004687{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004688 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004689 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004690 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004691 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004692 struct ftrace_hash **orig_hash;
4693 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004694 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004695 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004696 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004697 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004698 int count = 0;
4699 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004700 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004701
Naveen N. Raocbab5672017-05-16 23:21:25 +05304702 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004703 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304704 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004705 int not;
4706
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004707 func_g.type = filter_parse_regex(glob, strlen(glob),
4708 &func_g.search, &not);
4709 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004710
Steven Rostedtb6887d72009-02-17 12:32:04 -05004711 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004712 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004713 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004714 }
4715
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004716 mutex_lock(&ftrace_lock);
4717 /* Check if the probe_ops is already registered */
4718 list_for_each_entry(probe, &tr->func_probes, list) {
4719 if (probe->probe_ops == probe_ops)
4720 break;
4721 }
4722 if (&probe->list == &tr->func_probes)
4723 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004724
4725 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004726 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4727 goto err_unlock_ftrace;
4728
4729 acquire_probe_locked(probe);
4730
4731 mutex_unlock(&ftrace_lock);
4732
4733 mutex_lock(&probe->ops.func_hash->regex_lock);
4734
4735 orig_hash = &probe->ops.func_hash->filter_hash;
4736 old_hash = *orig_hash;
4737
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004738 if (ftrace_hash_empty(old_hash))
4739 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004740
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004741 old_hash_ops.filter_hash = old_hash;
4742 /* Probes only have filters */
4743 old_hash_ops.notrace_hash = NULL;
4744
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004745 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004746 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004747 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004748 goto out_unlock;
4749
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004750 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004751
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004752 size = 1 << hash->size_bits;
4753 for (i = 0; i < size; i++) {
4754 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004755
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004756 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004757 kallsyms_lookup(entry->ip, NULL, NULL,
4758 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004759 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004760 continue;
4761 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004762 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004763 remove_hash_entry(hash, entry);
4764 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004765 }
4766 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004767
4768 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004769 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004770 ret = -EINVAL;
4771 goto out_unlock;
4772 }
4773
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004774 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004775
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004776 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004777
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004778 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004779
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004780 if (ftrace_hash_empty(hash))
4781 ftrace_shutdown(&probe->ops, 0);
4782
4783 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004784 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004785
4786 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004787 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004788 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004789 &old_hash_ops);
Paul E. McKenney74401722018-11-06 18:44:52 -08004790 synchronize_rcu();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004791
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004792 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4793 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004794 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004795 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004796 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004797 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004798 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004799
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004800 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004801 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004802 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004803
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004804 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004805
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004806 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004807
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004808 err_unlock_ftrace:
4809 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004810 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004811}
4812
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304813void clear_ftrace_function_probes(struct trace_array *tr)
4814{
4815 struct ftrace_func_probe *probe, *n;
4816
4817 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4818 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4819}
4820
Steven Rostedtf6180772009-02-14 00:40:25 -05004821static LIST_HEAD(ftrace_commands);
4822static DEFINE_MUTEX(ftrace_cmd_mutex);
4823
Tom Zanussi38de93a2013-10-24 08:34:18 -05004824/*
4825 * Currently we only register ftrace commands from __init, so mark this
4826 * __init too.
4827 */
4828__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004829{
4830 struct ftrace_func_command *p;
4831 int ret = 0;
4832
4833 mutex_lock(&ftrace_cmd_mutex);
4834 list_for_each_entry(p, &ftrace_commands, list) {
4835 if (strcmp(cmd->name, p->name) == 0) {
4836 ret = -EBUSY;
4837 goto out_unlock;
4838 }
4839 }
4840 list_add(&cmd->list, &ftrace_commands);
4841 out_unlock:
4842 mutex_unlock(&ftrace_cmd_mutex);
4843
4844 return ret;
4845}
4846
Tom Zanussi38de93a2013-10-24 08:34:18 -05004847/*
4848 * Currently we only unregister ftrace commands from __init, so mark
4849 * this __init too.
4850 */
4851__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004852{
4853 struct ftrace_func_command *p, *n;
4854 int ret = -ENODEV;
4855
4856 mutex_lock(&ftrace_cmd_mutex);
4857 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4858 if (strcmp(cmd->name, p->name) == 0) {
4859 ret = 0;
4860 list_del_init(&p->list);
4861 goto out_unlock;
4862 }
4863 }
4864 out_unlock:
4865 mutex_unlock(&ftrace_cmd_mutex);
4866
4867 return ret;
4868}
4869
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004870static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004871 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004872{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004873 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004874 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004875 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004876 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004877 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004878
4879 func = strsep(&next, ":");
4880
4881 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004882 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004883 if (!ret)
4884 ret = -EINVAL;
4885 if (ret < 0)
4886 return ret;
4887 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004888 }
4889
Steven Rostedtf6180772009-02-14 00:40:25 -05004890 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004891
4892 command = strsep(&next, ":");
4893
Steven Rostedtf6180772009-02-14 00:40:25 -05004894 mutex_lock(&ftrace_cmd_mutex);
4895 list_for_each_entry(p, &ftrace_commands, list) {
4896 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004897 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004898 goto out_unlock;
4899 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004900 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004901 out_unlock:
4902 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004903
Steven Rostedtf6180772009-02-14 00:40:25 -05004904 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004905}
4906
Ingo Molnare309b412008-05-12 21:20:51 +02004907static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004908ftrace_regex_write(struct file *file, const char __user *ubuf,
4909 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004910{
4911 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004912 struct trace_parser *parser;
4913 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004914
Li Zefan4ba79782009-09-22 13:52:20 +08004915 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004916 return 0;
4917
Steven Rostedt5072c592008-05-12 21:20:43 +02004918 if (file->f_mode & FMODE_READ) {
4919 struct seq_file *m = file->private_data;
4920 iter = m->private;
4921 } else
4922 iter = file->private_data;
4923
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004924 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004925 return -ENODEV;
4926
4927 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004928
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004929 parser = &iter->parser;
4930 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004931
Li Zefan4ba79782009-09-22 13:52:20 +08004932 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004933 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004934 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004935 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004936 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004937 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004938 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004939 }
4940
Steven Rostedt5072c592008-05-12 21:20:43 +02004941 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004942 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004943 return ret;
4944}
4945
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004946ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004947ftrace_filter_write(struct file *file, const char __user *ubuf,
4948 size_t cnt, loff_t *ppos)
4949{
4950 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4951}
4952
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004953ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004954ftrace_notrace_write(struct file *file, const char __user *ubuf,
4955 size_t cnt, loff_t *ppos)
4956{
4957 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4958}
4959
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004960static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004961ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4962{
4963 struct ftrace_func_entry *entry;
4964
4965 if (!ftrace_location(ip))
4966 return -EINVAL;
4967
4968 if (remove) {
4969 entry = ftrace_lookup_ip(hash, ip);
4970 if (!entry)
4971 return -ENOENT;
4972 free_hash_entry(hash, entry);
4973 return 0;
4974 }
4975
4976 return add_hash_entry(hash, ip);
4977}
4978
4979static int
4980ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4981 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004982{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004983 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004984 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004985 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004986
Steven Rostedt41c52c02008-05-22 11:46:33 -04004987 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004988 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004989
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004990 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004991
Steven Rostedtf45948e2011-05-02 12:29:25 -04004992 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004993 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004994 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004995 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004996
Wang Nanb972cc52014-07-15 08:40:20 +08004997 if (reset)
4998 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4999 else
5000 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
5001
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005002 if (!hash) {
5003 ret = -ENOMEM;
5004 goto out_regex_unlock;
5005 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04005006
Jiri Olsaac483c42012-01-02 10:04:14 +01005007 if (buf && !ftrace_match_records(hash, buf, len)) {
5008 ret = -EINVAL;
5009 goto out_regex_unlock;
5010 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005011 if (ip) {
5012 ret = ftrace_match_addr(hash, ip, remove);
5013 if (ret < 0)
5014 goto out_regex_unlock;
5015 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005016
5017 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04005018 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005019 mutex_unlock(&ftrace_lock);
5020
Jiri Olsaac483c42012-01-02 10:04:14 +01005021 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005022 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005023
5024 free_ftrace_hash(hash);
5025 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04005026}
5027
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005028static int
5029ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
5030 int reset, int enable)
5031{
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05305032 return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005033}
5034
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005035#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005036
5037struct ftrace_direct_func {
5038 struct list_head next;
5039 unsigned long addr;
5040 int count;
5041};
5042
5043static LIST_HEAD(ftrace_direct_funcs);
5044
5045/**
5046 * ftrace_find_direct_func - test an address if it is a registered direct caller
5047 * @addr: The address of a registered direct caller
5048 *
5049 * This searches to see if a ftrace direct caller has been registered
5050 * at a specific address, and if so, it returns a descriptor for it.
5051 *
5052 * This can be used by architecture code to see if an address is
5053 * a direct caller (trampoline) attached to a fentry/mcount location.
5054 * This is useful for the function_graph tracer, as it may need to
5055 * do adjustments if it traced a location that also has a direct
5056 * trampoline attached to it.
5057 */
5058struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
5059{
5060 struct ftrace_direct_func *entry;
5061 bool found = false;
5062
5063 /* May be called by fgraph trampoline (protected by rcu tasks) */
5064 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
5065 if (entry->addr == addr) {
5066 found = true;
5067 break;
5068 }
5069 }
5070 if (found)
5071 return entry;
5072
5073 return NULL;
5074}
5075
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005076static struct ftrace_direct_func *ftrace_alloc_direct_func(unsigned long addr)
5077{
5078 struct ftrace_direct_func *direct;
5079
5080 direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5081 if (!direct)
5082 return NULL;
5083 direct->addr = addr;
5084 direct->count = 0;
5085 list_add_rcu(&direct->next, &ftrace_direct_funcs);
5086 ftrace_direct_func_count++;
5087 return direct;
5088}
5089
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005090/**
5091 * register_ftrace_direct - Call a custom trampoline directly
5092 * @ip: The address of the nop at the beginning of a function
5093 * @addr: The address of the trampoline to call at @ip
5094 *
5095 * This is used to connect a direct call from the nop location (@ip)
5096 * at the start of ftrace traced functions. The location that it calls
5097 * (@addr) must be able to handle a direct call, and save the parameters
5098 * of the function being traced, and restore them (or inject new ones
5099 * if needed), before returning.
5100 *
5101 * Returns:
5102 * 0 on success
5103 * -EBUSY - Another direct function is already attached (there can be only one)
5104 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5105 * -ENOMEM - There was an allocation failure.
5106 */
5107int register_ftrace_direct(unsigned long ip, unsigned long addr)
5108{
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005109 struct ftrace_direct_func *direct;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005110 struct ftrace_func_entry *entry;
5111 struct ftrace_hash *free_hash = NULL;
5112 struct dyn_ftrace *rec;
5113 int ret = -EBUSY;
5114
5115 mutex_lock(&direct_mutex);
5116
5117 /* See if there's a direct function at @ip already */
Alexei Starovoitovff205762019-12-08 16:01:12 -08005118 if (ftrace_find_rec_direct(ip))
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005119 goto out_unlock;
5120
5121 ret = -ENODEV;
5122 rec = lookup_rec(ip, ip);
5123 if (!rec)
5124 goto out_unlock;
5125
5126 /*
5127 * Check if the rec says it has a direct call but we didn't
5128 * find one earlier?
5129 */
5130 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5131 goto out_unlock;
5132
5133 /* Make sure the ip points to the exact record */
Steven Rostedt (VMware)406acdd2019-11-15 14:19:04 -05005134 if (ip != rec->ip) {
5135 ip = rec->ip;
5136 /* Need to check this ip for a direct. */
Alexei Starovoitovff205762019-12-08 16:01:12 -08005137 if (ftrace_find_rec_direct(ip))
Steven Rostedt (VMware)406acdd2019-11-15 14:19:04 -05005138 goto out_unlock;
5139 }
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005140
5141 ret = -ENOMEM;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005142 direct = ftrace_find_direct_func(addr);
5143 if (!direct) {
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005144 direct = ftrace_alloc_direct_func(addr);
Jiri Olsa1904a812021-10-08 11:13:33 +02005145 if (!direct)
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005146 goto out_unlock;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005147 }
5148
Jiri Olsa1904a812021-10-08 11:13:33 +02005149 entry = ftrace_add_rec_direct(ip, addr, &free_hash);
5150 if (!entry)
5151 goto out_unlock;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005152
5153 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5154 if (ret)
5155 remove_hash_entry(direct_functions, entry);
5156
5157 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5158 ret = register_ftrace_function(&direct_ops);
5159 if (ret)
5160 ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5161 }
5162
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005163 if (ret) {
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005164 kfree(entry);
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005165 if (!direct->count) {
5166 list_del_rcu(&direct->next);
5167 synchronize_rcu_tasks();
5168 kfree(direct);
5169 if (free_hash)
5170 free_ftrace_hash(free_hash);
5171 free_hash = NULL;
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05005172 ftrace_direct_func_count--;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005173 }
5174 } else {
Steven Rostedt (VMware)1c7f9b62019-11-15 14:13:20 -05005175 direct->count++;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005176 }
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005177 out_unlock:
5178 mutex_unlock(&direct_mutex);
5179
5180 if (free_hash) {
5181 synchronize_rcu_tasks();
5182 free_ftrace_hash(free_hash);
5183 }
5184
5185 return ret;
5186}
5187EXPORT_SYMBOL_GPL(register_ftrace_direct);
5188
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005189static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5190 struct dyn_ftrace **recp)
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005191{
5192 struct ftrace_func_entry *entry;
5193 struct dyn_ftrace *rec;
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005194
5195 rec = lookup_rec(*ip, *ip);
5196 if (!rec)
5197 return NULL;
5198
5199 entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5200 if (!entry) {
5201 WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5202 return NULL;
5203 }
5204
5205 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5206
5207 /* Passed in ip just needs to be on the call site */
5208 *ip = rec->ip;
5209
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005210 if (recp)
5211 *recp = rec;
5212
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005213 return entry;
5214}
5215
5216int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5217{
5218 struct ftrace_direct_func *direct;
5219 struct ftrace_func_entry *entry;
Jiri Olsa7d5b7ca2021-12-06 19:20:30 +01005220 struct ftrace_hash *hash;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005221 int ret = -ENODEV;
5222
5223 mutex_lock(&direct_mutex);
5224
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005225 entry = find_direct_entry(&ip, NULL);
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005226 if (!entry)
5227 goto out_unlock;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005228
Jiri Olsa7d5b7ca2021-12-06 19:20:30 +01005229 hash = direct_ops.func_hash->filter_hash;
5230 if (hash->count == 1)
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005231 unregister_ftrace_function(&direct_ops);
5232
5233 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5234
5235 WARN_ON(ret);
5236
5237 remove_hash_entry(direct_functions, entry);
5238
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005239 direct = ftrace_find_direct_func(addr);
5240 if (!WARN_ON(!direct)) {
5241 /* This is the good path (see the ! before WARN) */
5242 direct->count--;
5243 WARN_ON(direct->count < 0);
5244 if (!direct->count) {
5245 list_del_rcu(&direct->next);
5246 synchronize_rcu_tasks();
5247 kfree(direct);
Steven Rostedt (VMware)353da872020-04-22 21:38:45 -04005248 kfree(entry);
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05005249 ftrace_direct_func_count--;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005250 }
5251 }
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005252 out_unlock:
5253 mutex_unlock(&direct_mutex);
5254
5255 return ret;
5256}
5257EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005258
5259static struct ftrace_ops stub_ops = {
5260 .func = ftrace_stub,
5261};
5262
5263/**
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005264 * ftrace_modify_direct_caller - modify ftrace nop directly
5265 * @entry: The ftrace hash entry of the direct helper for @rec
5266 * @rec: The record representing the function site to patch
5267 * @old_addr: The location that the site at @rec->ip currently calls
5268 * @new_addr: The location that the site at @rec->ip should call
5269 *
5270 * An architecture may overwrite this function to optimize the
5271 * changing of the direct callback on an ftrace nop location.
5272 * This is called with the ftrace_lock mutex held, and no other
5273 * ftrace callbacks are on the associated record (@rec). Thus,
5274 * it is safe to modify the ftrace record, where it should be
5275 * currently calling @old_addr directly, to call @new_addr.
5276 *
5277 * Safety checks should be made to make sure that the code at
5278 * @rec->ip is currently calling @old_addr. And this must
5279 * also update entry->direct to @new_addr.
5280 */
5281int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5282 struct dyn_ftrace *rec,
5283 unsigned long old_addr,
5284 unsigned long new_addr)
5285{
5286 unsigned long ip = rec->ip;
5287 int ret;
5288
5289 /*
5290 * The ftrace_lock was used to determine if the record
5291 * had more than one registered user to it. If it did,
5292 * we needed to prevent that from changing to do the quick
5293 * switch. But if it did not (only a direct caller was attached)
5294 * then this function is called. But this function can deal
5295 * with attached callers to the rec that we care about, and
5296 * since this function uses standard ftrace calls that take
5297 * the ftrace_lock mutex, we need to release it.
5298 */
5299 mutex_unlock(&ftrace_lock);
5300
5301 /*
5302 * By setting a stub function at the same address, we force
5303 * the code to call the iterator and the direct_ops helper.
5304 * This means that @ip does not call the direct call, and
5305 * we can simply modify it.
5306 */
5307 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5308 if (ret)
5309 goto out_lock;
5310
5311 ret = register_ftrace_function(&stub_ops);
5312 if (ret) {
5313 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5314 goto out_lock;
5315 }
5316
5317 entry->direct = new_addr;
5318
5319 /*
5320 * By removing the stub, we put back the direct call, calling
5321 * the @new_addr.
5322 */
5323 unregister_ftrace_function(&stub_ops);
5324 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5325
5326 out_lock:
5327 mutex_lock(&ftrace_lock);
5328
5329 return ret;
5330}
5331
5332/**
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005333 * modify_ftrace_direct - Modify an existing direct call to call something else
5334 * @ip: The instruction pointer to modify
5335 * @old_addr: The address that the current @ip calls directly
5336 * @new_addr: The address that the @ip should call
5337 *
5338 * This modifies a ftrace direct caller at an instruction pointer without
5339 * having to disable it first. The direct call will switch over to the
5340 * @new_addr without missing anything.
5341 *
5342 * Returns: zero on success. Non zero on error, which includes:
5343 * -ENODEV : the @ip given has no direct caller attached
5344 * -EINVAL : the @old_addr does not match the current direct caller
5345 */
5346int modify_ftrace_direct(unsigned long ip,
5347 unsigned long old_addr, unsigned long new_addr)
5348{
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005349 struct ftrace_direct_func *direct, *new_direct = NULL;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005350 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005351 struct dyn_ftrace *rec;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005352 int ret = -ENODEV;
5353
5354 mutex_lock(&direct_mutex);
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005355
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005356 mutex_lock(&ftrace_lock);
5357 entry = find_direct_entry(&ip, &rec);
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005358 if (!entry)
5359 goto out_unlock;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005360
5361 ret = -EINVAL;
5362 if (entry->direct != old_addr)
5363 goto out_unlock;
5364
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005365 direct = ftrace_find_direct_func(old_addr);
5366 if (WARN_ON(!direct))
5367 goto out_unlock;
5368 if (direct->count > 1) {
5369 ret = -ENOMEM;
5370 new_direct = ftrace_alloc_direct_func(new_addr);
5371 if (!new_direct)
5372 goto out_unlock;
5373 direct->count--;
5374 new_direct->count++;
5375 } else {
5376 direct->addr = new_addr;
5377 }
5378
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005379 /*
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005380 * If there's no other ftrace callback on the rec->ip location,
5381 * then it can be changed directly by the architecture.
5382 * If there is another caller, then we just need to change the
5383 * direct caller helper to point to @new_addr.
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005384 */
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005385 if (ftrace_rec_count(rec) == 1) {
5386 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5387 } else {
5388 entry->direct = new_addr;
5389 ret = 0;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005390 }
5391
Alexei Starovoitov8a141dd2021-03-16 12:58:15 -07005392 if (unlikely(ret && new_direct)) {
5393 direct->count++;
5394 list_del_rcu(&new_direct->next);
5395 synchronize_rcu_tasks();
5396 kfree(new_direct);
5397 ftrace_direct_func_count--;
5398 }
5399
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005400 out_unlock:
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005401 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005402 mutex_unlock(&direct_mutex);
5403 return ret;
5404}
5405EXPORT_SYMBOL_GPL(modify_ftrace_direct);
Jiri Olsaf64dd462021-10-08 11:13:34 +02005406
5407#define MULTI_FLAGS (FTRACE_OPS_FL_IPMODIFY | FTRACE_OPS_FL_DIRECT | \
5408 FTRACE_OPS_FL_SAVE_REGS)
5409
5410static int check_direct_multi(struct ftrace_ops *ops)
5411{
5412 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5413 return -EINVAL;
5414 if ((ops->flags & MULTI_FLAGS) != MULTI_FLAGS)
5415 return -EINVAL;
5416 return 0;
5417}
5418
5419static void remove_direct_functions_hash(struct ftrace_hash *hash, unsigned long addr)
5420{
5421 struct ftrace_func_entry *entry, *del;
5422 int size, i;
5423
5424 size = 1 << hash->size_bits;
5425 for (i = 0; i < size; i++) {
5426 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5427 del = __ftrace_lookup_ip(direct_functions, entry->ip);
5428 if (del && del->direct == addr) {
5429 remove_hash_entry(direct_functions, del);
5430 kfree(del);
5431 }
5432 }
5433 }
5434}
5435
5436/**
5437 * register_ftrace_direct_multi - Call a custom trampoline directly
5438 * for multiple functions registered in @ops
5439 * @ops: The address of the struct ftrace_ops object
5440 * @addr: The address of the trampoline to call at @ops functions
5441 *
5442 * This is used to connect a direct calls to @addr from the nop locations
5443 * of the functions registered in @ops (with by ftrace_set_filter_ip
5444 * function).
5445 *
5446 * The location that it calls (@addr) must be able to handle a direct call,
5447 * and save the parameters of the function being traced, and restore them
5448 * (or inject new ones if needed), before returning.
5449 *
5450 * Returns:
5451 * 0 on success
5452 * -EINVAL - The @ops object was already registered with this call or
5453 * when there are no functions in @ops object.
5454 * -EBUSY - Another direct function is already attached (there can be only one)
5455 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
5456 * -ENOMEM - There was an allocation failure.
5457 */
5458int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5459{
5460 struct ftrace_hash *hash, *free_hash = NULL;
5461 struct ftrace_func_entry *entry, *new;
5462 int err = -EBUSY, size, i;
5463
5464 if (ops->func || ops->trampoline)
5465 return -EINVAL;
5466 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED))
5467 return -EINVAL;
5468 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5469 return -EINVAL;
5470
5471 hash = ops->func_hash->filter_hash;
5472 if (ftrace_hash_empty(hash))
5473 return -EINVAL;
5474
5475 mutex_lock(&direct_mutex);
5476
5477 /* Make sure requested entries are not already registered.. */
5478 size = 1 << hash->size_bits;
5479 for (i = 0; i < size; i++) {
5480 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5481 if (ftrace_find_rec_direct(entry->ip))
5482 goto out_unlock;
5483 }
5484 }
5485
5486 /* ... and insert them to direct_functions hash. */
5487 err = -ENOMEM;
5488 for (i = 0; i < size; i++) {
5489 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
5490 new = ftrace_add_rec_direct(entry->ip, addr, &free_hash);
5491 if (!new)
5492 goto out_remove;
5493 entry->direct = addr;
5494 }
5495 }
5496
5497 ops->func = call_direct_funcs;
5498 ops->flags = MULTI_FLAGS;
5499 ops->trampoline = FTRACE_REGS_ADDR;
5500
5501 err = register_ftrace_function(ops);
5502
5503 out_remove:
5504 if (err)
5505 remove_direct_functions_hash(hash, addr);
5506
5507 out_unlock:
5508 mutex_unlock(&direct_mutex);
5509
5510 if (free_hash) {
5511 synchronize_rcu_tasks();
5512 free_ftrace_hash(free_hash);
5513 }
5514 return err;
5515}
5516EXPORT_SYMBOL_GPL(register_ftrace_direct_multi);
5517
5518/**
5519 * unregister_ftrace_direct_multi - Remove calls to custom trampoline
5520 * previously registered by register_ftrace_direct_multi for @ops object.
5521 * @ops: The address of the struct ftrace_ops object
5522 *
5523 * This is used to remove a direct calls to @addr from the nop locations
5524 * of the functions registered in @ops (with by ftrace_set_filter_ip
5525 * function).
5526 *
5527 * Returns:
5528 * 0 on success
5529 * -EINVAL - The @ops object was not properly registered.
5530 */
5531int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5532{
5533 struct ftrace_hash *hash = ops->func_hash->filter_hash;
5534 int err;
5535
5536 if (check_direct_multi(ops))
5537 return -EINVAL;
5538 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5539 return -EINVAL;
5540
5541 mutex_lock(&direct_mutex);
5542 err = unregister_ftrace_function(ops);
5543 remove_direct_functions_hash(hash, addr);
5544 mutex_unlock(&direct_mutex);
Jiri Olsafea3ffa2021-12-06 19:20:31 +01005545
5546 /* cleanup for possible another register call */
5547 ops->func = NULL;
5548 ops->trampoline = 0;
Jiri Olsaf64dd462021-10-08 11:13:34 +02005549 return err;
5550}
5551EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi);
Jiri Olsaccf5a892021-10-08 11:13:35 +02005552
5553/**
5554 * modify_ftrace_direct_multi - Modify an existing direct 'multi' call
5555 * to call something else
5556 * @ops: The address of the struct ftrace_ops object
5557 * @addr: The address of the new trampoline to call at @ops functions
5558 *
5559 * This is used to unregister currently registered direct caller and
5560 * register new one @addr on functions registered in @ops object.
5561 *
5562 * Note there's window between ftrace_shutdown and ftrace_startup calls
5563 * where there will be no callbacks called.
5564 *
5565 * Returns: zero on success. Non zero on error, which includes:
5566 * -EINVAL - The @ops object was not properly registered.
5567 */
5568int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
5569{
Steven Rostedt (VMware)ed292712021-10-14 16:11:14 -04005570 struct ftrace_hash *hash;
Jiri Olsaccf5a892021-10-08 11:13:35 +02005571 struct ftrace_func_entry *entry, *iter;
Steven Rostedt (VMware)ed292712021-10-14 16:11:14 -04005572 static struct ftrace_ops tmp_ops = {
5573 .func = ftrace_stub,
5574 .flags = FTRACE_OPS_FL_STUB,
5575 };
Jiri Olsaccf5a892021-10-08 11:13:35 +02005576 int i, size;
5577 int err;
5578
5579 if (check_direct_multi(ops))
5580 return -EINVAL;
5581 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
5582 return -EINVAL;
5583
5584 mutex_lock(&direct_mutex);
Steven Rostedt (VMware)ed292712021-10-14 16:11:14 -04005585
5586 /* Enable the tmp_ops to have the same functions as the direct ops */
5587 ftrace_ops_init(&tmp_ops);
5588 tmp_ops.func_hash = ops->func_hash;
5589
5590 err = register_ftrace_function(&tmp_ops);
5591 if (err)
5592 goto out_direct;
Jiri Olsaccf5a892021-10-08 11:13:35 +02005593
5594 /*
Steven Rostedt (VMware)ed292712021-10-14 16:11:14 -04005595 * Now the ftrace_ops_list_func() is called to do the direct callers.
5596 * We can safely change the direct functions attached to each entry.
Jiri Olsaccf5a892021-10-08 11:13:35 +02005597 */
Steven Rostedt (VMware)ed292712021-10-14 16:11:14 -04005598 mutex_lock(&ftrace_lock);
Jiri Olsaccf5a892021-10-08 11:13:35 +02005599
Steven Rostedt (VMware)ed292712021-10-14 16:11:14 -04005600 hash = ops->func_hash->filter_hash;
Jiri Olsaccf5a892021-10-08 11:13:35 +02005601 size = 1 << hash->size_bits;
5602 for (i = 0; i < size; i++) {
5603 hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
5604 entry = __ftrace_lookup_ip(direct_functions, iter->ip);
5605 if (!entry)
5606 continue;
5607 entry->direct = addr;
5608 }
5609 }
5610
Jiri Olsa2e6e9052021-11-09 12:42:17 +01005611 mutex_unlock(&ftrace_lock);
5612
Steven Rostedt (VMware)ed292712021-10-14 16:11:14 -04005613 /* Removing the tmp_ops will add the updated direct callers to the functions */
5614 unregister_ftrace_function(&tmp_ops);
Jiri Olsaccf5a892021-10-08 11:13:35 +02005615
Steven Rostedt (VMware)ed292712021-10-14 16:11:14 -04005616 out_direct:
Jiri Olsaccf5a892021-10-08 11:13:35 +02005617 mutex_unlock(&direct_mutex);
5618 return err;
5619}
5620EXPORT_SYMBOL_GPL(modify_ftrace_direct_multi);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005621#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5622
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005623/**
5624 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5625 * @ops - the ops to set the filter with
5626 * @ip - the address to add to or remove from the filter.
5627 * @remove - non zero to remove the ip from the filter
5628 * @reset - non zero to reset all filters before applying this filter.
5629 *
5630 * Filters denote which functions should be enabled when tracing is enabled
Ingo Molnarf2cc0202021-03-23 18:49:35 +01005631 * If @ip is NULL, it fails to update filter.
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005632 */
5633int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5634 int remove, int reset)
5635{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005636 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005637 return ftrace_set_addr(ops, ip, remove, reset, 1);
5638}
5639EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5640
Joel Fernandesd032ae82016-11-15 12:31:20 -08005641/**
5642 * ftrace_ops_set_global_filter - setup ops to use global filters
5643 * @ops - the ops which will use the global filters
5644 *
5645 * ftrace users who need global function trace filtering should call this.
5646 * It can set the global filter only if ops were not initialized before.
5647 */
5648void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5649{
5650 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5651 return;
5652
5653 ftrace_ops_init(ops);
5654 ops->func_hash = &global_ops.local_hash;
5655}
5656EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5657
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005658static int
5659ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5660 int reset, int enable)
5661{
5662 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
5663}
5664
Steven Rostedt77a2b372008-05-12 21:20:45 +02005665/**
5666 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04005667 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02005668 * @buf - the string that holds the function filter text.
5669 * @len - the length of the string.
5670 * @reset - non zero to reset all filters before applying this filter.
5671 *
5672 * Filters denote which functions should be enabled when tracing is enabled.
5673 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5674 */
Jiri Olsaac483c42012-01-02 10:04:14 +01005675int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04005676 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02005677{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005678 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01005679 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04005680}
Steven Rostedt936e0742011-05-05 22:54:01 -04005681EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005682
Steven Rostedt41c52c02008-05-22 11:46:33 -04005683/**
5684 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04005685 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04005686 * @buf - the string that holds the function notrace text.
5687 * @len - the length of the string.
5688 * @reset - non zero to reset all filters before applying this filter.
5689 *
5690 * Notrace Filters denote which functions should not be enabled when tracing
5691 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5692 * for tracing.
5693 */
Jiri Olsaac483c42012-01-02 10:04:14 +01005694int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04005695 int len, int reset)
5696{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005697 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01005698 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04005699}
5700EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5701/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08005702 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04005703 * @buf - the string that holds the function filter text.
5704 * @len - the length of the string.
5705 * @reset - non zero to reset all filters before applying this filter.
5706 *
5707 * Filters denote which functions should be enabled when tracing is enabled.
5708 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5709 */
5710void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5711{
5712 ftrace_set_regex(&global_ops, buf, len, reset, 1);
5713}
5714EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5715
5716/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08005717 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04005718 * @buf - the string that holds the function notrace text.
5719 * @len - the length of the string.
5720 * @reset - non zero to reset all filters before applying this filter.
5721 *
5722 * Notrace Filters denote which functions should not be enabled when tracing
5723 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5724 * for tracing.
5725 */
5726void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04005727{
Steven Rostedtf45948e2011-05-02 12:29:25 -04005728 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02005729}
Steven Rostedt936e0742011-05-05 22:54:01 -04005730EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02005731
Steven Rostedt2af15d62009-05-28 13:37:24 -04005732/*
5733 * command line interface to allow users to set filters on boot up.
5734 */
5735#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
5736static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5737static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5738
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04005739/* Used by function selftest to not test if filter is set */
5740bool ftrace_filter_param __initdata;
5741
Steven Rostedt2af15d62009-05-28 13:37:24 -04005742static int __init set_ftrace_notrace(char *str)
5743{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04005744 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08005745 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005746 return 1;
5747}
5748__setup("ftrace_notrace=", set_ftrace_notrace);
5749
5750static int __init set_ftrace_filter(char *str)
5751{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04005752 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08005753 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005754 return 1;
5755}
5756__setup("ftrace_filter=", set_ftrace_filter);
5757
Stefan Assmann369bc182009-10-12 22:17:21 +02005758#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08005759static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005760static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005761static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05005762
Stefan Assmann369bc182009-10-12 22:17:21 +02005763static int __init set_graph_function(char *str)
5764{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02005765 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02005766 return 1;
5767}
5768__setup("ftrace_graph_filter=", set_graph_function);
5769
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005770static int __init set_graph_notrace_function(char *str)
5771{
5772 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5773 return 1;
5774}
5775__setup("ftrace_graph_notrace=", set_graph_notrace_function);
5776
Todd Brandt65a50c62017-03-02 16:12:15 -08005777static int __init set_graph_max_depth_function(char *str)
5778{
5779 if (!str)
5780 return 0;
5781 fgraph_max_depth = simple_strtoul(str, NULL, 0);
5782 return 1;
5783}
5784__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5785
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005786static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02005787{
5788 int ret;
5789 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005790 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005791
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005792 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05005793 if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005794 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02005795
5796 while (buf) {
5797 func = strsep(&buf, ",");
5798 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005799 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02005800 if (ret)
5801 printk(KERN_DEBUG "ftrace: function %s not "
5802 "traceable\n", func);
5803 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005804
5805 if (enable)
5806 ftrace_graph_hash = hash;
5807 else
5808 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02005809}
5810#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5811
Steven Rostedt2a85a372011-12-19 21:57:44 -05005812void __init
5813ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04005814{
5815 char *func;
5816
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005817 ftrace_ops_init(ops);
5818
Steven Rostedt2af15d62009-05-28 13:37:24 -04005819 while (buf) {
5820 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04005821 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005822 }
5823}
5824
5825static void __init set_ftrace_early_filters(void)
5826{
5827 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005828 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005829 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005830 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005831#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5832 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005833 set_ftrace_early_graph(ftrace_graph_buf, 1);
5834 if (ftrace_graph_notrace_buf[0])
5835 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005836#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04005837}
5838
Steven Rostedtfc13cb02011-12-19 14:41:25 -05005839int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02005840{
5841 struct seq_file *m = (struct seq_file *)file->private_data;
5842 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005843 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005844 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04005845 int filter_hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02005846
Steven Rostedt5072c592008-05-12 21:20:43 +02005847 if (file->f_mode & FMODE_READ) {
5848 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02005849 seq_release(inode, file);
5850 } else
5851 iter = file->private_data;
5852
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005853 parser = &iter->parser;
5854 if (trace_parser_loaded(parser)) {
Steven Rostedt (VMware)8c9af472021-05-05 10:38:24 -04005855 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5856
5857 ftrace_process_regex(iter, parser->buffer,
5858 parser->idx, enable);
Steven Rostedt5072c592008-05-12 21:20:43 +02005859 }
5860
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005861 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005862
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005863 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005864
Steven Rostedt058e2972011-04-29 22:35:33 -04005865 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04005866 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5867
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005868 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005869 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04005870 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005871 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5872 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005873 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005874
Steven Rostedt058e2972011-04-29 22:35:33 -04005875 mutex_lock(&ftrace_lock);
Alex Shi045e2692020-11-06 22:54:46 +08005876 ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04005877 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04005878 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04005879 } else {
5880 /* For read only, the hash is the ops hash */
5881 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04005882 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005883
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005884 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005885 free_ftrace_hash(iter->hash);
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04005886 if (iter->tr)
5887 trace_array_put(iter->tr);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005888 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04005889
Steven Rostedt5072c592008-05-12 21:20:43 +02005890 return 0;
5891}
5892
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005893static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005894 .open = ftrace_avail_open,
5895 .read = seq_read,
5896 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08005897 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02005898};
5899
Steven Rostedt647bcd02011-05-03 14:39:21 -04005900static const struct file_operations ftrace_enabled_fops = {
5901 .open = ftrace_enabled_open,
5902 .read = seq_read,
5903 .llseek = seq_lseek,
5904 .release = seq_release_private,
5905};
5906
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005907static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005908 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005909 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005910 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005911 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005912 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005913};
5914
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005915static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005916 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005917 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005918 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005919 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005920 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005921};
5922
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005923#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5924
5925static DEFINE_MUTEX(graph_lock);
5926
Amol Grover24a97292020-02-01 12:57:04 +05305927struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
Amol Groverfd0e6852020-02-05 11:27:02 +05305928struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005929
5930enum graph_filter_type {
5931 GRAPH_FILTER_NOTRACE = 0,
5932 GRAPH_FILTER_FUNCTION,
5933};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005934
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005935#define FTRACE_GRAPH_EMPTY ((void *)1)
5936
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005937struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005938 struct ftrace_hash *hash;
5939 struct ftrace_func_entry *entry;
5940 int idx; /* for hash table iteration */
5941 enum graph_filter_type type;
5942 struct ftrace_hash *new_hash;
5943 const struct seq_operations *seq_ops;
5944 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005945};
5946
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005947static void *
Li Zefan85951842009-06-24 09:54:00 +08005948__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005949{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005950 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005951 struct ftrace_func_entry *entry = fgd->entry;
5952 struct hlist_head *head;
5953 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005954
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005955 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005956 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005957
5958 if (entry) {
5959 hlist_for_each_entry_continue(entry, hlist) {
5960 fgd->entry = entry;
5961 return entry;
5962 }
5963
5964 idx++;
5965 }
5966
5967 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5968 head = &fgd->hash->buckets[i];
5969 hlist_for_each_entry(entry, head, hlist) {
5970 fgd->entry = entry;
5971 fgd->idx = i;
5972 return entry;
5973 }
5974 }
5975 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005976}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005977
Li Zefan85951842009-06-24 09:54:00 +08005978static void *
5979g_next(struct seq_file *m, void *v, loff_t *pos)
5980{
5981 (*pos)++;
5982 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005983}
5984
5985static void *g_start(struct seq_file *m, loff_t *pos)
5986{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005987 struct ftrace_graph_data *fgd = m->private;
5988
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005989 mutex_lock(&graph_lock);
5990
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005991 if (fgd->type == GRAPH_FILTER_FUNCTION)
5992 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5993 lockdep_is_held(&graph_lock));
5994 else
5995 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5996 lockdep_is_held(&graph_lock));
5997
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005998 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005999 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05006000 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01006001
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006002 fgd->idx = 0;
6003 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08006004 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006005}
6006
6007static void g_stop(struct seq_file *m, void *p)
6008{
6009 mutex_unlock(&graph_lock);
6010}
6011
6012static int g_show(struct seq_file *m, void *v)
6013{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006014 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006015
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006016 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006017 return 0;
6018
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05006019 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09006020 struct ftrace_graph_data *fgd = m->private;
6021
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006022 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006023 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09006024 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006025 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01006026 return 0;
6027 }
6028
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006029 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006030
6031 return 0;
6032}
6033
James Morris88e9d342009-09-22 16:43:43 -07006034static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006035 .start = g_start,
6036 .next = g_next,
6037 .stop = g_stop,
6038 .show = g_show,
6039};
6040
6041static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006042__ftrace_graph_open(struct inode *inode, struct file *file,
6043 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006044{
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04006045 int ret;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006046 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006047
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04006048 ret = security_locked_down(LOCKDOWN_TRACEFS);
6049 if (ret)
6050 return ret;
6051
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006052 if (file->f_mode & FMODE_WRITE) {
6053 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
6054
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006055 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
6056 return -ENOMEM;
6057
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006058 if (file->f_flags & O_TRUNC)
6059 new_hash = alloc_ftrace_hash(size_bits);
6060 else
6061 new_hash = alloc_and_copy_ftrace_hash(size_bits,
6062 fgd->hash);
6063 if (!new_hash) {
6064 ret = -ENOMEM;
6065 goto out;
6066 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006067 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006068
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006069 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006070 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006071 if (!ret) {
6072 struct seq_file *m = file->private_data;
6073 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006074 } else {
6075 /* Failed */
6076 free_ftrace_hash(new_hash);
6077 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006078 }
6079 } else
6080 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08006081
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006082out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006083 if (ret < 0 && file->f_mode & FMODE_WRITE)
6084 trace_parser_put(&fgd->parser);
6085
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006086 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05006087
6088 /*
6089 * All uses of fgd->hash must be taken with the graph_lock
6090 * held. The graph_lock is going to be released, so force
6091 * fgd->hash to be reinitialized when it is taken again.
6092 */
6093 fgd->hash = NULL;
6094
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006095 return ret;
6096}
6097
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006098static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006099ftrace_graph_open(struct inode *inode, struct file *file)
6100{
6101 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006102 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006103
6104 if (unlikely(ftrace_disabled))
6105 return -ENODEV;
6106
6107 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6108 if (fgd == NULL)
6109 return -ENOMEM;
6110
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006111 mutex_lock(&graph_lock);
6112
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05006113 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
6114 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006115 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006116 fgd->seq_ops = &ftrace_graph_seq_ops;
6117
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006118 ret = __ftrace_graph_open(inode, file, fgd);
6119 if (ret < 0)
6120 kfree(fgd);
6121
6122 mutex_unlock(&graph_lock);
6123 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006124}
6125
6126static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006127ftrace_graph_notrace_open(struct inode *inode, struct file *file)
6128{
6129 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006130 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006131
6132 if (unlikely(ftrace_disabled))
6133 return -ENODEV;
6134
6135 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
6136 if (fgd == NULL)
6137 return -ENOMEM;
6138
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006139 mutex_lock(&graph_lock);
6140
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05006141 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6142 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006143 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006144 fgd->seq_ops = &ftrace_graph_seq_ops;
6145
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006146 ret = __ftrace_graph_open(inode, file, fgd);
6147 if (ret < 0)
6148 kfree(fgd);
6149
6150 mutex_unlock(&graph_lock);
6151 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006152}
6153
6154static int
Li Zefan87827112009-07-23 11:29:11 +08006155ftrace_graph_release(struct inode *inode, struct file *file)
6156{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006157 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006158 struct ftrace_hash *old_hash, *new_hash;
6159 struct trace_parser *parser;
6160 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006161
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006162 if (file->f_mode & FMODE_READ) {
6163 struct seq_file *m = file->private_data;
6164
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006165 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08006166 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006167 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006168 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006169 }
6170
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006171
6172 if (file->f_mode & FMODE_WRITE) {
6173
6174 parser = &fgd->parser;
6175
6176 if (trace_parser_loaded((parser))) {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006177 ret = ftrace_graph_set_hash(fgd->new_hash,
6178 parser->buffer);
6179 }
6180
6181 trace_parser_put(parser);
6182
6183 new_hash = __ftrace_hash_move(fgd->new_hash);
6184 if (!new_hash) {
6185 ret = -ENOMEM;
6186 goto out;
6187 }
6188
6189 mutex_lock(&graph_lock);
6190
6191 if (fgd->type == GRAPH_FILTER_FUNCTION) {
6192 old_hash = rcu_dereference_protected(ftrace_graph_hash,
6193 lockdep_is_held(&graph_lock));
6194 rcu_assign_pointer(ftrace_graph_hash, new_hash);
6195 } else {
6196 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
6197 lockdep_is_held(&graph_lock));
6198 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
6199 }
6200
6201 mutex_unlock(&graph_lock);
6202
Steven Rostedt (VMware)54a16ff2020-02-05 09:20:32 -05006203 /*
6204 * We need to do a hard force of sched synchronization.
6205 * This is because we use preempt_disable() to do RCU, but
6206 * the function tracers can be called where RCU is not watching
6207 * (like before user_exit()). We can not rely on the RCU
6208 * infrastructure to do the synchronization, thus we must do it
6209 * ourselves.
6210 */
Nicolas Saenz Julienne68e834982021-07-21 13:47:26 +02006211 if (old_hash != EMPTY_HASH)
6212 synchronize_rcu_tasks_rude();
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006213
6214 free_ftrace_hash(old_hash);
6215 }
6216
6217 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01006218 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006219 kfree(fgd);
6220
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006221 return ret;
Li Zefan87827112009-07-23 11:29:11 +08006222}
6223
6224static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006225ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006226{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03006227 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006228 struct dyn_ftrace *rec;
6229 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006230 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006231 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03006232 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006233
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01006234 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03006235 func_g.type = filter_parse_regex(buffer, strlen(buffer),
6236 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01006237
Dmitry Safonov3ba00922015-09-29 19:46:14 +03006238 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01006239
Steven Rostedt52baf112009-02-14 01:15:39 -05006240 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04006241
6242 if (unlikely(ftrace_disabled)) {
6243 mutex_unlock(&ftrace_lock);
6244 return -ENODEV;
6245 }
6246
Steven Rostedt265c8312009-02-13 12:43:56 -05006247 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006248
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05006249 if (rec->flags & FTRACE_FL_DISABLED)
6250 continue;
6251
Dmitry Safonov0b507e12015-09-29 19:46:15 +03006252 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006253 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006254
6255 if (!not) {
6256 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006257
6258 if (entry)
6259 continue;
6260 if (add_hash_entry(hash, rec->ip) < 0)
6261 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006262 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006263 if (entry) {
6264 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006265 fail = 0;
6266 }
6267 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006268 }
Steven Rostedt265c8312009-02-13 12:43:56 -05006269 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006270out:
Steven Rostedt52baf112009-02-14 01:15:39 -05006271 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006272
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006273 if (fail)
6274 return -EINVAL;
6275
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006276 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006277}
6278
6279static ssize_t
6280ftrace_graph_write(struct file *file, const char __user *ubuf,
6281 size_t cnt, loff_t *ppos)
6282{
Namhyung Kim6a101082013-10-14 17:24:25 +09006283 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09006284 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006285 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006286
Li Zefanc7c6b1f2010-02-10 15:43:04 +08006287 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006288 return 0;
6289
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05006290 /* Read mode uses seq functions */
6291 if (file->f_mode & FMODE_READ) {
6292 struct seq_file *m = file->private_data;
6293 fgd = m->private;
6294 }
6295
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006296 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02006297
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006298 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02006299
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006300 if (read >= 0 && trace_parser_loaded(parser) &&
6301 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09006302
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09006303 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05006304 parser->buffer);
6305 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006306 }
6307
Namhyung Kim6a101082013-10-14 17:24:25 +09006308 if (!ret)
6309 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08006310
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006311 return ret;
6312}
6313
6314static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08006315 .open = ftrace_graph_open,
6316 .read = seq_read,
6317 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006318 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08006319 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006320};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006321
6322static const struct file_operations ftrace_graph_notrace_fops = {
6323 .open = ftrace_graph_notrace_open,
6324 .read = seq_read,
6325 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006326 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006327 .release = ftrace_graph_release,
6328};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006329#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6330
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006331void ftrace_create_filter_files(struct ftrace_ops *ops,
6332 struct dentry *parent)
6333{
6334
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04006335 trace_create_file("set_ftrace_filter", TRACE_MODE_WRITE, parent,
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006336 ops, &ftrace_filter_fops);
6337
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04006338 trace_create_file("set_ftrace_notrace", TRACE_MODE_WRITE, parent,
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006339 ops, &ftrace_notrace_fops);
6340}
6341
6342/*
6343 * The name "destroy_filter_files" is really a misnomer. Although
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05306344 * in the future, it may actually delete the files, but this is
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006345 * really intended to make sure the ops passed in are disabled
6346 * and that when this function returns, the caller is free to
6347 * free the ops.
6348 *
6349 * The "destroy" name is only to match the "create" name that this
6350 * should be paired with.
6351 */
6352void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6353{
6354 mutex_lock(&ftrace_lock);
6355 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6356 ftrace_shutdown(ops, 0);
6357 ops->flags |= FTRACE_OPS_FL_DELETED;
Steven Rostedt (VMware)2840f842018-12-10 23:58:01 -05006358 ftrace_free_filter(ops);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006359 mutex_unlock(&ftrace_lock);
6360}
6361
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006362static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02006363{
Steven Rostedt5072c592008-05-12 21:20:43 +02006364
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04006365 trace_create_file("available_filter_functions", TRACE_MODE_READ,
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006366 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02006367
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04006368 trace_create_file("enabled_functions", TRACE_MODE_READ,
Steven Rostedt647bcd02011-05-03 14:39:21 -04006369 d_tracer, NULL, &ftrace_enabled_fops);
6370
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006371 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04006372
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006373#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04006374 trace_create_file("set_graph_function", TRACE_MODE_WRITE, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006375 NULL,
6376 &ftrace_graph_fops);
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04006377 trace_create_file("set_graph_notrace", TRACE_MODE_WRITE, d_tracer,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006378 NULL,
6379 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006380#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6381
Steven Rostedt5072c592008-05-12 21:20:43 +02006382 return 0;
6383}
6384
Steven Rostedt9fd49322012-04-24 22:32:06 -04006385static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05006386{
Steven Rostedt9fd49322012-04-24 22:32:06 -04006387 const unsigned long *ipa = a;
6388 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05006389
Steven Rostedt9fd49322012-04-24 22:32:06 -04006390 if (*ipa > *ipb)
6391 return 1;
6392 if (*ipa < *ipb)
6393 return -1;
6394 return 0;
6395}
6396
Steven Rostedt (VMware)8147dc782021-12-06 15:18:58 -05006397#ifdef CONFIG_FTRACE_SORT_STARTUP_TEST
6398static void test_is_sorted(unsigned long *start, unsigned long count)
6399{
6400 int i;
6401
6402 for (i = 1; i < count; i++) {
6403 if (WARN(start[i - 1] > start[i],
6404 "[%d] %pS at %lx is not sorted with %pS at %lx\n", i,
6405 (void *)start[i - 1], start[i - 1],
6406 (void *)start[i], start[i]))
6407 break;
6408 }
6409 if (i == count)
6410 pr_info("ftrace section at %px sorted properly\n", start);
6411}
6412#else
6413static void test_is_sorted(unsigned long *start, unsigned long count)
6414{
6415}
6416#endif
6417
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006418static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08006419 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006420 unsigned long *end)
6421{
Steven Rostedt706c81f2012-04-24 23:45:26 -04006422 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05006423 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04006424 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05006425 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006426 unsigned long *p;
6427 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04006428 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05006429 int ret = -ENOMEM;
6430
6431 count = end - start;
6432
6433 if (!count)
6434 return 0;
6435
Yinan Liu72b39422021-12-12 19:33:58 +08006436 /*
6437 * Sorting mcount in vmlinux at build time depend on
Steven Rostedt (Google)6b9b6412022-01-22 09:17:10 -05006438 * CONFIG_BUILDTIME_MCOUNT_SORT, while mcount loc in
Yinan Liu72b39422021-12-12 19:33:58 +08006439 * modules can not be sorted at build time.
6440 */
Steven Rostedt (Google)6b9b6412022-01-22 09:17:10 -05006441 if (!IS_ENABLED(CONFIG_BUILDTIME_MCOUNT_SORT) || mod) {
Yinan Liu72b39422021-12-12 19:33:58 +08006442 sort(start, count, sizeof(*start),
6443 ftrace_cmp_ips, NULL);
Steven Rostedt (VMware)8147dc782021-12-06 15:18:58 -05006444 } else {
6445 test_is_sorted(start, count);
Yinan Liu72b39422021-12-12 19:33:58 +08006446 }
Steven Rostedt9fd49322012-04-24 22:32:06 -04006447
Steven Rostedt706c81f2012-04-24 23:45:26 -04006448 start_pg = ftrace_allocate_pages(count);
6449 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05006450 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006451
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006452 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05006453
Steven Rostedt320823092011-12-16 14:42:37 -05006454 /*
6455 * Core and each module needs their own pages, as
6456 * modules will free them when they are removed.
6457 * Force a new page to be allocated for modules.
6458 */
Steven Rostedta7900872011-12-16 16:23:44 -05006459 if (!mod) {
6460 WARN_ON(ftrace_pages || ftrace_pages_start);
6461 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04006462 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05006463 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05006464 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05006465 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05006466
Steven Rostedta7900872011-12-16 16:23:44 -05006467 if (WARN_ON(ftrace_pages->next)) {
6468 /* Hmm, we have free pages? */
6469 while (ftrace_pages->next)
6470 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05006471 }
Steven Rostedta7900872011-12-16 16:23:44 -05006472
Steven Rostedt706c81f2012-04-24 23:45:26 -04006473 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05006474 }
6475
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006476 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04006477 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006478 while (p < end) {
Linus Torvaldsdb425232021-04-01 16:14:17 -04006479 unsigned long end_offset;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006480 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08006481 /*
6482 * Some architecture linkers will pad between
6483 * the different mcount_loc sections of different
6484 * object files to satisfy alignments.
6485 * Skip any NULL pointers.
6486 */
6487 if (!addr)
6488 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04006489
Linus Torvaldsdb425232021-04-01 16:14:17 -04006490 end_offset = (pg->index+1) * sizeof(pg->records[0]);
6491 if (end_offset > PAGE_SIZE << pg->order) {
Steven Rostedt706c81f2012-04-24 23:45:26 -04006492 /* We should have allocated enough */
6493 if (WARN_ON(!pg->next))
6494 break;
6495 pg = pg->next;
6496 }
6497
6498 rec = &pg->records[pg->index++];
6499 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006500 }
6501
Steven Rostedt706c81f2012-04-24 23:45:26 -04006502 /* We should have used all pages */
6503 WARN_ON(pg->next);
6504
6505 /* Assign the last page to ftrace_pages */
6506 ftrace_pages = pg;
6507
Steven Rostedta4f18ed2011-06-07 09:26:46 -04006508 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04006509 * We only need to disable interrupts on start up
6510 * because we are modifying code that an interrupt
6511 * may execute, and the modification is not atomic.
6512 * But for modules, nothing runs the code we modify
6513 * until we are finished with it, and there's no
6514 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04006515 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04006516 if (!mod)
6517 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006518 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04006519 if (!mod)
6520 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05006521 ret = 0;
6522 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006523 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006524
Steven Rostedta7900872011-12-16 16:23:44 -05006525 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006526}
6527
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006528struct ftrace_mod_func {
6529 struct list_head list;
6530 char *name;
6531 unsigned long ip;
6532 unsigned int size;
6533};
6534
6535struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006536 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006537 struct list_head list;
6538 struct module *mod;
6539 unsigned long start_addr;
6540 unsigned long end_addr;
6541 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006542 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006543};
6544
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006545static int ftrace_get_trampoline_kallsym(unsigned int symnum,
6546 unsigned long *value, char *type,
6547 char *name, char *module_name,
6548 int *exported)
6549{
6550 struct ftrace_ops *op;
6551
6552 list_for_each_entry_rcu(op, &ftrace_ops_trampoline_list, list) {
6553 if (!op->trampoline || symnum--)
6554 continue;
6555 *value = op->trampoline;
6556 *type = 't';
6557 strlcpy(name, FTRACE_TRAMPOLINE_SYM, KSYM_NAME_LEN);
6558 strlcpy(module_name, FTRACE_TRAMPOLINE_MOD, MODULE_NAME_LEN);
6559 *exported = 0;
6560 return 0;
6561 }
6562
6563 return -ERANGE;
6564}
6565
Steven Rostedt93eb6772009-04-15 13:24:06 -04006566#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05006567
6568#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6569
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006570static LIST_HEAD(ftrace_mod_maps);
6571
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006572static int referenced_filters(struct dyn_ftrace *rec)
6573{
6574 struct ftrace_ops *ops;
6575 int cnt = 0;
6576
6577 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
Chengming Zhou8a224ff2020-07-29 02:05:53 +08006578 if (ops_references_rec(ops, rec)) {
Chengming Zhouc5f51572020-07-29 02:05:54 +08006579 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_DIRECT))
6580 continue;
6581 if (WARN_ON_ONCE(ops->flags & FTRACE_OPS_FL_IPMODIFY))
6582 continue;
Chengming Zhou8a224ff2020-07-29 02:05:53 +08006583 cnt++;
6584 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
6585 rec->flags |= FTRACE_FL_REGS;
Chengming Zhouc5f51572020-07-29 02:05:54 +08006586 if (cnt == 1 && ops->trampoline)
6587 rec->flags |= FTRACE_FL_TRAMP;
6588 else
6589 rec->flags &= ~FTRACE_FL_TRAMP;
Chengming Zhou8a224ff2020-07-29 02:05:53 +08006590 }
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006591 }
6592
6593 return cnt;
6594}
6595
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006596static void
6597clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6598{
6599 struct ftrace_func_entry *entry;
6600 struct dyn_ftrace *rec;
6601 int i;
6602
6603 if (ftrace_hash_empty(hash))
6604 return;
6605
6606 for (i = 0; i < pg->index; i++) {
6607 rec = &pg->records[i];
6608 entry = __ftrace_lookup_ip(hash, rec->ip);
6609 /*
6610 * Do not allow this rec to match again.
6611 * Yeah, it may waste some memory, but will be removed
6612 * if/when the hash is modified again.
6613 */
6614 if (entry)
6615 entry->ip = 0;
6616 }
6617}
6618
Ingo Molnarf2cc0202021-03-23 18:49:35 +01006619/* Clear any records from hashes */
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006620static void clear_mod_from_hashes(struct ftrace_page *pg)
6621{
6622 struct trace_array *tr;
6623
6624 mutex_lock(&trace_types_lock);
6625 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6626 if (!tr->ops || !tr->ops->func_hash)
6627 continue;
6628 mutex_lock(&tr->ops->func_hash->regex_lock);
6629 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6630 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6631 mutex_unlock(&tr->ops->func_hash->regex_lock);
6632 }
6633 mutex_unlock(&trace_types_lock);
6634}
6635
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006636static void ftrace_free_mod_map(struct rcu_head *rcu)
6637{
6638 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6639 struct ftrace_mod_func *mod_func;
6640 struct ftrace_mod_func *n;
6641
6642 /* All the contents of mod_map are now not visible to readers */
6643 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6644 kfree(mod_func->name);
6645 list_del(&mod_func->list);
6646 kfree(mod_func);
6647 }
6648
6649 kfree(mod_map);
6650}
6651
jolsa@redhat.come7247a12009-10-07 19:00:35 +02006652void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04006653{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006654 struct ftrace_mod_map *mod_map;
6655 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04006656 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05006657 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006658 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04006659 struct ftrace_page *pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04006660
Steven Rostedt93eb6772009-04-15 13:24:06 -04006661 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04006662
6663 if (ftrace_disabled)
6664 goto out_unlock;
6665
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006666 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6667 if (mod_map->mod == mod) {
6668 list_del_rcu(&mod_map->list);
Paul E. McKenney74401722018-11-06 18:44:52 -08006669 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006670 break;
6671 }
6672 }
6673
Steven Rostedt320823092011-12-16 14:42:37 -05006674 /*
6675 * Each module has its own ftrace_pages, remove
6676 * them from the list.
6677 */
6678 last_pg = &ftrace_pages_start;
6679 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6680 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05006681 if (within_module_core(rec->ip, mod) ||
6682 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04006683 /*
Steven Rostedt320823092011-12-16 14:42:37 -05006684 * As core pages are first, the first
6685 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04006686 */
Steven Rostedt320823092011-12-16 14:42:37 -05006687 if (WARN_ON(pg == ftrace_pages_start))
6688 goto out_unlock;
6689
6690 /* Check if we are deleting the last page */
6691 if (pg == ftrace_pages)
6692 ftrace_pages = next_to_ftrace_page(last_pg);
6693
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04006694 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05006695 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006696
6697 pg->next = tmp_page;
6698 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05006699 } else
6700 last_pg = &pg->next;
6701 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04006702 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04006703 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006704
6705 for (pg = tmp_page; pg; pg = tmp_page) {
6706
6707 /* Needs to be called outside of ftrace_lock */
6708 clear_mod_from_hashes(pg);
6709
Linus Torvaldsdb425232021-04-01 16:14:17 -04006710 if (pg->records) {
6711 free_pages((unsigned long)pg->records, pg->order);
6712 ftrace_number_of_pages -= 1 << pg->order;
6713 }
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006714 tmp_page = pg->next;
6715 kfree(pg);
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04006716 ftrace_number_of_groups--;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006717 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04006718}
6719
Jessica Yu7dcd1822016-02-16 17:32:33 -05006720void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006721{
6722 struct dyn_ftrace *rec;
6723 struct ftrace_page *pg;
6724
6725 mutex_lock(&ftrace_lock);
6726
6727 if (ftrace_disabled)
6728 goto out_unlock;
6729
6730 /*
6731 * If the tracing is enabled, go ahead and enable the record.
6732 *
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05306733 * The reason not to enable the record immediately is the
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006734 * inherent check of ftrace_make_nop/ftrace_make_call for
6735 * correct previous instructions. Making first the NOP
6736 * conversion puts the module to the correct state, thus
6737 * passing the ftrace_make_call check.
6738 *
6739 * We also delay this to after the module code already set the
6740 * text to read-only, as we now need to set it back to read-write
6741 * so that we can modify the text.
6742 */
6743 if (ftrace_start_up)
6744 ftrace_arch_code_modify_prepare();
6745
6746 do_for_each_ftrace_rec(pg, rec) {
6747 int cnt;
6748 /*
6749 * do_for_each_ftrace_rec() is a double loop.
6750 * module text shares the pg. If a record is
6751 * not part of this module, then skip this pg,
6752 * which the "break" will do.
6753 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05006754 if (!within_module_core(rec->ip, mod) &&
6755 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006756 break;
6757
6758 cnt = 0;
6759
6760 /*
6761 * When adding a module, we need to check if tracers are
6762 * currently enabled and if they are, and can trace this record,
6763 * we need to enable the module functions as well as update the
6764 * reference counts for those function records.
6765 */
6766 if (ftrace_start_up)
6767 cnt += referenced_filters(rec);
6768
Chengming Zhou8a224ff2020-07-29 02:05:53 +08006769 rec->flags &= ~FTRACE_FL_DISABLED;
6770 rec->flags += cnt;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006771
6772 if (ftrace_start_up && cnt) {
6773 int failed = __ftrace_replace_code(rec, 1);
6774 if (failed) {
6775 ftrace_bug(failed, rec);
6776 goto out_loop;
6777 }
6778 }
6779
6780 } while_for_each_ftrace_rec();
6781
6782 out_loop:
6783 if (ftrace_start_up)
6784 ftrace_arch_code_modify_post_process();
6785
6786 out_unlock:
6787 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04006788
6789 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006790}
6791
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04006792void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04006793{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05006794 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01006795 return;
6796
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05006797 ftrace_process_locs(mod, mod->ftrace_callsites,
6798 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05006799}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006800
6801static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6802 struct dyn_ftrace *rec)
6803{
6804 struct ftrace_mod_func *mod_func;
6805 unsigned long symsize;
6806 unsigned long offset;
6807 char str[KSYM_SYMBOL_LEN];
6808 char *modname;
6809 const char *ret;
6810
6811 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
6812 if (!ret)
6813 return;
6814
6815 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
6816 if (!mod_func)
6817 return;
6818
6819 mod_func->name = kstrdup(str, GFP_KERNEL);
6820 if (!mod_func->name) {
6821 kfree(mod_func);
6822 return;
6823 }
6824
6825 mod_func->ip = rec->ip - offset;
6826 mod_func->size = symsize;
6827
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006828 mod_map->num_funcs++;
6829
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006830 list_add_rcu(&mod_func->list, &mod_map->funcs);
6831}
6832
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006833static struct ftrace_mod_map *
6834allocate_ftrace_mod_map(struct module *mod,
6835 unsigned long start, unsigned long end)
6836{
6837 struct ftrace_mod_map *mod_map;
6838
6839 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
6840 if (!mod_map)
6841 return NULL;
6842
6843 mod_map->mod = mod;
6844 mod_map->start_addr = start;
6845 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006846 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006847
6848 INIT_LIST_HEAD_RCU(&mod_map->funcs);
6849
6850 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
6851
6852 return mod_map;
6853}
6854
6855static const char *
6856ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
6857 unsigned long addr, unsigned long *size,
6858 unsigned long *off, char *sym)
6859{
6860 struct ftrace_mod_func *found_func = NULL;
6861 struct ftrace_mod_func *mod_func;
6862
6863 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6864 if (addr >= mod_func->ip &&
6865 addr < mod_func->ip + mod_func->size) {
6866 found_func = mod_func;
6867 break;
6868 }
6869 }
6870
6871 if (found_func) {
6872 if (size)
6873 *size = found_func->size;
6874 if (off)
6875 *off = addr - found_func->ip;
6876 if (sym)
6877 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6878
6879 return found_func->name;
6880 }
6881
6882 return NULL;
6883}
6884
6885const char *
6886ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6887 unsigned long *off, char **modname, char *sym)
6888{
6889 struct ftrace_mod_map *mod_map;
6890 const char *ret = NULL;
6891
Paul E. McKenney74401722018-11-06 18:44:52 -08006892 /* mod_map is freed via call_rcu() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006893 preempt_disable();
6894 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6895 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6896 if (ret) {
6897 if (modname)
6898 *modname = mod_map->mod->name;
6899 break;
6900 }
6901 }
6902 preempt_enable();
6903
6904 return ret;
6905}
6906
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006907int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6908 char *type, char *name,
6909 char *module_name, int *exported)
6910{
6911 struct ftrace_mod_map *mod_map;
6912 struct ftrace_mod_func *mod_func;
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006913 int ret;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006914
6915 preempt_disable();
6916 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6917
6918 if (symnum >= mod_map->num_funcs) {
6919 symnum -= mod_map->num_funcs;
6920 continue;
6921 }
6922
6923 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6924 if (symnum > 1) {
6925 symnum--;
6926 continue;
6927 }
6928
6929 *value = mod_func->ip;
6930 *type = 'T';
6931 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6932 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6933 *exported = 1;
6934 preempt_enable();
6935 return 0;
6936 }
6937 WARN_ON(1);
6938 break;
6939 }
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006940 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6941 module_name, exported);
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006942 preempt_enable();
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006943 return ret;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006944}
6945
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006946#else
6947static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6948 struct dyn_ftrace *rec) { }
6949static inline struct ftrace_mod_map *
6950allocate_ftrace_mod_map(struct module *mod,
6951 unsigned long start, unsigned long end)
6952{
6953 return NULL;
6954}
Adrian Hunterfc0ea792020-05-12 15:19:13 +03006955int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6956 char *type, char *name, char *module_name,
6957 int *exported)
6958{
6959 int ret;
6960
6961 preempt_disable();
6962 ret = ftrace_get_trampoline_kallsym(symnum, value, type, name,
6963 module_name, exported);
6964 preempt_enable();
6965 return ret;
6966}
Steven Rostedt93eb6772009-04-15 13:24:06 -04006967#endif /* CONFIG_MODULES */
6968
Joel Fernandes8715b102017-10-09 12:29:31 -07006969struct ftrace_init_func {
6970 struct list_head list;
6971 unsigned long ip;
6972};
6973
6974/* Clear any init ips from hashes */
6975static void
6976clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006977{
Joel Fernandes8715b102017-10-09 12:29:31 -07006978 struct ftrace_func_entry *entry;
6979
Changbin Du08468752019-09-10 22:33:36 +08006980 entry = ftrace_lookup_ip(hash, func->ip);
Joel Fernandes8715b102017-10-09 12:29:31 -07006981 /*
6982 * Do not allow this rec to match again.
6983 * Yeah, it may waste some memory, but will be removed
6984 * if/when the hash is modified again.
6985 */
6986 if (entry)
6987 entry->ip = 0;
6988}
6989
6990static void
6991clear_func_from_hashes(struct ftrace_init_func *func)
6992{
6993 struct trace_array *tr;
6994
6995 mutex_lock(&trace_types_lock);
6996 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6997 if (!tr->ops || !tr->ops->func_hash)
6998 continue;
6999 mutex_lock(&tr->ops->func_hash->regex_lock);
7000 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
7001 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
7002 mutex_unlock(&tr->ops->func_hash->regex_lock);
7003 }
7004 mutex_unlock(&trace_types_lock);
7005}
7006
7007static void add_to_clear_hash_list(struct list_head *clear_list,
7008 struct dyn_ftrace *rec)
7009{
7010 struct ftrace_init_func *func;
7011
7012 func = kmalloc(sizeof(*func), GFP_KERNEL);
7013 if (!func) {
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05007014 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
Joel Fernandes8715b102017-10-09 12:29:31 -07007015 return;
7016 }
7017
7018 func->ip = rec->ip;
7019 list_add(&func->list, clear_list);
7020}
7021
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04007022void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05007023{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04007024 unsigned long start = (unsigned long)(start_ptr);
7025 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05007026 struct ftrace_page **last_pg = &ftrace_pages_start;
7027 struct ftrace_page *pg;
7028 struct dyn_ftrace *rec;
7029 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04007030 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07007031 struct ftrace_init_func *func, *func_next;
7032 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05007033
Joel Fernandes8715b102017-10-09 12:29:31 -07007034 INIT_LIST_HEAD(&clear_hash);
7035
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05007036 key.ip = start;
7037 key.flags = end; /* overload flags, as it is unsigned long */
7038
7039 mutex_lock(&ftrace_lock);
7040
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04007041 /*
7042 * If we are freeing module init memory, then check if
7043 * any tracer is active. If so, we need to save a mapping of
7044 * the module functions being freed with the address.
7045 */
7046 if (mod && ftrace_ops_list != &ftrace_list_end)
7047 mod_map = allocate_ftrace_mod_map(mod, start, end);
7048
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05007049 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
7050 if (end < pg->records[0].ip ||
7051 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
7052 continue;
7053 again:
7054 rec = bsearch(&key, pg->records, pg->index,
7055 sizeof(struct dyn_ftrace),
7056 ftrace_cmp_recs);
7057 if (!rec)
7058 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04007059
Joel Fernandes8715b102017-10-09 12:29:31 -07007060 /* rec will be cleared from hashes after ftrace_lock unlock */
7061 add_to_clear_hash_list(&clear_hash, rec);
7062
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04007063 if (mod_map)
7064 save_ftrace_mod_rec(mod_map, rec);
7065
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05007066 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04007067 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05007068 if (!pg->index) {
7069 *last_pg = pg->next;
Linus Torvaldsdb425232021-04-01 16:14:17 -04007070 if (pg->records) {
7071 free_pages((unsigned long)pg->records, pg->order);
7072 ftrace_number_of_pages -= 1 << pg->order;
7073 }
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04007074 ftrace_number_of_groups--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05007075 kfree(pg);
7076 pg = container_of(last_pg, struct ftrace_page, next);
7077 if (!(*last_pg))
7078 ftrace_pages = pg;
7079 continue;
7080 }
7081 memmove(rec, rec + 1,
7082 (pg->index - (rec - pg->records)) * sizeof(*rec));
7083 /* More than one function may be in this block */
7084 goto again;
7085 }
7086 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07007087
7088 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
7089 clear_func_from_hashes(func);
7090 kfree(func);
7091 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05007092}
7093
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04007094void __init ftrace_free_init_mem(void)
7095{
7096 void *start = (void *)(&__init_begin);
7097 void *end = (void *)(&__init_end);
7098
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04007099 ftrace_free_mem(NULL, start, end);
Steven Rostedt93eb6772009-04-15 13:24:06 -04007100}
7101
Weizhao Ouyang6644c652021-09-09 17:02:16 +08007102int __init __weak ftrace_dyn_arch_init(void)
7103{
7104 return 0;
7105}
7106
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007107void __init ftrace_init(void)
7108{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01007109 extern unsigned long __start_mcount_loc[];
7110 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01007111 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007112 int ret;
7113
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007114 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01007115 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007116 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01007117 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007118 goto failed;
7119
7120 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01007121 if (!count) {
7122 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007123 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01007124 }
7125
7126 pr_info("ftrace: allocating %ld entries in %ld pages\n",
7127 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007128
7129 last_ftrace_enabled = ftrace_enabled = 1;
7130
Jiri Olsa5cb084b2009-10-13 16:33:53 -04007131 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08007132 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007133 __stop_mcount_loc);
7134
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04007135 pr_info("ftrace: allocated %ld pages with %ld groups\n",
7136 ftrace_number_of_pages, ftrace_number_of_groups);
7137
Steven Rostedt2af15d62009-05-28 13:37:24 -04007138 set_ftrace_early_filters();
7139
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007140 return;
7141 failed:
7142 ftrace_disabled = 1;
7143}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04007144
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04007145/* Do nothing if arch does not support this */
7146void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
7147{
7148}
7149
7150static void ftrace_update_trampoline(struct ftrace_ops *ops)
7151{
Adrian Hunterfc0ea792020-05-12 15:19:13 +03007152 unsigned long trampoline = ops->trampoline;
7153
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04007154 arch_ftrace_update_trampoline(ops);
Adrian Hunterfc0ea792020-05-12 15:19:13 +03007155 if (ops->trampoline && ops->trampoline != trampoline &&
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03007156 (ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP)) {
7157 /* Add to kallsyms before the perf events */
Adrian Hunterfc0ea792020-05-12 15:19:13 +03007158 ftrace_add_trampoline_to_kallsyms(ops);
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03007159 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
7160 ops->trampoline, ops->trampoline_size, false,
7161 FTRACE_TRAMPOLINE_SYM);
Adrian Hunter548e1f62020-05-12 15:19:15 +03007162 /*
7163 * Record the perf text poke event after the ksymbol register
7164 * event.
7165 */
7166 perf_event_text_poke((void *)ops->trampoline, NULL, 0,
7167 (void *)ops->trampoline,
7168 ops->trampoline_size);
Adrian Hunterdd9ddf42020-05-12 15:19:14 +03007169 }
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04007170}
7171
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007172void ftrace_init_trace_array(struct trace_array *tr)
7173{
7174 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04007175 INIT_LIST_HEAD(&tr->mod_trace);
7176 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007177}
Steven Rostedt3d083392008-05-12 21:20:42 +02007178#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01007179
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05007180struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04007181 .func = ftrace_stub,
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05007182 .flags = FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04007183 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04007184};
7185
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01007186static int __init ftrace_nodyn_init(void)
7187{
7188 ftrace_enabled = 1;
7189 return 0;
7190}
Steven Rostedt6f415672012-10-05 12:13:07 -04007191core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01007192
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05007193static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007194static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04007195static inline void ftrace_startup_all(int command) { }
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05007196
Ingo Molnarc7aafc52008-05-12 21:20:45 +02007197# define ftrace_startup_sysctl() do { } while (0)
7198# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04007199
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04007200static void ftrace_update_trampoline(struct ftrace_ops *ops)
7201{
7202}
7203
Steven Rostedt3d083392008-05-12 21:20:42 +02007204#endif /* CONFIG_DYNAMIC_FTRACE */
7205
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007206__init void ftrace_init_global_array_ops(struct trace_array *tr)
7207{
7208 tr->ops = &global_ops;
7209 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04007210 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007211}
7212
7213void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
7214{
7215 /* If we filter on pids, update to use the pid function */
7216 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
7217 if (WARN_ON(tr->ops->func != ftrace_stub))
7218 printk("ftrace ops had %pS for function\n",
7219 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007220 }
7221 tr->ops->func = func;
7222 tr->ops->private = tr;
7223}
7224
7225void ftrace_reset_array_ops(struct trace_array *tr)
7226{
7227 tr->ops->func = ftrace_stub;
7228}
7229
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09007230static nokprobe_inline void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007231__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007232 struct ftrace_ops *ignored, struct ftrace_regs *fregs)
Steven Rostedtb8489142011-05-04 09:27:52 -04007233{
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007234 struct pt_regs *regs = ftrace_get_regs(fregs);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04007235 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04007236 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04007237
王贇ce5e4802021-10-27 11:14:44 +08007238 /*
7239 * The ftrace_test_and_set_recursion() will disable preemption,
7240 * which is required since some of the ops may be dynamically
7241 * allocated, they must be freed after a synchronize_rcu().
7242 */
Steven Rostedt (VMware)ed65df62021-10-18 15:44:12 -04007243 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
Steven Rostedtedc15ca2012-11-02 17:47:21 -04007244 if (bit < 0)
7245 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04007246
Steven Rostedt0a016402012-11-02 17:03:03 -04007247 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (VMware)2fa717a2019-04-11 11:46:13 -04007248 /* Stub functions don't need to be called nor tested */
7249 if (op->flags & FTRACE_OPS_FL_STUB)
7250 continue;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05007251 /*
7252 * Check the following for each ops before calling their func:
7253 * if RCU flag is set, then rcu_is_watching() must be true
7254 * if PER_CPU is set, then ftrace_function_local_disable()
7255 * must be false
7256 * Otherwise test if the ip matches the ops filter
7257 *
7258 * If any of the above fails then the op->func() is not executed.
7259 */
7260 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05007261 ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04007262 if (FTRACE_WARN_ON(!op->func)) {
7263 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007264 goto out;
7265 }
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007266 op->func(ip, parent_ip, op, fregs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007267 }
Steven Rostedt0a016402012-11-02 17:03:03 -04007268 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05007269out:
Steven Rostedtedc15ca2012-11-02 17:47:21 -04007270 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04007271}
7272
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007273/*
7274 * Some archs only support passing ip and parent_ip. Even though
7275 * the list function ignores the op parameter, we do not want any
7276 * C side effects, where a function is called without the caller
7277 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04007278 * Archs are to support both the regs and ftrace_ops at the same time.
7279 * If they support ftrace_ops, it is assumed they support regs.
7280 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09007281 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
7282 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04007283 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08007284 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt (VMware)34cdd182020-06-17 16:56:16 -04007285 *
7286 * In vmlinux.lds.h, ftrace_ops_list_func() is defined to be
7287 * arch_ftrace_ops_list_func.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007288 */
7289#if ARCH_SUPPORTS_FTRACE_OPS
Steven Rostedt (VMware)34cdd182020-06-17 16:56:16 -04007290void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
7291 struct ftrace_ops *op, struct ftrace_regs *fregs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007292{
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007293 __ftrace_ops_list_func(ip, parent_ip, NULL, fregs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007294}
7295#else
Steven Rostedt (VMware)34cdd182020-06-17 16:56:16 -04007296void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007297{
Steven Rostedta1e2e312011-08-09 12:50:46 -04007298 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007299}
7300#endif
Steven Rostedt (VMware)34cdd182020-06-17 16:56:16 -04007301NOKPROBE_SYMBOL(arch_ftrace_ops_list_func);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04007302
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007303/*
7304 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05007305 * recursion, needs RCU protection and/or requires per cpu handling, then
7306 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007307 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05007308static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007309 struct ftrace_ops *op, struct ftrace_regs *fregs)
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007310{
7311 int bit;
7312
Steven Rostedt (VMware)ed65df62021-10-18 15:44:12 -04007313 bit = trace_test_and_set_recursion(ip, parent_ip, TRACE_LIST_START);
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007314 if (bit < 0)
7315 return;
7316
Steven Rostedt (VMware)b40341f2020-09-29 12:40:31 -04007317 if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
Steven Rostedt (VMware)d19ad072020-10-28 17:42:17 -04007318 op->func(ip, parent_ip, op, fregs);
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05007319
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007320 trace_clear_recursion(bit);
7321}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09007322NOKPROBE_SYMBOL(ftrace_ops_assist_func);
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04007323
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04007324/**
7325 * ftrace_ops_get_func - get the function a trampoline should call
7326 * @ops: the ops to get the function for
7327 *
7328 * Normally the mcount trampoline will call the ops->func, but there
7329 * are times that it should not. For example, if the ops does not
7330 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08007331 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04007332 *
7333 * Returns the function that the trampoline should call for @ops.
7334 */
7335ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
7336{
7337 /*
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05007338 * If the function does not handle recursion or needs to be RCU safe,
7339 * then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04007340 */
Steven Rostedt (VMware)a25d0362020-11-05 21:32:45 -05007341 if (ops->flags & (FTRACE_OPS_FL_RECURSION |
7342 FTRACE_OPS_FL_RCU))
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05007343 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04007344
7345 return ops->func;
7346}
7347
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007348static void
7349ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
7350 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05007351{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007352 struct trace_array *tr = data;
7353 struct trace_pid_list *pid_list;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007354 struct trace_pid_list *no_pid_list;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007355
7356 pid_list = rcu_dereference_sched(tr->function_pids);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007357 no_pid_list = rcu_dereference_sched(tr->function_no_pids);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007358
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007359 if (trace_ignore_this_task(pid_list, no_pid_list, next))
Steven Rostedt (VMware)717e3f52020-03-19 23:40:40 -04007360 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7361 FTRACE_PID_IGNORE);
7362 else
7363 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7364 next->pid);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007365}
7366
Namhyung Kim1e104862017-04-17 11:44:28 +09007367static void
7368ftrace_pid_follow_sched_process_fork(void *data,
7369 struct task_struct *self,
7370 struct task_struct *task)
7371{
7372 struct trace_pid_list *pid_list;
7373 struct trace_array *tr = data;
7374
7375 pid_list = rcu_dereference_sched(tr->function_pids);
7376 trace_filter_add_remove_task(pid_list, self, task);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007377
7378 pid_list = rcu_dereference_sched(tr->function_no_pids);
7379 trace_filter_add_remove_task(pid_list, self, task);
Namhyung Kim1e104862017-04-17 11:44:28 +09007380}
7381
7382static void
7383ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
7384{
7385 struct trace_pid_list *pid_list;
7386 struct trace_array *tr = data;
7387
7388 pid_list = rcu_dereference_sched(tr->function_pids);
7389 trace_filter_add_remove_task(pid_list, NULL, task);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007390
7391 pid_list = rcu_dereference_sched(tr->function_no_pids);
7392 trace_filter_add_remove_task(pid_list, NULL, task);
Namhyung Kim1e104862017-04-17 11:44:28 +09007393}
7394
7395void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
7396{
7397 if (enable) {
7398 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7399 tr);
Steven Rostedt (VMware)afcab632020-08-04 20:00:02 -04007400 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
Namhyung Kim1e104862017-04-17 11:44:28 +09007401 tr);
7402 } else {
7403 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
7404 tr);
Steven Rostedt (VMware)afcab632020-08-04 20:00:02 -04007405 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
Namhyung Kim1e104862017-04-17 11:44:28 +09007406 tr);
7407 }
7408}
7409
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007410static void clear_ftrace_pids(struct trace_array *tr, int type)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007411{
7412 struct trace_pid_list *pid_list;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007413 struct trace_pid_list *no_pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05007414 int cpu;
7415
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007416 pid_list = rcu_dereference_protected(tr->function_pids,
7417 lockdep_is_held(&ftrace_lock));
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007418 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7419 lockdep_is_held(&ftrace_lock));
7420
7421 /* Make sure there's something to do */
Steven Rostedt (VMware)27683622020-03-25 19:51:19 -04007422 if (!pid_type_enabled(type, pid_list, no_pid_list))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007423 return;
7424
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007425 /* See if the pids still need to be checked after this */
Steven Rostedt (VMware)27683622020-03-25 19:51:19 -04007426 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007427 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7428 for_each_possible_cpu(cpu)
7429 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = FTRACE_PID_TRACE;
7430 }
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007431
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007432 if (type & TRACE_PIDS)
7433 rcu_assign_pointer(tr->function_pids, NULL);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007434
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007435 if (type & TRACE_NO_PIDS)
7436 rcu_assign_pointer(tr->function_no_pids, NULL);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007437
7438 /* Wait till all users are no longer using pid filtering */
Paul E. McKenney74401722018-11-06 18:44:52 -08007439 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007440
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007441 if ((type & TRACE_PIDS) && pid_list)
Steven Rostedt (VMware)6954e412021-09-23 21:03:49 -04007442 trace_pid_list_free(pid_list);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007443
7444 if ((type & TRACE_NO_PIDS) && no_pid_list)
Steven Rostedt (VMware)6954e412021-09-23 21:03:49 -04007445 trace_pid_list_free(no_pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05007446}
7447
Namhyung Kimd879d0b2017-04-17 11:44:27 +09007448void ftrace_clear_pids(struct trace_array *tr)
7449{
7450 mutex_lock(&ftrace_lock);
7451
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007452 clear_ftrace_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
Namhyung Kimd879d0b2017-04-17 11:44:27 +09007453
7454 mutex_unlock(&ftrace_lock);
7455}
7456
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007457static void ftrace_pid_reset(struct trace_array *tr, int type)
Steven Rostedte32d8952008-12-04 00:26:41 -05007458{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007459 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007460 clear_ftrace_pids(tr, type);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007461
7462 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04007463 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007464
7465 mutex_unlock(&ftrace_lock);
7466}
7467
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007468/* Greater than any max PID */
7469#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7470
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007471static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007472 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007473{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007474 struct trace_pid_list *pid_list;
7475 struct trace_array *tr = m->private;
7476
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007477 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007478 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007479
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007480 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007481
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007482 if (!pid_list)
7483 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7484
7485 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007486}
7487
7488static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7489{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007490 struct trace_array *tr = m->private;
7491 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7492
Vasily Averine4075e82020-01-24 10:02:56 +03007493 if (v == FTRACE_NO_PIDS) {
7494 (*pos)++;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007495 return NULL;
Vasily Averine4075e82020-01-24 10:02:56 +03007496 }
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007497 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007498}
7499
7500static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007501 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007502{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007503 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007504 mutex_unlock(&ftrace_lock);
7505}
7506
7507static int fpid_show(struct seq_file *m, void *v)
7508{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007509 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01007510 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007511 return 0;
7512 }
7513
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007514 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007515}
7516
7517static const struct seq_operations ftrace_pid_sops = {
7518 .start = fpid_start,
7519 .next = fpid_next,
7520 .stop = fpid_stop,
7521 .show = fpid_show,
7522};
7523
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007524static void *fnpid_start(struct seq_file *m, loff_t *pos)
7525 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007526{
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007527 struct trace_pid_list *pid_list;
7528 struct trace_array *tr = m->private;
7529
7530 mutex_lock(&ftrace_lock);
7531 rcu_read_lock_sched();
7532
7533 pid_list = rcu_dereference_sched(tr->function_no_pids);
7534
7535 if (!pid_list)
7536 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7537
7538 return trace_pid_start(pid_list, pos);
7539}
7540
7541static void *fnpid_next(struct seq_file *m, void *v, loff_t *pos)
7542{
7543 struct trace_array *tr = m->private;
7544 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_no_pids);
7545
7546 if (v == FTRACE_NO_PIDS) {
7547 (*pos)++;
7548 return NULL;
7549 }
7550 return trace_pid_next(pid_list, v, pos);
7551}
7552
7553static const struct seq_operations ftrace_no_pid_sops = {
7554 .start = fnpid_start,
7555 .next = fnpid_next,
7556 .stop = fpid_stop,
7557 .show = fpid_show,
7558};
7559
7560static int pid_open(struct inode *inode, struct file *file, int type)
7561{
7562 const struct seq_operations *seq_ops;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007563 struct trace_array *tr = inode->i_private;
7564 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007565 int ret = 0;
7566
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04007567 ret = tracing_check_open_get_tr(tr);
7568 if (ret)
7569 return ret;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007570
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007571 if ((file->f_mode & FMODE_WRITE) &&
7572 (file->f_flags & O_TRUNC))
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007573 ftrace_pid_reset(tr, type);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007574
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007575 switch (type) {
7576 case TRACE_PIDS:
7577 seq_ops = &ftrace_pid_sops;
7578 break;
7579 case TRACE_NO_PIDS:
7580 seq_ops = &ftrace_no_pid_sops;
7581 break;
Kaitao Cheng026bb842020-05-29 22:12:14 +08007582 default:
7583 trace_array_put(tr);
7584 WARN_ON_ONCE(1);
7585 return -EINVAL;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007586 }
7587
7588 ret = seq_open(file, seq_ops);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007589 if (ret < 0) {
7590 trace_array_put(tr);
7591 } else {
7592 m = file->private_data;
7593 /* copy tr over to seq ops */
7594 m->private = tr;
7595 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007596
7597 return ret;
7598}
7599
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007600static int
7601ftrace_pid_open(struct inode *inode, struct file *file)
7602{
7603 return pid_open(inode, file, TRACE_PIDS);
7604}
7605
7606static int
7607ftrace_no_pid_open(struct inode *inode, struct file *file)
7608{
7609 return pid_open(inode, file, TRACE_NO_PIDS);
7610}
7611
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007612static void ignore_task_cpu(void *data)
7613{
7614 struct trace_array *tr = data;
7615 struct trace_pid_list *pid_list;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007616 struct trace_pid_list *no_pid_list;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007617
7618 /*
7619 * This function is called by on_each_cpu() while the
7620 * event_mutex is held.
7621 */
7622 pid_list = rcu_dereference_protected(tr->function_pids,
7623 mutex_is_locked(&ftrace_lock));
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007624 no_pid_list = rcu_dereference_protected(tr->function_no_pids,
7625 mutex_is_locked(&ftrace_lock));
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007626
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007627 if (trace_ignore_this_task(pid_list, no_pid_list, current))
Steven Rostedt (VMware)717e3f52020-03-19 23:40:40 -04007628 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7629 FTRACE_PID_IGNORE);
7630 else
7631 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
7632 current->pid);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007633}
7634
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007635static ssize_t
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007636pid_write(struct file *filp, const char __user *ubuf,
7637 size_t cnt, loff_t *ppos, int type)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007638{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007639 struct seq_file *m = filp->private_data;
7640 struct trace_array *tr = m->private;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007641 struct trace_pid_list *filtered_pids;
7642 struct trace_pid_list *other_pids;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007643 struct trace_pid_list *pid_list;
7644 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007645
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007646 if (!cnt)
7647 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007648
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007649 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007650
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007651 switch (type) {
7652 case TRACE_PIDS:
7653 filtered_pids = rcu_dereference_protected(tr->function_pids,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007654 lockdep_is_held(&ftrace_lock));
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007655 other_pids = rcu_dereference_protected(tr->function_no_pids,
7656 lockdep_is_held(&ftrace_lock));
7657 break;
7658 case TRACE_NO_PIDS:
7659 filtered_pids = rcu_dereference_protected(tr->function_no_pids,
7660 lockdep_is_held(&ftrace_lock));
7661 other_pids = rcu_dereference_protected(tr->function_pids,
7662 lockdep_is_held(&ftrace_lock));
7663 break;
Kaitao Cheng026bb842020-05-29 22:12:14 +08007664 default:
7665 ret = -EINVAL;
7666 WARN_ON_ONCE(1);
7667 goto out;
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007668 }
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007669
7670 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7671 if (ret < 0)
7672 goto out;
7673
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007674 switch (type) {
7675 case TRACE_PIDS:
7676 rcu_assign_pointer(tr->function_pids, pid_list);
7677 break;
7678 case TRACE_NO_PIDS:
7679 rcu_assign_pointer(tr->function_no_pids, pid_list);
7680 break;
7681 }
7682
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007683
7684 if (filtered_pids) {
Paul E. McKenney74401722018-11-06 18:44:52 -08007685 synchronize_rcu();
Steven Rostedt (VMware)6954e412021-09-23 21:03:49 -04007686 trace_pid_list_free(filtered_pids);
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007687 } else if (pid_list && !other_pids) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007688 /* Register a probe to set whether to ignore the tracing of a task */
7689 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7690 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007691
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007692 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007693 * Ignoring of pids is done at task switch. But we have to
7694 * check for those tasks that are currently running.
7695 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007696 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007697 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007698
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007699 ftrace_update_pid_func();
7700 ftrace_startup_all(0);
7701 out:
7702 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007703
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007704 if (ret > 0)
7705 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05007706
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007707 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007708}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007709
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007710static ssize_t
7711ftrace_pid_write(struct file *filp, const char __user *ubuf,
7712 size_t cnt, loff_t *ppos)
7713{
7714 return pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
7715}
7716
7717static ssize_t
7718ftrace_no_pid_write(struct file *filp, const char __user *ubuf,
7719 size_t cnt, loff_t *ppos)
7720{
7721 return pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
7722}
7723
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007724static int
7725ftrace_pid_release(struct inode *inode, struct file *file)
7726{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007727 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007728
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007729 trace_array_put(tr);
7730
7731 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007732}
7733
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007734static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007735 .open = ftrace_pid_open,
7736 .write = ftrace_pid_write,
7737 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05007738 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007739 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007740};
7741
Steven Rostedt (VMware)b3b1e6e2020-03-19 23:19:06 -04007742static const struct file_operations ftrace_no_pid_fops = {
7743 .open = ftrace_no_pid_open,
7744 .write = ftrace_no_pid_write,
7745 .read = seq_read,
7746 .llseek = tracing_lseek,
7747 .release = ftrace_pid_release,
7748};
7749
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007750void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007751{
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04007752 trace_create_file("set_ftrace_pid", TRACE_MODE_WRITE, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007753 tr, &ftrace_pid_fops);
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04007754 trace_create_file("set_ftrace_notrace_pid", TRACE_MODE_WRITE,
7755 d_tracer, tr, &ftrace_no_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007756}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007757
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04007758void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
7759 struct dentry *d_tracer)
7760{
7761 /* Only the top level directory has the dyn_tracefs and profile */
7762 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
7763
7764 ftrace_init_dyn_tracefs(d_tracer);
7765 ftrace_profile_tracefs(d_tracer);
7766}
7767
Steven Rostedt3d083392008-05-12 21:20:42 +02007768/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04007769 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04007770 *
7771 * This function should be used by panic code. It stops ftrace
7772 * but in a not so nice way. If you need to simply kill ftrace
7773 * from a non-atomic section, use ftrace_kill.
7774 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04007775void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04007776{
7777 ftrace_disabled = 1;
7778 ftrace_enabled = 0;
Yisheng Xie5ccba642018-02-02 10:14:49 +08007779 ftrace_trace_function = ftrace_stub;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04007780}
7781
7782/**
Steven Rostedt (VMware)61307222021-10-29 09:52:23 -04007783 * ftrace_is_dead - Test if ftrace is dead or not.
7784 *
7785 * Returns 1 if ftrace is "dead", zero otherwise.
Steven Rostedte0a413f2011-09-29 21:26:16 -04007786 */
7787int ftrace_is_dead(void)
7788{
7789 return ftrace_disabled;
7790}
7791
7792/**
Steven Rostedt3d083392008-05-12 21:20:42 +02007793 * register_ftrace_function - register a function for profiling
7794 * @ops - ops structure that holds the function for profiling.
7795 *
7796 * Register a function to be called by all functions in the
7797 * kernel.
7798 *
7799 * Note: @ops->func and all the functions it calls must be labeled
7800 * with "notrace", otherwise it will go into a
7801 * recursive loop.
7802 */
7803int register_ftrace_function(struct ftrace_ops *ops)
7804{
Colin Ian King3b1a8f42021-07-21 13:09:15 +01007805 int ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02007806
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09007807 ftrace_ops_init(ops);
7808
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007809 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01007810
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05007811 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04007812
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007813 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02007814
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007815 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02007816}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04007817EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02007818
7819/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01007820 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02007821 * @ops - ops structure that holds the function to unregister
7822 *
7823 * Unregister a function that was added to be called by ftrace profiling.
7824 */
7825int unregister_ftrace_function(struct ftrace_ops *ops)
7826{
7827 int ret;
7828
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007829 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05007830 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007831 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007832
7833 return ret;
7834}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04007835EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007836
Miroslav Benes71624312019-10-16 13:33:13 +02007837static bool is_permanent_ops_registered(void)
7838{
7839 struct ftrace_ops *op;
7840
7841 do_for_each_ftrace_op(op, ftrace_ops_list) {
7842 if (op->flags & FTRACE_OPS_FL_PERMANENT)
7843 return true;
7844 } while_for_each_ftrace_op(op);
7845
7846 return false;
7847}
7848
Ingo Molnare309b412008-05-12 21:20:51 +02007849int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007850ftrace_enable_sysctl(struct ctl_table *table, int write,
Tobias Klauser54fa9ba2020-09-07 11:32:07 +02007851 void *buffer, size_t *lenp, loff_t *ppos)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007852{
Steven Rostedt45a4a232011-04-21 23:16:46 -04007853 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02007854
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007855 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007856
Steven Rostedt45a4a232011-04-21 23:16:46 -04007857 if (unlikely(ftrace_disabled))
7858 goto out;
7859
7860 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007861
Li Zefana32c7762009-06-26 16:55:51 +08007862 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007863 goto out;
7864
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007865 if (ftrace_enabled) {
7866
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007867 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08007868 if (rcu_dereference_protected(ftrace_ops_list,
7869 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01007870 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007871
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05007872 ftrace_startup_sysctl();
7873
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007874 } else {
Miroslav Benes71624312019-10-16 13:33:13 +02007875 if (is_permanent_ops_registered()) {
7876 ftrace_enabled = true;
7877 ret = -EBUSY;
7878 goto out;
7879 }
7880
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007881 /* stopping ftrace calls (just send to ftrace_stub) */
7882 ftrace_trace_function = ftrace_stub;
7883
7884 ftrace_shutdown_sysctl();
7885 }
7886
Miroslav Benes71624312019-10-16 13:33:13 +02007887 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007888 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007889 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02007890 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02007891}