blob: c53533b833cf1c2bcd2d96cc40ccc39bebcab21b [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)b1b35f22018-11-20 12:51:07 -0500795 int index = current->curr_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
Namhyung Kim8861dd32016-08-31 11:55:29 +0900803 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
804 current->ret_stack[index].subtime = 0;
805
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400806 return 1;
807}
808
809static void profile_graph_return(struct ftrace_graph_ret *trace)
810{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400812 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400813 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400814 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400815
816 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500817 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400818 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 goto out;
820
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400821 /* If the calltime was zero'd ignore it */
822 if (!trace->calltime)
823 goto out;
824
Steven Rostedta2a16d62009-03-24 23:17:58 -0400825 calltime = trace->rettime - trace->calltime;
826
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400827 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400828 int index;
829
Steven Rostedt (VMware)b1b35f22018-11-20 12:51:07 -0500830 index = current->curr_ret_stack;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400831
832 /* Append this call time to the parent time to subtract */
833 if (index)
834 current->ret_stack[index - 1].subtime += calltime;
835
836 if (current->ret_stack[index].subtime < calltime)
837 calltime -= current->ret_stack[index].subtime;
838 else
839 calltime = 0;
840 }
841
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400843 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400844 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400845 rec->time_squared += calltime * calltime;
846 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400847
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400849 local_irq_restore(flags);
850}
851
852static int register_ftrace_profiler(void)
853{
854 return register_ftrace_graph(&profile_graph_return,
855 &profile_graph_entry);
856}
857
858static void unregister_ftrace_profiler(void)
859{
860 unregister_ftrace_graph();
861}
862#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100863static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400864 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900865 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400866 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400867};
868
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400869static int register_ftrace_profiler(void)
870{
871 return register_ftrace_function(&ftrace_profile_ops);
872}
873
874static void unregister_ftrace_profiler(void)
875{
876 unregister_ftrace_function(&ftrace_profile_ops);
877}
878#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
879
Steven Rostedt493762f2009-03-23 17:12:36 -0400880static ssize_t
881ftrace_profile_write(struct file *filp, const char __user *ubuf,
882 size_t cnt, loff_t *ppos)
883{
884 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400885 int ret;
886
Peter Huewe22fe9b52011-06-07 21:58:27 +0200887 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
888 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400889 return ret;
890
891 val = !!val;
892
893 mutex_lock(&ftrace_profile_lock);
894 if (ftrace_profile_enabled ^ val) {
895 if (val) {
896 ret = ftrace_profile_init();
897 if (ret < 0) {
898 cnt = ret;
899 goto out;
900 }
901
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400902 ret = register_ftrace_profiler();
903 if (ret < 0) {
904 cnt = ret;
905 goto out;
906 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400907 ftrace_profile_enabled = 1;
908 } else {
909 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400910 /*
911 * unregister_ftrace_profiler calls stop_machine
912 * so this acts like an synchronize_sched.
913 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400914 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400915 }
916 }
917 out:
918 mutex_unlock(&ftrace_profile_lock);
919
Jiri Olsacf8517c2009-10-23 19:36:16 -0400920 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400921
922 return cnt;
923}
924
925static ssize_t
926ftrace_profile_read(struct file *filp, char __user *ubuf,
927 size_t cnt, loff_t *ppos)
928{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400929 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400930 int r;
931
932 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
933 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
934}
935
936static const struct file_operations ftrace_profile_fops = {
937 .open = tracing_open_generic,
938 .read = ftrace_profile_read,
939 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200940 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400941};
942
Steven Rostedtcafb1682009-03-24 20:50:39 -0400943/* used to initialize the real stat files */
944static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400945 .name = "functions",
946 .stat_start = function_stat_start,
947 .stat_next = function_stat_next,
948 .stat_cmp = function_stat_cmp,
949 .stat_headers = function_stat_headers,
950 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400951};
952
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500953static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400954{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400955 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400956 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400957 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400958 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400959 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400960
Steven Rostedtcafb1682009-03-24 20:50:39 -0400961 for_each_possible_cpu(cpu) {
962 stat = &per_cpu(ftrace_profile_stats, cpu);
963
Geliang Tang6363c6b2016-03-15 22:12:34 +0800964 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400965 if (!name) {
966 /*
967 * The files created are permanent, if something happens
968 * we still do not free memory.
969 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400970 WARN(1,
971 "Could not allocate stat file for cpu %d\n",
972 cpu);
973 return;
974 }
975 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400976 stat->stat.name = name;
977 ret = register_stat_tracer(&stat->stat);
978 if (ret) {
979 WARN(1,
980 "Could not register function stat for cpu %d\n",
981 cpu);
982 kfree(name);
983 return;
984 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400985 }
986
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500987 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -0400988 d_tracer, NULL, &ftrace_profile_fops);
989 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700990 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -0400991}
992
993#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500994static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400995{
996}
997#endif /* CONFIG_FUNCTION_PROFILER */
998
Steven Rostedt3d083392008-05-12 21:20:42 +0200999#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001000
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001001static struct ftrace_ops *removed_ops;
1002
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001003/*
1004 * Set when doing a global update, like enabling all recs or disabling them.
1005 * It is not set when just updating a single ftrace_ops.
1006 */
1007static bool update_all_ops;
1008
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001009#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001010# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001011#endif
1012
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001013struct ftrace_func_entry {
1014 struct hlist_node hlist;
1015 unsigned long ip;
1016};
1017
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001018struct ftrace_func_probe {
1019 struct ftrace_probe_ops *probe_ops;
1020 struct ftrace_ops ops;
1021 struct trace_array *tr;
1022 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001023 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001024 int ref;
1025};
1026
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001027/*
1028 * We make these constant because no one should touch them,
1029 * but they are used as the default "empty hash", to avoid allocating
1030 * it all the time. These are in a read only section such that if
1031 * anyone does try to modify it, it will cause an exception.
1032 */
1033static const struct hlist_head empty_buckets[1];
1034static const struct ftrace_hash empty_hash = {
1035 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001036};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001037#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001038
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001039struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001040 .func = ftrace_stub,
1041 .local_hash.notrace_hash = EMPTY_HASH,
1042 .local_hash.filter_hash = EMPTY_HASH,
1043 INIT_OPS_HASH(global_ops)
1044 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001045 FTRACE_OPS_FL_INITIALIZED |
1046 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001047};
1048
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001049/*
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001050 * Used by the stack undwinder to know about dynamic ftrace trampolines.
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001051 */
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001052struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001053{
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001054 struct ftrace_ops *op = NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001055
1056 /*
1057 * Some of the ops may be dynamically allocated,
1058 * they are freed after a synchronize_sched().
1059 */
1060 preempt_disable_notrace();
1061
1062 do_for_each_ftrace_op(op, ftrace_ops_list) {
1063 /*
1064 * This is to check for dynamically allocated trampolines.
1065 * Trampolines that are in kernel text will have
1066 * core_kernel_text() return true.
1067 */
1068 if (op->trampoline && op->trampoline_size)
1069 if (addr >= op->trampoline &&
1070 addr < op->trampoline + op->trampoline_size) {
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001071 preempt_enable_notrace();
1072 return op;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001073 }
1074 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001075 preempt_enable_notrace();
1076
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001077 return NULL;
1078}
1079
1080/*
1081 * This is used by __kernel_text_address() to return true if the
1082 * address is on a dynamically allocated trampoline that would
1083 * not return true for either core_kernel_text() or
1084 * is_module_text_address().
1085 */
1086bool is_ftrace_trampoline(unsigned long addr)
1087{
1088 return ftrace_ops_trampoline(addr) != NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001089}
1090
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001091struct ftrace_page {
1092 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001093 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001094 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001095 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001096};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001097
Steven Rostedta7900872011-12-16 16:23:44 -05001098#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1099#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001100
1101/* estimate from running different kernels */
1102#define NR_TO_INIT 10000
1103
1104static struct ftrace_page *ftrace_pages_start;
1105static struct ftrace_page *ftrace_pages;
1106
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001107static __always_inline unsigned long
1108ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1109{
1110 if (hash->size_bits > 0)
1111 return hash_long(ip, hash->size_bits);
1112
1113 return 0;
1114}
1115
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001116/* Only use this function if ftrace_hash_empty() has already been tested */
1117static __always_inline struct ftrace_func_entry *
1118__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001119{
1120 unsigned long key;
1121 struct ftrace_func_entry *entry;
1122 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001124 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001125 hhd = &hash->buckets[key];
1126
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001127 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001128 if (entry->ip == ip)
1129 return entry;
1130 }
1131 return NULL;
1132}
1133
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001134/**
1135 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1136 * @hash: The hash to look at
1137 * @ip: The instruction pointer to test
1138 *
1139 * Search a given @hash to see if a given instruction pointer (@ip)
1140 * exists in it.
1141 *
1142 * Returns the entry that holds the @ip if found. NULL otherwise.
1143 */
1144struct ftrace_func_entry *
1145ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1146{
1147 if (ftrace_hash_empty(hash))
1148 return NULL;
1149
1150 return __ftrace_lookup_ip(hash, ip);
1151}
1152
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001153static void __add_hash_entry(struct ftrace_hash *hash,
1154 struct ftrace_func_entry *entry)
1155{
1156 struct hlist_head *hhd;
1157 unsigned long key;
1158
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001159 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001160 hhd = &hash->buckets[key];
1161 hlist_add_head(&entry->hlist, hhd);
1162 hash->count++;
1163}
1164
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001165static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1166{
1167 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001168
1169 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1170 if (!entry)
1171 return -ENOMEM;
1172
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001173 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001174 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001175
1176 return 0;
1177}
1178
1179static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001180free_hash_entry(struct ftrace_hash *hash,
1181 struct ftrace_func_entry *entry)
1182{
1183 hlist_del(&entry->hlist);
1184 kfree(entry);
1185 hash->count--;
1186}
1187
1188static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001189remove_hash_entry(struct ftrace_hash *hash,
1190 struct ftrace_func_entry *entry)
1191{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001192 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001193 hash->count--;
1194}
1195
1196static void ftrace_hash_clear(struct ftrace_hash *hash)
1197{
1198 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001199 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001200 struct ftrace_func_entry *entry;
1201 int size = 1 << hash->size_bits;
1202 int i;
1203
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001204 if (!hash->count)
1205 return;
1206
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001207 for (i = 0; i < size; i++) {
1208 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001210 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001211 }
1212 FTRACE_WARN_ON(hash->count);
1213}
1214
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001215static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1216{
1217 list_del(&ftrace_mod->list);
1218 kfree(ftrace_mod->module);
1219 kfree(ftrace_mod->func);
1220 kfree(ftrace_mod);
1221}
1222
1223static void clear_ftrace_mod_list(struct list_head *head)
1224{
1225 struct ftrace_mod_load *p, *n;
1226
1227 /* stack tracer isn't supported yet */
1228 if (!head)
1229 return;
1230
1231 mutex_lock(&ftrace_lock);
1232 list_for_each_entry_safe(p, n, head, list)
1233 free_ftrace_mod(p);
1234 mutex_unlock(&ftrace_lock);
1235}
1236
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001237static void free_ftrace_hash(struct ftrace_hash *hash)
1238{
1239 if (!hash || hash == EMPTY_HASH)
1240 return;
1241 ftrace_hash_clear(hash);
1242 kfree(hash->buckets);
1243 kfree(hash);
1244}
1245
Steven Rostedt07fd5512011-05-05 18:03:47 -04001246static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1247{
1248 struct ftrace_hash *hash;
1249
1250 hash = container_of(rcu, struct ftrace_hash, rcu);
1251 free_ftrace_hash(hash);
1252}
1253
1254static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1255{
1256 if (!hash || hash == EMPTY_HASH)
1257 return;
1258 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1259}
1260
Jiri Olsa5500fa52012-02-15 15:51:54 +01001261void ftrace_free_filter(struct ftrace_ops *ops)
1262{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001263 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001264 free_ftrace_hash(ops->func_hash->filter_hash);
1265 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001266}
1267
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001268static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1269{
1270 struct ftrace_hash *hash;
1271 int size;
1272
1273 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1274 if (!hash)
1275 return NULL;
1276
1277 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001278 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001279
1280 if (!hash->buckets) {
1281 kfree(hash);
1282 return NULL;
1283 }
1284
1285 hash->size_bits = size_bits;
1286
1287 return hash;
1288}
1289
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001290
1291static int ftrace_add_mod(struct trace_array *tr,
1292 const char *func, const char *module,
1293 int enable)
1294{
1295 struct ftrace_mod_load *ftrace_mod;
1296 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1297
1298 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1299 if (!ftrace_mod)
1300 return -ENOMEM;
1301
1302 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1303 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1304 ftrace_mod->enable = enable;
1305
1306 if (!ftrace_mod->func || !ftrace_mod->module)
1307 goto out_free;
1308
1309 list_add(&ftrace_mod->list, mod_head);
1310
1311 return 0;
1312
1313 out_free:
1314 free_ftrace_mod(ftrace_mod);
1315
1316 return -ENOMEM;
1317}
1318
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319static struct ftrace_hash *
1320alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1321{
1322 struct ftrace_func_entry *entry;
1323 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001324 int size;
1325 int ret;
1326 int i;
1327
1328 new_hash = alloc_ftrace_hash(size_bits);
1329 if (!new_hash)
1330 return NULL;
1331
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001332 if (hash)
1333 new_hash->flags = hash->flags;
1334
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001335 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001336 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 return new_hash;
1338
1339 size = 1 << hash->size_bits;
1340 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001341 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342 ret = add_hash_entry(new_hash, entry->ip);
1343 if (ret < 0)
1344 goto free_hash;
1345 }
1346 }
1347
1348 FTRACE_WARN_ON(new_hash->count != hash->count);
1349
1350 return new_hash;
1351
1352 free_hash:
1353 free_ftrace_hash(new_hash);
1354 return NULL;
1355}
1356
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001357static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001358ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001359static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001360ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001361
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001362static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1363 struct ftrace_hash *new_hash);
1364
Namhyung Kim3e278c02017-01-20 11:44:45 +09001365static struct ftrace_hash *
1366__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001367{
1368 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001369 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001370 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001371 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001372 int size = src->count;
1373 int bits = 0;
1374 int i;
1375
1376 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001377 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001378 */
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001379 if (ftrace_hash_empty(src))
Namhyung Kim3e278c02017-01-20 11:44:45 +09001380 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001381
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001382 /*
1383 * Make the hash size about 1/2 the # found
1384 */
1385 for (size /= 2; size; size >>= 1)
1386 bits++;
1387
1388 /* Don't allocate too much */
1389 if (bits > FTRACE_HASH_MAX_BITS)
1390 bits = FTRACE_HASH_MAX_BITS;
1391
Steven Rostedt07fd5512011-05-05 18:03:47 -04001392 new_hash = alloc_ftrace_hash(bits);
1393 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001394 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001395
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001396 new_hash->flags = src->flags;
1397
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001398 size = 1 << src->size_bits;
1399 for (i = 0; i < size; i++) {
1400 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001401 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001402 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001403 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001404 }
1405 }
1406
Namhyung Kim3e278c02017-01-20 11:44:45 +09001407 return new_hash;
1408}
1409
1410static int
1411ftrace_hash_move(struct ftrace_ops *ops, int enable,
1412 struct ftrace_hash **dst, struct ftrace_hash *src)
1413{
1414 struct ftrace_hash *new_hash;
1415 int ret;
1416
1417 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1418 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1419 return -EINVAL;
1420
1421 new_hash = __ftrace_hash_move(src);
1422 if (!new_hash)
1423 return -ENOMEM;
1424
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001425 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1426 if (enable) {
1427 /* IPMODIFY should be updated only when filter_hash updating */
1428 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1429 if (ret < 0) {
1430 free_ftrace_hash(new_hash);
1431 return ret;
1432 }
1433 }
1434
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001435 /*
1436 * Remove the current set, update the hash and add
1437 * them back.
1438 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001439 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001440
Steven Rostedt07fd5512011-05-05 18:03:47 -04001441 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001442
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001443 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001444
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001445 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001446}
1447
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001448static bool hash_contains_ip(unsigned long ip,
1449 struct ftrace_ops_hash *hash)
1450{
1451 /*
1452 * The function record is a match if it exists in the filter
1453 * hash and not in the notrace hash. Note, an emty hash is
1454 * considered a match for the filter hash, but an empty
1455 * notrace hash is considered not in the notrace hash.
1456 */
1457 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001458 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001459 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001460 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001461}
1462
Steven Rostedt265c8312009-02-13 12:43:56 -05001463/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001464 * Test the hashes for this ops to see if we want to call
1465 * the ops->func or not.
1466 *
1467 * It's a match if the ip is in the ops->filter_hash or
1468 * the filter_hash does not exist or is empty,
1469 * AND
1470 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001471 *
1472 * This needs to be called with preemption disabled as
1473 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001474 */
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001475int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001476ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001477{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001478 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001479 int ret;
1480
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001481#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1482 /*
1483 * There's a small race when adding ops that the ftrace handler
1484 * that wants regs, may be called without them. We can not
1485 * allow that handler to be called if regs is NULL.
1486 */
1487 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1488 return 0;
1489#endif
1490
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001491 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1492 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001493
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001494 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001495 ret = 1;
1496 else
1497 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001498
1499 return ret;
1500}
1501
1502/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001503 * This is a double for. Do not use 'break' to break out of the loop,
1504 * you must use a goto.
1505 */
1506#define do_for_each_ftrace_rec(pg, rec) \
1507 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1508 int _____i; \
1509 for (_____i = 0; _____i < pg->index; _____i++) { \
1510 rec = &pg->records[_____i];
1511
1512#define while_for_each_ftrace_rec() \
1513 } \
1514 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301515
Steven Rostedt5855fea2011-12-16 19:27:42 -05001516
1517static int ftrace_cmp_recs(const void *a, const void *b)
1518{
Steven Rostedta650e022012-04-25 13:48:13 -04001519 const struct dyn_ftrace *key = a;
1520 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001521
Steven Rostedta650e022012-04-25 13:48:13 -04001522 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001523 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001524 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1525 return 1;
1526 return 0;
1527}
1528
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001529/**
1530 * ftrace_location_range - return the first address of a traced location
1531 * if it touches the given ip range
1532 * @start: start of range to search.
1533 * @end: end of range to search (inclusive). @end points to the last byte
1534 * to check.
1535 *
1536 * Returns rec->ip if the related ftrace location is a least partly within
1537 * the given address range. That is, the first address of the instruction
1538 * that is either a NOP or call to the function tracer. It checks the ftrace
1539 * internal tables to determine if the address belongs or not.
1540 */
1541unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001542{
1543 struct ftrace_page *pg;
1544 struct dyn_ftrace *rec;
1545 struct dyn_ftrace key;
1546
1547 key.ip = start;
1548 key.flags = end; /* overload flags, as it is unsigned long */
1549
1550 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1551 if (end < pg->records[0].ip ||
1552 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1553 continue;
1554 rec = bsearch(&key, pg->records, pg->index,
1555 sizeof(struct dyn_ftrace),
1556 ftrace_cmp_recs);
1557 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001558 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001559 }
1560
Steven Rostedt5855fea2011-12-16 19:27:42 -05001561 return 0;
1562}
1563
Steven Rostedtc88fd862011-08-16 09:53:39 -04001564/**
1565 * ftrace_location - return true if the ip giving is a traced location
1566 * @ip: the instruction pointer to check
1567 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001568 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001569 * That is, the instruction that is either a NOP or call to
1570 * the function tracer. It checks the ftrace internal tables to
1571 * determine if the address belongs or not.
1572 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001573unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001574{
Steven Rostedta650e022012-04-25 13:48:13 -04001575 return ftrace_location_range(ip, ip);
1576}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001577
Steven Rostedta650e022012-04-25 13:48:13 -04001578/**
1579 * ftrace_text_reserved - return true if range contains an ftrace location
1580 * @start: start of range to search
1581 * @end: end of range to search (inclusive). @end points to the last byte to check.
1582 *
1583 * Returns 1 if @start and @end contains a ftrace location.
1584 * That is, the instruction that is either a NOP or call to
1585 * the function tracer. It checks the ftrace internal tables to
1586 * determine if the address belongs or not.
1587 */
Sasha Levind88471c2013-01-09 18:09:20 -05001588int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001589{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001590 unsigned long ret;
1591
1592 ret = ftrace_location_range((unsigned long)start,
1593 (unsigned long)end);
1594
1595 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001596}
1597
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001598/* Test if ops registered to this rec needs regs */
1599static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1600{
1601 struct ftrace_ops *ops;
1602 bool keep_regs = false;
1603
1604 for (ops = ftrace_ops_list;
1605 ops != &ftrace_list_end; ops = ops->next) {
1606 /* pass rec in as regs to have non-NULL val */
1607 if (ftrace_ops_test(ops, rec->ip, rec)) {
1608 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1609 keep_regs = true;
1610 break;
1611 }
1612 }
1613 }
1614
1615 return keep_regs;
1616}
1617
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001618static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001619 int filter_hash,
1620 bool inc)
1621{
1622 struct ftrace_hash *hash;
1623 struct ftrace_hash *other_hash;
1624 struct ftrace_page *pg;
1625 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001626 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001627 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001628 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001629
1630 /* Only update if the ops has been registered */
1631 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001632 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001633
1634 /*
1635 * In the filter_hash case:
1636 * If the count is zero, we update all records.
1637 * Otherwise we just update the items in the hash.
1638 *
1639 * In the notrace_hash case:
1640 * We enable the update in the hash.
1641 * As disabling notrace means enabling the tracing,
1642 * and enabling notrace means disabling, the inc variable
1643 * gets inversed.
1644 */
1645 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001646 hash = ops->func_hash->filter_hash;
1647 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001648 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001649 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001650 } else {
1651 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001652 hash = ops->func_hash->notrace_hash;
1653 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001654 /*
1655 * If the notrace hash has no items,
1656 * then there's nothing to do.
1657 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001658 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001659 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001660 }
1661
1662 do_for_each_ftrace_rec(pg, rec) {
1663 int in_other_hash = 0;
1664 int in_hash = 0;
1665 int match = 0;
1666
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001667 if (rec->flags & FTRACE_FL_DISABLED)
1668 continue;
1669
Steven Rostedted926f92011-05-03 13:25:24 -04001670 if (all) {
1671 /*
1672 * Only the filter_hash affects all records.
1673 * Update if the record is not in the notrace hash.
1674 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001675 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001676 match = 1;
1677 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001678 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1679 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001680
1681 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001682 * If filter_hash is set, we want to match all functions
1683 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001684 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001685 * If filter_hash is not set, then we are decrementing.
1686 * That means we match anything that is in the hash
1687 * and also in the other_hash. That is, we need to turn
1688 * off functions in the other hash because they are disabled
1689 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001690 */
1691 if (filter_hash && in_hash && !in_other_hash)
1692 match = 1;
1693 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001694 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001695 match = 1;
1696 }
1697 if (!match)
1698 continue;
1699
1700 if (inc) {
1701 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001702 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001703 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001704
1705 /*
1706 * If there's only a single callback registered to a
1707 * function, and the ops has a trampoline registered
1708 * for it, then we can call it directly.
1709 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001710 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001711 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001712 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001713 /*
1714 * If we are adding another function callback
1715 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001716 * custom trampoline in use, then we need to go
1717 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001718 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001719 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001720
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001721 /*
1722 * If any ops wants regs saved for this function
1723 * then all ops will get saved regs.
1724 */
1725 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1726 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001727 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001728 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001729 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001730 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001731
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001732 /*
1733 * If the rec had REGS enabled and the ops that is
1734 * being removed had REGS set, then see if there is
1735 * still any ops for this record that wants regs.
1736 * If not, we can stop recording them.
1737 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001738 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001739 rec->flags & FTRACE_FL_REGS &&
1740 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1741 if (!test_rec_ops_needs_regs(rec))
1742 rec->flags &= ~FTRACE_FL_REGS;
1743 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001744
1745 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001746 * If the rec had TRAMP enabled, then it needs to
1747 * be cleared. As TRAMP can only be enabled iff
1748 * there is only a single ops attached to it.
1749 * In otherwords, always disable it on decrementing.
1750 * In the future, we may set it if rec count is
1751 * decremented to one, and the ops that is left
1752 * has a trampoline.
1753 */
1754 rec->flags &= ~FTRACE_FL_TRAMP;
1755
1756 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001757 * flags will be cleared in ftrace_check_record()
1758 * if rec count is zero.
1759 */
Steven Rostedted926f92011-05-03 13:25:24 -04001760 }
1761 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001762
1763 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1764 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1765
Steven Rostedted926f92011-05-03 13:25:24 -04001766 /* Shortcut, if we handled all records, we are done. */
1767 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001768 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001769 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001770
1771 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001772}
1773
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001774static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001775 int filter_hash)
1776{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001777 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001778}
1779
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001780static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001781 int filter_hash)
1782{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001783 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001784}
1785
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001786static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1787 int filter_hash, int inc)
1788{
1789 struct ftrace_ops *op;
1790
1791 __ftrace_hash_rec_update(ops, filter_hash, inc);
1792
1793 if (ops->func_hash != &global_ops.local_hash)
1794 return;
1795
1796 /*
1797 * If the ops shares the global_ops hash, then we need to update
1798 * all ops that are enabled and use this hash.
1799 */
1800 do_for_each_ftrace_op(op, ftrace_ops_list) {
1801 /* Already done */
1802 if (op == ops)
1803 continue;
1804 if (op->func_hash == &global_ops.local_hash)
1805 __ftrace_hash_rec_update(op, filter_hash, inc);
1806 } while_for_each_ftrace_op(op);
1807}
1808
1809static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1810 int filter_hash)
1811{
1812 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1813}
1814
1815static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1816 int filter_hash)
1817{
1818 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1819}
1820
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001821/*
1822 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1823 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1824 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1825 * Note that old_hash and new_hash has below meanings
1826 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1827 * - If the hash is EMPTY_HASH, it hits nothing
1828 * - Anything else hits the recs which match the hash entries.
1829 */
1830static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1831 struct ftrace_hash *old_hash,
1832 struct ftrace_hash *new_hash)
1833{
1834 struct ftrace_page *pg;
1835 struct dyn_ftrace *rec, *end = NULL;
1836 int in_old, in_new;
1837
1838 /* Only update if the ops has been registered */
1839 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1840 return 0;
1841
1842 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1843 return 0;
1844
1845 /*
1846 * Since the IPMODIFY is a very address sensitive action, we do not
1847 * allow ftrace_ops to set all functions to new hash.
1848 */
1849 if (!new_hash || !old_hash)
1850 return -EINVAL;
1851
1852 /* Update rec->flags */
1853 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001854
1855 if (rec->flags & FTRACE_FL_DISABLED)
1856 continue;
1857
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001858 /* We need to update only differences of filter_hash */
1859 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1860 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1861 if (in_old == in_new)
1862 continue;
1863
1864 if (in_new) {
1865 /* New entries must ensure no others are using it */
1866 if (rec->flags & FTRACE_FL_IPMODIFY)
1867 goto rollback;
1868 rec->flags |= FTRACE_FL_IPMODIFY;
1869 } else /* Removed entry */
1870 rec->flags &= ~FTRACE_FL_IPMODIFY;
1871 } while_for_each_ftrace_rec();
1872
1873 return 0;
1874
1875rollback:
1876 end = rec;
1877
1878 /* Roll back what we did above */
1879 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001880
1881 if (rec->flags & FTRACE_FL_DISABLED)
1882 continue;
1883
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001884 if (rec == end)
1885 goto err_out;
1886
1887 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1888 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1889 if (in_old == in_new)
1890 continue;
1891
1892 if (in_new)
1893 rec->flags &= ~FTRACE_FL_IPMODIFY;
1894 else
1895 rec->flags |= FTRACE_FL_IPMODIFY;
1896 } while_for_each_ftrace_rec();
1897
1898err_out:
1899 return -EBUSY;
1900}
1901
1902static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1903{
1904 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1905
1906 if (ftrace_hash_empty(hash))
1907 hash = NULL;
1908
1909 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1910}
1911
1912/* Disabling always succeeds */
1913static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1914{
1915 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1916
1917 if (ftrace_hash_empty(hash))
1918 hash = NULL;
1919
1920 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1921}
1922
1923static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1924 struct ftrace_hash *new_hash)
1925{
1926 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1927
1928 if (ftrace_hash_empty(old_hash))
1929 old_hash = NULL;
1930
1931 if (ftrace_hash_empty(new_hash))
1932 new_hash = NULL;
1933
1934 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1935}
1936
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001937static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001938{
1939 int i;
1940
1941 printk(KERN_CONT "%s", fmt);
1942
1943 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1944 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1945}
1946
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001947static struct ftrace_ops *
1948ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001949static struct ftrace_ops *
1950ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001951
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001952enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001953const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001954
1955static void print_bug_type(void)
1956{
1957 switch (ftrace_bug_type) {
1958 case FTRACE_BUG_UNKNOWN:
1959 break;
1960 case FTRACE_BUG_INIT:
1961 pr_info("Initializing ftrace call sites\n");
1962 break;
1963 case FTRACE_BUG_NOP:
1964 pr_info("Setting ftrace call site to NOP\n");
1965 break;
1966 case FTRACE_BUG_CALL:
1967 pr_info("Setting ftrace call site to call ftrace function\n");
1968 break;
1969 case FTRACE_BUG_UPDATE:
1970 pr_info("Updating ftrace call site to call a different ftrace function\n");
1971 break;
1972 }
1973}
1974
Steven Rostedtc88fd862011-08-16 09:53:39 -04001975/**
1976 * ftrace_bug - report and shutdown function tracer
1977 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001978 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001979 *
1980 * The arch code that enables or disables the function tracing
1981 * can call ftrace_bug() when it has detected a problem in
1982 * modifying the code. @failed should be one of either:
1983 * EFAULT - if the problem happens on reading the @ip address
1984 * EINVAL - if what is read at @ip is not what was expected
1985 * EPERM - if the problem happens on writting to the @ip address
1986 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001987void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001988{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001989 unsigned long ip = rec ? rec->ip : 0;
1990
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001991 switch (failed) {
1992 case -EFAULT:
1993 FTRACE_WARN_ON_ONCE(1);
1994 pr_info("ftrace faulted on modifying ");
1995 print_ip_sym(ip);
1996 break;
1997 case -EINVAL:
1998 FTRACE_WARN_ON_ONCE(1);
1999 pr_info("ftrace failed to modify ");
2000 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002001 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002002 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002003 if (ftrace_expected) {
2004 print_ip_ins(" expected: ", ftrace_expected);
2005 pr_cont("\n");
2006 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002007 break;
2008 case -EPERM:
2009 FTRACE_WARN_ON_ONCE(1);
2010 pr_info("ftrace faulted on writing ");
2011 print_ip_sym(ip);
2012 break;
2013 default:
2014 FTRACE_WARN_ON_ONCE(1);
2015 pr_info("ftrace faulted on unknown error ");
2016 print_ip_sym(ip);
2017 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002018 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002019 if (rec) {
2020 struct ftrace_ops *ops = NULL;
2021
2022 pr_info("ftrace record flags: %lx\n", rec->flags);
2023 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2024 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2025 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2026 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002027 if (ops) {
2028 do {
2029 pr_cont("\ttramp: %pS (%pS)",
2030 (void *)ops->trampoline,
2031 (void *)ops->func);
2032 ops = ftrace_find_tramp_ops_next(rec, ops);
2033 } while (ops);
2034 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002035 pr_cont("\ttramp: ERROR!");
2036
2037 }
2038 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002039 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002040 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002041}
2042
Steven Rostedtc88fd862011-08-16 09:53:39 -04002043static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002044{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002045 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002046
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002047 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2048
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002049 if (rec->flags & FTRACE_FL_DISABLED)
2050 return FTRACE_UPDATE_IGNORE;
2051
Steven Rostedt982c3502008-11-15 16:31:41 -05002052 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002053 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002054 *
Steven Rostedted926f92011-05-03 13:25:24 -04002055 * If the record has a ref count, then we need to enable it
2056 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002057 *
Steven Rostedted926f92011-05-03 13:25:24 -04002058 * Otherwise we make sure its disabled.
2059 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002060 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002061 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002062 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002063 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002064 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002065
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002066 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002067 * If enabling and the REGS flag does not match the REGS_EN, or
2068 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2069 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002070 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002071 if (flag) {
2072 if (!(rec->flags & FTRACE_FL_REGS) !=
2073 !(rec->flags & FTRACE_FL_REGS_EN))
2074 flag |= FTRACE_FL_REGS;
2075
2076 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2077 !(rec->flags & FTRACE_FL_TRAMP_EN))
2078 flag |= FTRACE_FL_TRAMP;
2079 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002080
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002081 /* If the state of this record hasn't changed, then do nothing */
2082 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002083 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002084
2085 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002086 /* Save off if rec is being enabled (for return value) */
2087 flag ^= rec->flags & FTRACE_FL_ENABLED;
2088
2089 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002090 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002091 if (flag & FTRACE_FL_REGS) {
2092 if (rec->flags & FTRACE_FL_REGS)
2093 rec->flags |= FTRACE_FL_REGS_EN;
2094 else
2095 rec->flags &= ~FTRACE_FL_REGS_EN;
2096 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002097 if (flag & FTRACE_FL_TRAMP) {
2098 if (rec->flags & FTRACE_FL_TRAMP)
2099 rec->flags |= FTRACE_FL_TRAMP_EN;
2100 else
2101 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2102 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002103 }
2104
2105 /*
2106 * If this record is being updated from a nop, then
2107 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002108 * Otherwise,
2109 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002110 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002111 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002112 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002113 if (flag & FTRACE_FL_ENABLED) {
2114 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002115 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002116 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002117
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002118 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002119 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002120 }
2121
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002122 if (update) {
2123 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002124 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002125 rec->flags = 0;
2126 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002127 /*
2128 * Just disable the record, but keep the ops TRAMP
2129 * and REGS states. The _EN flags must be disabled though.
2130 */
2131 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2132 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002133 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002134
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002135 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002136 return FTRACE_UPDATE_MAKE_NOP;
2137}
2138
2139/**
2140 * ftrace_update_record, set a record that now is tracing or not
2141 * @rec: the record to update
2142 * @enable: set to 1 if the record is tracing, zero to force disable
2143 *
2144 * The records that represent all functions that can be traced need
2145 * to be updated when tracing has been enabled.
2146 */
2147int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2148{
2149 return ftrace_check_record(rec, enable, 1);
2150}
2151
2152/**
2153 * ftrace_test_record, check if the record has been enabled or not
2154 * @rec: the record to test
2155 * @enable: set to 1 to check if enabled, 0 if it is disabled
2156 *
2157 * The arch code may need to test if a record is already set to
2158 * tracing to determine how to modify the function code that it
2159 * represents.
2160 */
2161int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2162{
2163 return ftrace_check_record(rec, enable, 0);
2164}
2165
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002166static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002167ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2168{
2169 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002170 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002171
2172 do_for_each_ftrace_op(op, ftrace_ops_list) {
2173
2174 if (!op->trampoline)
2175 continue;
2176
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002177 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002178 return op;
2179 } while_for_each_ftrace_op(op);
2180
2181 return NULL;
2182}
2183
2184static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002185ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2186 struct ftrace_ops *op)
2187{
2188 unsigned long ip = rec->ip;
2189
2190 while_for_each_ftrace_op(op) {
2191
2192 if (!op->trampoline)
2193 continue;
2194
2195 if (hash_contains_ip(ip, op->func_hash))
2196 return op;
2197 }
2198
2199 return NULL;
2200}
2201
2202static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002203ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2204{
2205 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002206 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002207
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002208 /*
2209 * Need to check removed ops first.
2210 * If they are being removed, and this rec has a tramp,
2211 * and this rec is in the ops list, then it would be the
2212 * one with the tramp.
2213 */
2214 if (removed_ops) {
2215 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002216 return removed_ops;
2217 }
2218
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002219 /*
2220 * Need to find the current trampoline for a rec.
2221 * Now, a trampoline is only attached to a rec if there
2222 * was a single 'ops' attached to it. But this can be called
2223 * when we are adding another op to the rec or removing the
2224 * current one. Thus, if the op is being added, we can
2225 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002226 * yet.
2227 *
2228 * If an ops is being modified (hooking to different functions)
2229 * then we don't care about the new functions that are being
2230 * added, just the old ones (that are probably being removed).
2231 *
2232 * If we are adding an ops to a function that already is using
2233 * a trampoline, it needs to be removed (trampolines are only
2234 * for single ops connected), then an ops that is not being
2235 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002236 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002237 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002238
2239 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002240 continue;
2241
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002242 /*
2243 * If the ops is being added, it hasn't gotten to
2244 * the point to be removed from this tree yet.
2245 */
2246 if (op->flags & FTRACE_OPS_FL_ADDING)
2247 continue;
2248
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002249
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002250 /*
2251 * If the ops is being modified and is in the old
2252 * hash, then it is probably being removed from this
2253 * function.
2254 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002255 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2256 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002257 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002258 /*
2259 * If the ops is not being added or modified, and it's
2260 * in its normal filter hash, then this must be the one
2261 * we want!
2262 */
2263 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2264 hash_contains_ip(ip, op->func_hash))
2265 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002266
2267 } while_for_each_ftrace_op(op);
2268
2269 return NULL;
2270}
2271
2272static struct ftrace_ops *
2273ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2274{
2275 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002276 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002277
2278 do_for_each_ftrace_op(op, ftrace_ops_list) {
2279 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002280 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002281 return op;
2282 } while_for_each_ftrace_op(op);
2283
2284 return NULL;
2285}
2286
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002287/**
2288 * ftrace_get_addr_new - Get the call address to set to
2289 * @rec: The ftrace record descriptor
2290 *
2291 * If the record has the FTRACE_FL_REGS set, that means that it
2292 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2293 * is not not set, then it wants to convert to the normal callback.
2294 *
2295 * Returns the address of the trampoline to set to
2296 */
2297unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2298{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002299 struct ftrace_ops *ops;
2300
2301 /* Trampolines take precedence over regs */
2302 if (rec->flags & FTRACE_FL_TRAMP) {
2303 ops = ftrace_find_tramp_ops_new(rec);
2304 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002305 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2306 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002307 /* Ftrace is shutting down, return anything */
2308 return (unsigned long)FTRACE_ADDR;
2309 }
2310 return ops->trampoline;
2311 }
2312
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002313 if (rec->flags & FTRACE_FL_REGS)
2314 return (unsigned long)FTRACE_REGS_ADDR;
2315 else
2316 return (unsigned long)FTRACE_ADDR;
2317}
2318
2319/**
2320 * ftrace_get_addr_curr - Get the call address that is already there
2321 * @rec: The ftrace record descriptor
2322 *
2323 * The FTRACE_FL_REGS_EN is set when the record already points to
2324 * a function that saves all the regs. Basically the '_EN' version
2325 * represents the current state of the function.
2326 *
2327 * Returns the address of the trampoline that is currently being called
2328 */
2329unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2330{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002331 struct ftrace_ops *ops;
2332
2333 /* Trampolines take precedence over regs */
2334 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2335 ops = ftrace_find_tramp_ops_curr(rec);
2336 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002337 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2338 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002339 /* Ftrace is shutting down, return anything */
2340 return (unsigned long)FTRACE_ADDR;
2341 }
2342 return ops->trampoline;
2343 }
2344
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002345 if (rec->flags & FTRACE_FL_REGS_EN)
2346 return (unsigned long)FTRACE_REGS_ADDR;
2347 else
2348 return (unsigned long)FTRACE_ADDR;
2349}
2350
Steven Rostedtc88fd862011-08-16 09:53:39 -04002351static int
2352__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2353{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002354 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002355 unsigned long ftrace_addr;
2356 int ret;
2357
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002358 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002359
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002360 /* This needs to be done before we call ftrace_update_record */
2361 ftrace_old_addr = ftrace_get_addr_curr(rec);
2362
2363 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002364
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002365 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2366
Steven Rostedtc88fd862011-08-16 09:53:39 -04002367 switch (ret) {
2368 case FTRACE_UPDATE_IGNORE:
2369 return 0;
2370
2371 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002372 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002373 return ftrace_make_call(rec, ftrace_addr);
2374
2375 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002376 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002377 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002378
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002379 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002380 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002381 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002382 }
2383
2384 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002385}
2386
Steven Rostedte4f5d542012-04-27 09:13:18 -04002387void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002388{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002389 struct dyn_ftrace *rec;
2390 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002391 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002392
Steven Rostedt45a4a232011-04-21 23:16:46 -04002393 if (unlikely(ftrace_disabled))
2394 return;
2395
Steven Rostedt265c8312009-02-13 12:43:56 -05002396 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002397
2398 if (rec->flags & FTRACE_FL_DISABLED)
2399 continue;
2400
Steven Rostedte4f5d542012-04-27 09:13:18 -04002401 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002402 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002403 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002404 /* Stop processing */
2405 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002406 }
2407 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002408}
2409
Steven Rostedtc88fd862011-08-16 09:53:39 -04002410struct ftrace_rec_iter {
2411 struct ftrace_page *pg;
2412 int index;
2413};
2414
2415/**
2416 * ftrace_rec_iter_start, start up iterating over traced functions
2417 *
2418 * Returns an iterator handle that is used to iterate over all
2419 * the records that represent address locations where functions
2420 * are traced.
2421 *
2422 * May return NULL if no records are available.
2423 */
2424struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2425{
2426 /*
2427 * We only use a single iterator.
2428 * Protected by the ftrace_lock mutex.
2429 */
2430 static struct ftrace_rec_iter ftrace_rec_iter;
2431 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2432
2433 iter->pg = ftrace_pages_start;
2434 iter->index = 0;
2435
2436 /* Could have empty pages */
2437 while (iter->pg && !iter->pg->index)
2438 iter->pg = iter->pg->next;
2439
2440 if (!iter->pg)
2441 return NULL;
2442
2443 return iter;
2444}
2445
2446/**
2447 * ftrace_rec_iter_next, get the next record to process.
2448 * @iter: The handle to the iterator.
2449 *
2450 * Returns the next iterator after the given iterator @iter.
2451 */
2452struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2453{
2454 iter->index++;
2455
2456 if (iter->index >= iter->pg->index) {
2457 iter->pg = iter->pg->next;
2458 iter->index = 0;
2459
2460 /* Could have empty pages */
2461 while (iter->pg && !iter->pg->index)
2462 iter->pg = iter->pg->next;
2463 }
2464
2465 if (!iter->pg)
2466 return NULL;
2467
2468 return iter;
2469}
2470
2471/**
2472 * ftrace_rec_iter_record, get the record at the iterator location
2473 * @iter: The current iterator location
2474 *
2475 * Returns the record that the current @iter is at.
2476 */
2477struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2478{
2479 return &iter->pg->records[iter->index];
2480}
2481
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302482static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002483ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002484{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002485 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002486
Steven Rostedt45a4a232011-04-21 23:16:46 -04002487 if (unlikely(ftrace_disabled))
2488 return 0;
2489
Shaohua Li25aac9d2009-01-09 11:29:40 +08002490 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002491 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002492 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002493 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302494 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002495 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302496 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002497}
2498
Steven Rostedt000ab692009-02-17 13:35:06 -05002499/*
2500 * archs can override this function if they must do something
2501 * before the modifying code is performed.
2502 */
2503int __weak ftrace_arch_code_modify_prepare(void)
2504{
2505 return 0;
2506}
2507
2508/*
2509 * archs can override this function if they must do something
2510 * after the modifying code is performed.
2511 */
2512int __weak ftrace_arch_code_modify_post_process(void)
2513{
2514 return 0;
2515}
2516
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002517void ftrace_modify_all_code(int command)
2518{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002519 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002520 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002521
2522 /*
2523 * If the ftrace_caller calls a ftrace_ops func directly,
2524 * we need to make sure that it only traces functions it
2525 * expects to trace. When doing the switch of functions,
2526 * we need to update to the ftrace_ops_list_func first
2527 * before the transition between old and new calls are set,
2528 * as the ftrace_ops_list_func will check the ops hashes
2529 * to make sure the ops are having the right functions
2530 * traced.
2531 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002532 if (update) {
2533 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2534 if (FTRACE_WARN_ON(err))
2535 return;
2536 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002537
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002538 if (command & FTRACE_UPDATE_CALLS)
2539 ftrace_replace_code(1);
2540 else if (command & FTRACE_DISABLE_CALLS)
2541 ftrace_replace_code(0);
2542
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002543 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2544 function_trace_op = set_function_trace_op;
2545 smp_wmb();
2546 /* If irqs are disabled, we are in stop machine */
2547 if (!irqs_disabled())
2548 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002549 err = ftrace_update_ftrace_func(ftrace_trace_function);
2550 if (FTRACE_WARN_ON(err))
2551 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002552 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002553
2554 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002555 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002556 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002557 err = ftrace_disable_ftrace_graph_caller();
2558 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002559}
2560
Ingo Molnare309b412008-05-12 21:20:51 +02002561static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002562{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002563 int *command = data;
2564
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002565 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002566
Steven Rostedtc88fd862011-08-16 09:53:39 -04002567 return 0;
2568}
2569
2570/**
2571 * ftrace_run_stop_machine, go back to the stop machine method
2572 * @command: The command to tell ftrace what to do
2573 *
2574 * If an arch needs to fall back to the stop machine method, the
2575 * it can call this function.
2576 */
2577void ftrace_run_stop_machine(int command)
2578{
2579 stop_machine(__ftrace_modify_code, &command, NULL);
2580}
2581
2582/**
2583 * arch_ftrace_update_code, modify the code to trace or not trace
2584 * @command: The command that needs to be done
2585 *
2586 * Archs can override this function if it does not need to
2587 * run stop_machine() to modify code.
2588 */
2589void __weak arch_ftrace_update_code(int command)
2590{
2591 ftrace_run_stop_machine(command);
2592}
2593
2594static void ftrace_run_update_code(int command)
2595{
2596 int ret;
2597
2598 ret = ftrace_arch_code_modify_prepare();
2599 FTRACE_WARN_ON(ret);
2600 if (ret)
2601 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002602
2603 /*
2604 * By default we use stop_machine() to modify the code.
2605 * But archs can do what ever they want as long as it
2606 * is safe. The stop_machine() is the safest, but also
2607 * produces the most overhead.
2608 */
2609 arch_ftrace_update_code(command);
2610
Steven Rostedt000ab692009-02-17 13:35:06 -05002611 ret = ftrace_arch_code_modify_post_process();
2612 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002613}
2614
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002615static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002616 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002617{
2618 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002619 ops->old_hash.filter_hash = old_hash->filter_hash;
2620 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002621 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002622 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002623 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002624 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2625}
2626
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002627static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002628static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002629
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002630void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2631{
2632}
2633
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002634static void ftrace_startup_enable(int command)
2635{
2636 if (saved_ftrace_func != ftrace_trace_function) {
2637 saved_ftrace_func = ftrace_trace_function;
2638 command |= FTRACE_UPDATE_TRACE_FUNC;
2639 }
2640
2641 if (!command || !ftrace_enabled)
2642 return;
2643
2644 ftrace_run_update_code(command);
2645}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002646
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002647static void ftrace_startup_all(int command)
2648{
2649 update_all_ops = true;
2650 ftrace_startup_enable(command);
2651 update_all_ops = false;
2652}
2653
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002654int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002655{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002656 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002657
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002658 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002659 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002660
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002661 ret = __register_ftrace_function(ops);
2662 if (ret)
2663 return ret;
2664
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002665 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002666
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002667 /*
2668 * Note that ftrace probes uses this to start up
2669 * and modify functions it will probe. But we still
2670 * set the ADDING flag for modification, as probes
2671 * do not have trampolines. If they add them in the
2672 * future, then the probes will need to distinguish
2673 * between adding and updating probes.
2674 */
2675 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002676
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002677 ret = ftrace_hash_ipmodify_enable(ops);
2678 if (ret < 0) {
2679 /* Rollback registration process */
2680 __unregister_ftrace_function(ops);
2681 ftrace_start_up--;
2682 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2683 return ret;
2684 }
2685
Jiri Olsa7f50d062016-03-16 15:34:33 +01002686 if (ftrace_hash_rec_enable(ops, 1))
2687 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002688
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002689 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002690
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002691 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2692
Steven Rostedta1cd6172011-05-23 15:24:25 -04002693 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002694}
2695
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002696int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002697{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002698 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002699
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002700 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002701 return -ENODEV;
2702
2703 ret = __unregister_ftrace_function(ops);
2704 if (ret)
2705 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002706
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002707 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002708 /*
2709 * Just warn in case of unbalance, no need to kill ftrace, it's not
2710 * critical but the ftrace_call callers may be never nopped again after
2711 * further ftrace uses.
2712 */
2713 WARN_ON_ONCE(ftrace_start_up < 0);
2714
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002715 /* Disabling ipmodify never fails */
2716 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002717
2718 if (ftrace_hash_rec_disable(ops, 1))
2719 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002720
Namhyung Kima737e6d2014-06-12 23:56:12 +09002721 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002722
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002723 if (saved_ftrace_func != ftrace_trace_function) {
2724 saved_ftrace_func = ftrace_trace_function;
2725 command |= FTRACE_UPDATE_TRACE_FUNC;
2726 }
2727
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002728 if (!command || !ftrace_enabled) {
2729 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002730 * If these are dynamic or per_cpu ops, they still
2731 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002732 * not currently active, we can just free them
2733 * without synchronizing all CPUs.
2734 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002735 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002736 goto free_ops;
2737
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002738 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002739 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002740
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002741 /*
2742 * If the ops uses a trampoline, then it needs to be
2743 * tested first on update.
2744 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002745 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002746 removed_ops = ops;
2747
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002748 /* The trampoline logic checks the old hashes */
2749 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2750 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2751
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002752 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002753
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002754 /*
2755 * If there's no more ops registered with ftrace, run a
2756 * sanity check to make sure all rec flags are cleared.
2757 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002758 if (rcu_dereference_protected(ftrace_ops_list,
2759 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002760 struct ftrace_page *pg;
2761 struct dyn_ftrace *rec;
2762
2763 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002764 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002765 pr_warn(" %pS flags:%lx\n",
2766 (void *)rec->ip, rec->flags);
2767 } while_for_each_ftrace_rec();
2768 }
2769
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002770 ops->old_hash.filter_hash = NULL;
2771 ops->old_hash.notrace_hash = NULL;
2772
2773 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002774 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002775
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002776 /*
2777 * Dynamic ops may be freed, we must make sure that all
2778 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002779 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002780 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002781 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002782 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002783 /*
2784 * We need to do a hard force of sched synchronization.
2785 * This is because we use preempt_disable() to do RCU, but
2786 * the function tracers can be called where RCU is not watching
2787 * (like before user_exit()). We can not rely on the RCU
2788 * infrastructure to do the synchronization, thus we must do it
2789 * ourselves.
2790 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002791 schedule_on_each_cpu(ftrace_sync);
2792
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002793 /*
2794 * When the kernel is preeptive, tasks can be preempted
2795 * while on a ftrace trampoline. Just scheduling a task on
2796 * a CPU is not good enough to flush them. Calling
2797 * synchornize_rcu_tasks() will wait for those tasks to
2798 * execute and either schedule voluntarily or enter user space.
2799 */
2800 if (IS_ENABLED(CONFIG_PREEMPT))
2801 synchronize_rcu_tasks();
2802
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002803 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002804 arch_ftrace_trampoline_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002805 }
2806
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002807 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002808}
2809
Ingo Molnare309b412008-05-12 21:20:51 +02002810static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002811{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302812 int command;
2813
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002814 if (unlikely(ftrace_disabled))
2815 return;
2816
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002817 /* Force update next time */
2818 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002819 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302820 if (ftrace_start_up) {
2821 command = FTRACE_UPDATE_CALLS;
2822 if (ftrace_graph_active)
2823 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002824 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302825 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002826}
2827
Ingo Molnare309b412008-05-12 21:20:51 +02002828static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002829{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302830 int command;
2831
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002832 if (unlikely(ftrace_disabled))
2833 return;
2834
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002835 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302836 if (ftrace_start_up) {
2837 command = FTRACE_DISABLE_CALLS;
2838 if (ftrace_graph_active)
2839 command |= FTRACE_STOP_FUNC_RET;
2840 ftrace_run_update_code(command);
2841 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002842}
2843
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002844static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002845unsigned long ftrace_update_tot_cnt;
2846
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002847static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002848{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002849 /*
2850 * Filter_hash being empty will default to trace module.
2851 * But notrace hash requires a test of individual module functions.
2852 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002853 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2854 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002855}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002856
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002857/*
2858 * Check if the current ops references the record.
2859 *
2860 * If the ops traces all functions, then it was already accounted for.
2861 * If the ops does not trace the current record function, skip it.
2862 * If the ops ignores the function via notrace filter, skip it.
2863 */
2864static inline bool
2865ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2866{
2867 /* If ops isn't enabled, ignore it */
2868 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002869 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002870
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002871 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002872 if (ops_traces_mod(ops))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002873 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002874
2875 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002876 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002877 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002878 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002879
2880 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002881 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002882 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002883
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002884 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002885}
2886
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002887static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002888{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002889 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002890 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002891 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002892 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002893 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002894 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002895
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002896 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002897
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002898 /*
2899 * When a module is loaded, this function is called to convert
2900 * the calls to mcount in its text to nops, and also to create
2901 * an entry in the ftrace data. Now, if ftrace is activated
2902 * after this call, but before the module sets its text to
2903 * read-only, the modification of enabling ftrace can fail if
2904 * the read-only is done while ftrace is converting the calls.
2905 * To prevent this, the module's records are set as disabled
2906 * and will be enabled after the call to set the module's text
2907 * to read-only.
2908 */
2909 if (mod)
2910 rec_flags |= FTRACE_FL_DISABLED;
2911
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002912 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302913
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002914 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002915
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002916 /* If something went wrong, bail without enabling anything */
2917 if (unlikely(ftrace_disabled))
2918 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002919
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002920 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002921 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302922
Vasily Gorbik2f4df002018-08-06 15:17:46 +02002923#ifndef CC_USING_NOP_MCOUNT
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002924 /*
2925 * Do the initial record conversion from mcount jump
2926 * to the NOP instructions.
2927 */
2928 if (!ftrace_code_disable(mod, p))
2929 break;
Vasily Gorbik2f4df002018-08-06 15:17:46 +02002930#endif
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002931
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002932 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002933 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002934 }
2935
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002936 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002937 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002938 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002939
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002940 return 0;
2941}
2942
Steven Rostedta7900872011-12-16 16:23:44 -05002943static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002944{
Steven Rostedta7900872011-12-16 16:23:44 -05002945 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002946 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002947
Steven Rostedta7900872011-12-16 16:23:44 -05002948 if (WARN_ON(!count))
2949 return -EINVAL;
2950
2951 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002952
2953 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002954 * We want to fill as much as possible. No more than a page
2955 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002956 */
Steven Rostedta7900872011-12-16 16:23:44 -05002957 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2958 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002959
Steven Rostedta7900872011-12-16 16:23:44 -05002960 again:
2961 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2962
2963 if (!pg->records) {
2964 /* if we can't allocate this size, try something smaller */
2965 if (!order)
2966 return -ENOMEM;
2967 order >>= 1;
2968 goto again;
2969 }
2970
2971 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2972 pg->size = cnt;
2973
2974 if (cnt > count)
2975 cnt = count;
2976
2977 return cnt;
2978}
2979
2980static struct ftrace_page *
2981ftrace_allocate_pages(unsigned long num_to_init)
2982{
2983 struct ftrace_page *start_pg;
2984 struct ftrace_page *pg;
2985 int order;
2986 int cnt;
2987
2988 if (!num_to_init)
2989 return 0;
2990
2991 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2992 if (!pg)
2993 return NULL;
2994
2995 /*
2996 * Try to allocate as much as possible in one continues
2997 * location that fills in all of the space. We want to
2998 * waste as little space as possible.
2999 */
3000 for (;;) {
3001 cnt = ftrace_allocate_records(pg, num_to_init);
3002 if (cnt < 0)
3003 goto free_pages;
3004
3005 num_to_init -= cnt;
3006 if (!num_to_init)
3007 break;
3008
3009 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3010 if (!pg->next)
3011 goto free_pages;
3012
3013 pg = pg->next;
3014 }
3015
3016 return start_pg;
3017
3018 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003019 pg = start_pg;
3020 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003021 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3022 free_pages((unsigned long)pg->records, order);
3023 start_pg = pg->next;
3024 kfree(pg);
3025 pg = start_pg;
3026 }
3027 pr_info("ftrace: FAILED to allocate memory for functions\n");
3028 return NULL;
3029}
3030
Steven Rostedt5072c592008-05-12 21:20:43 +02003031#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3032
3033struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003034 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003035 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003036 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003037 struct ftrace_page *pg;
3038 struct dyn_ftrace *func;
3039 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003040 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003041 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003042 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003043 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003044 struct trace_array *tr;
3045 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003046 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003047 int idx;
3048 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003049};
3050
Ingo Molnare309b412008-05-12 21:20:51 +02003051static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003052t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003053{
3054 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003055 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003056 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003057 struct ftrace_hash *hash;
3058 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003059 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003060 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003061 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003062
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003063 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003064 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003065
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003066 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003067 return NULL;
3068
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003069 func_probes = &tr->func_probes;
3070 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003071 return NULL;
3072
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003073 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003074 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003075 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003076 }
3077
3078 if (iter->probe_entry)
3079 hnd = &iter->probe_entry->hlist;
3080
3081 hash = iter->probe->ops.func_hash->filter_hash;
3082 size = 1 << hash->size_bits;
3083
3084 retry:
3085 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003086 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003087 return NULL;
3088 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003089 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003090 hash = iter->probe->ops.func_hash->filter_hash;
3091 size = 1 << hash->size_bits;
3092 iter->pidx = 0;
3093 }
3094
3095 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003096
3097 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003098 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003099 hnd = NULL;
3100 goto retry;
3101 }
3102
3103 if (!hnd)
3104 hnd = hhd->first;
3105 else {
3106 hnd = hnd->next;
3107 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003108 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003109 goto retry;
3110 }
3111 }
3112
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003113 if (WARN_ON_ONCE(!hnd))
3114 return NULL;
3115
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003116 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003117
3118 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003119}
3120
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003121static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003122{
3123 struct ftrace_iterator *iter = m->private;
3124 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003125 loff_t l;
3126
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003127 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003128 return NULL;
3129
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003130 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003131 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003132
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003133 iter->probe = NULL;
3134 iter->probe_entry = NULL;
3135 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003136 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003137 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003138 if (!p)
3139 break;
3140 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003141 if (!p)
3142 return NULL;
3143
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003144 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003145 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003146
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003147 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003148}
3149
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003150static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003151t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003152{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003153 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003154 struct ftrace_probe_ops *probe_ops;
3155 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003156
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003157 probe = iter->probe;
3158 probe_entry = iter->probe_entry;
3159
3160 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003161 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003162
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003163 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003164
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003165 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003166 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003167
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003168 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3169 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003170
3171 return 0;
3172}
3173
3174static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003175t_mod_next(struct seq_file *m, loff_t *pos)
3176{
3177 struct ftrace_iterator *iter = m->private;
3178 struct trace_array *tr = iter->tr;
3179
3180 (*pos)++;
3181 iter->pos = *pos;
3182
3183 iter->mod_list = iter->mod_list->next;
3184
3185 if (iter->mod_list == &tr->mod_trace ||
3186 iter->mod_list == &tr->mod_notrace) {
3187 iter->flags &= ~FTRACE_ITER_MOD;
3188 return NULL;
3189 }
3190
3191 iter->mod_pos = *pos;
3192
3193 return iter;
3194}
3195
3196static void *t_mod_start(struct seq_file *m, loff_t *pos)
3197{
3198 struct ftrace_iterator *iter = m->private;
3199 void *p = NULL;
3200 loff_t l;
3201
3202 if (iter->func_pos > *pos)
3203 return NULL;
3204
3205 iter->mod_pos = iter->func_pos;
3206
3207 /* probes are only available if tr is set */
3208 if (!iter->tr)
3209 return NULL;
3210
3211 for (l = 0; l <= (*pos - iter->func_pos); ) {
3212 p = t_mod_next(m, &l);
3213 if (!p)
3214 break;
3215 }
3216 if (!p) {
3217 iter->flags &= ~FTRACE_ITER_MOD;
3218 return t_probe_start(m, pos);
3219 }
3220
3221 /* Only set this if we have an item */
3222 iter->flags |= FTRACE_ITER_MOD;
3223
3224 return iter;
3225}
3226
3227static int
3228t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3229{
3230 struct ftrace_mod_load *ftrace_mod;
3231 struct trace_array *tr = iter->tr;
3232
3233 if (WARN_ON_ONCE(!iter->mod_list) ||
3234 iter->mod_list == &tr->mod_trace ||
3235 iter->mod_list == &tr->mod_notrace)
3236 return -EIO;
3237
3238 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3239
3240 if (ftrace_mod->func)
3241 seq_printf(m, "%s", ftrace_mod->func);
3242 else
3243 seq_putc(m, '*');
3244
3245 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3246
3247 return 0;
3248}
3249
3250static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003251t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003252{
3253 struct ftrace_iterator *iter = m->private;
3254 struct dyn_ftrace *rec = NULL;
3255
3256 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003257
Steven Rostedt5072c592008-05-12 21:20:43 +02003258 retry:
3259 if (iter->idx >= iter->pg->index) {
3260 if (iter->pg->next) {
3261 iter->pg = iter->pg->next;
3262 iter->idx = 0;
3263 goto retry;
3264 }
3265 } else {
3266 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003267 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3268 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003269
3270 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003271 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003272
Steven Rostedt5072c592008-05-12 21:20:43 +02003273 rec = NULL;
3274 goto retry;
3275 }
3276 }
3277
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003278 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003279 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003280
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003281 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003282 iter->func = rec;
3283
3284 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003285}
3286
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003287static void *
3288t_next(struct seq_file *m, void *v, loff_t *pos)
3289{
3290 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003291 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003292 void *ret;
3293
3294 if (unlikely(ftrace_disabled))
3295 return NULL;
3296
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003297 if (iter->flags & FTRACE_ITER_PROBE)
3298 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003299
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003300 if (iter->flags & FTRACE_ITER_MOD)
3301 return t_mod_next(m, pos);
3302
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003303 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003304 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003305 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003306 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003307 }
3308
3309 ret = t_func_next(m, pos);
3310
3311 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003312 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003313
3314 return ret;
3315}
3316
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003317static void reset_iter_read(struct ftrace_iterator *iter)
3318{
3319 iter->pos = 0;
3320 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003321 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003322}
3323
3324static void *t_start(struct seq_file *m, loff_t *pos)
3325{
3326 struct ftrace_iterator *iter = m->private;
3327 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003328 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003329
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003330 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003331
3332 if (unlikely(ftrace_disabled))
3333 return NULL;
3334
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003335 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003336 * If an lseek was done, then reset and start from beginning.
3337 */
3338 if (*pos < iter->pos)
3339 reset_iter_read(iter);
3340
3341 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003342 * For set_ftrace_filter reading, if we have the filter
3343 * off, we can short cut and just print out that all
3344 * functions are enabled.
3345 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003346 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3347 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003348 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003349 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003350 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003351 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003352 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003353 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003354 return iter;
3355 }
3356
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003357 if (iter->flags & FTRACE_ITER_MOD)
3358 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003359
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003360 /*
3361 * Unfortunately, we need to restart at ftrace_pages_start
3362 * every time we let go of the ftrace_mutex. This is because
3363 * those pointers can change without the lock.
3364 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003365 iter->pg = ftrace_pages_start;
3366 iter->idx = 0;
3367 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003368 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003369 if (!p)
3370 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003371 }
walimis5821e1b2008-11-15 15:19:06 +08003372
Steven Rostedt69a30832011-12-19 15:21:16 -05003373 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003374 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003375
3376 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003377}
3378
3379static void t_stop(struct seq_file *m, void *p)
3380{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003381 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003382}
3383
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003384void * __weak
3385arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3386{
3387 return NULL;
3388}
3389
3390static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3391 struct dyn_ftrace *rec)
3392{
3393 void *ptr;
3394
3395 ptr = arch_ftrace_trampoline_func(ops, rec);
3396 if (ptr)
3397 seq_printf(m, " ->%pS", ptr);
3398}
3399
Steven Rostedt5072c592008-05-12 21:20:43 +02003400static int t_show(struct seq_file *m, void *v)
3401{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003402 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003403 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003404
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003405 if (iter->flags & FTRACE_ITER_PROBE)
3406 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003407
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003408 if (iter->flags & FTRACE_ITER_MOD)
3409 return t_mod_show(m, iter);
3410
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003411 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003412 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003413 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003414 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003415 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003416 return 0;
3417 }
3418
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003419 rec = iter->func;
3420
Steven Rostedt5072c592008-05-12 21:20:43 +02003421 if (!rec)
3422 return 0;
3423
Steven Rostedt647bcd02011-05-03 14:39:21 -04003424 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003425 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003426 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003427
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003428 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003429 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003430 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3431 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003432 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003433 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003434 if (ops) {
3435 do {
3436 seq_printf(m, "\ttramp: %pS (%pS)",
3437 (void *)ops->trampoline,
3438 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003439 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003440 ops = ftrace_find_tramp_ops_next(rec, ops);
3441 } while (ops);
3442 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003443 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003444 } else {
3445 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003446 }
3447 }
3448
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003449 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003450
3451 return 0;
3452}
3453
James Morris88e9d342009-09-22 16:43:43 -07003454static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003455 .start = t_start,
3456 .next = t_next,
3457 .stop = t_stop,
3458 .show = t_show,
3459};
3460
Ingo Molnare309b412008-05-12 21:20:51 +02003461static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003462ftrace_avail_open(struct inode *inode, struct file *file)
3463{
3464 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003465
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003466 if (unlikely(ftrace_disabled))
3467 return -ENODEV;
3468
Jiri Olsa50e18b92012-04-25 10:23:39 +02003469 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003470 if (!iter)
3471 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003472
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003473 iter->pg = ftrace_pages_start;
3474 iter->ops = &global_ops;
3475
3476 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003477}
3478
Steven Rostedt647bcd02011-05-03 14:39:21 -04003479static int
3480ftrace_enabled_open(struct inode *inode, struct file *file)
3481{
3482 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003483
Jiri Olsa50e18b92012-04-25 10:23:39 +02003484 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003485 if (!iter)
3486 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003487
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003488 iter->pg = ftrace_pages_start;
3489 iter->flags = FTRACE_ITER_ENABLED;
3490 iter->ops = &global_ops;
3491
3492 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003493}
3494
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003495/**
3496 * ftrace_regex_open - initialize function tracer filter files
3497 * @ops: The ftrace_ops that hold the hash filters
3498 * @flag: The type of filter to process
3499 * @inode: The inode, usually passed in to your open routine
3500 * @file: The file, usually passed in to your open routine
3501 *
3502 * ftrace_regex_open() initializes the filter files for the
3503 * @ops. Depending on @flag it may process the filter hash or
3504 * the notrace hash of @ops. With this called from the open
3505 * routine, you can use ftrace_filter_write() for the write
3506 * routine if @flag has FTRACE_ITER_FILTER set, or
3507 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003508 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003509 * release must call ftrace_regex_release().
3510 */
3511int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003512ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003513 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003514{
3515 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003516 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003517 struct list_head *mod_head;
3518 struct trace_array *tr = ops->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003519 int ret = 0;
3520
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003521 ftrace_ops_init(ops);
3522
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003523 if (unlikely(ftrace_disabled))
3524 return -ENODEV;
3525
Steven Rostedt5072c592008-05-12 21:20:43 +02003526 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3527 if (!iter)
3528 return -ENOMEM;
3529
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003530 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3531 kfree(iter);
3532 return -ENOMEM;
3533 }
3534
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003535 iter->ops = ops;
3536 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003537 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003538
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003539 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003540
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003541 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003542 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003543 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003544 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003545 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003546 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003547 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003548
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003549 iter->mod_list = mod_head;
3550
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003551 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003552 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3553
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003554 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003555 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003556 clear_ftrace_mod_list(mod_head);
3557 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003558 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003559 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003560
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003561 if (!iter->hash) {
3562 trace_parser_put(&iter->parser);
3563 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003564 ret = -ENOMEM;
3565 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003566 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003567 } else
3568 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003569
Steven Rostedt5072c592008-05-12 21:20:43 +02003570 if (file->f_mode & FMODE_READ) {
3571 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003572
3573 ret = seq_open(file, &show_ftrace_seq_ops);
3574 if (!ret) {
3575 struct seq_file *m = file->private_data;
3576 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003577 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003578 /* Failed */
3579 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003580 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003581 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003582 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003583 } else
3584 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003585
3586 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003587 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003588
3589 return ret;
3590}
3591
Steven Rostedt41c52c02008-05-22 11:46:33 -04003592static int
3593ftrace_filter_open(struct inode *inode, struct file *file)
3594{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003595 struct ftrace_ops *ops = inode->i_private;
3596
3597 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003598 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003599 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003600}
3601
3602static int
3603ftrace_notrace_open(struct inode *inode, struct file *file)
3604{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003605 struct ftrace_ops *ops = inode->i_private;
3606
3607 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003608 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003609}
3610
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003611/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3612struct ftrace_glob {
3613 char *search;
3614 unsigned len;
3615 int type;
3616};
3617
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003618/*
3619 * If symbols in an architecture don't correspond exactly to the user-visible
3620 * name of what they represent, it is possible to define this function to
3621 * perform the necessary adjustments.
3622*/
3623char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3624{
3625 return str;
3626}
3627
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003628static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003629{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003630 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003631 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003632
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003633 str = arch_ftrace_match_adjust(str, g->search);
3634
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003635 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003636 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003637 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003638 matched = 1;
3639 break;
3640 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003641 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003642 matched = 1;
3643 break;
3644 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003645 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003646 matched = 1;
3647 break;
3648 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003649 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003650 if (slen >= g->len &&
3651 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003652 matched = 1;
3653 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003654 case MATCH_GLOB:
3655 if (glob_match(g->search, str))
3656 matched = 1;
3657 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003658 }
3659
3660 return matched;
3661}
3662
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003663static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003664enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003665{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003666 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003667 int ret = 0;
3668
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003669 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003670 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003671 /* Do nothing if it doesn't exist */
3672 if (!entry)
3673 return 0;
3674
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003675 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003676 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003677 /* Do nothing if it exists */
3678 if (entry)
3679 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003680
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003681 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003682 }
3683 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003684}
3685
Steven Rostedt64e7c442009-02-13 17:08:48 -05003686static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003687ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3688 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003689{
3690 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003691 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003692
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003693 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3694
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003695 if (mod_g) {
3696 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3697
3698 /* blank module name to match all modules */
3699 if (!mod_g->len) {
3700 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003701 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003702 goto func_match;
3703 return 0;
3704 }
3705
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003706 /*
3707 * exclude_mod is set to trace everything but the given
3708 * module. If it is set and the module matches, then
3709 * return 0. If it is not set, and the module doesn't match
3710 * also return 0. Otherwise, check the function to see if
3711 * that matches.
3712 */
3713 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003714 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003715func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003716 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003717 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003718 return 1;
3719 }
3720
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003721 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003722}
3723
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003724static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003725match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003726{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003727 struct ftrace_page *pg;
3728 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003729 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003730 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3731 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3732 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003733 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003734 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03003735 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003736
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003737 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003738 func_g.type = filter_parse_regex(func, len, &func_g.search,
3739 &clear_filter);
3740 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003741 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003742
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003743 if (mod) {
3744 mod_g.type = filter_parse_regex(mod, strlen(mod),
3745 &mod_g.search, &exclude_mod);
3746 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003747 }
3748
Steven Rostedt52baf112009-02-14 01:15:39 -05003749 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003750
3751 if (unlikely(ftrace_disabled))
3752 goto out_unlock;
3753
Steven Rostedt265c8312009-02-13 12:43:56 -05003754 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003755
3756 if (rec->flags & FTRACE_FL_DISABLED)
3757 continue;
3758
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003759 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003760 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003761 if (ret < 0) {
3762 found = ret;
3763 goto out_unlock;
3764 }
Li Zefan311d16d2009-12-08 11:15:11 +08003765 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003766 }
3767 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003768 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003769 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003770
3771 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003772}
3773
Steven Rostedt64e7c442009-02-13 17:08:48 -05003774static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003775ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003776{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003777 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003778}
3779
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003780static void ftrace_ops_update_code(struct ftrace_ops *ops,
3781 struct ftrace_ops_hash *old_hash)
3782{
3783 struct ftrace_ops *op;
3784
3785 if (!ftrace_enabled)
3786 return;
3787
3788 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3789 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3790 return;
3791 }
3792
3793 /*
3794 * If this is the shared global_ops filter, then we need to
3795 * check if there is another ops that shares it, is enabled.
3796 * If so, we still need to run the modify code.
3797 */
3798 if (ops->func_hash != &global_ops.local_hash)
3799 return;
3800
3801 do_for_each_ftrace_op(op, ftrace_ops_list) {
3802 if (op->func_hash == &global_ops.local_hash &&
3803 op->flags & FTRACE_OPS_FL_ENABLED) {
3804 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3805 /* Only need to do this once */
3806 return;
3807 }
3808 } while_for_each_ftrace_op(op);
3809}
3810
3811static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3812 struct ftrace_hash **orig_hash,
3813 struct ftrace_hash *hash,
3814 int enable)
3815{
3816 struct ftrace_ops_hash old_hash_ops;
3817 struct ftrace_hash *old_hash;
3818 int ret;
3819
3820 old_hash = *orig_hash;
3821 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3822 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3823 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3824 if (!ret) {
3825 ftrace_ops_update_code(ops, &old_hash_ops);
3826 free_ftrace_hash_rcu(old_hash);
3827 }
3828 return ret;
3829}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003830
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003831static bool module_exists(const char *module)
3832{
3833 /* All modules have the symbol __this_module */
3834 const char this_mod[] = "__this_module";
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003835 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003836 unsigned long val;
3837 int n;
3838
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003839 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003840
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003841 if (n > sizeof(modname) - 1)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003842 return false;
3843
3844 val = module_kallsyms_lookup_name(modname);
3845 return val != 0;
3846}
3847
3848static int cache_mod(struct trace_array *tr,
3849 const char *func, char *module, int enable)
3850{
3851 struct ftrace_mod_load *ftrace_mod, *n;
3852 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3853 int ret;
3854
3855 mutex_lock(&ftrace_lock);
3856
3857 /* We do not cache inverse filters */
3858 if (func[0] == '!') {
3859 func++;
3860 ret = -EINVAL;
3861
3862 /* Look to remove this hash */
3863 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3864 if (strcmp(ftrace_mod->module, module) != 0)
3865 continue;
3866
3867 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03003868 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003869 (ftrace_mod->func &&
3870 strcmp(ftrace_mod->func, func) == 0)) {
3871 ret = 0;
3872 free_ftrace_mod(ftrace_mod);
3873 continue;
3874 }
3875 }
3876 goto out;
3877 }
3878
3879 ret = -EINVAL;
3880 /* We only care about modules that have not been loaded yet */
3881 if (module_exists(module))
3882 goto out;
3883
3884 /* Save this string off, and execute it when the module is loaded */
3885 ret = ftrace_add_mod(tr, func, module, enable);
3886 out:
3887 mutex_unlock(&ftrace_lock);
3888
3889 return ret;
3890}
3891
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003892static int
3893ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3894 int reset, int enable);
3895
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003896#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003897static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3898 char *mod, bool enable)
3899{
3900 struct ftrace_mod_load *ftrace_mod, *n;
3901 struct ftrace_hash **orig_hash, *new_hash;
3902 LIST_HEAD(process_mods);
3903 char *func;
3904 int ret;
3905
3906 mutex_lock(&ops->func_hash->regex_lock);
3907
3908 if (enable)
3909 orig_hash = &ops->func_hash->filter_hash;
3910 else
3911 orig_hash = &ops->func_hash->notrace_hash;
3912
3913 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3914 *orig_hash);
3915 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04003916 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003917
3918 mutex_lock(&ftrace_lock);
3919
3920 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3921
3922 if (strcmp(ftrace_mod->module, mod) != 0)
3923 continue;
3924
3925 if (ftrace_mod->func)
3926 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
3927 else
3928 func = kstrdup("*", GFP_KERNEL);
3929
3930 if (!func) /* warn? */
3931 continue;
3932
3933 list_del(&ftrace_mod->list);
3934 list_add(&ftrace_mod->list, &process_mods);
3935
3936 /* Use the newly allocated func, as it may be "*" */
3937 kfree(ftrace_mod->func);
3938 ftrace_mod->func = func;
3939 }
3940
3941 mutex_unlock(&ftrace_lock);
3942
3943 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
3944
3945 func = ftrace_mod->func;
3946
3947 /* Grabs ftrace_lock, which is why we have this extra step */
3948 match_records(new_hash, func, strlen(func), mod);
3949 free_ftrace_mod(ftrace_mod);
3950 }
3951
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04003952 if (enable && list_empty(head))
3953 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
3954
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003955 mutex_lock(&ftrace_lock);
3956
3957 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
3958 new_hash, enable);
3959 mutex_unlock(&ftrace_lock);
3960
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04003961 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003962 mutex_unlock(&ops->func_hash->regex_lock);
3963
3964 free_ftrace_hash(new_hash);
3965}
3966
3967static void process_cached_mods(const char *mod_name)
3968{
3969 struct trace_array *tr;
3970 char *mod;
3971
3972 mod = kstrdup(mod_name, GFP_KERNEL);
3973 if (!mod)
3974 return;
3975
3976 mutex_lock(&trace_types_lock);
3977 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
3978 if (!list_empty(&tr->mod_trace))
3979 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
3980 if (!list_empty(&tr->mod_notrace))
3981 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
3982 }
3983 mutex_unlock(&trace_types_lock);
3984
3985 kfree(mod);
3986}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003987#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003988
Steven Rostedtf6180772009-02-14 00:40:25 -05003989/*
3990 * We register the module command as a template to show others how
3991 * to register the a command as well.
3992 */
3993
3994static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003995ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003996 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003997{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003998 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003999 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004000
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004001 /* match_records() modifies func, and we need the original */
4002 func = kstrdup(func_orig, GFP_KERNEL);
4003 if (!func)
4004 return -ENOMEM;
4005
Steven Rostedtf6180772009-02-14 00:40:25 -05004006 /*
4007 * cmd == 'mod' because we only registered this func
4008 * for the 'mod' ftrace_func_command.
4009 * But if you register one func with multiple commands,
4010 * you can tell which command was used by the cmd
4011 * parameter.
4012 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004013 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004014 kfree(func);
4015
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004016 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004017 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004018 if (ret < 0)
4019 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004020 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004021}
4022
4023static struct ftrace_func_command ftrace_mod_cmd = {
4024 .name = "mod",
4025 .func = ftrace_mod_callback,
4026};
4027
4028static int __init ftrace_mod_cmd_init(void)
4029{
4030 return register_ftrace_command(&ftrace_mod_cmd);
4031}
Steven Rostedt6f415672012-10-05 12:13:07 -04004032core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004033
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004034static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004035 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004036{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004037 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004038 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004039
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004040 probe = container_of(op, struct ftrace_func_probe, ops);
4041 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004042
4043 /*
4044 * Disable preemption for these calls to prevent a RCU grace
4045 * period. This syncs the hash iteration and freeing of items
4046 * on the hash. rcu_read_lock is too dangerous here.
4047 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004048 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004049 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004050 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004051}
4052
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004053struct ftrace_func_map {
4054 struct ftrace_func_entry entry;
4055 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004056};
4057
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004058struct ftrace_func_mapper {
4059 struct ftrace_hash hash;
4060};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004061
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004062/**
4063 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4064 *
4065 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4066 */
4067struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004068{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004069 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004070
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004071 /*
4072 * The mapper is simply a ftrace_hash, but since the entries
4073 * in the hash are not ftrace_func_entry type, we define it
4074 * as a separate structure.
4075 */
4076 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4077 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004078}
4079
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004080/**
4081 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4082 * @mapper: The mapper that has the ip maps
4083 * @ip: the instruction pointer to find the data for
4084 *
4085 * Returns the data mapped to @ip if found otherwise NULL. The return
4086 * is actually the address of the mapper data pointer. The address is
4087 * returned for use cases where the data is no bigger than a long, and
4088 * the user can use the data pointer as its data instead of having to
4089 * allocate more memory for the reference.
4090 */
4091void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4092 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004093{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004094 struct ftrace_func_entry *entry;
4095 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004096
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004097 entry = ftrace_lookup_ip(&mapper->hash, ip);
4098 if (!entry)
4099 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004100
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004101 map = (struct ftrace_func_map *)entry;
4102 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004103}
4104
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004105/**
4106 * ftrace_func_mapper_add_ip - Map some data to an ip
4107 * @mapper: The mapper that has the ip maps
4108 * @ip: The instruction pointer address to map @data to
4109 * @data: The data to map to @ip
4110 *
4111 * Returns 0 on succes otherwise an error.
4112 */
4113int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4114 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004115{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004116 struct ftrace_func_entry *entry;
4117 struct ftrace_func_map *map;
4118
4119 entry = ftrace_lookup_ip(&mapper->hash, ip);
4120 if (entry)
4121 return -EBUSY;
4122
4123 map = kmalloc(sizeof(*map), GFP_KERNEL);
4124 if (!map)
4125 return -ENOMEM;
4126
4127 map->entry.ip = ip;
4128 map->data = data;
4129
4130 __add_hash_entry(&mapper->hash, &map->entry);
4131
4132 return 0;
4133}
4134
4135/**
4136 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4137 * @mapper: The mapper that has the ip maps
4138 * @ip: The instruction pointer address to remove the data from
4139 *
4140 * Returns the data if it is found, otherwise NULL.
4141 * Note, if the data pointer is used as the data itself, (see
4142 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4143 * if the data pointer was set to zero.
4144 */
4145void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4146 unsigned long ip)
4147{
4148 struct ftrace_func_entry *entry;
4149 struct ftrace_func_map *map;
4150 void *data;
4151
4152 entry = ftrace_lookup_ip(&mapper->hash, ip);
4153 if (!entry)
4154 return NULL;
4155
4156 map = (struct ftrace_func_map *)entry;
4157 data = map->data;
4158
4159 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004160 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004161
4162 return data;
4163}
4164
4165/**
4166 * free_ftrace_func_mapper - free a mapping of ips and data
4167 * @mapper: The mapper that has the ip maps
4168 * @free_func: A function to be called on each data item.
4169 *
4170 * This is used to free the function mapper. The @free_func is optional
4171 * and can be used if the data needs to be freed as well.
4172 */
4173void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4174 ftrace_mapper_func free_func)
4175{
4176 struct ftrace_func_entry *entry;
4177 struct ftrace_func_map *map;
4178 struct hlist_head *hhd;
4179 int size = 1 << mapper->hash.size_bits;
4180 int i;
4181
4182 if (free_func && mapper->hash.count) {
4183 for (i = 0; i < size; i++) {
4184 hhd = &mapper->hash.buckets[i];
4185 hlist_for_each_entry(entry, hhd, hlist) {
4186 map = (struct ftrace_func_map *)entry;
4187 free_func(map);
4188 }
4189 }
4190 }
4191 free_ftrace_hash(&mapper->hash);
4192}
4193
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004194static void release_probe(struct ftrace_func_probe *probe)
4195{
4196 struct ftrace_probe_ops *probe_ops;
4197
4198 mutex_lock(&ftrace_lock);
4199
4200 WARN_ON(probe->ref <= 0);
4201
4202 /* Subtract the ref that was used to protect this instance */
4203 probe->ref--;
4204
4205 if (!probe->ref) {
4206 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004207 /*
4208 * Sending zero as ip tells probe_ops to free
4209 * the probe->data itself
4210 */
4211 if (probe_ops->free)
4212 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004213 list_del(&probe->list);
4214 kfree(probe);
4215 }
4216 mutex_unlock(&ftrace_lock);
4217}
4218
4219static void acquire_probe_locked(struct ftrace_func_probe *probe)
4220{
4221 /*
4222 * Add one ref to keep it from being freed when releasing the
4223 * ftrace_lock mutex.
4224 */
4225 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004226}
4227
Steven Rostedt59df055f2009-02-14 15:29:06 -05004228int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004229register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004230 struct ftrace_probe_ops *probe_ops,
4231 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004232{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004233 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004234 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004235 struct ftrace_hash **orig_hash;
4236 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004237 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004238 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004239 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004240 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004241 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004242
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004243 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004244 return -EINVAL;
4245
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004246 /* We do not support '!' for function probes */
4247 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004248 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004249
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004250
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004251 mutex_lock(&ftrace_lock);
4252 /* Check if the probe_ops is already registered */
4253 list_for_each_entry(probe, &tr->func_probes, list) {
4254 if (probe->probe_ops == probe_ops)
4255 break;
4256 }
4257 if (&probe->list == &tr->func_probes) {
4258 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4259 if (!probe) {
4260 mutex_unlock(&ftrace_lock);
4261 return -ENOMEM;
4262 }
4263 probe->probe_ops = probe_ops;
4264 probe->ops.func = function_trace_probe_call;
4265 probe->tr = tr;
4266 ftrace_ops_init(&probe->ops);
4267 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004268 }
4269
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004270 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004271
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004272 mutex_unlock(&ftrace_lock);
4273
4274 mutex_lock(&probe->ops.func_hash->regex_lock);
4275
4276 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004277 old_hash = *orig_hash;
4278 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4279
4280 ret = ftrace_match_records(hash, glob, strlen(glob));
4281
4282 /* Nothing found? */
4283 if (!ret)
4284 ret = -EINVAL;
4285
4286 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004287 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004288
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004289 size = 1 << hash->size_bits;
4290 for (i = 0; i < size; i++) {
4291 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4292 if (ftrace_lookup_ip(old_hash, entry->ip))
4293 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004294 /*
4295 * The caller might want to do something special
4296 * for each function we find. We call the callback
4297 * to give the caller an opportunity to do so.
4298 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004299 if (probe_ops->init) {
4300 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004301 entry->ip, data,
4302 &probe->data);
4303 if (ret < 0) {
4304 if (probe_ops->free && count)
4305 probe_ops->free(probe_ops, tr,
4306 0, probe->data);
4307 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004308 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004309 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004310 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004311 count++;
4312 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004313 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004314
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004315 mutex_lock(&ftrace_lock);
4316
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004317 if (!count) {
4318 /* Nothing was added? */
4319 ret = -EINVAL;
4320 goto out_unlock;
4321 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004322
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004323 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4324 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004325 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004326 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004327
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004328 /* One ref for each new function traced */
4329 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004330
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004331 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4332 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004333
Steven Rostedt59df055f2009-02-14 15:29:06 -05004334 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004335 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004336
4337 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004338 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004339 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004340 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004341 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004342
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004343 release_probe(probe);
4344
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004345 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004346
4347 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004348 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004349 goto out_unlock;
4350
4351 /* Failed to do the move, need to call the free functions */
4352 for (i = 0; i < size; i++) {
4353 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4354 if (ftrace_lookup_ip(old_hash, entry->ip))
4355 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004356 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004357 }
4358 }
4359 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004360}
4361
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004362int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004363unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4364 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004365{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004366 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004367 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004368 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004369 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004370 struct ftrace_hash **orig_hash;
4371 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004372 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004373 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004374 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004375 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004376 int count = 0;
4377 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004378 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004379
Naveen N. Raocbab5672017-05-16 23:21:25 +05304380 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004381 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304382 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004383 int not;
4384
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004385 func_g.type = filter_parse_regex(glob, strlen(glob),
4386 &func_g.search, &not);
4387 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004388
Steven Rostedtb6887d72009-02-17 12:32:04 -05004389 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004390 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004391 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004392 }
4393
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004394 mutex_lock(&ftrace_lock);
4395 /* Check if the probe_ops is already registered */
4396 list_for_each_entry(probe, &tr->func_probes, list) {
4397 if (probe->probe_ops == probe_ops)
4398 break;
4399 }
4400 if (&probe->list == &tr->func_probes)
4401 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004402
4403 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004404 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4405 goto err_unlock_ftrace;
4406
4407 acquire_probe_locked(probe);
4408
4409 mutex_unlock(&ftrace_lock);
4410
4411 mutex_lock(&probe->ops.func_hash->regex_lock);
4412
4413 orig_hash = &probe->ops.func_hash->filter_hash;
4414 old_hash = *orig_hash;
4415
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004416 if (ftrace_hash_empty(old_hash))
4417 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004418
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004419 old_hash_ops.filter_hash = old_hash;
4420 /* Probes only have filters */
4421 old_hash_ops.notrace_hash = NULL;
4422
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004423 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004424 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004425 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004426 goto out_unlock;
4427
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004428 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004429
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004430 size = 1 << hash->size_bits;
4431 for (i = 0; i < size; i++) {
4432 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004433
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004434 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004435 kallsyms_lookup(entry->ip, NULL, NULL,
4436 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004437 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004438 continue;
4439 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004440 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004441 remove_hash_entry(hash, entry);
4442 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004443 }
4444 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004445
4446 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004447 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004448 ret = -EINVAL;
4449 goto out_unlock;
4450 }
4451
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004452 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004453
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004454 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004455
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004456 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004457
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004458 if (ftrace_hash_empty(hash))
4459 ftrace_shutdown(&probe->ops, 0);
4460
4461 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004462 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004463
4464 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004465 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004466 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004467 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004468 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004469
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004470 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4471 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004472 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004473 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004474 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004475 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004476 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004477
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004478 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004479 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004480 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004481
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004482 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004483
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004484 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004485
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004486 err_unlock_ftrace:
4487 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004488 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004489}
4490
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304491void clear_ftrace_function_probes(struct trace_array *tr)
4492{
4493 struct ftrace_func_probe *probe, *n;
4494
4495 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4496 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4497}
4498
Steven Rostedtf6180772009-02-14 00:40:25 -05004499static LIST_HEAD(ftrace_commands);
4500static DEFINE_MUTEX(ftrace_cmd_mutex);
4501
Tom Zanussi38de93a2013-10-24 08:34:18 -05004502/*
4503 * Currently we only register ftrace commands from __init, so mark this
4504 * __init too.
4505 */
4506__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004507{
4508 struct ftrace_func_command *p;
4509 int ret = 0;
4510
4511 mutex_lock(&ftrace_cmd_mutex);
4512 list_for_each_entry(p, &ftrace_commands, list) {
4513 if (strcmp(cmd->name, p->name) == 0) {
4514 ret = -EBUSY;
4515 goto out_unlock;
4516 }
4517 }
4518 list_add(&cmd->list, &ftrace_commands);
4519 out_unlock:
4520 mutex_unlock(&ftrace_cmd_mutex);
4521
4522 return ret;
4523}
4524
Tom Zanussi38de93a2013-10-24 08:34:18 -05004525/*
4526 * Currently we only unregister ftrace commands from __init, so mark
4527 * this __init too.
4528 */
4529__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004530{
4531 struct ftrace_func_command *p, *n;
4532 int ret = -ENODEV;
4533
4534 mutex_lock(&ftrace_cmd_mutex);
4535 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4536 if (strcmp(cmd->name, p->name) == 0) {
4537 ret = 0;
4538 list_del_init(&p->list);
4539 goto out_unlock;
4540 }
4541 }
4542 out_unlock:
4543 mutex_unlock(&ftrace_cmd_mutex);
4544
4545 return ret;
4546}
4547
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004548static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004549 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004550{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004551 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004552 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004553 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004554 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004555 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004556
4557 func = strsep(&next, ":");
4558
4559 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004560 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004561 if (!ret)
4562 ret = -EINVAL;
4563 if (ret < 0)
4564 return ret;
4565 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004566 }
4567
Steven Rostedtf6180772009-02-14 00:40:25 -05004568 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004569
4570 command = strsep(&next, ":");
4571
Steven Rostedtf6180772009-02-14 00:40:25 -05004572 mutex_lock(&ftrace_cmd_mutex);
4573 list_for_each_entry(p, &ftrace_commands, list) {
4574 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004575 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004576 goto out_unlock;
4577 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004578 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004579 out_unlock:
4580 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004581
Steven Rostedtf6180772009-02-14 00:40:25 -05004582 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004583}
4584
Ingo Molnare309b412008-05-12 21:20:51 +02004585static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004586ftrace_regex_write(struct file *file, const char __user *ubuf,
4587 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004588{
4589 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004590 struct trace_parser *parser;
4591 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004592
Li Zefan4ba79782009-09-22 13:52:20 +08004593 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004594 return 0;
4595
Steven Rostedt5072c592008-05-12 21:20:43 +02004596 if (file->f_mode & FMODE_READ) {
4597 struct seq_file *m = file->private_data;
4598 iter = m->private;
4599 } else
4600 iter = file->private_data;
4601
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004602 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004603 return -ENODEV;
4604
4605 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004606
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004607 parser = &iter->parser;
4608 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004609
Li Zefan4ba79782009-09-22 13:52:20 +08004610 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004611 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004612 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004613 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004614 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004615 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004616 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004617 }
4618
Steven Rostedt5072c592008-05-12 21:20:43 +02004619 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004620 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004621 return ret;
4622}
4623
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004624ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004625ftrace_filter_write(struct file *file, const char __user *ubuf,
4626 size_t cnt, loff_t *ppos)
4627{
4628 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4629}
4630
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004631ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004632ftrace_notrace_write(struct file *file, const char __user *ubuf,
4633 size_t cnt, loff_t *ppos)
4634{
4635 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4636}
4637
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004638static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004639ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4640{
4641 struct ftrace_func_entry *entry;
4642
4643 if (!ftrace_location(ip))
4644 return -EINVAL;
4645
4646 if (remove) {
4647 entry = ftrace_lookup_ip(hash, ip);
4648 if (!entry)
4649 return -ENOENT;
4650 free_hash_entry(hash, entry);
4651 return 0;
4652 }
4653
4654 return add_hash_entry(hash, ip);
4655}
4656
4657static int
4658ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4659 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004660{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004661 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004662 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004663 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004664
Steven Rostedt41c52c02008-05-22 11:46:33 -04004665 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004666 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004667
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004668 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004669
Steven Rostedtf45948e2011-05-02 12:29:25 -04004670 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004671 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004672 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004673 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004674
Wang Nanb972cc52014-07-15 08:40:20 +08004675 if (reset)
4676 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4677 else
4678 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4679
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004680 if (!hash) {
4681 ret = -ENOMEM;
4682 goto out_regex_unlock;
4683 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004684
Jiri Olsaac483c42012-01-02 10:04:14 +01004685 if (buf && !ftrace_match_records(hash, buf, len)) {
4686 ret = -EINVAL;
4687 goto out_regex_unlock;
4688 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004689 if (ip) {
4690 ret = ftrace_match_addr(hash, ip, remove);
4691 if (ret < 0)
4692 goto out_regex_unlock;
4693 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004694
4695 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004696 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004697 mutex_unlock(&ftrace_lock);
4698
Jiri Olsaac483c42012-01-02 10:04:14 +01004699 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004700 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004701
4702 free_ftrace_hash(hash);
4703 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004704}
4705
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004706static int
4707ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4708 int reset, int enable)
4709{
4710 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4711}
4712
4713/**
4714 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4715 * @ops - the ops to set the filter with
4716 * @ip - the address to add to or remove from the filter.
4717 * @remove - non zero to remove the ip from the filter
4718 * @reset - non zero to reset all filters before applying this filter.
4719 *
4720 * Filters denote which functions should be enabled when tracing is enabled
4721 * If @ip is NULL, it failes to update filter.
4722 */
4723int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4724 int remove, int reset)
4725{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004726 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004727 return ftrace_set_addr(ops, ip, remove, reset, 1);
4728}
4729EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4730
Joel Fernandesd032ae82016-11-15 12:31:20 -08004731/**
4732 * ftrace_ops_set_global_filter - setup ops to use global filters
4733 * @ops - the ops which will use the global filters
4734 *
4735 * ftrace users who need global function trace filtering should call this.
4736 * It can set the global filter only if ops were not initialized before.
4737 */
4738void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4739{
4740 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4741 return;
4742
4743 ftrace_ops_init(ops);
4744 ops->func_hash = &global_ops.local_hash;
4745}
4746EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4747
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004748static int
4749ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4750 int reset, int enable)
4751{
4752 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4753}
4754
Steven Rostedt77a2b372008-05-12 21:20:45 +02004755/**
4756 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004757 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004758 * @buf - the string that holds the function filter text.
4759 * @len - the length of the string.
4760 * @reset - non zero to reset all filters before applying this filter.
4761 *
4762 * Filters denote which functions should be enabled when tracing is enabled.
4763 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4764 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004765int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004766 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004767{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004768 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004769 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004770}
Steven Rostedt936e0742011-05-05 22:54:01 -04004771EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004772
Steven Rostedt41c52c02008-05-22 11:46:33 -04004773/**
4774 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004775 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004776 * @buf - the string that holds the function notrace text.
4777 * @len - the length of the string.
4778 * @reset - non zero to reset all filters before applying this filter.
4779 *
4780 * Notrace Filters denote which functions should not be enabled when tracing
4781 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4782 * for tracing.
4783 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004784int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004785 int len, int reset)
4786{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004787 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004788 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004789}
4790EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4791/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004792 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004793 * @buf - the string that holds the function filter text.
4794 * @len - the length of the string.
4795 * @reset - non zero to reset all filters before applying this filter.
4796 *
4797 * Filters denote which functions should be enabled when tracing is enabled.
4798 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4799 */
4800void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4801{
4802 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4803}
4804EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4805
4806/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004807 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004808 * @buf - the string that holds the function notrace text.
4809 * @len - the length of the string.
4810 * @reset - non zero to reset all filters before applying this filter.
4811 *
4812 * Notrace Filters denote which functions should not be enabled when tracing
4813 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4814 * for tracing.
4815 */
4816void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004817{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004818 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004819}
Steven Rostedt936e0742011-05-05 22:54:01 -04004820EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004821
Steven Rostedt2af15d62009-05-28 13:37:24 -04004822/*
4823 * command line interface to allow users to set filters on boot up.
4824 */
4825#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4826static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4827static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4828
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004829/* Used by function selftest to not test if filter is set */
4830bool ftrace_filter_param __initdata;
4831
Steven Rostedt2af15d62009-05-28 13:37:24 -04004832static int __init set_ftrace_notrace(char *str)
4833{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004834 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004835 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004836 return 1;
4837}
4838__setup("ftrace_notrace=", set_ftrace_notrace);
4839
4840static int __init set_ftrace_filter(char *str)
4841{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004842 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004843 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004844 return 1;
4845}
4846__setup("ftrace_filter=", set_ftrace_filter);
4847
Stefan Assmann369bc182009-10-12 22:17:21 +02004848#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004849static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004850static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004851static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004852
Stefan Assmann369bc182009-10-12 22:17:21 +02004853static int __init set_graph_function(char *str)
4854{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004855 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004856 return 1;
4857}
4858__setup("ftrace_graph_filter=", set_graph_function);
4859
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004860static int __init set_graph_notrace_function(char *str)
4861{
4862 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4863 return 1;
4864}
4865__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4866
Todd Brandt65a50c652017-03-02 16:12:15 -08004867static int __init set_graph_max_depth_function(char *str)
4868{
4869 if (!str)
4870 return 0;
4871 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4872 return 1;
4873}
4874__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4875
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004876static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004877{
4878 int ret;
4879 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004880 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004881
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004882 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4883 if (WARN_ON(!hash))
4884 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004885
4886 while (buf) {
4887 func = strsep(&buf, ",");
4888 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004889 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004890 if (ret)
4891 printk(KERN_DEBUG "ftrace: function %s not "
4892 "traceable\n", func);
4893 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004894
4895 if (enable)
4896 ftrace_graph_hash = hash;
4897 else
4898 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02004899}
4900#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4901
Steven Rostedt2a85a372011-12-19 21:57:44 -05004902void __init
4903ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004904{
4905 char *func;
4906
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004907 ftrace_ops_init(ops);
4908
Steven Rostedt2af15d62009-05-28 13:37:24 -04004909 while (buf) {
4910 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004911 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004912 }
4913}
4914
4915static void __init set_ftrace_early_filters(void)
4916{
4917 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004918 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004919 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004920 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004921#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4922 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004923 set_ftrace_early_graph(ftrace_graph_buf, 1);
4924 if (ftrace_graph_notrace_buf[0])
4925 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004926#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004927}
4928
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004929int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004930{
4931 struct seq_file *m = (struct seq_file *)file->private_data;
4932 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004933 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004934 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004935 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004936 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004937
Steven Rostedt5072c592008-05-12 21:20:43 +02004938 if (file->f_mode & FMODE_READ) {
4939 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004940 seq_release(inode, file);
4941 } else
4942 iter = file->private_data;
4943
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004944 parser = &iter->parser;
4945 if (trace_parser_loaded(parser)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004946 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004947 }
4948
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004949 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004950
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004951 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004952
Steven Rostedt058e2972011-04-29 22:35:33 -04004953 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004954 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4955
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004956 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004957 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04004958 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004959 iter->hash->flags |= FTRACE_HASH_FL_MOD;
4960 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004961 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004962
Steven Rostedt058e2972011-04-29 22:35:33 -04004963 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004964 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
4965 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04004966 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04004967 } else {
4968 /* For read only, the hash is the ops hash */
4969 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04004970 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004971
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004972 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004973 free_ftrace_hash(iter->hash);
4974 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004975
Steven Rostedt5072c592008-05-12 21:20:43 +02004976 return 0;
4977}
4978
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004979static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004980 .open = ftrace_avail_open,
4981 .read = seq_read,
4982 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004983 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004984};
4985
Steven Rostedt647bcd02011-05-03 14:39:21 -04004986static const struct file_operations ftrace_enabled_fops = {
4987 .open = ftrace_enabled_open,
4988 .read = seq_read,
4989 .llseek = seq_lseek,
4990 .release = seq_release_private,
4991};
4992
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004993static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004994 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004995 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004996 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004997 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004998 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004999};
5000
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005001static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005002 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005003 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005004 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005005 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005006 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005007};
5008
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005009#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5010
5011static DEFINE_MUTEX(graph_lock);
5012
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005013struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5014struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5015
5016enum graph_filter_type {
5017 GRAPH_FILTER_NOTRACE = 0,
5018 GRAPH_FILTER_FUNCTION,
5019};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005020
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005021#define FTRACE_GRAPH_EMPTY ((void *)1)
5022
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005023struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005024 struct ftrace_hash *hash;
5025 struct ftrace_func_entry *entry;
5026 int idx; /* for hash table iteration */
5027 enum graph_filter_type type;
5028 struct ftrace_hash *new_hash;
5029 const struct seq_operations *seq_ops;
5030 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005031};
5032
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005033static void *
Li Zefan85951842009-06-24 09:54:00 +08005034__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005035{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005036 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005037 struct ftrace_func_entry *entry = fgd->entry;
5038 struct hlist_head *head;
5039 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005040
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005041 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005042 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005043
5044 if (entry) {
5045 hlist_for_each_entry_continue(entry, hlist) {
5046 fgd->entry = entry;
5047 return entry;
5048 }
5049
5050 idx++;
5051 }
5052
5053 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5054 head = &fgd->hash->buckets[i];
5055 hlist_for_each_entry(entry, head, hlist) {
5056 fgd->entry = entry;
5057 fgd->idx = i;
5058 return entry;
5059 }
5060 }
5061 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005062}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005063
Li Zefan85951842009-06-24 09:54:00 +08005064static void *
5065g_next(struct seq_file *m, void *v, loff_t *pos)
5066{
5067 (*pos)++;
5068 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005069}
5070
5071static void *g_start(struct seq_file *m, loff_t *pos)
5072{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005073 struct ftrace_graph_data *fgd = m->private;
5074
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005075 mutex_lock(&graph_lock);
5076
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005077 if (fgd->type == GRAPH_FILTER_FUNCTION)
5078 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5079 lockdep_is_held(&graph_lock));
5080 else
5081 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5082 lockdep_is_held(&graph_lock));
5083
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005084 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005085 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005086 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005087
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005088 fgd->idx = 0;
5089 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005090 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005091}
5092
5093static void g_stop(struct seq_file *m, void *p)
5094{
5095 mutex_unlock(&graph_lock);
5096}
5097
5098static int g_show(struct seq_file *m, void *v)
5099{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005100 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005101
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005102 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005103 return 0;
5104
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005105 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005106 struct ftrace_graph_data *fgd = m->private;
5107
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005108 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005109 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005110 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005111 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005112 return 0;
5113 }
5114
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005115 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005116
5117 return 0;
5118}
5119
James Morris88e9d342009-09-22 16:43:43 -07005120static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005121 .start = g_start,
5122 .next = g_next,
5123 .stop = g_stop,
5124 .show = g_show,
5125};
5126
5127static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005128__ftrace_graph_open(struct inode *inode, struct file *file,
5129 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005130{
5131 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005132 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005133
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005134 if (file->f_mode & FMODE_WRITE) {
5135 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5136
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005137 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5138 return -ENOMEM;
5139
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005140 if (file->f_flags & O_TRUNC)
5141 new_hash = alloc_ftrace_hash(size_bits);
5142 else
5143 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5144 fgd->hash);
5145 if (!new_hash) {
5146 ret = -ENOMEM;
5147 goto out;
5148 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005149 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005150
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005151 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005152 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005153 if (!ret) {
5154 struct seq_file *m = file->private_data;
5155 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005156 } else {
5157 /* Failed */
5158 free_ftrace_hash(new_hash);
5159 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005160 }
5161 } else
5162 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005163
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005164out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005165 if (ret < 0 && file->f_mode & FMODE_WRITE)
5166 trace_parser_put(&fgd->parser);
5167
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005168 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005169
5170 /*
5171 * All uses of fgd->hash must be taken with the graph_lock
5172 * held. The graph_lock is going to be released, so force
5173 * fgd->hash to be reinitialized when it is taken again.
5174 */
5175 fgd->hash = NULL;
5176
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005177 return ret;
5178}
5179
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005180static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005181ftrace_graph_open(struct inode *inode, struct file *file)
5182{
5183 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005184 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005185
5186 if (unlikely(ftrace_disabled))
5187 return -ENODEV;
5188
5189 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5190 if (fgd == NULL)
5191 return -ENOMEM;
5192
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005193 mutex_lock(&graph_lock);
5194
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005195 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5196 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005197 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005198 fgd->seq_ops = &ftrace_graph_seq_ops;
5199
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005200 ret = __ftrace_graph_open(inode, file, fgd);
5201 if (ret < 0)
5202 kfree(fgd);
5203
5204 mutex_unlock(&graph_lock);
5205 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005206}
5207
5208static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005209ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5210{
5211 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005212 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005213
5214 if (unlikely(ftrace_disabled))
5215 return -ENODEV;
5216
5217 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5218 if (fgd == NULL)
5219 return -ENOMEM;
5220
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005221 mutex_lock(&graph_lock);
5222
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005223 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5224 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005225 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005226 fgd->seq_ops = &ftrace_graph_seq_ops;
5227
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005228 ret = __ftrace_graph_open(inode, file, fgd);
5229 if (ret < 0)
5230 kfree(fgd);
5231
5232 mutex_unlock(&graph_lock);
5233 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005234}
5235
5236static int
Li Zefan87827112009-07-23 11:29:11 +08005237ftrace_graph_release(struct inode *inode, struct file *file)
5238{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005239 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005240 struct ftrace_hash *old_hash, *new_hash;
5241 struct trace_parser *parser;
5242 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005243
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005244 if (file->f_mode & FMODE_READ) {
5245 struct seq_file *m = file->private_data;
5246
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005247 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005248 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005249 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005250 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005251 }
5252
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005253
5254 if (file->f_mode & FMODE_WRITE) {
5255
5256 parser = &fgd->parser;
5257
5258 if (trace_parser_loaded((parser))) {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005259 ret = ftrace_graph_set_hash(fgd->new_hash,
5260 parser->buffer);
5261 }
5262
5263 trace_parser_put(parser);
5264
5265 new_hash = __ftrace_hash_move(fgd->new_hash);
5266 if (!new_hash) {
5267 ret = -ENOMEM;
5268 goto out;
5269 }
5270
5271 mutex_lock(&graph_lock);
5272
5273 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5274 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5275 lockdep_is_held(&graph_lock));
5276 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5277 } else {
5278 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5279 lockdep_is_held(&graph_lock));
5280 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5281 }
5282
5283 mutex_unlock(&graph_lock);
5284
5285 /* Wait till all users are no longer using the old hash */
5286 synchronize_sched();
5287
5288 free_ftrace_hash(old_hash);
5289 }
5290
5291 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005292 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005293 kfree(fgd);
5294
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005295 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005296}
5297
5298static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005299ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005300{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005301 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005302 struct dyn_ftrace *rec;
5303 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005304 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005305 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005306 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005307
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005308 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005309 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5310 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005311
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005312 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005313
Steven Rostedt52baf112009-02-14 01:15:39 -05005314 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005315
5316 if (unlikely(ftrace_disabled)) {
5317 mutex_unlock(&ftrace_lock);
5318 return -ENODEV;
5319 }
5320
Steven Rostedt265c8312009-02-13 12:43:56 -05005321 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005322
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005323 if (rec->flags & FTRACE_FL_DISABLED)
5324 continue;
5325
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005326 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005327 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005328
5329 if (!not) {
5330 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005331
5332 if (entry)
5333 continue;
5334 if (add_hash_entry(hash, rec->ip) < 0)
5335 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005336 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005337 if (entry) {
5338 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005339 fail = 0;
5340 }
5341 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005342 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005343 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005344out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005345 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005346
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005347 if (fail)
5348 return -EINVAL;
5349
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005350 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005351}
5352
5353static ssize_t
5354ftrace_graph_write(struct file *file, const char __user *ubuf,
5355 size_t cnt, loff_t *ppos)
5356{
Namhyung Kim6a101082013-10-14 17:24:25 +09005357 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005358 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005359 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005360
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005361 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005362 return 0;
5363
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005364 /* Read mode uses seq functions */
5365 if (file->f_mode & FMODE_READ) {
5366 struct seq_file *m = file->private_data;
5367 fgd = m->private;
5368 }
5369
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005370 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005371
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005372 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005373
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005374 if (read >= 0 && trace_parser_loaded(parser) &&
5375 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005376
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005377 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005378 parser->buffer);
5379 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005380 }
5381
Namhyung Kim6a101082013-10-14 17:24:25 +09005382 if (!ret)
5383 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005384
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005385 return ret;
5386}
5387
5388static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005389 .open = ftrace_graph_open,
5390 .read = seq_read,
5391 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005392 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005393 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005394};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005395
5396static const struct file_operations ftrace_graph_notrace_fops = {
5397 .open = ftrace_graph_notrace_open,
5398 .read = seq_read,
5399 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005400 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005401 .release = ftrace_graph_release,
5402};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005403#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5404
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005405void ftrace_create_filter_files(struct ftrace_ops *ops,
5406 struct dentry *parent)
5407{
5408
5409 trace_create_file("set_ftrace_filter", 0644, parent,
5410 ops, &ftrace_filter_fops);
5411
5412 trace_create_file("set_ftrace_notrace", 0644, parent,
5413 ops, &ftrace_notrace_fops);
5414}
5415
5416/*
5417 * The name "destroy_filter_files" is really a misnomer. Although
5418 * in the future, it may actualy delete the files, but this is
5419 * really intended to make sure the ops passed in are disabled
5420 * and that when this function returns, the caller is free to
5421 * free the ops.
5422 *
5423 * The "destroy" name is only to match the "create" name that this
5424 * should be paired with.
5425 */
5426void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5427{
5428 mutex_lock(&ftrace_lock);
5429 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5430 ftrace_shutdown(ops, 0);
5431 ops->flags |= FTRACE_OPS_FL_DELETED;
5432 mutex_unlock(&ftrace_lock);
5433}
5434
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005435static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005436{
Steven Rostedt5072c592008-05-12 21:20:43 +02005437
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005438 trace_create_file("available_filter_functions", 0444,
5439 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005440
Steven Rostedt647bcd02011-05-03 14:39:21 -04005441 trace_create_file("enabled_functions", 0444,
5442 d_tracer, NULL, &ftrace_enabled_fops);
5443
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005444 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005445
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005446#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Chen LinX1ce05002014-09-03 14:31:09 +08005447 trace_create_file("set_graph_function", 0644, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005448 NULL,
5449 &ftrace_graph_fops);
Chen LinX1ce05002014-09-03 14:31:09 +08005450 trace_create_file("set_graph_notrace", 0644, d_tracer,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005451 NULL,
5452 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005453#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5454
Steven Rostedt5072c592008-05-12 21:20:43 +02005455 return 0;
5456}
5457
Steven Rostedt9fd49322012-04-24 22:32:06 -04005458static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005459{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005460 const unsigned long *ipa = a;
5461 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005462
Steven Rostedt9fd49322012-04-24 22:32:06 -04005463 if (*ipa > *ipb)
5464 return 1;
5465 if (*ipa < *ipb)
5466 return -1;
5467 return 0;
5468}
5469
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005470static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005471 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005472 unsigned long *end)
5473{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005474 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005475 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005476 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005477 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005478 unsigned long *p;
5479 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005480 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005481 int ret = -ENOMEM;
5482
5483 count = end - start;
5484
5485 if (!count)
5486 return 0;
5487
Steven Rostedt9fd49322012-04-24 22:32:06 -04005488 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005489 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005490
Steven Rostedt706c81f2012-04-24 23:45:26 -04005491 start_pg = ftrace_allocate_pages(count);
5492 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005493 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005494
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005495 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005496
Steven Rostedt320823092011-12-16 14:42:37 -05005497 /*
5498 * Core and each module needs their own pages, as
5499 * modules will free them when they are removed.
5500 * Force a new page to be allocated for modules.
5501 */
Steven Rostedta7900872011-12-16 16:23:44 -05005502 if (!mod) {
5503 WARN_ON(ftrace_pages || ftrace_pages_start);
5504 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005505 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005506 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005507 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005508 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005509
Steven Rostedta7900872011-12-16 16:23:44 -05005510 if (WARN_ON(ftrace_pages->next)) {
5511 /* Hmm, we have free pages? */
5512 while (ftrace_pages->next)
5513 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005514 }
Steven Rostedta7900872011-12-16 16:23:44 -05005515
Steven Rostedt706c81f2012-04-24 23:45:26 -04005516 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005517 }
5518
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005519 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005520 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005521 while (p < end) {
5522 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005523 /*
5524 * Some architecture linkers will pad between
5525 * the different mcount_loc sections of different
5526 * object files to satisfy alignments.
5527 * Skip any NULL pointers.
5528 */
5529 if (!addr)
5530 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005531
5532 if (pg->index == pg->size) {
5533 /* We should have allocated enough */
5534 if (WARN_ON(!pg->next))
5535 break;
5536 pg = pg->next;
5537 }
5538
5539 rec = &pg->records[pg->index++];
5540 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005541 }
5542
Steven Rostedt706c81f2012-04-24 23:45:26 -04005543 /* We should have used all pages */
5544 WARN_ON(pg->next);
5545
5546 /* Assign the last page to ftrace_pages */
5547 ftrace_pages = pg;
5548
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005549 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005550 * We only need to disable interrupts on start up
5551 * because we are modifying code that an interrupt
5552 * may execute, and the modification is not atomic.
5553 * But for modules, nothing runs the code we modify
5554 * until we are finished with it, and there's no
5555 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005556 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005557 if (!mod)
5558 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005559 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005560 if (!mod)
5561 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005562 ret = 0;
5563 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005564 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005565
Steven Rostedta7900872011-12-16 16:23:44 -05005566 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005567}
5568
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005569struct ftrace_mod_func {
5570 struct list_head list;
5571 char *name;
5572 unsigned long ip;
5573 unsigned int size;
5574};
5575
5576struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005577 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005578 struct list_head list;
5579 struct module *mod;
5580 unsigned long start_addr;
5581 unsigned long end_addr;
5582 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005583 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005584};
5585
Steven Rostedt93eb6772009-04-15 13:24:06 -04005586#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005587
5588#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5589
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005590static LIST_HEAD(ftrace_mod_maps);
5591
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005592static int referenced_filters(struct dyn_ftrace *rec)
5593{
5594 struct ftrace_ops *ops;
5595 int cnt = 0;
5596
5597 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5598 if (ops_references_rec(ops, rec))
5599 cnt++;
5600 }
5601
5602 return cnt;
5603}
5604
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005605static void
5606clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5607{
5608 struct ftrace_func_entry *entry;
5609 struct dyn_ftrace *rec;
5610 int i;
5611
5612 if (ftrace_hash_empty(hash))
5613 return;
5614
5615 for (i = 0; i < pg->index; i++) {
5616 rec = &pg->records[i];
5617 entry = __ftrace_lookup_ip(hash, rec->ip);
5618 /*
5619 * Do not allow this rec to match again.
5620 * Yeah, it may waste some memory, but will be removed
5621 * if/when the hash is modified again.
5622 */
5623 if (entry)
5624 entry->ip = 0;
5625 }
5626}
5627
5628/* Clear any records from hashs */
5629static void clear_mod_from_hashes(struct ftrace_page *pg)
5630{
5631 struct trace_array *tr;
5632
5633 mutex_lock(&trace_types_lock);
5634 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5635 if (!tr->ops || !tr->ops->func_hash)
5636 continue;
5637 mutex_lock(&tr->ops->func_hash->regex_lock);
5638 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5639 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5640 mutex_unlock(&tr->ops->func_hash->regex_lock);
5641 }
5642 mutex_unlock(&trace_types_lock);
5643}
5644
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005645static void ftrace_free_mod_map(struct rcu_head *rcu)
5646{
5647 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5648 struct ftrace_mod_func *mod_func;
5649 struct ftrace_mod_func *n;
5650
5651 /* All the contents of mod_map are now not visible to readers */
5652 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5653 kfree(mod_func->name);
5654 list_del(&mod_func->list);
5655 kfree(mod_func);
5656 }
5657
5658 kfree(mod_map);
5659}
5660
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005661void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005662{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005663 struct ftrace_mod_map *mod_map;
5664 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005665 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005666 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005667 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005668 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005669 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005670
Steven Rostedt93eb6772009-04-15 13:24:06 -04005671 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005672
5673 if (ftrace_disabled)
5674 goto out_unlock;
5675
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005676 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5677 if (mod_map->mod == mod) {
5678 list_del_rcu(&mod_map->list);
5679 call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5680 break;
5681 }
5682 }
5683
Steven Rostedt320823092011-12-16 14:42:37 -05005684 /*
5685 * Each module has its own ftrace_pages, remove
5686 * them from the list.
5687 */
5688 last_pg = &ftrace_pages_start;
5689 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5690 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005691 if (within_module_core(rec->ip, mod) ||
5692 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005693 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005694 * As core pages are first, the first
5695 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005696 */
Steven Rostedt320823092011-12-16 14:42:37 -05005697 if (WARN_ON(pg == ftrace_pages_start))
5698 goto out_unlock;
5699
5700 /* Check if we are deleting the last page */
5701 if (pg == ftrace_pages)
5702 ftrace_pages = next_to_ftrace_page(last_pg);
5703
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04005704 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05005705 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005706
5707 pg->next = tmp_page;
5708 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005709 } else
5710 last_pg = &pg->next;
5711 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005712 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005713 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005714
5715 for (pg = tmp_page; pg; pg = tmp_page) {
5716
5717 /* Needs to be called outside of ftrace_lock */
5718 clear_mod_from_hashes(pg);
5719
5720 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5721 free_pages((unsigned long)pg->records, order);
5722 tmp_page = pg->next;
5723 kfree(pg);
5724 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04005725}
5726
Jessica Yu7dcd1822016-02-16 17:32:33 -05005727void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005728{
5729 struct dyn_ftrace *rec;
5730 struct ftrace_page *pg;
5731
5732 mutex_lock(&ftrace_lock);
5733
5734 if (ftrace_disabled)
5735 goto out_unlock;
5736
5737 /*
5738 * If the tracing is enabled, go ahead and enable the record.
5739 *
5740 * The reason not to enable the record immediatelly is the
5741 * inherent check of ftrace_make_nop/ftrace_make_call for
5742 * correct previous instructions. Making first the NOP
5743 * conversion puts the module to the correct state, thus
5744 * passing the ftrace_make_call check.
5745 *
5746 * We also delay this to after the module code already set the
5747 * text to read-only, as we now need to set it back to read-write
5748 * so that we can modify the text.
5749 */
5750 if (ftrace_start_up)
5751 ftrace_arch_code_modify_prepare();
5752
5753 do_for_each_ftrace_rec(pg, rec) {
5754 int cnt;
5755 /*
5756 * do_for_each_ftrace_rec() is a double loop.
5757 * module text shares the pg. If a record is
5758 * not part of this module, then skip this pg,
5759 * which the "break" will do.
5760 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005761 if (!within_module_core(rec->ip, mod) &&
5762 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005763 break;
5764
5765 cnt = 0;
5766
5767 /*
5768 * When adding a module, we need to check if tracers are
5769 * currently enabled and if they are, and can trace this record,
5770 * we need to enable the module functions as well as update the
5771 * reference counts for those function records.
5772 */
5773 if (ftrace_start_up)
5774 cnt += referenced_filters(rec);
5775
5776 /* This clears FTRACE_FL_DISABLED */
5777 rec->flags = cnt;
5778
5779 if (ftrace_start_up && cnt) {
5780 int failed = __ftrace_replace_code(rec, 1);
5781 if (failed) {
5782 ftrace_bug(failed, rec);
5783 goto out_loop;
5784 }
5785 }
5786
5787 } while_for_each_ftrace_rec();
5788
5789 out_loop:
5790 if (ftrace_start_up)
5791 ftrace_arch_code_modify_post_process();
5792
5793 out_unlock:
5794 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04005795
5796 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005797}
5798
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005799void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005800{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005801 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005802 return;
5803
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005804 ftrace_process_locs(mod, mod->ftrace_callsites,
5805 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005806}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005807
5808static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5809 struct dyn_ftrace *rec)
5810{
5811 struct ftrace_mod_func *mod_func;
5812 unsigned long symsize;
5813 unsigned long offset;
5814 char str[KSYM_SYMBOL_LEN];
5815 char *modname;
5816 const char *ret;
5817
5818 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5819 if (!ret)
5820 return;
5821
5822 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5823 if (!mod_func)
5824 return;
5825
5826 mod_func->name = kstrdup(str, GFP_KERNEL);
5827 if (!mod_func->name) {
5828 kfree(mod_func);
5829 return;
5830 }
5831
5832 mod_func->ip = rec->ip - offset;
5833 mod_func->size = symsize;
5834
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005835 mod_map->num_funcs++;
5836
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005837 list_add_rcu(&mod_func->list, &mod_map->funcs);
5838}
5839
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005840static struct ftrace_mod_map *
5841allocate_ftrace_mod_map(struct module *mod,
5842 unsigned long start, unsigned long end)
5843{
5844 struct ftrace_mod_map *mod_map;
5845
5846 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5847 if (!mod_map)
5848 return NULL;
5849
5850 mod_map->mod = mod;
5851 mod_map->start_addr = start;
5852 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005853 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005854
5855 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5856
5857 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5858
5859 return mod_map;
5860}
5861
5862static const char *
5863ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5864 unsigned long addr, unsigned long *size,
5865 unsigned long *off, char *sym)
5866{
5867 struct ftrace_mod_func *found_func = NULL;
5868 struct ftrace_mod_func *mod_func;
5869
5870 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5871 if (addr >= mod_func->ip &&
5872 addr < mod_func->ip + mod_func->size) {
5873 found_func = mod_func;
5874 break;
5875 }
5876 }
5877
5878 if (found_func) {
5879 if (size)
5880 *size = found_func->size;
5881 if (off)
5882 *off = addr - found_func->ip;
5883 if (sym)
5884 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5885
5886 return found_func->name;
5887 }
5888
5889 return NULL;
5890}
5891
5892const char *
5893ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5894 unsigned long *off, char **modname, char *sym)
5895{
5896 struct ftrace_mod_map *mod_map;
5897 const char *ret = NULL;
5898
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005899 /* mod_map is freed via call_rcu_sched() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005900 preempt_disable();
5901 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5902 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
5903 if (ret) {
5904 if (modname)
5905 *modname = mod_map->mod->name;
5906 break;
5907 }
5908 }
5909 preempt_enable();
5910
5911 return ret;
5912}
5913
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005914int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
5915 char *type, char *name,
5916 char *module_name, int *exported)
5917{
5918 struct ftrace_mod_map *mod_map;
5919 struct ftrace_mod_func *mod_func;
5920
5921 preempt_disable();
5922 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5923
5924 if (symnum >= mod_map->num_funcs) {
5925 symnum -= mod_map->num_funcs;
5926 continue;
5927 }
5928
5929 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5930 if (symnum > 1) {
5931 symnum--;
5932 continue;
5933 }
5934
5935 *value = mod_func->ip;
5936 *type = 'T';
5937 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
5938 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
5939 *exported = 1;
5940 preempt_enable();
5941 return 0;
5942 }
5943 WARN_ON(1);
5944 break;
5945 }
5946 preempt_enable();
5947 return -ERANGE;
5948}
5949
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005950#else
5951static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5952 struct dyn_ftrace *rec) { }
5953static inline struct ftrace_mod_map *
5954allocate_ftrace_mod_map(struct module *mod,
5955 unsigned long start, unsigned long end)
5956{
5957 return NULL;
5958}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005959#endif /* CONFIG_MODULES */
5960
Joel Fernandes8715b102017-10-09 12:29:31 -07005961struct ftrace_init_func {
5962 struct list_head list;
5963 unsigned long ip;
5964};
5965
5966/* Clear any init ips from hashes */
5967static void
5968clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005969{
Joel Fernandes8715b102017-10-09 12:29:31 -07005970 struct ftrace_func_entry *entry;
5971
5972 if (ftrace_hash_empty(hash))
5973 return;
5974
5975 entry = __ftrace_lookup_ip(hash, func->ip);
5976
5977 /*
5978 * Do not allow this rec to match again.
5979 * Yeah, it may waste some memory, but will be removed
5980 * if/when the hash is modified again.
5981 */
5982 if (entry)
5983 entry->ip = 0;
5984}
5985
5986static void
5987clear_func_from_hashes(struct ftrace_init_func *func)
5988{
5989 struct trace_array *tr;
5990
5991 mutex_lock(&trace_types_lock);
5992 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5993 if (!tr->ops || !tr->ops->func_hash)
5994 continue;
5995 mutex_lock(&tr->ops->func_hash->regex_lock);
5996 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
5997 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
5998 mutex_unlock(&tr->ops->func_hash->regex_lock);
5999 }
6000 mutex_unlock(&trace_types_lock);
6001}
6002
6003static void add_to_clear_hash_list(struct list_head *clear_list,
6004 struct dyn_ftrace *rec)
6005{
6006 struct ftrace_init_func *func;
6007
6008 func = kmalloc(sizeof(*func), GFP_KERNEL);
6009 if (!func) {
6010 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6011 return;
6012 }
6013
6014 func->ip = rec->ip;
6015 list_add(&func->list, clear_list);
6016}
6017
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006018void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006019{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006020 unsigned long start = (unsigned long)(start_ptr);
6021 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006022 struct ftrace_page **last_pg = &ftrace_pages_start;
6023 struct ftrace_page *pg;
6024 struct dyn_ftrace *rec;
6025 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006026 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07006027 struct ftrace_init_func *func, *func_next;
6028 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006029 int order;
6030
Joel Fernandes8715b102017-10-09 12:29:31 -07006031 INIT_LIST_HEAD(&clear_hash);
6032
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006033 key.ip = start;
6034 key.flags = end; /* overload flags, as it is unsigned long */
6035
6036 mutex_lock(&ftrace_lock);
6037
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006038 /*
6039 * If we are freeing module init memory, then check if
6040 * any tracer is active. If so, we need to save a mapping of
6041 * the module functions being freed with the address.
6042 */
6043 if (mod && ftrace_ops_list != &ftrace_list_end)
6044 mod_map = allocate_ftrace_mod_map(mod, start, end);
6045
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006046 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6047 if (end < pg->records[0].ip ||
6048 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6049 continue;
6050 again:
6051 rec = bsearch(&key, pg->records, pg->index,
6052 sizeof(struct dyn_ftrace),
6053 ftrace_cmp_recs);
6054 if (!rec)
6055 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006056
Joel Fernandes8715b102017-10-09 12:29:31 -07006057 /* rec will be cleared from hashes after ftrace_lock unlock */
6058 add_to_clear_hash_list(&clear_hash, rec);
6059
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006060 if (mod_map)
6061 save_ftrace_mod_rec(mod_map, rec);
6062
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006063 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04006064 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006065 if (!pg->index) {
6066 *last_pg = pg->next;
6067 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6068 free_pages((unsigned long)pg->records, order);
6069 kfree(pg);
6070 pg = container_of(last_pg, struct ftrace_page, next);
6071 if (!(*last_pg))
6072 ftrace_pages = pg;
6073 continue;
6074 }
6075 memmove(rec, rec + 1,
6076 (pg->index - (rec - pg->records)) * sizeof(*rec));
6077 /* More than one function may be in this block */
6078 goto again;
6079 }
6080 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07006081
6082 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6083 clear_func_from_hashes(func);
6084 kfree(func);
6085 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006086}
6087
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006088void __init ftrace_free_init_mem(void)
6089{
6090 void *start = (void *)(&__init_begin);
6091 void *end = (void *)(&__init_end);
6092
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006093 ftrace_free_mem(NULL, start, end);
Steven Rostedt93eb6772009-04-15 13:24:06 -04006094}
6095
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006096void __init ftrace_init(void)
6097{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006098 extern unsigned long __start_mcount_loc[];
6099 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006100 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006101 int ret;
6102
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006103 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006104 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006105 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01006106 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006107 goto failed;
6108
6109 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006110 if (!count) {
6111 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006112 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006113 }
6114
6115 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6116 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006117
6118 last_ftrace_enabled = ftrace_enabled = 1;
6119
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006120 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08006121 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006122 __stop_mcount_loc);
6123
Steven Rostedt2af15d62009-05-28 13:37:24 -04006124 set_ftrace_early_filters();
6125
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006126 return;
6127 failed:
6128 ftrace_disabled = 1;
6129}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006130
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006131/* Do nothing if arch does not support this */
6132void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6133{
6134}
6135
6136static void ftrace_update_trampoline(struct ftrace_ops *ops)
6137{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006138 arch_ftrace_update_trampoline(ops);
6139}
6140
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006141void ftrace_init_trace_array(struct trace_array *tr)
6142{
6143 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04006144 INIT_LIST_HEAD(&tr->mod_trace);
6145 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006146}
Steven Rostedt3d083392008-05-12 21:20:42 +02006147#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006148
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05006149struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04006150 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006151 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6152 FTRACE_OPS_FL_INITIALIZED |
6153 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04006154};
6155
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006156static int __init ftrace_nodyn_init(void)
6157{
6158 ftrace_enabled = 1;
6159 return 0;
6160}
Steven Rostedt6f415672012-10-05 12:13:07 -04006161core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006162
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006163static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006164static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006165static inline void ftrace_startup_all(int command) { }
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006166
Ingo Molnarc7aafc52008-05-12 21:20:45 +02006167# define ftrace_startup_sysctl() do { } while (0)
6168# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04006169
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006170static void ftrace_update_trampoline(struct ftrace_ops *ops)
6171{
6172}
6173
Steven Rostedt3d083392008-05-12 21:20:42 +02006174#endif /* CONFIG_DYNAMIC_FTRACE */
6175
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006176__init void ftrace_init_global_array_ops(struct trace_array *tr)
6177{
6178 tr->ops = &global_ops;
6179 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006180 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006181}
6182
6183void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6184{
6185 /* If we filter on pids, update to use the pid function */
6186 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6187 if (WARN_ON(tr->ops->func != ftrace_stub))
6188 printk("ftrace ops had %pS for function\n",
6189 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006190 }
6191 tr->ops->func = func;
6192 tr->ops->private = tr;
6193}
6194
6195void ftrace_reset_array_ops(struct trace_array *tr)
6196{
6197 tr->ops->func = ftrace_stub;
6198}
6199
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006200static inline void
6201__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006202 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006203{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006204 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006205 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04006206
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006207 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6208 if (bit < 0)
6209 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04006210
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006211 /*
6212 * Some of the ops may be dynamically allocated,
6213 * they must be freed after a synchronize_sched().
6214 */
6215 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006216
Steven Rostedt0a016402012-11-02 17:03:03 -04006217 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006218 /*
6219 * Check the following for each ops before calling their func:
6220 * if RCU flag is set, then rcu_is_watching() must be true
6221 * if PER_CPU is set, then ftrace_function_local_disable()
6222 * must be false
6223 * Otherwise test if the ip matches the ops filter
6224 *
6225 * If any of the above fails then the op->func() is not executed.
6226 */
6227 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006228 ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04006229 if (FTRACE_WARN_ON(!op->func)) {
6230 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006231 goto out;
6232 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04006233 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006234 }
Steven Rostedt0a016402012-11-02 17:03:03 -04006235 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006236out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006237 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006238 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04006239}
6240
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006241/*
6242 * Some archs only support passing ip and parent_ip. Even though
6243 * the list function ignores the op parameter, we do not want any
6244 * C side effects, where a function is called without the caller
6245 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006246 * Archs are to support both the regs and ftrace_ops at the same time.
6247 * If they support ftrace_ops, it is assumed they support regs.
6248 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09006249 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6250 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006251 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08006252 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006253 */
6254#if ARCH_SUPPORTS_FTRACE_OPS
6255static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006256 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006257{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006258 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006259}
6260#else
6261static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6262{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006263 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006264}
6265#endif
6266
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006267/*
6268 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006269 * recursion, needs RCU protection and/or requires per cpu handling, then
6270 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006271 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006272static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006273 struct ftrace_ops *op, struct pt_regs *regs)
6274{
6275 int bit;
6276
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006277 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6278 return;
6279
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006280 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6281 if (bit < 0)
6282 return;
6283
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006284 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006285
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006286 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006287
6288 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006289 trace_clear_recursion(bit);
6290}
6291
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006292/**
6293 * ftrace_ops_get_func - get the function a trampoline should call
6294 * @ops: the ops to get the function for
6295 *
6296 * Normally the mcount trampoline will call the ops->func, but there
6297 * are times that it should not. For example, if the ops does not
6298 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08006299 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006300 *
6301 * Returns the function that the trampoline should call for @ops.
6302 */
6303ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6304{
6305 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006306 * If the function does not handle recursion, needs to be RCU safe,
6307 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006308 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006309 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006310 ops->flags & FTRACE_OPS_FL_RCU)
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006311 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006312
6313 return ops->func;
6314}
6315
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006316static void
6317ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6318 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05006319{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006320 struct trace_array *tr = data;
6321 struct trace_pid_list *pid_list;
6322
6323 pid_list = rcu_dereference_sched(tr->function_pids);
6324
6325 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6326 trace_ignore_this_task(pid_list, next));
6327}
6328
Namhyung Kim1e104862017-04-17 11:44:28 +09006329static void
6330ftrace_pid_follow_sched_process_fork(void *data,
6331 struct task_struct *self,
6332 struct task_struct *task)
6333{
6334 struct trace_pid_list *pid_list;
6335 struct trace_array *tr = data;
6336
6337 pid_list = rcu_dereference_sched(tr->function_pids);
6338 trace_filter_add_remove_task(pid_list, self, task);
6339}
6340
6341static void
6342ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6343{
6344 struct trace_pid_list *pid_list;
6345 struct trace_array *tr = data;
6346
6347 pid_list = rcu_dereference_sched(tr->function_pids);
6348 trace_filter_add_remove_task(pid_list, NULL, task);
6349}
6350
6351void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6352{
6353 if (enable) {
6354 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6355 tr);
6356 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6357 tr);
6358 } else {
6359 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6360 tr);
6361 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6362 tr);
6363 }
6364}
6365
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006366static void clear_ftrace_pids(struct trace_array *tr)
6367{
6368 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05006369 int cpu;
6370
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006371 pid_list = rcu_dereference_protected(tr->function_pids,
6372 lockdep_is_held(&ftrace_lock));
6373 if (!pid_list)
6374 return;
6375
6376 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6377
6378 for_each_possible_cpu(cpu)
6379 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6380
6381 rcu_assign_pointer(tr->function_pids, NULL);
6382
6383 /* Wait till all users are no longer using pid filtering */
6384 synchronize_sched();
6385
6386 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05006387}
6388
Namhyung Kimd879d0b2017-04-17 11:44:27 +09006389void ftrace_clear_pids(struct trace_array *tr)
6390{
6391 mutex_lock(&ftrace_lock);
6392
6393 clear_ftrace_pids(tr);
6394
6395 mutex_unlock(&ftrace_lock);
6396}
6397
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006398static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05006399{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006400 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006401 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006402
6403 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006404 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006405
6406 mutex_unlock(&ftrace_lock);
6407}
6408
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006409/* Greater than any max PID */
6410#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6411
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006412static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006413 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006414{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006415 struct trace_pid_list *pid_list;
6416 struct trace_array *tr = m->private;
6417
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006418 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006419 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006420
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006421 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006422
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006423 if (!pid_list)
6424 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6425
6426 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006427}
6428
6429static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6430{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006431 struct trace_array *tr = m->private;
6432 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6433
6434 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006435 return NULL;
6436
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006437 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006438}
6439
6440static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006441 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006442{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006443 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006444 mutex_unlock(&ftrace_lock);
6445}
6446
6447static int fpid_show(struct seq_file *m, void *v)
6448{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006449 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006450 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006451 return 0;
6452 }
6453
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006454 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006455}
6456
6457static const struct seq_operations ftrace_pid_sops = {
6458 .start = fpid_start,
6459 .next = fpid_next,
6460 .stop = fpid_stop,
6461 .show = fpid_show,
6462};
6463
6464static int
6465ftrace_pid_open(struct inode *inode, struct file *file)
6466{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006467 struct trace_array *tr = inode->i_private;
6468 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006469 int ret = 0;
6470
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006471 if (trace_array_get(tr) < 0)
6472 return -ENODEV;
6473
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006474 if ((file->f_mode & FMODE_WRITE) &&
6475 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006476 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006477
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006478 ret = seq_open(file, &ftrace_pid_sops);
6479 if (ret < 0) {
6480 trace_array_put(tr);
6481 } else {
6482 m = file->private_data;
6483 /* copy tr over to seq ops */
6484 m->private = tr;
6485 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006486
6487 return ret;
6488}
6489
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006490static void ignore_task_cpu(void *data)
6491{
6492 struct trace_array *tr = data;
6493 struct trace_pid_list *pid_list;
6494
6495 /*
6496 * This function is called by on_each_cpu() while the
6497 * event_mutex is held.
6498 */
6499 pid_list = rcu_dereference_protected(tr->function_pids,
6500 mutex_is_locked(&ftrace_lock));
6501
6502 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6503 trace_ignore_this_task(pid_list, current));
6504}
6505
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006506static ssize_t
6507ftrace_pid_write(struct file *filp, const char __user *ubuf,
6508 size_t cnt, loff_t *ppos)
6509{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006510 struct seq_file *m = filp->private_data;
6511 struct trace_array *tr = m->private;
6512 struct trace_pid_list *filtered_pids = NULL;
6513 struct trace_pid_list *pid_list;
6514 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006515
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006516 if (!cnt)
6517 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006518
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006519 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006520
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006521 filtered_pids = rcu_dereference_protected(tr->function_pids,
6522 lockdep_is_held(&ftrace_lock));
6523
6524 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6525 if (ret < 0)
6526 goto out;
6527
6528 rcu_assign_pointer(tr->function_pids, pid_list);
6529
6530 if (filtered_pids) {
6531 synchronize_sched();
6532 trace_free_pid_list(filtered_pids);
6533 } else if (pid_list) {
6534 /* Register a probe to set whether to ignore the tracing of a task */
6535 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6536 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006537
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006538 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006539 * Ignoring of pids is done at task switch. But we have to
6540 * check for those tasks that are currently running.
6541 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006542 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006543 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006544
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006545 ftrace_update_pid_func();
6546 ftrace_startup_all(0);
6547 out:
6548 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006549
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006550 if (ret > 0)
6551 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05006552
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006553 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006554}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006555
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006556static int
6557ftrace_pid_release(struct inode *inode, struct file *file)
6558{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006559 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006560
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006561 trace_array_put(tr);
6562
6563 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006564}
6565
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006566static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006567 .open = ftrace_pid_open,
6568 .write = ftrace_pid_write,
6569 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006570 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006571 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006572};
6573
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006574void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006575{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006576 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006577 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006578}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006579
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04006580void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6581 struct dentry *d_tracer)
6582{
6583 /* Only the top level directory has the dyn_tracefs and profile */
6584 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6585
6586 ftrace_init_dyn_tracefs(d_tracer);
6587 ftrace_profile_tracefs(d_tracer);
6588}
6589
Steven Rostedt3d083392008-05-12 21:20:42 +02006590/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006591 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006592 *
6593 * This function should be used by panic code. It stops ftrace
6594 * but in a not so nice way. If you need to simply kill ftrace
6595 * from a non-atomic section, use ftrace_kill.
6596 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006597void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006598{
6599 ftrace_disabled = 1;
6600 ftrace_enabled = 0;
Yisheng Xie5ccba642018-02-02 10:14:49 +08006601 ftrace_trace_function = ftrace_stub;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006602}
6603
6604/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04006605 * Test if ftrace is dead or not.
6606 */
6607int ftrace_is_dead(void)
6608{
6609 return ftrace_disabled;
6610}
6611
6612/**
Steven Rostedt3d083392008-05-12 21:20:42 +02006613 * register_ftrace_function - register a function for profiling
6614 * @ops - ops structure that holds the function for profiling.
6615 *
6616 * Register a function to be called by all functions in the
6617 * kernel.
6618 *
6619 * Note: @ops->func and all the functions it calls must be labeled
6620 * with "notrace", otherwise it will go into a
6621 * recursive loop.
6622 */
6623int register_ftrace_function(struct ftrace_ops *ops)
6624{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006625 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006626
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09006627 ftrace_ops_init(ops);
6628
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006629 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006630
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006631 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04006632
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006633 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02006634
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006635 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02006636}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006637EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02006638
6639/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01006640 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02006641 * @ops - ops structure that holds the function to unregister
6642 *
6643 * Unregister a function that was added to be called by ftrace profiling.
6644 */
6645int unregister_ftrace_function(struct ftrace_ops *ops)
6646{
6647 int ret;
6648
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006649 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006650 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006651 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006652
6653 return ret;
6654}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006655EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006656
Ingo Molnare309b412008-05-12 21:20:51 +02006657int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006658ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006659 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006660 loff_t *ppos)
6661{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006662 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006663
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006664 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006665
Steven Rostedt45a4a232011-04-21 23:16:46 -04006666 if (unlikely(ftrace_disabled))
6667 goto out;
6668
6669 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006670
Li Zefana32c7762009-06-26 16:55:51 +08006671 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006672 goto out;
6673
Li Zefana32c7762009-06-26 16:55:51 +08006674 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006675
6676 if (ftrace_enabled) {
6677
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006678 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08006679 if (rcu_dereference_protected(ftrace_ops_list,
6680 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01006681 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006682
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006683 ftrace_startup_sysctl();
6684
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006685 } else {
6686 /* stopping ftrace calls (just send to ftrace_stub) */
6687 ftrace_trace_function = ftrace_stub;
6688
6689 ftrace_shutdown_sysctl();
6690 }
6691
6692 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006693 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006694 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006695}