blob: 3f7ee102868a21b2374735c8ea7492004d178ce7 [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 (Red Hat)395b97a2013-03-27 09:31:28 -040083 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040084 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040085};
86
Steven Rostedt4eebcc82008-05-12 21:20:48 +020087/* ftrace_enabled is a method to turn ftrace on or off */
88int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020089static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020090
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040091/* Current function tracing op */
92struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050093/* What to set function_trace_op to */
94static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050095
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040096static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040097{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040098 struct trace_array *tr;
99
100 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
101 return false;
102
103 tr = ops->private;
104
105 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400106}
107
108static void ftrace_update_trampoline(struct ftrace_ops *ops);
109
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200110/*
111 * ftrace_disabled is set when an anomaly is discovered.
112 * ftrace_disabled is much stronger than ftrace_enabled.
113 */
114static int ftrace_disabled __read_mostly;
115
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500116DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200117
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500118struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200119ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500120struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200121
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122#if ARCH_SUPPORTS_FTRACE_OPS
123static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400124 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400125#else
126/* See comment below, where ftrace_ops_list_func is defined */
127static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
128#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
129#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400130
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900131static inline void ftrace_ops_init(struct ftrace_ops *ops)
132{
133#ifdef CONFIG_DYNAMIC_FTRACE
134 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400135 mutex_init(&ops->local_hash.regex_lock);
136 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900137 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
138 }
139#endif
140}
141
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400142static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400143 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500144{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400145 struct trace_array *tr = op->private;
146
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500147 if (tr && this_cpu_read(tr->array_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500148 return;
149
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400150 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500151}
152
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500153static void ftrace_sync(struct work_struct *work)
154{
155 /*
156 * This function is just a stub to implement a hard force
Paul E. McKenney74401722018-11-06 18:44:52 -0800157 * of synchronize_rcu(). This requires synchronizing
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500158 * tasks even in userspace and idle.
159 *
160 * Yes, function tracing is rude.
161 */
162}
163
164static void ftrace_sync_ipi(void *data)
165{
166 /* Probably not needed, but do it anyway */
167 smp_rmb();
168}
169
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100170static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
171{
172 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500173 * 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 +0100174 * then it needs to call the list anyway.
175 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +0200176 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
177 FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100178 return ftrace_ops_list_func;
179
180 return ftrace_ops_get_func(ops);
181}
182
Steven Rostedt2b499382011-05-03 22:49:52 -0400183static void update_ftrace_function(void)
184{
185 ftrace_func_t func;
186
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400187 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400188 * Prepare the ftrace_ops that the arch callback will use.
189 * If there's only one ftrace_ops registered, the ftrace_ops_list
190 * will point to the ops we want.
191 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800192 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
193 lockdep_is_held(&ftrace_lock));
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400194
195 /* If there's no ftrace_ops registered, just call the stub function */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800196 if (set_function_trace_op == &ftrace_list_end) {
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400197 func = ftrace_stub;
198
199 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400200 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400201 * recursion safe and not dynamic and the arch supports passing ops,
202 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400203 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800204 } else if (rcu_dereference_protected(ftrace_ops_list->next,
205 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100206 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400207
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400208 } else {
209 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500210 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400211 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400212 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400213
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400214 update_function_graph_func();
215
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500216 /* If there's no change, then do nothing more here */
217 if (ftrace_trace_function == func)
218 return;
219
220 /*
221 * If we are using the list function, it doesn't care
222 * about the function_trace_ops.
223 */
224 if (func == ftrace_ops_list_func) {
225 ftrace_trace_function = func;
226 /*
227 * Don't even bother setting function_trace_ops,
228 * it would be racy to do so anyway.
229 */
230 return;
231 }
232
233#ifndef CONFIG_DYNAMIC_FTRACE
234 /*
235 * For static tracing, we need to be a bit more careful.
236 * The function change takes affect immediately. Thus,
237 * we need to coorditate the setting of the function_trace_ops
238 * with the setting of the ftrace_trace_function.
239 *
240 * Set the function to the list ops, which will call the
241 * function we want, albeit indirectly, but it handles the
242 * ftrace_ops and doesn't depend on function_trace_op.
243 */
244 ftrace_trace_function = ftrace_ops_list_func;
245 /*
246 * Make sure all CPUs see this. Yes this is slow, but static
247 * tracing is slow and nasty to have enabled.
248 */
249 schedule_on_each_cpu(ftrace_sync);
250 /* Now all cpus are using the list ops. */
251 function_trace_op = set_function_trace_op;
252 /* Make sure the function_trace_op is visible on all CPUs */
253 smp_wmb();
254 /* Nasty way to force a rmb on all cpus */
255 smp_call_function(ftrace_sync_ipi, NULL, 1);
256 /* OK, we are all set to update the ftrace_trace_function now! */
257#endif /* !CONFIG_DYNAMIC_FTRACE */
258
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400259 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400260}
261
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800262static void add_ftrace_ops(struct ftrace_ops __rcu **list,
263 struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200264{
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800265 rcu_assign_pointer(ops->next, *list);
266
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200267 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400268 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200269 * CPU might be walking that list. We need to make sure
270 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400271 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200272 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400273 rcu_assign_pointer(*list, ops);
274}
Steven Rostedt3d083392008-05-12 21:20:42 +0200275
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800276static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
277 struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400278{
279 struct ftrace_ops **p;
280
281 /*
282 * If we are removing the last function, then simply point
283 * to the ftrace_stub.
284 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800285 if (rcu_dereference_protected(*list,
286 lockdep_is_held(&ftrace_lock)) == ops &&
287 rcu_dereference_protected(ops->next,
288 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt2b499382011-05-03 22:49:52 -0400289 *list = &ftrace_list_end;
290 return 0;
291 }
292
293 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
294 if (*p == ops)
295 break;
296
297 if (*p != ops)
298 return -1;
299
300 *p = (*p)->next;
301 return 0;
302}
303
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400304static void ftrace_update_trampoline(struct ftrace_ops *ops);
305
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500306int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400307{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500308 if (ops->flags & FTRACE_OPS_FL_DELETED)
309 return -EINVAL;
310
Steven Rostedtb8489142011-05-04 09:27:52 -0400311 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
312 return -EBUSY;
313
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900314#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400315 /*
316 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
317 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
318 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
319 */
320 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
321 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
322 return -EINVAL;
323
324 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
325 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
326#endif
Miroslav Benes71624312019-10-16 13:33:13 +0200327 if (!ftrace_enabled && (ops->flags & FTRACE_OPS_FL_PERMANENT))
328 return -EBUSY;
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400329
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400330 if (!core_kernel_data((unsigned long)ops))
331 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
332
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500333 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400334
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400335 /* Always save the function, and reset at unregistering */
336 ops->saved_func = ops->func;
337
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400338 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400339 ops->func = ftrace_pid_func;
340
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400341 ftrace_update_trampoline(ops);
342
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400343 if (ftrace_enabled)
344 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200345
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200346 return 0;
347}
348
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500349int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200350{
Steven Rostedt2b499382011-05-03 22:49:52 -0400351 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200352
Steven Rostedtb8489142011-05-04 09:27:52 -0400353 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
354 return -EBUSY;
355
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500356 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400357
Steven Rostedt2b499382011-05-03 22:49:52 -0400358 if (ret < 0)
359 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400360
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400361 if (ftrace_enabled)
362 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200363
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400364 ops->func = ops->saved_func;
365
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500366 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200367}
368
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500369static void ftrace_update_pid_func(void)
370{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400371 struct ftrace_ops *op;
372
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400373 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500374 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900375 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500376
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400377 do_for_each_ftrace_op(op, ftrace_ops_list) {
378 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400379 op->func = ftrace_pids_enabled(op) ?
380 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400381 ftrace_update_trampoline(op);
382 }
383 } while_for_each_ftrace_op(op);
384
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400385 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500386}
387
Steven Rostedt493762f2009-03-23 17:12:36 -0400388#ifdef CONFIG_FUNCTION_PROFILER
389struct ftrace_profile {
390 struct hlist_node node;
391 unsigned long ip;
392 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400393#ifdef CONFIG_FUNCTION_GRAPH_TRACER
394 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400395 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400396#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400397};
398
399struct ftrace_profile_page {
400 struct ftrace_profile_page *next;
401 unsigned long index;
402 struct ftrace_profile records[];
403};
404
Steven Rostedtcafb1682009-03-24 20:50:39 -0400405struct ftrace_profile_stat {
406 atomic_t disabled;
407 struct hlist_head *hash;
408 struct ftrace_profile_page *pages;
409 struct ftrace_profile_page *start;
410 struct tracer_stat stat;
411};
412
Steven Rostedt493762f2009-03-23 17:12:36 -0400413#define PROFILE_RECORDS_SIZE \
414 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
415
416#define PROFILES_PER_PAGE \
417 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
418
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400419static int ftrace_profile_enabled __read_mostly;
420
421/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400422static DEFINE_MUTEX(ftrace_profile_lock);
423
Steven Rostedtcafb1682009-03-24 20:50:39 -0400424static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400425
Namhyung Kim20079eb2013-04-10 08:55:50 +0900426#define FTRACE_PROFILE_HASH_BITS 10
427#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400428
Steven Rostedt493762f2009-03-23 17:12:36 -0400429static void *
430function_stat_next(void *v, int idx)
431{
432 struct ftrace_profile *rec = v;
433 struct ftrace_profile_page *pg;
434
435 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
436
437 again:
Li Zefan0296e422009-06-26 11:15:37 +0800438 if (idx != 0)
439 rec++;
440
Steven Rostedt493762f2009-03-23 17:12:36 -0400441 if ((void *)rec >= (void *)&pg->records[pg->index]) {
442 pg = pg->next;
443 if (!pg)
444 return NULL;
445 rec = &pg->records[0];
446 if (!rec->counter)
447 goto again;
448 }
449
450 return rec;
451}
452
453static void *function_stat_start(struct tracer_stat *trace)
454{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400455 struct ftrace_profile_stat *stat =
456 container_of(trace, struct ftrace_profile_stat, stat);
457
458 if (!stat || !stat->start)
459 return NULL;
460
461 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400462}
463
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400464#ifdef CONFIG_FUNCTION_GRAPH_TRACER
465/* function graph compares on total time */
Andy Shevchenko80042c82019-10-07 16:56:56 +0300466static int function_stat_cmp(const void *p1, const void *p2)
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400467{
Andy Shevchenko80042c82019-10-07 16:56:56 +0300468 const struct ftrace_profile *a = p1;
469 const struct ftrace_profile *b = p2;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400470
471 if (a->time < b->time)
472 return -1;
473 if (a->time > b->time)
474 return 1;
475 else
476 return 0;
477}
478#else
479/* not function graph compares against hits */
Andy Shevchenko80042c82019-10-07 16:56:56 +0300480static int function_stat_cmp(const void *p1, const void *p2)
Steven Rostedt493762f2009-03-23 17:12:36 -0400481{
Andy Shevchenko80042c82019-10-07 16:56:56 +0300482 const struct ftrace_profile *a = p1;
483 const struct ftrace_profile *b = p2;
Steven Rostedt493762f2009-03-23 17:12:36 -0400484
485 if (a->counter < b->counter)
486 return -1;
487 if (a->counter > b->counter)
488 return 1;
489 else
490 return 0;
491}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400492#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400493
494static int function_stat_headers(struct seq_file *m)
495{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400496#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100497 seq_puts(m, " Function "
498 "Hit Time Avg s^2\n"
499 " -------- "
500 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400501#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100502 seq_puts(m, " Function Hit\n"
503 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400504#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400505 return 0;
506}
507
508static int function_stat_show(struct seq_file *m, void *v)
509{
510 struct ftrace_profile *rec = v;
511 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800512 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400513#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400514 static struct trace_seq s;
515 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400516 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400517#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800518 mutex_lock(&ftrace_profile_lock);
519
520 /* we raced with function_profile_reset() */
521 if (unlikely(rec->counter == 0)) {
522 ret = -EBUSY;
523 goto out;
524 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400525
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530526#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Wen Yange31f7932020-01-03 11:02:48 +0800527 avg = div64_ul(rec->time, rec->counter);
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530528 if (tracing_thresh && (avg < tracing_thresh))
529 goto out;
530#endif
531
Steven Rostedt493762f2009-03-23 17:12:36 -0400532 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400533 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400534
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400535#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100536 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400537
Chase Douglase330b3b2010-04-26 14:02:05 -0400538 /* Sample standard deviation (s^2) */
539 if (rec->counter <= 1)
540 stddev = 0;
541 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200542 /*
543 * Apply Welford's method:
544 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
545 */
546 stddev = rec->counter * rec->time_squared -
547 rec->time * rec->time;
548
Chase Douglase330b3b2010-04-26 14:02:05 -0400549 /*
550 * Divide only 1000 for ns^2 -> us^2 conversion.
551 * trace_print_graph_duration will divide 1000 again.
552 */
Wen Yange31f7932020-01-03 11:02:48 +0800553 stddev = div64_ul(stddev,
554 rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400555 }
556
Steven Rostedt34886c82009-03-25 21:00:47 -0400557 trace_seq_init(&s);
558 trace_print_graph_duration(rec->time, &s);
559 trace_seq_puts(&s, " ");
560 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400561 trace_seq_puts(&s, " ");
562 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400563 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400564#endif
565 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800566out:
567 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400568
Li Zefan3aaba202010-08-23 16:50:12 +0800569 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400570}
571
Steven Rostedtcafb1682009-03-24 20:50:39 -0400572static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400573{
574 struct ftrace_profile_page *pg;
575
Steven Rostedtcafb1682009-03-24 20:50:39 -0400576 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400577
578 while (pg) {
579 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
580 pg->index = 0;
581 pg = pg->next;
582 }
583
Steven Rostedtcafb1682009-03-24 20:50:39 -0400584 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400585 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
586}
587
Steven Rostedtcafb1682009-03-24 20:50:39 -0400588int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400589{
590 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400591 int functions;
592 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400593 int i;
594
595 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400596 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400597 return 0;
598
Steven Rostedtcafb1682009-03-24 20:50:39 -0400599 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
600 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400601 return -ENOMEM;
602
Steven Rostedt318e0a72009-03-25 20:06:34 -0400603#ifdef CONFIG_DYNAMIC_FTRACE
604 functions = ftrace_update_tot_cnt;
605#else
606 /*
607 * We do not know the number of functions that exist because
608 * dynamic tracing is what counts them. With past experience
609 * we have around 20K functions. That should be more than enough.
610 * It is highly unlikely we will execute every function in
611 * the kernel.
612 */
613 functions = 20000;
614#endif
615
Steven Rostedtcafb1682009-03-24 20:50:39 -0400616 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400617
Steven Rostedt318e0a72009-03-25 20:06:34 -0400618 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
619
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900620 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400621 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400622 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400623 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400624 pg = pg->next;
625 }
626
627 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400628
629 out_free:
630 pg = stat->start;
631 while (pg) {
632 unsigned long tmp = (unsigned long)pg;
633
634 pg = pg->next;
635 free_page(tmp);
636 }
637
Steven Rostedt318e0a72009-03-25 20:06:34 -0400638 stat->pages = NULL;
639 stat->start = NULL;
640
641 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400642}
643
Steven Rostedtcafb1682009-03-24 20:50:39 -0400644static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400645{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400647 int size;
648
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 stat = &per_cpu(ftrace_profile_stats, cpu);
650
651 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400652 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400653 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400654 return 0;
655 }
656
657 /*
658 * We are profiling all functions, but usually only a few thousand
659 * functions are hit. We'll make a hash of 1024 items.
660 */
661 size = FTRACE_PROFILE_HASH_SIZE;
662
Kees Cook6396bb22018-06-12 14:03:40 -0700663 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400664
Steven Rostedtcafb1682009-03-24 20:50:39 -0400665 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400666 return -ENOMEM;
667
Steven Rostedt318e0a72009-03-25 20:06:34 -0400668 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 if (ftrace_profile_pages_init(stat) < 0) {
670 kfree(stat->hash);
671 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400672 return -ENOMEM;
673 }
674
675 return 0;
676}
677
Steven Rostedtcafb1682009-03-24 20:50:39 -0400678static int ftrace_profile_init(void)
679{
680 int cpu;
681 int ret = 0;
682
Miao Xiec4602c12013-12-16 15:20:01 +0800683 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400684 ret = ftrace_profile_init_cpu(cpu);
685 if (ret)
686 break;
687 }
688
689 return ret;
690}
691
Steven Rostedt493762f2009-03-23 17:12:36 -0400692/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400693static struct ftrace_profile *
694ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400695{
696 struct ftrace_profile *rec;
697 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400698 unsigned long key;
699
Namhyung Kim20079eb2013-04-10 08:55:50 +0900700 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400702
703 if (hlist_empty(hhd))
704 return NULL;
705
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400706 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400707 if (rec->ip == ip)
708 return rec;
709 }
710
711 return NULL;
712}
713
Steven Rostedtcafb1682009-03-24 20:50:39 -0400714static void ftrace_add_profile(struct ftrace_profile_stat *stat,
715 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400716{
717 unsigned long key;
718
Namhyung Kim20079eb2013-04-10 08:55:50 +0900719 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400720 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400721}
722
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723/*
724 * The memory is already allocated, this simply finds a new record to use.
725 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400726static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400727ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400728{
729 struct ftrace_profile *rec = NULL;
730
Steven Rostedt318e0a72009-03-25 20:06:34 -0400731 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400732 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400733 goto out;
734
Steven Rostedt493762f2009-03-23 17:12:36 -0400735 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400736 * Try to find the function again since an NMI
737 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400738 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400740 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400742
Steven Rostedtcafb1682009-03-24 20:50:39 -0400743 if (stat->pages->index == PROFILES_PER_PAGE) {
744 if (!stat->pages->next)
745 goto out;
746 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 }
748
Steven Rostedtcafb1682009-03-24 20:50:39 -0400749 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400750 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400751 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400752
Steven Rostedt493762f2009-03-23 17:12:36 -0400753 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400755
756 return rec;
757}
758
Steven Rostedt493762f2009-03-23 17:12:36 -0400759static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400760function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400761 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400762{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400763 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400764 struct ftrace_profile *rec;
765 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400766
767 if (!ftrace_profile_enabled)
768 return;
769
Steven Rostedt493762f2009-03-23 17:12:36 -0400770 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771
Christoph Lameterbdffd892014-04-29 14:17:40 -0500772 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400773 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400774 goto out;
775
776 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400777 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400778 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400779 if (!rec)
780 goto out;
781 }
782
783 rec->counter++;
784 out:
785 local_irq_restore(flags);
786}
787
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400788#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500789static bool fgraph_graph_time = true;
790
791void ftrace_graph_graph_time_control(bool enable)
792{
793 fgraph_graph_time = enable;
794}
795
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400796static int profile_graph_entry(struct ftrace_graph_ent *trace)
797{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500798 struct ftrace_ret_stack *ret_stack;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900799
Steven Rostedta1e2e312011-08-09 12:50:46 -0400800 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900801
Steven Rostedt (VMware)a8f0f9e2017-08-17 16:37:25 -0400802 /* If function graph is shutting down, ret_stack can be NULL */
803 if (!current->ret_stack)
804 return 0;
805
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500806 ret_stack = ftrace_graph_get_ret_stack(current, 0);
807 if (ret_stack)
808 ret_stack->subtime = 0;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900809
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400810 return 1;
811}
812
813static void profile_graph_return(struct ftrace_graph_ret *trace)
814{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500815 struct ftrace_ret_stack *ret_stack;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400817 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400818 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400820
821 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500822 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400823 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 goto out;
825
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400826 /* If the calltime was zero'd ignore it */
827 if (!trace->calltime)
828 goto out;
829
Steven Rostedta2a16d62009-03-24 23:17:58 -0400830 calltime = trace->rettime - trace->calltime;
831
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400832 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400833
834 /* Append this call time to the parent time to subtract */
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500835 ret_stack = ftrace_graph_get_ret_stack(current, 1);
836 if (ret_stack)
837 ret_stack->subtime += calltime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400838
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500839 ret_stack = ftrace_graph_get_ret_stack(current, 0);
840 if (ret_stack && ret_stack->subtime < calltime)
841 calltime -= ret_stack->subtime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400842 else
843 calltime = 0;
844 }
845
Steven Rostedtcafb1682009-03-24 20:50:39 -0400846 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400847 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400848 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400849 rec->time_squared += calltime * calltime;
850 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400851
Steven Rostedtcafb1682009-03-24 20:50:39 -0400852 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400853 local_irq_restore(flags);
854}
855
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500856static struct fgraph_ops fprofiler_ops = {
857 .entryfunc = &profile_graph_entry,
858 .retfunc = &profile_graph_return,
859};
860
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400861static int register_ftrace_profiler(void)
862{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500863 return register_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400864}
865
866static void unregister_ftrace_profiler(void)
867{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500868 unregister_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400869}
870#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100871static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400872 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900873 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400874 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400875};
876
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400877static int register_ftrace_profiler(void)
878{
879 return register_ftrace_function(&ftrace_profile_ops);
880}
881
882static void unregister_ftrace_profiler(void)
883{
884 unregister_ftrace_function(&ftrace_profile_ops);
885}
886#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
887
Steven Rostedt493762f2009-03-23 17:12:36 -0400888static ssize_t
889ftrace_profile_write(struct file *filp, const char __user *ubuf,
890 size_t cnt, loff_t *ppos)
891{
892 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400893 int ret;
894
Peter Huewe22fe9b52011-06-07 21:58:27 +0200895 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
896 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400897 return ret;
898
899 val = !!val;
900
901 mutex_lock(&ftrace_profile_lock);
902 if (ftrace_profile_enabled ^ val) {
903 if (val) {
904 ret = ftrace_profile_init();
905 if (ret < 0) {
906 cnt = ret;
907 goto out;
908 }
909
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400910 ret = register_ftrace_profiler();
911 if (ret < 0) {
912 cnt = ret;
913 goto out;
914 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400915 ftrace_profile_enabled = 1;
916 } else {
917 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400918 /*
919 * unregister_ftrace_profiler calls stop_machine
Paul E. McKenney74401722018-11-06 18:44:52 -0800920 * so this acts like an synchronize_rcu.
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400921 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400922 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400923 }
924 }
925 out:
926 mutex_unlock(&ftrace_profile_lock);
927
Jiri Olsacf8517c2009-10-23 19:36:16 -0400928 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400929
930 return cnt;
931}
932
933static ssize_t
934ftrace_profile_read(struct file *filp, char __user *ubuf,
935 size_t cnt, loff_t *ppos)
936{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400937 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400938 int r;
939
940 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
941 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
942}
943
944static const struct file_operations ftrace_profile_fops = {
945 .open = tracing_open_generic,
946 .read = ftrace_profile_read,
947 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200948 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400949};
950
Steven Rostedtcafb1682009-03-24 20:50:39 -0400951/* used to initialize the real stat files */
952static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400953 .name = "functions",
954 .stat_start = function_stat_start,
955 .stat_next = function_stat_next,
956 .stat_cmp = function_stat_cmp,
957 .stat_headers = function_stat_headers,
958 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400959};
960
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500961static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400962{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400963 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400964 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400965 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400967 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400968
Steven Rostedtcafb1682009-03-24 20:50:39 -0400969 for_each_possible_cpu(cpu) {
970 stat = &per_cpu(ftrace_profile_stats, cpu);
971
Geliang Tang6363c6b2016-03-15 22:12:34 +0800972 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400973 if (!name) {
974 /*
975 * The files created are permanent, if something happens
976 * we still do not free memory.
977 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400978 WARN(1,
979 "Could not allocate stat file for cpu %d\n",
980 cpu);
981 return;
982 }
983 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400984 stat->stat.name = name;
985 ret = register_stat_tracer(&stat->stat);
986 if (ret) {
987 WARN(1,
988 "Could not register function stat for cpu %d\n",
989 cpu);
990 kfree(name);
991 return;
992 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400993 }
994
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500995 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -0400996 d_tracer, NULL, &ftrace_profile_fops);
997 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700998 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -0400999}
1000
1001#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001002static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001003{
1004}
1005#endif /* CONFIG_FUNCTION_PROFILER */
1006
Steven Rostedt3d083392008-05-12 21:20:42 +02001007#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001008
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001009static struct ftrace_ops *removed_ops;
1010
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001011/*
1012 * Set when doing a global update, like enabling all recs or disabling them.
1013 * It is not set when just updating a single ftrace_ops.
1014 */
1015static bool update_all_ops;
1016
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001017#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001018# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001019#endif
1020
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001021struct ftrace_func_probe {
1022 struct ftrace_probe_ops *probe_ops;
1023 struct ftrace_ops ops;
1024 struct trace_array *tr;
1025 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001026 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001027 int ref;
1028};
1029
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001030/*
1031 * We make these constant because no one should touch them,
1032 * but they are used as the default "empty hash", to avoid allocating
1033 * it all the time. These are in a read only section such that if
1034 * anyone does try to modify it, it will cause an exception.
1035 */
1036static const struct hlist_head empty_buckets[1];
1037static const struct ftrace_hash empty_hash = {
1038 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001039};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001040#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001041
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001042struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001043 .func = ftrace_stub,
1044 .local_hash.notrace_hash = EMPTY_HASH,
1045 .local_hash.filter_hash = EMPTY_HASH,
1046 INIT_OPS_HASH(global_ops)
1047 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001048 FTRACE_OPS_FL_INITIALIZED |
1049 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001050};
1051
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001052/*
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001053 * Used by the stack undwinder to know about dynamic ftrace trampolines.
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001054 */
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001055struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001056{
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001057 struct ftrace_ops *op = NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001058
1059 /*
1060 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08001061 * they are freed after a synchronize_rcu().
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001062 */
1063 preempt_disable_notrace();
1064
1065 do_for_each_ftrace_op(op, ftrace_ops_list) {
1066 /*
1067 * This is to check for dynamically allocated trampolines.
1068 * Trampolines that are in kernel text will have
1069 * core_kernel_text() return true.
1070 */
1071 if (op->trampoline && op->trampoline_size)
1072 if (addr >= op->trampoline &&
1073 addr < op->trampoline + op->trampoline_size) {
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001074 preempt_enable_notrace();
1075 return op;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001076 }
1077 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001078 preempt_enable_notrace();
1079
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001080 return NULL;
1081}
1082
1083/*
1084 * This is used by __kernel_text_address() to return true if the
1085 * address is on a dynamically allocated trampoline that would
1086 * not return true for either core_kernel_text() or
1087 * is_module_text_address().
1088 */
1089bool is_ftrace_trampoline(unsigned long addr)
1090{
1091 return ftrace_ops_trampoline(addr) != NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001092}
1093
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001094struct ftrace_page {
1095 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001096 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001097 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001098 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001099};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001100
Steven Rostedta7900872011-12-16 16:23:44 -05001101#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1102#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001103
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001104static struct ftrace_page *ftrace_pages_start;
1105static struct ftrace_page *ftrace_pages;
1106
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001107static __always_inline unsigned long
1108ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1109{
1110 if (hash->size_bits > 0)
1111 return hash_long(ip, hash->size_bits);
1112
1113 return 0;
1114}
1115
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001116/* Only use this function if ftrace_hash_empty() has already been tested */
1117static __always_inline struct ftrace_func_entry *
1118__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001119{
1120 unsigned long key;
1121 struct ftrace_func_entry *entry;
1122 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001124 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001125 hhd = &hash->buckets[key];
1126
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001127 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001128 if (entry->ip == ip)
1129 return entry;
1130 }
1131 return NULL;
1132}
1133
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001134/**
1135 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1136 * @hash: The hash to look at
1137 * @ip: The instruction pointer to test
1138 *
1139 * Search a given @hash to see if a given instruction pointer (@ip)
1140 * exists in it.
1141 *
1142 * Returns the entry that holds the @ip if found. NULL otherwise.
1143 */
1144struct ftrace_func_entry *
1145ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1146{
1147 if (ftrace_hash_empty(hash))
1148 return NULL;
1149
1150 return __ftrace_lookup_ip(hash, ip);
1151}
1152
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001153static void __add_hash_entry(struct ftrace_hash *hash,
1154 struct ftrace_func_entry *entry)
1155{
1156 struct hlist_head *hhd;
1157 unsigned long key;
1158
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001159 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001160 hhd = &hash->buckets[key];
1161 hlist_add_head(&entry->hlist, hhd);
1162 hash->count++;
1163}
1164
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001165static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1166{
1167 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001168
1169 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1170 if (!entry)
1171 return -ENOMEM;
1172
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001173 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001174 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001175
1176 return 0;
1177}
1178
1179static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001180free_hash_entry(struct ftrace_hash *hash,
1181 struct ftrace_func_entry *entry)
1182{
1183 hlist_del(&entry->hlist);
1184 kfree(entry);
1185 hash->count--;
1186}
1187
1188static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001189remove_hash_entry(struct ftrace_hash *hash,
1190 struct ftrace_func_entry *entry)
1191{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001192 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001193 hash->count--;
1194}
1195
1196static void ftrace_hash_clear(struct ftrace_hash *hash)
1197{
1198 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001199 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001200 struct ftrace_func_entry *entry;
1201 int size = 1 << hash->size_bits;
1202 int i;
1203
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001204 if (!hash->count)
1205 return;
1206
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001207 for (i = 0; i < size; i++) {
1208 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001210 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001211 }
1212 FTRACE_WARN_ON(hash->count);
1213}
1214
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001215static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1216{
1217 list_del(&ftrace_mod->list);
1218 kfree(ftrace_mod->module);
1219 kfree(ftrace_mod->func);
1220 kfree(ftrace_mod);
1221}
1222
1223static void clear_ftrace_mod_list(struct list_head *head)
1224{
1225 struct ftrace_mod_load *p, *n;
1226
1227 /* stack tracer isn't supported yet */
1228 if (!head)
1229 return;
1230
1231 mutex_lock(&ftrace_lock);
1232 list_for_each_entry_safe(p, n, head, list)
1233 free_ftrace_mod(p);
1234 mutex_unlock(&ftrace_lock);
1235}
1236
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001237static void free_ftrace_hash(struct ftrace_hash *hash)
1238{
1239 if (!hash || hash == EMPTY_HASH)
1240 return;
1241 ftrace_hash_clear(hash);
1242 kfree(hash->buckets);
1243 kfree(hash);
1244}
1245
Steven Rostedt07fd5512011-05-05 18:03:47 -04001246static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1247{
1248 struct ftrace_hash *hash;
1249
1250 hash = container_of(rcu, struct ftrace_hash, rcu);
1251 free_ftrace_hash(hash);
1252}
1253
1254static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1255{
1256 if (!hash || hash == EMPTY_HASH)
1257 return;
Paul E. McKenney74401722018-11-06 18:44:52 -08001258 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001259}
1260
Jiri Olsa5500fa52012-02-15 15:51:54 +01001261void ftrace_free_filter(struct ftrace_ops *ops)
1262{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001263 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001264 free_ftrace_hash(ops->func_hash->filter_hash);
1265 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001266}
1267
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001268static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1269{
1270 struct ftrace_hash *hash;
1271 int size;
1272
1273 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1274 if (!hash)
1275 return NULL;
1276
1277 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001278 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001279
1280 if (!hash->buckets) {
1281 kfree(hash);
1282 return NULL;
1283 }
1284
1285 hash->size_bits = size_bits;
1286
1287 return hash;
1288}
1289
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001290
1291static int ftrace_add_mod(struct trace_array *tr,
1292 const char *func, const char *module,
1293 int enable)
1294{
1295 struct ftrace_mod_load *ftrace_mod;
1296 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1297
1298 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1299 if (!ftrace_mod)
1300 return -ENOMEM;
1301
1302 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1303 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1304 ftrace_mod->enable = enable;
1305
1306 if (!ftrace_mod->func || !ftrace_mod->module)
1307 goto out_free;
1308
1309 list_add(&ftrace_mod->list, mod_head);
1310
1311 return 0;
1312
1313 out_free:
1314 free_ftrace_mod(ftrace_mod);
1315
1316 return -ENOMEM;
1317}
1318
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319static struct ftrace_hash *
1320alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1321{
1322 struct ftrace_func_entry *entry;
1323 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001324 int size;
1325 int ret;
1326 int i;
1327
1328 new_hash = alloc_ftrace_hash(size_bits);
1329 if (!new_hash)
1330 return NULL;
1331
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001332 if (hash)
1333 new_hash->flags = hash->flags;
1334
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001335 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001336 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 return new_hash;
1338
1339 size = 1 << hash->size_bits;
1340 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001341 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342 ret = add_hash_entry(new_hash, entry->ip);
1343 if (ret < 0)
1344 goto free_hash;
1345 }
1346 }
1347
1348 FTRACE_WARN_ON(new_hash->count != hash->count);
1349
1350 return new_hash;
1351
1352 free_hash:
1353 free_ftrace_hash(new_hash);
1354 return NULL;
1355}
1356
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001357static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001358ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001359static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001360ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001361
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001362static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1363 struct ftrace_hash *new_hash);
1364
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001365static struct ftrace_hash *dup_hash(struct ftrace_hash *src, int size)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001366{
1367 struct ftrace_func_entry *entry;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001368 struct ftrace_hash *new_hash;
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001369 struct hlist_head *hhd;
1370 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001371 int bits = 0;
1372 int i;
1373
1374 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001375 * Make the hash size about 1/2 the # found
1376 */
1377 for (size /= 2; size; size >>= 1)
1378 bits++;
1379
1380 /* Don't allocate too much */
1381 if (bits > FTRACE_HASH_MAX_BITS)
1382 bits = FTRACE_HASH_MAX_BITS;
1383
Steven Rostedt07fd5512011-05-05 18:03:47 -04001384 new_hash = alloc_ftrace_hash(bits);
1385 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001386 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001387
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001388 new_hash->flags = src->flags;
1389
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001390 size = 1 << src->size_bits;
1391 for (i = 0; i < size; i++) {
1392 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001393 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001394 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001395 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001396 }
1397 }
Namhyung Kim3e278c02017-01-20 11:44:45 +09001398 return new_hash;
1399}
1400
Steven Rostedt (VMware)714641c2019-11-08 12:25:46 -05001401static struct ftrace_hash *
1402__ftrace_hash_move(struct ftrace_hash *src)
1403{
1404 int size = src->count;
1405
1406 /*
1407 * If the new source is empty, just return the empty_hash.
1408 */
1409 if (ftrace_hash_empty(src))
1410 return EMPTY_HASH;
1411
1412 return dup_hash(src, size);
1413}
1414
Namhyung Kim3e278c02017-01-20 11:44:45 +09001415static int
1416ftrace_hash_move(struct ftrace_ops *ops, int enable,
1417 struct ftrace_hash **dst, struct ftrace_hash *src)
1418{
1419 struct ftrace_hash *new_hash;
1420 int ret;
1421
1422 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1423 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1424 return -EINVAL;
1425
1426 new_hash = __ftrace_hash_move(src);
1427 if (!new_hash)
1428 return -ENOMEM;
1429
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001430 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1431 if (enable) {
1432 /* IPMODIFY should be updated only when filter_hash updating */
1433 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1434 if (ret < 0) {
1435 free_ftrace_hash(new_hash);
1436 return ret;
1437 }
1438 }
1439
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001440 /*
1441 * Remove the current set, update the hash and add
1442 * them back.
1443 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001444 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001445
Steven Rostedt07fd5512011-05-05 18:03:47 -04001446 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001447
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001448 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001449
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001450 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001451}
1452
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001453static bool hash_contains_ip(unsigned long ip,
1454 struct ftrace_ops_hash *hash)
1455{
1456 /*
1457 * The function record is a match if it exists in the filter
1458 * hash and not in the notrace hash. Note, an emty hash is
1459 * considered a match for the filter hash, but an empty
1460 * notrace hash is considered not in the notrace hash.
1461 */
1462 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001463 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001464 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001465 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001466}
1467
Steven Rostedt265c8312009-02-13 12:43:56 -05001468/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001469 * Test the hashes for this ops to see if we want to call
1470 * the ops->func or not.
1471 *
1472 * It's a match if the ip is in the ops->filter_hash or
1473 * the filter_hash does not exist or is empty,
1474 * AND
1475 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001476 *
1477 * This needs to be called with preemption disabled as
Paul E. McKenney74401722018-11-06 18:44:52 -08001478 * the hashes are freed with call_rcu().
Steven Rostedtb8489142011-05-04 09:27:52 -04001479 */
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001480int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001481ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001482{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001483 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001484 int ret;
1485
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001486#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1487 /*
1488 * There's a small race when adding ops that the ftrace handler
1489 * that wants regs, may be called without them. We can not
1490 * allow that handler to be called if regs is NULL.
1491 */
1492 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1493 return 0;
1494#endif
1495
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001496 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1497 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001498
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001499 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001500 ret = 1;
1501 else
1502 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001503
1504 return ret;
1505}
1506
1507/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001508 * This is a double for. Do not use 'break' to break out of the loop,
1509 * you must use a goto.
1510 */
1511#define do_for_each_ftrace_rec(pg, rec) \
1512 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1513 int _____i; \
1514 for (_____i = 0; _____i < pg->index; _____i++) { \
1515 rec = &pg->records[_____i];
1516
1517#define while_for_each_ftrace_rec() \
1518 } \
1519 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301520
Steven Rostedt5855fea2011-12-16 19:27:42 -05001521
1522static int ftrace_cmp_recs(const void *a, const void *b)
1523{
Steven Rostedta650e022012-04-25 13:48:13 -04001524 const struct dyn_ftrace *key = a;
1525 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001526
Steven Rostedta650e022012-04-25 13:48:13 -04001527 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001528 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001529 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1530 return 1;
1531 return 0;
1532}
1533
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001534static struct dyn_ftrace *lookup_rec(unsigned long start, unsigned long end)
1535{
1536 struct ftrace_page *pg;
1537 struct dyn_ftrace *rec = NULL;
1538 struct dyn_ftrace key;
1539
1540 key.ip = start;
1541 key.flags = end; /* overload flags, as it is unsigned long */
1542
1543 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1544 if (end < pg->records[0].ip ||
1545 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1546 continue;
1547 rec = bsearch(&key, pg->records, pg->index,
1548 sizeof(struct dyn_ftrace),
1549 ftrace_cmp_recs);
1550 }
1551 return rec;
1552}
1553
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001554/**
1555 * ftrace_location_range - return the first address of a traced location
1556 * if it touches the given ip range
1557 * @start: start of range to search.
1558 * @end: end of range to search (inclusive). @end points to the last byte
1559 * to check.
1560 *
1561 * Returns rec->ip if the related ftrace location is a least partly within
1562 * the given address range. That is, the first address of the instruction
1563 * that is either a NOP or call to the function tracer. It checks the ftrace
1564 * internal tables to determine if the address belongs or not.
1565 */
1566unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001567{
Steven Rostedta650e022012-04-25 13:48:13 -04001568 struct dyn_ftrace *rec;
Steven Rostedta650e022012-04-25 13:48:13 -04001569
Steven Rostedt (VMware)7e16f582019-11-08 12:26:46 -05001570 rec = lookup_rec(start, end);
1571 if (rec)
1572 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001573
Steven Rostedt5855fea2011-12-16 19:27:42 -05001574 return 0;
1575}
1576
Steven Rostedtc88fd862011-08-16 09:53:39 -04001577/**
1578 * ftrace_location - return true if the ip giving is a traced location
1579 * @ip: the instruction pointer to check
1580 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001581 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001582 * That is, the instruction that is either a NOP or call to
1583 * the function tracer. It checks the ftrace internal tables to
1584 * determine if the address belongs or not.
1585 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001586unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001587{
Steven Rostedta650e022012-04-25 13:48:13 -04001588 return ftrace_location_range(ip, ip);
1589}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001590
Steven Rostedta650e022012-04-25 13:48:13 -04001591/**
1592 * ftrace_text_reserved - return true if range contains an ftrace location
1593 * @start: start of range to search
1594 * @end: end of range to search (inclusive). @end points to the last byte to check.
1595 *
1596 * Returns 1 if @start and @end contains a ftrace location.
1597 * That is, the instruction that is either a NOP or call to
1598 * the function tracer. It checks the ftrace internal tables to
1599 * determine if the address belongs or not.
1600 */
Sasha Levind88471c2013-01-09 18:09:20 -05001601int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001602{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001603 unsigned long ret;
1604
1605 ret = ftrace_location_range((unsigned long)start,
1606 (unsigned long)end);
1607
1608 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001609}
1610
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001611/* Test if ops registered to this rec needs regs */
1612static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1613{
1614 struct ftrace_ops *ops;
1615 bool keep_regs = false;
1616
1617 for (ops = ftrace_ops_list;
1618 ops != &ftrace_list_end; ops = ops->next) {
1619 /* pass rec in as regs to have non-NULL val */
1620 if (ftrace_ops_test(ops, rec->ip, rec)) {
1621 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1622 keep_regs = true;
1623 break;
1624 }
1625 }
1626 }
1627
1628 return keep_regs;
1629}
1630
Cheng Jiana1246922019-05-04 19:39:39 +08001631static struct ftrace_ops *
1632ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1633static struct ftrace_ops *
1634ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1635
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001636static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001637 int filter_hash,
1638 bool inc)
1639{
1640 struct ftrace_hash *hash;
1641 struct ftrace_hash *other_hash;
1642 struct ftrace_page *pg;
1643 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001644 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001645 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001646 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001647
1648 /* Only update if the ops has been registered */
1649 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001650 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001651
1652 /*
1653 * In the filter_hash case:
1654 * If the count is zero, we update all records.
1655 * Otherwise we just update the items in the hash.
1656 *
1657 * In the notrace_hash case:
1658 * We enable the update in the hash.
1659 * As disabling notrace means enabling the tracing,
1660 * and enabling notrace means disabling, the inc variable
1661 * gets inversed.
1662 */
1663 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001664 hash = ops->func_hash->filter_hash;
1665 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001666 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001667 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001668 } else {
1669 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001670 hash = ops->func_hash->notrace_hash;
1671 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001672 /*
1673 * If the notrace hash has no items,
1674 * then there's nothing to do.
1675 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001676 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001677 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001678 }
1679
1680 do_for_each_ftrace_rec(pg, rec) {
1681 int in_other_hash = 0;
1682 int in_hash = 0;
1683 int match = 0;
1684
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001685 if (rec->flags & FTRACE_FL_DISABLED)
1686 continue;
1687
Steven Rostedted926f92011-05-03 13:25:24 -04001688 if (all) {
1689 /*
1690 * Only the filter_hash affects all records.
1691 * Update if the record is not in the notrace hash.
1692 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001693 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001694 match = 1;
1695 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001696 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1697 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001698
1699 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001700 * If filter_hash is set, we want to match all functions
1701 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001702 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001703 * If filter_hash is not set, then we are decrementing.
1704 * That means we match anything that is in the hash
1705 * and also in the other_hash. That is, we need to turn
1706 * off functions in the other hash because they are disabled
1707 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001708 */
1709 if (filter_hash && in_hash && !in_other_hash)
1710 match = 1;
1711 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001712 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001713 match = 1;
1714 }
1715 if (!match)
1716 continue;
1717
1718 if (inc) {
1719 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001720 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001721 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001722
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05001723 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1724 rec->flags |= FTRACE_FL_DIRECT;
1725
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001726 /*
1727 * If there's only a single callback registered to a
1728 * function, and the ops has a trampoline registered
1729 * for it, then we can call it directly.
1730 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001731 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001732 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001733 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001734 /*
1735 * If we are adding another function callback
1736 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001737 * custom trampoline in use, then we need to go
1738 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001739 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001740 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001741
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001742 /*
1743 * If any ops wants regs saved for this function
1744 * then all ops will get saved regs.
1745 */
1746 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1747 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001748 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001749 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001750 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001751 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001752
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001753 /*
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05001754 * Only the internal direct_ops should have the
1755 * DIRECT flag set. Thus, if it is removing a
1756 * function, then that function should no longer
1757 * be direct.
1758 */
1759 if (ops->flags & FTRACE_OPS_FL_DIRECT)
1760 rec->flags &= ~FTRACE_FL_DIRECT;
1761
1762 /*
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001763 * If the rec had REGS enabled and the ops that is
1764 * being removed had REGS set, then see if there is
1765 * still any ops for this record that wants regs.
1766 * If not, we can stop recording them.
1767 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001768 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001769 rec->flags & FTRACE_FL_REGS &&
1770 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1771 if (!test_rec_ops_needs_regs(rec))
1772 rec->flags &= ~FTRACE_FL_REGS;
1773 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001774
1775 /*
Cheng Jiana1246922019-05-04 19:39:39 +08001776 * The TRAMP needs to be set only if rec count
1777 * is decremented to one, and the ops that is
1778 * left has a trampoline. As TRAMP can only be
1779 * enabled if there is only a single ops attached
1780 * to it.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001781 */
Cheng Jiana1246922019-05-04 19:39:39 +08001782 if (ftrace_rec_count(rec) == 1 &&
1783 ftrace_find_tramp_ops_any(rec))
1784 rec->flags |= FTRACE_FL_TRAMP;
1785 else
1786 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001787
1788 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001789 * flags will be cleared in ftrace_check_record()
1790 * if rec count is zero.
1791 */
Steven Rostedted926f92011-05-03 13:25:24 -04001792 }
1793 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001794
1795 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04001796 update |= ftrace_test_record(rec, true) != FTRACE_UPDATE_IGNORE;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001797
Steven Rostedted926f92011-05-03 13:25:24 -04001798 /* Shortcut, if we handled all records, we are done. */
1799 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001800 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001801 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001802
1803 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001804}
1805
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001806static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001807 int filter_hash)
1808{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001809 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001810}
1811
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001812static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001813 int filter_hash)
1814{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001815 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001816}
1817
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001818static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1819 int filter_hash, int inc)
1820{
1821 struct ftrace_ops *op;
1822
1823 __ftrace_hash_rec_update(ops, filter_hash, inc);
1824
1825 if (ops->func_hash != &global_ops.local_hash)
1826 return;
1827
1828 /*
1829 * If the ops shares the global_ops hash, then we need to update
1830 * all ops that are enabled and use this hash.
1831 */
1832 do_for_each_ftrace_op(op, ftrace_ops_list) {
1833 /* Already done */
1834 if (op == ops)
1835 continue;
1836 if (op->func_hash == &global_ops.local_hash)
1837 __ftrace_hash_rec_update(op, filter_hash, inc);
1838 } while_for_each_ftrace_op(op);
1839}
1840
1841static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1842 int filter_hash)
1843{
1844 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1845}
1846
1847static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1848 int filter_hash)
1849{
1850 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1851}
1852
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001853/*
1854 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1855 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1856 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1857 * Note that old_hash and new_hash has below meanings
1858 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1859 * - If the hash is EMPTY_HASH, it hits nothing
1860 * - Anything else hits the recs which match the hash entries.
1861 */
1862static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1863 struct ftrace_hash *old_hash,
1864 struct ftrace_hash *new_hash)
1865{
1866 struct ftrace_page *pg;
1867 struct dyn_ftrace *rec, *end = NULL;
1868 int in_old, in_new;
1869
1870 /* Only update if the ops has been registered */
1871 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1872 return 0;
1873
1874 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1875 return 0;
1876
1877 /*
1878 * Since the IPMODIFY is a very address sensitive action, we do not
1879 * allow ftrace_ops to set all functions to new hash.
1880 */
1881 if (!new_hash || !old_hash)
1882 return -EINVAL;
1883
1884 /* Update rec->flags */
1885 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001886
1887 if (rec->flags & FTRACE_FL_DISABLED)
1888 continue;
1889
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001890 /* We need to update only differences of filter_hash */
1891 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1892 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1893 if (in_old == in_new)
1894 continue;
1895
1896 if (in_new) {
1897 /* New entries must ensure no others are using it */
1898 if (rec->flags & FTRACE_FL_IPMODIFY)
1899 goto rollback;
1900 rec->flags |= FTRACE_FL_IPMODIFY;
1901 } else /* Removed entry */
1902 rec->flags &= ~FTRACE_FL_IPMODIFY;
1903 } while_for_each_ftrace_rec();
1904
1905 return 0;
1906
1907rollback:
1908 end = rec;
1909
1910 /* Roll back what we did above */
1911 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001912
1913 if (rec->flags & FTRACE_FL_DISABLED)
1914 continue;
1915
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001916 if (rec == end)
1917 goto err_out;
1918
1919 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1920 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1921 if (in_old == in_new)
1922 continue;
1923
1924 if (in_new)
1925 rec->flags &= ~FTRACE_FL_IPMODIFY;
1926 else
1927 rec->flags |= FTRACE_FL_IPMODIFY;
1928 } while_for_each_ftrace_rec();
1929
1930err_out:
1931 return -EBUSY;
1932}
1933
1934static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1935{
1936 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1937
1938 if (ftrace_hash_empty(hash))
1939 hash = NULL;
1940
1941 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1942}
1943
1944/* Disabling always succeeds */
1945static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1946{
1947 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1948
1949 if (ftrace_hash_empty(hash))
1950 hash = NULL;
1951
1952 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1953}
1954
1955static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1956 struct ftrace_hash *new_hash)
1957{
1958 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1959
1960 if (ftrace_hash_empty(old_hash))
1961 old_hash = NULL;
1962
1963 if (ftrace_hash_empty(new_hash))
1964 new_hash = NULL;
1965
1966 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1967}
1968
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001969static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001970{
1971 int i;
1972
1973 printk(KERN_CONT "%s", fmt);
1974
1975 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1976 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1977}
1978
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001979enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001980const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001981
1982static void print_bug_type(void)
1983{
1984 switch (ftrace_bug_type) {
1985 case FTRACE_BUG_UNKNOWN:
1986 break;
1987 case FTRACE_BUG_INIT:
1988 pr_info("Initializing ftrace call sites\n");
1989 break;
1990 case FTRACE_BUG_NOP:
1991 pr_info("Setting ftrace call site to NOP\n");
1992 break;
1993 case FTRACE_BUG_CALL:
1994 pr_info("Setting ftrace call site to call ftrace function\n");
1995 break;
1996 case FTRACE_BUG_UPDATE:
1997 pr_info("Updating ftrace call site to call a different ftrace function\n");
1998 break;
1999 }
2000}
2001
Steven Rostedtc88fd862011-08-16 09:53:39 -04002002/**
2003 * ftrace_bug - report and shutdown function tracer
2004 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002005 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04002006 *
2007 * The arch code that enables or disables the function tracing
2008 * can call ftrace_bug() when it has detected a problem in
2009 * modifying the code. @failed should be one of either:
2010 * EFAULT - if the problem happens on reading the @ip address
2011 * EINVAL - if what is read at @ip is not what was expected
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05302012 * EPERM - if the problem happens on writing to the @ip address
Steven Rostedtc88fd862011-08-16 09:53:39 -04002013 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002014void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002015{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002016 unsigned long ip = rec ? rec->ip : 0;
2017
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002018 switch (failed) {
2019 case -EFAULT:
2020 FTRACE_WARN_ON_ONCE(1);
2021 pr_info("ftrace faulted on modifying ");
2022 print_ip_sym(ip);
2023 break;
2024 case -EINVAL:
2025 FTRACE_WARN_ON_ONCE(1);
2026 pr_info("ftrace failed to modify ");
2027 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002028 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002029 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002030 if (ftrace_expected) {
2031 print_ip_ins(" expected: ", ftrace_expected);
2032 pr_cont("\n");
2033 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002034 break;
2035 case -EPERM:
2036 FTRACE_WARN_ON_ONCE(1);
2037 pr_info("ftrace faulted on writing ");
2038 print_ip_sym(ip);
2039 break;
2040 default:
2041 FTRACE_WARN_ON_ONCE(1);
2042 pr_info("ftrace faulted on unknown error ");
2043 print_ip_sym(ip);
2044 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002045 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002046 if (rec) {
2047 struct ftrace_ops *ops = NULL;
2048
2049 pr_info("ftrace record flags: %lx\n", rec->flags);
2050 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2051 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2052 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2053 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002054 if (ops) {
2055 do {
2056 pr_cont("\ttramp: %pS (%pS)",
2057 (void *)ops->trampoline,
2058 (void *)ops->func);
2059 ops = ftrace_find_tramp_ops_next(rec, ops);
2060 } while (ops);
2061 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002062 pr_cont("\ttramp: ERROR!");
2063
2064 }
2065 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002066 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002067 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002068}
2069
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002070static int ftrace_check_record(struct dyn_ftrace *rec, bool enable, bool update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002071{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002072 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002073
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002074 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2075
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002076 if (rec->flags & FTRACE_FL_DISABLED)
2077 return FTRACE_UPDATE_IGNORE;
2078
Steven Rostedt982c3502008-11-15 16:31:41 -05002079 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002080 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002081 *
Steven Rostedted926f92011-05-03 13:25:24 -04002082 * If the record has a ref count, then we need to enable it
2083 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002084 *
Steven Rostedted926f92011-05-03 13:25:24 -04002085 * Otherwise we make sure its disabled.
2086 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002087 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002088 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002089 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002090 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002091 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002092
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002093 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002094 * If enabling and the REGS flag does not match the REGS_EN, or
2095 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2096 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002097 * Same for direct calls.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002098 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002099 if (flag) {
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002100 if (!(rec->flags & FTRACE_FL_REGS) !=
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002101 !(rec->flags & FTRACE_FL_REGS_EN))
2102 flag |= FTRACE_FL_REGS;
2103
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002104 if (!(rec->flags & FTRACE_FL_TRAMP) !=
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002105 !(rec->flags & FTRACE_FL_TRAMP_EN))
2106 flag |= FTRACE_FL_TRAMP;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002107
2108 /*
2109 * Direct calls are special, as count matters.
2110 * We must test the record for direct, if the
2111 * DIRECT and DIRECT_EN do not match, but only
2112 * if the count is 1. That's because, if the
2113 * count is something other than one, we do not
2114 * want the direct enabled (it will be done via the
2115 * direct helper). But if DIRECT_EN is set, and
2116 * the count is not one, we need to clear it.
2117 */
2118 if (ftrace_rec_count(rec) == 1) {
2119 if (!(rec->flags & FTRACE_FL_DIRECT) !=
2120 !(rec->flags & FTRACE_FL_DIRECT_EN))
2121 flag |= FTRACE_FL_DIRECT;
2122 } else if (rec->flags & FTRACE_FL_DIRECT_EN) {
2123 flag |= FTRACE_FL_DIRECT;
2124 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002125 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002126
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002127 /* If the state of this record hasn't changed, then do nothing */
2128 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002129 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002130
2131 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002132 /* Save off if rec is being enabled (for return value) */
2133 flag ^= rec->flags & FTRACE_FL_ENABLED;
2134
2135 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002136 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002137 if (flag & FTRACE_FL_REGS) {
2138 if (rec->flags & FTRACE_FL_REGS)
2139 rec->flags |= FTRACE_FL_REGS_EN;
2140 else
2141 rec->flags &= ~FTRACE_FL_REGS_EN;
2142 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002143 if (flag & FTRACE_FL_TRAMP) {
2144 if (rec->flags & FTRACE_FL_TRAMP)
2145 rec->flags |= FTRACE_FL_TRAMP_EN;
2146 else
2147 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2148 }
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002149 if (flag & FTRACE_FL_DIRECT) {
2150 /*
2151 * If there's only one user (direct_ops helper)
2152 * then we can call the direct function
2153 * directly (no ftrace trampoline).
2154 */
2155 if (ftrace_rec_count(rec) == 1) {
2156 if (rec->flags & FTRACE_FL_DIRECT)
2157 rec->flags |= FTRACE_FL_DIRECT_EN;
2158 else
2159 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2160 } else {
2161 /*
2162 * Can only call directly if there's
2163 * only one callback to the function.
2164 */
2165 rec->flags &= ~FTRACE_FL_DIRECT_EN;
2166 }
2167 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002168 }
2169
2170 /*
2171 * If this record is being updated from a nop, then
2172 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002173 * Otherwise,
2174 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002175 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002176 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002177 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002178 if (flag & FTRACE_FL_ENABLED) {
2179 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002180 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002181 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002182
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002183 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002184 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002185 }
2186
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002187 if (update) {
2188 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002189 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002190 rec->flags = 0;
2191 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002192 /*
2193 * Just disable the record, but keep the ops TRAMP
2194 * and REGS states. The _EN flags must be disabled though.
2195 */
2196 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002197 FTRACE_FL_REGS_EN | FTRACE_FL_DIRECT_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002198 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002199
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002200 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002201 return FTRACE_UPDATE_MAKE_NOP;
2202}
2203
2204/**
2205 * ftrace_update_record, set a record that now is tracing or not
2206 * @rec: the record to update
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002207 * @enable: set to true if the record is tracing, false to force disable
Steven Rostedtc88fd862011-08-16 09:53:39 -04002208 *
2209 * The records that represent all functions that can be traced need
2210 * to be updated when tracing has been enabled.
2211 */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002212int ftrace_update_record(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002213{
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002214 return ftrace_check_record(rec, enable, true);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002215}
2216
2217/**
2218 * ftrace_test_record, check if the record has been enabled or not
2219 * @rec: the record to test
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002220 * @enable: set to true to check if enabled, false if it is disabled
Steven Rostedtc88fd862011-08-16 09:53:39 -04002221 *
2222 * The arch code may need to test if a record is already set to
2223 * tracing to determine how to modify the function code that it
2224 * represents.
2225 */
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002226int ftrace_test_record(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002227{
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002228 return ftrace_check_record(rec, enable, false);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002229}
2230
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002231static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002232ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2233{
2234 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002235 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002236
2237 do_for_each_ftrace_op(op, ftrace_ops_list) {
2238
2239 if (!op->trampoline)
2240 continue;
2241
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002242 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002243 return op;
2244 } while_for_each_ftrace_op(op);
2245
2246 return NULL;
2247}
2248
2249static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002250ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2251 struct ftrace_ops *op)
2252{
2253 unsigned long ip = rec->ip;
2254
2255 while_for_each_ftrace_op(op) {
2256
2257 if (!op->trampoline)
2258 continue;
2259
2260 if (hash_contains_ip(ip, op->func_hash))
2261 return op;
2262 }
2263
2264 return NULL;
2265}
2266
2267static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002268ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2269{
2270 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002271 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002272
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002273 /*
2274 * Need to check removed ops first.
2275 * If they are being removed, and this rec has a tramp,
2276 * and this rec is in the ops list, then it would be the
2277 * one with the tramp.
2278 */
2279 if (removed_ops) {
2280 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002281 return removed_ops;
2282 }
2283
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002284 /*
2285 * Need to find the current trampoline for a rec.
2286 * Now, a trampoline is only attached to a rec if there
2287 * was a single 'ops' attached to it. But this can be called
2288 * when we are adding another op to the rec or removing the
2289 * current one. Thus, if the op is being added, we can
2290 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002291 * yet.
2292 *
2293 * If an ops is being modified (hooking to different functions)
2294 * then we don't care about the new functions that are being
2295 * added, just the old ones (that are probably being removed).
2296 *
2297 * If we are adding an ops to a function that already is using
2298 * a trampoline, it needs to be removed (trampolines are only
2299 * for single ops connected), then an ops that is not being
2300 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002301 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002302 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002303
2304 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002305 continue;
2306
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002307 /*
2308 * If the ops is being added, it hasn't gotten to
2309 * the point to be removed from this tree yet.
2310 */
2311 if (op->flags & FTRACE_OPS_FL_ADDING)
2312 continue;
2313
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002314
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002315 /*
2316 * If the ops is being modified and is in the old
2317 * hash, then it is probably being removed from this
2318 * function.
2319 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002320 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2321 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002322 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002323 /*
2324 * If the ops is not being added or modified, and it's
2325 * in its normal filter hash, then this must be the one
2326 * we want!
2327 */
2328 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2329 hash_contains_ip(ip, op->func_hash))
2330 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002331
2332 } while_for_each_ftrace_op(op);
2333
2334 return NULL;
2335}
2336
2337static struct ftrace_ops *
2338ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2339{
2340 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002341 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002342
2343 do_for_each_ftrace_op(op, ftrace_ops_list) {
2344 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002345 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002346 return op;
2347 } while_for_each_ftrace_op(op);
2348
2349 return NULL;
2350}
2351
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002352#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
2353/* Protected by rcu_tasks for reading, and direct_mutex for writing */
2354static struct ftrace_hash *direct_functions = EMPTY_HASH;
2355static DEFINE_MUTEX(direct_mutex);
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05002356int ftrace_direct_func_count;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002357
2358/*
2359 * Search the direct_functions hash to see if the given instruction pointer
2360 * has a direct caller attached to it.
2361 */
Alexei Starovoitovff205762019-12-08 16:01:12 -08002362unsigned long ftrace_find_rec_direct(unsigned long ip)
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002363{
2364 struct ftrace_func_entry *entry;
2365
2366 entry = __ftrace_lookup_ip(direct_functions, ip);
2367 if (!entry)
2368 return 0;
2369
2370 return entry->direct;
2371}
2372
2373static void call_direct_funcs(unsigned long ip, unsigned long pip,
2374 struct ftrace_ops *ops, struct pt_regs *regs)
2375{
2376 unsigned long addr;
2377
Alexei Starovoitovff205762019-12-08 16:01:12 -08002378 addr = ftrace_find_rec_direct(ip);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002379 if (!addr)
2380 return;
2381
2382 arch_ftrace_set_direct_caller(regs, addr);
2383}
2384
2385struct ftrace_ops direct_ops = {
2386 .func = call_direct_funcs,
2387 .flags = FTRACE_OPS_FL_IPMODIFY | FTRACE_OPS_FL_RECURSION_SAFE
2388 | FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS
2389 | FTRACE_OPS_FL_PERMANENT,
2390};
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002391#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
2392
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002393/**
2394 * ftrace_get_addr_new - Get the call address to set to
2395 * @rec: The ftrace record descriptor
2396 *
2397 * If the record has the FTRACE_FL_REGS set, that means that it
2398 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2399 * is not not set, then it wants to convert to the normal callback.
2400 *
2401 * Returns the address of the trampoline to set to
2402 */
2403unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2404{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002405 struct ftrace_ops *ops;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002406 unsigned long addr;
2407
2408 if ((rec->flags & FTRACE_FL_DIRECT) &&
2409 (ftrace_rec_count(rec) == 1)) {
Alexei Starovoitovff205762019-12-08 16:01:12 -08002410 addr = ftrace_find_rec_direct(rec->ip);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002411 if (addr)
2412 return addr;
2413 WARN_ON_ONCE(1);
2414 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002415
2416 /* Trampolines take precedence over regs */
2417 if (rec->flags & FTRACE_FL_TRAMP) {
2418 ops = ftrace_find_tramp_ops_new(rec);
2419 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002420 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2421 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002422 /* Ftrace is shutting down, return anything */
2423 return (unsigned long)FTRACE_ADDR;
2424 }
2425 return ops->trampoline;
2426 }
2427
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002428 if (rec->flags & FTRACE_FL_REGS)
2429 return (unsigned long)FTRACE_REGS_ADDR;
2430 else
2431 return (unsigned long)FTRACE_ADDR;
2432}
2433
2434/**
2435 * ftrace_get_addr_curr - Get the call address that is already there
2436 * @rec: The ftrace record descriptor
2437 *
2438 * The FTRACE_FL_REGS_EN is set when the record already points to
2439 * a function that saves all the regs. Basically the '_EN' version
2440 * represents the current state of the function.
2441 *
2442 * Returns the address of the trampoline that is currently being called
2443 */
2444unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2445{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002446 struct ftrace_ops *ops;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002447 unsigned long addr;
2448
2449 /* Direct calls take precedence over trampolines */
2450 if (rec->flags & FTRACE_FL_DIRECT_EN) {
Alexei Starovoitovff205762019-12-08 16:01:12 -08002451 addr = ftrace_find_rec_direct(rec->ip);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05002452 if (addr)
2453 return addr;
2454 WARN_ON_ONCE(1);
2455 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002456
2457 /* Trampolines take precedence over regs */
2458 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2459 ops = ftrace_find_tramp_ops_curr(rec);
2460 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002461 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2462 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002463 /* Ftrace is shutting down, return anything */
2464 return (unsigned long)FTRACE_ADDR;
2465 }
2466 return ops->trampoline;
2467 }
2468
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002469 if (rec->flags & FTRACE_FL_REGS_EN)
2470 return (unsigned long)FTRACE_REGS_ADDR;
2471 else
2472 return (unsigned long)FTRACE_ADDR;
2473}
2474
Steven Rostedtc88fd862011-08-16 09:53:39 -04002475static int
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002476__ftrace_replace_code(struct dyn_ftrace *rec, bool enable)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002477{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002478 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002479 unsigned long ftrace_addr;
2480 int ret;
2481
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002482 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002483
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002484 /* This needs to be done before we call ftrace_update_record */
2485 ftrace_old_addr = ftrace_get_addr_curr(rec);
2486
2487 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002488
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002489 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2490
Steven Rostedtc88fd862011-08-16 09:53:39 -04002491 switch (ret) {
2492 case FTRACE_UPDATE_IGNORE:
2493 return 0;
2494
2495 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002496 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002497 return ftrace_make_call(rec, ftrace_addr);
2498
2499 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002500 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002501 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002502
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002503 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002504 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002505 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002506 }
2507
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05302508 return -1; /* unknown ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002509}
2510
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002511void __weak ftrace_replace_code(int mod_flags)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002512{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002513 struct dyn_ftrace *rec;
2514 struct ftrace_page *pg;
Steven Rostedt (VMware)7375dca2019-05-20 09:26:24 -04002515 bool enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002516 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002517 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002518
Steven Rostedt45a4a232011-04-21 23:16:46 -04002519 if (unlikely(ftrace_disabled))
2520 return;
2521
Steven Rostedt265c8312009-02-13 12:43:56 -05002522 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002523
2524 if (rec->flags & FTRACE_FL_DISABLED)
2525 continue;
2526
Steven Rostedte4f5d542012-04-27 09:13:18 -04002527 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002528 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002529 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002530 /* Stop processing */
2531 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002532 }
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002533 if (schedulable)
2534 cond_resched();
Steven Rostedt265c8312009-02-13 12:43:56 -05002535 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002536}
2537
Steven Rostedtc88fd862011-08-16 09:53:39 -04002538struct ftrace_rec_iter {
2539 struct ftrace_page *pg;
2540 int index;
2541};
2542
2543/**
2544 * ftrace_rec_iter_start, start up iterating over traced functions
2545 *
2546 * Returns an iterator handle that is used to iterate over all
2547 * the records that represent address locations where functions
2548 * are traced.
2549 *
2550 * May return NULL if no records are available.
2551 */
2552struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2553{
2554 /*
2555 * We only use a single iterator.
2556 * Protected by the ftrace_lock mutex.
2557 */
2558 static struct ftrace_rec_iter ftrace_rec_iter;
2559 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2560
2561 iter->pg = ftrace_pages_start;
2562 iter->index = 0;
2563
2564 /* Could have empty pages */
2565 while (iter->pg && !iter->pg->index)
2566 iter->pg = iter->pg->next;
2567
2568 if (!iter->pg)
2569 return NULL;
2570
2571 return iter;
2572}
2573
2574/**
2575 * ftrace_rec_iter_next, get the next record to process.
2576 * @iter: The handle to the iterator.
2577 *
2578 * Returns the next iterator after the given iterator @iter.
2579 */
2580struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2581{
2582 iter->index++;
2583
2584 if (iter->index >= iter->pg->index) {
2585 iter->pg = iter->pg->next;
2586 iter->index = 0;
2587
2588 /* Could have empty pages */
2589 while (iter->pg && !iter->pg->index)
2590 iter->pg = iter->pg->next;
2591 }
2592
2593 if (!iter->pg)
2594 return NULL;
2595
2596 return iter;
2597}
2598
2599/**
2600 * ftrace_rec_iter_record, get the record at the iterator location
2601 * @iter: The current iterator location
2602 *
2603 * Returns the record that the current @iter is at.
2604 */
2605struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2606{
2607 return &iter->pg->records[iter->index];
2608}
2609
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302610static int
Mark Rutlandfbf6c732019-10-16 17:51:10 +01002611ftrace_nop_initialize(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002612{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002613 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002614
Steven Rostedt45a4a232011-04-21 23:16:46 -04002615 if (unlikely(ftrace_disabled))
2616 return 0;
2617
Mark Rutlandfbf6c732019-10-16 17:51:10 +01002618 ret = ftrace_init_nop(mod, rec);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002619 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002620 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002621 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302622 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002623 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302624 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002625}
2626
Steven Rostedt000ab692009-02-17 13:35:06 -05002627/*
2628 * archs can override this function if they must do something
2629 * before the modifying code is performed.
2630 */
2631int __weak ftrace_arch_code_modify_prepare(void)
2632{
2633 return 0;
2634}
2635
2636/*
2637 * archs can override this function if they must do something
2638 * after the modifying code is performed.
2639 */
2640int __weak ftrace_arch_code_modify_post_process(void)
2641{
2642 return 0;
2643}
2644
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002645void ftrace_modify_all_code(int command)
2646{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002647 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002648 int mod_flags = 0;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002649 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002650
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002651 if (command & FTRACE_MAY_SLEEP)
2652 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2653
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002654 /*
2655 * If the ftrace_caller calls a ftrace_ops func directly,
2656 * we need to make sure that it only traces functions it
2657 * expects to trace. When doing the switch of functions,
2658 * we need to update to the ftrace_ops_list_func first
2659 * before the transition between old and new calls are set,
2660 * as the ftrace_ops_list_func will check the ops hashes
2661 * to make sure the ops are having the right functions
2662 * traced.
2663 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002664 if (update) {
2665 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2666 if (FTRACE_WARN_ON(err))
2667 return;
2668 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002669
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002670 if (command & FTRACE_UPDATE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002671 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002672 else if (command & FTRACE_DISABLE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002673 ftrace_replace_code(mod_flags);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002674
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002675 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2676 function_trace_op = set_function_trace_op;
2677 smp_wmb();
2678 /* If irqs are disabled, we are in stop machine */
2679 if (!irqs_disabled())
2680 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002681 err = ftrace_update_ftrace_func(ftrace_trace_function);
2682 if (FTRACE_WARN_ON(err))
2683 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002684 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002685
2686 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002687 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002688 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002689 err = ftrace_disable_ftrace_graph_caller();
2690 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002691}
2692
Ingo Molnare309b412008-05-12 21:20:51 +02002693static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002694{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002695 int *command = data;
2696
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002697 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002698
Steven Rostedtc88fd862011-08-16 09:53:39 -04002699 return 0;
2700}
2701
2702/**
2703 * ftrace_run_stop_machine, go back to the stop machine method
2704 * @command: The command to tell ftrace what to do
2705 *
2706 * If an arch needs to fall back to the stop machine method, the
2707 * it can call this function.
2708 */
2709void ftrace_run_stop_machine(int command)
2710{
2711 stop_machine(__ftrace_modify_code, &command, NULL);
2712}
2713
2714/**
2715 * arch_ftrace_update_code, modify the code to trace or not trace
2716 * @command: The command that needs to be done
2717 *
2718 * Archs can override this function if it does not need to
2719 * run stop_machine() to modify code.
2720 */
2721void __weak arch_ftrace_update_code(int command)
2722{
2723 ftrace_run_stop_machine(command);
2724}
2725
2726static void ftrace_run_update_code(int command)
2727{
2728 int ret;
2729
2730 ret = ftrace_arch_code_modify_prepare();
2731 FTRACE_WARN_ON(ret);
2732 if (ret)
Petr Mladekd5b844a2019-06-27 10:13:34 +02002733 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002734
2735 /*
2736 * By default we use stop_machine() to modify the code.
2737 * But archs can do what ever they want as long as it
2738 * is safe. The stop_machine() is the safest, but also
2739 * produces the most overhead.
2740 */
2741 arch_ftrace_update_code(command);
2742
Steven Rostedt000ab692009-02-17 13:35:06 -05002743 ret = ftrace_arch_code_modify_post_process();
2744 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002745}
2746
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002747static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002748 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002749{
2750 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002751 ops->old_hash.filter_hash = old_hash->filter_hash;
2752 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002753 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002754 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002755 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002756 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2757}
2758
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002759static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002760static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002761
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002762void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2763{
2764}
2765
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002766static void ftrace_startup_enable(int command)
2767{
2768 if (saved_ftrace_func != ftrace_trace_function) {
2769 saved_ftrace_func = ftrace_trace_function;
2770 command |= FTRACE_UPDATE_TRACE_FUNC;
2771 }
2772
2773 if (!command || !ftrace_enabled)
2774 return;
2775
2776 ftrace_run_update_code(command);
2777}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002778
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002779static void ftrace_startup_all(int command)
2780{
2781 update_all_ops = true;
2782 ftrace_startup_enable(command);
2783 update_all_ops = false;
2784}
2785
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002786int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002787{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002788 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002789
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002790 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002791 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002792
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002793 ret = __register_ftrace_function(ops);
2794 if (ret)
2795 return ret;
2796
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002797 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002798
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002799 /*
2800 * Note that ftrace probes uses this to start up
2801 * and modify functions it will probe. But we still
2802 * set the ADDING flag for modification, as probes
2803 * do not have trampolines. If they add them in the
2804 * future, then the probes will need to distinguish
2805 * between adding and updating probes.
2806 */
2807 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002808
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002809 ret = ftrace_hash_ipmodify_enable(ops);
2810 if (ret < 0) {
2811 /* Rollback registration process */
2812 __unregister_ftrace_function(ops);
2813 ftrace_start_up--;
2814 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2815 return ret;
2816 }
2817
Jiri Olsa7f50d062016-03-16 15:34:33 +01002818 if (ftrace_hash_rec_enable(ops, 1))
2819 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002820
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002821 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002822
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002823 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2824
Steven Rostedta1cd6172011-05-23 15:24:25 -04002825 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002826}
2827
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002828int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002829{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002830 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002831
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002832 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002833 return -ENODEV;
2834
2835 ret = __unregister_ftrace_function(ops);
2836 if (ret)
2837 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002838
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002839 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002840 /*
2841 * Just warn in case of unbalance, no need to kill ftrace, it's not
2842 * critical but the ftrace_call callers may be never nopped again after
2843 * further ftrace uses.
2844 */
2845 WARN_ON_ONCE(ftrace_start_up < 0);
2846
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002847 /* Disabling ipmodify never fails */
2848 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002849
2850 if (ftrace_hash_rec_disable(ops, 1))
2851 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002852
Namhyung Kima737e6d2014-06-12 23:56:12 +09002853 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002854
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002855 if (saved_ftrace_func != ftrace_trace_function) {
2856 saved_ftrace_func = ftrace_trace_function;
2857 command |= FTRACE_UPDATE_TRACE_FUNC;
2858 }
2859
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002860 if (!command || !ftrace_enabled) {
2861 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002862 * If these are dynamic or per_cpu ops, they still
2863 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002864 * not currently active, we can just free them
2865 * without synchronizing all CPUs.
2866 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002867 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002868 goto free_ops;
2869
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002870 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002871 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002872
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002873 /*
2874 * If the ops uses a trampoline, then it needs to be
2875 * tested first on update.
2876 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002877 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002878 removed_ops = ops;
2879
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002880 /* The trampoline logic checks the old hashes */
2881 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2882 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2883
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002884 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002885
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002886 /*
2887 * If there's no more ops registered with ftrace, run a
2888 * sanity check to make sure all rec flags are cleared.
2889 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002890 if (rcu_dereference_protected(ftrace_ops_list,
2891 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002892 struct ftrace_page *pg;
2893 struct dyn_ftrace *rec;
2894
2895 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002896 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002897 pr_warn(" %pS flags:%lx\n",
2898 (void *)rec->ip, rec->flags);
2899 } while_for_each_ftrace_rec();
2900 }
2901
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002902 ops->old_hash.filter_hash = NULL;
2903 ops->old_hash.notrace_hash = NULL;
2904
2905 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002906 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002907
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002908 /*
2909 * Dynamic ops may be freed, we must make sure that all
2910 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002911 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002912 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002913 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002914 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002915 /*
2916 * We need to do a hard force of sched synchronization.
2917 * This is because we use preempt_disable() to do RCU, but
2918 * the function tracers can be called where RCU is not watching
2919 * (like before user_exit()). We can not rely on the RCU
2920 * infrastructure to do the synchronization, thus we must do it
2921 * ourselves.
2922 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002923 schedule_on_each_cpu(ftrace_sync);
2924
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002925 /*
2926 * When the kernel is preeptive, tasks can be preempted
2927 * while on a ftrace trampoline. Just scheduling a task on
2928 * a CPU is not good enough to flush them. Calling
2929 * synchornize_rcu_tasks() will wait for those tasks to
2930 * execute and either schedule voluntarily or enter user space.
2931 */
Thomas Gleixner30c93702019-07-26 23:19:40 +02002932 if (IS_ENABLED(CONFIG_PREEMPTION))
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002933 synchronize_rcu_tasks();
2934
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002935 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002936 arch_ftrace_trampoline_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002937 }
2938
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002939 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002940}
2941
Ingo Molnare309b412008-05-12 21:20:51 +02002942static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002943{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302944 int command;
2945
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002946 if (unlikely(ftrace_disabled))
2947 return;
2948
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002949 /* Force update next time */
2950 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002951 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302952 if (ftrace_start_up) {
2953 command = FTRACE_UPDATE_CALLS;
2954 if (ftrace_graph_active)
2955 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002956 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302957 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002958}
2959
Ingo Molnare309b412008-05-12 21:20:51 +02002960static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002961{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302962 int command;
2963
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002964 if (unlikely(ftrace_disabled))
2965 return;
2966
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002967 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302968 if (ftrace_start_up) {
2969 command = FTRACE_DISABLE_CALLS;
2970 if (ftrace_graph_active)
2971 command |= FTRACE_STOP_FUNC_RET;
2972 ftrace_run_update_code(command);
2973 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002974}
2975
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002976static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002977unsigned long ftrace_update_tot_cnt;
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04002978unsigned long ftrace_number_of_pages;
2979unsigned long ftrace_number_of_groups;
Steven Rostedt3d083392008-05-12 21:20:42 +02002980
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002981static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002982{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002983 /*
2984 * Filter_hash being empty will default to trace module.
2985 * But notrace hash requires a test of individual module functions.
2986 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002987 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2988 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002989}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002990
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002991/*
2992 * Check if the current ops references the record.
2993 *
2994 * If the ops traces all functions, then it was already accounted for.
2995 * If the ops does not trace the current record function, skip it.
2996 * If the ops ignores the function via notrace filter, skip it.
2997 */
2998static inline bool
2999ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3000{
3001 /* If ops isn't enabled, ignore it */
3002 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003003 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003004
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003005 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003006 if (ops_traces_mod(ops))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003007 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003008
3009 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003010 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05003011 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003012 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003013
3014 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003015 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003016 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003017
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05003018 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003019}
3020
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003021static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02003022{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003023 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08003024 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01003025 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003026 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003027 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003028 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04003029
Ingo Molnar750ed1a2008-05-12 21:20:46 +02003030 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02003031
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003032 /*
3033 * When a module is loaded, this function is called to convert
3034 * the calls to mcount in its text to nops, and also to create
3035 * an entry in the ftrace data. Now, if ftrace is activated
3036 * after this call, but before the module sets its text to
3037 * read-only, the modification of enabling ftrace can fail if
3038 * the read-only is done while ftrace is converting the calls.
3039 * To prevent this, the module's records are set as disabled
3040 * and will be enabled after the call to set the module's text
3041 * to read-only.
3042 */
3043 if (mod)
3044 rec_flags |= FTRACE_FL_DISABLED;
3045
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003046 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05303047
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003048 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04003049
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003050 /* If something went wrong, bail without enabling anything */
3051 if (unlikely(ftrace_disabled))
3052 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02003053
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003054 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05003055 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05303056
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003057 /*
3058 * Do the initial record conversion from mcount jump
3059 * to the NOP instructions.
3060 */
Vasily Gorbikcbdaeaf2019-06-05 13:11:58 +02003061 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
Mark Rutlandfbf6c732019-10-16 17:51:10 +01003062 !ftrace_nop_initialize(mod, p))
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003063 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003064
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003065 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003066 }
Steven Rostedt3d083392008-05-12 21:20:42 +02003067 }
3068
Ingo Molnar750ed1a2008-05-12 21:20:46 +02003069 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02003070 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01003071 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02003072
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02003073 return 0;
3074}
3075
Steven Rostedta7900872011-12-16 16:23:44 -05003076static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003077{
Steven Rostedta7900872011-12-16 16:23:44 -05003078 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003079 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003080
Steven Rostedta7900872011-12-16 16:23:44 -05003081 if (WARN_ON(!count))
3082 return -EINVAL;
3083
3084 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003085
3086 /*
Steven Rostedta7900872011-12-16 16:23:44 -05003087 * We want to fill as much as possible. No more than a page
3088 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003089 */
Steven Rostedta7900872011-12-16 16:23:44 -05003090 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3091 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02003092
Steven Rostedta7900872011-12-16 16:23:44 -05003093 again:
3094 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3095
3096 if (!pg->records) {
3097 /* if we can't allocate this size, try something smaller */
3098 if (!order)
3099 return -ENOMEM;
3100 order >>= 1;
3101 goto again;
3102 }
3103
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04003104 ftrace_number_of_pages += 1 << order;
3105 ftrace_number_of_groups++;
3106
Steven Rostedta7900872011-12-16 16:23:44 -05003107 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3108 pg->size = cnt;
3109
3110 if (cnt > count)
3111 cnt = count;
3112
3113 return cnt;
3114}
3115
3116static struct ftrace_page *
3117ftrace_allocate_pages(unsigned long num_to_init)
3118{
3119 struct ftrace_page *start_pg;
3120 struct ftrace_page *pg;
3121 int order;
3122 int cnt;
3123
3124 if (!num_to_init)
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05303125 return NULL;
Steven Rostedta7900872011-12-16 16:23:44 -05003126
3127 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3128 if (!pg)
3129 return NULL;
3130
3131 /*
3132 * Try to allocate as much as possible in one continues
3133 * location that fills in all of the space. We want to
3134 * waste as little space as possible.
3135 */
3136 for (;;) {
3137 cnt = ftrace_allocate_records(pg, num_to_init);
3138 if (cnt < 0)
3139 goto free_pages;
3140
3141 num_to_init -= cnt;
3142 if (!num_to_init)
3143 break;
3144
3145 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3146 if (!pg->next)
3147 goto free_pages;
3148
3149 pg = pg->next;
3150 }
3151
3152 return start_pg;
3153
3154 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003155 pg = start_pg;
3156 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003157 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3158 free_pages((unsigned long)pg->records, order);
3159 start_pg = pg->next;
3160 kfree(pg);
3161 pg = start_pg;
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04003162 ftrace_number_of_pages -= 1 << order;
3163 ftrace_number_of_groups--;
Steven Rostedta7900872011-12-16 16:23:44 -05003164 }
3165 pr_info("ftrace: FAILED to allocate memory for functions\n");
3166 return NULL;
3167}
3168
Steven Rostedt5072c592008-05-12 21:20:43 +02003169#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3170
3171struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003172 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003173 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003174 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003175 struct ftrace_page *pg;
3176 struct dyn_ftrace *func;
3177 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003178 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003179 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003180 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003181 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003182 struct trace_array *tr;
3183 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003184 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003185 int idx;
3186 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003187};
3188
Ingo Molnare309b412008-05-12 21:20:51 +02003189static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003190t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003191{
3192 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003193 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003194 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003195 struct ftrace_hash *hash;
3196 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003197 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003198 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003199 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003200
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003201 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003202 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003203
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003204 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003205 return NULL;
3206
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003207 func_probes = &tr->func_probes;
3208 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003209 return NULL;
3210
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003211 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003212 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003213 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003214 }
3215
3216 if (iter->probe_entry)
3217 hnd = &iter->probe_entry->hlist;
3218
3219 hash = iter->probe->ops.func_hash->filter_hash;
Naveen N. Rao7bd46642019-07-04 20:04:41 +05303220
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04003221 /*
3222 * A probe being registered may temporarily have an empty hash
3223 * and it's at the end of the func_probes list.
3224 */
3225 if (!hash || hash == EMPTY_HASH)
Naveen N. Rao7bd46642019-07-04 20:04:41 +05303226 return NULL;
3227
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003228 size = 1 << hash->size_bits;
3229
3230 retry:
3231 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003232 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003233 return NULL;
3234 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003235 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003236 hash = iter->probe->ops.func_hash->filter_hash;
3237 size = 1 << hash->size_bits;
3238 iter->pidx = 0;
3239 }
3240
3241 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003242
3243 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003244 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003245 hnd = NULL;
3246 goto retry;
3247 }
3248
3249 if (!hnd)
3250 hnd = hhd->first;
3251 else {
3252 hnd = hnd->next;
3253 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003254 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003255 goto retry;
3256 }
3257 }
3258
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003259 if (WARN_ON_ONCE(!hnd))
3260 return NULL;
3261
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003262 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003263
3264 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003265}
3266
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003267static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003268{
3269 struct ftrace_iterator *iter = m->private;
3270 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003271 loff_t l;
3272
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003273 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003274 return NULL;
3275
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003276 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003277 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003278
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003279 iter->probe = NULL;
3280 iter->probe_entry = NULL;
3281 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003282 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003283 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003284 if (!p)
3285 break;
3286 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003287 if (!p)
3288 return NULL;
3289
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003290 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003291 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003292
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003293 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003294}
3295
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003296static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003297t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003298{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003299 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003300 struct ftrace_probe_ops *probe_ops;
3301 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003302
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003303 probe = iter->probe;
3304 probe_entry = iter->probe_entry;
3305
3306 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003307 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003308
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003309 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003310
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003311 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003312 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003313
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003314 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3315 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003316
3317 return 0;
3318}
3319
3320static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003321t_mod_next(struct seq_file *m, loff_t *pos)
3322{
3323 struct ftrace_iterator *iter = m->private;
3324 struct trace_array *tr = iter->tr;
3325
3326 (*pos)++;
3327 iter->pos = *pos;
3328
3329 iter->mod_list = iter->mod_list->next;
3330
3331 if (iter->mod_list == &tr->mod_trace ||
3332 iter->mod_list == &tr->mod_notrace) {
3333 iter->flags &= ~FTRACE_ITER_MOD;
3334 return NULL;
3335 }
3336
3337 iter->mod_pos = *pos;
3338
3339 return iter;
3340}
3341
3342static void *t_mod_start(struct seq_file *m, loff_t *pos)
3343{
3344 struct ftrace_iterator *iter = m->private;
3345 void *p = NULL;
3346 loff_t l;
3347
3348 if (iter->func_pos > *pos)
3349 return NULL;
3350
3351 iter->mod_pos = iter->func_pos;
3352
3353 /* probes are only available if tr is set */
3354 if (!iter->tr)
3355 return NULL;
3356
3357 for (l = 0; l <= (*pos - iter->func_pos); ) {
3358 p = t_mod_next(m, &l);
3359 if (!p)
3360 break;
3361 }
3362 if (!p) {
3363 iter->flags &= ~FTRACE_ITER_MOD;
3364 return t_probe_start(m, pos);
3365 }
3366
3367 /* Only set this if we have an item */
3368 iter->flags |= FTRACE_ITER_MOD;
3369
3370 return iter;
3371}
3372
3373static int
3374t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3375{
3376 struct ftrace_mod_load *ftrace_mod;
3377 struct trace_array *tr = iter->tr;
3378
3379 if (WARN_ON_ONCE(!iter->mod_list) ||
3380 iter->mod_list == &tr->mod_trace ||
3381 iter->mod_list == &tr->mod_notrace)
3382 return -EIO;
3383
3384 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3385
3386 if (ftrace_mod->func)
3387 seq_printf(m, "%s", ftrace_mod->func);
3388 else
3389 seq_putc(m, '*');
3390
3391 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3392
3393 return 0;
3394}
3395
3396static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003397t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003398{
3399 struct ftrace_iterator *iter = m->private;
3400 struct dyn_ftrace *rec = NULL;
3401
3402 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003403
Steven Rostedt5072c592008-05-12 21:20:43 +02003404 retry:
3405 if (iter->idx >= iter->pg->index) {
3406 if (iter->pg->next) {
3407 iter->pg = iter->pg->next;
3408 iter->idx = 0;
3409 goto retry;
3410 }
3411 } else {
3412 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003413 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3414 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003415
3416 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003417 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003418
Steven Rostedt5072c592008-05-12 21:20:43 +02003419 rec = NULL;
3420 goto retry;
3421 }
3422 }
3423
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003424 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003425 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003426
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003427 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003428 iter->func = rec;
3429
3430 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003431}
3432
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003433static void *
3434t_next(struct seq_file *m, void *v, loff_t *pos)
3435{
3436 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003437 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003438 void *ret;
3439
3440 if (unlikely(ftrace_disabled))
3441 return NULL;
3442
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003443 if (iter->flags & FTRACE_ITER_PROBE)
3444 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003445
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003446 if (iter->flags & FTRACE_ITER_MOD)
3447 return t_mod_next(m, pos);
3448
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003449 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003450 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003451 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003452 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003453 }
3454
3455 ret = t_func_next(m, pos);
3456
3457 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003458 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003459
3460 return ret;
3461}
3462
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003463static void reset_iter_read(struct ftrace_iterator *iter)
3464{
3465 iter->pos = 0;
3466 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003467 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003468}
3469
3470static void *t_start(struct seq_file *m, loff_t *pos)
3471{
3472 struct ftrace_iterator *iter = m->private;
3473 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003474 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003475
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003476 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003477
3478 if (unlikely(ftrace_disabled))
3479 return NULL;
3480
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003481 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003482 * If an lseek was done, then reset and start from beginning.
3483 */
3484 if (*pos < iter->pos)
3485 reset_iter_read(iter);
3486
3487 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003488 * For set_ftrace_filter reading, if we have the filter
3489 * off, we can short cut and just print out that all
3490 * functions are enabled.
3491 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003492 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3493 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003494 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003495 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003496 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003497 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003498 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003499 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003500 return iter;
3501 }
3502
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003503 if (iter->flags & FTRACE_ITER_MOD)
3504 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003505
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003506 /*
3507 * Unfortunately, we need to restart at ftrace_pages_start
3508 * every time we let go of the ftrace_mutex. This is because
3509 * those pointers can change without the lock.
3510 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003511 iter->pg = ftrace_pages_start;
3512 iter->idx = 0;
3513 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003514 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003515 if (!p)
3516 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003517 }
walimis5821e1b2008-11-15 15:19:06 +08003518
Steven Rostedt69a30832011-12-19 15:21:16 -05003519 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003520 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003521
3522 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003523}
3524
3525static void t_stop(struct seq_file *m, void *p)
3526{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003527 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003528}
3529
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003530void * __weak
3531arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3532{
3533 return NULL;
3534}
3535
3536static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3537 struct dyn_ftrace *rec)
3538{
3539 void *ptr;
3540
3541 ptr = arch_ftrace_trampoline_func(ops, rec);
3542 if (ptr)
3543 seq_printf(m, " ->%pS", ptr);
3544}
3545
Steven Rostedt5072c592008-05-12 21:20:43 +02003546static int t_show(struct seq_file *m, void *v)
3547{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003548 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003549 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003550
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003551 if (iter->flags & FTRACE_ITER_PROBE)
3552 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003553
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003554 if (iter->flags & FTRACE_ITER_MOD)
3555 return t_mod_show(m, iter);
3556
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003557 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003558 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003559 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003560 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003561 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003562 return 0;
3563 }
3564
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003565 rec = iter->func;
3566
Steven Rostedt5072c592008-05-12 21:20:43 +02003567 if (!rec)
3568 return 0;
3569
Steven Rostedt647bcd02011-05-03 14:39:21 -04003570 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003571 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003572 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003573
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05003574 seq_printf(m, " (%ld)%s%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003575 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003576 rec->flags & FTRACE_FL_REGS ? " R" : " ",
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05003577 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ",
3578 rec->flags & FTRACE_FL_DIRECT ? " D" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003579 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003580 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003581 if (ops) {
3582 do {
3583 seq_printf(m, "\ttramp: %pS (%pS)",
3584 (void *)ops->trampoline,
3585 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003586 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003587 ops = ftrace_find_tramp_ops_next(rec, ops);
3588 } while (ops);
3589 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003590 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003591 } else {
3592 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003593 }
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05003594 if (rec->flags & FTRACE_FL_DIRECT) {
3595 unsigned long direct;
3596
Alexei Starovoitovff205762019-12-08 16:01:12 -08003597 direct = ftrace_find_rec_direct(rec->ip);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05003598 if (direct)
3599 seq_printf(m, "\n\tdirect-->%pS", (void *)direct);
3600 }
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003601 }
3602
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003603 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003604
3605 return 0;
3606}
3607
James Morris88e9d342009-09-22 16:43:43 -07003608static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003609 .start = t_start,
3610 .next = t_next,
3611 .stop = t_stop,
3612 .show = t_show,
3613};
3614
Ingo Molnare309b412008-05-12 21:20:51 +02003615static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003616ftrace_avail_open(struct inode *inode, struct file *file)
3617{
3618 struct ftrace_iterator *iter;
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003619 int ret;
3620
3621 ret = security_locked_down(LOCKDOWN_TRACEFS);
3622 if (ret)
3623 return ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003624
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003625 if (unlikely(ftrace_disabled))
3626 return -ENODEV;
3627
Jiri Olsa50e18b92012-04-25 10:23:39 +02003628 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003629 if (!iter)
3630 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003631
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003632 iter->pg = ftrace_pages_start;
3633 iter->ops = &global_ops;
3634
3635 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003636}
3637
Steven Rostedt647bcd02011-05-03 14:39:21 -04003638static int
3639ftrace_enabled_open(struct inode *inode, struct file *file)
3640{
3641 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003642
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003643 /*
3644 * This shows us what functions are currently being
3645 * traced and by what. Not sure if we want lockdown
3646 * to hide such critical information for an admin.
3647 * Although, perhaps it can show information we don't
3648 * want people to see, but if something is tracing
3649 * something, we probably want to know about it.
3650 */
3651
Jiri Olsa50e18b92012-04-25 10:23:39 +02003652 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003653 if (!iter)
3654 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003655
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003656 iter->pg = ftrace_pages_start;
3657 iter->flags = FTRACE_ITER_ENABLED;
3658 iter->ops = &global_ops;
3659
3660 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003661}
3662
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003663/**
3664 * ftrace_regex_open - initialize function tracer filter files
3665 * @ops: The ftrace_ops that hold the hash filters
3666 * @flag: The type of filter to process
3667 * @inode: The inode, usually passed in to your open routine
3668 * @file: The file, usually passed in to your open routine
3669 *
3670 * ftrace_regex_open() initializes the filter files for the
3671 * @ops. Depending on @flag it may process the filter hash or
3672 * the notrace hash of @ops. With this called from the open
3673 * routine, you can use ftrace_filter_write() for the write
3674 * routine if @flag has FTRACE_ITER_FILTER set, or
3675 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003676 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003677 * release must call ftrace_regex_release().
3678 */
3679int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003680ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003681 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003682{
3683 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003684 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003685 struct list_head *mod_head;
3686 struct trace_array *tr = ops->private;
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003687 int ret = -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003688
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003689 ftrace_ops_init(ops);
3690
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003691 if (unlikely(ftrace_disabled))
3692 return -ENODEV;
3693
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04003694 if (tracing_check_open_get_tr(tr))
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003695 return -ENODEV;
3696
Steven Rostedt5072c592008-05-12 21:20:43 +02003697 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3698 if (!iter)
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003699 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003700
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003701 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3702 goto out;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003703
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003704 iter->ops = ops;
3705 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003706 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003707
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003708 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003709
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003710 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003711 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003712 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003713 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003714 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003715 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003716 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003717
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003718 iter->mod_list = mod_head;
3719
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003720 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003721 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3722
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003723 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003724 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003725 clear_ftrace_mod_list(mod_head);
3726 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003727 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003728 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003729
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003730 if (!iter->hash) {
3731 trace_parser_put(&iter->parser);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003732 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003733 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003734 } else
3735 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003736
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003737 ret = 0;
3738
Steven Rostedt5072c592008-05-12 21:20:43 +02003739 if (file->f_mode & FMODE_READ) {
3740 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003741
3742 ret = seq_open(file, &show_ftrace_seq_ops);
3743 if (!ret) {
3744 struct seq_file *m = file->private_data;
3745 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003746 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003747 /* Failed */
3748 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003749 trace_parser_put(&iter->parser);
Li Zefan79fe2492009-09-22 13:54:28 +08003750 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003751 } else
3752 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003753
3754 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003755 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003756
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04003757 out:
3758 if (ret) {
3759 kfree(iter);
3760 if (tr)
3761 trace_array_put(tr);
3762 }
3763
Steven Rostedt5072c592008-05-12 21:20:43 +02003764 return ret;
3765}
3766
Steven Rostedt41c52c02008-05-22 11:46:33 -04003767static int
3768ftrace_filter_open(struct inode *inode, struct file *file)
3769{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003770 struct ftrace_ops *ops = inode->i_private;
3771
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003772 /* Checks for tracefs lockdown */
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003773 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003774 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003775 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003776}
3777
3778static int
3779ftrace_notrace_open(struct inode *inode, struct file *file)
3780{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003781 struct ftrace_ops *ops = inode->i_private;
3782
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04003783 /* Checks for tracefs lockdown */
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003784 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003785 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003786}
3787
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003788/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3789struct ftrace_glob {
3790 char *search;
3791 unsigned len;
3792 int type;
3793};
3794
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003795/*
3796 * If symbols in an architecture don't correspond exactly to the user-visible
3797 * name of what they represent, it is possible to define this function to
3798 * perform the necessary adjustments.
3799*/
3800char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3801{
3802 return str;
3803}
3804
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003805static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003806{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003807 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003808 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003809
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003810 str = arch_ftrace_match_adjust(str, g->search);
3811
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003812 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003813 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003814 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003815 matched = 1;
3816 break;
3817 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003818 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003819 matched = 1;
3820 break;
3821 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003822 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003823 matched = 1;
3824 break;
3825 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003826 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003827 if (slen >= g->len &&
3828 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003829 matched = 1;
3830 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003831 case MATCH_GLOB:
3832 if (glob_match(g->search, str))
3833 matched = 1;
3834 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003835 }
3836
3837 return matched;
3838}
3839
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003840static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003841enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003842{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003843 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003844 int ret = 0;
3845
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003846 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003847 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003848 /* Do nothing if it doesn't exist */
3849 if (!entry)
3850 return 0;
3851
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003852 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003853 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003854 /* Do nothing if it exists */
3855 if (entry)
3856 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003857
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003858 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003859 }
3860 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003861}
3862
Steven Rostedt64e7c442009-02-13 17:08:48 -05003863static int
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003864add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3865 int clear_filter)
3866{
3867 long index = simple_strtoul(func_g->search, NULL, 0);
3868 struct ftrace_page *pg;
3869 struct dyn_ftrace *rec;
3870
3871 /* The index starts at 1 */
3872 if (--index < 0)
3873 return 0;
3874
3875 do_for_each_ftrace_rec(pg, rec) {
3876 if (pg->index <= index) {
3877 index -= pg->index;
3878 /* this is a double loop, break goes to the next page */
3879 break;
3880 }
3881 rec = &pg->records[index];
3882 enter_record(hash, rec, clear_filter);
3883 return 1;
3884 } while_for_each_ftrace_rec();
3885 return 0;
3886}
3887
3888static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003889ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3890 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003891{
3892 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003893 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003894
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003895 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3896
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003897 if (mod_g) {
3898 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3899
3900 /* blank module name to match all modules */
3901 if (!mod_g->len) {
3902 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003903 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003904 goto func_match;
3905 return 0;
3906 }
3907
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003908 /*
3909 * exclude_mod is set to trace everything but the given
3910 * module. If it is set and the module matches, then
3911 * return 0. If it is not set, and the module doesn't match
3912 * also return 0. Otherwise, check the function to see if
3913 * that matches.
3914 */
3915 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003916 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003917func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003918 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003919 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003920 return 1;
3921 }
3922
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003923 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003924}
3925
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003926static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003927match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003928{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003929 struct ftrace_page *pg;
3930 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003931 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003932 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3933 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3934 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003935 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003936 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03003937 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003938
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003939 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003940 func_g.type = filter_parse_regex(func, len, &func_g.search,
3941 &clear_filter);
3942 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003943 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003944
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003945 if (mod) {
3946 mod_g.type = filter_parse_regex(mod, strlen(mod),
3947 &mod_g.search, &exclude_mod);
3948 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003949 }
3950
Steven Rostedt52baf112009-02-14 01:15:39 -05003951 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003952
3953 if (unlikely(ftrace_disabled))
3954 goto out_unlock;
3955
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003956 if (func_g.type == MATCH_INDEX) {
3957 found = add_rec_by_index(hash, &func_g, clear_filter);
3958 goto out_unlock;
3959 }
3960
Steven Rostedt265c8312009-02-13 12:43:56 -05003961 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003962
3963 if (rec->flags & FTRACE_FL_DISABLED)
3964 continue;
3965
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003966 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003967 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003968 if (ret < 0) {
3969 found = ret;
3970 goto out_unlock;
3971 }
Li Zefan311d16d2009-12-08 11:15:11 +08003972 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003973 }
3974 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003975 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003976 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003977
3978 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003979}
3980
Steven Rostedt64e7c442009-02-13 17:08:48 -05003981static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003982ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003983{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003984 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003985}
3986
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003987static void ftrace_ops_update_code(struct ftrace_ops *ops,
3988 struct ftrace_ops_hash *old_hash)
3989{
3990 struct ftrace_ops *op;
3991
3992 if (!ftrace_enabled)
3993 return;
3994
3995 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3996 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3997 return;
3998 }
3999
4000 /*
4001 * If this is the shared global_ops filter, then we need to
4002 * check if there is another ops that shares it, is enabled.
4003 * If so, we still need to run the modify code.
4004 */
4005 if (ops->func_hash != &global_ops.local_hash)
4006 return;
4007
4008 do_for_each_ftrace_op(op, ftrace_ops_list) {
4009 if (op->func_hash == &global_ops.local_hash &&
4010 op->flags & FTRACE_OPS_FL_ENABLED) {
4011 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4012 /* Only need to do this once */
4013 return;
4014 }
4015 } while_for_each_ftrace_op(op);
4016}
4017
4018static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
4019 struct ftrace_hash **orig_hash,
4020 struct ftrace_hash *hash,
4021 int enable)
4022{
4023 struct ftrace_ops_hash old_hash_ops;
4024 struct ftrace_hash *old_hash;
4025 int ret;
4026
4027 old_hash = *orig_hash;
4028 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4029 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4030 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4031 if (!ret) {
4032 ftrace_ops_update_code(ops, &old_hash_ops);
4033 free_ftrace_hash_rcu(old_hash);
4034 }
4035 return ret;
4036}
Steven Rostedt64e7c442009-02-13 17:08:48 -05004037
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004038static bool module_exists(const char *module)
4039{
4040 /* All modules have the symbol __this_module */
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01004041 static const char this_mod[] = "__this_module";
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02004042 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004043 unsigned long val;
4044 int n;
4045
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02004046 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004047
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02004048 if (n > sizeof(modname) - 1)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004049 return false;
4050
4051 val = module_kallsyms_lookup_name(modname);
4052 return val != 0;
4053}
4054
4055static int cache_mod(struct trace_array *tr,
4056 const char *func, char *module, int enable)
4057{
4058 struct ftrace_mod_load *ftrace_mod, *n;
4059 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
4060 int ret;
4061
4062 mutex_lock(&ftrace_lock);
4063
4064 /* We do not cache inverse filters */
4065 if (func[0] == '!') {
4066 func++;
4067 ret = -EINVAL;
4068
4069 /* Look to remove this hash */
4070 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4071 if (strcmp(ftrace_mod->module, module) != 0)
4072 continue;
4073
4074 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03004075 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004076 (ftrace_mod->func &&
4077 strcmp(ftrace_mod->func, func) == 0)) {
4078 ret = 0;
4079 free_ftrace_mod(ftrace_mod);
4080 continue;
4081 }
4082 }
4083 goto out;
4084 }
4085
4086 ret = -EINVAL;
4087 /* We only care about modules that have not been loaded yet */
4088 if (module_exists(module))
4089 goto out;
4090
4091 /* Save this string off, and execute it when the module is loaded */
4092 ret = ftrace_add_mod(tr, func, module, enable);
4093 out:
4094 mutex_unlock(&ftrace_lock);
4095
4096 return ret;
4097}
4098
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004099static int
4100ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4101 int reset, int enable);
4102
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004103#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004104static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
4105 char *mod, bool enable)
4106{
4107 struct ftrace_mod_load *ftrace_mod, *n;
4108 struct ftrace_hash **orig_hash, *new_hash;
4109 LIST_HEAD(process_mods);
4110 char *func;
4111 int ret;
4112
4113 mutex_lock(&ops->func_hash->regex_lock);
4114
4115 if (enable)
4116 orig_hash = &ops->func_hash->filter_hash;
4117 else
4118 orig_hash = &ops->func_hash->notrace_hash;
4119
4120 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4121 *orig_hash);
4122 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004123 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004124
4125 mutex_lock(&ftrace_lock);
4126
4127 list_for_each_entry_safe(ftrace_mod, n, head, list) {
4128
4129 if (strcmp(ftrace_mod->module, mod) != 0)
4130 continue;
4131
4132 if (ftrace_mod->func)
4133 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4134 else
4135 func = kstrdup("*", GFP_KERNEL);
4136
4137 if (!func) /* warn? */
4138 continue;
4139
4140 list_del(&ftrace_mod->list);
4141 list_add(&ftrace_mod->list, &process_mods);
4142
4143 /* Use the newly allocated func, as it may be "*" */
4144 kfree(ftrace_mod->func);
4145 ftrace_mod->func = func;
4146 }
4147
4148 mutex_unlock(&ftrace_lock);
4149
4150 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4151
4152 func = ftrace_mod->func;
4153
4154 /* Grabs ftrace_lock, which is why we have this extra step */
4155 match_records(new_hash, func, strlen(func), mod);
4156 free_ftrace_mod(ftrace_mod);
4157 }
4158
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004159 if (enable && list_empty(head))
4160 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4161
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004162 mutex_lock(&ftrace_lock);
4163
4164 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4165 new_hash, enable);
4166 mutex_unlock(&ftrace_lock);
4167
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004168 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004169 mutex_unlock(&ops->func_hash->regex_lock);
4170
4171 free_ftrace_hash(new_hash);
4172}
4173
4174static void process_cached_mods(const char *mod_name)
4175{
4176 struct trace_array *tr;
4177 char *mod;
4178
4179 mod = kstrdup(mod_name, GFP_KERNEL);
4180 if (!mod)
4181 return;
4182
4183 mutex_lock(&trace_types_lock);
4184 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4185 if (!list_empty(&tr->mod_trace))
4186 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4187 if (!list_empty(&tr->mod_notrace))
4188 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4189 }
4190 mutex_unlock(&trace_types_lock);
4191
4192 kfree(mod);
4193}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004194#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004195
Steven Rostedtf6180772009-02-14 00:40:25 -05004196/*
4197 * We register the module command as a template to show others how
4198 * to register the a command as well.
4199 */
4200
4201static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004202ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004203 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004204{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004205 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004206 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004207
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004208 /* match_records() modifies func, and we need the original */
4209 func = kstrdup(func_orig, GFP_KERNEL);
4210 if (!func)
4211 return -ENOMEM;
4212
Steven Rostedtf6180772009-02-14 00:40:25 -05004213 /*
4214 * cmd == 'mod' because we only registered this func
4215 * for the 'mod' ftrace_func_command.
4216 * But if you register one func with multiple commands,
4217 * you can tell which command was used by the cmd
4218 * parameter.
4219 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004220 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004221 kfree(func);
4222
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004223 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004224 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004225 if (ret < 0)
4226 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004227 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004228}
4229
4230static struct ftrace_func_command ftrace_mod_cmd = {
4231 .name = "mod",
4232 .func = ftrace_mod_callback,
4233};
4234
4235static int __init ftrace_mod_cmd_init(void)
4236{
4237 return register_ftrace_command(&ftrace_mod_cmd);
4238}
Steven Rostedt6f415672012-10-05 12:13:07 -04004239core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004240
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004241static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004242 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004243{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004244 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004245 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004246
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004247 probe = container_of(op, struct ftrace_func_probe, ops);
4248 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004249
4250 /*
4251 * Disable preemption for these calls to prevent a RCU grace
4252 * period. This syncs the hash iteration and freeing of items
4253 * on the hash. rcu_read_lock is too dangerous here.
4254 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004255 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004256 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004257 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004258}
4259
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004260struct ftrace_func_map {
4261 struct ftrace_func_entry entry;
4262 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004263};
4264
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004265struct ftrace_func_mapper {
4266 struct ftrace_hash hash;
4267};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004268
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004269/**
4270 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4271 *
4272 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4273 */
4274struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004275{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004276 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004277
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004278 /*
4279 * The mapper is simply a ftrace_hash, but since the entries
4280 * in the hash are not ftrace_func_entry type, we define it
4281 * as a separate structure.
4282 */
4283 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4284 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004285}
4286
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004287/**
4288 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4289 * @mapper: The mapper that has the ip maps
4290 * @ip: the instruction pointer to find the data for
4291 *
4292 * Returns the data mapped to @ip if found otherwise NULL. The return
4293 * is actually the address of the mapper data pointer. The address is
4294 * returned for use cases where the data is no bigger than a long, and
4295 * the user can use the data pointer as its data instead of having to
4296 * allocate more memory for the reference.
4297 */
4298void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4299 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004300{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004301 struct ftrace_func_entry *entry;
4302 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004303
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004304 entry = ftrace_lookup_ip(&mapper->hash, ip);
4305 if (!entry)
4306 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004307
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004308 map = (struct ftrace_func_map *)entry;
4309 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004310}
4311
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004312/**
4313 * ftrace_func_mapper_add_ip - Map some data to an ip
4314 * @mapper: The mapper that has the ip maps
4315 * @ip: The instruction pointer address to map @data to
4316 * @data: The data to map to @ip
4317 *
4318 * Returns 0 on succes otherwise an error.
4319 */
4320int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4321 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004322{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004323 struct ftrace_func_entry *entry;
4324 struct ftrace_func_map *map;
4325
4326 entry = ftrace_lookup_ip(&mapper->hash, ip);
4327 if (entry)
4328 return -EBUSY;
4329
4330 map = kmalloc(sizeof(*map), GFP_KERNEL);
4331 if (!map)
4332 return -ENOMEM;
4333
4334 map->entry.ip = ip;
4335 map->data = data;
4336
4337 __add_hash_entry(&mapper->hash, &map->entry);
4338
4339 return 0;
4340}
4341
4342/**
4343 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4344 * @mapper: The mapper that has the ip maps
4345 * @ip: The instruction pointer address to remove the data from
4346 *
4347 * Returns the data if it is found, otherwise NULL.
4348 * Note, if the data pointer is used as the data itself, (see
4349 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4350 * if the data pointer was set to zero.
4351 */
4352void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4353 unsigned long ip)
4354{
4355 struct ftrace_func_entry *entry;
4356 struct ftrace_func_map *map;
4357 void *data;
4358
4359 entry = ftrace_lookup_ip(&mapper->hash, ip);
4360 if (!entry)
4361 return NULL;
4362
4363 map = (struct ftrace_func_map *)entry;
4364 data = map->data;
4365
4366 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004367 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004368
4369 return data;
4370}
4371
4372/**
4373 * free_ftrace_func_mapper - free a mapping of ips and data
4374 * @mapper: The mapper that has the ip maps
4375 * @free_func: A function to be called on each data item.
4376 *
4377 * This is used to free the function mapper. The @free_func is optional
4378 * and can be used if the data needs to be freed as well.
4379 */
4380void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4381 ftrace_mapper_func free_func)
4382{
4383 struct ftrace_func_entry *entry;
4384 struct ftrace_func_map *map;
4385 struct hlist_head *hhd;
Wei Li04e03d92019-06-06 11:17:54 +08004386 int size, i;
4387
4388 if (!mapper)
4389 return;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004390
4391 if (free_func && mapper->hash.count) {
Wei Li04e03d92019-06-06 11:17:54 +08004392 size = 1 << mapper->hash.size_bits;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004393 for (i = 0; i < size; i++) {
4394 hhd = &mapper->hash.buckets[i];
4395 hlist_for_each_entry(entry, hhd, hlist) {
4396 map = (struct ftrace_func_map *)entry;
4397 free_func(map);
4398 }
4399 }
4400 }
4401 free_ftrace_hash(&mapper->hash);
4402}
4403
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004404static void release_probe(struct ftrace_func_probe *probe)
4405{
4406 struct ftrace_probe_ops *probe_ops;
4407
4408 mutex_lock(&ftrace_lock);
4409
4410 WARN_ON(probe->ref <= 0);
4411
4412 /* Subtract the ref that was used to protect this instance */
4413 probe->ref--;
4414
4415 if (!probe->ref) {
4416 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004417 /*
4418 * Sending zero as ip tells probe_ops to free
4419 * the probe->data itself
4420 */
4421 if (probe_ops->free)
4422 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004423 list_del(&probe->list);
4424 kfree(probe);
4425 }
4426 mutex_unlock(&ftrace_lock);
4427}
4428
4429static void acquire_probe_locked(struct ftrace_func_probe *probe)
4430{
4431 /*
4432 * Add one ref to keep it from being freed when releasing the
4433 * ftrace_lock mutex.
4434 */
4435 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004436}
4437
Steven Rostedt59df055f2009-02-14 15:29:06 -05004438int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004439register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004440 struct ftrace_probe_ops *probe_ops,
4441 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004442{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004443 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004444 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004445 struct ftrace_hash **orig_hash;
4446 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004447 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004448 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004449 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004450 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004451 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004452
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004453 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004454 return -EINVAL;
4455
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004456 /* We do not support '!' for function probes */
4457 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004458 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004459
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004460
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004461 mutex_lock(&ftrace_lock);
4462 /* Check if the probe_ops is already registered */
4463 list_for_each_entry(probe, &tr->func_probes, list) {
4464 if (probe->probe_ops == probe_ops)
4465 break;
4466 }
4467 if (&probe->list == &tr->func_probes) {
4468 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4469 if (!probe) {
4470 mutex_unlock(&ftrace_lock);
4471 return -ENOMEM;
4472 }
4473 probe->probe_ops = probe_ops;
4474 probe->ops.func = function_trace_probe_call;
4475 probe->tr = tr;
4476 ftrace_ops_init(&probe->ops);
4477 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004478 }
4479
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004480 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004481
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004482 mutex_unlock(&ftrace_lock);
4483
Steven Rostedt (VMware)372e0d02019-08-30 16:30:01 -04004484 /*
4485 * Note, there's a small window here that the func_hash->filter_hash
4486 * may be NULL or empty. Need to be carefule when reading the loop.
4487 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004488 mutex_lock(&probe->ops.func_hash->regex_lock);
4489
4490 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004491 old_hash = *orig_hash;
4492 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4493
Naveen N. Rao5b0022d2019-07-04 20:04:42 +05304494 if (!hash) {
4495 ret = -ENOMEM;
4496 goto out;
4497 }
4498
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004499 ret = ftrace_match_records(hash, glob, strlen(glob));
4500
4501 /* Nothing found? */
4502 if (!ret)
4503 ret = -EINVAL;
4504
4505 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004506 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004507
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004508 size = 1 << hash->size_bits;
4509 for (i = 0; i < size; i++) {
4510 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4511 if (ftrace_lookup_ip(old_hash, entry->ip))
4512 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004513 /*
4514 * The caller might want to do something special
4515 * for each function we find. We call the callback
4516 * to give the caller an opportunity to do so.
4517 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004518 if (probe_ops->init) {
4519 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004520 entry->ip, data,
4521 &probe->data);
4522 if (ret < 0) {
4523 if (probe_ops->free && count)
4524 probe_ops->free(probe_ops, tr,
4525 0, probe->data);
4526 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004527 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004528 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004529 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004530 count++;
4531 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004532 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004533
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004534 mutex_lock(&ftrace_lock);
4535
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004536 if (!count) {
4537 /* Nothing was added? */
4538 ret = -EINVAL;
4539 goto out_unlock;
4540 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004541
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004542 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4543 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004544 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004545 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004546
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004547 /* One ref for each new function traced */
4548 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004549
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004550 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4551 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004552
Steven Rostedt59df055f2009-02-14 15:29:06 -05004553 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004554 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004555
4556 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004557 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004558 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004559 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004560 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004561
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004562 release_probe(probe);
4563
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004564 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004565
4566 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004567 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004568 goto out_unlock;
4569
4570 /* Failed to do the move, need to call the free functions */
4571 for (i = 0; i < size; i++) {
4572 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4573 if (ftrace_lookup_ip(old_hash, entry->ip))
4574 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004575 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004576 }
4577 }
4578 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004579}
4580
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004581int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004582unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4583 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004584{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004585 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004586 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004587 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004588 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004589 struct ftrace_hash **orig_hash;
4590 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004591 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004592 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004593 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004594 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004595 int count = 0;
4596 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004597 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004598
Naveen N. Raocbab5672017-05-16 23:21:25 +05304599 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004600 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304601 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004602 int not;
4603
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004604 func_g.type = filter_parse_regex(glob, strlen(glob),
4605 &func_g.search, &not);
4606 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004607
Steven Rostedtb6887d72009-02-17 12:32:04 -05004608 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004609 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004610 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004611 }
4612
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004613 mutex_lock(&ftrace_lock);
4614 /* Check if the probe_ops is already registered */
4615 list_for_each_entry(probe, &tr->func_probes, list) {
4616 if (probe->probe_ops == probe_ops)
4617 break;
4618 }
4619 if (&probe->list == &tr->func_probes)
4620 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004621
4622 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004623 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4624 goto err_unlock_ftrace;
4625
4626 acquire_probe_locked(probe);
4627
4628 mutex_unlock(&ftrace_lock);
4629
4630 mutex_lock(&probe->ops.func_hash->regex_lock);
4631
4632 orig_hash = &probe->ops.func_hash->filter_hash;
4633 old_hash = *orig_hash;
4634
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004635 if (ftrace_hash_empty(old_hash))
4636 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004637
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004638 old_hash_ops.filter_hash = old_hash;
4639 /* Probes only have filters */
4640 old_hash_ops.notrace_hash = NULL;
4641
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004642 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004643 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004644 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004645 goto out_unlock;
4646
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004647 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004648
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004649 size = 1 << hash->size_bits;
4650 for (i = 0; i < size; i++) {
4651 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004652
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004653 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004654 kallsyms_lookup(entry->ip, NULL, NULL,
4655 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004656 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004657 continue;
4658 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004659 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004660 remove_hash_entry(hash, entry);
4661 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004662 }
4663 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004664
4665 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004666 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004667 ret = -EINVAL;
4668 goto out_unlock;
4669 }
4670
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004671 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004672
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004673 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004674
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004675 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004676
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004677 if (ftrace_hash_empty(hash))
4678 ftrace_shutdown(&probe->ops, 0);
4679
4680 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004681 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004682
4683 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004684 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004685 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004686 &old_hash_ops);
Paul E. McKenney74401722018-11-06 18:44:52 -08004687 synchronize_rcu();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004688
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004689 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4690 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004691 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004692 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004693 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004694 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004695 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004696
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004697 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004698 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004699 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004700
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004701 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004702
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004703 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004704
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004705 err_unlock_ftrace:
4706 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004707 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004708}
4709
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304710void clear_ftrace_function_probes(struct trace_array *tr)
4711{
4712 struct ftrace_func_probe *probe, *n;
4713
4714 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4715 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4716}
4717
Steven Rostedtf6180772009-02-14 00:40:25 -05004718static LIST_HEAD(ftrace_commands);
4719static DEFINE_MUTEX(ftrace_cmd_mutex);
4720
Tom Zanussi38de93a2013-10-24 08:34:18 -05004721/*
4722 * Currently we only register ftrace commands from __init, so mark this
4723 * __init too.
4724 */
4725__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004726{
4727 struct ftrace_func_command *p;
4728 int ret = 0;
4729
4730 mutex_lock(&ftrace_cmd_mutex);
4731 list_for_each_entry(p, &ftrace_commands, list) {
4732 if (strcmp(cmd->name, p->name) == 0) {
4733 ret = -EBUSY;
4734 goto out_unlock;
4735 }
4736 }
4737 list_add(&cmd->list, &ftrace_commands);
4738 out_unlock:
4739 mutex_unlock(&ftrace_cmd_mutex);
4740
4741 return ret;
4742}
4743
Tom Zanussi38de93a2013-10-24 08:34:18 -05004744/*
4745 * Currently we only unregister ftrace commands from __init, so mark
4746 * this __init too.
4747 */
4748__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004749{
4750 struct ftrace_func_command *p, *n;
4751 int ret = -ENODEV;
4752
4753 mutex_lock(&ftrace_cmd_mutex);
4754 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4755 if (strcmp(cmd->name, p->name) == 0) {
4756 ret = 0;
4757 list_del_init(&p->list);
4758 goto out_unlock;
4759 }
4760 }
4761 out_unlock:
4762 mutex_unlock(&ftrace_cmd_mutex);
4763
4764 return ret;
4765}
4766
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004767static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004768 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004769{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004770 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004771 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004772 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004773 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004774 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004775
4776 func = strsep(&next, ":");
4777
4778 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004779 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004780 if (!ret)
4781 ret = -EINVAL;
4782 if (ret < 0)
4783 return ret;
4784 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004785 }
4786
Steven Rostedtf6180772009-02-14 00:40:25 -05004787 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004788
4789 command = strsep(&next, ":");
4790
Steven Rostedtf6180772009-02-14 00:40:25 -05004791 mutex_lock(&ftrace_cmd_mutex);
4792 list_for_each_entry(p, &ftrace_commands, list) {
4793 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004794 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004795 goto out_unlock;
4796 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004797 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004798 out_unlock:
4799 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004800
Steven Rostedtf6180772009-02-14 00:40:25 -05004801 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004802}
4803
Ingo Molnare309b412008-05-12 21:20:51 +02004804static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004805ftrace_regex_write(struct file *file, const char __user *ubuf,
4806 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004807{
4808 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004809 struct trace_parser *parser;
4810 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004811
Li Zefan4ba79782009-09-22 13:52:20 +08004812 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004813 return 0;
4814
Steven Rostedt5072c592008-05-12 21:20:43 +02004815 if (file->f_mode & FMODE_READ) {
4816 struct seq_file *m = file->private_data;
4817 iter = m->private;
4818 } else
4819 iter = file->private_data;
4820
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004821 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004822 return -ENODEV;
4823
4824 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004825
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004826 parser = &iter->parser;
4827 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004828
Li Zefan4ba79782009-09-22 13:52:20 +08004829 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004830 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004831 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004832 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004833 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004834 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004835 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004836 }
4837
Steven Rostedt5072c592008-05-12 21:20:43 +02004838 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004839 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004840 return ret;
4841}
4842
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004843ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004844ftrace_filter_write(struct file *file, const char __user *ubuf,
4845 size_t cnt, loff_t *ppos)
4846{
4847 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4848}
4849
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004850ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004851ftrace_notrace_write(struct file *file, const char __user *ubuf,
4852 size_t cnt, loff_t *ppos)
4853{
4854 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4855}
4856
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004857static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004858ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4859{
4860 struct ftrace_func_entry *entry;
4861
4862 if (!ftrace_location(ip))
4863 return -EINVAL;
4864
4865 if (remove) {
4866 entry = ftrace_lookup_ip(hash, ip);
4867 if (!entry)
4868 return -ENOENT;
4869 free_hash_entry(hash, entry);
4870 return 0;
4871 }
4872
4873 return add_hash_entry(hash, ip);
4874}
4875
4876static int
4877ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4878 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004879{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004880 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004881 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004882 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004883
Steven Rostedt41c52c02008-05-22 11:46:33 -04004884 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004885 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004886
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004887 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004888
Steven Rostedtf45948e2011-05-02 12:29:25 -04004889 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004890 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004891 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004892 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004893
Wang Nanb972cc52014-07-15 08:40:20 +08004894 if (reset)
4895 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4896 else
4897 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4898
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004899 if (!hash) {
4900 ret = -ENOMEM;
4901 goto out_regex_unlock;
4902 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004903
Jiri Olsaac483c42012-01-02 10:04:14 +01004904 if (buf && !ftrace_match_records(hash, buf, len)) {
4905 ret = -EINVAL;
4906 goto out_regex_unlock;
4907 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004908 if (ip) {
4909 ret = ftrace_match_addr(hash, ip, remove);
4910 if (ret < 0)
4911 goto out_regex_unlock;
4912 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004913
4914 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004915 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004916 mutex_unlock(&ftrace_lock);
4917
Jiri Olsaac483c42012-01-02 10:04:14 +01004918 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004919 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004920
4921 free_ftrace_hash(hash);
4922 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004923}
4924
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004925static int
4926ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4927 int reset, int enable)
4928{
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05304929 return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004930}
4931
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05004932#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05004933
4934struct ftrace_direct_func {
4935 struct list_head next;
4936 unsigned long addr;
4937 int count;
4938};
4939
4940static LIST_HEAD(ftrace_direct_funcs);
4941
4942/**
4943 * ftrace_find_direct_func - test an address if it is a registered direct caller
4944 * @addr: The address of a registered direct caller
4945 *
4946 * This searches to see if a ftrace direct caller has been registered
4947 * at a specific address, and if so, it returns a descriptor for it.
4948 *
4949 * This can be used by architecture code to see if an address is
4950 * a direct caller (trampoline) attached to a fentry/mcount location.
4951 * This is useful for the function_graph tracer, as it may need to
4952 * do adjustments if it traced a location that also has a direct
4953 * trampoline attached to it.
4954 */
4955struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
4956{
4957 struct ftrace_direct_func *entry;
4958 bool found = false;
4959
4960 /* May be called by fgraph trampoline (protected by rcu tasks) */
4961 list_for_each_entry_rcu(entry, &ftrace_direct_funcs, next) {
4962 if (entry->addr == addr) {
4963 found = true;
4964 break;
4965 }
4966 }
4967 if (found)
4968 return entry;
4969
4970 return NULL;
4971}
4972
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05004973/**
4974 * register_ftrace_direct - Call a custom trampoline directly
4975 * @ip: The address of the nop at the beginning of a function
4976 * @addr: The address of the trampoline to call at @ip
4977 *
4978 * This is used to connect a direct call from the nop location (@ip)
4979 * at the start of ftrace traced functions. The location that it calls
4980 * (@addr) must be able to handle a direct call, and save the parameters
4981 * of the function being traced, and restore them (or inject new ones
4982 * if needed), before returning.
4983 *
4984 * Returns:
4985 * 0 on success
4986 * -EBUSY - Another direct function is already attached (there can be only one)
4987 * -ENODEV - @ip does not point to a ftrace nop location (or not supported)
4988 * -ENOMEM - There was an allocation failure.
4989 */
4990int register_ftrace_direct(unsigned long ip, unsigned long addr)
4991{
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05004992 struct ftrace_direct_func *direct;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05004993 struct ftrace_func_entry *entry;
4994 struct ftrace_hash *free_hash = NULL;
4995 struct dyn_ftrace *rec;
4996 int ret = -EBUSY;
4997
4998 mutex_lock(&direct_mutex);
4999
5000 /* See if there's a direct function at @ip already */
Alexei Starovoitovff205762019-12-08 16:01:12 -08005001 if (ftrace_find_rec_direct(ip))
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005002 goto out_unlock;
5003
5004 ret = -ENODEV;
5005 rec = lookup_rec(ip, ip);
5006 if (!rec)
5007 goto out_unlock;
5008
5009 /*
5010 * Check if the rec says it has a direct call but we didn't
5011 * find one earlier?
5012 */
5013 if (WARN_ON(rec->flags & FTRACE_FL_DIRECT))
5014 goto out_unlock;
5015
5016 /* Make sure the ip points to the exact record */
Steven Rostedt (VMware)406acdd2019-11-15 14:19:04 -05005017 if (ip != rec->ip) {
5018 ip = rec->ip;
5019 /* Need to check this ip for a direct. */
Alexei Starovoitovff205762019-12-08 16:01:12 -08005020 if (ftrace_find_rec_direct(ip))
Steven Rostedt (VMware)406acdd2019-11-15 14:19:04 -05005021 goto out_unlock;
5022 }
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005023
5024 ret = -ENOMEM;
5025 if (ftrace_hash_empty(direct_functions) ||
5026 direct_functions->count > 2 * (1 << direct_functions->size_bits)) {
5027 struct ftrace_hash *new_hash;
5028 int size = ftrace_hash_empty(direct_functions) ? 0 :
5029 direct_functions->count + 1;
5030
5031 if (size < 32)
5032 size = 32;
5033
5034 new_hash = dup_hash(direct_functions, size);
5035 if (!new_hash)
5036 goto out_unlock;
5037
5038 free_hash = direct_functions;
5039 direct_functions = new_hash;
5040 }
5041
5042 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
5043 if (!entry)
5044 goto out_unlock;
5045
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005046 direct = ftrace_find_direct_func(addr);
5047 if (!direct) {
5048 direct = kmalloc(sizeof(*direct), GFP_KERNEL);
5049 if (!direct) {
5050 kfree(entry);
5051 goto out_unlock;
5052 }
5053 direct->addr = addr;
5054 direct->count = 0;
5055 list_add_rcu(&direct->next, &ftrace_direct_funcs);
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05005056 ftrace_direct_func_count++;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005057 }
5058
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005059 entry->ip = ip;
5060 entry->direct = addr;
5061 __add_hash_entry(direct_functions, entry);
5062
5063 ret = ftrace_set_filter_ip(&direct_ops, ip, 0, 0);
5064 if (ret)
5065 remove_hash_entry(direct_functions, entry);
5066
5067 if (!ret && !(direct_ops.flags & FTRACE_OPS_FL_ENABLED)) {
5068 ret = register_ftrace_function(&direct_ops);
5069 if (ret)
5070 ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5071 }
5072
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005073 if (ret) {
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005074 kfree(entry);
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005075 if (!direct->count) {
5076 list_del_rcu(&direct->next);
5077 synchronize_rcu_tasks();
5078 kfree(direct);
5079 if (free_hash)
5080 free_ftrace_hash(free_hash);
5081 free_hash = NULL;
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05005082 ftrace_direct_func_count--;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005083 }
5084 } else {
Steven Rostedt (VMware)1c7f9b62019-11-15 14:13:20 -05005085 direct->count++;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005086 }
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005087 out_unlock:
5088 mutex_unlock(&direct_mutex);
5089
5090 if (free_hash) {
5091 synchronize_rcu_tasks();
5092 free_ftrace_hash(free_hash);
5093 }
5094
5095 return ret;
5096}
5097EXPORT_SYMBOL_GPL(register_ftrace_direct);
5098
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005099static struct ftrace_func_entry *find_direct_entry(unsigned long *ip,
5100 struct dyn_ftrace **recp)
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005101{
5102 struct ftrace_func_entry *entry;
5103 struct dyn_ftrace *rec;
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005104
5105 rec = lookup_rec(*ip, *ip);
5106 if (!rec)
5107 return NULL;
5108
5109 entry = __ftrace_lookup_ip(direct_functions, rec->ip);
5110 if (!entry) {
5111 WARN_ON(rec->flags & FTRACE_FL_DIRECT);
5112 return NULL;
5113 }
5114
5115 WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
5116
5117 /* Passed in ip just needs to be on the call site */
5118 *ip = rec->ip;
5119
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005120 if (recp)
5121 *recp = rec;
5122
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005123 return entry;
5124}
5125
5126int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
5127{
5128 struct ftrace_direct_func *direct;
5129 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005130 int ret = -ENODEV;
5131
5132 mutex_lock(&direct_mutex);
5133
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005134 entry = find_direct_entry(&ip, NULL);
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005135 if (!entry)
5136 goto out_unlock;
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005137
5138 if (direct_functions->count == 1)
5139 unregister_ftrace_function(&direct_ops);
5140
5141 ret = ftrace_set_filter_ip(&direct_ops, ip, 1, 0);
5142
5143 WARN_ON(ret);
5144
5145 remove_hash_entry(direct_functions, entry);
5146
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005147 direct = ftrace_find_direct_func(addr);
5148 if (!WARN_ON(!direct)) {
5149 /* This is the good path (see the ! before WARN) */
5150 direct->count--;
5151 WARN_ON(direct->count < 0);
5152 if (!direct->count) {
5153 list_del_rcu(&direct->next);
5154 synchronize_rcu_tasks();
5155 kfree(direct);
Steven Rostedt (VMware)a3ad1a72019-11-08 13:12:57 -05005156 ftrace_direct_func_count--;
Steven Rostedt (VMware)013bf0d2019-11-08 13:11:27 -05005157 }
5158 }
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005159 out_unlock:
5160 mutex_unlock(&direct_mutex);
5161
5162 return ret;
5163}
5164EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005165
5166static struct ftrace_ops stub_ops = {
5167 .func = ftrace_stub,
5168};
5169
5170/**
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005171 * ftrace_modify_direct_caller - modify ftrace nop directly
5172 * @entry: The ftrace hash entry of the direct helper for @rec
5173 * @rec: The record representing the function site to patch
5174 * @old_addr: The location that the site at @rec->ip currently calls
5175 * @new_addr: The location that the site at @rec->ip should call
5176 *
5177 * An architecture may overwrite this function to optimize the
5178 * changing of the direct callback on an ftrace nop location.
5179 * This is called with the ftrace_lock mutex held, and no other
5180 * ftrace callbacks are on the associated record (@rec). Thus,
5181 * it is safe to modify the ftrace record, where it should be
5182 * currently calling @old_addr directly, to call @new_addr.
5183 *
5184 * Safety checks should be made to make sure that the code at
5185 * @rec->ip is currently calling @old_addr. And this must
5186 * also update entry->direct to @new_addr.
5187 */
5188int __weak ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
5189 struct dyn_ftrace *rec,
5190 unsigned long old_addr,
5191 unsigned long new_addr)
5192{
5193 unsigned long ip = rec->ip;
5194 int ret;
5195
5196 /*
5197 * The ftrace_lock was used to determine if the record
5198 * had more than one registered user to it. If it did,
5199 * we needed to prevent that from changing to do the quick
5200 * switch. But if it did not (only a direct caller was attached)
5201 * then this function is called. But this function can deal
5202 * with attached callers to the rec that we care about, and
5203 * since this function uses standard ftrace calls that take
5204 * the ftrace_lock mutex, we need to release it.
5205 */
5206 mutex_unlock(&ftrace_lock);
5207
5208 /*
5209 * By setting a stub function at the same address, we force
5210 * the code to call the iterator and the direct_ops helper.
5211 * This means that @ip does not call the direct call, and
5212 * we can simply modify it.
5213 */
5214 ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
5215 if (ret)
5216 goto out_lock;
5217
5218 ret = register_ftrace_function(&stub_ops);
5219 if (ret) {
5220 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5221 goto out_lock;
5222 }
5223
5224 entry->direct = new_addr;
5225
5226 /*
5227 * By removing the stub, we put back the direct call, calling
5228 * the @new_addr.
5229 */
5230 unregister_ftrace_function(&stub_ops);
5231 ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
5232
5233 out_lock:
5234 mutex_lock(&ftrace_lock);
5235
5236 return ret;
5237}
5238
5239/**
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005240 * modify_ftrace_direct - Modify an existing direct call to call something else
5241 * @ip: The instruction pointer to modify
5242 * @old_addr: The address that the current @ip calls directly
5243 * @new_addr: The address that the @ip should call
5244 *
5245 * This modifies a ftrace direct caller at an instruction pointer without
5246 * having to disable it first. The direct call will switch over to the
5247 * @new_addr without missing anything.
5248 *
5249 * Returns: zero on success. Non zero on error, which includes:
5250 * -ENODEV : the @ip given has no direct caller attached
5251 * -EINVAL : the @old_addr does not match the current direct caller
5252 */
5253int modify_ftrace_direct(unsigned long ip,
5254 unsigned long old_addr, unsigned long new_addr)
5255{
5256 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005257 struct dyn_ftrace *rec;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005258 int ret = -ENODEV;
5259
5260 mutex_lock(&direct_mutex);
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005261
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005262 mutex_lock(&ftrace_lock);
5263 entry = find_direct_entry(&ip, &rec);
Steven Rostedt (VMware)128161f2019-11-15 14:14:45 -05005264 if (!entry)
5265 goto out_unlock;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005266
5267 ret = -EINVAL;
5268 if (entry->direct != old_addr)
5269 goto out_unlock;
5270
5271 /*
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005272 * If there's no other ftrace callback on the rec->ip location,
5273 * then it can be changed directly by the architecture.
5274 * If there is another caller, then we just need to change the
5275 * direct caller helper to point to @new_addr.
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005276 */
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005277 if (ftrace_rec_count(rec) == 1) {
5278 ret = ftrace_modify_direct_caller(entry, rec, old_addr, new_addr);
5279 } else {
5280 entry->direct = new_addr;
5281 ret = 0;
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005282 }
5283
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005284 out_unlock:
Steven Rostedt (VMware)ea806eb2019-11-17 17:04:15 -05005285 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)0567d682019-11-14 14:39:35 -05005286 mutex_unlock(&direct_mutex);
5287 return ret;
5288}
5289EXPORT_SYMBOL_GPL(modify_ftrace_direct);
Steven Rostedt (VMware)763e34e2019-11-08 13:07:06 -05005290#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
5291
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005292/**
5293 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
5294 * @ops - the ops to set the filter with
5295 * @ip - the address to add to or remove from the filter.
5296 * @remove - non zero to remove the ip from the filter
5297 * @reset - non zero to reset all filters before applying this filter.
5298 *
5299 * Filters denote which functions should be enabled when tracing is enabled
5300 * If @ip is NULL, it failes to update filter.
5301 */
5302int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
5303 int remove, int reset)
5304{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005305 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005306 return ftrace_set_addr(ops, ip, remove, reset, 1);
5307}
5308EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
5309
Joel Fernandesd032ae82016-11-15 12:31:20 -08005310/**
5311 * ftrace_ops_set_global_filter - setup ops to use global filters
5312 * @ops - the ops which will use the global filters
5313 *
5314 * ftrace users who need global function trace filtering should call this.
5315 * It can set the global filter only if ops were not initialized before.
5316 */
5317void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
5318{
5319 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
5320 return;
5321
5322 ftrace_ops_init(ops);
5323 ops->func_hash = &global_ops.local_hash;
5324}
5325EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
5326
Masami Hiramatsu647664e2012-06-05 19:28:08 +09005327static int
5328ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
5329 int reset, int enable)
5330{
5331 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
5332}
5333
Steven Rostedt77a2b372008-05-12 21:20:45 +02005334/**
5335 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04005336 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02005337 * @buf - the string that holds the function filter text.
5338 * @len - the length of the string.
5339 * @reset - non zero to reset all filters before applying this filter.
5340 *
5341 * Filters denote which functions should be enabled when tracing is enabled.
5342 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5343 */
Jiri Olsaac483c42012-01-02 10:04:14 +01005344int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04005345 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02005346{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005347 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01005348 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04005349}
Steven Rostedt936e0742011-05-05 22:54:01 -04005350EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005351
Steven Rostedt41c52c02008-05-22 11:46:33 -04005352/**
5353 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04005354 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04005355 * @buf - the string that holds the function notrace text.
5356 * @len - the length of the string.
5357 * @reset - non zero to reset all filters before applying this filter.
5358 *
5359 * Notrace Filters denote which functions should not be enabled when tracing
5360 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5361 * for tracing.
5362 */
Jiri Olsaac483c42012-01-02 10:04:14 +01005363int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04005364 int len, int reset)
5365{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005366 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01005367 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04005368}
5369EXPORT_SYMBOL_GPL(ftrace_set_notrace);
5370/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08005371 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04005372 * @buf - the string that holds the function filter text.
5373 * @len - the length of the string.
5374 * @reset - non zero to reset all filters before applying this filter.
5375 *
5376 * Filters denote which functions should be enabled when tracing is enabled.
5377 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
5378 */
5379void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
5380{
5381 ftrace_set_regex(&global_ops, buf, len, reset, 1);
5382}
5383EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
5384
5385/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08005386 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04005387 * @buf - the string that holds the function notrace text.
5388 * @len - the length of the string.
5389 * @reset - non zero to reset all filters before applying this filter.
5390 *
5391 * Notrace Filters denote which functions should not be enabled when tracing
5392 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
5393 * for tracing.
5394 */
5395void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04005396{
Steven Rostedtf45948e2011-05-02 12:29:25 -04005397 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02005398}
Steven Rostedt936e0742011-05-05 22:54:01 -04005399EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02005400
Steven Rostedt2af15d62009-05-28 13:37:24 -04005401/*
5402 * command line interface to allow users to set filters on boot up.
5403 */
5404#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
5405static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
5406static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
5407
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04005408/* Used by function selftest to not test if filter is set */
5409bool ftrace_filter_param __initdata;
5410
Steven Rostedt2af15d62009-05-28 13:37:24 -04005411static int __init set_ftrace_notrace(char *str)
5412{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04005413 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08005414 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005415 return 1;
5416}
5417__setup("ftrace_notrace=", set_ftrace_notrace);
5418
5419static int __init set_ftrace_filter(char *str)
5420{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04005421 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08005422 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005423 return 1;
5424}
5425__setup("ftrace_filter=", set_ftrace_filter);
5426
Stefan Assmann369bc182009-10-12 22:17:21 +02005427#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08005428static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005429static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005430static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05005431
Stefan Assmann369bc182009-10-12 22:17:21 +02005432static int __init set_graph_function(char *str)
5433{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02005434 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02005435 return 1;
5436}
5437__setup("ftrace_graph_filter=", set_graph_function);
5438
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005439static int __init set_graph_notrace_function(char *str)
5440{
5441 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
5442 return 1;
5443}
5444__setup("ftrace_graph_notrace=", set_graph_notrace_function);
5445
Todd Brandt65a50c62017-03-02 16:12:15 -08005446static int __init set_graph_max_depth_function(char *str)
5447{
5448 if (!str)
5449 return 0;
5450 fgraph_max_depth = simple_strtoul(str, NULL, 0);
5451 return 1;
5452}
5453__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
5454
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005455static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02005456{
5457 int ret;
5458 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005459 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005460
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005461 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05005462 if (MEM_FAIL(!hash, "Failed to allocate hash\n"))
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005463 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02005464
5465 while (buf) {
5466 func = strsep(&buf, ",");
5467 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005468 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02005469 if (ret)
5470 printk(KERN_DEBUG "ftrace: function %s not "
5471 "traceable\n", func);
5472 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05005473
5474 if (enable)
5475 ftrace_graph_hash = hash;
5476 else
5477 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02005478}
5479#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5480
Steven Rostedt2a85a372011-12-19 21:57:44 -05005481void __init
5482ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04005483{
5484 char *func;
5485
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005486 ftrace_ops_init(ops);
5487
Steven Rostedt2af15d62009-05-28 13:37:24 -04005488 while (buf) {
5489 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04005490 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005491 }
5492}
5493
5494static void __init set_ftrace_early_filters(void)
5495{
5496 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005497 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04005498 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05005499 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005500#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5501 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09005502 set_ftrace_early_graph(ftrace_graph_buf, 1);
5503 if (ftrace_graph_notrace_buf[0])
5504 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02005505#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04005506}
5507
Steven Rostedtfc13cb02011-12-19 14:41:25 -05005508int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02005509{
5510 struct seq_file *m = (struct seq_file *)file->private_data;
5511 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005512 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005513 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04005514 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005515 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02005516
Steven Rostedt5072c592008-05-12 21:20:43 +02005517 if (file->f_mode & FMODE_READ) {
5518 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02005519 seq_release(inode, file);
5520 } else
5521 iter = file->private_data;
5522
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005523 parser = &iter->parser;
5524 if (trace_parser_loaded(parser)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005525 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02005526 }
5527
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005528 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005529
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005530 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005531
Steven Rostedt058e2972011-04-29 22:35:33 -04005532 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04005533 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5534
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005535 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005536 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04005537 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005538 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5539 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005540 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005541
Steven Rostedt058e2972011-04-29 22:35:33 -04005542 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04005543 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5544 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04005545 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04005546 } else {
5547 /* For read only, the hash is the ops hash */
5548 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04005549 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005550
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005551 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005552 free_ftrace_hash(iter->hash);
Steven Rostedt (VMware)9ef16692019-10-11 17:56:57 -04005553 if (iter->tr)
5554 trace_array_put(iter->tr);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005555 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04005556
Steven Rostedt5072c592008-05-12 21:20:43 +02005557 return 0;
5558}
5559
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005560static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005561 .open = ftrace_avail_open,
5562 .read = seq_read,
5563 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08005564 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02005565};
5566
Steven Rostedt647bcd02011-05-03 14:39:21 -04005567static const struct file_operations ftrace_enabled_fops = {
5568 .open = ftrace_enabled_open,
5569 .read = seq_read,
5570 .llseek = seq_lseek,
5571 .release = seq_release_private,
5572};
5573
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005574static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005575 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005576 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005577 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005578 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005579 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005580};
5581
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005582static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005583 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005584 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005585 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005586 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005587 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005588};
5589
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005590#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5591
5592static DEFINE_MUTEX(graph_lock);
5593
Amol Grover24a97292020-02-01 12:57:04 +05305594struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
Amol Groverfd0e6852020-02-05 11:27:02 +05305595struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005596
5597enum graph_filter_type {
5598 GRAPH_FILTER_NOTRACE = 0,
5599 GRAPH_FILTER_FUNCTION,
5600};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005601
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005602#define FTRACE_GRAPH_EMPTY ((void *)1)
5603
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005604struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005605 struct ftrace_hash *hash;
5606 struct ftrace_func_entry *entry;
5607 int idx; /* for hash table iteration */
5608 enum graph_filter_type type;
5609 struct ftrace_hash *new_hash;
5610 const struct seq_operations *seq_ops;
5611 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005612};
5613
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005614static void *
Li Zefan85951842009-06-24 09:54:00 +08005615__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005616{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005617 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005618 struct ftrace_func_entry *entry = fgd->entry;
5619 struct hlist_head *head;
5620 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005621
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005622 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005623 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005624
5625 if (entry) {
5626 hlist_for_each_entry_continue(entry, hlist) {
5627 fgd->entry = entry;
5628 return entry;
5629 }
5630
5631 idx++;
5632 }
5633
5634 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5635 head = &fgd->hash->buckets[i];
5636 hlist_for_each_entry(entry, head, hlist) {
5637 fgd->entry = entry;
5638 fgd->idx = i;
5639 return entry;
5640 }
5641 }
5642 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005643}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005644
Li Zefan85951842009-06-24 09:54:00 +08005645static void *
5646g_next(struct seq_file *m, void *v, loff_t *pos)
5647{
5648 (*pos)++;
5649 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005650}
5651
5652static void *g_start(struct seq_file *m, loff_t *pos)
5653{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005654 struct ftrace_graph_data *fgd = m->private;
5655
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005656 mutex_lock(&graph_lock);
5657
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005658 if (fgd->type == GRAPH_FILTER_FUNCTION)
5659 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5660 lockdep_is_held(&graph_lock));
5661 else
5662 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5663 lockdep_is_held(&graph_lock));
5664
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005665 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005666 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005667 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005668
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005669 fgd->idx = 0;
5670 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005671 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005672}
5673
5674static void g_stop(struct seq_file *m, void *p)
5675{
5676 mutex_unlock(&graph_lock);
5677}
5678
5679static int g_show(struct seq_file *m, void *v)
5680{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005681 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005682
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005683 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005684 return 0;
5685
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005686 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005687 struct ftrace_graph_data *fgd = m->private;
5688
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005689 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005690 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005691 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005692 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005693 return 0;
5694 }
5695
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005696 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005697
5698 return 0;
5699}
5700
James Morris88e9d342009-09-22 16:43:43 -07005701static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005702 .start = g_start,
5703 .next = g_next,
5704 .stop = g_stop,
5705 .show = g_show,
5706};
5707
5708static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005709__ftrace_graph_open(struct inode *inode, struct file *file,
5710 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005711{
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04005712 int ret;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005713 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005714
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04005715 ret = security_locked_down(LOCKDOWN_TRACEFS);
5716 if (ret)
5717 return ret;
5718
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005719 if (file->f_mode & FMODE_WRITE) {
5720 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5721
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005722 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5723 return -ENOMEM;
5724
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005725 if (file->f_flags & O_TRUNC)
5726 new_hash = alloc_ftrace_hash(size_bits);
5727 else
5728 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5729 fgd->hash);
5730 if (!new_hash) {
5731 ret = -ENOMEM;
5732 goto out;
5733 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005734 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005735
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005736 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005737 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005738 if (!ret) {
5739 struct seq_file *m = file->private_data;
5740 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005741 } else {
5742 /* Failed */
5743 free_ftrace_hash(new_hash);
5744 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005745 }
5746 } else
5747 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005748
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005749out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005750 if (ret < 0 && file->f_mode & FMODE_WRITE)
5751 trace_parser_put(&fgd->parser);
5752
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005753 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005754
5755 /*
5756 * All uses of fgd->hash must be taken with the graph_lock
5757 * held. The graph_lock is going to be released, so force
5758 * fgd->hash to be reinitialized when it is taken again.
5759 */
5760 fgd->hash = NULL;
5761
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005762 return ret;
5763}
5764
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005765static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005766ftrace_graph_open(struct inode *inode, struct file *file)
5767{
5768 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005769 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005770
5771 if (unlikely(ftrace_disabled))
5772 return -ENODEV;
5773
5774 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5775 if (fgd == NULL)
5776 return -ENOMEM;
5777
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005778 mutex_lock(&graph_lock);
5779
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005780 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5781 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005782 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005783 fgd->seq_ops = &ftrace_graph_seq_ops;
5784
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005785 ret = __ftrace_graph_open(inode, file, fgd);
5786 if (ret < 0)
5787 kfree(fgd);
5788
5789 mutex_unlock(&graph_lock);
5790 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005791}
5792
5793static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005794ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5795{
5796 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005797 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005798
5799 if (unlikely(ftrace_disabled))
5800 return -ENODEV;
5801
5802 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5803 if (fgd == NULL)
5804 return -ENOMEM;
5805
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005806 mutex_lock(&graph_lock);
5807
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005808 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5809 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005810 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005811 fgd->seq_ops = &ftrace_graph_seq_ops;
5812
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005813 ret = __ftrace_graph_open(inode, file, fgd);
5814 if (ret < 0)
5815 kfree(fgd);
5816
5817 mutex_unlock(&graph_lock);
5818 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005819}
5820
5821static int
Li Zefan87827112009-07-23 11:29:11 +08005822ftrace_graph_release(struct inode *inode, struct file *file)
5823{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005824 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005825 struct ftrace_hash *old_hash, *new_hash;
5826 struct trace_parser *parser;
5827 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005828
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005829 if (file->f_mode & FMODE_READ) {
5830 struct seq_file *m = file->private_data;
5831
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005832 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005833 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005834 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005835 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005836 }
5837
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005838
5839 if (file->f_mode & FMODE_WRITE) {
5840
5841 parser = &fgd->parser;
5842
5843 if (trace_parser_loaded((parser))) {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005844 ret = ftrace_graph_set_hash(fgd->new_hash,
5845 parser->buffer);
5846 }
5847
5848 trace_parser_put(parser);
5849
5850 new_hash = __ftrace_hash_move(fgd->new_hash);
5851 if (!new_hash) {
5852 ret = -ENOMEM;
5853 goto out;
5854 }
5855
5856 mutex_lock(&graph_lock);
5857
5858 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5859 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5860 lockdep_is_held(&graph_lock));
5861 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5862 } else {
5863 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5864 lockdep_is_held(&graph_lock));
5865 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5866 }
5867
5868 mutex_unlock(&graph_lock);
5869
Steven Rostedt (VMware)54a16ff2020-02-05 09:20:32 -05005870 /*
5871 * We need to do a hard force of sched synchronization.
5872 * This is because we use preempt_disable() to do RCU, but
5873 * the function tracers can be called where RCU is not watching
5874 * (like before user_exit()). We can not rely on the RCU
5875 * infrastructure to do the synchronization, thus we must do it
5876 * ourselves.
5877 */
5878 schedule_on_each_cpu(ftrace_sync);
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005879
5880 free_ftrace_hash(old_hash);
5881 }
5882
5883 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005884 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005885 kfree(fgd);
5886
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005887 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005888}
5889
5890static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005891ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005892{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005893 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005894 struct dyn_ftrace *rec;
5895 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005896 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005897 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005898 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005899
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005900 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005901 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5902 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005903
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005904 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005905
Steven Rostedt52baf112009-02-14 01:15:39 -05005906 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005907
5908 if (unlikely(ftrace_disabled)) {
5909 mutex_unlock(&ftrace_lock);
5910 return -ENODEV;
5911 }
5912
Steven Rostedt265c8312009-02-13 12:43:56 -05005913 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005914
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005915 if (rec->flags & FTRACE_FL_DISABLED)
5916 continue;
5917
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005918 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005919 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005920
5921 if (!not) {
5922 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005923
5924 if (entry)
5925 continue;
5926 if (add_hash_entry(hash, rec->ip) < 0)
5927 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005928 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005929 if (entry) {
5930 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005931 fail = 0;
5932 }
5933 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005934 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005935 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005936out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005937 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005938
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005939 if (fail)
5940 return -EINVAL;
5941
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005942 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005943}
5944
5945static ssize_t
5946ftrace_graph_write(struct file *file, const char __user *ubuf,
5947 size_t cnt, loff_t *ppos)
5948{
Namhyung Kim6a101082013-10-14 17:24:25 +09005949 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005950 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005951 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005952
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005953 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005954 return 0;
5955
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005956 /* Read mode uses seq functions */
5957 if (file->f_mode & FMODE_READ) {
5958 struct seq_file *m = file->private_data;
5959 fgd = m->private;
5960 }
5961
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005962 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005963
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005964 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005965
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005966 if (read >= 0 && trace_parser_loaded(parser) &&
5967 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005968
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005969 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005970 parser->buffer);
5971 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005972 }
5973
Namhyung Kim6a101082013-10-14 17:24:25 +09005974 if (!ret)
5975 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005976
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005977 return ret;
5978}
5979
5980static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005981 .open = ftrace_graph_open,
5982 .read = seq_read,
5983 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005984 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005985 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005986};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005987
5988static const struct file_operations ftrace_graph_notrace_fops = {
5989 .open = ftrace_graph_notrace_open,
5990 .read = seq_read,
5991 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005992 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005993 .release = ftrace_graph_release,
5994};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005995#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5996
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005997void ftrace_create_filter_files(struct ftrace_ops *ops,
5998 struct dentry *parent)
5999{
6000
6001 trace_create_file("set_ftrace_filter", 0644, parent,
6002 ops, &ftrace_filter_fops);
6003
6004 trace_create_file("set_ftrace_notrace", 0644, parent,
6005 ops, &ftrace_notrace_fops);
6006}
6007
6008/*
6009 * The name "destroy_filter_files" is really a misnomer. Although
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05306010 * in the future, it may actually delete the files, but this is
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006011 * really intended to make sure the ops passed in are disabled
6012 * and that when this function returns, the caller is free to
6013 * free the ops.
6014 *
6015 * The "destroy" name is only to match the "create" name that this
6016 * should be paired with.
6017 */
6018void ftrace_destroy_filter_files(struct ftrace_ops *ops)
6019{
6020 mutex_lock(&ftrace_lock);
6021 if (ops->flags & FTRACE_OPS_FL_ENABLED)
6022 ftrace_shutdown(ops, 0);
6023 ops->flags |= FTRACE_OPS_FL_DELETED;
Steven Rostedt (VMware)2840f842018-12-10 23:58:01 -05006024 ftrace_free_filter(ops);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006025 mutex_unlock(&ftrace_lock);
6026}
6027
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006028static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02006029{
Steven Rostedt5072c592008-05-12 21:20:43 +02006030
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006031 trace_create_file("available_filter_functions", 0444,
6032 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02006033
Steven Rostedt647bcd02011-05-03 14:39:21 -04006034 trace_create_file("enabled_functions", 0444,
6035 d_tracer, NULL, &ftrace_enabled_fops);
6036
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05006037 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04006038
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006039#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Chen LinX1ce05002014-09-03 14:31:09 +08006040 trace_create_file("set_graph_function", 0644, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006041 NULL,
6042 &ftrace_graph_fops);
Chen LinX1ce05002014-09-03 14:31:09 +08006043 trace_create_file("set_graph_notrace", 0644, d_tracer,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09006044 NULL,
6045 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05006046#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
6047
Steven Rostedt5072c592008-05-12 21:20:43 +02006048 return 0;
6049}
6050
Steven Rostedt9fd49322012-04-24 22:32:06 -04006051static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05006052{
Steven Rostedt9fd49322012-04-24 22:32:06 -04006053 const unsigned long *ipa = a;
6054 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05006055
Steven Rostedt9fd49322012-04-24 22:32:06 -04006056 if (*ipa > *ipb)
6057 return 1;
6058 if (*ipa < *ipb)
6059 return -1;
6060 return 0;
6061}
6062
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006063static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08006064 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006065 unsigned long *end)
6066{
Steven Rostedt706c81f2012-04-24 23:45:26 -04006067 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05006068 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04006069 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05006070 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006071 unsigned long *p;
6072 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04006073 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05006074 int ret = -ENOMEM;
6075
6076 count = end - start;
6077
6078 if (!count)
6079 return 0;
6080
Steven Rostedt9fd49322012-04-24 22:32:06 -04006081 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02006082 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04006083
Steven Rostedt706c81f2012-04-24 23:45:26 -04006084 start_pg = ftrace_allocate_pages(count);
6085 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05006086 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006087
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006088 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05006089
Steven Rostedt320823092011-12-16 14:42:37 -05006090 /*
6091 * Core and each module needs their own pages, as
6092 * modules will free them when they are removed.
6093 * Force a new page to be allocated for modules.
6094 */
Steven Rostedta7900872011-12-16 16:23:44 -05006095 if (!mod) {
6096 WARN_ON(ftrace_pages || ftrace_pages_start);
6097 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04006098 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05006099 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05006100 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05006101 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05006102
Steven Rostedta7900872011-12-16 16:23:44 -05006103 if (WARN_ON(ftrace_pages->next)) {
6104 /* Hmm, we have free pages? */
6105 while (ftrace_pages->next)
6106 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05006107 }
Steven Rostedta7900872011-12-16 16:23:44 -05006108
Steven Rostedt706c81f2012-04-24 23:45:26 -04006109 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05006110 }
6111
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006112 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04006113 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006114 while (p < end) {
6115 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08006116 /*
6117 * Some architecture linkers will pad between
6118 * the different mcount_loc sections of different
6119 * object files to satisfy alignments.
6120 * Skip any NULL pointers.
6121 */
6122 if (!addr)
6123 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04006124
6125 if (pg->index == pg->size) {
6126 /* We should have allocated enough */
6127 if (WARN_ON(!pg->next))
6128 break;
6129 pg = pg->next;
6130 }
6131
6132 rec = &pg->records[pg->index++];
6133 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006134 }
6135
Steven Rostedt706c81f2012-04-24 23:45:26 -04006136 /* We should have used all pages */
6137 WARN_ON(pg->next);
6138
6139 /* Assign the last page to ftrace_pages */
6140 ftrace_pages = pg;
6141
Steven Rostedta4f18ed2011-06-07 09:26:46 -04006142 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04006143 * We only need to disable interrupts on start up
6144 * because we are modifying code that an interrupt
6145 * may execute, and the modification is not atomic.
6146 * But for modules, nothing runs the code we modify
6147 * until we are finished with it, and there's no
6148 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04006149 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04006150 if (!mod)
6151 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006152 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04006153 if (!mod)
6154 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05006155 ret = 0;
6156 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006157 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006158
Steven Rostedta7900872011-12-16 16:23:44 -05006159 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006160}
6161
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006162struct ftrace_mod_func {
6163 struct list_head list;
6164 char *name;
6165 unsigned long ip;
6166 unsigned int size;
6167};
6168
6169struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006170 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006171 struct list_head list;
6172 struct module *mod;
6173 unsigned long start_addr;
6174 unsigned long end_addr;
6175 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006176 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006177};
6178
Steven Rostedt93eb6772009-04-15 13:24:06 -04006179#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05006180
6181#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
6182
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006183static LIST_HEAD(ftrace_mod_maps);
6184
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006185static int referenced_filters(struct dyn_ftrace *rec)
6186{
6187 struct ftrace_ops *ops;
6188 int cnt = 0;
6189
6190 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
6191 if (ops_references_rec(ops, rec))
6192 cnt++;
6193 }
6194
6195 return cnt;
6196}
6197
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006198static void
6199clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
6200{
6201 struct ftrace_func_entry *entry;
6202 struct dyn_ftrace *rec;
6203 int i;
6204
6205 if (ftrace_hash_empty(hash))
6206 return;
6207
6208 for (i = 0; i < pg->index; i++) {
6209 rec = &pg->records[i];
6210 entry = __ftrace_lookup_ip(hash, rec->ip);
6211 /*
6212 * Do not allow this rec to match again.
6213 * Yeah, it may waste some memory, but will be removed
6214 * if/when the hash is modified again.
6215 */
6216 if (entry)
6217 entry->ip = 0;
6218 }
6219}
6220
6221/* Clear any records from hashs */
6222static void clear_mod_from_hashes(struct ftrace_page *pg)
6223{
6224 struct trace_array *tr;
6225
6226 mutex_lock(&trace_types_lock);
6227 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6228 if (!tr->ops || !tr->ops->func_hash)
6229 continue;
6230 mutex_lock(&tr->ops->func_hash->regex_lock);
6231 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
6232 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
6233 mutex_unlock(&tr->ops->func_hash->regex_lock);
6234 }
6235 mutex_unlock(&trace_types_lock);
6236}
6237
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006238static void ftrace_free_mod_map(struct rcu_head *rcu)
6239{
6240 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
6241 struct ftrace_mod_func *mod_func;
6242 struct ftrace_mod_func *n;
6243
6244 /* All the contents of mod_map are now not visible to readers */
6245 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
6246 kfree(mod_func->name);
6247 list_del(&mod_func->list);
6248 kfree(mod_func);
6249 }
6250
6251 kfree(mod_map);
6252}
6253
jolsa@redhat.come7247a12009-10-07 19:00:35 +02006254void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04006255{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006256 struct ftrace_mod_map *mod_map;
6257 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04006258 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05006259 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006260 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04006261 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05006262 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04006263
Steven Rostedt93eb6772009-04-15 13:24:06 -04006264 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04006265
6266 if (ftrace_disabled)
6267 goto out_unlock;
6268
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006269 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
6270 if (mod_map->mod == mod) {
6271 list_del_rcu(&mod_map->list);
Paul E. McKenney74401722018-11-06 18:44:52 -08006272 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04006273 break;
6274 }
6275 }
6276
Steven Rostedt320823092011-12-16 14:42:37 -05006277 /*
6278 * Each module has its own ftrace_pages, remove
6279 * them from the list.
6280 */
6281 last_pg = &ftrace_pages_start;
6282 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
6283 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05006284 if (within_module_core(rec->ip, mod) ||
6285 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04006286 /*
Steven Rostedt320823092011-12-16 14:42:37 -05006287 * As core pages are first, the first
6288 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04006289 */
Steven Rostedt320823092011-12-16 14:42:37 -05006290 if (WARN_ON(pg == ftrace_pages_start))
6291 goto out_unlock;
6292
6293 /* Check if we are deleting the last page */
6294 if (pg == ftrace_pages)
6295 ftrace_pages = next_to_ftrace_page(last_pg);
6296
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04006297 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05006298 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006299
6300 pg->next = tmp_page;
6301 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05006302 } else
6303 last_pg = &pg->next;
6304 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04006305 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04006306 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006307
6308 for (pg = tmp_page; pg; pg = tmp_page) {
6309
6310 /* Needs to be called outside of ftrace_lock */
6311 clear_mod_from_hashes(pg);
6312
6313 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6314 free_pages((unsigned long)pg->records, order);
6315 tmp_page = pg->next;
6316 kfree(pg);
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04006317 ftrace_number_of_pages -= 1 << order;
6318 ftrace_number_of_groups--;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04006319 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04006320}
6321
Jessica Yu7dcd1822016-02-16 17:32:33 -05006322void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006323{
6324 struct dyn_ftrace *rec;
6325 struct ftrace_page *pg;
6326
6327 mutex_lock(&ftrace_lock);
6328
6329 if (ftrace_disabled)
6330 goto out_unlock;
6331
6332 /*
6333 * If the tracing is enabled, go ahead and enable the record.
6334 *
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05306335 * The reason not to enable the record immediately is the
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006336 * inherent check of ftrace_make_nop/ftrace_make_call for
6337 * correct previous instructions. Making first the NOP
6338 * conversion puts the module to the correct state, thus
6339 * passing the ftrace_make_call check.
6340 *
6341 * We also delay this to after the module code already set the
6342 * text to read-only, as we now need to set it back to read-write
6343 * so that we can modify the text.
6344 */
6345 if (ftrace_start_up)
6346 ftrace_arch_code_modify_prepare();
6347
6348 do_for_each_ftrace_rec(pg, rec) {
6349 int cnt;
6350 /*
6351 * do_for_each_ftrace_rec() is a double loop.
6352 * module text shares the pg. If a record is
6353 * not part of this module, then skip this pg,
6354 * which the "break" will do.
6355 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05006356 if (!within_module_core(rec->ip, mod) &&
6357 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006358 break;
6359
6360 cnt = 0;
6361
6362 /*
6363 * When adding a module, we need to check if tracers are
6364 * currently enabled and if they are, and can trace this record,
6365 * we need to enable the module functions as well as update the
6366 * reference counts for those function records.
6367 */
6368 if (ftrace_start_up)
6369 cnt += referenced_filters(rec);
6370
6371 /* This clears FTRACE_FL_DISABLED */
6372 rec->flags = cnt;
6373
6374 if (ftrace_start_up && cnt) {
6375 int failed = __ftrace_replace_code(rec, 1);
6376 if (failed) {
6377 ftrace_bug(failed, rec);
6378 goto out_loop;
6379 }
6380 }
6381
6382 } while_for_each_ftrace_rec();
6383
6384 out_loop:
6385 if (ftrace_start_up)
6386 ftrace_arch_code_modify_post_process();
6387
6388 out_unlock:
6389 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04006390
6391 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05006392}
6393
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04006394void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04006395{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05006396 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01006397 return;
6398
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05006399 ftrace_process_locs(mod, mod->ftrace_callsites,
6400 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05006401}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006402
6403static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6404 struct dyn_ftrace *rec)
6405{
6406 struct ftrace_mod_func *mod_func;
6407 unsigned long symsize;
6408 unsigned long offset;
6409 char str[KSYM_SYMBOL_LEN];
6410 char *modname;
6411 const char *ret;
6412
6413 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
6414 if (!ret)
6415 return;
6416
6417 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
6418 if (!mod_func)
6419 return;
6420
6421 mod_func->name = kstrdup(str, GFP_KERNEL);
6422 if (!mod_func->name) {
6423 kfree(mod_func);
6424 return;
6425 }
6426
6427 mod_func->ip = rec->ip - offset;
6428 mod_func->size = symsize;
6429
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006430 mod_map->num_funcs++;
6431
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006432 list_add_rcu(&mod_func->list, &mod_map->funcs);
6433}
6434
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006435static struct ftrace_mod_map *
6436allocate_ftrace_mod_map(struct module *mod,
6437 unsigned long start, unsigned long end)
6438{
6439 struct ftrace_mod_map *mod_map;
6440
6441 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
6442 if (!mod_map)
6443 return NULL;
6444
6445 mod_map->mod = mod;
6446 mod_map->start_addr = start;
6447 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006448 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006449
6450 INIT_LIST_HEAD_RCU(&mod_map->funcs);
6451
6452 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
6453
6454 return mod_map;
6455}
6456
6457static const char *
6458ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
6459 unsigned long addr, unsigned long *size,
6460 unsigned long *off, char *sym)
6461{
6462 struct ftrace_mod_func *found_func = NULL;
6463 struct ftrace_mod_func *mod_func;
6464
6465 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6466 if (addr >= mod_func->ip &&
6467 addr < mod_func->ip + mod_func->size) {
6468 found_func = mod_func;
6469 break;
6470 }
6471 }
6472
6473 if (found_func) {
6474 if (size)
6475 *size = found_func->size;
6476 if (off)
6477 *off = addr - found_func->ip;
6478 if (sym)
6479 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6480
6481 return found_func->name;
6482 }
6483
6484 return NULL;
6485}
6486
6487const char *
6488ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6489 unsigned long *off, char **modname, char *sym)
6490{
6491 struct ftrace_mod_map *mod_map;
6492 const char *ret = NULL;
6493
Paul E. McKenney74401722018-11-06 18:44:52 -08006494 /* mod_map is freed via call_rcu() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006495 preempt_disable();
6496 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6497 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6498 if (ret) {
6499 if (modname)
6500 *modname = mod_map->mod->name;
6501 break;
6502 }
6503 }
6504 preempt_enable();
6505
6506 return ret;
6507}
6508
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04006509int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6510 char *type, char *name,
6511 char *module_name, int *exported)
6512{
6513 struct ftrace_mod_map *mod_map;
6514 struct ftrace_mod_func *mod_func;
6515
6516 preempt_disable();
6517 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6518
6519 if (symnum >= mod_map->num_funcs) {
6520 symnum -= mod_map->num_funcs;
6521 continue;
6522 }
6523
6524 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6525 if (symnum > 1) {
6526 symnum--;
6527 continue;
6528 }
6529
6530 *value = mod_func->ip;
6531 *type = 'T';
6532 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6533 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6534 *exported = 1;
6535 preempt_enable();
6536 return 0;
6537 }
6538 WARN_ON(1);
6539 break;
6540 }
6541 preempt_enable();
6542 return -ERANGE;
6543}
6544
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006545#else
6546static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6547 struct dyn_ftrace *rec) { }
6548static inline struct ftrace_mod_map *
6549allocate_ftrace_mod_map(struct module *mod,
6550 unsigned long start, unsigned long end)
6551{
6552 return NULL;
6553}
Steven Rostedt93eb6772009-04-15 13:24:06 -04006554#endif /* CONFIG_MODULES */
6555
Joel Fernandes8715b102017-10-09 12:29:31 -07006556struct ftrace_init_func {
6557 struct list_head list;
6558 unsigned long ip;
6559};
6560
6561/* Clear any init ips from hashes */
6562static void
6563clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006564{
Joel Fernandes8715b102017-10-09 12:29:31 -07006565 struct ftrace_func_entry *entry;
6566
Changbin Du08468752019-09-10 22:33:36 +08006567 entry = ftrace_lookup_ip(hash, func->ip);
Joel Fernandes8715b102017-10-09 12:29:31 -07006568 /*
6569 * Do not allow this rec to match again.
6570 * Yeah, it may waste some memory, but will be removed
6571 * if/when the hash is modified again.
6572 */
6573 if (entry)
6574 entry->ip = 0;
6575}
6576
6577static void
6578clear_func_from_hashes(struct ftrace_init_func *func)
6579{
6580 struct trace_array *tr;
6581
6582 mutex_lock(&trace_types_lock);
6583 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6584 if (!tr->ops || !tr->ops->func_hash)
6585 continue;
6586 mutex_lock(&tr->ops->func_hash->regex_lock);
6587 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6588 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6589 mutex_unlock(&tr->ops->func_hash->regex_lock);
6590 }
6591 mutex_unlock(&trace_types_lock);
6592}
6593
6594static void add_to_clear_hash_list(struct list_head *clear_list,
6595 struct dyn_ftrace *rec)
6596{
6597 struct ftrace_init_func *func;
6598
6599 func = kmalloc(sizeof(*func), GFP_KERNEL);
6600 if (!func) {
Steven Rostedt (VMware)24589e32020-01-25 10:52:30 -05006601 MEM_FAIL(1, "alloc failure, ftrace filter could be stale\n");
Joel Fernandes8715b102017-10-09 12:29:31 -07006602 return;
6603 }
6604
6605 func->ip = rec->ip;
6606 list_add(&func->list, clear_list);
6607}
6608
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006609void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006610{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006611 unsigned long start = (unsigned long)(start_ptr);
6612 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006613 struct ftrace_page **last_pg = &ftrace_pages_start;
6614 struct ftrace_page *pg;
6615 struct dyn_ftrace *rec;
6616 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006617 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07006618 struct ftrace_init_func *func, *func_next;
6619 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006620 int order;
6621
Joel Fernandes8715b102017-10-09 12:29:31 -07006622 INIT_LIST_HEAD(&clear_hash);
6623
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006624 key.ip = start;
6625 key.flags = end; /* overload flags, as it is unsigned long */
6626
6627 mutex_lock(&ftrace_lock);
6628
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006629 /*
6630 * If we are freeing module init memory, then check if
6631 * any tracer is active. If so, we need to save a mapping of
6632 * the module functions being freed with the address.
6633 */
6634 if (mod && ftrace_ops_list != &ftrace_list_end)
6635 mod_map = allocate_ftrace_mod_map(mod, start, end);
6636
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006637 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6638 if (end < pg->records[0].ip ||
6639 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6640 continue;
6641 again:
6642 rec = bsearch(&key, pg->records, pg->index,
6643 sizeof(struct dyn_ftrace),
6644 ftrace_cmp_recs);
6645 if (!rec)
6646 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006647
Joel Fernandes8715b102017-10-09 12:29:31 -07006648 /* rec will be cleared from hashes after ftrace_lock unlock */
6649 add_to_clear_hash_list(&clear_hash, rec);
6650
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006651 if (mod_map)
6652 save_ftrace_mod_rec(mod_map, rec);
6653
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006654 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04006655 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006656 if (!pg->index) {
6657 *last_pg = pg->next;
6658 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6659 free_pages((unsigned long)pg->records, order);
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04006660 ftrace_number_of_pages -= 1 << order;
6661 ftrace_number_of_groups--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006662 kfree(pg);
6663 pg = container_of(last_pg, struct ftrace_page, next);
6664 if (!(*last_pg))
6665 ftrace_pages = pg;
6666 continue;
6667 }
6668 memmove(rec, rec + 1,
6669 (pg->index - (rec - pg->records)) * sizeof(*rec));
6670 /* More than one function may be in this block */
6671 goto again;
6672 }
6673 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07006674
6675 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6676 clear_func_from_hashes(func);
6677 kfree(func);
6678 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006679}
6680
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006681void __init ftrace_free_init_mem(void)
6682{
6683 void *start = (void *)(&__init_begin);
6684 void *end = (void *)(&__init_end);
6685
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006686 ftrace_free_mem(NULL, start, end);
Steven Rostedt93eb6772009-04-15 13:24:06 -04006687}
6688
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006689void __init ftrace_init(void)
6690{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006691 extern unsigned long __start_mcount_loc[];
6692 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006693 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006694 int ret;
6695
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006696 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006697 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006698 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01006699 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006700 goto failed;
6701
6702 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006703 if (!count) {
6704 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006705 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006706 }
6707
6708 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6709 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006710
6711 last_ftrace_enabled = ftrace_enabled = 1;
6712
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006713 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08006714 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006715 __stop_mcount_loc);
6716
Steven Rostedt (VMware)da537f02019-10-01 14:38:07 -04006717 pr_info("ftrace: allocated %ld pages with %ld groups\n",
6718 ftrace_number_of_pages, ftrace_number_of_groups);
6719
Steven Rostedt2af15d62009-05-28 13:37:24 -04006720 set_ftrace_early_filters();
6721
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006722 return;
6723 failed:
6724 ftrace_disabled = 1;
6725}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006726
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006727/* Do nothing if arch does not support this */
6728void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6729{
6730}
6731
6732static void ftrace_update_trampoline(struct ftrace_ops *ops)
6733{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006734 arch_ftrace_update_trampoline(ops);
6735}
6736
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006737void ftrace_init_trace_array(struct trace_array *tr)
6738{
6739 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04006740 INIT_LIST_HEAD(&tr->mod_trace);
6741 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006742}
Steven Rostedt3d083392008-05-12 21:20:42 +02006743#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006744
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05006745struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04006746 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006747 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6748 FTRACE_OPS_FL_INITIALIZED |
6749 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04006750};
6751
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006752static int __init ftrace_nodyn_init(void)
6753{
6754 ftrace_enabled = 1;
6755 return 0;
6756}
Steven Rostedt6f415672012-10-05 12:13:07 -04006757core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006758
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006759static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006760static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006761static inline void ftrace_startup_all(int command) { }
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006762
Ingo Molnarc7aafc52008-05-12 21:20:45 +02006763# define ftrace_startup_sysctl() do { } while (0)
6764# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04006765
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006766static void ftrace_update_trampoline(struct ftrace_ops *ops)
6767{
6768}
6769
Steven Rostedt3d083392008-05-12 21:20:42 +02006770#endif /* CONFIG_DYNAMIC_FTRACE */
6771
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006772__init void ftrace_init_global_array_ops(struct trace_array *tr)
6773{
6774 tr->ops = &global_ops;
6775 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006776 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006777}
6778
6779void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6780{
6781 /* If we filter on pids, update to use the pid function */
6782 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6783 if (WARN_ON(tr->ops->func != ftrace_stub))
6784 printk("ftrace ops had %pS for function\n",
6785 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006786 }
6787 tr->ops->func = func;
6788 tr->ops->private = tr;
6789}
6790
6791void ftrace_reset_array_ops(struct trace_array *tr)
6792{
6793 tr->ops->func = ftrace_stub;
6794}
6795
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006796static nokprobe_inline void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006797__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006798 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006799{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006800 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006801 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04006802
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006803 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6804 if (bit < 0)
6805 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04006806
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006807 /*
6808 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08006809 * they must be freed after a synchronize_rcu().
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006810 */
6811 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006812
Steven Rostedt0a016402012-11-02 17:03:03 -04006813 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (VMware)2fa717a2019-04-11 11:46:13 -04006814 /* Stub functions don't need to be called nor tested */
6815 if (op->flags & FTRACE_OPS_FL_STUB)
6816 continue;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006817 /*
6818 * Check the following for each ops before calling their func:
6819 * if RCU flag is set, then rcu_is_watching() must be true
6820 * if PER_CPU is set, then ftrace_function_local_disable()
6821 * must be false
6822 * Otherwise test if the ip matches the ops filter
6823 *
6824 * If any of the above fails then the op->func() is not executed.
6825 */
6826 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006827 ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04006828 if (FTRACE_WARN_ON(!op->func)) {
6829 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006830 goto out;
6831 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04006832 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006833 }
Steven Rostedt0a016402012-11-02 17:03:03 -04006834 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006835out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006836 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006837 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04006838}
6839
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006840/*
6841 * Some archs only support passing ip and parent_ip. Even though
6842 * the list function ignores the op parameter, we do not want any
6843 * C side effects, where a function is called without the caller
6844 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006845 * Archs are to support both the regs and ftrace_ops at the same time.
6846 * If they support ftrace_ops, it is assumed they support regs.
6847 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09006848 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6849 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006850 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08006851 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006852 */
6853#if ARCH_SUPPORTS_FTRACE_OPS
6854static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006855 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006856{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006857 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006858}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006859NOKPROBE_SYMBOL(ftrace_ops_list_func);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006860#else
6861static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6862{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006863 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006864}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006865NOKPROBE_SYMBOL(ftrace_ops_no_ops);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006866#endif
6867
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006868/*
6869 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006870 * recursion, needs RCU protection and/or requires per cpu handling, then
6871 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006872 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006873static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006874 struct ftrace_ops *op, struct pt_regs *regs)
6875{
6876 int bit;
6877
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006878 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6879 return;
6880
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006881 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6882 if (bit < 0)
6883 return;
6884
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006885 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006886
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006887 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006888
6889 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006890 trace_clear_recursion(bit);
6891}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006892NOKPROBE_SYMBOL(ftrace_ops_assist_func);
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006893
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006894/**
6895 * ftrace_ops_get_func - get the function a trampoline should call
6896 * @ops: the ops to get the function for
6897 *
6898 * Normally the mcount trampoline will call the ops->func, but there
6899 * are times that it should not. For example, if the ops does not
6900 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08006901 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006902 *
6903 * Returns the function that the trampoline should call for @ops.
6904 */
6905ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6906{
6907 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006908 * If the function does not handle recursion, needs to be RCU safe,
6909 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006910 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006911 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006912 ops->flags & FTRACE_OPS_FL_RCU)
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006913 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006914
6915 return ops->func;
6916}
6917
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006918static void
6919ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6920 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05006921{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006922 struct trace_array *tr = data;
6923 struct trace_pid_list *pid_list;
6924
6925 pid_list = rcu_dereference_sched(tr->function_pids);
6926
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006927 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006928 trace_ignore_this_task(pid_list, next));
6929}
6930
Namhyung Kim1e104862017-04-17 11:44:28 +09006931static void
6932ftrace_pid_follow_sched_process_fork(void *data,
6933 struct task_struct *self,
6934 struct task_struct *task)
6935{
6936 struct trace_pid_list *pid_list;
6937 struct trace_array *tr = data;
6938
6939 pid_list = rcu_dereference_sched(tr->function_pids);
6940 trace_filter_add_remove_task(pid_list, self, task);
6941}
6942
6943static void
6944ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6945{
6946 struct trace_pid_list *pid_list;
6947 struct trace_array *tr = data;
6948
6949 pid_list = rcu_dereference_sched(tr->function_pids);
6950 trace_filter_add_remove_task(pid_list, NULL, task);
6951}
6952
6953void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6954{
6955 if (enable) {
6956 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6957 tr);
6958 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6959 tr);
6960 } else {
6961 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6962 tr);
6963 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6964 tr);
6965 }
6966}
6967
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006968static void clear_ftrace_pids(struct trace_array *tr)
6969{
6970 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05006971 int cpu;
6972
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006973 pid_list = rcu_dereference_protected(tr->function_pids,
6974 lockdep_is_held(&ftrace_lock));
6975 if (!pid_list)
6976 return;
6977
6978 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6979
6980 for_each_possible_cpu(cpu)
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05006981 per_cpu_ptr(tr->array_buffer.data, cpu)->ftrace_ignore_pid = false;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006982
6983 rcu_assign_pointer(tr->function_pids, NULL);
6984
6985 /* Wait till all users are no longer using pid filtering */
Paul E. McKenney74401722018-11-06 18:44:52 -08006986 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006987
6988 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05006989}
6990
Namhyung Kimd879d0b2017-04-17 11:44:27 +09006991void ftrace_clear_pids(struct trace_array *tr)
6992{
6993 mutex_lock(&ftrace_lock);
6994
6995 clear_ftrace_pids(tr);
6996
6997 mutex_unlock(&ftrace_lock);
6998}
6999
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007000static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05007001{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007002 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007003 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007004
7005 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04007006 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007007
7008 mutex_unlock(&ftrace_lock);
7009}
7010
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007011/* Greater than any max PID */
7012#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
7013
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007014static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007015 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007016{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007017 struct trace_pid_list *pid_list;
7018 struct trace_array *tr = m->private;
7019
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007020 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007021 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007022
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007023 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007024
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007025 if (!pid_list)
7026 return !(*pos) ? FTRACE_NO_PIDS : NULL;
7027
7028 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007029}
7030
7031static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
7032{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007033 struct trace_array *tr = m->private;
7034 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
7035
Vasily Averine4075e82020-01-24 10:02:56 +03007036 if (v == FTRACE_NO_PIDS) {
7037 (*pos)++;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007038 return NULL;
Vasily Averine4075e82020-01-24 10:02:56 +03007039 }
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007040 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007041}
7042
7043static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007044 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007045{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007046 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007047 mutex_unlock(&ftrace_lock);
7048}
7049
7050static int fpid_show(struct seq_file *m, void *v)
7051{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007052 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01007053 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007054 return 0;
7055 }
7056
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007057 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007058}
7059
7060static const struct seq_operations ftrace_pid_sops = {
7061 .start = fpid_start,
7062 .next = fpid_next,
7063 .stop = fpid_stop,
7064 .show = fpid_show,
7065};
7066
7067static int
7068ftrace_pid_open(struct inode *inode, struct file *file)
7069{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007070 struct trace_array *tr = inode->i_private;
7071 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007072 int ret = 0;
7073
Steven Rostedt (VMware)8530dec2019-10-11 17:39:57 -04007074 ret = tracing_check_open_get_tr(tr);
7075 if (ret)
7076 return ret;
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007077
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007078 if ((file->f_mode & FMODE_WRITE) &&
7079 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007080 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007081
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007082 ret = seq_open(file, &ftrace_pid_sops);
7083 if (ret < 0) {
7084 trace_array_put(tr);
7085 } else {
7086 m = file->private_data;
7087 /* copy tr over to seq ops */
7088 m->private = tr;
7089 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007090
7091 return ret;
7092}
7093
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007094static void ignore_task_cpu(void *data)
7095{
7096 struct trace_array *tr = data;
7097 struct trace_pid_list *pid_list;
7098
7099 /*
7100 * This function is called by on_each_cpu() while the
7101 * event_mutex is held.
7102 */
7103 pid_list = rcu_dereference_protected(tr->function_pids,
7104 mutex_is_locked(&ftrace_lock));
7105
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -05007106 this_cpu_write(tr->array_buffer.data->ftrace_ignore_pid,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007107 trace_ignore_this_task(pid_list, current));
7108}
7109
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007110static ssize_t
7111ftrace_pid_write(struct file *filp, const char __user *ubuf,
7112 size_t cnt, loff_t *ppos)
7113{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007114 struct seq_file *m = filp->private_data;
7115 struct trace_array *tr = m->private;
7116 struct trace_pid_list *filtered_pids = NULL;
7117 struct trace_pid_list *pid_list;
7118 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007119
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007120 if (!cnt)
7121 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007122
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007123 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007124
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007125 filtered_pids = rcu_dereference_protected(tr->function_pids,
7126 lockdep_is_held(&ftrace_lock));
7127
7128 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
7129 if (ret < 0)
7130 goto out;
7131
7132 rcu_assign_pointer(tr->function_pids, pid_list);
7133
7134 if (filtered_pids) {
Paul E. McKenney74401722018-11-06 18:44:52 -08007135 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007136 trace_free_pid_list(filtered_pids);
7137 } else if (pid_list) {
7138 /* Register a probe to set whether to ignore the tracing of a task */
7139 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
7140 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007141
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007142 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007143 * Ignoring of pids is done at task switch. But we have to
7144 * check for those tasks that are currently running.
7145 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007146 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007147 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007148
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007149 ftrace_update_pid_func();
7150 ftrace_startup_all(0);
7151 out:
7152 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007153
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007154 if (ret > 0)
7155 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05007156
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007157 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007158}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007159
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007160static int
7161ftrace_pid_release(struct inode *inode, struct file *file)
7162{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007163 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007164
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007165 trace_array_put(tr);
7166
7167 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007168}
7169
Steven Rostedt5e2336a2009-03-05 21:44:55 -05007170static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007171 .open = ftrace_pid_open,
7172 .write = ftrace_pid_write,
7173 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05007174 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04007175 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007176};
7177
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007178void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007179{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01007180 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04007181 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007182}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05007183
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04007184void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
7185 struct dentry *d_tracer)
7186{
7187 /* Only the top level directory has the dyn_tracefs and profile */
7188 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
7189
7190 ftrace_init_dyn_tracefs(d_tracer);
7191 ftrace_profile_tracefs(d_tracer);
7192}
7193
Steven Rostedt3d083392008-05-12 21:20:42 +02007194/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04007195 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04007196 *
7197 * This function should be used by panic code. It stops ftrace
7198 * but in a not so nice way. If you need to simply kill ftrace
7199 * from a non-atomic section, use ftrace_kill.
7200 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04007201void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04007202{
7203 ftrace_disabled = 1;
7204 ftrace_enabled = 0;
Yisheng Xie5ccba642018-02-02 10:14:49 +08007205 ftrace_trace_function = ftrace_stub;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04007206}
7207
7208/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04007209 * Test if ftrace is dead or not.
7210 */
7211int ftrace_is_dead(void)
7212{
7213 return ftrace_disabled;
7214}
7215
7216/**
Steven Rostedt3d083392008-05-12 21:20:42 +02007217 * register_ftrace_function - register a function for profiling
7218 * @ops - ops structure that holds the function for profiling.
7219 *
7220 * Register a function to be called by all functions in the
7221 * kernel.
7222 *
7223 * Note: @ops->func and all the functions it calls must be labeled
7224 * with "notrace", otherwise it will go into a
7225 * recursive loop.
7226 */
7227int register_ftrace_function(struct ftrace_ops *ops)
7228{
Steven Rostedt45a4a232011-04-21 23:16:46 -04007229 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02007230
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09007231 ftrace_ops_init(ops);
7232
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007233 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01007234
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05007235 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04007236
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007237 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02007238
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007239 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02007240}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04007241EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02007242
7243/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01007244 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02007245 * @ops - ops structure that holds the function to unregister
7246 *
7247 * Unregister a function that was added to be called by ftrace profiling.
7248 */
7249int unregister_ftrace_function(struct ftrace_ops *ops)
7250{
7251 int ret;
7252
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007253 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05007254 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007255 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007256
7257 return ret;
7258}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04007259EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007260
Miroslav Benes71624312019-10-16 13:33:13 +02007261static bool is_permanent_ops_registered(void)
7262{
7263 struct ftrace_ops *op;
7264
7265 do_for_each_ftrace_op(op, ftrace_ops_list) {
7266 if (op->flags & FTRACE_OPS_FL_PERMANENT)
7267 return true;
7268 } while_for_each_ftrace_op(op);
7269
7270 return false;
7271}
7272
Ingo Molnare309b412008-05-12 21:20:51 +02007273int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007274ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07007275 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007276 loff_t *ppos)
7277{
Steven Rostedt45a4a232011-04-21 23:16:46 -04007278 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02007279
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007280 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007281
Steven Rostedt45a4a232011-04-21 23:16:46 -04007282 if (unlikely(ftrace_disabled))
7283 goto out;
7284
7285 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007286
Li Zefana32c7762009-06-26 16:55:51 +08007287 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007288 goto out;
7289
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007290 if (ftrace_enabled) {
7291
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007292 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08007293 if (rcu_dereference_protected(ftrace_ops_list,
7294 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01007295 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007296
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05007297 ftrace_startup_sysctl();
7298
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007299 } else {
Miroslav Benes71624312019-10-16 13:33:13 +02007300 if (is_permanent_ops_registered()) {
7301 ftrace_enabled = true;
7302 ret = -EBUSY;
7303 goto out;
7304 }
7305
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007306 /* stopping ftrace calls (just send to ftrace_stub) */
7307 ftrace_trace_function = ftrace_stub;
7308
7309 ftrace_shutdown_sysctl();
7310 }
7311
Miroslav Benes71624312019-10-16 13:33:13 +02007312 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02007313 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05007314 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02007315 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02007316}