blob: 8ef9fc22603737337463669b522649b19cc36f95 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002/*
3 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 *
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 *
8 * Originally ported from the -rt patch by:
9 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 *
11 * Based on code in the latency_tracer, that is:
12 *
13 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010014 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020015 */
16
Steven Rostedt3d083392008-05-12 21:20:42 +020017#include <linux/stop_machine.h>
18#include <linux/clocksource.h>
Ingo Molnar29930022017-02-08 18:51:36 +010019#include <linux/sched/task.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020020#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/seq_file.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050022#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020023#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010024#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020025#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050026#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040027#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010028#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020029#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020031#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050032#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020033#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050034#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080035#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020036
Steven Rostedtad8d75f2009-04-14 19:39:12 -040037#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040038
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -040039#include <asm/sections.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040040#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053041
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050042#include "ftrace_internal.h"
Steven Rostedt0706f1c2009-03-23 23:12:58 -040043#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040044#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020045
Steven Rostedt6912896e2008-10-23 09:33:03 -040046#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040047 ({ \
48 int ___r = cond; \
49 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040050 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040051 ___r; \
52 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040053
54#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040055 ({ \
56 int ___r = cond; \
57 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040058 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040059 ___r; \
60 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040061
Steven Rostedt8fc0c702009-02-16 15:28:00 -050062/* hash bits for specific function selection */
63#define FTRACE_HASH_BITS 7
64#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
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),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040072#define ASSIGN_OPS_HASH(opsname, val) \
73 .func_hash = val, \
74 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090075#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040076#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040077#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090078#endif
79
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050080struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040081 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040082 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040083 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084};
85
Steven Rostedt4eebcc82008-05-12 21:20:48 +020086/* ftrace_enabled is a method to turn ftrace on or off */
87int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020088static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020089
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040090/* Current function tracing op */
91struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050092/* What to set function_trace_op to */
93static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050094
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040095static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040096{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040097 struct trace_array *tr;
98
99 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
100 return false;
101
102 tr = ops->private;
103
104 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400105}
106
107static void ftrace_update_trampoline(struct ftrace_ops *ops);
108
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200109/*
110 * ftrace_disabled is set when an anomaly is discovered.
111 * ftrace_disabled is much stronger than ftrace_enabled.
112 */
113static int ftrace_disabled __read_mostly;
114
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500115DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200116
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500117struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200118ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500119struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200120
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400121#if ARCH_SUPPORTS_FTRACE_OPS
122static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400123 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400124#else
125/* See comment below, where ftrace_ops_list_func is defined */
126static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
127#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
128#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400129
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900130static inline void ftrace_ops_init(struct ftrace_ops *ops)
131{
132#ifdef CONFIG_DYNAMIC_FTRACE
133 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400134 mutex_init(&ops->local_hash.regex_lock);
135 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900136 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
137 }
138#endif
139}
140
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400141static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400142 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500143{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400144 struct trace_array *tr = op->private;
145
146 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500147 return;
148
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400149 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500150}
151
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500152static void ftrace_sync(struct work_struct *work)
153{
154 /*
155 * This function is just a stub to implement a hard force
156 * of synchronize_sched(). This requires synchronizing
157 * tasks even in userspace and idle.
158 *
159 * Yes, function tracing is rude.
160 */
161}
162
163static void ftrace_sync_ipi(void *data)
164{
165 /* Probably not needed, but do it anyway */
166 smp_rmb();
167}
168
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100169static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
170{
171 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500172 * 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 +0100173 * then it needs to call the list anyway.
174 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +0200175 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
176 FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100177 return ftrace_ops_list_func;
178
179 return ftrace_ops_get_func(ops);
180}
181
Steven Rostedt2b499382011-05-03 22:49:52 -0400182static void update_ftrace_function(void)
183{
184 ftrace_func_t func;
185
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400186 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400187 * Prepare the ftrace_ops that the arch callback will use.
188 * If there's only one ftrace_ops registered, the ftrace_ops_list
189 * will point to the ops we want.
190 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800191 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
192 lockdep_is_held(&ftrace_lock));
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400193
194 /* If there's no ftrace_ops registered, just call the stub function */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800195 if (set_function_trace_op == &ftrace_list_end) {
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400196 func = ftrace_stub;
197
198 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400199 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400200 * recursion safe and not dynamic and the arch supports passing ops,
201 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400202 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800203 } else if (rcu_dereference_protected(ftrace_ops_list->next,
204 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100205 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400206
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400207 } else {
208 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500209 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400210 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400211 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400212
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400213 update_function_graph_func();
214
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500215 /* If there's no change, then do nothing more here */
216 if (ftrace_trace_function == func)
217 return;
218
219 /*
220 * If we are using the list function, it doesn't care
221 * about the function_trace_ops.
222 */
223 if (func == ftrace_ops_list_func) {
224 ftrace_trace_function = func;
225 /*
226 * Don't even bother setting function_trace_ops,
227 * it would be racy to do so anyway.
228 */
229 return;
230 }
231
232#ifndef CONFIG_DYNAMIC_FTRACE
233 /*
234 * For static tracing, we need to be a bit more careful.
235 * The function change takes affect immediately. Thus,
236 * we need to coorditate the setting of the function_trace_ops
237 * with the setting of the ftrace_trace_function.
238 *
239 * Set the function to the list ops, which will call the
240 * function we want, albeit indirectly, but it handles the
241 * ftrace_ops and doesn't depend on function_trace_op.
242 */
243 ftrace_trace_function = ftrace_ops_list_func;
244 /*
245 * Make sure all CPUs see this. Yes this is slow, but static
246 * tracing is slow and nasty to have enabled.
247 */
248 schedule_on_each_cpu(ftrace_sync);
249 /* Now all cpus are using the list ops. */
250 function_trace_op = set_function_trace_op;
251 /* Make sure the function_trace_op is visible on all CPUs */
252 smp_wmb();
253 /* Nasty way to force a rmb on all cpus */
254 smp_call_function(ftrace_sync_ipi, NULL, 1);
255 /* OK, we are all set to update the ftrace_trace_function now! */
256#endif /* !CONFIG_DYNAMIC_FTRACE */
257
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400258 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400259}
260
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800261static void add_ftrace_ops(struct ftrace_ops __rcu **list,
262 struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200263{
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800264 rcu_assign_pointer(ops->next, *list);
265
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200266 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400267 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200268 * CPU might be walking that list. We need to make sure
269 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400270 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200271 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400272 rcu_assign_pointer(*list, ops);
273}
Steven Rostedt3d083392008-05-12 21:20:42 +0200274
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800275static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
276 struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400277{
278 struct ftrace_ops **p;
279
280 /*
281 * If we are removing the last function, then simply point
282 * to the ftrace_stub.
283 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800284 if (rcu_dereference_protected(*list,
285 lockdep_is_held(&ftrace_lock)) == ops &&
286 rcu_dereference_protected(ops->next,
287 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt2b499382011-05-03 22:49:52 -0400288 *list = &ftrace_list_end;
289 return 0;
290 }
291
292 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
293 if (*p == ops)
294 break;
295
296 if (*p != ops)
297 return -1;
298
299 *p = (*p)->next;
300 return 0;
301}
302
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400303static void ftrace_update_trampoline(struct ftrace_ops *ops);
304
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500305int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400306{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500307 if (ops->flags & FTRACE_OPS_FL_DELETED)
308 return -EINVAL;
309
Steven Rostedtb8489142011-05-04 09:27:52 -0400310 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
311 return -EBUSY;
312
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900313#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400314 /*
315 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
316 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
317 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
318 */
319 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
320 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
321 return -EINVAL;
322
323 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
324 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
325#endif
326
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400327 if (!core_kernel_data((unsigned long)ops))
328 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
329
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500330 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400331
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400332 /* Always save the function, and reset at unregistering */
333 ops->saved_func = ops->func;
334
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400335 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400336 ops->func = ftrace_pid_func;
337
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400338 ftrace_update_trampoline(ops);
339
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400340 if (ftrace_enabled)
341 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200342
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200343 return 0;
344}
345
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500346int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200347{
Steven Rostedt2b499382011-05-03 22:49:52 -0400348 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200349
Steven Rostedtb8489142011-05-04 09:27:52 -0400350 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
351 return -EBUSY;
352
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500353 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400354
Steven Rostedt2b499382011-05-03 22:49:52 -0400355 if (ret < 0)
356 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400357
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400358 if (ftrace_enabled)
359 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200360
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400361 ops->func = ops->saved_func;
362
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500363 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200364}
365
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500366static void ftrace_update_pid_func(void)
367{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400368 struct ftrace_ops *op;
369
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400370 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500371 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900372 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500373
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400374 do_for_each_ftrace_op(op, ftrace_ops_list) {
375 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400376 op->func = ftrace_pids_enabled(op) ?
377 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400378 ftrace_update_trampoline(op);
379 }
380 } while_for_each_ftrace_op(op);
381
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400382 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500383}
384
Steven Rostedt493762f2009-03-23 17:12:36 -0400385#ifdef CONFIG_FUNCTION_PROFILER
386struct ftrace_profile {
387 struct hlist_node node;
388 unsigned long ip;
389 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400390#ifdef CONFIG_FUNCTION_GRAPH_TRACER
391 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400392 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400393#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400394};
395
396struct ftrace_profile_page {
397 struct ftrace_profile_page *next;
398 unsigned long index;
399 struct ftrace_profile records[];
400};
401
Steven Rostedtcafb1682009-03-24 20:50:39 -0400402struct ftrace_profile_stat {
403 atomic_t disabled;
404 struct hlist_head *hash;
405 struct ftrace_profile_page *pages;
406 struct ftrace_profile_page *start;
407 struct tracer_stat stat;
408};
409
Steven Rostedt493762f2009-03-23 17:12:36 -0400410#define PROFILE_RECORDS_SIZE \
411 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
412
413#define PROFILES_PER_PAGE \
414 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
415
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400416static int ftrace_profile_enabled __read_mostly;
417
418/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400419static DEFINE_MUTEX(ftrace_profile_lock);
420
Steven Rostedtcafb1682009-03-24 20:50:39 -0400421static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400422
Namhyung Kim20079eb2013-04-10 08:55:50 +0900423#define FTRACE_PROFILE_HASH_BITS 10
424#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400425
Steven Rostedt493762f2009-03-23 17:12:36 -0400426static void *
427function_stat_next(void *v, int idx)
428{
429 struct ftrace_profile *rec = v;
430 struct ftrace_profile_page *pg;
431
432 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
433
434 again:
Li Zefan0296e422009-06-26 11:15:37 +0800435 if (idx != 0)
436 rec++;
437
Steven Rostedt493762f2009-03-23 17:12:36 -0400438 if ((void *)rec >= (void *)&pg->records[pg->index]) {
439 pg = pg->next;
440 if (!pg)
441 return NULL;
442 rec = &pg->records[0];
443 if (!rec->counter)
444 goto again;
445 }
446
447 return rec;
448}
449
450static void *function_stat_start(struct tracer_stat *trace)
451{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400452 struct ftrace_profile_stat *stat =
453 container_of(trace, struct ftrace_profile_stat, stat);
454
455 if (!stat || !stat->start)
456 return NULL;
457
458 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400459}
460
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400461#ifdef CONFIG_FUNCTION_GRAPH_TRACER
462/* function graph compares on total time */
463static int function_stat_cmp(void *p1, void *p2)
464{
465 struct ftrace_profile *a = p1;
466 struct ftrace_profile *b = p2;
467
468 if (a->time < b->time)
469 return -1;
470 if (a->time > b->time)
471 return 1;
472 else
473 return 0;
474}
475#else
476/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400477static int function_stat_cmp(void *p1, void *p2)
478{
479 struct ftrace_profile *a = p1;
480 struct ftrace_profile *b = p2;
481
482 if (a->counter < b->counter)
483 return -1;
484 if (a->counter > b->counter)
485 return 1;
486 else
487 return 0;
488}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400489#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400490
491static int function_stat_headers(struct seq_file *m)
492{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400493#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100494 seq_puts(m, " Function "
495 "Hit Time Avg s^2\n"
496 " -------- "
497 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400498#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100499 seq_puts(m, " Function Hit\n"
500 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400501#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400502 return 0;
503}
504
505static int function_stat_show(struct seq_file *m, void *v)
506{
507 struct ftrace_profile *rec = v;
508 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800509 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400510#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400511 static struct trace_seq s;
512 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400513 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400514#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800515 mutex_lock(&ftrace_profile_lock);
516
517 /* we raced with function_profile_reset() */
518 if (unlikely(rec->counter == 0)) {
519 ret = -EBUSY;
520 goto out;
521 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400522
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530523#ifdef CONFIG_FUNCTION_GRAPH_TRACER
524 avg = rec->time;
525 do_div(avg, rec->counter);
526 if (tracing_thresh && (avg < tracing_thresh))
527 goto out;
528#endif
529
Steven Rostedt493762f2009-03-23 17:12:36 -0400530 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400531 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400532
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400533#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100534 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400535
Chase Douglase330b3b2010-04-26 14:02:05 -0400536 /* Sample standard deviation (s^2) */
537 if (rec->counter <= 1)
538 stddev = 0;
539 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200540 /*
541 * Apply Welford's method:
542 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
543 */
544 stddev = rec->counter * rec->time_squared -
545 rec->time * rec->time;
546
Chase Douglase330b3b2010-04-26 14:02:05 -0400547 /*
548 * Divide only 1000 for ns^2 -> us^2 conversion.
549 * trace_print_graph_duration will divide 1000 again.
550 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200551 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400552 }
553
Steven Rostedt34886c82009-03-25 21:00:47 -0400554 trace_seq_init(&s);
555 trace_print_graph_duration(rec->time, &s);
556 trace_seq_puts(&s, " ");
557 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400558 trace_seq_puts(&s, " ");
559 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400560 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400561#endif
562 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800563out:
564 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400565
Li Zefan3aaba202010-08-23 16:50:12 +0800566 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400567}
568
Steven Rostedtcafb1682009-03-24 20:50:39 -0400569static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400570{
571 struct ftrace_profile_page *pg;
572
Steven Rostedtcafb1682009-03-24 20:50:39 -0400573 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400574
575 while (pg) {
576 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
577 pg->index = 0;
578 pg = pg->next;
579 }
580
Steven Rostedtcafb1682009-03-24 20:50:39 -0400581 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400582 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
583}
584
Steven Rostedtcafb1682009-03-24 20:50:39 -0400585int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400586{
587 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400588 int functions;
589 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400590 int i;
591
592 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400593 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400594 return 0;
595
Steven Rostedtcafb1682009-03-24 20:50:39 -0400596 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
597 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400598 return -ENOMEM;
599
Steven Rostedt318e0a72009-03-25 20:06:34 -0400600#ifdef CONFIG_DYNAMIC_FTRACE
601 functions = ftrace_update_tot_cnt;
602#else
603 /*
604 * We do not know the number of functions that exist because
605 * dynamic tracing is what counts them. With past experience
606 * we have around 20K functions. That should be more than enough.
607 * It is highly unlikely we will execute every function in
608 * the kernel.
609 */
610 functions = 20000;
611#endif
612
Steven Rostedtcafb1682009-03-24 20:50:39 -0400613 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400614
Steven Rostedt318e0a72009-03-25 20:06:34 -0400615 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
616
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900617 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400618 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400619 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400620 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400621 pg = pg->next;
622 }
623
624 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400625
626 out_free:
627 pg = stat->start;
628 while (pg) {
629 unsigned long tmp = (unsigned long)pg;
630
631 pg = pg->next;
632 free_page(tmp);
633 }
634
Steven Rostedt318e0a72009-03-25 20:06:34 -0400635 stat->pages = NULL;
636 stat->start = NULL;
637
638 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400639}
640
Steven Rostedtcafb1682009-03-24 20:50:39 -0400641static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400642{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400644 int size;
645
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 stat = &per_cpu(ftrace_profile_stats, cpu);
647
648 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400649 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400650 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400651 return 0;
652 }
653
654 /*
655 * We are profiling all functions, but usually only a few thousand
656 * functions are hit. We'll make a hash of 1024 items.
657 */
658 size = FTRACE_PROFILE_HASH_SIZE;
659
Kees Cook6396bb22018-06-12 14:03:40 -0700660 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400661
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400663 return -ENOMEM;
664
Steven Rostedt318e0a72009-03-25 20:06:34 -0400665 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 if (ftrace_profile_pages_init(stat) < 0) {
667 kfree(stat->hash);
668 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400669 return -ENOMEM;
670 }
671
672 return 0;
673}
674
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675static int ftrace_profile_init(void)
676{
677 int cpu;
678 int ret = 0;
679
Miao Xiec4602c12013-12-16 15:20:01 +0800680 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400681 ret = ftrace_profile_init_cpu(cpu);
682 if (ret)
683 break;
684 }
685
686 return ret;
687}
688
Steven Rostedt493762f2009-03-23 17:12:36 -0400689/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400690static struct ftrace_profile *
691ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400692{
693 struct ftrace_profile *rec;
694 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400695 unsigned long key;
696
Namhyung Kim20079eb2013-04-10 08:55:50 +0900697 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400698 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400699
700 if (hlist_empty(hhd))
701 return NULL;
702
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400703 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400704 if (rec->ip == ip)
705 return rec;
706 }
707
708 return NULL;
709}
710
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711static void ftrace_add_profile(struct ftrace_profile_stat *stat,
712 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400713{
714 unsigned long key;
715
Namhyung Kim20079eb2013-04-10 08:55:50 +0900716 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400717 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400718}
719
Steven Rostedt318e0a72009-03-25 20:06:34 -0400720/*
721 * The memory is already allocated, this simply finds a new record to use.
722 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400723static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400724ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400725{
726 struct ftrace_profile *rec = NULL;
727
Steven Rostedt318e0a72009-03-25 20:06:34 -0400728 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400729 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400730 goto out;
731
Steven Rostedt493762f2009-03-23 17:12:36 -0400732 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400733 * Try to find the function again since an NMI
734 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400735 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400736 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400737 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400738 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400739
Steven Rostedtcafb1682009-03-24 20:50:39 -0400740 if (stat->pages->index == PROFILES_PER_PAGE) {
741 if (!stat->pages->next)
742 goto out;
743 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 }
745
Steven Rostedtcafb1682009-03-24 20:50:39 -0400746 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400749
Steven Rostedt493762f2009-03-23 17:12:36 -0400750 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400751 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400752
753 return rec;
754}
755
Steven Rostedt493762f2009-03-23 17:12:36 -0400756static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400757function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400758 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400759{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400761 struct ftrace_profile *rec;
762 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400763
764 if (!ftrace_profile_enabled)
765 return;
766
Steven Rostedt493762f2009-03-23 17:12:36 -0400767 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400768
Christoph Lameterbdffd892014-04-29 14:17:40 -0500769 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400770 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771 goto out;
772
773 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400774 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400775 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400776 if (!rec)
777 goto out;
778 }
779
780 rec->counter++;
781 out:
782 local_irq_restore(flags);
783}
784
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400785#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500786static bool fgraph_graph_time = true;
787
788void ftrace_graph_graph_time_control(bool enable)
789{
790 fgraph_graph_time = enable;
791}
792
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400793static int profile_graph_entry(struct ftrace_graph_ent *trace)
794{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500795 struct ftrace_ret_stack *ret_stack;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900796
Steven Rostedta1e2e312011-08-09 12:50:46 -0400797 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900798
Steven Rostedt (VMware)a8f0f9e2017-08-17 16:37:25 -0400799 /* If function graph is shutting down, ret_stack can be NULL */
800 if (!current->ret_stack)
801 return 0;
802
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500803 ret_stack = ftrace_graph_get_ret_stack(current, 0);
804 if (ret_stack)
805 ret_stack->subtime = 0;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900806
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400807 return 1;
808}
809
810static void profile_graph_return(struct ftrace_graph_ret *trace)
811{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500812 struct ftrace_ret_stack *ret_stack;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400814 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400815 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400817
818 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500819 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400820 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 goto out;
822
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400823 /* If the calltime was zero'd ignore it */
824 if (!trace->calltime)
825 goto out;
826
Steven Rostedta2a16d62009-03-24 23:17:58 -0400827 calltime = trace->rettime - trace->calltime;
828
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400829 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400830
831 /* Append this call time to the parent time to subtract */
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500832 ret_stack = ftrace_graph_get_ret_stack(current, 1);
833 if (ret_stack)
834 ret_stack->subtime += calltime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400835
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500836 ret_stack = ftrace_graph_get_ret_stack(current, 0);
837 if (ret_stack && ret_stack->subtime < calltime)
838 calltime -= ret_stack->subtime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400839 else
840 calltime = 0;
841 }
842
Steven Rostedtcafb1682009-03-24 20:50:39 -0400843 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400844 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400845 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400846 rec->time_squared += calltime * calltime;
847 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400848
Steven Rostedtcafb1682009-03-24 20:50:39 -0400849 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400850 local_irq_restore(flags);
851}
852
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500853static struct fgraph_ops fprofiler_ops = {
854 .entryfunc = &profile_graph_entry,
855 .retfunc = &profile_graph_return,
856};
857
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400858static int register_ftrace_profiler(void)
859{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500860 return register_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400861}
862
863static void unregister_ftrace_profiler(void)
864{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500865 unregister_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400866}
867#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100868static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400869 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900870 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400871 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400872};
873
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400874static int register_ftrace_profiler(void)
875{
876 return register_ftrace_function(&ftrace_profile_ops);
877}
878
879static void unregister_ftrace_profiler(void)
880{
881 unregister_ftrace_function(&ftrace_profile_ops);
882}
883#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
884
Steven Rostedt493762f2009-03-23 17:12:36 -0400885static ssize_t
886ftrace_profile_write(struct file *filp, const char __user *ubuf,
887 size_t cnt, loff_t *ppos)
888{
889 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400890 int ret;
891
Peter Huewe22fe9b52011-06-07 21:58:27 +0200892 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
893 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400894 return ret;
895
896 val = !!val;
897
898 mutex_lock(&ftrace_profile_lock);
899 if (ftrace_profile_enabled ^ val) {
900 if (val) {
901 ret = ftrace_profile_init();
902 if (ret < 0) {
903 cnt = ret;
904 goto out;
905 }
906
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400907 ret = register_ftrace_profiler();
908 if (ret < 0) {
909 cnt = ret;
910 goto out;
911 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400912 ftrace_profile_enabled = 1;
913 } else {
914 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400915 /*
916 * unregister_ftrace_profiler calls stop_machine
917 * so this acts like an synchronize_sched.
918 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400919 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400920 }
921 }
922 out:
923 mutex_unlock(&ftrace_profile_lock);
924
Jiri Olsacf8517c2009-10-23 19:36:16 -0400925 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400926
927 return cnt;
928}
929
930static ssize_t
931ftrace_profile_read(struct file *filp, char __user *ubuf,
932 size_t cnt, loff_t *ppos)
933{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400934 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400935 int r;
936
937 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
938 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
939}
940
941static const struct file_operations ftrace_profile_fops = {
942 .open = tracing_open_generic,
943 .read = ftrace_profile_read,
944 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200945 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400946};
947
Steven Rostedtcafb1682009-03-24 20:50:39 -0400948/* used to initialize the real stat files */
949static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400950 .name = "functions",
951 .stat_start = function_stat_start,
952 .stat_next = function_stat_next,
953 .stat_cmp = function_stat_cmp,
954 .stat_headers = function_stat_headers,
955 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400956};
957
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500958static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400959{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400960 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400961 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400962 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400963 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400964 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400965
Steven Rostedtcafb1682009-03-24 20:50:39 -0400966 for_each_possible_cpu(cpu) {
967 stat = &per_cpu(ftrace_profile_stats, cpu);
968
Geliang Tang6363c6b2016-03-15 22:12:34 +0800969 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400970 if (!name) {
971 /*
972 * The files created are permanent, if something happens
973 * we still do not free memory.
974 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400975 WARN(1,
976 "Could not allocate stat file for cpu %d\n",
977 cpu);
978 return;
979 }
980 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400981 stat->stat.name = name;
982 ret = register_stat_tracer(&stat->stat);
983 if (ret) {
984 WARN(1,
985 "Could not register function stat for cpu %d\n",
986 cpu);
987 kfree(name);
988 return;
989 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400990 }
991
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500992 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -0400993 d_tracer, NULL, &ftrace_profile_fops);
994 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700995 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -0400996}
997
998#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500999static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001000{
1001}
1002#endif /* CONFIG_FUNCTION_PROFILER */
1003
Steven Rostedt3d083392008-05-12 21:20:42 +02001004#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001005
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001006static struct ftrace_ops *removed_ops;
1007
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001008/*
1009 * Set when doing a global update, like enabling all recs or disabling them.
1010 * It is not set when just updating a single ftrace_ops.
1011 */
1012static bool update_all_ops;
1013
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001014#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001015# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001016#endif
1017
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001018struct ftrace_func_entry {
1019 struct hlist_node hlist;
1020 unsigned long ip;
1021};
1022
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001023struct ftrace_func_probe {
1024 struct ftrace_probe_ops *probe_ops;
1025 struct ftrace_ops ops;
1026 struct trace_array *tr;
1027 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001028 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001029 int ref;
1030};
1031
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001032/*
1033 * We make these constant because no one should touch them,
1034 * but they are used as the default "empty hash", to avoid allocating
1035 * it all the time. These are in a read only section such that if
1036 * anyone does try to modify it, it will cause an exception.
1037 */
1038static const struct hlist_head empty_buckets[1];
1039static const struct ftrace_hash empty_hash = {
1040 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001041};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001042#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001043
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001044struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001045 .func = ftrace_stub,
1046 .local_hash.notrace_hash = EMPTY_HASH,
1047 .local_hash.filter_hash = EMPTY_HASH,
1048 INIT_OPS_HASH(global_ops)
1049 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001050 FTRACE_OPS_FL_INITIALIZED |
1051 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001052};
1053
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001054/*
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001055 * Used by the stack undwinder to know about dynamic ftrace trampolines.
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001056 */
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001057struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001058{
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001059 struct ftrace_ops *op = NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001060
1061 /*
1062 * Some of the ops may be dynamically allocated,
1063 * they are freed after a synchronize_sched().
1064 */
1065 preempt_disable_notrace();
1066
1067 do_for_each_ftrace_op(op, ftrace_ops_list) {
1068 /*
1069 * This is to check for dynamically allocated trampolines.
1070 * Trampolines that are in kernel text will have
1071 * core_kernel_text() return true.
1072 */
1073 if (op->trampoline && op->trampoline_size)
1074 if (addr >= op->trampoline &&
1075 addr < op->trampoline + op->trampoline_size) {
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001076 preempt_enable_notrace();
1077 return op;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001078 }
1079 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001080 preempt_enable_notrace();
1081
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001082 return NULL;
1083}
1084
1085/*
1086 * This is used by __kernel_text_address() to return true if the
1087 * address is on a dynamically allocated trampoline that would
1088 * not return true for either core_kernel_text() or
1089 * is_module_text_address().
1090 */
1091bool is_ftrace_trampoline(unsigned long addr)
1092{
1093 return ftrace_ops_trampoline(addr) != NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001094}
1095
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001096struct ftrace_page {
1097 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001098 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001099 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001100 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001101};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001102
Steven Rostedta7900872011-12-16 16:23:44 -05001103#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1104#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001105
1106/* estimate from running different kernels */
1107#define NR_TO_INIT 10000
1108
1109static struct ftrace_page *ftrace_pages_start;
1110static struct ftrace_page *ftrace_pages;
1111
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001112static __always_inline unsigned long
1113ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1114{
1115 if (hash->size_bits > 0)
1116 return hash_long(ip, hash->size_bits);
1117
1118 return 0;
1119}
1120
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001121/* Only use this function if ftrace_hash_empty() has already been tested */
1122static __always_inline struct ftrace_func_entry *
1123__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001124{
1125 unsigned long key;
1126 struct ftrace_func_entry *entry;
1127 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001128
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001129 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001130 hhd = &hash->buckets[key];
1131
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001132 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001133 if (entry->ip == ip)
1134 return entry;
1135 }
1136 return NULL;
1137}
1138
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001139/**
1140 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1141 * @hash: The hash to look at
1142 * @ip: The instruction pointer to test
1143 *
1144 * Search a given @hash to see if a given instruction pointer (@ip)
1145 * exists in it.
1146 *
1147 * Returns the entry that holds the @ip if found. NULL otherwise.
1148 */
1149struct ftrace_func_entry *
1150ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1151{
1152 if (ftrace_hash_empty(hash))
1153 return NULL;
1154
1155 return __ftrace_lookup_ip(hash, ip);
1156}
1157
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001158static void __add_hash_entry(struct ftrace_hash *hash,
1159 struct ftrace_func_entry *entry)
1160{
1161 struct hlist_head *hhd;
1162 unsigned long key;
1163
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001164 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001165 hhd = &hash->buckets[key];
1166 hlist_add_head(&entry->hlist, hhd);
1167 hash->count++;
1168}
1169
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001170static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1171{
1172 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001173
1174 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1175 if (!entry)
1176 return -ENOMEM;
1177
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001179 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001180
1181 return 0;
1182}
1183
1184static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001185free_hash_entry(struct ftrace_hash *hash,
1186 struct ftrace_func_entry *entry)
1187{
1188 hlist_del(&entry->hlist);
1189 kfree(entry);
1190 hash->count--;
1191}
1192
1193static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001194remove_hash_entry(struct ftrace_hash *hash,
1195 struct ftrace_func_entry *entry)
1196{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001197 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001198 hash->count--;
1199}
1200
1201static void ftrace_hash_clear(struct ftrace_hash *hash)
1202{
1203 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001204 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001205 struct ftrace_func_entry *entry;
1206 int size = 1 << hash->size_bits;
1207 int i;
1208
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001209 if (!hash->count)
1210 return;
1211
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001212 for (i = 0; i < size; i++) {
1213 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001214 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001215 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001216 }
1217 FTRACE_WARN_ON(hash->count);
1218}
1219
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001220static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1221{
1222 list_del(&ftrace_mod->list);
1223 kfree(ftrace_mod->module);
1224 kfree(ftrace_mod->func);
1225 kfree(ftrace_mod);
1226}
1227
1228static void clear_ftrace_mod_list(struct list_head *head)
1229{
1230 struct ftrace_mod_load *p, *n;
1231
1232 /* stack tracer isn't supported yet */
1233 if (!head)
1234 return;
1235
1236 mutex_lock(&ftrace_lock);
1237 list_for_each_entry_safe(p, n, head, list)
1238 free_ftrace_mod(p);
1239 mutex_unlock(&ftrace_lock);
1240}
1241
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001242static void free_ftrace_hash(struct ftrace_hash *hash)
1243{
1244 if (!hash || hash == EMPTY_HASH)
1245 return;
1246 ftrace_hash_clear(hash);
1247 kfree(hash->buckets);
1248 kfree(hash);
1249}
1250
Steven Rostedt07fd5512011-05-05 18:03:47 -04001251static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1252{
1253 struct ftrace_hash *hash;
1254
1255 hash = container_of(rcu, struct ftrace_hash, rcu);
1256 free_ftrace_hash(hash);
1257}
1258
1259static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1260{
1261 if (!hash || hash == EMPTY_HASH)
1262 return;
1263 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1264}
1265
Jiri Olsa5500fa52012-02-15 15:51:54 +01001266void ftrace_free_filter(struct ftrace_ops *ops)
1267{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001268 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001269 free_ftrace_hash(ops->func_hash->filter_hash);
1270 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001271}
1272
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001273static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1274{
1275 struct ftrace_hash *hash;
1276 int size;
1277
1278 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1279 if (!hash)
1280 return NULL;
1281
1282 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001283 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001284
1285 if (!hash->buckets) {
1286 kfree(hash);
1287 return NULL;
1288 }
1289
1290 hash->size_bits = size_bits;
1291
1292 return hash;
1293}
1294
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001295
1296static int ftrace_add_mod(struct trace_array *tr,
1297 const char *func, const char *module,
1298 int enable)
1299{
1300 struct ftrace_mod_load *ftrace_mod;
1301 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1302
1303 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1304 if (!ftrace_mod)
1305 return -ENOMEM;
1306
1307 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1308 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1309 ftrace_mod->enable = enable;
1310
1311 if (!ftrace_mod->func || !ftrace_mod->module)
1312 goto out_free;
1313
1314 list_add(&ftrace_mod->list, mod_head);
1315
1316 return 0;
1317
1318 out_free:
1319 free_ftrace_mod(ftrace_mod);
1320
1321 return -ENOMEM;
1322}
1323
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001324static struct ftrace_hash *
1325alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1326{
1327 struct ftrace_func_entry *entry;
1328 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001329 int size;
1330 int ret;
1331 int i;
1332
1333 new_hash = alloc_ftrace_hash(size_bits);
1334 if (!new_hash)
1335 return NULL;
1336
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001337 if (hash)
1338 new_hash->flags = hash->flags;
1339
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001340 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001341 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342 return new_hash;
1343
1344 size = 1 << hash->size_bits;
1345 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001346 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001347 ret = add_hash_entry(new_hash, entry->ip);
1348 if (ret < 0)
1349 goto free_hash;
1350 }
1351 }
1352
1353 FTRACE_WARN_ON(new_hash->count != hash->count);
1354
1355 return new_hash;
1356
1357 free_hash:
1358 free_ftrace_hash(new_hash);
1359 return NULL;
1360}
1361
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001362static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001363ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001364static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001365ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001366
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001367static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1368 struct ftrace_hash *new_hash);
1369
Namhyung Kim3e278c02017-01-20 11:44:45 +09001370static struct ftrace_hash *
1371__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001372{
1373 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001374 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001375 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001376 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001377 int size = src->count;
1378 int bits = 0;
1379 int i;
1380
1381 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001382 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001383 */
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001384 if (ftrace_hash_empty(src))
Namhyung Kim3e278c02017-01-20 11:44:45 +09001385 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001386
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001387 /*
1388 * Make the hash size about 1/2 the # found
1389 */
1390 for (size /= 2; size; size >>= 1)
1391 bits++;
1392
1393 /* Don't allocate too much */
1394 if (bits > FTRACE_HASH_MAX_BITS)
1395 bits = FTRACE_HASH_MAX_BITS;
1396
Steven Rostedt07fd5512011-05-05 18:03:47 -04001397 new_hash = alloc_ftrace_hash(bits);
1398 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001399 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001400
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001401 new_hash->flags = src->flags;
1402
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001403 size = 1 << src->size_bits;
1404 for (i = 0; i < size; i++) {
1405 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001406 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001407 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001408 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001409 }
1410 }
1411
Namhyung Kim3e278c02017-01-20 11:44:45 +09001412 return new_hash;
1413}
1414
1415static 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
1478 * the hashes are freed with call_rcu_sched().
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
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001534/**
1535 * ftrace_location_range - return the first address of a traced location
1536 * if it touches the given ip range
1537 * @start: start of range to search.
1538 * @end: end of range to search (inclusive). @end points to the last byte
1539 * to check.
1540 *
1541 * Returns rec->ip if the related ftrace location is a least partly within
1542 * the given address range. That is, the first address of the instruction
1543 * that is either a NOP or call to the function tracer. It checks the ftrace
1544 * internal tables to determine if the address belongs or not.
1545 */
1546unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001547{
1548 struct ftrace_page *pg;
1549 struct dyn_ftrace *rec;
1550 struct dyn_ftrace key;
1551
1552 key.ip = start;
1553 key.flags = end; /* overload flags, as it is unsigned long */
1554
1555 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1556 if (end < pg->records[0].ip ||
1557 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1558 continue;
1559 rec = bsearch(&key, pg->records, pg->index,
1560 sizeof(struct dyn_ftrace),
1561 ftrace_cmp_recs);
1562 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001563 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001564 }
1565
Steven Rostedt5855fea2011-12-16 19:27:42 -05001566 return 0;
1567}
1568
Steven Rostedtc88fd862011-08-16 09:53:39 -04001569/**
1570 * ftrace_location - return true if the ip giving is a traced location
1571 * @ip: the instruction pointer to check
1572 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001573 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001574 * That is, the instruction that is either a NOP or call to
1575 * the function tracer. It checks the ftrace internal tables to
1576 * determine if the address belongs or not.
1577 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001578unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001579{
Steven Rostedta650e022012-04-25 13:48:13 -04001580 return ftrace_location_range(ip, ip);
1581}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001582
Steven Rostedta650e022012-04-25 13:48:13 -04001583/**
1584 * ftrace_text_reserved - return true if range contains an ftrace location
1585 * @start: start of range to search
1586 * @end: end of range to search (inclusive). @end points to the last byte to check.
1587 *
1588 * Returns 1 if @start and @end contains a ftrace location.
1589 * That is, the instruction that is either a NOP or call to
1590 * the function tracer. It checks the ftrace internal tables to
1591 * determine if the address belongs or not.
1592 */
Sasha Levind88471c2013-01-09 18:09:20 -05001593int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001594{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001595 unsigned long ret;
1596
1597 ret = ftrace_location_range((unsigned long)start,
1598 (unsigned long)end);
1599
1600 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001601}
1602
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001603/* Test if ops registered to this rec needs regs */
1604static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1605{
1606 struct ftrace_ops *ops;
1607 bool keep_regs = false;
1608
1609 for (ops = ftrace_ops_list;
1610 ops != &ftrace_list_end; ops = ops->next) {
1611 /* pass rec in as regs to have non-NULL val */
1612 if (ftrace_ops_test(ops, rec->ip, rec)) {
1613 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1614 keep_regs = true;
1615 break;
1616 }
1617 }
1618 }
1619
1620 return keep_regs;
1621}
1622
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001623static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001624 int filter_hash,
1625 bool inc)
1626{
1627 struct ftrace_hash *hash;
1628 struct ftrace_hash *other_hash;
1629 struct ftrace_page *pg;
1630 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001631 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001632 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001633 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001634
1635 /* Only update if the ops has been registered */
1636 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001637 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001638
1639 /*
1640 * In the filter_hash case:
1641 * If the count is zero, we update all records.
1642 * Otherwise we just update the items in the hash.
1643 *
1644 * In the notrace_hash case:
1645 * We enable the update in the hash.
1646 * As disabling notrace means enabling the tracing,
1647 * and enabling notrace means disabling, the inc variable
1648 * gets inversed.
1649 */
1650 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001651 hash = ops->func_hash->filter_hash;
1652 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001653 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001654 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001655 } else {
1656 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001657 hash = ops->func_hash->notrace_hash;
1658 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001659 /*
1660 * If the notrace hash has no items,
1661 * then there's nothing to do.
1662 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001663 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001664 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001665 }
1666
1667 do_for_each_ftrace_rec(pg, rec) {
1668 int in_other_hash = 0;
1669 int in_hash = 0;
1670 int match = 0;
1671
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001672 if (rec->flags & FTRACE_FL_DISABLED)
1673 continue;
1674
Steven Rostedted926f92011-05-03 13:25:24 -04001675 if (all) {
1676 /*
1677 * Only the filter_hash affects all records.
1678 * Update if the record is not in the notrace hash.
1679 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001680 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001681 match = 1;
1682 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001683 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1684 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001685
1686 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001687 * If filter_hash is set, we want to match all functions
1688 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001689 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001690 * If filter_hash is not set, then we are decrementing.
1691 * That means we match anything that is in the hash
1692 * and also in the other_hash. That is, we need to turn
1693 * off functions in the other hash because they are disabled
1694 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001695 */
1696 if (filter_hash && in_hash && !in_other_hash)
1697 match = 1;
1698 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001699 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001700 match = 1;
1701 }
1702 if (!match)
1703 continue;
1704
1705 if (inc) {
1706 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001707 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001708 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001709
1710 /*
1711 * If there's only a single callback registered to a
1712 * function, and the ops has a trampoline registered
1713 * for it, then we can call it directly.
1714 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001715 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001716 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001717 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001718 /*
1719 * If we are adding another function callback
1720 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001721 * custom trampoline in use, then we need to go
1722 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001723 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001724 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001725
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001726 /*
1727 * If any ops wants regs saved for this function
1728 * then all ops will get saved regs.
1729 */
1730 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1731 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001732 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001733 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001734 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001735 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001736
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001737 /*
1738 * If the rec had REGS enabled and the ops that is
1739 * being removed had REGS set, then see if there is
1740 * still any ops for this record that wants regs.
1741 * If not, we can stop recording them.
1742 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001743 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001744 rec->flags & FTRACE_FL_REGS &&
1745 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1746 if (!test_rec_ops_needs_regs(rec))
1747 rec->flags &= ~FTRACE_FL_REGS;
1748 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001749
1750 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001751 * If the rec had TRAMP enabled, then it needs to
1752 * be cleared. As TRAMP can only be enabled iff
1753 * there is only a single ops attached to it.
1754 * In otherwords, always disable it on decrementing.
1755 * In the future, we may set it if rec count is
1756 * decremented to one, and the ops that is left
1757 * has a trampoline.
1758 */
1759 rec->flags &= ~FTRACE_FL_TRAMP;
1760
1761 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001762 * flags will be cleared in ftrace_check_record()
1763 * if rec count is zero.
1764 */
Steven Rostedted926f92011-05-03 13:25:24 -04001765 }
1766 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001767
1768 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1769 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1770
Steven Rostedted926f92011-05-03 13:25:24 -04001771 /* Shortcut, if we handled all records, we are done. */
1772 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001773 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001774 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001775
1776 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001777}
1778
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001779static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001780 int filter_hash)
1781{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001782 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001783}
1784
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001785static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001786 int filter_hash)
1787{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001788 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001789}
1790
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001791static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1792 int filter_hash, int inc)
1793{
1794 struct ftrace_ops *op;
1795
1796 __ftrace_hash_rec_update(ops, filter_hash, inc);
1797
1798 if (ops->func_hash != &global_ops.local_hash)
1799 return;
1800
1801 /*
1802 * If the ops shares the global_ops hash, then we need to update
1803 * all ops that are enabled and use this hash.
1804 */
1805 do_for_each_ftrace_op(op, ftrace_ops_list) {
1806 /* Already done */
1807 if (op == ops)
1808 continue;
1809 if (op->func_hash == &global_ops.local_hash)
1810 __ftrace_hash_rec_update(op, filter_hash, inc);
1811 } while_for_each_ftrace_op(op);
1812}
1813
1814static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1815 int filter_hash)
1816{
1817 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1818}
1819
1820static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1821 int filter_hash)
1822{
1823 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1824}
1825
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001826/*
1827 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1828 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1829 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1830 * Note that old_hash and new_hash has below meanings
1831 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1832 * - If the hash is EMPTY_HASH, it hits nothing
1833 * - Anything else hits the recs which match the hash entries.
1834 */
1835static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1836 struct ftrace_hash *old_hash,
1837 struct ftrace_hash *new_hash)
1838{
1839 struct ftrace_page *pg;
1840 struct dyn_ftrace *rec, *end = NULL;
1841 int in_old, in_new;
1842
1843 /* Only update if the ops has been registered */
1844 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1845 return 0;
1846
1847 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1848 return 0;
1849
1850 /*
1851 * Since the IPMODIFY is a very address sensitive action, we do not
1852 * allow ftrace_ops to set all functions to new hash.
1853 */
1854 if (!new_hash || !old_hash)
1855 return -EINVAL;
1856
1857 /* Update rec->flags */
1858 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001859
1860 if (rec->flags & FTRACE_FL_DISABLED)
1861 continue;
1862
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001863 /* We need to update only differences of filter_hash */
1864 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1865 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1866 if (in_old == in_new)
1867 continue;
1868
1869 if (in_new) {
1870 /* New entries must ensure no others are using it */
1871 if (rec->flags & FTRACE_FL_IPMODIFY)
1872 goto rollback;
1873 rec->flags |= FTRACE_FL_IPMODIFY;
1874 } else /* Removed entry */
1875 rec->flags &= ~FTRACE_FL_IPMODIFY;
1876 } while_for_each_ftrace_rec();
1877
1878 return 0;
1879
1880rollback:
1881 end = rec;
1882
1883 /* Roll back what we did above */
1884 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001885
1886 if (rec->flags & FTRACE_FL_DISABLED)
1887 continue;
1888
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001889 if (rec == end)
1890 goto err_out;
1891
1892 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1893 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1894 if (in_old == in_new)
1895 continue;
1896
1897 if (in_new)
1898 rec->flags &= ~FTRACE_FL_IPMODIFY;
1899 else
1900 rec->flags |= FTRACE_FL_IPMODIFY;
1901 } while_for_each_ftrace_rec();
1902
1903err_out:
1904 return -EBUSY;
1905}
1906
1907static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1908{
1909 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1910
1911 if (ftrace_hash_empty(hash))
1912 hash = NULL;
1913
1914 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1915}
1916
1917/* Disabling always succeeds */
1918static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1919{
1920 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1921
1922 if (ftrace_hash_empty(hash))
1923 hash = NULL;
1924
1925 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1926}
1927
1928static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1929 struct ftrace_hash *new_hash)
1930{
1931 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1932
1933 if (ftrace_hash_empty(old_hash))
1934 old_hash = NULL;
1935
1936 if (ftrace_hash_empty(new_hash))
1937 new_hash = NULL;
1938
1939 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1940}
1941
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001942static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001943{
1944 int i;
1945
1946 printk(KERN_CONT "%s", fmt);
1947
1948 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1949 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1950}
1951
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001952static struct ftrace_ops *
1953ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001954static struct ftrace_ops *
1955ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001956
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001957enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001958const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001959
1960static void print_bug_type(void)
1961{
1962 switch (ftrace_bug_type) {
1963 case FTRACE_BUG_UNKNOWN:
1964 break;
1965 case FTRACE_BUG_INIT:
1966 pr_info("Initializing ftrace call sites\n");
1967 break;
1968 case FTRACE_BUG_NOP:
1969 pr_info("Setting ftrace call site to NOP\n");
1970 break;
1971 case FTRACE_BUG_CALL:
1972 pr_info("Setting ftrace call site to call ftrace function\n");
1973 break;
1974 case FTRACE_BUG_UPDATE:
1975 pr_info("Updating ftrace call site to call a different ftrace function\n");
1976 break;
1977 }
1978}
1979
Steven Rostedtc88fd862011-08-16 09:53:39 -04001980/**
1981 * ftrace_bug - report and shutdown function tracer
1982 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001983 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001984 *
1985 * The arch code that enables or disables the function tracing
1986 * can call ftrace_bug() when it has detected a problem in
1987 * modifying the code. @failed should be one of either:
1988 * EFAULT - if the problem happens on reading the @ip address
1989 * EINVAL - if what is read at @ip is not what was expected
1990 * EPERM - if the problem happens on writting to the @ip address
1991 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001992void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001993{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001994 unsigned long ip = rec ? rec->ip : 0;
1995
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001996 switch (failed) {
1997 case -EFAULT:
1998 FTRACE_WARN_ON_ONCE(1);
1999 pr_info("ftrace faulted on modifying ");
2000 print_ip_sym(ip);
2001 break;
2002 case -EINVAL:
2003 FTRACE_WARN_ON_ONCE(1);
2004 pr_info("ftrace failed to modify ");
2005 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002006 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002007 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002008 if (ftrace_expected) {
2009 print_ip_ins(" expected: ", ftrace_expected);
2010 pr_cont("\n");
2011 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002012 break;
2013 case -EPERM:
2014 FTRACE_WARN_ON_ONCE(1);
2015 pr_info("ftrace faulted on writing ");
2016 print_ip_sym(ip);
2017 break;
2018 default:
2019 FTRACE_WARN_ON_ONCE(1);
2020 pr_info("ftrace faulted on unknown error ");
2021 print_ip_sym(ip);
2022 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002023 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002024 if (rec) {
2025 struct ftrace_ops *ops = NULL;
2026
2027 pr_info("ftrace record flags: %lx\n", rec->flags);
2028 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2029 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2030 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2031 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002032 if (ops) {
2033 do {
2034 pr_cont("\ttramp: %pS (%pS)",
2035 (void *)ops->trampoline,
2036 (void *)ops->func);
2037 ops = ftrace_find_tramp_ops_next(rec, ops);
2038 } while (ops);
2039 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002040 pr_cont("\ttramp: ERROR!");
2041
2042 }
2043 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002044 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002045 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002046}
2047
Steven Rostedtc88fd862011-08-16 09:53:39 -04002048static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002049{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002050 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002051
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002052 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2053
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002054 if (rec->flags & FTRACE_FL_DISABLED)
2055 return FTRACE_UPDATE_IGNORE;
2056
Steven Rostedt982c3502008-11-15 16:31:41 -05002057 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002058 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002059 *
Steven Rostedted926f92011-05-03 13:25:24 -04002060 * If the record has a ref count, then we need to enable it
2061 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002062 *
Steven Rostedted926f92011-05-03 13:25:24 -04002063 * Otherwise we make sure its disabled.
2064 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002065 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002066 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002067 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002068 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002069 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002070
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002071 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002072 * If enabling and the REGS flag does not match the REGS_EN, or
2073 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2074 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002075 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002076 if (flag) {
2077 if (!(rec->flags & FTRACE_FL_REGS) !=
2078 !(rec->flags & FTRACE_FL_REGS_EN))
2079 flag |= FTRACE_FL_REGS;
2080
2081 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2082 !(rec->flags & FTRACE_FL_TRAMP_EN))
2083 flag |= FTRACE_FL_TRAMP;
2084 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002085
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002086 /* If the state of this record hasn't changed, then do nothing */
2087 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002088 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002089
2090 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002091 /* Save off if rec is being enabled (for return value) */
2092 flag ^= rec->flags & FTRACE_FL_ENABLED;
2093
2094 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002095 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002096 if (flag & FTRACE_FL_REGS) {
2097 if (rec->flags & FTRACE_FL_REGS)
2098 rec->flags |= FTRACE_FL_REGS_EN;
2099 else
2100 rec->flags &= ~FTRACE_FL_REGS_EN;
2101 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002102 if (flag & FTRACE_FL_TRAMP) {
2103 if (rec->flags & FTRACE_FL_TRAMP)
2104 rec->flags |= FTRACE_FL_TRAMP_EN;
2105 else
2106 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2107 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002108 }
2109
2110 /*
2111 * If this record is being updated from a nop, then
2112 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002113 * Otherwise,
2114 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002115 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002116 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002117 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002118 if (flag & FTRACE_FL_ENABLED) {
2119 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002120 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002121 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002122
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002123 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002124 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002125 }
2126
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002127 if (update) {
2128 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002129 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002130 rec->flags = 0;
2131 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002132 /*
2133 * Just disable the record, but keep the ops TRAMP
2134 * and REGS states. The _EN flags must be disabled though.
2135 */
2136 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2137 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002138 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002139
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002140 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002141 return FTRACE_UPDATE_MAKE_NOP;
2142}
2143
2144/**
2145 * ftrace_update_record, set a record that now is tracing or not
2146 * @rec: the record to update
2147 * @enable: set to 1 if the record is tracing, zero to force disable
2148 *
2149 * The records that represent all functions that can be traced need
2150 * to be updated when tracing has been enabled.
2151 */
2152int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2153{
2154 return ftrace_check_record(rec, enable, 1);
2155}
2156
2157/**
2158 * ftrace_test_record, check if the record has been enabled or not
2159 * @rec: the record to test
2160 * @enable: set to 1 to check if enabled, 0 if it is disabled
2161 *
2162 * The arch code may need to test if a record is already set to
2163 * tracing to determine how to modify the function code that it
2164 * represents.
2165 */
2166int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2167{
2168 return ftrace_check_record(rec, enable, 0);
2169}
2170
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002171static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002172ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2173{
2174 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002175 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002176
2177 do_for_each_ftrace_op(op, ftrace_ops_list) {
2178
2179 if (!op->trampoline)
2180 continue;
2181
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002182 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002183 return op;
2184 } while_for_each_ftrace_op(op);
2185
2186 return NULL;
2187}
2188
2189static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002190ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2191 struct ftrace_ops *op)
2192{
2193 unsigned long ip = rec->ip;
2194
2195 while_for_each_ftrace_op(op) {
2196
2197 if (!op->trampoline)
2198 continue;
2199
2200 if (hash_contains_ip(ip, op->func_hash))
2201 return op;
2202 }
2203
2204 return NULL;
2205}
2206
2207static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002208ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2209{
2210 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002211 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002212
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002213 /*
2214 * Need to check removed ops first.
2215 * If they are being removed, and this rec has a tramp,
2216 * and this rec is in the ops list, then it would be the
2217 * one with the tramp.
2218 */
2219 if (removed_ops) {
2220 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002221 return removed_ops;
2222 }
2223
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002224 /*
2225 * Need to find the current trampoline for a rec.
2226 * Now, a trampoline is only attached to a rec if there
2227 * was a single 'ops' attached to it. But this can be called
2228 * when we are adding another op to the rec or removing the
2229 * current one. Thus, if the op is being added, we can
2230 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002231 * yet.
2232 *
2233 * If an ops is being modified (hooking to different functions)
2234 * then we don't care about the new functions that are being
2235 * added, just the old ones (that are probably being removed).
2236 *
2237 * If we are adding an ops to a function that already is using
2238 * a trampoline, it needs to be removed (trampolines are only
2239 * for single ops connected), then an ops that is not being
2240 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002241 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002242 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002243
2244 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002245 continue;
2246
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002247 /*
2248 * If the ops is being added, it hasn't gotten to
2249 * the point to be removed from this tree yet.
2250 */
2251 if (op->flags & FTRACE_OPS_FL_ADDING)
2252 continue;
2253
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002254
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002255 /*
2256 * If the ops is being modified and is in the old
2257 * hash, then it is probably being removed from this
2258 * function.
2259 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002260 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2261 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002262 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002263 /*
2264 * If the ops is not being added or modified, and it's
2265 * in its normal filter hash, then this must be the one
2266 * we want!
2267 */
2268 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2269 hash_contains_ip(ip, op->func_hash))
2270 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002271
2272 } while_for_each_ftrace_op(op);
2273
2274 return NULL;
2275}
2276
2277static struct ftrace_ops *
2278ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2279{
2280 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002281 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002282
2283 do_for_each_ftrace_op(op, ftrace_ops_list) {
2284 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002285 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002286 return op;
2287 } while_for_each_ftrace_op(op);
2288
2289 return NULL;
2290}
2291
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002292/**
2293 * ftrace_get_addr_new - Get the call address to set to
2294 * @rec: The ftrace record descriptor
2295 *
2296 * If the record has the FTRACE_FL_REGS set, that means that it
2297 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2298 * is not not set, then it wants to convert to the normal callback.
2299 *
2300 * Returns the address of the trampoline to set to
2301 */
2302unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2303{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002304 struct ftrace_ops *ops;
2305
2306 /* Trampolines take precedence over regs */
2307 if (rec->flags & FTRACE_FL_TRAMP) {
2308 ops = ftrace_find_tramp_ops_new(rec);
2309 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002310 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2311 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002312 /* Ftrace is shutting down, return anything */
2313 return (unsigned long)FTRACE_ADDR;
2314 }
2315 return ops->trampoline;
2316 }
2317
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002318 if (rec->flags & FTRACE_FL_REGS)
2319 return (unsigned long)FTRACE_REGS_ADDR;
2320 else
2321 return (unsigned long)FTRACE_ADDR;
2322}
2323
2324/**
2325 * ftrace_get_addr_curr - Get the call address that is already there
2326 * @rec: The ftrace record descriptor
2327 *
2328 * The FTRACE_FL_REGS_EN is set when the record already points to
2329 * a function that saves all the regs. Basically the '_EN' version
2330 * represents the current state of the function.
2331 *
2332 * Returns the address of the trampoline that is currently being called
2333 */
2334unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2335{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002336 struct ftrace_ops *ops;
2337
2338 /* Trampolines take precedence over regs */
2339 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2340 ops = ftrace_find_tramp_ops_curr(rec);
2341 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002342 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2343 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002344 /* Ftrace is shutting down, return anything */
2345 return (unsigned long)FTRACE_ADDR;
2346 }
2347 return ops->trampoline;
2348 }
2349
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002350 if (rec->flags & FTRACE_FL_REGS_EN)
2351 return (unsigned long)FTRACE_REGS_ADDR;
2352 else
2353 return (unsigned long)FTRACE_ADDR;
2354}
2355
Steven Rostedtc88fd862011-08-16 09:53:39 -04002356static int
2357__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2358{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002359 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002360 unsigned long ftrace_addr;
2361 int ret;
2362
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002363 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002364
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002365 /* This needs to be done before we call ftrace_update_record */
2366 ftrace_old_addr = ftrace_get_addr_curr(rec);
2367
2368 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002369
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002370 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2371
Steven Rostedtc88fd862011-08-16 09:53:39 -04002372 switch (ret) {
2373 case FTRACE_UPDATE_IGNORE:
2374 return 0;
2375
2376 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002377 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002378 return ftrace_make_call(rec, ftrace_addr);
2379
2380 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002381 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002382 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002383
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002384 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002385 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002386 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002387 }
2388
2389 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002390}
2391
Steven Rostedte4f5d542012-04-27 09:13:18 -04002392void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002393{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002394 struct dyn_ftrace *rec;
2395 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002396 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002397
Steven Rostedt45a4a232011-04-21 23:16:46 -04002398 if (unlikely(ftrace_disabled))
2399 return;
2400
Steven Rostedt265c8312009-02-13 12:43:56 -05002401 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002402
2403 if (rec->flags & FTRACE_FL_DISABLED)
2404 continue;
2405
Steven Rostedte4f5d542012-04-27 09:13:18 -04002406 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002407 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002408 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002409 /* Stop processing */
2410 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002411 }
2412 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002413}
2414
Steven Rostedtc88fd862011-08-16 09:53:39 -04002415struct ftrace_rec_iter {
2416 struct ftrace_page *pg;
2417 int index;
2418};
2419
2420/**
2421 * ftrace_rec_iter_start, start up iterating over traced functions
2422 *
2423 * Returns an iterator handle that is used to iterate over all
2424 * the records that represent address locations where functions
2425 * are traced.
2426 *
2427 * May return NULL if no records are available.
2428 */
2429struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2430{
2431 /*
2432 * We only use a single iterator.
2433 * Protected by the ftrace_lock mutex.
2434 */
2435 static struct ftrace_rec_iter ftrace_rec_iter;
2436 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2437
2438 iter->pg = ftrace_pages_start;
2439 iter->index = 0;
2440
2441 /* Could have empty pages */
2442 while (iter->pg && !iter->pg->index)
2443 iter->pg = iter->pg->next;
2444
2445 if (!iter->pg)
2446 return NULL;
2447
2448 return iter;
2449}
2450
2451/**
2452 * ftrace_rec_iter_next, get the next record to process.
2453 * @iter: The handle to the iterator.
2454 *
2455 * Returns the next iterator after the given iterator @iter.
2456 */
2457struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2458{
2459 iter->index++;
2460
2461 if (iter->index >= iter->pg->index) {
2462 iter->pg = iter->pg->next;
2463 iter->index = 0;
2464
2465 /* Could have empty pages */
2466 while (iter->pg && !iter->pg->index)
2467 iter->pg = iter->pg->next;
2468 }
2469
2470 if (!iter->pg)
2471 return NULL;
2472
2473 return iter;
2474}
2475
2476/**
2477 * ftrace_rec_iter_record, get the record at the iterator location
2478 * @iter: The current iterator location
2479 *
2480 * Returns the record that the current @iter is at.
2481 */
2482struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2483{
2484 return &iter->pg->records[iter->index];
2485}
2486
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302487static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002488ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002489{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002490 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002491
Steven Rostedt45a4a232011-04-21 23:16:46 -04002492 if (unlikely(ftrace_disabled))
2493 return 0;
2494
Shaohua Li25aac9d2009-01-09 11:29:40 +08002495 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002496 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002497 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002498 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302499 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002500 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302501 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002502}
2503
Steven Rostedt000ab692009-02-17 13:35:06 -05002504/*
2505 * archs can override this function if they must do something
2506 * before the modifying code is performed.
2507 */
2508int __weak ftrace_arch_code_modify_prepare(void)
2509{
2510 return 0;
2511}
2512
2513/*
2514 * archs can override this function if they must do something
2515 * after the modifying code is performed.
2516 */
2517int __weak ftrace_arch_code_modify_post_process(void)
2518{
2519 return 0;
2520}
2521
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002522void ftrace_modify_all_code(int command)
2523{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002524 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002525 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002526
2527 /*
2528 * If the ftrace_caller calls a ftrace_ops func directly,
2529 * we need to make sure that it only traces functions it
2530 * expects to trace. When doing the switch of functions,
2531 * we need to update to the ftrace_ops_list_func first
2532 * before the transition between old and new calls are set,
2533 * as the ftrace_ops_list_func will check the ops hashes
2534 * to make sure the ops are having the right functions
2535 * traced.
2536 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002537 if (update) {
2538 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2539 if (FTRACE_WARN_ON(err))
2540 return;
2541 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002542
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002543 if (command & FTRACE_UPDATE_CALLS)
2544 ftrace_replace_code(1);
2545 else if (command & FTRACE_DISABLE_CALLS)
2546 ftrace_replace_code(0);
2547
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002548 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2549 function_trace_op = set_function_trace_op;
2550 smp_wmb();
2551 /* If irqs are disabled, we are in stop machine */
2552 if (!irqs_disabled())
2553 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002554 err = ftrace_update_ftrace_func(ftrace_trace_function);
2555 if (FTRACE_WARN_ON(err))
2556 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002557 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002558
2559 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002560 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002561 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002562 err = ftrace_disable_ftrace_graph_caller();
2563 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002564}
2565
Ingo Molnare309b412008-05-12 21:20:51 +02002566static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002567{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002568 int *command = data;
2569
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002570 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002571
Steven Rostedtc88fd862011-08-16 09:53:39 -04002572 return 0;
2573}
2574
2575/**
2576 * ftrace_run_stop_machine, go back to the stop machine method
2577 * @command: The command to tell ftrace what to do
2578 *
2579 * If an arch needs to fall back to the stop machine method, the
2580 * it can call this function.
2581 */
2582void ftrace_run_stop_machine(int command)
2583{
2584 stop_machine(__ftrace_modify_code, &command, NULL);
2585}
2586
2587/**
2588 * arch_ftrace_update_code, modify the code to trace or not trace
2589 * @command: The command that needs to be done
2590 *
2591 * Archs can override this function if it does not need to
2592 * run stop_machine() to modify code.
2593 */
2594void __weak arch_ftrace_update_code(int command)
2595{
2596 ftrace_run_stop_machine(command);
2597}
2598
2599static void ftrace_run_update_code(int command)
2600{
2601 int ret;
2602
2603 ret = ftrace_arch_code_modify_prepare();
2604 FTRACE_WARN_ON(ret);
2605 if (ret)
2606 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002607
2608 /*
2609 * By default we use stop_machine() to modify the code.
2610 * But archs can do what ever they want as long as it
2611 * is safe. The stop_machine() is the safest, but also
2612 * produces the most overhead.
2613 */
2614 arch_ftrace_update_code(command);
2615
Steven Rostedt000ab692009-02-17 13:35:06 -05002616 ret = ftrace_arch_code_modify_post_process();
2617 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002618}
2619
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002620static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002621 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002622{
2623 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002624 ops->old_hash.filter_hash = old_hash->filter_hash;
2625 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002626 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002627 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002628 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002629 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2630}
2631
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002632static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002633static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002634
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002635void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2636{
2637}
2638
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002639static void ftrace_startup_enable(int command)
2640{
2641 if (saved_ftrace_func != ftrace_trace_function) {
2642 saved_ftrace_func = ftrace_trace_function;
2643 command |= FTRACE_UPDATE_TRACE_FUNC;
2644 }
2645
2646 if (!command || !ftrace_enabled)
2647 return;
2648
2649 ftrace_run_update_code(command);
2650}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002651
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002652static void ftrace_startup_all(int command)
2653{
2654 update_all_ops = true;
2655 ftrace_startup_enable(command);
2656 update_all_ops = false;
2657}
2658
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002659int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002660{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002661 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002662
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002663 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002664 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002665
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002666 ret = __register_ftrace_function(ops);
2667 if (ret)
2668 return ret;
2669
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002670 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002671
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002672 /*
2673 * Note that ftrace probes uses this to start up
2674 * and modify functions it will probe. But we still
2675 * set the ADDING flag for modification, as probes
2676 * do not have trampolines. If they add them in the
2677 * future, then the probes will need to distinguish
2678 * between adding and updating probes.
2679 */
2680 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002681
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002682 ret = ftrace_hash_ipmodify_enable(ops);
2683 if (ret < 0) {
2684 /* Rollback registration process */
2685 __unregister_ftrace_function(ops);
2686 ftrace_start_up--;
2687 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2688 return ret;
2689 }
2690
Jiri Olsa7f50d062016-03-16 15:34:33 +01002691 if (ftrace_hash_rec_enable(ops, 1))
2692 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002693
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002694 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002695
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002696 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2697
Steven Rostedta1cd6172011-05-23 15:24:25 -04002698 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002699}
2700
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002701int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002702{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002703 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002704
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002705 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002706 return -ENODEV;
2707
2708 ret = __unregister_ftrace_function(ops);
2709 if (ret)
2710 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002711
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002712 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002713 /*
2714 * Just warn in case of unbalance, no need to kill ftrace, it's not
2715 * critical but the ftrace_call callers may be never nopped again after
2716 * further ftrace uses.
2717 */
2718 WARN_ON_ONCE(ftrace_start_up < 0);
2719
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002720 /* Disabling ipmodify never fails */
2721 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002722
2723 if (ftrace_hash_rec_disable(ops, 1))
2724 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002725
Namhyung Kima737e6d2014-06-12 23:56:12 +09002726 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002727
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002728 if (saved_ftrace_func != ftrace_trace_function) {
2729 saved_ftrace_func = ftrace_trace_function;
2730 command |= FTRACE_UPDATE_TRACE_FUNC;
2731 }
2732
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002733 if (!command || !ftrace_enabled) {
2734 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002735 * If these are dynamic or per_cpu ops, they still
2736 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002737 * not currently active, we can just free them
2738 * without synchronizing all CPUs.
2739 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002740 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002741 goto free_ops;
2742
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002743 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002744 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002745
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002746 /*
2747 * If the ops uses a trampoline, then it needs to be
2748 * tested first on update.
2749 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002750 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002751 removed_ops = ops;
2752
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002753 /* The trampoline logic checks the old hashes */
2754 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2755 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2756
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002757 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002758
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002759 /*
2760 * If there's no more ops registered with ftrace, run a
2761 * sanity check to make sure all rec flags are cleared.
2762 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002763 if (rcu_dereference_protected(ftrace_ops_list,
2764 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002765 struct ftrace_page *pg;
2766 struct dyn_ftrace *rec;
2767
2768 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002769 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002770 pr_warn(" %pS flags:%lx\n",
2771 (void *)rec->ip, rec->flags);
2772 } while_for_each_ftrace_rec();
2773 }
2774
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002775 ops->old_hash.filter_hash = NULL;
2776 ops->old_hash.notrace_hash = NULL;
2777
2778 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002779 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002780
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002781 /*
2782 * Dynamic ops may be freed, we must make sure that all
2783 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002784 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002785 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002786 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002787 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002788 /*
2789 * We need to do a hard force of sched synchronization.
2790 * This is because we use preempt_disable() to do RCU, but
2791 * the function tracers can be called where RCU is not watching
2792 * (like before user_exit()). We can not rely on the RCU
2793 * infrastructure to do the synchronization, thus we must do it
2794 * ourselves.
2795 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002796 schedule_on_each_cpu(ftrace_sync);
2797
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002798 /*
2799 * When the kernel is preeptive, tasks can be preempted
2800 * while on a ftrace trampoline. Just scheduling a task on
2801 * a CPU is not good enough to flush them. Calling
2802 * synchornize_rcu_tasks() will wait for those tasks to
2803 * execute and either schedule voluntarily or enter user space.
2804 */
2805 if (IS_ENABLED(CONFIG_PREEMPT))
2806 synchronize_rcu_tasks();
2807
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002808 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002809 arch_ftrace_trampoline_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002810 }
2811
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002812 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002813}
2814
Ingo Molnare309b412008-05-12 21:20:51 +02002815static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002816{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302817 int command;
2818
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002819 if (unlikely(ftrace_disabled))
2820 return;
2821
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002822 /* Force update next time */
2823 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002824 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302825 if (ftrace_start_up) {
2826 command = FTRACE_UPDATE_CALLS;
2827 if (ftrace_graph_active)
2828 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002829 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302830 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002831}
2832
Ingo Molnare309b412008-05-12 21:20:51 +02002833static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002834{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302835 int command;
2836
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002837 if (unlikely(ftrace_disabled))
2838 return;
2839
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002840 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302841 if (ftrace_start_up) {
2842 command = FTRACE_DISABLE_CALLS;
2843 if (ftrace_graph_active)
2844 command |= FTRACE_STOP_FUNC_RET;
2845 ftrace_run_update_code(command);
2846 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002847}
2848
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002849static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002850unsigned long ftrace_update_tot_cnt;
2851
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002852static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002853{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002854 /*
2855 * Filter_hash being empty will default to trace module.
2856 * But notrace hash requires a test of individual module functions.
2857 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002858 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2859 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002860}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002861
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002862/*
2863 * Check if the current ops references the record.
2864 *
2865 * If the ops traces all functions, then it was already accounted for.
2866 * If the ops does not trace the current record function, skip it.
2867 * If the ops ignores the function via notrace filter, skip it.
2868 */
2869static inline bool
2870ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2871{
2872 /* If ops isn't enabled, ignore it */
2873 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002874 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002875
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002876 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002877 if (ops_traces_mod(ops))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002878 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002879
2880 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002881 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002882 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002883 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002884
2885 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002886 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002887 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002888
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002889 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002890}
2891
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002892static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002893{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002894 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002895 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002896 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002897 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002898 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002899 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002900
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002901 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002902
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002903 /*
2904 * When a module is loaded, this function is called to convert
2905 * the calls to mcount in its text to nops, and also to create
2906 * an entry in the ftrace data. Now, if ftrace is activated
2907 * after this call, but before the module sets its text to
2908 * read-only, the modification of enabling ftrace can fail if
2909 * the read-only is done while ftrace is converting the calls.
2910 * To prevent this, the module's records are set as disabled
2911 * and will be enabled after the call to set the module's text
2912 * to read-only.
2913 */
2914 if (mod)
2915 rec_flags |= FTRACE_FL_DISABLED;
2916
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002917 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302918
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002919 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002920
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002921 /* If something went wrong, bail without enabling anything */
2922 if (unlikely(ftrace_disabled))
2923 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002924
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002925 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002926 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302927
Vasily Gorbik2f4df002018-08-06 15:17:46 +02002928#ifndef CC_USING_NOP_MCOUNT
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002929 /*
2930 * Do the initial record conversion from mcount jump
2931 * to the NOP instructions.
2932 */
2933 if (!ftrace_code_disable(mod, p))
2934 break;
Vasily Gorbik2f4df002018-08-06 15:17:46 +02002935#endif
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002936
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002937 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002938 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002939 }
2940
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002941 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002942 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002943 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002944
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002945 return 0;
2946}
2947
Steven Rostedta7900872011-12-16 16:23:44 -05002948static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002949{
Steven Rostedta7900872011-12-16 16:23:44 -05002950 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002951 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002952
Steven Rostedta7900872011-12-16 16:23:44 -05002953 if (WARN_ON(!count))
2954 return -EINVAL;
2955
2956 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002957
2958 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002959 * We want to fill as much as possible. No more than a page
2960 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002961 */
Steven Rostedta7900872011-12-16 16:23:44 -05002962 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2963 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002964
Steven Rostedta7900872011-12-16 16:23:44 -05002965 again:
2966 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2967
2968 if (!pg->records) {
2969 /* if we can't allocate this size, try something smaller */
2970 if (!order)
2971 return -ENOMEM;
2972 order >>= 1;
2973 goto again;
2974 }
2975
2976 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2977 pg->size = cnt;
2978
2979 if (cnt > count)
2980 cnt = count;
2981
2982 return cnt;
2983}
2984
2985static struct ftrace_page *
2986ftrace_allocate_pages(unsigned long num_to_init)
2987{
2988 struct ftrace_page *start_pg;
2989 struct ftrace_page *pg;
2990 int order;
2991 int cnt;
2992
2993 if (!num_to_init)
2994 return 0;
2995
2996 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2997 if (!pg)
2998 return NULL;
2999
3000 /*
3001 * Try to allocate as much as possible in one continues
3002 * location that fills in all of the space. We want to
3003 * waste as little space as possible.
3004 */
3005 for (;;) {
3006 cnt = ftrace_allocate_records(pg, num_to_init);
3007 if (cnt < 0)
3008 goto free_pages;
3009
3010 num_to_init -= cnt;
3011 if (!num_to_init)
3012 break;
3013
3014 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3015 if (!pg->next)
3016 goto free_pages;
3017
3018 pg = pg->next;
3019 }
3020
3021 return start_pg;
3022
3023 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003024 pg = start_pg;
3025 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003026 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3027 free_pages((unsigned long)pg->records, order);
3028 start_pg = pg->next;
3029 kfree(pg);
3030 pg = start_pg;
3031 }
3032 pr_info("ftrace: FAILED to allocate memory for functions\n");
3033 return NULL;
3034}
3035
Steven Rostedt5072c592008-05-12 21:20:43 +02003036#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3037
3038struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003039 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003040 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003041 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003042 struct ftrace_page *pg;
3043 struct dyn_ftrace *func;
3044 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003045 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003046 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003047 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003048 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003049 struct trace_array *tr;
3050 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003051 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003052 int idx;
3053 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003054};
3055
Ingo Molnare309b412008-05-12 21:20:51 +02003056static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003057t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003058{
3059 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003060 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003061 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003062 struct ftrace_hash *hash;
3063 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003064 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003065 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003066 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003067
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003068 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003069 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003070
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003071 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003072 return NULL;
3073
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003074 func_probes = &tr->func_probes;
3075 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003076 return NULL;
3077
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003078 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003079 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003080 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003081 }
3082
3083 if (iter->probe_entry)
3084 hnd = &iter->probe_entry->hlist;
3085
3086 hash = iter->probe->ops.func_hash->filter_hash;
3087 size = 1 << hash->size_bits;
3088
3089 retry:
3090 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003091 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003092 return NULL;
3093 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003094 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003095 hash = iter->probe->ops.func_hash->filter_hash;
3096 size = 1 << hash->size_bits;
3097 iter->pidx = 0;
3098 }
3099
3100 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003101
3102 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003103 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003104 hnd = NULL;
3105 goto retry;
3106 }
3107
3108 if (!hnd)
3109 hnd = hhd->first;
3110 else {
3111 hnd = hnd->next;
3112 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003113 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003114 goto retry;
3115 }
3116 }
3117
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003118 if (WARN_ON_ONCE(!hnd))
3119 return NULL;
3120
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003121 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003122
3123 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003124}
3125
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003126static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003127{
3128 struct ftrace_iterator *iter = m->private;
3129 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003130 loff_t l;
3131
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003132 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003133 return NULL;
3134
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003135 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003136 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003137
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003138 iter->probe = NULL;
3139 iter->probe_entry = NULL;
3140 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003141 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003142 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003143 if (!p)
3144 break;
3145 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003146 if (!p)
3147 return NULL;
3148
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003149 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003150 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003151
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003152 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003153}
3154
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003155static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003156t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003157{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003158 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003159 struct ftrace_probe_ops *probe_ops;
3160 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003161
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003162 probe = iter->probe;
3163 probe_entry = iter->probe_entry;
3164
3165 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003166 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003167
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003168 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003169
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003170 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003171 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003172
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003173 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3174 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003175
3176 return 0;
3177}
3178
3179static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003180t_mod_next(struct seq_file *m, loff_t *pos)
3181{
3182 struct ftrace_iterator *iter = m->private;
3183 struct trace_array *tr = iter->tr;
3184
3185 (*pos)++;
3186 iter->pos = *pos;
3187
3188 iter->mod_list = iter->mod_list->next;
3189
3190 if (iter->mod_list == &tr->mod_trace ||
3191 iter->mod_list == &tr->mod_notrace) {
3192 iter->flags &= ~FTRACE_ITER_MOD;
3193 return NULL;
3194 }
3195
3196 iter->mod_pos = *pos;
3197
3198 return iter;
3199}
3200
3201static void *t_mod_start(struct seq_file *m, loff_t *pos)
3202{
3203 struct ftrace_iterator *iter = m->private;
3204 void *p = NULL;
3205 loff_t l;
3206
3207 if (iter->func_pos > *pos)
3208 return NULL;
3209
3210 iter->mod_pos = iter->func_pos;
3211
3212 /* probes are only available if tr is set */
3213 if (!iter->tr)
3214 return NULL;
3215
3216 for (l = 0; l <= (*pos - iter->func_pos); ) {
3217 p = t_mod_next(m, &l);
3218 if (!p)
3219 break;
3220 }
3221 if (!p) {
3222 iter->flags &= ~FTRACE_ITER_MOD;
3223 return t_probe_start(m, pos);
3224 }
3225
3226 /* Only set this if we have an item */
3227 iter->flags |= FTRACE_ITER_MOD;
3228
3229 return iter;
3230}
3231
3232static int
3233t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3234{
3235 struct ftrace_mod_load *ftrace_mod;
3236 struct trace_array *tr = iter->tr;
3237
3238 if (WARN_ON_ONCE(!iter->mod_list) ||
3239 iter->mod_list == &tr->mod_trace ||
3240 iter->mod_list == &tr->mod_notrace)
3241 return -EIO;
3242
3243 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3244
3245 if (ftrace_mod->func)
3246 seq_printf(m, "%s", ftrace_mod->func);
3247 else
3248 seq_putc(m, '*');
3249
3250 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3251
3252 return 0;
3253}
3254
3255static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003256t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003257{
3258 struct ftrace_iterator *iter = m->private;
3259 struct dyn_ftrace *rec = NULL;
3260
3261 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003262
Steven Rostedt5072c592008-05-12 21:20:43 +02003263 retry:
3264 if (iter->idx >= iter->pg->index) {
3265 if (iter->pg->next) {
3266 iter->pg = iter->pg->next;
3267 iter->idx = 0;
3268 goto retry;
3269 }
3270 } else {
3271 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003272 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3273 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003274
3275 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003276 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003277
Steven Rostedt5072c592008-05-12 21:20:43 +02003278 rec = NULL;
3279 goto retry;
3280 }
3281 }
3282
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003283 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003284 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003285
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003286 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003287 iter->func = rec;
3288
3289 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003290}
3291
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003292static void *
3293t_next(struct seq_file *m, void *v, loff_t *pos)
3294{
3295 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003296 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003297 void *ret;
3298
3299 if (unlikely(ftrace_disabled))
3300 return NULL;
3301
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003302 if (iter->flags & FTRACE_ITER_PROBE)
3303 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003304
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003305 if (iter->flags & FTRACE_ITER_MOD)
3306 return t_mod_next(m, pos);
3307
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003308 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003309 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003310 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003311 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003312 }
3313
3314 ret = t_func_next(m, pos);
3315
3316 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003317 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003318
3319 return ret;
3320}
3321
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003322static void reset_iter_read(struct ftrace_iterator *iter)
3323{
3324 iter->pos = 0;
3325 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003326 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003327}
3328
3329static void *t_start(struct seq_file *m, loff_t *pos)
3330{
3331 struct ftrace_iterator *iter = m->private;
3332 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003333 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003334
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003335 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003336
3337 if (unlikely(ftrace_disabled))
3338 return NULL;
3339
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003340 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003341 * If an lseek was done, then reset and start from beginning.
3342 */
3343 if (*pos < iter->pos)
3344 reset_iter_read(iter);
3345
3346 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003347 * For set_ftrace_filter reading, if we have the filter
3348 * off, we can short cut and just print out that all
3349 * functions are enabled.
3350 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003351 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3352 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003353 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003354 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003355 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003356 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003357 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003358 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003359 return iter;
3360 }
3361
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003362 if (iter->flags & FTRACE_ITER_MOD)
3363 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003364
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003365 /*
3366 * Unfortunately, we need to restart at ftrace_pages_start
3367 * every time we let go of the ftrace_mutex. This is because
3368 * those pointers can change without the lock.
3369 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003370 iter->pg = ftrace_pages_start;
3371 iter->idx = 0;
3372 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003373 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003374 if (!p)
3375 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003376 }
walimis5821e1b2008-11-15 15:19:06 +08003377
Steven Rostedt69a30832011-12-19 15:21:16 -05003378 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003379 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003380
3381 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003382}
3383
3384static void t_stop(struct seq_file *m, void *p)
3385{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003386 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003387}
3388
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003389void * __weak
3390arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3391{
3392 return NULL;
3393}
3394
3395static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3396 struct dyn_ftrace *rec)
3397{
3398 void *ptr;
3399
3400 ptr = arch_ftrace_trampoline_func(ops, rec);
3401 if (ptr)
3402 seq_printf(m, " ->%pS", ptr);
3403}
3404
Steven Rostedt5072c592008-05-12 21:20:43 +02003405static int t_show(struct seq_file *m, void *v)
3406{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003407 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003408 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003409
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003410 if (iter->flags & FTRACE_ITER_PROBE)
3411 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003412
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003413 if (iter->flags & FTRACE_ITER_MOD)
3414 return t_mod_show(m, iter);
3415
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003416 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003417 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003418 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003419 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003420 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003421 return 0;
3422 }
3423
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003424 rec = iter->func;
3425
Steven Rostedt5072c592008-05-12 21:20:43 +02003426 if (!rec)
3427 return 0;
3428
Steven Rostedt647bcd02011-05-03 14:39:21 -04003429 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003430 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003431 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003432
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003433 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003434 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003435 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3436 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003437 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003438 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003439 if (ops) {
3440 do {
3441 seq_printf(m, "\ttramp: %pS (%pS)",
3442 (void *)ops->trampoline,
3443 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003444 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003445 ops = ftrace_find_tramp_ops_next(rec, ops);
3446 } while (ops);
3447 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003448 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003449 } else {
3450 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003451 }
3452 }
3453
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003454 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003455
3456 return 0;
3457}
3458
James Morris88e9d342009-09-22 16:43:43 -07003459static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003460 .start = t_start,
3461 .next = t_next,
3462 .stop = t_stop,
3463 .show = t_show,
3464};
3465
Ingo Molnare309b412008-05-12 21:20:51 +02003466static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003467ftrace_avail_open(struct inode *inode, struct file *file)
3468{
3469 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003470
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003471 if (unlikely(ftrace_disabled))
3472 return -ENODEV;
3473
Jiri Olsa50e18b92012-04-25 10:23:39 +02003474 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003475 if (!iter)
3476 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003477
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003478 iter->pg = ftrace_pages_start;
3479 iter->ops = &global_ops;
3480
3481 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003482}
3483
Steven Rostedt647bcd02011-05-03 14:39:21 -04003484static int
3485ftrace_enabled_open(struct inode *inode, struct file *file)
3486{
3487 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003488
Jiri Olsa50e18b92012-04-25 10:23:39 +02003489 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003490 if (!iter)
3491 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003492
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003493 iter->pg = ftrace_pages_start;
3494 iter->flags = FTRACE_ITER_ENABLED;
3495 iter->ops = &global_ops;
3496
3497 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003498}
3499
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003500/**
3501 * ftrace_regex_open - initialize function tracer filter files
3502 * @ops: The ftrace_ops that hold the hash filters
3503 * @flag: The type of filter to process
3504 * @inode: The inode, usually passed in to your open routine
3505 * @file: The file, usually passed in to your open routine
3506 *
3507 * ftrace_regex_open() initializes the filter files for the
3508 * @ops. Depending on @flag it may process the filter hash or
3509 * the notrace hash of @ops. With this called from the open
3510 * routine, you can use ftrace_filter_write() for the write
3511 * routine if @flag has FTRACE_ITER_FILTER set, or
3512 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003513 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003514 * release must call ftrace_regex_release().
3515 */
3516int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003517ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003518 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003519{
3520 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003521 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003522 struct list_head *mod_head;
3523 struct trace_array *tr = ops->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003524 int ret = 0;
3525
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003526 ftrace_ops_init(ops);
3527
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003528 if (unlikely(ftrace_disabled))
3529 return -ENODEV;
3530
Steven Rostedt5072c592008-05-12 21:20:43 +02003531 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3532 if (!iter)
3533 return -ENOMEM;
3534
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003535 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3536 kfree(iter);
3537 return -ENOMEM;
3538 }
3539
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003540 iter->ops = ops;
3541 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003542 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003543
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003544 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003545
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003546 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003547 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003548 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003549 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003550 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003551 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003552 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003553
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003554 iter->mod_list = mod_head;
3555
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003556 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003557 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3558
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003559 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003560 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003561 clear_ftrace_mod_list(mod_head);
3562 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003563 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003564 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003565
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003566 if (!iter->hash) {
3567 trace_parser_put(&iter->parser);
3568 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003569 ret = -ENOMEM;
3570 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003571 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003572 } else
3573 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003574
Steven Rostedt5072c592008-05-12 21:20:43 +02003575 if (file->f_mode & FMODE_READ) {
3576 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003577
3578 ret = seq_open(file, &show_ftrace_seq_ops);
3579 if (!ret) {
3580 struct seq_file *m = file->private_data;
3581 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003582 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003583 /* Failed */
3584 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003585 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003586 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003587 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003588 } else
3589 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003590
3591 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003592 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003593
3594 return ret;
3595}
3596
Steven Rostedt41c52c02008-05-22 11:46:33 -04003597static int
3598ftrace_filter_open(struct inode *inode, struct file *file)
3599{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003600 struct ftrace_ops *ops = inode->i_private;
3601
3602 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003603 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003604 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003605}
3606
3607static int
3608ftrace_notrace_open(struct inode *inode, struct file *file)
3609{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003610 struct ftrace_ops *ops = inode->i_private;
3611
3612 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003613 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003614}
3615
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003616/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3617struct ftrace_glob {
3618 char *search;
3619 unsigned len;
3620 int type;
3621};
3622
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003623/*
3624 * If symbols in an architecture don't correspond exactly to the user-visible
3625 * name of what they represent, it is possible to define this function to
3626 * perform the necessary adjustments.
3627*/
3628char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3629{
3630 return str;
3631}
3632
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003633static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003634{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003635 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003636 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003637
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003638 str = arch_ftrace_match_adjust(str, g->search);
3639
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003640 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003641 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003642 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003643 matched = 1;
3644 break;
3645 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003646 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003647 matched = 1;
3648 break;
3649 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003650 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003651 matched = 1;
3652 break;
3653 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003654 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003655 if (slen >= g->len &&
3656 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003657 matched = 1;
3658 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003659 case MATCH_GLOB:
3660 if (glob_match(g->search, str))
3661 matched = 1;
3662 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003663 }
3664
3665 return matched;
3666}
3667
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003668static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003669enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003670{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003671 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003672 int ret = 0;
3673
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003674 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003675 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003676 /* Do nothing if it doesn't exist */
3677 if (!entry)
3678 return 0;
3679
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003680 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003681 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003682 /* Do nothing if it exists */
3683 if (entry)
3684 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003685
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003686 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003687 }
3688 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003689}
3690
Steven Rostedt64e7c442009-02-13 17:08:48 -05003691static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003692ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3693 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003694{
3695 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003696 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003697
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003698 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3699
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003700 if (mod_g) {
3701 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3702
3703 /* blank module name to match all modules */
3704 if (!mod_g->len) {
3705 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003706 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003707 goto func_match;
3708 return 0;
3709 }
3710
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003711 /*
3712 * exclude_mod is set to trace everything but the given
3713 * module. If it is set and the module matches, then
3714 * return 0. If it is not set, and the module doesn't match
3715 * also return 0. Otherwise, check the function to see if
3716 * that matches.
3717 */
3718 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003719 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003720func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003721 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003722 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003723 return 1;
3724 }
3725
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003726 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003727}
3728
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003729static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003730match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003731{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003732 struct ftrace_page *pg;
3733 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003734 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003735 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3736 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3737 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003738 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003739 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03003740 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003741
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003742 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003743 func_g.type = filter_parse_regex(func, len, &func_g.search,
3744 &clear_filter);
3745 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003746 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003747
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003748 if (mod) {
3749 mod_g.type = filter_parse_regex(mod, strlen(mod),
3750 &mod_g.search, &exclude_mod);
3751 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003752 }
3753
Steven Rostedt52baf112009-02-14 01:15:39 -05003754 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003755
3756 if (unlikely(ftrace_disabled))
3757 goto out_unlock;
3758
Steven Rostedt265c8312009-02-13 12:43:56 -05003759 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003760
3761 if (rec->flags & FTRACE_FL_DISABLED)
3762 continue;
3763
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003764 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003765 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003766 if (ret < 0) {
3767 found = ret;
3768 goto out_unlock;
3769 }
Li Zefan311d16d2009-12-08 11:15:11 +08003770 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003771 }
3772 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003773 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003774 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003775
3776 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003777}
3778
Steven Rostedt64e7c442009-02-13 17:08:48 -05003779static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003780ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003781{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003782 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003783}
3784
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003785static void ftrace_ops_update_code(struct ftrace_ops *ops,
3786 struct ftrace_ops_hash *old_hash)
3787{
3788 struct ftrace_ops *op;
3789
3790 if (!ftrace_enabled)
3791 return;
3792
3793 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3794 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3795 return;
3796 }
3797
3798 /*
3799 * If this is the shared global_ops filter, then we need to
3800 * check if there is another ops that shares it, is enabled.
3801 * If so, we still need to run the modify code.
3802 */
3803 if (ops->func_hash != &global_ops.local_hash)
3804 return;
3805
3806 do_for_each_ftrace_op(op, ftrace_ops_list) {
3807 if (op->func_hash == &global_ops.local_hash &&
3808 op->flags & FTRACE_OPS_FL_ENABLED) {
3809 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3810 /* Only need to do this once */
3811 return;
3812 }
3813 } while_for_each_ftrace_op(op);
3814}
3815
3816static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3817 struct ftrace_hash **orig_hash,
3818 struct ftrace_hash *hash,
3819 int enable)
3820{
3821 struct ftrace_ops_hash old_hash_ops;
3822 struct ftrace_hash *old_hash;
3823 int ret;
3824
3825 old_hash = *orig_hash;
3826 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3827 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3828 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3829 if (!ret) {
3830 ftrace_ops_update_code(ops, &old_hash_ops);
3831 free_ftrace_hash_rcu(old_hash);
3832 }
3833 return ret;
3834}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003835
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003836static bool module_exists(const char *module)
3837{
3838 /* All modules have the symbol __this_module */
3839 const char this_mod[] = "__this_module";
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003840 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003841 unsigned long val;
3842 int n;
3843
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003844 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003845
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003846 if (n > sizeof(modname) - 1)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003847 return false;
3848
3849 val = module_kallsyms_lookup_name(modname);
3850 return val != 0;
3851}
3852
3853static int cache_mod(struct trace_array *tr,
3854 const char *func, char *module, int enable)
3855{
3856 struct ftrace_mod_load *ftrace_mod, *n;
3857 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3858 int ret;
3859
3860 mutex_lock(&ftrace_lock);
3861
3862 /* We do not cache inverse filters */
3863 if (func[0] == '!') {
3864 func++;
3865 ret = -EINVAL;
3866
3867 /* Look to remove this hash */
3868 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3869 if (strcmp(ftrace_mod->module, module) != 0)
3870 continue;
3871
3872 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03003873 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003874 (ftrace_mod->func &&
3875 strcmp(ftrace_mod->func, func) == 0)) {
3876 ret = 0;
3877 free_ftrace_mod(ftrace_mod);
3878 continue;
3879 }
3880 }
3881 goto out;
3882 }
3883
3884 ret = -EINVAL;
3885 /* We only care about modules that have not been loaded yet */
3886 if (module_exists(module))
3887 goto out;
3888
3889 /* Save this string off, and execute it when the module is loaded */
3890 ret = ftrace_add_mod(tr, func, module, enable);
3891 out:
3892 mutex_unlock(&ftrace_lock);
3893
3894 return ret;
3895}
3896
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003897static int
3898ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3899 int reset, int enable);
3900
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003901#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003902static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3903 char *mod, bool enable)
3904{
3905 struct ftrace_mod_load *ftrace_mod, *n;
3906 struct ftrace_hash **orig_hash, *new_hash;
3907 LIST_HEAD(process_mods);
3908 char *func;
3909 int ret;
3910
3911 mutex_lock(&ops->func_hash->regex_lock);
3912
3913 if (enable)
3914 orig_hash = &ops->func_hash->filter_hash;
3915 else
3916 orig_hash = &ops->func_hash->notrace_hash;
3917
3918 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3919 *orig_hash);
3920 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04003921 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003922
3923 mutex_lock(&ftrace_lock);
3924
3925 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3926
3927 if (strcmp(ftrace_mod->module, mod) != 0)
3928 continue;
3929
3930 if (ftrace_mod->func)
3931 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
3932 else
3933 func = kstrdup("*", GFP_KERNEL);
3934
3935 if (!func) /* warn? */
3936 continue;
3937
3938 list_del(&ftrace_mod->list);
3939 list_add(&ftrace_mod->list, &process_mods);
3940
3941 /* Use the newly allocated func, as it may be "*" */
3942 kfree(ftrace_mod->func);
3943 ftrace_mod->func = func;
3944 }
3945
3946 mutex_unlock(&ftrace_lock);
3947
3948 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
3949
3950 func = ftrace_mod->func;
3951
3952 /* Grabs ftrace_lock, which is why we have this extra step */
3953 match_records(new_hash, func, strlen(func), mod);
3954 free_ftrace_mod(ftrace_mod);
3955 }
3956
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04003957 if (enable && list_empty(head))
3958 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
3959
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003960 mutex_lock(&ftrace_lock);
3961
3962 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
3963 new_hash, enable);
3964 mutex_unlock(&ftrace_lock);
3965
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04003966 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003967 mutex_unlock(&ops->func_hash->regex_lock);
3968
3969 free_ftrace_hash(new_hash);
3970}
3971
3972static void process_cached_mods(const char *mod_name)
3973{
3974 struct trace_array *tr;
3975 char *mod;
3976
3977 mod = kstrdup(mod_name, GFP_KERNEL);
3978 if (!mod)
3979 return;
3980
3981 mutex_lock(&trace_types_lock);
3982 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
3983 if (!list_empty(&tr->mod_trace))
3984 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
3985 if (!list_empty(&tr->mod_notrace))
3986 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
3987 }
3988 mutex_unlock(&trace_types_lock);
3989
3990 kfree(mod);
3991}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003992#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003993
Steven Rostedtf6180772009-02-14 00:40:25 -05003994/*
3995 * We register the module command as a template to show others how
3996 * to register the a command as well.
3997 */
3998
3999static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004000ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004001 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004002{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004003 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004004 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004005
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004006 /* match_records() modifies func, and we need the original */
4007 func = kstrdup(func_orig, GFP_KERNEL);
4008 if (!func)
4009 return -ENOMEM;
4010
Steven Rostedtf6180772009-02-14 00:40:25 -05004011 /*
4012 * cmd == 'mod' because we only registered this func
4013 * for the 'mod' ftrace_func_command.
4014 * But if you register one func with multiple commands,
4015 * you can tell which command was used by the cmd
4016 * parameter.
4017 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004018 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004019 kfree(func);
4020
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004021 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004022 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004023 if (ret < 0)
4024 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004025 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004026}
4027
4028static struct ftrace_func_command ftrace_mod_cmd = {
4029 .name = "mod",
4030 .func = ftrace_mod_callback,
4031};
4032
4033static int __init ftrace_mod_cmd_init(void)
4034{
4035 return register_ftrace_command(&ftrace_mod_cmd);
4036}
Steven Rostedt6f415672012-10-05 12:13:07 -04004037core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004038
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004039static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004040 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004041{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004042 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004043 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004044
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004045 probe = container_of(op, struct ftrace_func_probe, ops);
4046 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004047
4048 /*
4049 * Disable preemption for these calls to prevent a RCU grace
4050 * period. This syncs the hash iteration and freeing of items
4051 * on the hash. rcu_read_lock is too dangerous here.
4052 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004053 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004054 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004055 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004056}
4057
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004058struct ftrace_func_map {
4059 struct ftrace_func_entry entry;
4060 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004061};
4062
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004063struct ftrace_func_mapper {
4064 struct ftrace_hash hash;
4065};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004066
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004067/**
4068 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4069 *
4070 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4071 */
4072struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004073{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004074 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004075
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004076 /*
4077 * The mapper is simply a ftrace_hash, but since the entries
4078 * in the hash are not ftrace_func_entry type, we define it
4079 * as a separate structure.
4080 */
4081 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4082 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004083}
4084
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004085/**
4086 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4087 * @mapper: The mapper that has the ip maps
4088 * @ip: the instruction pointer to find the data for
4089 *
4090 * Returns the data mapped to @ip if found otherwise NULL. The return
4091 * is actually the address of the mapper data pointer. The address is
4092 * returned for use cases where the data is no bigger than a long, and
4093 * the user can use the data pointer as its data instead of having to
4094 * allocate more memory for the reference.
4095 */
4096void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4097 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004098{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004099 struct ftrace_func_entry *entry;
4100 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004101
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004102 entry = ftrace_lookup_ip(&mapper->hash, ip);
4103 if (!entry)
4104 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004105
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004106 map = (struct ftrace_func_map *)entry;
4107 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004108}
4109
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004110/**
4111 * ftrace_func_mapper_add_ip - Map some data to an ip
4112 * @mapper: The mapper that has the ip maps
4113 * @ip: The instruction pointer address to map @data to
4114 * @data: The data to map to @ip
4115 *
4116 * Returns 0 on succes otherwise an error.
4117 */
4118int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4119 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004120{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004121 struct ftrace_func_entry *entry;
4122 struct ftrace_func_map *map;
4123
4124 entry = ftrace_lookup_ip(&mapper->hash, ip);
4125 if (entry)
4126 return -EBUSY;
4127
4128 map = kmalloc(sizeof(*map), GFP_KERNEL);
4129 if (!map)
4130 return -ENOMEM;
4131
4132 map->entry.ip = ip;
4133 map->data = data;
4134
4135 __add_hash_entry(&mapper->hash, &map->entry);
4136
4137 return 0;
4138}
4139
4140/**
4141 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4142 * @mapper: The mapper that has the ip maps
4143 * @ip: The instruction pointer address to remove the data from
4144 *
4145 * Returns the data if it is found, otherwise NULL.
4146 * Note, if the data pointer is used as the data itself, (see
4147 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4148 * if the data pointer was set to zero.
4149 */
4150void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4151 unsigned long ip)
4152{
4153 struct ftrace_func_entry *entry;
4154 struct ftrace_func_map *map;
4155 void *data;
4156
4157 entry = ftrace_lookup_ip(&mapper->hash, ip);
4158 if (!entry)
4159 return NULL;
4160
4161 map = (struct ftrace_func_map *)entry;
4162 data = map->data;
4163
4164 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004165 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004166
4167 return data;
4168}
4169
4170/**
4171 * free_ftrace_func_mapper - free a mapping of ips and data
4172 * @mapper: The mapper that has the ip maps
4173 * @free_func: A function to be called on each data item.
4174 *
4175 * This is used to free the function mapper. The @free_func is optional
4176 * and can be used if the data needs to be freed as well.
4177 */
4178void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4179 ftrace_mapper_func free_func)
4180{
4181 struct ftrace_func_entry *entry;
4182 struct ftrace_func_map *map;
4183 struct hlist_head *hhd;
4184 int size = 1 << mapper->hash.size_bits;
4185 int i;
4186
4187 if (free_func && mapper->hash.count) {
4188 for (i = 0; i < size; i++) {
4189 hhd = &mapper->hash.buckets[i];
4190 hlist_for_each_entry(entry, hhd, hlist) {
4191 map = (struct ftrace_func_map *)entry;
4192 free_func(map);
4193 }
4194 }
4195 }
4196 free_ftrace_hash(&mapper->hash);
4197}
4198
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004199static void release_probe(struct ftrace_func_probe *probe)
4200{
4201 struct ftrace_probe_ops *probe_ops;
4202
4203 mutex_lock(&ftrace_lock);
4204
4205 WARN_ON(probe->ref <= 0);
4206
4207 /* Subtract the ref that was used to protect this instance */
4208 probe->ref--;
4209
4210 if (!probe->ref) {
4211 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004212 /*
4213 * Sending zero as ip tells probe_ops to free
4214 * the probe->data itself
4215 */
4216 if (probe_ops->free)
4217 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004218 list_del(&probe->list);
4219 kfree(probe);
4220 }
4221 mutex_unlock(&ftrace_lock);
4222}
4223
4224static void acquire_probe_locked(struct ftrace_func_probe *probe)
4225{
4226 /*
4227 * Add one ref to keep it from being freed when releasing the
4228 * ftrace_lock mutex.
4229 */
4230 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004231}
4232
Steven Rostedt59df055f2009-02-14 15:29:06 -05004233int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004234register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004235 struct ftrace_probe_ops *probe_ops,
4236 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004237{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004238 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004239 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004240 struct ftrace_hash **orig_hash;
4241 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004242 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004243 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004244 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004245 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004246 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004247
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004248 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004249 return -EINVAL;
4250
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004251 /* We do not support '!' for function probes */
4252 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004253 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004254
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004255
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004256 mutex_lock(&ftrace_lock);
4257 /* Check if the probe_ops is already registered */
4258 list_for_each_entry(probe, &tr->func_probes, list) {
4259 if (probe->probe_ops == probe_ops)
4260 break;
4261 }
4262 if (&probe->list == &tr->func_probes) {
4263 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4264 if (!probe) {
4265 mutex_unlock(&ftrace_lock);
4266 return -ENOMEM;
4267 }
4268 probe->probe_ops = probe_ops;
4269 probe->ops.func = function_trace_probe_call;
4270 probe->tr = tr;
4271 ftrace_ops_init(&probe->ops);
4272 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004273 }
4274
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004275 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004276
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004277 mutex_unlock(&ftrace_lock);
4278
4279 mutex_lock(&probe->ops.func_hash->regex_lock);
4280
4281 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004282 old_hash = *orig_hash;
4283 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4284
4285 ret = ftrace_match_records(hash, glob, strlen(glob));
4286
4287 /* Nothing found? */
4288 if (!ret)
4289 ret = -EINVAL;
4290
4291 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004292 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004293
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004294 size = 1 << hash->size_bits;
4295 for (i = 0; i < size; i++) {
4296 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4297 if (ftrace_lookup_ip(old_hash, entry->ip))
4298 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004299 /*
4300 * The caller might want to do something special
4301 * for each function we find. We call the callback
4302 * to give the caller an opportunity to do so.
4303 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004304 if (probe_ops->init) {
4305 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004306 entry->ip, data,
4307 &probe->data);
4308 if (ret < 0) {
4309 if (probe_ops->free && count)
4310 probe_ops->free(probe_ops, tr,
4311 0, probe->data);
4312 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004313 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004314 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004315 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004316 count++;
4317 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004318 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004319
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004320 mutex_lock(&ftrace_lock);
4321
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004322 if (!count) {
4323 /* Nothing was added? */
4324 ret = -EINVAL;
4325 goto out_unlock;
4326 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004327
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004328 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4329 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004330 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004331 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004332
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004333 /* One ref for each new function traced */
4334 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004335
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004336 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4337 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004338
Steven Rostedt59df055f2009-02-14 15:29:06 -05004339 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004340 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004341
4342 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004343 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004344 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004345 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004346 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004347
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004348 release_probe(probe);
4349
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004350 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004351
4352 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004353 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004354 goto out_unlock;
4355
4356 /* Failed to do the move, need to call the free functions */
4357 for (i = 0; i < size; i++) {
4358 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4359 if (ftrace_lookup_ip(old_hash, entry->ip))
4360 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004361 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004362 }
4363 }
4364 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004365}
4366
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004367int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004368unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4369 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004370{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004371 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004372 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004373 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004374 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004375 struct ftrace_hash **orig_hash;
4376 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004377 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004378 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004379 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004380 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004381 int count = 0;
4382 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004383 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004384
Naveen N. Raocbab5672017-05-16 23:21:25 +05304385 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004386 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304387 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004388 int not;
4389
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004390 func_g.type = filter_parse_regex(glob, strlen(glob),
4391 &func_g.search, &not);
4392 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004393
Steven Rostedtb6887d72009-02-17 12:32:04 -05004394 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004395 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004396 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004397 }
4398
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004399 mutex_lock(&ftrace_lock);
4400 /* Check if the probe_ops is already registered */
4401 list_for_each_entry(probe, &tr->func_probes, list) {
4402 if (probe->probe_ops == probe_ops)
4403 break;
4404 }
4405 if (&probe->list == &tr->func_probes)
4406 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004407
4408 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004409 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4410 goto err_unlock_ftrace;
4411
4412 acquire_probe_locked(probe);
4413
4414 mutex_unlock(&ftrace_lock);
4415
4416 mutex_lock(&probe->ops.func_hash->regex_lock);
4417
4418 orig_hash = &probe->ops.func_hash->filter_hash;
4419 old_hash = *orig_hash;
4420
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004421 if (ftrace_hash_empty(old_hash))
4422 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004423
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004424 old_hash_ops.filter_hash = old_hash;
4425 /* Probes only have filters */
4426 old_hash_ops.notrace_hash = NULL;
4427
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004428 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004429 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004430 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004431 goto out_unlock;
4432
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004433 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004434
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004435 size = 1 << hash->size_bits;
4436 for (i = 0; i < size; i++) {
4437 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004438
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004439 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004440 kallsyms_lookup(entry->ip, NULL, NULL,
4441 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004442 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004443 continue;
4444 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004445 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004446 remove_hash_entry(hash, entry);
4447 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004448 }
4449 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004450
4451 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004452 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004453 ret = -EINVAL;
4454 goto out_unlock;
4455 }
4456
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004457 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004458
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004459 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004460
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004461 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004462
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004463 if (ftrace_hash_empty(hash))
4464 ftrace_shutdown(&probe->ops, 0);
4465
4466 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004467 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004468
4469 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004470 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004471 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004472 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004473 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004474
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004475 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4476 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004477 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004478 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004479 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004480 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004481 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004482
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004483 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004484 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004485 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004486
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004487 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004488
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004489 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004490
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004491 err_unlock_ftrace:
4492 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004493 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004494}
4495
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304496void clear_ftrace_function_probes(struct trace_array *tr)
4497{
4498 struct ftrace_func_probe *probe, *n;
4499
4500 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4501 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4502}
4503
Steven Rostedtf6180772009-02-14 00:40:25 -05004504static LIST_HEAD(ftrace_commands);
4505static DEFINE_MUTEX(ftrace_cmd_mutex);
4506
Tom Zanussi38de93a2013-10-24 08:34:18 -05004507/*
4508 * Currently we only register ftrace commands from __init, so mark this
4509 * __init too.
4510 */
4511__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004512{
4513 struct ftrace_func_command *p;
4514 int ret = 0;
4515
4516 mutex_lock(&ftrace_cmd_mutex);
4517 list_for_each_entry(p, &ftrace_commands, list) {
4518 if (strcmp(cmd->name, p->name) == 0) {
4519 ret = -EBUSY;
4520 goto out_unlock;
4521 }
4522 }
4523 list_add(&cmd->list, &ftrace_commands);
4524 out_unlock:
4525 mutex_unlock(&ftrace_cmd_mutex);
4526
4527 return ret;
4528}
4529
Tom Zanussi38de93a2013-10-24 08:34:18 -05004530/*
4531 * Currently we only unregister ftrace commands from __init, so mark
4532 * this __init too.
4533 */
4534__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004535{
4536 struct ftrace_func_command *p, *n;
4537 int ret = -ENODEV;
4538
4539 mutex_lock(&ftrace_cmd_mutex);
4540 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4541 if (strcmp(cmd->name, p->name) == 0) {
4542 ret = 0;
4543 list_del_init(&p->list);
4544 goto out_unlock;
4545 }
4546 }
4547 out_unlock:
4548 mutex_unlock(&ftrace_cmd_mutex);
4549
4550 return ret;
4551}
4552
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004553static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004554 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004555{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004556 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004557 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004558 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004559 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004560 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004561
4562 func = strsep(&next, ":");
4563
4564 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004565 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004566 if (!ret)
4567 ret = -EINVAL;
4568 if (ret < 0)
4569 return ret;
4570 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004571 }
4572
Steven Rostedtf6180772009-02-14 00:40:25 -05004573 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004574
4575 command = strsep(&next, ":");
4576
Steven Rostedtf6180772009-02-14 00:40:25 -05004577 mutex_lock(&ftrace_cmd_mutex);
4578 list_for_each_entry(p, &ftrace_commands, list) {
4579 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004580 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004581 goto out_unlock;
4582 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004583 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004584 out_unlock:
4585 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004586
Steven Rostedtf6180772009-02-14 00:40:25 -05004587 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004588}
4589
Ingo Molnare309b412008-05-12 21:20:51 +02004590static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004591ftrace_regex_write(struct file *file, const char __user *ubuf,
4592 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004593{
4594 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004595 struct trace_parser *parser;
4596 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004597
Li Zefan4ba79782009-09-22 13:52:20 +08004598 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004599 return 0;
4600
Steven Rostedt5072c592008-05-12 21:20:43 +02004601 if (file->f_mode & FMODE_READ) {
4602 struct seq_file *m = file->private_data;
4603 iter = m->private;
4604 } else
4605 iter = file->private_data;
4606
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004607 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004608 return -ENODEV;
4609
4610 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004611
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004612 parser = &iter->parser;
4613 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004614
Li Zefan4ba79782009-09-22 13:52:20 +08004615 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004616 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004617 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004618 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004619 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004620 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004621 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004622 }
4623
Steven Rostedt5072c592008-05-12 21:20:43 +02004624 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004625 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004626 return ret;
4627}
4628
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004629ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004630ftrace_filter_write(struct file *file, const char __user *ubuf,
4631 size_t cnt, loff_t *ppos)
4632{
4633 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4634}
4635
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004636ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004637ftrace_notrace_write(struct file *file, const char __user *ubuf,
4638 size_t cnt, loff_t *ppos)
4639{
4640 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4641}
4642
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004643static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004644ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4645{
4646 struct ftrace_func_entry *entry;
4647
4648 if (!ftrace_location(ip))
4649 return -EINVAL;
4650
4651 if (remove) {
4652 entry = ftrace_lookup_ip(hash, ip);
4653 if (!entry)
4654 return -ENOENT;
4655 free_hash_entry(hash, entry);
4656 return 0;
4657 }
4658
4659 return add_hash_entry(hash, ip);
4660}
4661
4662static int
4663ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4664 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004665{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004666 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004667 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004668 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004669
Steven Rostedt41c52c02008-05-22 11:46:33 -04004670 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004671 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004672
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004673 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004674
Steven Rostedtf45948e2011-05-02 12:29:25 -04004675 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004676 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004677 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004678 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004679
Wang Nanb972cc52014-07-15 08:40:20 +08004680 if (reset)
4681 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4682 else
4683 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4684
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004685 if (!hash) {
4686 ret = -ENOMEM;
4687 goto out_regex_unlock;
4688 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004689
Jiri Olsaac483c42012-01-02 10:04:14 +01004690 if (buf && !ftrace_match_records(hash, buf, len)) {
4691 ret = -EINVAL;
4692 goto out_regex_unlock;
4693 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004694 if (ip) {
4695 ret = ftrace_match_addr(hash, ip, remove);
4696 if (ret < 0)
4697 goto out_regex_unlock;
4698 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004699
4700 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004701 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004702 mutex_unlock(&ftrace_lock);
4703
Jiri Olsaac483c42012-01-02 10:04:14 +01004704 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004705 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004706
4707 free_ftrace_hash(hash);
4708 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004709}
4710
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004711static int
4712ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4713 int reset, int enable)
4714{
4715 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4716}
4717
4718/**
4719 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4720 * @ops - the ops to set the filter with
4721 * @ip - the address to add to or remove from the filter.
4722 * @remove - non zero to remove the ip from the filter
4723 * @reset - non zero to reset all filters before applying this filter.
4724 *
4725 * Filters denote which functions should be enabled when tracing is enabled
4726 * If @ip is NULL, it failes to update filter.
4727 */
4728int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4729 int remove, int reset)
4730{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004731 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004732 return ftrace_set_addr(ops, ip, remove, reset, 1);
4733}
4734EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4735
Joel Fernandesd032ae82016-11-15 12:31:20 -08004736/**
4737 * ftrace_ops_set_global_filter - setup ops to use global filters
4738 * @ops - the ops which will use the global filters
4739 *
4740 * ftrace users who need global function trace filtering should call this.
4741 * It can set the global filter only if ops were not initialized before.
4742 */
4743void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4744{
4745 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4746 return;
4747
4748 ftrace_ops_init(ops);
4749 ops->func_hash = &global_ops.local_hash;
4750}
4751EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4752
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004753static int
4754ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4755 int reset, int enable)
4756{
4757 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4758}
4759
Steven Rostedt77a2b372008-05-12 21:20:45 +02004760/**
4761 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004762 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004763 * @buf - the string that holds the function filter text.
4764 * @len - the length of the string.
4765 * @reset - non zero to reset all filters before applying this filter.
4766 *
4767 * Filters denote which functions should be enabled when tracing is enabled.
4768 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4769 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004770int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004771 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004772{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004773 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004774 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004775}
Steven Rostedt936e0742011-05-05 22:54:01 -04004776EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004777
Steven Rostedt41c52c02008-05-22 11:46:33 -04004778/**
4779 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004780 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004781 * @buf - the string that holds the function notrace text.
4782 * @len - the length of the string.
4783 * @reset - non zero to reset all filters before applying this filter.
4784 *
4785 * Notrace Filters denote which functions should not be enabled when tracing
4786 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4787 * for tracing.
4788 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004789int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004790 int len, int reset)
4791{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004792 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004793 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004794}
4795EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4796/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004797 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004798 * @buf - the string that holds the function filter text.
4799 * @len - the length of the string.
4800 * @reset - non zero to reset all filters before applying this filter.
4801 *
4802 * Filters denote which functions should be enabled when tracing is enabled.
4803 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4804 */
4805void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4806{
4807 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4808}
4809EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4810
4811/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004812 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004813 * @buf - the string that holds the function notrace text.
4814 * @len - the length of the string.
4815 * @reset - non zero to reset all filters before applying this filter.
4816 *
4817 * Notrace Filters denote which functions should not be enabled when tracing
4818 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4819 * for tracing.
4820 */
4821void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004822{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004823 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004824}
Steven Rostedt936e0742011-05-05 22:54:01 -04004825EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004826
Steven Rostedt2af15d62009-05-28 13:37:24 -04004827/*
4828 * command line interface to allow users to set filters on boot up.
4829 */
4830#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4831static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4832static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4833
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004834/* Used by function selftest to not test if filter is set */
4835bool ftrace_filter_param __initdata;
4836
Steven Rostedt2af15d62009-05-28 13:37:24 -04004837static int __init set_ftrace_notrace(char *str)
4838{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004839 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004840 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004841 return 1;
4842}
4843__setup("ftrace_notrace=", set_ftrace_notrace);
4844
4845static int __init set_ftrace_filter(char *str)
4846{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004847 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004848 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004849 return 1;
4850}
4851__setup("ftrace_filter=", set_ftrace_filter);
4852
Stefan Assmann369bc182009-10-12 22:17:21 +02004853#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004854static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004855static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004856static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004857
Stefan Assmann369bc182009-10-12 22:17:21 +02004858static int __init set_graph_function(char *str)
4859{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004860 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004861 return 1;
4862}
4863__setup("ftrace_graph_filter=", set_graph_function);
4864
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004865static int __init set_graph_notrace_function(char *str)
4866{
4867 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4868 return 1;
4869}
4870__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4871
Todd Brandt65a50c652017-03-02 16:12:15 -08004872static int __init set_graph_max_depth_function(char *str)
4873{
4874 if (!str)
4875 return 0;
4876 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4877 return 1;
4878}
4879__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4880
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004881static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004882{
4883 int ret;
4884 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004885 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004886
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004887 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4888 if (WARN_ON(!hash))
4889 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004890
4891 while (buf) {
4892 func = strsep(&buf, ",");
4893 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004894 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004895 if (ret)
4896 printk(KERN_DEBUG "ftrace: function %s not "
4897 "traceable\n", func);
4898 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004899
4900 if (enable)
4901 ftrace_graph_hash = hash;
4902 else
4903 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02004904}
4905#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4906
Steven Rostedt2a85a372011-12-19 21:57:44 -05004907void __init
4908ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004909{
4910 char *func;
4911
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004912 ftrace_ops_init(ops);
4913
Steven Rostedt2af15d62009-05-28 13:37:24 -04004914 while (buf) {
4915 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004916 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004917 }
4918}
4919
4920static void __init set_ftrace_early_filters(void)
4921{
4922 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004923 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004924 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004925 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004926#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4927 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004928 set_ftrace_early_graph(ftrace_graph_buf, 1);
4929 if (ftrace_graph_notrace_buf[0])
4930 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004931#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004932}
4933
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004934int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004935{
4936 struct seq_file *m = (struct seq_file *)file->private_data;
4937 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004938 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004939 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004940 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004941 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004942
Steven Rostedt5072c592008-05-12 21:20:43 +02004943 if (file->f_mode & FMODE_READ) {
4944 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004945 seq_release(inode, file);
4946 } else
4947 iter = file->private_data;
4948
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004949 parser = &iter->parser;
4950 if (trace_parser_loaded(parser)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004951 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004952 }
4953
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004954 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004955
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004956 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004957
Steven Rostedt058e2972011-04-29 22:35:33 -04004958 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004959 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4960
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004961 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004962 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04004963 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004964 iter->hash->flags |= FTRACE_HASH_FL_MOD;
4965 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004966 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004967
Steven Rostedt058e2972011-04-29 22:35:33 -04004968 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004969 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
4970 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04004971 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04004972 } else {
4973 /* For read only, the hash is the ops hash */
4974 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04004975 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004976
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004977 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004978 free_ftrace_hash(iter->hash);
4979 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004980
Steven Rostedt5072c592008-05-12 21:20:43 +02004981 return 0;
4982}
4983
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004984static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004985 .open = ftrace_avail_open,
4986 .read = seq_read,
4987 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004988 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004989};
4990
Steven Rostedt647bcd02011-05-03 14:39:21 -04004991static const struct file_operations ftrace_enabled_fops = {
4992 .open = ftrace_enabled_open,
4993 .read = seq_read,
4994 .llseek = seq_lseek,
4995 .release = seq_release_private,
4996};
4997
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004998static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004999 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005000 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005001 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005002 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005003 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005004};
5005
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005006static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005007 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005008 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005009 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005010 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005011 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005012};
5013
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005014#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5015
5016static DEFINE_MUTEX(graph_lock);
5017
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005018struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5019struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5020
5021enum graph_filter_type {
5022 GRAPH_FILTER_NOTRACE = 0,
5023 GRAPH_FILTER_FUNCTION,
5024};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005025
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005026#define FTRACE_GRAPH_EMPTY ((void *)1)
5027
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005028struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005029 struct ftrace_hash *hash;
5030 struct ftrace_func_entry *entry;
5031 int idx; /* for hash table iteration */
5032 enum graph_filter_type type;
5033 struct ftrace_hash *new_hash;
5034 const struct seq_operations *seq_ops;
5035 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005036};
5037
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005038static void *
Li Zefan85951842009-06-24 09:54:00 +08005039__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005040{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005041 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005042 struct ftrace_func_entry *entry = fgd->entry;
5043 struct hlist_head *head;
5044 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005045
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005046 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005047 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005048
5049 if (entry) {
5050 hlist_for_each_entry_continue(entry, hlist) {
5051 fgd->entry = entry;
5052 return entry;
5053 }
5054
5055 idx++;
5056 }
5057
5058 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5059 head = &fgd->hash->buckets[i];
5060 hlist_for_each_entry(entry, head, hlist) {
5061 fgd->entry = entry;
5062 fgd->idx = i;
5063 return entry;
5064 }
5065 }
5066 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005067}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005068
Li Zefan85951842009-06-24 09:54:00 +08005069static void *
5070g_next(struct seq_file *m, void *v, loff_t *pos)
5071{
5072 (*pos)++;
5073 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005074}
5075
5076static void *g_start(struct seq_file *m, loff_t *pos)
5077{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005078 struct ftrace_graph_data *fgd = m->private;
5079
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005080 mutex_lock(&graph_lock);
5081
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005082 if (fgd->type == GRAPH_FILTER_FUNCTION)
5083 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5084 lockdep_is_held(&graph_lock));
5085 else
5086 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5087 lockdep_is_held(&graph_lock));
5088
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005089 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005090 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005091 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005092
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005093 fgd->idx = 0;
5094 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005095 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005096}
5097
5098static void g_stop(struct seq_file *m, void *p)
5099{
5100 mutex_unlock(&graph_lock);
5101}
5102
5103static int g_show(struct seq_file *m, void *v)
5104{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005105 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005106
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005107 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005108 return 0;
5109
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005110 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005111 struct ftrace_graph_data *fgd = m->private;
5112
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005113 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005114 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005115 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005116 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005117 return 0;
5118 }
5119
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005120 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005121
5122 return 0;
5123}
5124
James Morris88e9d342009-09-22 16:43:43 -07005125static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005126 .start = g_start,
5127 .next = g_next,
5128 .stop = g_stop,
5129 .show = g_show,
5130};
5131
5132static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005133__ftrace_graph_open(struct inode *inode, struct file *file,
5134 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005135{
5136 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005137 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005138
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005139 if (file->f_mode & FMODE_WRITE) {
5140 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5141
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005142 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5143 return -ENOMEM;
5144
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005145 if (file->f_flags & O_TRUNC)
5146 new_hash = alloc_ftrace_hash(size_bits);
5147 else
5148 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5149 fgd->hash);
5150 if (!new_hash) {
5151 ret = -ENOMEM;
5152 goto out;
5153 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005154 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005155
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005156 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005157 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005158 if (!ret) {
5159 struct seq_file *m = file->private_data;
5160 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005161 } else {
5162 /* Failed */
5163 free_ftrace_hash(new_hash);
5164 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005165 }
5166 } else
5167 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005168
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005169out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005170 if (ret < 0 && file->f_mode & FMODE_WRITE)
5171 trace_parser_put(&fgd->parser);
5172
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005173 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005174
5175 /*
5176 * All uses of fgd->hash must be taken with the graph_lock
5177 * held. The graph_lock is going to be released, so force
5178 * fgd->hash to be reinitialized when it is taken again.
5179 */
5180 fgd->hash = NULL;
5181
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005182 return ret;
5183}
5184
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005185static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005186ftrace_graph_open(struct inode *inode, struct file *file)
5187{
5188 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005189 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005190
5191 if (unlikely(ftrace_disabled))
5192 return -ENODEV;
5193
5194 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5195 if (fgd == NULL)
5196 return -ENOMEM;
5197
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005198 mutex_lock(&graph_lock);
5199
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005200 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5201 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005202 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005203 fgd->seq_ops = &ftrace_graph_seq_ops;
5204
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005205 ret = __ftrace_graph_open(inode, file, fgd);
5206 if (ret < 0)
5207 kfree(fgd);
5208
5209 mutex_unlock(&graph_lock);
5210 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005211}
5212
5213static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005214ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5215{
5216 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005217 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005218
5219 if (unlikely(ftrace_disabled))
5220 return -ENODEV;
5221
5222 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5223 if (fgd == NULL)
5224 return -ENOMEM;
5225
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005226 mutex_lock(&graph_lock);
5227
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005228 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5229 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005230 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005231 fgd->seq_ops = &ftrace_graph_seq_ops;
5232
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005233 ret = __ftrace_graph_open(inode, file, fgd);
5234 if (ret < 0)
5235 kfree(fgd);
5236
5237 mutex_unlock(&graph_lock);
5238 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005239}
5240
5241static int
Li Zefan87827112009-07-23 11:29:11 +08005242ftrace_graph_release(struct inode *inode, struct file *file)
5243{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005244 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005245 struct ftrace_hash *old_hash, *new_hash;
5246 struct trace_parser *parser;
5247 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005248
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005249 if (file->f_mode & FMODE_READ) {
5250 struct seq_file *m = file->private_data;
5251
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005252 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005253 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005254 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005255 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005256 }
5257
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005258
5259 if (file->f_mode & FMODE_WRITE) {
5260
5261 parser = &fgd->parser;
5262
5263 if (trace_parser_loaded((parser))) {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005264 ret = ftrace_graph_set_hash(fgd->new_hash,
5265 parser->buffer);
5266 }
5267
5268 trace_parser_put(parser);
5269
5270 new_hash = __ftrace_hash_move(fgd->new_hash);
5271 if (!new_hash) {
5272 ret = -ENOMEM;
5273 goto out;
5274 }
5275
5276 mutex_lock(&graph_lock);
5277
5278 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5279 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5280 lockdep_is_held(&graph_lock));
5281 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5282 } else {
5283 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5284 lockdep_is_held(&graph_lock));
5285 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5286 }
5287
5288 mutex_unlock(&graph_lock);
5289
5290 /* Wait till all users are no longer using the old hash */
5291 synchronize_sched();
5292
5293 free_ftrace_hash(old_hash);
5294 }
5295
5296 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005297 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005298 kfree(fgd);
5299
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005300 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005301}
5302
5303static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005304ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005305{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005306 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005307 struct dyn_ftrace *rec;
5308 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005309 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005310 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005311 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005312
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005313 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005314 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5315 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005316
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005317 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005318
Steven Rostedt52baf112009-02-14 01:15:39 -05005319 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005320
5321 if (unlikely(ftrace_disabled)) {
5322 mutex_unlock(&ftrace_lock);
5323 return -ENODEV;
5324 }
5325
Steven Rostedt265c8312009-02-13 12:43:56 -05005326 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005327
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005328 if (rec->flags & FTRACE_FL_DISABLED)
5329 continue;
5330
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005331 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005332 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005333
5334 if (!not) {
5335 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005336
5337 if (entry)
5338 continue;
5339 if (add_hash_entry(hash, rec->ip) < 0)
5340 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005341 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005342 if (entry) {
5343 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005344 fail = 0;
5345 }
5346 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005347 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005348 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005349out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005350 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005351
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005352 if (fail)
5353 return -EINVAL;
5354
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005355 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005356}
5357
5358static ssize_t
5359ftrace_graph_write(struct file *file, const char __user *ubuf,
5360 size_t cnt, loff_t *ppos)
5361{
Namhyung Kim6a101082013-10-14 17:24:25 +09005362 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005363 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005364 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005365
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005366 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005367 return 0;
5368
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005369 /* Read mode uses seq functions */
5370 if (file->f_mode & FMODE_READ) {
5371 struct seq_file *m = file->private_data;
5372 fgd = m->private;
5373 }
5374
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005375 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005376
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005377 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005378
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005379 if (read >= 0 && trace_parser_loaded(parser) &&
5380 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005381
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005382 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005383 parser->buffer);
5384 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005385 }
5386
Namhyung Kim6a101082013-10-14 17:24:25 +09005387 if (!ret)
5388 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005389
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005390 return ret;
5391}
5392
5393static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005394 .open = ftrace_graph_open,
5395 .read = seq_read,
5396 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005397 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005398 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005399};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005400
5401static const struct file_operations ftrace_graph_notrace_fops = {
5402 .open = ftrace_graph_notrace_open,
5403 .read = seq_read,
5404 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005405 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005406 .release = ftrace_graph_release,
5407};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005408#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5409
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005410void ftrace_create_filter_files(struct ftrace_ops *ops,
5411 struct dentry *parent)
5412{
5413
5414 trace_create_file("set_ftrace_filter", 0644, parent,
5415 ops, &ftrace_filter_fops);
5416
5417 trace_create_file("set_ftrace_notrace", 0644, parent,
5418 ops, &ftrace_notrace_fops);
5419}
5420
5421/*
5422 * The name "destroy_filter_files" is really a misnomer. Although
5423 * in the future, it may actualy delete the files, but this is
5424 * really intended to make sure the ops passed in are disabled
5425 * and that when this function returns, the caller is free to
5426 * free the ops.
5427 *
5428 * The "destroy" name is only to match the "create" name that this
5429 * should be paired with.
5430 */
5431void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5432{
5433 mutex_lock(&ftrace_lock);
5434 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5435 ftrace_shutdown(ops, 0);
5436 ops->flags |= FTRACE_OPS_FL_DELETED;
5437 mutex_unlock(&ftrace_lock);
5438}
5439
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005440static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005441{
Steven Rostedt5072c592008-05-12 21:20:43 +02005442
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005443 trace_create_file("available_filter_functions", 0444,
5444 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005445
Steven Rostedt647bcd02011-05-03 14:39:21 -04005446 trace_create_file("enabled_functions", 0444,
5447 d_tracer, NULL, &ftrace_enabled_fops);
5448
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005449 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005450
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005451#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Chen LinX1ce05002014-09-03 14:31:09 +08005452 trace_create_file("set_graph_function", 0644, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005453 NULL,
5454 &ftrace_graph_fops);
Chen LinX1ce05002014-09-03 14:31:09 +08005455 trace_create_file("set_graph_notrace", 0644, d_tracer,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005456 NULL,
5457 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005458#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5459
Steven Rostedt5072c592008-05-12 21:20:43 +02005460 return 0;
5461}
5462
Steven Rostedt9fd49322012-04-24 22:32:06 -04005463static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005464{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005465 const unsigned long *ipa = a;
5466 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005467
Steven Rostedt9fd49322012-04-24 22:32:06 -04005468 if (*ipa > *ipb)
5469 return 1;
5470 if (*ipa < *ipb)
5471 return -1;
5472 return 0;
5473}
5474
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005475static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005476 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005477 unsigned long *end)
5478{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005479 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005480 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005481 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005482 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005483 unsigned long *p;
5484 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005485 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005486 int ret = -ENOMEM;
5487
5488 count = end - start;
5489
5490 if (!count)
5491 return 0;
5492
Steven Rostedt9fd49322012-04-24 22:32:06 -04005493 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005494 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005495
Steven Rostedt706c81f2012-04-24 23:45:26 -04005496 start_pg = ftrace_allocate_pages(count);
5497 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005498 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005499
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005500 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005501
Steven Rostedt320823092011-12-16 14:42:37 -05005502 /*
5503 * Core and each module needs their own pages, as
5504 * modules will free them when they are removed.
5505 * Force a new page to be allocated for modules.
5506 */
Steven Rostedta7900872011-12-16 16:23:44 -05005507 if (!mod) {
5508 WARN_ON(ftrace_pages || ftrace_pages_start);
5509 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005510 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005511 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005512 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005513 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005514
Steven Rostedta7900872011-12-16 16:23:44 -05005515 if (WARN_ON(ftrace_pages->next)) {
5516 /* Hmm, we have free pages? */
5517 while (ftrace_pages->next)
5518 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005519 }
Steven Rostedta7900872011-12-16 16:23:44 -05005520
Steven Rostedt706c81f2012-04-24 23:45:26 -04005521 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005522 }
5523
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005524 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005525 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005526 while (p < end) {
5527 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005528 /*
5529 * Some architecture linkers will pad between
5530 * the different mcount_loc sections of different
5531 * object files to satisfy alignments.
5532 * Skip any NULL pointers.
5533 */
5534 if (!addr)
5535 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005536
5537 if (pg->index == pg->size) {
5538 /* We should have allocated enough */
5539 if (WARN_ON(!pg->next))
5540 break;
5541 pg = pg->next;
5542 }
5543
5544 rec = &pg->records[pg->index++];
5545 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005546 }
5547
Steven Rostedt706c81f2012-04-24 23:45:26 -04005548 /* We should have used all pages */
5549 WARN_ON(pg->next);
5550
5551 /* Assign the last page to ftrace_pages */
5552 ftrace_pages = pg;
5553
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005554 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005555 * We only need to disable interrupts on start up
5556 * because we are modifying code that an interrupt
5557 * may execute, and the modification is not atomic.
5558 * But for modules, nothing runs the code we modify
5559 * until we are finished with it, and there's no
5560 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005561 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005562 if (!mod)
5563 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005564 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005565 if (!mod)
5566 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005567 ret = 0;
5568 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005569 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005570
Steven Rostedta7900872011-12-16 16:23:44 -05005571 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005572}
5573
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005574struct ftrace_mod_func {
5575 struct list_head list;
5576 char *name;
5577 unsigned long ip;
5578 unsigned int size;
5579};
5580
5581struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005582 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005583 struct list_head list;
5584 struct module *mod;
5585 unsigned long start_addr;
5586 unsigned long end_addr;
5587 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005588 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005589};
5590
Steven Rostedt93eb6772009-04-15 13:24:06 -04005591#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005592
5593#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5594
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005595static LIST_HEAD(ftrace_mod_maps);
5596
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005597static int referenced_filters(struct dyn_ftrace *rec)
5598{
5599 struct ftrace_ops *ops;
5600 int cnt = 0;
5601
5602 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5603 if (ops_references_rec(ops, rec))
5604 cnt++;
5605 }
5606
5607 return cnt;
5608}
5609
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005610static void
5611clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5612{
5613 struct ftrace_func_entry *entry;
5614 struct dyn_ftrace *rec;
5615 int i;
5616
5617 if (ftrace_hash_empty(hash))
5618 return;
5619
5620 for (i = 0; i < pg->index; i++) {
5621 rec = &pg->records[i];
5622 entry = __ftrace_lookup_ip(hash, rec->ip);
5623 /*
5624 * Do not allow this rec to match again.
5625 * Yeah, it may waste some memory, but will be removed
5626 * if/when the hash is modified again.
5627 */
5628 if (entry)
5629 entry->ip = 0;
5630 }
5631}
5632
5633/* Clear any records from hashs */
5634static void clear_mod_from_hashes(struct ftrace_page *pg)
5635{
5636 struct trace_array *tr;
5637
5638 mutex_lock(&trace_types_lock);
5639 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5640 if (!tr->ops || !tr->ops->func_hash)
5641 continue;
5642 mutex_lock(&tr->ops->func_hash->regex_lock);
5643 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5644 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5645 mutex_unlock(&tr->ops->func_hash->regex_lock);
5646 }
5647 mutex_unlock(&trace_types_lock);
5648}
5649
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005650static void ftrace_free_mod_map(struct rcu_head *rcu)
5651{
5652 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5653 struct ftrace_mod_func *mod_func;
5654 struct ftrace_mod_func *n;
5655
5656 /* All the contents of mod_map are now not visible to readers */
5657 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5658 kfree(mod_func->name);
5659 list_del(&mod_func->list);
5660 kfree(mod_func);
5661 }
5662
5663 kfree(mod_map);
5664}
5665
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005666void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005667{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005668 struct ftrace_mod_map *mod_map;
5669 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005670 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005671 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005672 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005673 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005674 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005675
Steven Rostedt93eb6772009-04-15 13:24:06 -04005676 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005677
5678 if (ftrace_disabled)
5679 goto out_unlock;
5680
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005681 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5682 if (mod_map->mod == mod) {
5683 list_del_rcu(&mod_map->list);
5684 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5685 break;
5686 }
5687 }
5688
Steven Rostedt320823092011-12-16 14:42:37 -05005689 /*
5690 * Each module has its own ftrace_pages, remove
5691 * them from the list.
5692 */
5693 last_pg = &ftrace_pages_start;
5694 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5695 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005696 if (within_module_core(rec->ip, mod) ||
5697 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005698 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005699 * As core pages are first, the first
5700 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005701 */
Steven Rostedt320823092011-12-16 14:42:37 -05005702 if (WARN_ON(pg == ftrace_pages_start))
5703 goto out_unlock;
5704
5705 /* Check if we are deleting the last page */
5706 if (pg == ftrace_pages)
5707 ftrace_pages = next_to_ftrace_page(last_pg);
5708
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04005709 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05005710 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005711
5712 pg->next = tmp_page;
5713 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005714 } else
5715 last_pg = &pg->next;
5716 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005717 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005718 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005719
5720 for (pg = tmp_page; pg; pg = tmp_page) {
5721
5722 /* Needs to be called outside of ftrace_lock */
5723 clear_mod_from_hashes(pg);
5724
5725 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5726 free_pages((unsigned long)pg->records, order);
5727 tmp_page = pg->next;
5728 kfree(pg);
5729 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04005730}
5731
Jessica Yu7dcd1822016-02-16 17:32:33 -05005732void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005733{
5734 struct dyn_ftrace *rec;
5735 struct ftrace_page *pg;
5736
5737 mutex_lock(&ftrace_lock);
5738
5739 if (ftrace_disabled)
5740 goto out_unlock;
5741
5742 /*
5743 * If the tracing is enabled, go ahead and enable the record.
5744 *
5745 * The reason not to enable the record immediatelly is the
5746 * inherent check of ftrace_make_nop/ftrace_make_call for
5747 * correct previous instructions. Making first the NOP
5748 * conversion puts the module to the correct state, thus
5749 * passing the ftrace_make_call check.
5750 *
5751 * We also delay this to after the module code already set the
5752 * text to read-only, as we now need to set it back to read-write
5753 * so that we can modify the text.
5754 */
5755 if (ftrace_start_up)
5756 ftrace_arch_code_modify_prepare();
5757
5758 do_for_each_ftrace_rec(pg, rec) {
5759 int cnt;
5760 /*
5761 * do_for_each_ftrace_rec() is a double loop.
5762 * module text shares the pg. If a record is
5763 * not part of this module, then skip this pg,
5764 * which the "break" will do.
5765 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005766 if (!within_module_core(rec->ip, mod) &&
5767 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005768 break;
5769
5770 cnt = 0;
5771
5772 /*
5773 * When adding a module, we need to check if tracers are
5774 * currently enabled and if they are, and can trace this record,
5775 * we need to enable the module functions as well as update the
5776 * reference counts for those function records.
5777 */
5778 if (ftrace_start_up)
5779 cnt += referenced_filters(rec);
5780
5781 /* This clears FTRACE_FL_DISABLED */
5782 rec->flags = cnt;
5783
5784 if (ftrace_start_up && cnt) {
5785 int failed = __ftrace_replace_code(rec, 1);
5786 if (failed) {
5787 ftrace_bug(failed, rec);
5788 goto out_loop;
5789 }
5790 }
5791
5792 } while_for_each_ftrace_rec();
5793
5794 out_loop:
5795 if (ftrace_start_up)
5796 ftrace_arch_code_modify_post_process();
5797
5798 out_unlock:
5799 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04005800
5801 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005802}
5803
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005804void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005805{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005806 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005807 return;
5808
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005809 ftrace_process_locs(mod, mod->ftrace_callsites,
5810 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005811}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005812
5813static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5814 struct dyn_ftrace *rec)
5815{
5816 struct ftrace_mod_func *mod_func;
5817 unsigned long symsize;
5818 unsigned long offset;
5819 char str[KSYM_SYMBOL_LEN];
5820 char *modname;
5821 const char *ret;
5822
5823 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5824 if (!ret)
5825 return;
5826
5827 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5828 if (!mod_func)
5829 return;
5830
5831 mod_func->name = kstrdup(str, GFP_KERNEL);
5832 if (!mod_func->name) {
5833 kfree(mod_func);
5834 return;
5835 }
5836
5837 mod_func->ip = rec->ip - offset;
5838 mod_func->size = symsize;
5839
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005840 mod_map->num_funcs++;
5841
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005842 list_add_rcu(&mod_func->list, &mod_map->funcs);
5843}
5844
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005845static struct ftrace_mod_map *
5846allocate_ftrace_mod_map(struct module *mod,
5847 unsigned long start, unsigned long end)
5848{
5849 struct ftrace_mod_map *mod_map;
5850
5851 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5852 if (!mod_map)
5853 return NULL;
5854
5855 mod_map->mod = mod;
5856 mod_map->start_addr = start;
5857 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005858 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005859
5860 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5861
5862 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5863
5864 return mod_map;
5865}
5866
5867static const char *
5868ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5869 unsigned long addr, unsigned long *size,
5870 unsigned long *off, char *sym)
5871{
5872 struct ftrace_mod_func *found_func = NULL;
5873 struct ftrace_mod_func *mod_func;
5874
5875 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5876 if (addr >= mod_func->ip &&
5877 addr < mod_func->ip + mod_func->size) {
5878 found_func = mod_func;
5879 break;
5880 }
5881 }
5882
5883 if (found_func) {
5884 if (size)
5885 *size = found_func->size;
5886 if (off)
5887 *off = addr - found_func->ip;
5888 if (sym)
5889 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5890
5891 return found_func->name;
5892 }
5893
5894 return NULL;
5895}
5896
5897const char *
5898ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5899 unsigned long *off, char **modname, char *sym)
5900{
5901 struct ftrace_mod_map *mod_map;
5902 const char *ret = NULL;
5903
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005904 /* mod_map is freed via call_rcu_sched() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005905 preempt_disable();
5906 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5907 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
5908 if (ret) {
5909 if (modname)
5910 *modname = mod_map->mod->name;
5911 break;
5912 }
5913 }
5914 preempt_enable();
5915
5916 return ret;
5917}
5918
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005919int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
5920 char *type, char *name,
5921 char *module_name, int *exported)
5922{
5923 struct ftrace_mod_map *mod_map;
5924 struct ftrace_mod_func *mod_func;
5925
5926 preempt_disable();
5927 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5928
5929 if (symnum >= mod_map->num_funcs) {
5930 symnum -= mod_map->num_funcs;
5931 continue;
5932 }
5933
5934 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5935 if (symnum > 1) {
5936 symnum--;
5937 continue;
5938 }
5939
5940 *value = mod_func->ip;
5941 *type = 'T';
5942 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
5943 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
5944 *exported = 1;
5945 preempt_enable();
5946 return 0;
5947 }
5948 WARN_ON(1);
5949 break;
5950 }
5951 preempt_enable();
5952 return -ERANGE;
5953}
5954
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005955#else
5956static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5957 struct dyn_ftrace *rec) { }
5958static inline struct ftrace_mod_map *
5959allocate_ftrace_mod_map(struct module *mod,
5960 unsigned long start, unsigned long end)
5961{
5962 return NULL;
5963}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005964#endif /* CONFIG_MODULES */
5965
Joel Fernandes8715b102017-10-09 12:29:31 -07005966struct ftrace_init_func {
5967 struct list_head list;
5968 unsigned long ip;
5969};
5970
5971/* Clear any init ips from hashes */
5972static void
5973clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005974{
Joel Fernandes8715b102017-10-09 12:29:31 -07005975 struct ftrace_func_entry *entry;
5976
5977 if (ftrace_hash_empty(hash))
5978 return;
5979
5980 entry = __ftrace_lookup_ip(hash, func->ip);
5981
5982 /*
5983 * Do not allow this rec to match again.
5984 * Yeah, it may waste some memory, but will be removed
5985 * if/when the hash is modified again.
5986 */
5987 if (entry)
5988 entry->ip = 0;
5989}
5990
5991static void
5992clear_func_from_hashes(struct ftrace_init_func *func)
5993{
5994 struct trace_array *tr;
5995
5996 mutex_lock(&trace_types_lock);
5997 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5998 if (!tr->ops || !tr->ops->func_hash)
5999 continue;
6000 mutex_lock(&tr->ops->func_hash->regex_lock);
6001 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6002 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6003 mutex_unlock(&tr->ops->func_hash->regex_lock);
6004 }
6005 mutex_unlock(&trace_types_lock);
6006}
6007
6008static void add_to_clear_hash_list(struct list_head *clear_list,
6009 struct dyn_ftrace *rec)
6010{
6011 struct ftrace_init_func *func;
6012
6013 func = kmalloc(sizeof(*func), GFP_KERNEL);
6014 if (!func) {
6015 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6016 return;
6017 }
6018
6019 func->ip = rec->ip;
6020 list_add(&func->list, clear_list);
6021}
6022
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006023void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006024{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006025 unsigned long start = (unsigned long)(start_ptr);
6026 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006027 struct ftrace_page **last_pg = &ftrace_pages_start;
6028 struct ftrace_page *pg;
6029 struct dyn_ftrace *rec;
6030 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006031 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07006032 struct ftrace_init_func *func, *func_next;
6033 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006034 int order;
6035
Joel Fernandes8715b102017-10-09 12:29:31 -07006036 INIT_LIST_HEAD(&clear_hash);
6037
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006038 key.ip = start;
6039 key.flags = end; /* overload flags, as it is unsigned long */
6040
6041 mutex_lock(&ftrace_lock);
6042
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006043 /*
6044 * If we are freeing module init memory, then check if
6045 * any tracer is active. If so, we need to save a mapping of
6046 * the module functions being freed with the address.
6047 */
6048 if (mod && ftrace_ops_list != &ftrace_list_end)
6049 mod_map = allocate_ftrace_mod_map(mod, start, end);
6050
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006051 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6052 if (end < pg->records[0].ip ||
6053 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6054 continue;
6055 again:
6056 rec = bsearch(&key, pg->records, pg->index,
6057 sizeof(struct dyn_ftrace),
6058 ftrace_cmp_recs);
6059 if (!rec)
6060 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006061
Joel Fernandes8715b102017-10-09 12:29:31 -07006062 /* rec will be cleared from hashes after ftrace_lock unlock */
6063 add_to_clear_hash_list(&clear_hash, rec);
6064
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006065 if (mod_map)
6066 save_ftrace_mod_rec(mod_map, rec);
6067
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006068 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04006069 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006070 if (!pg->index) {
6071 *last_pg = pg->next;
6072 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6073 free_pages((unsigned long)pg->records, order);
6074 kfree(pg);
6075 pg = container_of(last_pg, struct ftrace_page, next);
6076 if (!(*last_pg))
6077 ftrace_pages = pg;
6078 continue;
6079 }
6080 memmove(rec, rec + 1,
6081 (pg->index - (rec - pg->records)) * sizeof(*rec));
6082 /* More than one function may be in this block */
6083 goto again;
6084 }
6085 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07006086
6087 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6088 clear_func_from_hashes(func);
6089 kfree(func);
6090 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006091}
6092
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006093void __init ftrace_free_init_mem(void)
6094{
6095 void *start = (void *)(&__init_begin);
6096 void *end = (void *)(&__init_end);
6097
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006098 ftrace_free_mem(NULL, start, end);
Steven Rostedt93eb6772009-04-15 13:24:06 -04006099}
6100
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006101void __init ftrace_init(void)
6102{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006103 extern unsigned long __start_mcount_loc[];
6104 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006105 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006106 int ret;
6107
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006108 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006109 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006110 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01006111 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006112 goto failed;
6113
6114 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006115 if (!count) {
6116 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006117 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006118 }
6119
6120 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6121 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006122
6123 last_ftrace_enabled = ftrace_enabled = 1;
6124
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006125 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08006126 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006127 __stop_mcount_loc);
6128
Steven Rostedt2af15d62009-05-28 13:37:24 -04006129 set_ftrace_early_filters();
6130
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006131 return;
6132 failed:
6133 ftrace_disabled = 1;
6134}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006135
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006136/* Do nothing if arch does not support this */
6137void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6138{
6139}
6140
6141static void ftrace_update_trampoline(struct ftrace_ops *ops)
6142{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006143 arch_ftrace_update_trampoline(ops);
6144}
6145
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006146void ftrace_init_trace_array(struct trace_array *tr)
6147{
6148 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04006149 INIT_LIST_HEAD(&tr->mod_trace);
6150 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006151}
Steven Rostedt3d083392008-05-12 21:20:42 +02006152#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006153
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05006154struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04006155 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006156 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6157 FTRACE_OPS_FL_INITIALIZED |
6158 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04006159};
6160
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006161static int __init ftrace_nodyn_init(void)
6162{
6163 ftrace_enabled = 1;
6164 return 0;
6165}
Steven Rostedt6f415672012-10-05 12:13:07 -04006166core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006167
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006168static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006169static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006170static inline void ftrace_startup_all(int command) { }
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006171
Ingo Molnarc7aafc52008-05-12 21:20:45 +02006172# define ftrace_startup_sysctl() do { } while (0)
6173# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04006174
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006175static void ftrace_update_trampoline(struct ftrace_ops *ops)
6176{
6177}
6178
Steven Rostedt3d083392008-05-12 21:20:42 +02006179#endif /* CONFIG_DYNAMIC_FTRACE */
6180
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006181__init void ftrace_init_global_array_ops(struct trace_array *tr)
6182{
6183 tr->ops = &global_ops;
6184 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006185 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006186}
6187
6188void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6189{
6190 /* If we filter on pids, update to use the pid function */
6191 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6192 if (WARN_ON(tr->ops->func != ftrace_stub))
6193 printk("ftrace ops had %pS for function\n",
6194 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006195 }
6196 tr->ops->func = func;
6197 tr->ops->private = tr;
6198}
6199
6200void ftrace_reset_array_ops(struct trace_array *tr)
6201{
6202 tr->ops->func = ftrace_stub;
6203}
6204
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006205static inline void
6206__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006207 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006208{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006209 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006210 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04006211
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006212 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6213 if (bit < 0)
6214 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04006215
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006216 /*
6217 * Some of the ops may be dynamically allocated,
6218 * they must be freed after a synchronize_sched().
6219 */
6220 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006221
Steven Rostedt0a016402012-11-02 17:03:03 -04006222 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006223 /*
6224 * Check the following for each ops before calling their func:
6225 * if RCU flag is set, then rcu_is_watching() must be true
6226 * if PER_CPU is set, then ftrace_function_local_disable()
6227 * must be false
6228 * Otherwise test if the ip matches the ops filter
6229 *
6230 * If any of the above fails then the op->func() is not executed.
6231 */
6232 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006233 ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04006234 if (FTRACE_WARN_ON(!op->func)) {
6235 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006236 goto out;
6237 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04006238 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006239 }
Steven Rostedt0a016402012-11-02 17:03:03 -04006240 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006241out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006242 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006243 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04006244}
6245
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006246/*
6247 * Some archs only support passing ip and parent_ip. Even though
6248 * the list function ignores the op parameter, we do not want any
6249 * C side effects, where a function is called without the caller
6250 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006251 * Archs are to support both the regs and ftrace_ops at the same time.
6252 * If they support ftrace_ops, it is assumed they support regs.
6253 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09006254 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6255 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006256 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08006257 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006258 */
6259#if ARCH_SUPPORTS_FTRACE_OPS
6260static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006261 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006262{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006263 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006264}
6265#else
6266static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6267{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006268 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006269}
6270#endif
6271
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006272/*
6273 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006274 * recursion, needs RCU protection and/or requires per cpu handling, then
6275 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006276 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006277static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006278 struct ftrace_ops *op, struct pt_regs *regs)
6279{
6280 int bit;
6281
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006282 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6283 return;
6284
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006285 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6286 if (bit < 0)
6287 return;
6288
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006289 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006290
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006291 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006292
6293 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006294 trace_clear_recursion(bit);
6295}
6296
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006297/**
6298 * ftrace_ops_get_func - get the function a trampoline should call
6299 * @ops: the ops to get the function for
6300 *
6301 * Normally the mcount trampoline will call the ops->func, but there
6302 * are times that it should not. For example, if the ops does not
6303 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08006304 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006305 *
6306 * Returns the function that the trampoline should call for @ops.
6307 */
6308ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6309{
6310 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006311 * If the function does not handle recursion, needs to be RCU safe,
6312 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006313 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006314 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006315 ops->flags & FTRACE_OPS_FL_RCU)
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006316 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006317
6318 return ops->func;
6319}
6320
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006321static void
6322ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6323 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05006324{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006325 struct trace_array *tr = data;
6326 struct trace_pid_list *pid_list;
6327
6328 pid_list = rcu_dereference_sched(tr->function_pids);
6329
6330 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6331 trace_ignore_this_task(pid_list, next));
6332}
6333
Namhyung Kim1e104862017-04-17 11:44:28 +09006334static void
6335ftrace_pid_follow_sched_process_fork(void *data,
6336 struct task_struct *self,
6337 struct task_struct *task)
6338{
6339 struct trace_pid_list *pid_list;
6340 struct trace_array *tr = data;
6341
6342 pid_list = rcu_dereference_sched(tr->function_pids);
6343 trace_filter_add_remove_task(pid_list, self, task);
6344}
6345
6346static void
6347ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6348{
6349 struct trace_pid_list *pid_list;
6350 struct trace_array *tr = data;
6351
6352 pid_list = rcu_dereference_sched(tr->function_pids);
6353 trace_filter_add_remove_task(pid_list, NULL, task);
6354}
6355
6356void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6357{
6358 if (enable) {
6359 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6360 tr);
6361 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6362 tr);
6363 } else {
6364 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6365 tr);
6366 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6367 tr);
6368 }
6369}
6370
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006371static void clear_ftrace_pids(struct trace_array *tr)
6372{
6373 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05006374 int cpu;
6375
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006376 pid_list = rcu_dereference_protected(tr->function_pids,
6377 lockdep_is_held(&ftrace_lock));
6378 if (!pid_list)
6379 return;
6380
6381 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6382
6383 for_each_possible_cpu(cpu)
6384 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6385
6386 rcu_assign_pointer(tr->function_pids, NULL);
6387
6388 /* Wait till all users are no longer using pid filtering */
6389 synchronize_sched();
6390
6391 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05006392}
6393
Namhyung Kimd879d0b2017-04-17 11:44:27 +09006394void ftrace_clear_pids(struct trace_array *tr)
6395{
6396 mutex_lock(&ftrace_lock);
6397
6398 clear_ftrace_pids(tr);
6399
6400 mutex_unlock(&ftrace_lock);
6401}
6402
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006403static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05006404{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006405 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006406 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006407
6408 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006409 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006410
6411 mutex_unlock(&ftrace_lock);
6412}
6413
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006414/* Greater than any max PID */
6415#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6416
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006417static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006418 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006419{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006420 struct trace_pid_list *pid_list;
6421 struct trace_array *tr = m->private;
6422
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006423 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006424 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006425
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006426 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006427
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006428 if (!pid_list)
6429 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6430
6431 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006432}
6433
6434static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6435{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006436 struct trace_array *tr = m->private;
6437 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6438
6439 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006440 return NULL;
6441
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006442 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006443}
6444
6445static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006446 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006447{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006448 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006449 mutex_unlock(&ftrace_lock);
6450}
6451
6452static int fpid_show(struct seq_file *m, void *v)
6453{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006454 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006455 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006456 return 0;
6457 }
6458
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006459 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006460}
6461
6462static const struct seq_operations ftrace_pid_sops = {
6463 .start = fpid_start,
6464 .next = fpid_next,
6465 .stop = fpid_stop,
6466 .show = fpid_show,
6467};
6468
6469static int
6470ftrace_pid_open(struct inode *inode, struct file *file)
6471{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006472 struct trace_array *tr = inode->i_private;
6473 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006474 int ret = 0;
6475
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006476 if (trace_array_get(tr) < 0)
6477 return -ENODEV;
6478
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006479 if ((file->f_mode & FMODE_WRITE) &&
6480 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006481 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006482
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006483 ret = seq_open(file, &ftrace_pid_sops);
6484 if (ret < 0) {
6485 trace_array_put(tr);
6486 } else {
6487 m = file->private_data;
6488 /* copy tr over to seq ops */
6489 m->private = tr;
6490 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006491
6492 return ret;
6493}
6494
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006495static void ignore_task_cpu(void *data)
6496{
6497 struct trace_array *tr = data;
6498 struct trace_pid_list *pid_list;
6499
6500 /*
6501 * This function is called by on_each_cpu() while the
6502 * event_mutex is held.
6503 */
6504 pid_list = rcu_dereference_protected(tr->function_pids,
6505 mutex_is_locked(&ftrace_lock));
6506
6507 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6508 trace_ignore_this_task(pid_list, current));
6509}
6510
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006511static ssize_t
6512ftrace_pid_write(struct file *filp, const char __user *ubuf,
6513 size_t cnt, loff_t *ppos)
6514{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006515 struct seq_file *m = filp->private_data;
6516 struct trace_array *tr = m->private;
6517 struct trace_pid_list *filtered_pids = NULL;
6518 struct trace_pid_list *pid_list;
6519 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006520
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006521 if (!cnt)
6522 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006523
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006524 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006525
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006526 filtered_pids = rcu_dereference_protected(tr->function_pids,
6527 lockdep_is_held(&ftrace_lock));
6528
6529 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6530 if (ret < 0)
6531 goto out;
6532
6533 rcu_assign_pointer(tr->function_pids, pid_list);
6534
6535 if (filtered_pids) {
6536 synchronize_sched();
6537 trace_free_pid_list(filtered_pids);
6538 } else if (pid_list) {
6539 /* Register a probe to set whether to ignore the tracing of a task */
6540 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6541 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006542
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006543 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006544 * Ignoring of pids is done at task switch. But we have to
6545 * check for those tasks that are currently running.
6546 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006547 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006548 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006549
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006550 ftrace_update_pid_func();
6551 ftrace_startup_all(0);
6552 out:
6553 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006554
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006555 if (ret > 0)
6556 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05006557
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006558 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006559}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006560
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006561static int
6562ftrace_pid_release(struct inode *inode, struct file *file)
6563{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006564 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006565
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006566 trace_array_put(tr);
6567
6568 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006569}
6570
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006571static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006572 .open = ftrace_pid_open,
6573 .write = ftrace_pid_write,
6574 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006575 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006576 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006577};
6578
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006579void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006580{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006581 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006582 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006583}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006584
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04006585void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6586 struct dentry *d_tracer)
6587{
6588 /* Only the top level directory has the dyn_tracefs and profile */
6589 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6590
6591 ftrace_init_dyn_tracefs(d_tracer);
6592 ftrace_profile_tracefs(d_tracer);
6593}
6594
Steven Rostedt3d083392008-05-12 21:20:42 +02006595/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006596 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006597 *
6598 * This function should be used by panic code. It stops ftrace
6599 * but in a not so nice way. If you need to simply kill ftrace
6600 * from a non-atomic section, use ftrace_kill.
6601 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006602void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006603{
6604 ftrace_disabled = 1;
6605 ftrace_enabled = 0;
Yisheng Xie5ccba642018-02-02 10:14:49 +08006606 ftrace_trace_function = ftrace_stub;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006607}
6608
6609/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04006610 * Test if ftrace is dead or not.
6611 */
6612int ftrace_is_dead(void)
6613{
6614 return ftrace_disabled;
6615}
6616
6617/**
Steven Rostedt3d083392008-05-12 21:20:42 +02006618 * register_ftrace_function - register a function for profiling
6619 * @ops - ops structure that holds the function for profiling.
6620 *
6621 * Register a function to be called by all functions in the
6622 * kernel.
6623 *
6624 * Note: @ops->func and all the functions it calls must be labeled
6625 * with "notrace", otherwise it will go into a
6626 * recursive loop.
6627 */
6628int register_ftrace_function(struct ftrace_ops *ops)
6629{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006630 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006631
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09006632 ftrace_ops_init(ops);
6633
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006634 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006635
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006636 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04006637
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006638 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02006639
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006640 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02006641}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006642EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02006643
6644/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01006645 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02006646 * @ops - ops structure that holds the function to unregister
6647 *
6648 * Unregister a function that was added to be called by ftrace profiling.
6649 */
6650int unregister_ftrace_function(struct ftrace_ops *ops)
6651{
6652 int ret;
6653
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006654 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006655 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006656 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006657
6658 return ret;
6659}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006660EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006661
Ingo Molnare309b412008-05-12 21:20:51 +02006662int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006663ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006664 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006665 loff_t *ppos)
6666{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006667 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006668
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006669 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006670
Steven Rostedt45a4a232011-04-21 23:16:46 -04006671 if (unlikely(ftrace_disabled))
6672 goto out;
6673
6674 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006675
Li Zefana32c7762009-06-26 16:55:51 +08006676 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006677 goto out;
6678
Li Zefana32c7762009-06-26 16:55:51 +08006679 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006680
6681 if (ftrace_enabled) {
6682
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006683 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08006684 if (rcu_dereference_protected(ftrace_ops_list,
6685 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01006686 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006687
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006688 ftrace_startup_sysctl();
6689
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006690 } else {
6691 /* stopping ftrace calls (just send to ftrace_stub) */
6692 ftrace_trace_function = ftrace_stub;
6693
6694 ftrace_shutdown_sysctl();
6695 }
6696
6697 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006698 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006699 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006700}