blob: fa79323331b22f327872ae41e185a17562f6d78a [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)a0572f62018-12-05 12:48:53 -050080enum {
81 FTRACE_MODIFY_ENABLE_FL = (1 << 0),
82 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
83};
84
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050085struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040086 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040087 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040088 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040089};
90
Steven Rostedt4eebcc82008-05-12 21:20:48 +020091/* ftrace_enabled is a method to turn ftrace on or off */
92int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020093static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020094
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040095/* Current function tracing op */
96struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050097/* What to set function_trace_op to */
98static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050099
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400100static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400101{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400102 struct trace_array *tr;
103
104 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
105 return false;
106
107 tr = ops->private;
108
109 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400110}
111
112static void ftrace_update_trampoline(struct ftrace_ops *ops);
113
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200114/*
115 * ftrace_disabled is set when an anomaly is discovered.
116 * ftrace_disabled is much stronger than ftrace_enabled.
117 */
118static int ftrace_disabled __read_mostly;
119
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500120DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200121
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500122struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200123ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500124struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400126#if ARCH_SUPPORTS_FTRACE_OPS
127static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400128 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400129#else
130/* See comment below, where ftrace_ops_list_func is defined */
131static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
132#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
133#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400134
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900135static inline void ftrace_ops_init(struct ftrace_ops *ops)
136{
137#ifdef CONFIG_DYNAMIC_FTRACE
138 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400139 mutex_init(&ops->local_hash.regex_lock);
140 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900141 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
142 }
143#endif
144}
145
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400146static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400147 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500148{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400149 struct trace_array *tr = op->private;
150
151 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500152 return;
153
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400154 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500155}
156
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500157static void ftrace_sync(struct work_struct *work)
158{
159 /*
160 * This function is just a stub to implement a hard force
Paul E. McKenney74401722018-11-06 18:44:52 -0800161 * of synchronize_rcu(). This requires synchronizing
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500162 * tasks even in userspace and idle.
163 *
164 * Yes, function tracing is rude.
165 */
166}
167
168static void ftrace_sync_ipi(void *data)
169{
170 /* Probably not needed, but do it anyway */
171 smp_rmb();
172}
173
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100174static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
175{
176 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500177 * 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 +0100178 * then it needs to call the list anyway.
179 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +0200180 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
181 FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100182 return ftrace_ops_list_func;
183
184 return ftrace_ops_get_func(ops);
185}
186
Steven Rostedt2b499382011-05-03 22:49:52 -0400187static void update_ftrace_function(void)
188{
189 ftrace_func_t func;
190
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400191 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400192 * Prepare the ftrace_ops that the arch callback will use.
193 * If there's only one ftrace_ops registered, the ftrace_ops_list
194 * will point to the ops we want.
195 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800196 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
197 lockdep_is_held(&ftrace_lock));
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400198
199 /* If there's no ftrace_ops registered, just call the stub function */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800200 if (set_function_trace_op == &ftrace_list_end) {
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400201 func = ftrace_stub;
202
203 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400204 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400205 * recursion safe and not dynamic and the arch supports passing ops,
206 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400207 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800208 } else if (rcu_dereference_protected(ftrace_ops_list->next,
209 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100210 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400211
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400212 } else {
213 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500214 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400215 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400216 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400217
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400218 update_function_graph_func();
219
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500220 /* If there's no change, then do nothing more here */
221 if (ftrace_trace_function == func)
222 return;
223
224 /*
225 * If we are using the list function, it doesn't care
226 * about the function_trace_ops.
227 */
228 if (func == ftrace_ops_list_func) {
229 ftrace_trace_function = func;
230 /*
231 * Don't even bother setting function_trace_ops,
232 * it would be racy to do so anyway.
233 */
234 return;
235 }
236
237#ifndef CONFIG_DYNAMIC_FTRACE
238 /*
239 * For static tracing, we need to be a bit more careful.
240 * The function change takes affect immediately. Thus,
241 * we need to coorditate the setting of the function_trace_ops
242 * with the setting of the ftrace_trace_function.
243 *
244 * Set the function to the list ops, which will call the
245 * function we want, albeit indirectly, but it handles the
246 * ftrace_ops and doesn't depend on function_trace_op.
247 */
248 ftrace_trace_function = ftrace_ops_list_func;
249 /*
250 * Make sure all CPUs see this. Yes this is slow, but static
251 * tracing is slow and nasty to have enabled.
252 */
253 schedule_on_each_cpu(ftrace_sync);
254 /* Now all cpus are using the list ops. */
255 function_trace_op = set_function_trace_op;
256 /* Make sure the function_trace_op is visible on all CPUs */
257 smp_wmb();
258 /* Nasty way to force a rmb on all cpus */
259 smp_call_function(ftrace_sync_ipi, NULL, 1);
260 /* OK, we are all set to update the ftrace_trace_function now! */
261#endif /* !CONFIG_DYNAMIC_FTRACE */
262
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400263 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400264}
265
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800266static void add_ftrace_ops(struct ftrace_ops __rcu **list,
267 struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200268{
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800269 rcu_assign_pointer(ops->next, *list);
270
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200271 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400272 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200273 * CPU might be walking that list. We need to make sure
274 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400275 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200276 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400277 rcu_assign_pointer(*list, ops);
278}
Steven Rostedt3d083392008-05-12 21:20:42 +0200279
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800280static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
281 struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400282{
283 struct ftrace_ops **p;
284
285 /*
286 * If we are removing the last function, then simply point
287 * to the ftrace_stub.
288 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800289 if (rcu_dereference_protected(*list,
290 lockdep_is_held(&ftrace_lock)) == ops &&
291 rcu_dereference_protected(ops->next,
292 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt2b499382011-05-03 22:49:52 -0400293 *list = &ftrace_list_end;
294 return 0;
295 }
296
297 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
298 if (*p == ops)
299 break;
300
301 if (*p != ops)
302 return -1;
303
304 *p = (*p)->next;
305 return 0;
306}
307
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400308static void ftrace_update_trampoline(struct ftrace_ops *ops);
309
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500310int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400311{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500312 if (ops->flags & FTRACE_OPS_FL_DELETED)
313 return -EINVAL;
314
Steven Rostedtb8489142011-05-04 09:27:52 -0400315 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
316 return -EBUSY;
317
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900318#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400319 /*
320 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
321 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
322 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
323 */
324 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
325 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
326 return -EINVAL;
327
328 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
329 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
330#endif
331
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400332 if (!core_kernel_data((unsigned long)ops))
333 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
334
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500335 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400336
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400337 /* Always save the function, and reset at unregistering */
338 ops->saved_func = ops->func;
339
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400340 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400341 ops->func = ftrace_pid_func;
342
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400343 ftrace_update_trampoline(ops);
344
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400345 if (ftrace_enabled)
346 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200347
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200348 return 0;
349}
350
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500351int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200352{
Steven Rostedt2b499382011-05-03 22:49:52 -0400353 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200354
Steven Rostedtb8489142011-05-04 09:27:52 -0400355 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
356 return -EBUSY;
357
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500358 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400359
Steven Rostedt2b499382011-05-03 22:49:52 -0400360 if (ret < 0)
361 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400362
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400363 if (ftrace_enabled)
364 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200365
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400366 ops->func = ops->saved_func;
367
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500368 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200369}
370
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500371static void ftrace_update_pid_func(void)
372{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400373 struct ftrace_ops *op;
374
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400375 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500376 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900377 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500378
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400379 do_for_each_ftrace_op(op, ftrace_ops_list) {
380 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400381 op->func = ftrace_pids_enabled(op) ?
382 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400383 ftrace_update_trampoline(op);
384 }
385 } while_for_each_ftrace_op(op);
386
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400387 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500388}
389
Steven Rostedt493762f2009-03-23 17:12:36 -0400390#ifdef CONFIG_FUNCTION_PROFILER
391struct ftrace_profile {
392 struct hlist_node node;
393 unsigned long ip;
394 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400395#ifdef CONFIG_FUNCTION_GRAPH_TRACER
396 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400397 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400398#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400399};
400
401struct ftrace_profile_page {
402 struct ftrace_profile_page *next;
403 unsigned long index;
404 struct ftrace_profile records[];
405};
406
Steven Rostedtcafb1682009-03-24 20:50:39 -0400407struct ftrace_profile_stat {
408 atomic_t disabled;
409 struct hlist_head *hash;
410 struct ftrace_profile_page *pages;
411 struct ftrace_profile_page *start;
412 struct tracer_stat stat;
413};
414
Steven Rostedt493762f2009-03-23 17:12:36 -0400415#define PROFILE_RECORDS_SIZE \
416 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
417
418#define PROFILES_PER_PAGE \
419 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
420
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400421static int ftrace_profile_enabled __read_mostly;
422
423/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400424static DEFINE_MUTEX(ftrace_profile_lock);
425
Steven Rostedtcafb1682009-03-24 20:50:39 -0400426static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400427
Namhyung Kim20079eb2013-04-10 08:55:50 +0900428#define FTRACE_PROFILE_HASH_BITS 10
429#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400430
Steven Rostedt493762f2009-03-23 17:12:36 -0400431static void *
432function_stat_next(void *v, int idx)
433{
434 struct ftrace_profile *rec = v;
435 struct ftrace_profile_page *pg;
436
437 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
438
439 again:
Li Zefan0296e422009-06-26 11:15:37 +0800440 if (idx != 0)
441 rec++;
442
Steven Rostedt493762f2009-03-23 17:12:36 -0400443 if ((void *)rec >= (void *)&pg->records[pg->index]) {
444 pg = pg->next;
445 if (!pg)
446 return NULL;
447 rec = &pg->records[0];
448 if (!rec->counter)
449 goto again;
450 }
451
452 return rec;
453}
454
455static void *function_stat_start(struct tracer_stat *trace)
456{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400457 struct ftrace_profile_stat *stat =
458 container_of(trace, struct ftrace_profile_stat, stat);
459
460 if (!stat || !stat->start)
461 return NULL;
462
463 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400464}
465
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400466#ifdef CONFIG_FUNCTION_GRAPH_TRACER
467/* function graph compares on total time */
468static int function_stat_cmp(void *p1, void *p2)
469{
470 struct ftrace_profile *a = p1;
471 struct ftrace_profile *b = p2;
472
473 if (a->time < b->time)
474 return -1;
475 if (a->time > b->time)
476 return 1;
477 else
478 return 0;
479}
480#else
481/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400482static int function_stat_cmp(void *p1, void *p2)
483{
484 struct ftrace_profile *a = p1;
485 struct ftrace_profile *b = p2;
486
487 if (a->counter < b->counter)
488 return -1;
489 if (a->counter > b->counter)
490 return 1;
491 else
492 return 0;
493}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400494#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400495
496static int function_stat_headers(struct seq_file *m)
497{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400498#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100499 seq_puts(m, " Function "
500 "Hit Time Avg s^2\n"
501 " -------- "
502 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400503#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100504 seq_puts(m, " Function Hit\n"
505 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400506#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400507 return 0;
508}
509
510static int function_stat_show(struct seq_file *m, void *v)
511{
512 struct ftrace_profile *rec = v;
513 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800514 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400515#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400516 static struct trace_seq s;
517 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400518 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400519#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800520 mutex_lock(&ftrace_profile_lock);
521
522 /* we raced with function_profile_reset() */
523 if (unlikely(rec->counter == 0)) {
524 ret = -EBUSY;
525 goto out;
526 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400527
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530528#ifdef CONFIG_FUNCTION_GRAPH_TRACER
529 avg = rec->time;
530 do_div(avg, rec->counter);
531 if (tracing_thresh && (avg < tracing_thresh))
532 goto out;
533#endif
534
Steven Rostedt493762f2009-03-23 17:12:36 -0400535 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400536 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400537
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400538#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100539 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400540
Chase Douglase330b3b2010-04-26 14:02:05 -0400541 /* Sample standard deviation (s^2) */
542 if (rec->counter <= 1)
543 stddev = 0;
544 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200545 /*
546 * Apply Welford's method:
547 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
548 */
549 stddev = rec->counter * rec->time_squared -
550 rec->time * rec->time;
551
Chase Douglase330b3b2010-04-26 14:02:05 -0400552 /*
553 * Divide only 1000 for ns^2 -> us^2 conversion.
554 * trace_print_graph_duration will divide 1000 again.
555 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200556 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400557 }
558
Steven Rostedt34886c82009-03-25 21:00:47 -0400559 trace_seq_init(&s);
560 trace_print_graph_duration(rec->time, &s);
561 trace_seq_puts(&s, " ");
562 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400563 trace_seq_puts(&s, " ");
564 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400565 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400566#endif
567 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800568out:
569 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400570
Li Zefan3aaba202010-08-23 16:50:12 +0800571 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400572}
573
Steven Rostedtcafb1682009-03-24 20:50:39 -0400574static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400575{
576 struct ftrace_profile_page *pg;
577
Steven Rostedtcafb1682009-03-24 20:50:39 -0400578 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400579
580 while (pg) {
581 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
582 pg->index = 0;
583 pg = pg->next;
584 }
585
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400587 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
588}
589
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400591{
592 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400593 int functions;
594 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400595 int i;
596
597 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400599 return 0;
600
Steven Rostedtcafb1682009-03-24 20:50:39 -0400601 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
602 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 return -ENOMEM;
604
Steven Rostedt318e0a72009-03-25 20:06:34 -0400605#ifdef CONFIG_DYNAMIC_FTRACE
606 functions = ftrace_update_tot_cnt;
607#else
608 /*
609 * We do not know the number of functions that exist because
610 * dynamic tracing is what counts them. With past experience
611 * we have around 20K functions. That should be more than enough.
612 * It is highly unlikely we will execute every function in
613 * the kernel.
614 */
615 functions = 20000;
616#endif
617
Steven Rostedtcafb1682009-03-24 20:50:39 -0400618 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400619
Steven Rostedt318e0a72009-03-25 20:06:34 -0400620 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
621
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900622 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400624 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400625 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400626 pg = pg->next;
627 }
628
629 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400630
631 out_free:
632 pg = stat->start;
633 while (pg) {
634 unsigned long tmp = (unsigned long)pg;
635
636 pg = pg->next;
637 free_page(tmp);
638 }
639
Steven Rostedt318e0a72009-03-25 20:06:34 -0400640 stat->pages = NULL;
641 stat->start = NULL;
642
643 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400644}
645
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400647{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400649 int size;
650
Steven Rostedtcafb1682009-03-24 20:50:39 -0400651 stat = &per_cpu(ftrace_profile_stats, cpu);
652
653 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400654 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400655 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400656 return 0;
657 }
658
659 /*
660 * We are profiling all functions, but usually only a few thousand
661 * functions are hit. We'll make a hash of 1024 items.
662 */
663 size = FTRACE_PROFILE_HASH_SIZE;
664
Kees Cook6396bb22018-06-12 14:03:40 -0700665 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400666
Steven Rostedtcafb1682009-03-24 20:50:39 -0400667 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400668 return -ENOMEM;
669
Steven Rostedt318e0a72009-03-25 20:06:34 -0400670 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400671 if (ftrace_profile_pages_init(stat) < 0) {
672 kfree(stat->hash);
673 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400674 return -ENOMEM;
675 }
676
677 return 0;
678}
679
Steven Rostedtcafb1682009-03-24 20:50:39 -0400680static int ftrace_profile_init(void)
681{
682 int cpu;
683 int ret = 0;
684
Miao Xiec4602c12013-12-16 15:20:01 +0800685 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686 ret = ftrace_profile_init_cpu(cpu);
687 if (ret)
688 break;
689 }
690
691 return ret;
692}
693
Steven Rostedt493762f2009-03-23 17:12:36 -0400694/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400695static struct ftrace_profile *
696ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400697{
698 struct ftrace_profile *rec;
699 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400700 unsigned long key;
701
Namhyung Kim20079eb2013-04-10 08:55:50 +0900702 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400704
705 if (hlist_empty(hhd))
706 return NULL;
707
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400708 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400709 if (rec->ip == ip)
710 return rec;
711 }
712
713 return NULL;
714}
715
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716static void ftrace_add_profile(struct ftrace_profile_stat *stat,
717 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400718{
719 unsigned long key;
720
Namhyung Kim20079eb2013-04-10 08:55:50 +0900721 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400723}
724
Steven Rostedt318e0a72009-03-25 20:06:34 -0400725/*
726 * The memory is already allocated, this simply finds a new record to use.
727 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400728static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400729ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400730{
731 struct ftrace_profile *rec = NULL;
732
Steven Rostedt318e0a72009-03-25 20:06:34 -0400733 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400734 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400735 goto out;
736
Steven Rostedt493762f2009-03-23 17:12:36 -0400737 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400738 * Try to find the function again since an NMI
739 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400740 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400743 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400744
Steven Rostedtcafb1682009-03-24 20:50:39 -0400745 if (stat->pages->index == PROFILES_PER_PAGE) {
746 if (!stat->pages->next)
747 goto out;
748 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 }
750
Steven Rostedtcafb1682009-03-24 20:50:39 -0400751 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400752 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400753 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400754
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400756 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400757
758 return rec;
759}
760
Steven Rostedt493762f2009-03-23 17:12:36 -0400761static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400762function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400763 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400764{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400765 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400766 struct ftrace_profile *rec;
767 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400768
769 if (!ftrace_profile_enabled)
770 return;
771
Steven Rostedt493762f2009-03-23 17:12:36 -0400772 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773
Christoph Lameterbdffd892014-04-29 14:17:40 -0500774 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400775 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400776 goto out;
777
778 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400779 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400780 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400781 if (!rec)
782 goto out;
783 }
784
785 rec->counter++;
786 out:
787 local_irq_restore(flags);
788}
789
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400790#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500791static bool fgraph_graph_time = true;
792
793void ftrace_graph_graph_time_control(bool enable)
794{
795 fgraph_graph_time = enable;
796}
797
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400798static int profile_graph_entry(struct ftrace_graph_ent *trace)
799{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500800 struct ftrace_ret_stack *ret_stack;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900801
Steven Rostedta1e2e312011-08-09 12:50:46 -0400802 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900803
Steven Rostedt (VMware)a8f0f9e2017-08-17 16:37:25 -0400804 /* If function graph is shutting down, ret_stack can be NULL */
805 if (!current->ret_stack)
806 return 0;
807
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500808 ret_stack = ftrace_graph_get_ret_stack(current, 0);
809 if (ret_stack)
810 ret_stack->subtime = 0;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900811
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400812 return 1;
813}
814
815static void profile_graph_return(struct ftrace_graph_ret *trace)
816{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500817 struct ftrace_ret_stack *ret_stack;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400818 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400819 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400820 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400822
823 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500824 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400825 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400826 goto out;
827
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400828 /* If the calltime was zero'd ignore it */
829 if (!trace->calltime)
830 goto out;
831
Steven Rostedta2a16d62009-03-24 23:17:58 -0400832 calltime = trace->rettime - trace->calltime;
833
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400834 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400835
836 /* Append this call time to the parent time to subtract */
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500837 ret_stack = ftrace_graph_get_ret_stack(current, 1);
838 if (ret_stack)
839 ret_stack->subtime += calltime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400840
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500841 ret_stack = ftrace_graph_get_ret_stack(current, 0);
842 if (ret_stack && ret_stack->subtime < calltime)
843 calltime -= ret_stack->subtime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400844 else
845 calltime = 0;
846 }
847
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400849 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400850 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400851 rec->time_squared += calltime * calltime;
852 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400853
Steven Rostedtcafb1682009-03-24 20:50:39 -0400854 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400855 local_irq_restore(flags);
856}
857
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500858static struct fgraph_ops fprofiler_ops = {
859 .entryfunc = &profile_graph_entry,
860 .retfunc = &profile_graph_return,
861};
862
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400863static int register_ftrace_profiler(void)
864{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500865 return register_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400866}
867
868static void unregister_ftrace_profiler(void)
869{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500870 unregister_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400871}
872#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100873static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400874 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900875 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400876 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400877};
878
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400879static int register_ftrace_profiler(void)
880{
881 return register_ftrace_function(&ftrace_profile_ops);
882}
883
884static void unregister_ftrace_profiler(void)
885{
886 unregister_ftrace_function(&ftrace_profile_ops);
887}
888#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
889
Steven Rostedt493762f2009-03-23 17:12:36 -0400890static ssize_t
891ftrace_profile_write(struct file *filp, const char __user *ubuf,
892 size_t cnt, loff_t *ppos)
893{
894 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400895 int ret;
896
Peter Huewe22fe9b52011-06-07 21:58:27 +0200897 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
898 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400899 return ret;
900
901 val = !!val;
902
903 mutex_lock(&ftrace_profile_lock);
904 if (ftrace_profile_enabled ^ val) {
905 if (val) {
906 ret = ftrace_profile_init();
907 if (ret < 0) {
908 cnt = ret;
909 goto out;
910 }
911
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400912 ret = register_ftrace_profiler();
913 if (ret < 0) {
914 cnt = ret;
915 goto out;
916 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400917 ftrace_profile_enabled = 1;
918 } else {
919 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400920 /*
921 * unregister_ftrace_profiler calls stop_machine
Paul E. McKenney74401722018-11-06 18:44:52 -0800922 * so this acts like an synchronize_rcu.
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400923 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400924 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400925 }
926 }
927 out:
928 mutex_unlock(&ftrace_profile_lock);
929
Jiri Olsacf8517c2009-10-23 19:36:16 -0400930 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400931
932 return cnt;
933}
934
935static ssize_t
936ftrace_profile_read(struct file *filp, char __user *ubuf,
937 size_t cnt, loff_t *ppos)
938{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400939 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400940 int r;
941
942 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
943 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
944}
945
946static const struct file_operations ftrace_profile_fops = {
947 .open = tracing_open_generic,
948 .read = ftrace_profile_read,
949 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200950 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400951};
952
Steven Rostedtcafb1682009-03-24 20:50:39 -0400953/* used to initialize the real stat files */
954static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400955 .name = "functions",
956 .stat_start = function_stat_start,
957 .stat_next = function_stat_next,
958 .stat_cmp = function_stat_cmp,
959 .stat_headers = function_stat_headers,
960 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400961};
962
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500963static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400964{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400965 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400967 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400968 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400969 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400970
Steven Rostedtcafb1682009-03-24 20:50:39 -0400971 for_each_possible_cpu(cpu) {
972 stat = &per_cpu(ftrace_profile_stats, cpu);
973
Geliang Tang6363c6b2016-03-15 22:12:34 +0800974 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400975 if (!name) {
976 /*
977 * The files created are permanent, if something happens
978 * we still do not free memory.
979 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400980 WARN(1,
981 "Could not allocate stat file for cpu %d\n",
982 cpu);
983 return;
984 }
985 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400986 stat->stat.name = name;
987 ret = register_stat_tracer(&stat->stat);
988 if (ret) {
989 WARN(1,
990 "Could not register function stat for cpu %d\n",
991 cpu);
992 kfree(name);
993 return;
994 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400995 }
996
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500997 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -0400998 d_tracer, NULL, &ftrace_profile_fops);
999 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001000 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001001}
1002
1003#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001004static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001005{
1006}
1007#endif /* CONFIG_FUNCTION_PROFILER */
1008
Steven Rostedt3d083392008-05-12 21:20:42 +02001009#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001010
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001011static struct ftrace_ops *removed_ops;
1012
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001013/*
1014 * Set when doing a global update, like enabling all recs or disabling them.
1015 * It is not set when just updating a single ftrace_ops.
1016 */
1017static bool update_all_ops;
1018
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001019#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001020# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001021#endif
1022
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001023struct ftrace_func_entry {
1024 struct hlist_node hlist;
1025 unsigned long ip;
1026};
1027
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001028struct ftrace_func_probe {
1029 struct ftrace_probe_ops *probe_ops;
1030 struct ftrace_ops ops;
1031 struct trace_array *tr;
1032 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001033 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001034 int ref;
1035};
1036
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001037/*
1038 * We make these constant because no one should touch them,
1039 * but they are used as the default "empty hash", to avoid allocating
1040 * it all the time. These are in a read only section such that if
1041 * anyone does try to modify it, it will cause an exception.
1042 */
1043static const struct hlist_head empty_buckets[1];
1044static const struct ftrace_hash empty_hash = {
1045 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001046};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001047#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001048
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001049struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001050 .func = ftrace_stub,
1051 .local_hash.notrace_hash = EMPTY_HASH,
1052 .local_hash.filter_hash = EMPTY_HASH,
1053 INIT_OPS_HASH(global_ops)
1054 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001055 FTRACE_OPS_FL_INITIALIZED |
1056 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001057};
1058
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001059/*
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001060 * Used by the stack undwinder to know about dynamic ftrace trampolines.
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001061 */
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001062struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001063{
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001064 struct ftrace_ops *op = NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001065
1066 /*
1067 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08001068 * they are freed after a synchronize_rcu().
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001069 */
1070 preempt_disable_notrace();
1071
1072 do_for_each_ftrace_op(op, ftrace_ops_list) {
1073 /*
1074 * This is to check for dynamically allocated trampolines.
1075 * Trampolines that are in kernel text will have
1076 * core_kernel_text() return true.
1077 */
1078 if (op->trampoline && op->trampoline_size)
1079 if (addr >= op->trampoline &&
1080 addr < op->trampoline + op->trampoline_size) {
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001081 preempt_enable_notrace();
1082 return op;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001083 }
1084 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001085 preempt_enable_notrace();
1086
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001087 return NULL;
1088}
1089
1090/*
1091 * This is used by __kernel_text_address() to return true if the
1092 * address is on a dynamically allocated trampoline that would
1093 * not return true for either core_kernel_text() or
1094 * is_module_text_address().
1095 */
1096bool is_ftrace_trampoline(unsigned long addr)
1097{
1098 return ftrace_ops_trampoline(addr) != NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001099}
1100
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101struct ftrace_page {
1102 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001103 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001104 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001105 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001106};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001107
Steven Rostedta7900872011-12-16 16:23:44 -05001108#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1109#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001110
1111/* estimate from running different kernels */
1112#define NR_TO_INIT 10000
1113
1114static struct ftrace_page *ftrace_pages_start;
1115static struct ftrace_page *ftrace_pages;
1116
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001117static __always_inline unsigned long
1118ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1119{
1120 if (hash->size_bits > 0)
1121 return hash_long(ip, hash->size_bits);
1122
1123 return 0;
1124}
1125
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001126/* Only use this function if ftrace_hash_empty() has already been tested */
1127static __always_inline struct ftrace_func_entry *
1128__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001129{
1130 unsigned long key;
1131 struct ftrace_func_entry *entry;
1132 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001133
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001134 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001135 hhd = &hash->buckets[key];
1136
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001137 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001138 if (entry->ip == ip)
1139 return entry;
1140 }
1141 return NULL;
1142}
1143
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001144/**
1145 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1146 * @hash: The hash to look at
1147 * @ip: The instruction pointer to test
1148 *
1149 * Search a given @hash to see if a given instruction pointer (@ip)
1150 * exists in it.
1151 *
1152 * Returns the entry that holds the @ip if found. NULL otherwise.
1153 */
1154struct ftrace_func_entry *
1155ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1156{
1157 if (ftrace_hash_empty(hash))
1158 return NULL;
1159
1160 return __ftrace_lookup_ip(hash, ip);
1161}
1162
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001163static void __add_hash_entry(struct ftrace_hash *hash,
1164 struct ftrace_func_entry *entry)
1165{
1166 struct hlist_head *hhd;
1167 unsigned long key;
1168
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001169 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001170 hhd = &hash->buckets[key];
1171 hlist_add_head(&entry->hlist, hhd);
1172 hash->count++;
1173}
1174
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001175static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1176{
1177 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178
1179 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1180 if (!entry)
1181 return -ENOMEM;
1182
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001183 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001184 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001185
1186 return 0;
1187}
1188
1189static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001190free_hash_entry(struct ftrace_hash *hash,
1191 struct ftrace_func_entry *entry)
1192{
1193 hlist_del(&entry->hlist);
1194 kfree(entry);
1195 hash->count--;
1196}
1197
1198static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199remove_hash_entry(struct ftrace_hash *hash,
1200 struct ftrace_func_entry *entry)
1201{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001202 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001203 hash->count--;
1204}
1205
1206static void ftrace_hash_clear(struct ftrace_hash *hash)
1207{
1208 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001209 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210 struct ftrace_func_entry *entry;
1211 int size = 1 << hash->size_bits;
1212 int i;
1213
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001214 if (!hash->count)
1215 return;
1216
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001217 for (i = 0; i < size; i++) {
1218 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001219 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001220 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001221 }
1222 FTRACE_WARN_ON(hash->count);
1223}
1224
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001225static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1226{
1227 list_del(&ftrace_mod->list);
1228 kfree(ftrace_mod->module);
1229 kfree(ftrace_mod->func);
1230 kfree(ftrace_mod);
1231}
1232
1233static void clear_ftrace_mod_list(struct list_head *head)
1234{
1235 struct ftrace_mod_load *p, *n;
1236
1237 /* stack tracer isn't supported yet */
1238 if (!head)
1239 return;
1240
1241 mutex_lock(&ftrace_lock);
1242 list_for_each_entry_safe(p, n, head, list)
1243 free_ftrace_mod(p);
1244 mutex_unlock(&ftrace_lock);
1245}
1246
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001247static void free_ftrace_hash(struct ftrace_hash *hash)
1248{
1249 if (!hash || hash == EMPTY_HASH)
1250 return;
1251 ftrace_hash_clear(hash);
1252 kfree(hash->buckets);
1253 kfree(hash);
1254}
1255
Steven Rostedt07fd5512011-05-05 18:03:47 -04001256static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1257{
1258 struct ftrace_hash *hash;
1259
1260 hash = container_of(rcu, struct ftrace_hash, rcu);
1261 free_ftrace_hash(hash);
1262}
1263
1264static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1265{
1266 if (!hash || hash == EMPTY_HASH)
1267 return;
Paul E. McKenney74401722018-11-06 18:44:52 -08001268 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001269}
1270
Jiri Olsa5500fa52012-02-15 15:51:54 +01001271void ftrace_free_filter(struct ftrace_ops *ops)
1272{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001273 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001274 free_ftrace_hash(ops->func_hash->filter_hash);
1275 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001276}
1277
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001278static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1279{
1280 struct ftrace_hash *hash;
1281 int size;
1282
1283 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1284 if (!hash)
1285 return NULL;
1286
1287 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001288 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001289
1290 if (!hash->buckets) {
1291 kfree(hash);
1292 return NULL;
1293 }
1294
1295 hash->size_bits = size_bits;
1296
1297 return hash;
1298}
1299
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001300
1301static int ftrace_add_mod(struct trace_array *tr,
1302 const char *func, const char *module,
1303 int enable)
1304{
1305 struct ftrace_mod_load *ftrace_mod;
1306 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1307
1308 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1309 if (!ftrace_mod)
1310 return -ENOMEM;
1311
1312 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1313 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1314 ftrace_mod->enable = enable;
1315
1316 if (!ftrace_mod->func || !ftrace_mod->module)
1317 goto out_free;
1318
1319 list_add(&ftrace_mod->list, mod_head);
1320
1321 return 0;
1322
1323 out_free:
1324 free_ftrace_mod(ftrace_mod);
1325
1326 return -ENOMEM;
1327}
1328
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001329static struct ftrace_hash *
1330alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1331{
1332 struct ftrace_func_entry *entry;
1333 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001334 int size;
1335 int ret;
1336 int i;
1337
1338 new_hash = alloc_ftrace_hash(size_bits);
1339 if (!new_hash)
1340 return NULL;
1341
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001342 if (hash)
1343 new_hash->flags = hash->flags;
1344
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001345 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001346 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001347 return new_hash;
1348
1349 size = 1 << hash->size_bits;
1350 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001351 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001352 ret = add_hash_entry(new_hash, entry->ip);
1353 if (ret < 0)
1354 goto free_hash;
1355 }
1356 }
1357
1358 FTRACE_WARN_ON(new_hash->count != hash->count);
1359
1360 return new_hash;
1361
1362 free_hash:
1363 free_ftrace_hash(new_hash);
1364 return NULL;
1365}
1366
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001367static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001368ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001369static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001370ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001371
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001372static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1373 struct ftrace_hash *new_hash);
1374
Namhyung Kim3e278c02017-01-20 11:44:45 +09001375static struct ftrace_hash *
1376__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001377{
1378 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001379 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001380 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001381 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001382 int size = src->count;
1383 int bits = 0;
1384 int i;
1385
1386 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001387 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001388 */
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001389 if (ftrace_hash_empty(src))
Namhyung Kim3e278c02017-01-20 11:44:45 +09001390 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001391
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001392 /*
1393 * Make the hash size about 1/2 the # found
1394 */
1395 for (size /= 2; size; size >>= 1)
1396 bits++;
1397
1398 /* Don't allocate too much */
1399 if (bits > FTRACE_HASH_MAX_BITS)
1400 bits = FTRACE_HASH_MAX_BITS;
1401
Steven Rostedt07fd5512011-05-05 18:03:47 -04001402 new_hash = alloc_ftrace_hash(bits);
1403 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001404 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001405
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001406 new_hash->flags = src->flags;
1407
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001408 size = 1 << src->size_bits;
1409 for (i = 0; i < size; i++) {
1410 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001411 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001412 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001413 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001414 }
1415 }
1416
Namhyung Kim3e278c02017-01-20 11:44:45 +09001417 return new_hash;
1418}
1419
1420static int
1421ftrace_hash_move(struct ftrace_ops *ops, int enable,
1422 struct ftrace_hash **dst, struct ftrace_hash *src)
1423{
1424 struct ftrace_hash *new_hash;
1425 int ret;
1426
1427 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1428 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1429 return -EINVAL;
1430
1431 new_hash = __ftrace_hash_move(src);
1432 if (!new_hash)
1433 return -ENOMEM;
1434
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001435 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1436 if (enable) {
1437 /* IPMODIFY should be updated only when filter_hash updating */
1438 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1439 if (ret < 0) {
1440 free_ftrace_hash(new_hash);
1441 return ret;
1442 }
1443 }
1444
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001445 /*
1446 * Remove the current set, update the hash and add
1447 * them back.
1448 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001449 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001450
Steven Rostedt07fd5512011-05-05 18:03:47 -04001451 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001452
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001453 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001454
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001455 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001456}
1457
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001458static bool hash_contains_ip(unsigned long ip,
1459 struct ftrace_ops_hash *hash)
1460{
1461 /*
1462 * The function record is a match if it exists in the filter
1463 * hash and not in the notrace hash. Note, an emty hash is
1464 * considered a match for the filter hash, but an empty
1465 * notrace hash is considered not in the notrace hash.
1466 */
1467 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001468 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001469 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001470 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001471}
1472
Steven Rostedt265c8312009-02-13 12:43:56 -05001473/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001474 * Test the hashes for this ops to see if we want to call
1475 * the ops->func or not.
1476 *
1477 * It's a match if the ip is in the ops->filter_hash or
1478 * the filter_hash does not exist or is empty,
1479 * AND
1480 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001481 *
1482 * This needs to be called with preemption disabled as
Paul E. McKenney74401722018-11-06 18:44:52 -08001483 * the hashes are freed with call_rcu().
Steven Rostedtb8489142011-05-04 09:27:52 -04001484 */
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001485int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001486ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001487{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001488 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001489 int ret;
1490
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001491#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1492 /*
1493 * There's a small race when adding ops that the ftrace handler
1494 * that wants regs, may be called without them. We can not
1495 * allow that handler to be called if regs is NULL.
1496 */
1497 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1498 return 0;
1499#endif
1500
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001501 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1502 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001503
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001504 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001505 ret = 1;
1506 else
1507 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001508
1509 return ret;
1510}
1511
1512/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001513 * This is a double for. Do not use 'break' to break out of the loop,
1514 * you must use a goto.
1515 */
1516#define do_for_each_ftrace_rec(pg, rec) \
1517 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1518 int _____i; \
1519 for (_____i = 0; _____i < pg->index; _____i++) { \
1520 rec = &pg->records[_____i];
1521
1522#define while_for_each_ftrace_rec() \
1523 } \
1524 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301525
Steven Rostedt5855fea2011-12-16 19:27:42 -05001526
1527static int ftrace_cmp_recs(const void *a, const void *b)
1528{
Steven Rostedta650e022012-04-25 13:48:13 -04001529 const struct dyn_ftrace *key = a;
1530 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001531
Steven Rostedta650e022012-04-25 13:48:13 -04001532 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001533 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001534 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1535 return 1;
1536 return 0;
1537}
1538
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001539/**
1540 * ftrace_location_range - return the first address of a traced location
1541 * if it touches the given ip range
1542 * @start: start of range to search.
1543 * @end: end of range to search (inclusive). @end points to the last byte
1544 * to check.
1545 *
1546 * Returns rec->ip if the related ftrace location is a least partly within
1547 * the given address range. That is, the first address of the instruction
1548 * that is either a NOP or call to the function tracer. It checks the ftrace
1549 * internal tables to determine if the address belongs or not.
1550 */
1551unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001552{
1553 struct ftrace_page *pg;
1554 struct dyn_ftrace *rec;
1555 struct dyn_ftrace key;
1556
1557 key.ip = start;
1558 key.flags = end; /* overload flags, as it is unsigned long */
1559
1560 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1561 if (end < pg->records[0].ip ||
1562 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1563 continue;
1564 rec = bsearch(&key, pg->records, pg->index,
1565 sizeof(struct dyn_ftrace),
1566 ftrace_cmp_recs);
1567 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001568 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001569 }
1570
Steven Rostedt5855fea2011-12-16 19:27:42 -05001571 return 0;
1572}
1573
Steven Rostedtc88fd862011-08-16 09:53:39 -04001574/**
1575 * ftrace_location - return true if the ip giving is a traced location
1576 * @ip: the instruction pointer to check
1577 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001578 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001579 * That is, the instruction that is either a NOP or call to
1580 * the function tracer. It checks the ftrace internal tables to
1581 * determine if the address belongs or not.
1582 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001583unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001584{
Steven Rostedta650e022012-04-25 13:48:13 -04001585 return ftrace_location_range(ip, ip);
1586}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001587
Steven Rostedta650e022012-04-25 13:48:13 -04001588/**
1589 * ftrace_text_reserved - return true if range contains an ftrace location
1590 * @start: start of range to search
1591 * @end: end of range to search (inclusive). @end points to the last byte to check.
1592 *
1593 * Returns 1 if @start and @end contains a ftrace location.
1594 * That is, the instruction that is either a NOP or call to
1595 * the function tracer. It checks the ftrace internal tables to
1596 * determine if the address belongs or not.
1597 */
Sasha Levind88471c2013-01-09 18:09:20 -05001598int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001599{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001600 unsigned long ret;
1601
1602 ret = ftrace_location_range((unsigned long)start,
1603 (unsigned long)end);
1604
1605 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001606}
1607
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001608/* Test if ops registered to this rec needs regs */
1609static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1610{
1611 struct ftrace_ops *ops;
1612 bool keep_regs = false;
1613
1614 for (ops = ftrace_ops_list;
1615 ops != &ftrace_list_end; ops = ops->next) {
1616 /* pass rec in as regs to have non-NULL val */
1617 if (ftrace_ops_test(ops, rec->ip, rec)) {
1618 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1619 keep_regs = true;
1620 break;
1621 }
1622 }
1623 }
1624
1625 return keep_regs;
1626}
1627
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001628static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001629 int filter_hash,
1630 bool inc)
1631{
1632 struct ftrace_hash *hash;
1633 struct ftrace_hash *other_hash;
1634 struct ftrace_page *pg;
1635 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001636 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001637 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001638 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001639
1640 /* Only update if the ops has been registered */
1641 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001642 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001643
1644 /*
1645 * In the filter_hash case:
1646 * If the count is zero, we update all records.
1647 * Otherwise we just update the items in the hash.
1648 *
1649 * In the notrace_hash case:
1650 * We enable the update in the hash.
1651 * As disabling notrace means enabling the tracing,
1652 * and enabling notrace means disabling, the inc variable
1653 * gets inversed.
1654 */
1655 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001656 hash = ops->func_hash->filter_hash;
1657 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001658 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001659 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001660 } else {
1661 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001662 hash = ops->func_hash->notrace_hash;
1663 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001664 /*
1665 * If the notrace hash has no items,
1666 * then there's nothing to do.
1667 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001668 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001669 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001670 }
1671
1672 do_for_each_ftrace_rec(pg, rec) {
1673 int in_other_hash = 0;
1674 int in_hash = 0;
1675 int match = 0;
1676
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001677 if (rec->flags & FTRACE_FL_DISABLED)
1678 continue;
1679
Steven Rostedted926f92011-05-03 13:25:24 -04001680 if (all) {
1681 /*
1682 * Only the filter_hash affects all records.
1683 * Update if the record is not in the notrace hash.
1684 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001685 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001686 match = 1;
1687 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001688 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1689 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001690
1691 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001692 * If filter_hash is set, we want to match all functions
1693 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001694 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001695 * If filter_hash is not set, then we are decrementing.
1696 * That means we match anything that is in the hash
1697 * and also in the other_hash. That is, we need to turn
1698 * off functions in the other hash because they are disabled
1699 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001700 */
1701 if (filter_hash && in_hash && !in_other_hash)
1702 match = 1;
1703 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001704 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001705 match = 1;
1706 }
1707 if (!match)
1708 continue;
1709
1710 if (inc) {
1711 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001712 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001713 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001714
1715 /*
1716 * If there's only a single callback registered to a
1717 * function, and the ops has a trampoline registered
1718 * for it, then we can call it directly.
1719 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001720 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001721 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001722 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001723 /*
1724 * If we are adding another function callback
1725 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001726 * custom trampoline in use, then we need to go
1727 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001728 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001729 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001730
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001731 /*
1732 * If any ops wants regs saved for this function
1733 * then all ops will get saved regs.
1734 */
1735 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1736 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001737 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001738 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001739 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001740 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001741
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001742 /*
1743 * If the rec had REGS enabled and the ops that is
1744 * being removed had REGS set, then see if there is
1745 * still any ops for this record that wants regs.
1746 * If not, we can stop recording them.
1747 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001748 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001749 rec->flags & FTRACE_FL_REGS &&
1750 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1751 if (!test_rec_ops_needs_regs(rec))
1752 rec->flags &= ~FTRACE_FL_REGS;
1753 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001754
1755 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001756 * If the rec had TRAMP enabled, then it needs to
1757 * be cleared. As TRAMP can only be enabled iff
1758 * there is only a single ops attached to it.
1759 * In otherwords, always disable it on decrementing.
1760 * In the future, we may set it if rec count is
1761 * decremented to one, and the ops that is left
1762 * has a trampoline.
1763 */
1764 rec->flags &= ~FTRACE_FL_TRAMP;
1765
1766 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001767 * flags will be cleared in ftrace_check_record()
1768 * if rec count is zero.
1769 */
Steven Rostedted926f92011-05-03 13:25:24 -04001770 }
1771 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001772
1773 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1774 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1775
Steven Rostedted926f92011-05-03 13:25:24 -04001776 /* Shortcut, if we handled all records, we are done. */
1777 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001778 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001779 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001780
1781 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001782}
1783
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001784static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001785 int filter_hash)
1786{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001787 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001788}
1789
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001790static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001791 int filter_hash)
1792{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001793 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001794}
1795
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001796static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1797 int filter_hash, int inc)
1798{
1799 struct ftrace_ops *op;
1800
1801 __ftrace_hash_rec_update(ops, filter_hash, inc);
1802
1803 if (ops->func_hash != &global_ops.local_hash)
1804 return;
1805
1806 /*
1807 * If the ops shares the global_ops hash, then we need to update
1808 * all ops that are enabled and use this hash.
1809 */
1810 do_for_each_ftrace_op(op, ftrace_ops_list) {
1811 /* Already done */
1812 if (op == ops)
1813 continue;
1814 if (op->func_hash == &global_ops.local_hash)
1815 __ftrace_hash_rec_update(op, filter_hash, inc);
1816 } while_for_each_ftrace_op(op);
1817}
1818
1819static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1820 int filter_hash)
1821{
1822 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1823}
1824
1825static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1826 int filter_hash)
1827{
1828 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1829}
1830
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001831/*
1832 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1833 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1834 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1835 * Note that old_hash and new_hash has below meanings
1836 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1837 * - If the hash is EMPTY_HASH, it hits nothing
1838 * - Anything else hits the recs which match the hash entries.
1839 */
1840static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1841 struct ftrace_hash *old_hash,
1842 struct ftrace_hash *new_hash)
1843{
1844 struct ftrace_page *pg;
1845 struct dyn_ftrace *rec, *end = NULL;
1846 int in_old, in_new;
1847
1848 /* Only update if the ops has been registered */
1849 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1850 return 0;
1851
1852 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1853 return 0;
1854
1855 /*
1856 * Since the IPMODIFY is a very address sensitive action, we do not
1857 * allow ftrace_ops to set all functions to new hash.
1858 */
1859 if (!new_hash || !old_hash)
1860 return -EINVAL;
1861
1862 /* Update rec->flags */
1863 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001864
1865 if (rec->flags & FTRACE_FL_DISABLED)
1866 continue;
1867
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001868 /* We need to update only differences of filter_hash */
1869 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1870 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1871 if (in_old == in_new)
1872 continue;
1873
1874 if (in_new) {
1875 /* New entries must ensure no others are using it */
1876 if (rec->flags & FTRACE_FL_IPMODIFY)
1877 goto rollback;
1878 rec->flags |= FTRACE_FL_IPMODIFY;
1879 } else /* Removed entry */
1880 rec->flags &= ~FTRACE_FL_IPMODIFY;
1881 } while_for_each_ftrace_rec();
1882
1883 return 0;
1884
1885rollback:
1886 end = rec;
1887
1888 /* Roll back what we did above */
1889 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001890
1891 if (rec->flags & FTRACE_FL_DISABLED)
1892 continue;
1893
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001894 if (rec == end)
1895 goto err_out;
1896
1897 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1898 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1899 if (in_old == in_new)
1900 continue;
1901
1902 if (in_new)
1903 rec->flags &= ~FTRACE_FL_IPMODIFY;
1904 else
1905 rec->flags |= FTRACE_FL_IPMODIFY;
1906 } while_for_each_ftrace_rec();
1907
1908err_out:
1909 return -EBUSY;
1910}
1911
1912static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1913{
1914 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1915
1916 if (ftrace_hash_empty(hash))
1917 hash = NULL;
1918
1919 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1920}
1921
1922/* Disabling always succeeds */
1923static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1924{
1925 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1926
1927 if (ftrace_hash_empty(hash))
1928 hash = NULL;
1929
1930 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1931}
1932
1933static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1934 struct ftrace_hash *new_hash)
1935{
1936 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1937
1938 if (ftrace_hash_empty(old_hash))
1939 old_hash = NULL;
1940
1941 if (ftrace_hash_empty(new_hash))
1942 new_hash = NULL;
1943
1944 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1945}
1946
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001947static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001948{
1949 int i;
1950
1951 printk(KERN_CONT "%s", fmt);
1952
1953 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1954 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1955}
1956
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001957static struct ftrace_ops *
1958ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001959static struct ftrace_ops *
1960ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001961
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001962enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001963const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001964
1965static void print_bug_type(void)
1966{
1967 switch (ftrace_bug_type) {
1968 case FTRACE_BUG_UNKNOWN:
1969 break;
1970 case FTRACE_BUG_INIT:
1971 pr_info("Initializing ftrace call sites\n");
1972 break;
1973 case FTRACE_BUG_NOP:
1974 pr_info("Setting ftrace call site to NOP\n");
1975 break;
1976 case FTRACE_BUG_CALL:
1977 pr_info("Setting ftrace call site to call ftrace function\n");
1978 break;
1979 case FTRACE_BUG_UPDATE:
1980 pr_info("Updating ftrace call site to call a different ftrace function\n");
1981 break;
1982 }
1983}
1984
Steven Rostedtc88fd862011-08-16 09:53:39 -04001985/**
1986 * ftrace_bug - report and shutdown function tracer
1987 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001988 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001989 *
1990 * The arch code that enables or disables the function tracing
1991 * can call ftrace_bug() when it has detected a problem in
1992 * modifying the code. @failed should be one of either:
1993 * EFAULT - if the problem happens on reading the @ip address
1994 * EINVAL - if what is read at @ip is not what was expected
1995 * EPERM - if the problem happens on writting to the @ip address
1996 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001997void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001998{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001999 unsigned long ip = rec ? rec->ip : 0;
2000
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002001 switch (failed) {
2002 case -EFAULT:
2003 FTRACE_WARN_ON_ONCE(1);
2004 pr_info("ftrace faulted on modifying ");
2005 print_ip_sym(ip);
2006 break;
2007 case -EINVAL:
2008 FTRACE_WARN_ON_ONCE(1);
2009 pr_info("ftrace failed to modify ");
2010 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002011 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002012 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002013 if (ftrace_expected) {
2014 print_ip_ins(" expected: ", ftrace_expected);
2015 pr_cont("\n");
2016 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002017 break;
2018 case -EPERM:
2019 FTRACE_WARN_ON_ONCE(1);
2020 pr_info("ftrace faulted on writing ");
2021 print_ip_sym(ip);
2022 break;
2023 default:
2024 FTRACE_WARN_ON_ONCE(1);
2025 pr_info("ftrace faulted on unknown error ");
2026 print_ip_sym(ip);
2027 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002028 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002029 if (rec) {
2030 struct ftrace_ops *ops = NULL;
2031
2032 pr_info("ftrace record flags: %lx\n", rec->flags);
2033 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2034 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2035 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2036 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002037 if (ops) {
2038 do {
2039 pr_cont("\ttramp: %pS (%pS)",
2040 (void *)ops->trampoline,
2041 (void *)ops->func);
2042 ops = ftrace_find_tramp_ops_next(rec, ops);
2043 } while (ops);
2044 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002045 pr_cont("\ttramp: ERROR!");
2046
2047 }
2048 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002049 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002050 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002051}
2052
Steven Rostedtc88fd862011-08-16 09:53:39 -04002053static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002054{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002055 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002056
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002057 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2058
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002059 if (rec->flags & FTRACE_FL_DISABLED)
2060 return FTRACE_UPDATE_IGNORE;
2061
Steven Rostedt982c3502008-11-15 16:31:41 -05002062 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002063 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002064 *
Steven Rostedted926f92011-05-03 13:25:24 -04002065 * If the record has a ref count, then we need to enable it
2066 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002067 *
Steven Rostedted926f92011-05-03 13:25:24 -04002068 * Otherwise we make sure its disabled.
2069 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002070 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002071 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002072 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002073 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002074 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002075
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002076 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002077 * If enabling and the REGS flag does not match the REGS_EN, or
2078 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2079 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002080 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002081 if (flag) {
2082 if (!(rec->flags & FTRACE_FL_REGS) !=
2083 !(rec->flags & FTRACE_FL_REGS_EN))
2084 flag |= FTRACE_FL_REGS;
2085
2086 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2087 !(rec->flags & FTRACE_FL_TRAMP_EN))
2088 flag |= FTRACE_FL_TRAMP;
2089 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002090
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002091 /* If the state of this record hasn't changed, then do nothing */
2092 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002093 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002094
2095 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002096 /* Save off if rec is being enabled (for return value) */
2097 flag ^= rec->flags & FTRACE_FL_ENABLED;
2098
2099 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002100 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002101 if (flag & FTRACE_FL_REGS) {
2102 if (rec->flags & FTRACE_FL_REGS)
2103 rec->flags |= FTRACE_FL_REGS_EN;
2104 else
2105 rec->flags &= ~FTRACE_FL_REGS_EN;
2106 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002107 if (flag & FTRACE_FL_TRAMP) {
2108 if (rec->flags & FTRACE_FL_TRAMP)
2109 rec->flags |= FTRACE_FL_TRAMP_EN;
2110 else
2111 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2112 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002113 }
2114
2115 /*
2116 * If this record is being updated from a nop, then
2117 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002118 * Otherwise,
2119 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002120 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002121 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002122 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002123 if (flag & FTRACE_FL_ENABLED) {
2124 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002125 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002126 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002127
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002128 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002129 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002130 }
2131
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002132 if (update) {
2133 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002134 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002135 rec->flags = 0;
2136 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002137 /*
2138 * Just disable the record, but keep the ops TRAMP
2139 * and REGS states. The _EN flags must be disabled though.
2140 */
2141 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2142 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002143 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002144
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002145 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002146 return FTRACE_UPDATE_MAKE_NOP;
2147}
2148
2149/**
2150 * ftrace_update_record, set a record that now is tracing or not
2151 * @rec: the record to update
2152 * @enable: set to 1 if the record is tracing, zero to force disable
2153 *
2154 * The records that represent all functions that can be traced need
2155 * to be updated when tracing has been enabled.
2156 */
2157int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2158{
2159 return ftrace_check_record(rec, enable, 1);
2160}
2161
2162/**
2163 * ftrace_test_record, check if the record has been enabled or not
2164 * @rec: the record to test
2165 * @enable: set to 1 to check if enabled, 0 if it is disabled
2166 *
2167 * The arch code may need to test if a record is already set to
2168 * tracing to determine how to modify the function code that it
2169 * represents.
2170 */
2171int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2172{
2173 return ftrace_check_record(rec, enable, 0);
2174}
2175
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002176static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002177ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2178{
2179 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002180 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002181
2182 do_for_each_ftrace_op(op, ftrace_ops_list) {
2183
2184 if (!op->trampoline)
2185 continue;
2186
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002187 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002188 return op;
2189 } while_for_each_ftrace_op(op);
2190
2191 return NULL;
2192}
2193
2194static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002195ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2196 struct ftrace_ops *op)
2197{
2198 unsigned long ip = rec->ip;
2199
2200 while_for_each_ftrace_op(op) {
2201
2202 if (!op->trampoline)
2203 continue;
2204
2205 if (hash_contains_ip(ip, op->func_hash))
2206 return op;
2207 }
2208
2209 return NULL;
2210}
2211
2212static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002213ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2214{
2215 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002216 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002217
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002218 /*
2219 * Need to check removed ops first.
2220 * If they are being removed, and this rec has a tramp,
2221 * and this rec is in the ops list, then it would be the
2222 * one with the tramp.
2223 */
2224 if (removed_ops) {
2225 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002226 return removed_ops;
2227 }
2228
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002229 /*
2230 * Need to find the current trampoline for a rec.
2231 * Now, a trampoline is only attached to a rec if there
2232 * was a single 'ops' attached to it. But this can be called
2233 * when we are adding another op to the rec or removing the
2234 * current one. Thus, if the op is being added, we can
2235 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002236 * yet.
2237 *
2238 * If an ops is being modified (hooking to different functions)
2239 * then we don't care about the new functions that are being
2240 * added, just the old ones (that are probably being removed).
2241 *
2242 * If we are adding an ops to a function that already is using
2243 * a trampoline, it needs to be removed (trampolines are only
2244 * for single ops connected), then an ops that is not being
2245 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002246 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002247 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002248
2249 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002250 continue;
2251
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002252 /*
2253 * If the ops is being added, it hasn't gotten to
2254 * the point to be removed from this tree yet.
2255 */
2256 if (op->flags & FTRACE_OPS_FL_ADDING)
2257 continue;
2258
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002259
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002260 /*
2261 * If the ops is being modified and is in the old
2262 * hash, then it is probably being removed from this
2263 * function.
2264 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002265 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2266 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002267 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002268 /*
2269 * If the ops is not being added or modified, and it's
2270 * in its normal filter hash, then this must be the one
2271 * we want!
2272 */
2273 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2274 hash_contains_ip(ip, op->func_hash))
2275 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002276
2277 } while_for_each_ftrace_op(op);
2278
2279 return NULL;
2280}
2281
2282static struct ftrace_ops *
2283ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2284{
2285 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002286 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002287
2288 do_for_each_ftrace_op(op, ftrace_ops_list) {
2289 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002290 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002291 return op;
2292 } while_for_each_ftrace_op(op);
2293
2294 return NULL;
2295}
2296
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002297/**
2298 * ftrace_get_addr_new - Get the call address to set to
2299 * @rec: The ftrace record descriptor
2300 *
2301 * If the record has the FTRACE_FL_REGS set, that means that it
2302 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2303 * is not not set, then it wants to convert to the normal callback.
2304 *
2305 * Returns the address of the trampoline to set to
2306 */
2307unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2308{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002309 struct ftrace_ops *ops;
2310
2311 /* Trampolines take precedence over regs */
2312 if (rec->flags & FTRACE_FL_TRAMP) {
2313 ops = ftrace_find_tramp_ops_new(rec);
2314 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002315 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2316 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002317 /* Ftrace is shutting down, return anything */
2318 return (unsigned long)FTRACE_ADDR;
2319 }
2320 return ops->trampoline;
2321 }
2322
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002323 if (rec->flags & FTRACE_FL_REGS)
2324 return (unsigned long)FTRACE_REGS_ADDR;
2325 else
2326 return (unsigned long)FTRACE_ADDR;
2327}
2328
2329/**
2330 * ftrace_get_addr_curr - Get the call address that is already there
2331 * @rec: The ftrace record descriptor
2332 *
2333 * The FTRACE_FL_REGS_EN is set when the record already points to
2334 * a function that saves all the regs. Basically the '_EN' version
2335 * represents the current state of the function.
2336 *
2337 * Returns the address of the trampoline that is currently being called
2338 */
2339unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2340{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002341 struct ftrace_ops *ops;
2342
2343 /* Trampolines take precedence over regs */
2344 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2345 ops = ftrace_find_tramp_ops_curr(rec);
2346 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002347 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2348 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002349 /* Ftrace is shutting down, return anything */
2350 return (unsigned long)FTRACE_ADDR;
2351 }
2352 return ops->trampoline;
2353 }
2354
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002355 if (rec->flags & FTRACE_FL_REGS_EN)
2356 return (unsigned long)FTRACE_REGS_ADDR;
2357 else
2358 return (unsigned long)FTRACE_ADDR;
2359}
2360
Steven Rostedtc88fd862011-08-16 09:53:39 -04002361static int
2362__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2363{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002364 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002365 unsigned long ftrace_addr;
2366 int ret;
2367
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002368 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002369
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002370 /* This needs to be done before we call ftrace_update_record */
2371 ftrace_old_addr = ftrace_get_addr_curr(rec);
2372
2373 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002374
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002375 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2376
Steven Rostedtc88fd862011-08-16 09:53:39 -04002377 switch (ret) {
2378 case FTRACE_UPDATE_IGNORE:
2379 return 0;
2380
2381 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002382 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002383 return ftrace_make_call(rec, ftrace_addr);
2384
2385 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002386 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002387 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002388
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002389 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002390 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002391 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002392 }
2393
2394 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002395}
2396
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002397void __weak ftrace_replace_code(int mod_flags)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002398{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002399 struct dyn_ftrace *rec;
2400 struct ftrace_page *pg;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002401 int enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2402 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002403 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002404
Steven Rostedt45a4a232011-04-21 23:16:46 -04002405 if (unlikely(ftrace_disabled))
2406 return;
2407
Steven Rostedt265c8312009-02-13 12:43:56 -05002408 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002409
2410 if (rec->flags & FTRACE_FL_DISABLED)
2411 continue;
2412
Steven Rostedte4f5d542012-04-27 09:13:18 -04002413 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002414 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002415 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002416 /* Stop processing */
2417 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002418 }
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002419 if (schedulable)
2420 cond_resched();
Steven Rostedt265c8312009-02-13 12:43:56 -05002421 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002422}
2423
Steven Rostedtc88fd862011-08-16 09:53:39 -04002424struct ftrace_rec_iter {
2425 struct ftrace_page *pg;
2426 int index;
2427};
2428
2429/**
2430 * ftrace_rec_iter_start, start up iterating over traced functions
2431 *
2432 * Returns an iterator handle that is used to iterate over all
2433 * the records that represent address locations where functions
2434 * are traced.
2435 *
2436 * May return NULL if no records are available.
2437 */
2438struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2439{
2440 /*
2441 * We only use a single iterator.
2442 * Protected by the ftrace_lock mutex.
2443 */
2444 static struct ftrace_rec_iter ftrace_rec_iter;
2445 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2446
2447 iter->pg = ftrace_pages_start;
2448 iter->index = 0;
2449
2450 /* Could have empty pages */
2451 while (iter->pg && !iter->pg->index)
2452 iter->pg = iter->pg->next;
2453
2454 if (!iter->pg)
2455 return NULL;
2456
2457 return iter;
2458}
2459
2460/**
2461 * ftrace_rec_iter_next, get the next record to process.
2462 * @iter: The handle to the iterator.
2463 *
2464 * Returns the next iterator after the given iterator @iter.
2465 */
2466struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2467{
2468 iter->index++;
2469
2470 if (iter->index >= iter->pg->index) {
2471 iter->pg = iter->pg->next;
2472 iter->index = 0;
2473
2474 /* Could have empty pages */
2475 while (iter->pg && !iter->pg->index)
2476 iter->pg = iter->pg->next;
2477 }
2478
2479 if (!iter->pg)
2480 return NULL;
2481
2482 return iter;
2483}
2484
2485/**
2486 * ftrace_rec_iter_record, get the record at the iterator location
2487 * @iter: The current iterator location
2488 *
2489 * Returns the record that the current @iter is at.
2490 */
2491struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2492{
2493 return &iter->pg->records[iter->index];
2494}
2495
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302496static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002497ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002498{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002499 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002500
Steven Rostedt45a4a232011-04-21 23:16:46 -04002501 if (unlikely(ftrace_disabled))
2502 return 0;
2503
Shaohua Li25aac9d2009-01-09 11:29:40 +08002504 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002505 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002506 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002507 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302508 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002509 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302510 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002511}
2512
Steven Rostedt000ab692009-02-17 13:35:06 -05002513/*
2514 * archs can override this function if they must do something
2515 * before the modifying code is performed.
2516 */
2517int __weak ftrace_arch_code_modify_prepare(void)
2518{
2519 return 0;
2520}
2521
2522/*
2523 * archs can override this function if they must do something
2524 * after the modifying code is performed.
2525 */
2526int __weak ftrace_arch_code_modify_post_process(void)
2527{
2528 return 0;
2529}
2530
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002531void ftrace_modify_all_code(int command)
2532{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002533 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002534 int mod_flags = 0;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002535 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002536
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002537 if (command & FTRACE_MAY_SLEEP)
2538 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2539
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002540 /*
2541 * If the ftrace_caller calls a ftrace_ops func directly,
2542 * we need to make sure that it only traces functions it
2543 * expects to trace. When doing the switch of functions,
2544 * we need to update to the ftrace_ops_list_func first
2545 * before the transition between old and new calls are set,
2546 * as the ftrace_ops_list_func will check the ops hashes
2547 * to make sure the ops are having the right functions
2548 * traced.
2549 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002550 if (update) {
2551 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2552 if (FTRACE_WARN_ON(err))
2553 return;
2554 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002555
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002556 if (command & FTRACE_UPDATE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002557 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002558 else if (command & FTRACE_DISABLE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002559 ftrace_replace_code(mod_flags);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002560
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002561 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2562 function_trace_op = set_function_trace_op;
2563 smp_wmb();
2564 /* If irqs are disabled, we are in stop machine */
2565 if (!irqs_disabled())
2566 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002567 err = ftrace_update_ftrace_func(ftrace_trace_function);
2568 if (FTRACE_WARN_ON(err))
2569 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002570 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002571
2572 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002573 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002574 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002575 err = ftrace_disable_ftrace_graph_caller();
2576 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002577}
2578
Ingo Molnare309b412008-05-12 21:20:51 +02002579static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002580{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002581 int *command = data;
2582
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002583 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002584
Steven Rostedtc88fd862011-08-16 09:53:39 -04002585 return 0;
2586}
2587
2588/**
2589 * ftrace_run_stop_machine, go back to the stop machine method
2590 * @command: The command to tell ftrace what to do
2591 *
2592 * If an arch needs to fall back to the stop machine method, the
2593 * it can call this function.
2594 */
2595void ftrace_run_stop_machine(int command)
2596{
2597 stop_machine(__ftrace_modify_code, &command, NULL);
2598}
2599
2600/**
2601 * arch_ftrace_update_code, modify the code to trace or not trace
2602 * @command: The command that needs to be done
2603 *
2604 * Archs can override this function if it does not need to
2605 * run stop_machine() to modify code.
2606 */
2607void __weak arch_ftrace_update_code(int command)
2608{
2609 ftrace_run_stop_machine(command);
2610}
2611
2612static void ftrace_run_update_code(int command)
2613{
2614 int ret;
2615
2616 ret = ftrace_arch_code_modify_prepare();
2617 FTRACE_WARN_ON(ret);
2618 if (ret)
2619 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002620
2621 /*
2622 * By default we use stop_machine() to modify the code.
2623 * But archs can do what ever they want as long as it
2624 * is safe. The stop_machine() is the safest, but also
2625 * produces the most overhead.
2626 */
2627 arch_ftrace_update_code(command);
2628
Steven Rostedt000ab692009-02-17 13:35:06 -05002629 ret = ftrace_arch_code_modify_post_process();
2630 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002631}
2632
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002633static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002634 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002635{
2636 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002637 ops->old_hash.filter_hash = old_hash->filter_hash;
2638 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002639 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002640 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002641 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002642 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2643}
2644
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002645static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002646static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002647
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002648void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2649{
2650}
2651
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002652static void ftrace_startup_enable(int command)
2653{
2654 if (saved_ftrace_func != ftrace_trace_function) {
2655 saved_ftrace_func = ftrace_trace_function;
2656 command |= FTRACE_UPDATE_TRACE_FUNC;
2657 }
2658
2659 if (!command || !ftrace_enabled)
2660 return;
2661
2662 ftrace_run_update_code(command);
2663}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002664
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002665static void ftrace_startup_all(int command)
2666{
2667 update_all_ops = true;
2668 ftrace_startup_enable(command);
2669 update_all_ops = false;
2670}
2671
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002672int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002673{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002674 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002675
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002676 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002677 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002678
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002679 ret = __register_ftrace_function(ops);
2680 if (ret)
2681 return ret;
2682
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002683 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002684
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002685 /*
2686 * Note that ftrace probes uses this to start up
2687 * and modify functions it will probe. But we still
2688 * set the ADDING flag for modification, as probes
2689 * do not have trampolines. If they add them in the
2690 * future, then the probes will need to distinguish
2691 * between adding and updating probes.
2692 */
2693 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002694
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002695 ret = ftrace_hash_ipmodify_enable(ops);
2696 if (ret < 0) {
2697 /* Rollback registration process */
2698 __unregister_ftrace_function(ops);
2699 ftrace_start_up--;
2700 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2701 return ret;
2702 }
2703
Jiri Olsa7f50d062016-03-16 15:34:33 +01002704 if (ftrace_hash_rec_enable(ops, 1))
2705 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002706
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002707 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002708
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002709 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2710
Steven Rostedta1cd6172011-05-23 15:24:25 -04002711 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002712}
2713
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002714int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002715{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002716 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002717
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002718 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002719 return -ENODEV;
2720
2721 ret = __unregister_ftrace_function(ops);
2722 if (ret)
2723 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002724
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002725 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002726 /*
2727 * Just warn in case of unbalance, no need to kill ftrace, it's not
2728 * critical but the ftrace_call callers may be never nopped again after
2729 * further ftrace uses.
2730 */
2731 WARN_ON_ONCE(ftrace_start_up < 0);
2732
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002733 /* Disabling ipmodify never fails */
2734 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002735
2736 if (ftrace_hash_rec_disable(ops, 1))
2737 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002738
Namhyung Kima737e6d2014-06-12 23:56:12 +09002739 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002740
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002741 if (saved_ftrace_func != ftrace_trace_function) {
2742 saved_ftrace_func = ftrace_trace_function;
2743 command |= FTRACE_UPDATE_TRACE_FUNC;
2744 }
2745
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002746 if (!command || !ftrace_enabled) {
2747 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002748 * If these are dynamic or per_cpu ops, they still
2749 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002750 * not currently active, we can just free them
2751 * without synchronizing all CPUs.
2752 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002753 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002754 goto free_ops;
2755
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002756 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002757 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002758
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002759 /*
2760 * If the ops uses a trampoline, then it needs to be
2761 * tested first on update.
2762 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002763 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002764 removed_ops = ops;
2765
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002766 /* The trampoline logic checks the old hashes */
2767 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2768 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2769
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002770 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002771
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002772 /*
2773 * If there's no more ops registered with ftrace, run a
2774 * sanity check to make sure all rec flags are cleared.
2775 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002776 if (rcu_dereference_protected(ftrace_ops_list,
2777 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002778 struct ftrace_page *pg;
2779 struct dyn_ftrace *rec;
2780
2781 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002782 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002783 pr_warn(" %pS flags:%lx\n",
2784 (void *)rec->ip, rec->flags);
2785 } while_for_each_ftrace_rec();
2786 }
2787
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002788 ops->old_hash.filter_hash = NULL;
2789 ops->old_hash.notrace_hash = NULL;
2790
2791 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002792 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002793
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002794 /*
2795 * Dynamic ops may be freed, we must make sure that all
2796 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002797 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002798 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002799 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002800 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002801 /*
2802 * We need to do a hard force of sched synchronization.
2803 * This is because we use preempt_disable() to do RCU, but
2804 * the function tracers can be called where RCU is not watching
2805 * (like before user_exit()). We can not rely on the RCU
2806 * infrastructure to do the synchronization, thus we must do it
2807 * ourselves.
2808 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002809 schedule_on_each_cpu(ftrace_sync);
2810
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002811 /*
2812 * When the kernel is preeptive, tasks can be preempted
2813 * while on a ftrace trampoline. Just scheduling a task on
2814 * a CPU is not good enough to flush them. Calling
2815 * synchornize_rcu_tasks() will wait for those tasks to
2816 * execute and either schedule voluntarily or enter user space.
2817 */
2818 if (IS_ENABLED(CONFIG_PREEMPT))
2819 synchronize_rcu_tasks();
2820
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002821 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002822 arch_ftrace_trampoline_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002823 }
2824
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002825 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002826}
2827
Ingo Molnare309b412008-05-12 21:20:51 +02002828static void ftrace_startup_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 Rostedtd61f82d2008-05-12 21:20:43 +02002835 /* Force update next time */
2836 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002837 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302838 if (ftrace_start_up) {
2839 command = FTRACE_UPDATE_CALLS;
2840 if (ftrace_graph_active)
2841 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002842 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302843 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002844}
2845
Ingo Molnare309b412008-05-12 21:20:51 +02002846static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002847{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302848 int command;
2849
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002850 if (unlikely(ftrace_disabled))
2851 return;
2852
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002853 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302854 if (ftrace_start_up) {
2855 command = FTRACE_DISABLE_CALLS;
2856 if (ftrace_graph_active)
2857 command |= FTRACE_STOP_FUNC_RET;
2858 ftrace_run_update_code(command);
2859 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002860}
2861
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002862static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002863unsigned long ftrace_update_tot_cnt;
2864
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002865static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002866{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002867 /*
2868 * Filter_hash being empty will default to trace module.
2869 * But notrace hash requires a test of individual module functions.
2870 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002871 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2872 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002873}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002874
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002875/*
2876 * Check if the current ops references the record.
2877 *
2878 * If the ops traces all functions, then it was already accounted for.
2879 * If the ops does not trace the current record function, skip it.
2880 * If the ops ignores the function via notrace filter, skip it.
2881 */
2882static inline bool
2883ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2884{
2885 /* If ops isn't enabled, ignore it */
2886 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002887 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002888
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002889 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002890 if (ops_traces_mod(ops))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002891 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002892
2893 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002894 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002895 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002896 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002897
2898 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002899 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002900 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002901
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002902 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002903}
2904
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002905static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002906{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002907 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002908 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002909 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002910 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002911 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002912 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002913
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002914 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002915
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002916 /*
2917 * When a module is loaded, this function is called to convert
2918 * the calls to mcount in its text to nops, and also to create
2919 * an entry in the ftrace data. Now, if ftrace is activated
2920 * after this call, but before the module sets its text to
2921 * read-only, the modification of enabling ftrace can fail if
2922 * the read-only is done while ftrace is converting the calls.
2923 * To prevent this, the module's records are set as disabled
2924 * and will be enabled after the call to set the module's text
2925 * to read-only.
2926 */
2927 if (mod)
2928 rec_flags |= FTRACE_FL_DISABLED;
2929
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002930 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302931
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002932 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002933
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002934 /* If something went wrong, bail without enabling anything */
2935 if (unlikely(ftrace_disabled))
2936 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002937
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002938 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002939 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302940
Vasily Gorbik2f4df002018-08-06 15:17:46 +02002941#ifndef CC_USING_NOP_MCOUNT
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002942 /*
2943 * Do the initial record conversion from mcount jump
2944 * to the NOP instructions.
2945 */
2946 if (!ftrace_code_disable(mod, p))
2947 break;
Vasily Gorbik2f4df002018-08-06 15:17:46 +02002948#endif
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002949
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002950 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002951 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002952 }
2953
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002954 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002955 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002956 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002957
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002958 return 0;
2959}
2960
Steven Rostedta7900872011-12-16 16:23:44 -05002961static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002962{
Steven Rostedta7900872011-12-16 16:23:44 -05002963 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002964 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002965
Steven Rostedta7900872011-12-16 16:23:44 -05002966 if (WARN_ON(!count))
2967 return -EINVAL;
2968
2969 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002970
2971 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002972 * We want to fill as much as possible. No more than a page
2973 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002974 */
Steven Rostedta7900872011-12-16 16:23:44 -05002975 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2976 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002977
Steven Rostedta7900872011-12-16 16:23:44 -05002978 again:
2979 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2980
2981 if (!pg->records) {
2982 /* if we can't allocate this size, try something smaller */
2983 if (!order)
2984 return -ENOMEM;
2985 order >>= 1;
2986 goto again;
2987 }
2988
2989 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2990 pg->size = cnt;
2991
2992 if (cnt > count)
2993 cnt = count;
2994
2995 return cnt;
2996}
2997
2998static struct ftrace_page *
2999ftrace_allocate_pages(unsigned long num_to_init)
3000{
3001 struct ftrace_page *start_pg;
3002 struct ftrace_page *pg;
3003 int order;
3004 int cnt;
3005
3006 if (!num_to_init)
3007 return 0;
3008
3009 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3010 if (!pg)
3011 return NULL;
3012
3013 /*
3014 * Try to allocate as much as possible in one continues
3015 * location that fills in all of the space. We want to
3016 * waste as little space as possible.
3017 */
3018 for (;;) {
3019 cnt = ftrace_allocate_records(pg, num_to_init);
3020 if (cnt < 0)
3021 goto free_pages;
3022
3023 num_to_init -= cnt;
3024 if (!num_to_init)
3025 break;
3026
3027 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3028 if (!pg->next)
3029 goto free_pages;
3030
3031 pg = pg->next;
3032 }
3033
3034 return start_pg;
3035
3036 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003037 pg = start_pg;
3038 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003039 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3040 free_pages((unsigned long)pg->records, order);
3041 start_pg = pg->next;
3042 kfree(pg);
3043 pg = start_pg;
3044 }
3045 pr_info("ftrace: FAILED to allocate memory for functions\n");
3046 return NULL;
3047}
3048
Steven Rostedt5072c592008-05-12 21:20:43 +02003049#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3050
3051struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003052 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003053 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003054 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003055 struct ftrace_page *pg;
3056 struct dyn_ftrace *func;
3057 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003058 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003059 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003060 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003061 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003062 struct trace_array *tr;
3063 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003064 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003065 int idx;
3066 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003067};
3068
Ingo Molnare309b412008-05-12 21:20:51 +02003069static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003070t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003071{
3072 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003073 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003074 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003075 struct ftrace_hash *hash;
3076 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003077 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003078 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003079 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003080
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003081 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003082 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003083
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003084 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003085 return NULL;
3086
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003087 func_probes = &tr->func_probes;
3088 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003089 return NULL;
3090
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003091 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003092 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003093 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003094 }
3095
3096 if (iter->probe_entry)
3097 hnd = &iter->probe_entry->hlist;
3098
3099 hash = iter->probe->ops.func_hash->filter_hash;
3100 size = 1 << hash->size_bits;
3101
3102 retry:
3103 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003104 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003105 return NULL;
3106 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003107 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003108 hash = iter->probe->ops.func_hash->filter_hash;
3109 size = 1 << hash->size_bits;
3110 iter->pidx = 0;
3111 }
3112
3113 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003114
3115 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003116 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003117 hnd = NULL;
3118 goto retry;
3119 }
3120
3121 if (!hnd)
3122 hnd = hhd->first;
3123 else {
3124 hnd = hnd->next;
3125 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003126 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003127 goto retry;
3128 }
3129 }
3130
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003131 if (WARN_ON_ONCE(!hnd))
3132 return NULL;
3133
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003134 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003135
3136 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003137}
3138
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003139static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003140{
3141 struct ftrace_iterator *iter = m->private;
3142 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003143 loff_t l;
3144
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003145 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003146 return NULL;
3147
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003148 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003149 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003150
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003151 iter->probe = NULL;
3152 iter->probe_entry = NULL;
3153 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003154 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003155 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003156 if (!p)
3157 break;
3158 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003159 if (!p)
3160 return NULL;
3161
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003162 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003163 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003164
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003165 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003166}
3167
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003168static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003169t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003170{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003171 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003172 struct ftrace_probe_ops *probe_ops;
3173 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003174
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003175 probe = iter->probe;
3176 probe_entry = iter->probe_entry;
3177
3178 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003179 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003180
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003181 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003182
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003183 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003184 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003185
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003186 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3187 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003188
3189 return 0;
3190}
3191
3192static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003193t_mod_next(struct seq_file *m, loff_t *pos)
3194{
3195 struct ftrace_iterator *iter = m->private;
3196 struct trace_array *tr = iter->tr;
3197
3198 (*pos)++;
3199 iter->pos = *pos;
3200
3201 iter->mod_list = iter->mod_list->next;
3202
3203 if (iter->mod_list == &tr->mod_trace ||
3204 iter->mod_list == &tr->mod_notrace) {
3205 iter->flags &= ~FTRACE_ITER_MOD;
3206 return NULL;
3207 }
3208
3209 iter->mod_pos = *pos;
3210
3211 return iter;
3212}
3213
3214static void *t_mod_start(struct seq_file *m, loff_t *pos)
3215{
3216 struct ftrace_iterator *iter = m->private;
3217 void *p = NULL;
3218 loff_t l;
3219
3220 if (iter->func_pos > *pos)
3221 return NULL;
3222
3223 iter->mod_pos = iter->func_pos;
3224
3225 /* probes are only available if tr is set */
3226 if (!iter->tr)
3227 return NULL;
3228
3229 for (l = 0; l <= (*pos - iter->func_pos); ) {
3230 p = t_mod_next(m, &l);
3231 if (!p)
3232 break;
3233 }
3234 if (!p) {
3235 iter->flags &= ~FTRACE_ITER_MOD;
3236 return t_probe_start(m, pos);
3237 }
3238
3239 /* Only set this if we have an item */
3240 iter->flags |= FTRACE_ITER_MOD;
3241
3242 return iter;
3243}
3244
3245static int
3246t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3247{
3248 struct ftrace_mod_load *ftrace_mod;
3249 struct trace_array *tr = iter->tr;
3250
3251 if (WARN_ON_ONCE(!iter->mod_list) ||
3252 iter->mod_list == &tr->mod_trace ||
3253 iter->mod_list == &tr->mod_notrace)
3254 return -EIO;
3255
3256 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3257
3258 if (ftrace_mod->func)
3259 seq_printf(m, "%s", ftrace_mod->func);
3260 else
3261 seq_putc(m, '*');
3262
3263 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3264
3265 return 0;
3266}
3267
3268static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003269t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003270{
3271 struct ftrace_iterator *iter = m->private;
3272 struct dyn_ftrace *rec = NULL;
3273
3274 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003275
Steven Rostedt5072c592008-05-12 21:20:43 +02003276 retry:
3277 if (iter->idx >= iter->pg->index) {
3278 if (iter->pg->next) {
3279 iter->pg = iter->pg->next;
3280 iter->idx = 0;
3281 goto retry;
3282 }
3283 } else {
3284 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003285 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3286 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003287
3288 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003289 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003290
Steven Rostedt5072c592008-05-12 21:20:43 +02003291 rec = NULL;
3292 goto retry;
3293 }
3294 }
3295
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003296 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003297 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003298
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003299 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003300 iter->func = rec;
3301
3302 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003303}
3304
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003305static void *
3306t_next(struct seq_file *m, void *v, loff_t *pos)
3307{
3308 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003309 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003310 void *ret;
3311
3312 if (unlikely(ftrace_disabled))
3313 return NULL;
3314
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003315 if (iter->flags & FTRACE_ITER_PROBE)
3316 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003317
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003318 if (iter->flags & FTRACE_ITER_MOD)
3319 return t_mod_next(m, pos);
3320
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003321 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003322 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003323 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003324 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003325 }
3326
3327 ret = t_func_next(m, pos);
3328
3329 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003330 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003331
3332 return ret;
3333}
3334
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003335static void reset_iter_read(struct ftrace_iterator *iter)
3336{
3337 iter->pos = 0;
3338 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003339 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003340}
3341
3342static void *t_start(struct seq_file *m, loff_t *pos)
3343{
3344 struct ftrace_iterator *iter = m->private;
3345 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003346 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003347
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003348 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003349
3350 if (unlikely(ftrace_disabled))
3351 return NULL;
3352
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003353 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003354 * If an lseek was done, then reset and start from beginning.
3355 */
3356 if (*pos < iter->pos)
3357 reset_iter_read(iter);
3358
3359 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003360 * For set_ftrace_filter reading, if we have the filter
3361 * off, we can short cut and just print out that all
3362 * functions are enabled.
3363 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003364 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3365 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003366 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003367 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003368 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003369 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003370 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003371 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003372 return iter;
3373 }
3374
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003375 if (iter->flags & FTRACE_ITER_MOD)
3376 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003377
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003378 /*
3379 * Unfortunately, we need to restart at ftrace_pages_start
3380 * every time we let go of the ftrace_mutex. This is because
3381 * those pointers can change without the lock.
3382 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003383 iter->pg = ftrace_pages_start;
3384 iter->idx = 0;
3385 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003386 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003387 if (!p)
3388 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003389 }
walimis5821e1b2008-11-15 15:19:06 +08003390
Steven Rostedt69a30832011-12-19 15:21:16 -05003391 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003392 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003393
3394 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003395}
3396
3397static void t_stop(struct seq_file *m, void *p)
3398{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003399 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003400}
3401
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003402void * __weak
3403arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3404{
3405 return NULL;
3406}
3407
3408static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3409 struct dyn_ftrace *rec)
3410{
3411 void *ptr;
3412
3413 ptr = arch_ftrace_trampoline_func(ops, rec);
3414 if (ptr)
3415 seq_printf(m, " ->%pS", ptr);
3416}
3417
Steven Rostedt5072c592008-05-12 21:20:43 +02003418static int t_show(struct seq_file *m, void *v)
3419{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003420 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003421 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003422
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003423 if (iter->flags & FTRACE_ITER_PROBE)
3424 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003425
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003426 if (iter->flags & FTRACE_ITER_MOD)
3427 return t_mod_show(m, iter);
3428
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003429 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003430 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003431 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003432 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003433 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003434 return 0;
3435 }
3436
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003437 rec = iter->func;
3438
Steven Rostedt5072c592008-05-12 21:20:43 +02003439 if (!rec)
3440 return 0;
3441
Steven Rostedt647bcd02011-05-03 14:39:21 -04003442 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003443 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003444 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003445
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003446 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003447 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003448 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3449 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003450 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003451 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003452 if (ops) {
3453 do {
3454 seq_printf(m, "\ttramp: %pS (%pS)",
3455 (void *)ops->trampoline,
3456 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003457 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003458 ops = ftrace_find_tramp_ops_next(rec, ops);
3459 } while (ops);
3460 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003461 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003462 } else {
3463 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003464 }
3465 }
3466
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003467 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003468
3469 return 0;
3470}
3471
James Morris88e9d342009-09-22 16:43:43 -07003472static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003473 .start = t_start,
3474 .next = t_next,
3475 .stop = t_stop,
3476 .show = t_show,
3477};
3478
Ingo Molnare309b412008-05-12 21:20:51 +02003479static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003480ftrace_avail_open(struct inode *inode, struct file *file)
3481{
3482 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003483
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003484 if (unlikely(ftrace_disabled))
3485 return -ENODEV;
3486
Jiri Olsa50e18b92012-04-25 10:23:39 +02003487 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003488 if (!iter)
3489 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003490
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003491 iter->pg = ftrace_pages_start;
3492 iter->ops = &global_ops;
3493
3494 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003495}
3496
Steven Rostedt647bcd02011-05-03 14:39:21 -04003497static int
3498ftrace_enabled_open(struct inode *inode, struct file *file)
3499{
3500 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003501
Jiri Olsa50e18b92012-04-25 10:23:39 +02003502 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003503 if (!iter)
3504 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003505
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003506 iter->pg = ftrace_pages_start;
3507 iter->flags = FTRACE_ITER_ENABLED;
3508 iter->ops = &global_ops;
3509
3510 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003511}
3512
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003513/**
3514 * ftrace_regex_open - initialize function tracer filter files
3515 * @ops: The ftrace_ops that hold the hash filters
3516 * @flag: The type of filter to process
3517 * @inode: The inode, usually passed in to your open routine
3518 * @file: The file, usually passed in to your open routine
3519 *
3520 * ftrace_regex_open() initializes the filter files for the
3521 * @ops. Depending on @flag it may process the filter hash or
3522 * the notrace hash of @ops. With this called from the open
3523 * routine, you can use ftrace_filter_write() for the write
3524 * routine if @flag has FTRACE_ITER_FILTER set, or
3525 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003526 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003527 * release must call ftrace_regex_release().
3528 */
3529int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003530ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003531 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003532{
3533 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003534 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003535 struct list_head *mod_head;
3536 struct trace_array *tr = ops->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003537 int ret = 0;
3538
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003539 ftrace_ops_init(ops);
3540
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003541 if (unlikely(ftrace_disabled))
3542 return -ENODEV;
3543
Steven Rostedt5072c592008-05-12 21:20:43 +02003544 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3545 if (!iter)
3546 return -ENOMEM;
3547
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003548 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3549 kfree(iter);
3550 return -ENOMEM;
3551 }
3552
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003553 iter->ops = ops;
3554 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003555 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003556
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003557 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003558
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003559 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003560 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003561 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003562 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003563 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003564 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003565 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003566
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003567 iter->mod_list = mod_head;
3568
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003569 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003570 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3571
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003572 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003573 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003574 clear_ftrace_mod_list(mod_head);
3575 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003576 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003577 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003578
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003579 if (!iter->hash) {
3580 trace_parser_put(&iter->parser);
3581 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003582 ret = -ENOMEM;
3583 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003584 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003585 } else
3586 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003587
Steven Rostedt5072c592008-05-12 21:20:43 +02003588 if (file->f_mode & FMODE_READ) {
3589 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003590
3591 ret = seq_open(file, &show_ftrace_seq_ops);
3592 if (!ret) {
3593 struct seq_file *m = file->private_data;
3594 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003595 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003596 /* Failed */
3597 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003598 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003599 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003600 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003601 } else
3602 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003603
3604 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003605 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003606
3607 return ret;
3608}
3609
Steven Rostedt41c52c02008-05-22 11:46:33 -04003610static int
3611ftrace_filter_open(struct inode *inode, struct file *file)
3612{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003613 struct ftrace_ops *ops = inode->i_private;
3614
3615 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003616 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003617 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003618}
3619
3620static int
3621ftrace_notrace_open(struct inode *inode, struct file *file)
3622{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003623 struct ftrace_ops *ops = inode->i_private;
3624
3625 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003626 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003627}
3628
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003629/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3630struct ftrace_glob {
3631 char *search;
3632 unsigned len;
3633 int type;
3634};
3635
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003636/*
3637 * If symbols in an architecture don't correspond exactly to the user-visible
3638 * name of what they represent, it is possible to define this function to
3639 * perform the necessary adjustments.
3640*/
3641char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3642{
3643 return str;
3644}
3645
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003646static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003647{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003648 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003649 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003650
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003651 str = arch_ftrace_match_adjust(str, g->search);
3652
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003653 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003654 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003655 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003656 matched = 1;
3657 break;
3658 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003659 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003660 matched = 1;
3661 break;
3662 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003663 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003664 matched = 1;
3665 break;
3666 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003667 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003668 if (slen >= g->len &&
3669 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003670 matched = 1;
3671 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003672 case MATCH_GLOB:
3673 if (glob_match(g->search, str))
3674 matched = 1;
3675 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003676 }
3677
3678 return matched;
3679}
3680
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003681static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003682enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003683{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003684 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003685 int ret = 0;
3686
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003687 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003688 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003689 /* Do nothing if it doesn't exist */
3690 if (!entry)
3691 return 0;
3692
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003693 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003694 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003695 /* Do nothing if it exists */
3696 if (entry)
3697 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003698
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003699 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003700 }
3701 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003702}
3703
Steven Rostedt64e7c442009-02-13 17:08:48 -05003704static int
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003705add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3706 int clear_filter)
3707{
3708 long index = simple_strtoul(func_g->search, NULL, 0);
3709 struct ftrace_page *pg;
3710 struct dyn_ftrace *rec;
3711
3712 /* The index starts at 1 */
3713 if (--index < 0)
3714 return 0;
3715
3716 do_for_each_ftrace_rec(pg, rec) {
3717 if (pg->index <= index) {
3718 index -= pg->index;
3719 /* this is a double loop, break goes to the next page */
3720 break;
3721 }
3722 rec = &pg->records[index];
3723 enter_record(hash, rec, clear_filter);
3724 return 1;
3725 } while_for_each_ftrace_rec();
3726 return 0;
3727}
3728
3729static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003730ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3731 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003732{
3733 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003734 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003735
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003736 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3737
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003738 if (mod_g) {
3739 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3740
3741 /* blank module name to match all modules */
3742 if (!mod_g->len) {
3743 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003744 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003745 goto func_match;
3746 return 0;
3747 }
3748
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003749 /*
3750 * exclude_mod is set to trace everything but the given
3751 * module. If it is set and the module matches, then
3752 * return 0. If it is not set, and the module doesn't match
3753 * also return 0. Otherwise, check the function to see if
3754 * that matches.
3755 */
3756 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003757 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003758func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003759 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003760 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003761 return 1;
3762 }
3763
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003764 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003765}
3766
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003767static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003768match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003769{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003770 struct ftrace_page *pg;
3771 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003772 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003773 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3774 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3775 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003776 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003777 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03003778 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003779
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003780 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003781 func_g.type = filter_parse_regex(func, len, &func_g.search,
3782 &clear_filter);
3783 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003784 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003785
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003786 if (mod) {
3787 mod_g.type = filter_parse_regex(mod, strlen(mod),
3788 &mod_g.search, &exclude_mod);
3789 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003790 }
3791
Steven Rostedt52baf112009-02-14 01:15:39 -05003792 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003793
3794 if (unlikely(ftrace_disabled))
3795 goto out_unlock;
3796
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003797 if (func_g.type == MATCH_INDEX) {
3798 found = add_rec_by_index(hash, &func_g, clear_filter);
3799 goto out_unlock;
3800 }
3801
Steven Rostedt265c8312009-02-13 12:43:56 -05003802 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003803
3804 if (rec->flags & FTRACE_FL_DISABLED)
3805 continue;
3806
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003807 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003808 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003809 if (ret < 0) {
3810 found = ret;
3811 goto out_unlock;
3812 }
Li Zefan311d16d2009-12-08 11:15:11 +08003813 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003814 }
3815 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003816 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003817 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003818
3819 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003820}
3821
Steven Rostedt64e7c442009-02-13 17:08:48 -05003822static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003823ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003824{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003825 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003826}
3827
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003828static void ftrace_ops_update_code(struct ftrace_ops *ops,
3829 struct ftrace_ops_hash *old_hash)
3830{
3831 struct ftrace_ops *op;
3832
3833 if (!ftrace_enabled)
3834 return;
3835
3836 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3837 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3838 return;
3839 }
3840
3841 /*
3842 * If this is the shared global_ops filter, then we need to
3843 * check if there is another ops that shares it, is enabled.
3844 * If so, we still need to run the modify code.
3845 */
3846 if (ops->func_hash != &global_ops.local_hash)
3847 return;
3848
3849 do_for_each_ftrace_op(op, ftrace_ops_list) {
3850 if (op->func_hash == &global_ops.local_hash &&
3851 op->flags & FTRACE_OPS_FL_ENABLED) {
3852 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3853 /* Only need to do this once */
3854 return;
3855 }
3856 } while_for_each_ftrace_op(op);
3857}
3858
3859static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3860 struct ftrace_hash **orig_hash,
3861 struct ftrace_hash *hash,
3862 int enable)
3863{
3864 struct ftrace_ops_hash old_hash_ops;
3865 struct ftrace_hash *old_hash;
3866 int ret;
3867
3868 old_hash = *orig_hash;
3869 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3870 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3871 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3872 if (!ret) {
3873 ftrace_ops_update_code(ops, &old_hash_ops);
3874 free_ftrace_hash_rcu(old_hash);
3875 }
3876 return ret;
3877}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003878
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003879static bool module_exists(const char *module)
3880{
3881 /* All modules have the symbol __this_module */
3882 const char this_mod[] = "__this_module";
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003883 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003884 unsigned long val;
3885 int n;
3886
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003887 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003888
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003889 if (n > sizeof(modname) - 1)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003890 return false;
3891
3892 val = module_kallsyms_lookup_name(modname);
3893 return val != 0;
3894}
3895
3896static int cache_mod(struct trace_array *tr,
3897 const char *func, char *module, int enable)
3898{
3899 struct ftrace_mod_load *ftrace_mod, *n;
3900 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3901 int ret;
3902
3903 mutex_lock(&ftrace_lock);
3904
3905 /* We do not cache inverse filters */
3906 if (func[0] == '!') {
3907 func++;
3908 ret = -EINVAL;
3909
3910 /* Look to remove this hash */
3911 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3912 if (strcmp(ftrace_mod->module, module) != 0)
3913 continue;
3914
3915 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03003916 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003917 (ftrace_mod->func &&
3918 strcmp(ftrace_mod->func, func) == 0)) {
3919 ret = 0;
3920 free_ftrace_mod(ftrace_mod);
3921 continue;
3922 }
3923 }
3924 goto out;
3925 }
3926
3927 ret = -EINVAL;
3928 /* We only care about modules that have not been loaded yet */
3929 if (module_exists(module))
3930 goto out;
3931
3932 /* Save this string off, and execute it when the module is loaded */
3933 ret = ftrace_add_mod(tr, func, module, enable);
3934 out:
3935 mutex_unlock(&ftrace_lock);
3936
3937 return ret;
3938}
3939
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003940static int
3941ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3942 int reset, int enable);
3943
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003944#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003945static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3946 char *mod, bool enable)
3947{
3948 struct ftrace_mod_load *ftrace_mod, *n;
3949 struct ftrace_hash **orig_hash, *new_hash;
3950 LIST_HEAD(process_mods);
3951 char *func;
3952 int ret;
3953
3954 mutex_lock(&ops->func_hash->regex_lock);
3955
3956 if (enable)
3957 orig_hash = &ops->func_hash->filter_hash;
3958 else
3959 orig_hash = &ops->func_hash->notrace_hash;
3960
3961 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3962 *orig_hash);
3963 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04003964 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003965
3966 mutex_lock(&ftrace_lock);
3967
3968 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3969
3970 if (strcmp(ftrace_mod->module, mod) != 0)
3971 continue;
3972
3973 if (ftrace_mod->func)
3974 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
3975 else
3976 func = kstrdup("*", GFP_KERNEL);
3977
3978 if (!func) /* warn? */
3979 continue;
3980
3981 list_del(&ftrace_mod->list);
3982 list_add(&ftrace_mod->list, &process_mods);
3983
3984 /* Use the newly allocated func, as it may be "*" */
3985 kfree(ftrace_mod->func);
3986 ftrace_mod->func = func;
3987 }
3988
3989 mutex_unlock(&ftrace_lock);
3990
3991 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
3992
3993 func = ftrace_mod->func;
3994
3995 /* Grabs ftrace_lock, which is why we have this extra step */
3996 match_records(new_hash, func, strlen(func), mod);
3997 free_ftrace_mod(ftrace_mod);
3998 }
3999
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04004000 if (enable && list_empty(head))
4001 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4002
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004003 mutex_lock(&ftrace_lock);
4004
4005 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4006 new_hash, enable);
4007 mutex_unlock(&ftrace_lock);
4008
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004009 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004010 mutex_unlock(&ops->func_hash->regex_lock);
4011
4012 free_ftrace_hash(new_hash);
4013}
4014
4015static void process_cached_mods(const char *mod_name)
4016{
4017 struct trace_array *tr;
4018 char *mod;
4019
4020 mod = kstrdup(mod_name, GFP_KERNEL);
4021 if (!mod)
4022 return;
4023
4024 mutex_lock(&trace_types_lock);
4025 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4026 if (!list_empty(&tr->mod_trace))
4027 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4028 if (!list_empty(&tr->mod_notrace))
4029 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4030 }
4031 mutex_unlock(&trace_types_lock);
4032
4033 kfree(mod);
4034}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004035#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004036
Steven Rostedtf6180772009-02-14 00:40:25 -05004037/*
4038 * We register the module command as a template to show others how
4039 * to register the a command as well.
4040 */
4041
4042static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004043ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004044 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004045{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004046 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004047 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004048
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004049 /* match_records() modifies func, and we need the original */
4050 func = kstrdup(func_orig, GFP_KERNEL);
4051 if (!func)
4052 return -ENOMEM;
4053
Steven Rostedtf6180772009-02-14 00:40:25 -05004054 /*
4055 * cmd == 'mod' because we only registered this func
4056 * for the 'mod' ftrace_func_command.
4057 * But if you register one func with multiple commands,
4058 * you can tell which command was used by the cmd
4059 * parameter.
4060 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004061 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004062 kfree(func);
4063
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004064 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004065 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004066 if (ret < 0)
4067 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004068 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004069}
4070
4071static struct ftrace_func_command ftrace_mod_cmd = {
4072 .name = "mod",
4073 .func = ftrace_mod_callback,
4074};
4075
4076static int __init ftrace_mod_cmd_init(void)
4077{
4078 return register_ftrace_command(&ftrace_mod_cmd);
4079}
Steven Rostedt6f415672012-10-05 12:13:07 -04004080core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004081
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004082static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004083 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004084{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004085 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004086 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004087
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004088 probe = container_of(op, struct ftrace_func_probe, ops);
4089 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004090
4091 /*
4092 * Disable preemption for these calls to prevent a RCU grace
4093 * period. This syncs the hash iteration and freeing of items
4094 * on the hash. rcu_read_lock is too dangerous here.
4095 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004096 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004097 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004098 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004099}
4100
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004101struct ftrace_func_map {
4102 struct ftrace_func_entry entry;
4103 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004104};
4105
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004106struct ftrace_func_mapper {
4107 struct ftrace_hash hash;
4108};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004109
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004110/**
4111 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4112 *
4113 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4114 */
4115struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004116{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004117 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004118
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004119 /*
4120 * The mapper is simply a ftrace_hash, but since the entries
4121 * in the hash are not ftrace_func_entry type, we define it
4122 * as a separate structure.
4123 */
4124 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4125 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004126}
4127
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004128/**
4129 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4130 * @mapper: The mapper that has the ip maps
4131 * @ip: the instruction pointer to find the data for
4132 *
4133 * Returns the data mapped to @ip if found otherwise NULL. The return
4134 * is actually the address of the mapper data pointer. The address is
4135 * returned for use cases where the data is no bigger than a long, and
4136 * the user can use the data pointer as its data instead of having to
4137 * allocate more memory for the reference.
4138 */
4139void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4140 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004141{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004142 struct ftrace_func_entry *entry;
4143 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004144
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004145 entry = ftrace_lookup_ip(&mapper->hash, ip);
4146 if (!entry)
4147 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004148
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004149 map = (struct ftrace_func_map *)entry;
4150 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004151}
4152
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004153/**
4154 * ftrace_func_mapper_add_ip - Map some data to an ip
4155 * @mapper: The mapper that has the ip maps
4156 * @ip: The instruction pointer address to map @data to
4157 * @data: The data to map to @ip
4158 *
4159 * Returns 0 on succes otherwise an error.
4160 */
4161int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4162 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004163{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004164 struct ftrace_func_entry *entry;
4165 struct ftrace_func_map *map;
4166
4167 entry = ftrace_lookup_ip(&mapper->hash, ip);
4168 if (entry)
4169 return -EBUSY;
4170
4171 map = kmalloc(sizeof(*map), GFP_KERNEL);
4172 if (!map)
4173 return -ENOMEM;
4174
4175 map->entry.ip = ip;
4176 map->data = data;
4177
4178 __add_hash_entry(&mapper->hash, &map->entry);
4179
4180 return 0;
4181}
4182
4183/**
4184 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4185 * @mapper: The mapper that has the ip maps
4186 * @ip: The instruction pointer address to remove the data from
4187 *
4188 * Returns the data if it is found, otherwise NULL.
4189 * Note, if the data pointer is used as the data itself, (see
4190 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4191 * if the data pointer was set to zero.
4192 */
4193void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4194 unsigned long ip)
4195{
4196 struct ftrace_func_entry *entry;
4197 struct ftrace_func_map *map;
4198 void *data;
4199
4200 entry = ftrace_lookup_ip(&mapper->hash, ip);
4201 if (!entry)
4202 return NULL;
4203
4204 map = (struct ftrace_func_map *)entry;
4205 data = map->data;
4206
4207 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004208 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004209
4210 return data;
4211}
4212
4213/**
4214 * free_ftrace_func_mapper - free a mapping of ips and data
4215 * @mapper: The mapper that has the ip maps
4216 * @free_func: A function to be called on each data item.
4217 *
4218 * This is used to free the function mapper. The @free_func is optional
4219 * and can be used if the data needs to be freed as well.
4220 */
4221void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4222 ftrace_mapper_func free_func)
4223{
4224 struct ftrace_func_entry *entry;
4225 struct ftrace_func_map *map;
4226 struct hlist_head *hhd;
4227 int size = 1 << mapper->hash.size_bits;
4228 int i;
4229
4230 if (free_func && mapper->hash.count) {
4231 for (i = 0; i < size; i++) {
4232 hhd = &mapper->hash.buckets[i];
4233 hlist_for_each_entry(entry, hhd, hlist) {
4234 map = (struct ftrace_func_map *)entry;
4235 free_func(map);
4236 }
4237 }
4238 }
4239 free_ftrace_hash(&mapper->hash);
4240}
4241
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004242static void release_probe(struct ftrace_func_probe *probe)
4243{
4244 struct ftrace_probe_ops *probe_ops;
4245
4246 mutex_lock(&ftrace_lock);
4247
4248 WARN_ON(probe->ref <= 0);
4249
4250 /* Subtract the ref that was used to protect this instance */
4251 probe->ref--;
4252
4253 if (!probe->ref) {
4254 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004255 /*
4256 * Sending zero as ip tells probe_ops to free
4257 * the probe->data itself
4258 */
4259 if (probe_ops->free)
4260 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004261 list_del(&probe->list);
4262 kfree(probe);
4263 }
4264 mutex_unlock(&ftrace_lock);
4265}
4266
4267static void acquire_probe_locked(struct ftrace_func_probe *probe)
4268{
4269 /*
4270 * Add one ref to keep it from being freed when releasing the
4271 * ftrace_lock mutex.
4272 */
4273 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004274}
4275
Steven Rostedt59df055f2009-02-14 15:29:06 -05004276int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004277register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004278 struct ftrace_probe_ops *probe_ops,
4279 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004280{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004281 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004282 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004283 struct ftrace_hash **orig_hash;
4284 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004285 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004286 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004287 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004288 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004289 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004290
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004291 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004292 return -EINVAL;
4293
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004294 /* We do not support '!' for function probes */
4295 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004296 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004297
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004298
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004299 mutex_lock(&ftrace_lock);
4300 /* Check if the probe_ops is already registered */
4301 list_for_each_entry(probe, &tr->func_probes, list) {
4302 if (probe->probe_ops == probe_ops)
4303 break;
4304 }
4305 if (&probe->list == &tr->func_probes) {
4306 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4307 if (!probe) {
4308 mutex_unlock(&ftrace_lock);
4309 return -ENOMEM;
4310 }
4311 probe->probe_ops = probe_ops;
4312 probe->ops.func = function_trace_probe_call;
4313 probe->tr = tr;
4314 ftrace_ops_init(&probe->ops);
4315 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004316 }
4317
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004318 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004319
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004320 mutex_unlock(&ftrace_lock);
4321
4322 mutex_lock(&probe->ops.func_hash->regex_lock);
4323
4324 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004325 old_hash = *orig_hash;
4326 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4327
4328 ret = ftrace_match_records(hash, glob, strlen(glob));
4329
4330 /* Nothing found? */
4331 if (!ret)
4332 ret = -EINVAL;
4333
4334 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004335 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004336
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004337 size = 1 << hash->size_bits;
4338 for (i = 0; i < size; i++) {
4339 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4340 if (ftrace_lookup_ip(old_hash, entry->ip))
4341 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004342 /*
4343 * The caller might want to do something special
4344 * for each function we find. We call the callback
4345 * to give the caller an opportunity to do so.
4346 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004347 if (probe_ops->init) {
4348 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004349 entry->ip, data,
4350 &probe->data);
4351 if (ret < 0) {
4352 if (probe_ops->free && count)
4353 probe_ops->free(probe_ops, tr,
4354 0, probe->data);
4355 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004356 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004357 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004358 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004359 count++;
4360 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004361 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004362
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004363 mutex_lock(&ftrace_lock);
4364
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004365 if (!count) {
4366 /* Nothing was added? */
4367 ret = -EINVAL;
4368 goto out_unlock;
4369 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004370
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004371 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4372 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004373 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004374 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004375
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004376 /* One ref for each new function traced */
4377 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004378
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004379 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4380 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004381
Steven Rostedt59df055f2009-02-14 15:29:06 -05004382 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004383 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004384
4385 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004386 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004387 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004388 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004389 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004390
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004391 release_probe(probe);
4392
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004393 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004394
4395 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004396 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004397 goto out_unlock;
4398
4399 /* Failed to do the move, need to call the free functions */
4400 for (i = 0; i < size; i++) {
4401 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4402 if (ftrace_lookup_ip(old_hash, entry->ip))
4403 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004404 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004405 }
4406 }
4407 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004408}
4409
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004410int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004411unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4412 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004413{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004414 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004415 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004416 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004417 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004418 struct ftrace_hash **orig_hash;
4419 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004420 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004421 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004422 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004423 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004424 int count = 0;
4425 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004426 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004427
Naveen N. Raocbab5672017-05-16 23:21:25 +05304428 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004429 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304430 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004431 int not;
4432
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004433 func_g.type = filter_parse_regex(glob, strlen(glob),
4434 &func_g.search, &not);
4435 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004436
Steven Rostedtb6887d72009-02-17 12:32:04 -05004437 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004438 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004439 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004440 }
4441
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004442 mutex_lock(&ftrace_lock);
4443 /* Check if the probe_ops is already registered */
4444 list_for_each_entry(probe, &tr->func_probes, list) {
4445 if (probe->probe_ops == probe_ops)
4446 break;
4447 }
4448 if (&probe->list == &tr->func_probes)
4449 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004450
4451 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004452 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4453 goto err_unlock_ftrace;
4454
4455 acquire_probe_locked(probe);
4456
4457 mutex_unlock(&ftrace_lock);
4458
4459 mutex_lock(&probe->ops.func_hash->regex_lock);
4460
4461 orig_hash = &probe->ops.func_hash->filter_hash;
4462 old_hash = *orig_hash;
4463
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004464 if (ftrace_hash_empty(old_hash))
4465 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004466
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004467 old_hash_ops.filter_hash = old_hash;
4468 /* Probes only have filters */
4469 old_hash_ops.notrace_hash = NULL;
4470
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004471 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004472 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004473 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004474 goto out_unlock;
4475
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004476 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004477
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004478 size = 1 << hash->size_bits;
4479 for (i = 0; i < size; i++) {
4480 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004481
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004482 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004483 kallsyms_lookup(entry->ip, NULL, NULL,
4484 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004485 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004486 continue;
4487 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004488 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004489 remove_hash_entry(hash, entry);
4490 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004491 }
4492 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004493
4494 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004495 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004496 ret = -EINVAL;
4497 goto out_unlock;
4498 }
4499
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004500 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004501
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004502 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004503
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004504 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004505
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004506 if (ftrace_hash_empty(hash))
4507 ftrace_shutdown(&probe->ops, 0);
4508
4509 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004510 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004511
4512 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004513 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004514 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004515 &old_hash_ops);
Paul E. McKenney74401722018-11-06 18:44:52 -08004516 synchronize_rcu();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004517
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004518 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4519 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004520 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004521 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004522 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004523 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004524 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004525
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004526 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004527 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004528 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004529
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004530 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004531
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004532 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004533
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004534 err_unlock_ftrace:
4535 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004536 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004537}
4538
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304539void clear_ftrace_function_probes(struct trace_array *tr)
4540{
4541 struct ftrace_func_probe *probe, *n;
4542
4543 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4544 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4545}
4546
Steven Rostedtf6180772009-02-14 00:40:25 -05004547static LIST_HEAD(ftrace_commands);
4548static DEFINE_MUTEX(ftrace_cmd_mutex);
4549
Tom Zanussi38de93a2013-10-24 08:34:18 -05004550/*
4551 * Currently we only register ftrace commands from __init, so mark this
4552 * __init too.
4553 */
4554__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004555{
4556 struct ftrace_func_command *p;
4557 int ret = 0;
4558
4559 mutex_lock(&ftrace_cmd_mutex);
4560 list_for_each_entry(p, &ftrace_commands, list) {
4561 if (strcmp(cmd->name, p->name) == 0) {
4562 ret = -EBUSY;
4563 goto out_unlock;
4564 }
4565 }
4566 list_add(&cmd->list, &ftrace_commands);
4567 out_unlock:
4568 mutex_unlock(&ftrace_cmd_mutex);
4569
4570 return ret;
4571}
4572
Tom Zanussi38de93a2013-10-24 08:34:18 -05004573/*
4574 * Currently we only unregister ftrace commands from __init, so mark
4575 * this __init too.
4576 */
4577__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004578{
4579 struct ftrace_func_command *p, *n;
4580 int ret = -ENODEV;
4581
4582 mutex_lock(&ftrace_cmd_mutex);
4583 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4584 if (strcmp(cmd->name, p->name) == 0) {
4585 ret = 0;
4586 list_del_init(&p->list);
4587 goto out_unlock;
4588 }
4589 }
4590 out_unlock:
4591 mutex_unlock(&ftrace_cmd_mutex);
4592
4593 return ret;
4594}
4595
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004596static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004597 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004598{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004599 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004600 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004601 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004602 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004603 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004604
4605 func = strsep(&next, ":");
4606
4607 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004608 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004609 if (!ret)
4610 ret = -EINVAL;
4611 if (ret < 0)
4612 return ret;
4613 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004614 }
4615
Steven Rostedtf6180772009-02-14 00:40:25 -05004616 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004617
4618 command = strsep(&next, ":");
4619
Steven Rostedtf6180772009-02-14 00:40:25 -05004620 mutex_lock(&ftrace_cmd_mutex);
4621 list_for_each_entry(p, &ftrace_commands, list) {
4622 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004623 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004624 goto out_unlock;
4625 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004626 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004627 out_unlock:
4628 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004629
Steven Rostedtf6180772009-02-14 00:40:25 -05004630 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004631}
4632
Ingo Molnare309b412008-05-12 21:20:51 +02004633static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004634ftrace_regex_write(struct file *file, const char __user *ubuf,
4635 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004636{
4637 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004638 struct trace_parser *parser;
4639 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004640
Li Zefan4ba79782009-09-22 13:52:20 +08004641 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004642 return 0;
4643
Steven Rostedt5072c592008-05-12 21:20:43 +02004644 if (file->f_mode & FMODE_READ) {
4645 struct seq_file *m = file->private_data;
4646 iter = m->private;
4647 } else
4648 iter = file->private_data;
4649
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004650 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004651 return -ENODEV;
4652
4653 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004654
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004655 parser = &iter->parser;
4656 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004657
Li Zefan4ba79782009-09-22 13:52:20 +08004658 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004659 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004660 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004661 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004662 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004663 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004664 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004665 }
4666
Steven Rostedt5072c592008-05-12 21:20:43 +02004667 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004668 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004669 return ret;
4670}
4671
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004672ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004673ftrace_filter_write(struct file *file, const char __user *ubuf,
4674 size_t cnt, loff_t *ppos)
4675{
4676 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4677}
4678
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004679ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004680ftrace_notrace_write(struct file *file, const char __user *ubuf,
4681 size_t cnt, loff_t *ppos)
4682{
4683 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4684}
4685
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004686static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004687ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4688{
4689 struct ftrace_func_entry *entry;
4690
4691 if (!ftrace_location(ip))
4692 return -EINVAL;
4693
4694 if (remove) {
4695 entry = ftrace_lookup_ip(hash, ip);
4696 if (!entry)
4697 return -ENOENT;
4698 free_hash_entry(hash, entry);
4699 return 0;
4700 }
4701
4702 return add_hash_entry(hash, ip);
4703}
4704
4705static int
4706ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4707 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004708{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004709 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004710 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004711 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004712
Steven Rostedt41c52c02008-05-22 11:46:33 -04004713 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004714 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004715
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004716 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004717
Steven Rostedtf45948e2011-05-02 12:29:25 -04004718 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004719 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004720 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004721 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004722
Wang Nanb972cc52014-07-15 08:40:20 +08004723 if (reset)
4724 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4725 else
4726 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4727
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004728 if (!hash) {
4729 ret = -ENOMEM;
4730 goto out_regex_unlock;
4731 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004732
Jiri Olsaac483c42012-01-02 10:04:14 +01004733 if (buf && !ftrace_match_records(hash, buf, len)) {
4734 ret = -EINVAL;
4735 goto out_regex_unlock;
4736 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004737 if (ip) {
4738 ret = ftrace_match_addr(hash, ip, remove);
4739 if (ret < 0)
4740 goto out_regex_unlock;
4741 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004742
4743 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004744 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004745 mutex_unlock(&ftrace_lock);
4746
Jiri Olsaac483c42012-01-02 10:04:14 +01004747 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004748 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004749
4750 free_ftrace_hash(hash);
4751 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004752}
4753
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004754static int
4755ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4756 int reset, int enable)
4757{
4758 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4759}
4760
4761/**
4762 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4763 * @ops - the ops to set the filter with
4764 * @ip - the address to add to or remove from the filter.
4765 * @remove - non zero to remove the ip from the filter
4766 * @reset - non zero to reset all filters before applying this filter.
4767 *
4768 * Filters denote which functions should be enabled when tracing is enabled
4769 * If @ip is NULL, it failes to update filter.
4770 */
4771int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4772 int remove, int reset)
4773{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004774 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004775 return ftrace_set_addr(ops, ip, remove, reset, 1);
4776}
4777EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4778
Joel Fernandesd032ae82016-11-15 12:31:20 -08004779/**
4780 * ftrace_ops_set_global_filter - setup ops to use global filters
4781 * @ops - the ops which will use the global filters
4782 *
4783 * ftrace users who need global function trace filtering should call this.
4784 * It can set the global filter only if ops were not initialized before.
4785 */
4786void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4787{
4788 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4789 return;
4790
4791 ftrace_ops_init(ops);
4792 ops->func_hash = &global_ops.local_hash;
4793}
4794EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4795
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004796static int
4797ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4798 int reset, int enable)
4799{
4800 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4801}
4802
Steven Rostedt77a2b372008-05-12 21:20:45 +02004803/**
4804 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004805 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004806 * @buf - the string that holds the function filter text.
4807 * @len - the length of the string.
4808 * @reset - non zero to reset all filters before applying this filter.
4809 *
4810 * Filters denote which functions should be enabled when tracing is enabled.
4811 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4812 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004813int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004814 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004815{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004816 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004817 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004818}
Steven Rostedt936e0742011-05-05 22:54:01 -04004819EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004820
Steven Rostedt41c52c02008-05-22 11:46:33 -04004821/**
4822 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004823 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004824 * @buf - the string that holds the function notrace text.
4825 * @len - the length of the string.
4826 * @reset - non zero to reset all filters before applying this filter.
4827 *
4828 * Notrace Filters denote which functions should not be enabled when tracing
4829 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4830 * for tracing.
4831 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004832int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004833 int len, int reset)
4834{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004835 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004836 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004837}
4838EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4839/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004840 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004841 * @buf - the string that holds the function filter text.
4842 * @len - the length of the string.
4843 * @reset - non zero to reset all filters before applying this filter.
4844 *
4845 * Filters denote which functions should be enabled when tracing is enabled.
4846 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4847 */
4848void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4849{
4850 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4851}
4852EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4853
4854/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004855 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004856 * @buf - the string that holds the function notrace text.
4857 * @len - the length of the string.
4858 * @reset - non zero to reset all filters before applying this filter.
4859 *
4860 * Notrace Filters denote which functions should not be enabled when tracing
4861 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4862 * for tracing.
4863 */
4864void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004865{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004866 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004867}
Steven Rostedt936e0742011-05-05 22:54:01 -04004868EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004869
Steven Rostedt2af15d62009-05-28 13:37:24 -04004870/*
4871 * command line interface to allow users to set filters on boot up.
4872 */
4873#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4874static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4875static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4876
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004877/* Used by function selftest to not test if filter is set */
4878bool ftrace_filter_param __initdata;
4879
Steven Rostedt2af15d62009-05-28 13:37:24 -04004880static int __init set_ftrace_notrace(char *str)
4881{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004882 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004883 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004884 return 1;
4885}
4886__setup("ftrace_notrace=", set_ftrace_notrace);
4887
4888static int __init set_ftrace_filter(char *str)
4889{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004890 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004891 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004892 return 1;
4893}
4894__setup("ftrace_filter=", set_ftrace_filter);
4895
Stefan Assmann369bc182009-10-12 22:17:21 +02004896#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004897static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004898static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004899static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004900
Stefan Assmann369bc182009-10-12 22:17:21 +02004901static int __init set_graph_function(char *str)
4902{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004903 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004904 return 1;
4905}
4906__setup("ftrace_graph_filter=", set_graph_function);
4907
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004908static int __init set_graph_notrace_function(char *str)
4909{
4910 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4911 return 1;
4912}
4913__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4914
Todd Brandt65a50c62017-03-02 16:12:15 -08004915static int __init set_graph_max_depth_function(char *str)
4916{
4917 if (!str)
4918 return 0;
4919 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4920 return 1;
4921}
4922__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4923
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004924static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004925{
4926 int ret;
4927 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004928 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004929
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004930 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4931 if (WARN_ON(!hash))
4932 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004933
4934 while (buf) {
4935 func = strsep(&buf, ",");
4936 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004937 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004938 if (ret)
4939 printk(KERN_DEBUG "ftrace: function %s not "
4940 "traceable\n", func);
4941 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004942
4943 if (enable)
4944 ftrace_graph_hash = hash;
4945 else
4946 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02004947}
4948#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4949
Steven Rostedt2a85a372011-12-19 21:57:44 -05004950void __init
4951ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004952{
4953 char *func;
4954
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004955 ftrace_ops_init(ops);
4956
Steven Rostedt2af15d62009-05-28 13:37:24 -04004957 while (buf) {
4958 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004959 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004960 }
4961}
4962
4963static void __init set_ftrace_early_filters(void)
4964{
4965 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004966 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004967 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004968 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004969#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4970 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004971 set_ftrace_early_graph(ftrace_graph_buf, 1);
4972 if (ftrace_graph_notrace_buf[0])
4973 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004974#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004975}
4976
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004977int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004978{
4979 struct seq_file *m = (struct seq_file *)file->private_data;
4980 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004981 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004982 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004983 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004984 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004985
Steven Rostedt5072c592008-05-12 21:20:43 +02004986 if (file->f_mode & FMODE_READ) {
4987 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004988 seq_release(inode, file);
4989 } else
4990 iter = file->private_data;
4991
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004992 parser = &iter->parser;
4993 if (trace_parser_loaded(parser)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004994 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004995 }
4996
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004997 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004998
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004999 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005000
Steven Rostedt058e2972011-04-29 22:35:33 -04005001 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04005002 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5003
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005004 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005005 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04005006 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005007 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5008 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005009 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005010
Steven Rostedt058e2972011-04-29 22:35:33 -04005011 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04005012 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5013 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04005014 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04005015 } else {
5016 /* For read only, the hash is the ops hash */
5017 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04005018 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005019
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005020 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005021 free_ftrace_hash(iter->hash);
5022 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04005023
Steven Rostedt5072c592008-05-12 21:20:43 +02005024 return 0;
5025}
5026
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005027static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005028 .open = ftrace_avail_open,
5029 .read = seq_read,
5030 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08005031 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02005032};
5033
Steven Rostedt647bcd02011-05-03 14:39:21 -04005034static const struct file_operations ftrace_enabled_fops = {
5035 .open = ftrace_enabled_open,
5036 .read = seq_read,
5037 .llseek = seq_lseek,
5038 .release = seq_release_private,
5039};
5040
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005041static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005042 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005043 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005044 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005045 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005046 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005047};
5048
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005049static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005050 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005051 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005052 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005053 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005054 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005055};
5056
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005057#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5058
5059static DEFINE_MUTEX(graph_lock);
5060
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005061struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5062struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5063
5064enum graph_filter_type {
5065 GRAPH_FILTER_NOTRACE = 0,
5066 GRAPH_FILTER_FUNCTION,
5067};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005068
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005069#define FTRACE_GRAPH_EMPTY ((void *)1)
5070
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005071struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005072 struct ftrace_hash *hash;
5073 struct ftrace_func_entry *entry;
5074 int idx; /* for hash table iteration */
5075 enum graph_filter_type type;
5076 struct ftrace_hash *new_hash;
5077 const struct seq_operations *seq_ops;
5078 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005079};
5080
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005081static void *
Li Zefan85951842009-06-24 09:54:00 +08005082__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005083{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005084 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005085 struct ftrace_func_entry *entry = fgd->entry;
5086 struct hlist_head *head;
5087 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005088
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005089 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005090 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005091
5092 if (entry) {
5093 hlist_for_each_entry_continue(entry, hlist) {
5094 fgd->entry = entry;
5095 return entry;
5096 }
5097
5098 idx++;
5099 }
5100
5101 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5102 head = &fgd->hash->buckets[i];
5103 hlist_for_each_entry(entry, head, hlist) {
5104 fgd->entry = entry;
5105 fgd->idx = i;
5106 return entry;
5107 }
5108 }
5109 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005110}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005111
Li Zefan85951842009-06-24 09:54:00 +08005112static void *
5113g_next(struct seq_file *m, void *v, loff_t *pos)
5114{
5115 (*pos)++;
5116 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005117}
5118
5119static void *g_start(struct seq_file *m, loff_t *pos)
5120{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005121 struct ftrace_graph_data *fgd = m->private;
5122
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005123 mutex_lock(&graph_lock);
5124
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005125 if (fgd->type == GRAPH_FILTER_FUNCTION)
5126 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5127 lockdep_is_held(&graph_lock));
5128 else
5129 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5130 lockdep_is_held(&graph_lock));
5131
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005132 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005133 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005134 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005135
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005136 fgd->idx = 0;
5137 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005138 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005139}
5140
5141static void g_stop(struct seq_file *m, void *p)
5142{
5143 mutex_unlock(&graph_lock);
5144}
5145
5146static int g_show(struct seq_file *m, void *v)
5147{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005148 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005149
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005150 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005151 return 0;
5152
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005153 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005154 struct ftrace_graph_data *fgd = m->private;
5155
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005156 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005157 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005158 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005159 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005160 return 0;
5161 }
5162
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005163 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005164
5165 return 0;
5166}
5167
James Morris88e9d342009-09-22 16:43:43 -07005168static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005169 .start = g_start,
5170 .next = g_next,
5171 .stop = g_stop,
5172 .show = g_show,
5173};
5174
5175static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005176__ftrace_graph_open(struct inode *inode, struct file *file,
5177 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005178{
5179 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005180 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005181
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005182 if (file->f_mode & FMODE_WRITE) {
5183 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5184
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005185 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5186 return -ENOMEM;
5187
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005188 if (file->f_flags & O_TRUNC)
5189 new_hash = alloc_ftrace_hash(size_bits);
5190 else
5191 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5192 fgd->hash);
5193 if (!new_hash) {
5194 ret = -ENOMEM;
5195 goto out;
5196 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005197 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005198
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005199 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005200 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005201 if (!ret) {
5202 struct seq_file *m = file->private_data;
5203 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005204 } else {
5205 /* Failed */
5206 free_ftrace_hash(new_hash);
5207 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005208 }
5209 } else
5210 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005211
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005212out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005213 if (ret < 0 && file->f_mode & FMODE_WRITE)
5214 trace_parser_put(&fgd->parser);
5215
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005216 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005217
5218 /*
5219 * All uses of fgd->hash must be taken with the graph_lock
5220 * held. The graph_lock is going to be released, so force
5221 * fgd->hash to be reinitialized when it is taken again.
5222 */
5223 fgd->hash = NULL;
5224
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005225 return ret;
5226}
5227
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005228static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005229ftrace_graph_open(struct inode *inode, struct file *file)
5230{
5231 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005232 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005233
5234 if (unlikely(ftrace_disabled))
5235 return -ENODEV;
5236
5237 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5238 if (fgd == NULL)
5239 return -ENOMEM;
5240
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005241 mutex_lock(&graph_lock);
5242
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005243 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5244 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005245 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005246 fgd->seq_ops = &ftrace_graph_seq_ops;
5247
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005248 ret = __ftrace_graph_open(inode, file, fgd);
5249 if (ret < 0)
5250 kfree(fgd);
5251
5252 mutex_unlock(&graph_lock);
5253 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005254}
5255
5256static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005257ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5258{
5259 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005260 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005261
5262 if (unlikely(ftrace_disabled))
5263 return -ENODEV;
5264
5265 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5266 if (fgd == NULL)
5267 return -ENOMEM;
5268
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005269 mutex_lock(&graph_lock);
5270
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005271 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5272 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005273 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005274 fgd->seq_ops = &ftrace_graph_seq_ops;
5275
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005276 ret = __ftrace_graph_open(inode, file, fgd);
5277 if (ret < 0)
5278 kfree(fgd);
5279
5280 mutex_unlock(&graph_lock);
5281 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005282}
5283
5284static int
Li Zefan87827112009-07-23 11:29:11 +08005285ftrace_graph_release(struct inode *inode, struct file *file)
5286{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005287 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005288 struct ftrace_hash *old_hash, *new_hash;
5289 struct trace_parser *parser;
5290 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005291
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005292 if (file->f_mode & FMODE_READ) {
5293 struct seq_file *m = file->private_data;
5294
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005295 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005296 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005297 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005298 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005299 }
5300
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005301
5302 if (file->f_mode & FMODE_WRITE) {
5303
5304 parser = &fgd->parser;
5305
5306 if (trace_parser_loaded((parser))) {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005307 ret = ftrace_graph_set_hash(fgd->new_hash,
5308 parser->buffer);
5309 }
5310
5311 trace_parser_put(parser);
5312
5313 new_hash = __ftrace_hash_move(fgd->new_hash);
5314 if (!new_hash) {
5315 ret = -ENOMEM;
5316 goto out;
5317 }
5318
5319 mutex_lock(&graph_lock);
5320
5321 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5322 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5323 lockdep_is_held(&graph_lock));
5324 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5325 } else {
5326 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5327 lockdep_is_held(&graph_lock));
5328 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5329 }
5330
5331 mutex_unlock(&graph_lock);
5332
5333 /* Wait till all users are no longer using the old hash */
Paul E. McKenney74401722018-11-06 18:44:52 -08005334 synchronize_rcu();
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005335
5336 free_ftrace_hash(old_hash);
5337 }
5338
5339 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005340 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005341 kfree(fgd);
5342
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005343 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005344}
5345
5346static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005347ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005348{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005349 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005350 struct dyn_ftrace *rec;
5351 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005352 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005353 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005354 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005355
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005356 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005357 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5358 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005359
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005360 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005361
Steven Rostedt52baf112009-02-14 01:15:39 -05005362 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005363
5364 if (unlikely(ftrace_disabled)) {
5365 mutex_unlock(&ftrace_lock);
5366 return -ENODEV;
5367 }
5368
Steven Rostedt265c8312009-02-13 12:43:56 -05005369 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005370
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005371 if (rec->flags & FTRACE_FL_DISABLED)
5372 continue;
5373
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005374 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005375 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005376
5377 if (!not) {
5378 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005379
5380 if (entry)
5381 continue;
5382 if (add_hash_entry(hash, rec->ip) < 0)
5383 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005384 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005385 if (entry) {
5386 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005387 fail = 0;
5388 }
5389 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005390 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005391 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005392out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005393 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005394
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005395 if (fail)
5396 return -EINVAL;
5397
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005398 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005399}
5400
5401static ssize_t
5402ftrace_graph_write(struct file *file, const char __user *ubuf,
5403 size_t cnt, loff_t *ppos)
5404{
Namhyung Kim6a101082013-10-14 17:24:25 +09005405 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005406 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005407 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005408
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005409 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005410 return 0;
5411
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005412 /* Read mode uses seq functions */
5413 if (file->f_mode & FMODE_READ) {
5414 struct seq_file *m = file->private_data;
5415 fgd = m->private;
5416 }
5417
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005418 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005419
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005420 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005421
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005422 if (read >= 0 && trace_parser_loaded(parser) &&
5423 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005424
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005425 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005426 parser->buffer);
5427 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005428 }
5429
Namhyung Kim6a101082013-10-14 17:24:25 +09005430 if (!ret)
5431 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005432
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005433 return ret;
5434}
5435
5436static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005437 .open = ftrace_graph_open,
5438 .read = seq_read,
5439 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005440 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005441 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005442};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005443
5444static const struct file_operations ftrace_graph_notrace_fops = {
5445 .open = ftrace_graph_notrace_open,
5446 .read = seq_read,
5447 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005448 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005449 .release = ftrace_graph_release,
5450};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005451#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5452
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005453void ftrace_create_filter_files(struct ftrace_ops *ops,
5454 struct dentry *parent)
5455{
5456
5457 trace_create_file("set_ftrace_filter", 0644, parent,
5458 ops, &ftrace_filter_fops);
5459
5460 trace_create_file("set_ftrace_notrace", 0644, parent,
5461 ops, &ftrace_notrace_fops);
5462}
5463
5464/*
5465 * The name "destroy_filter_files" is really a misnomer. Although
5466 * in the future, it may actualy delete the files, but this is
5467 * really intended to make sure the ops passed in are disabled
5468 * and that when this function returns, the caller is free to
5469 * free the ops.
5470 *
5471 * The "destroy" name is only to match the "create" name that this
5472 * should be paired with.
5473 */
5474void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5475{
5476 mutex_lock(&ftrace_lock);
5477 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5478 ftrace_shutdown(ops, 0);
5479 ops->flags |= FTRACE_OPS_FL_DELETED;
Steven Rostedt (VMware)2840f842018-12-10 23:58:01 -05005480 ftrace_free_filter(ops);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005481 mutex_unlock(&ftrace_lock);
5482}
5483
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005484static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005485{
Steven Rostedt5072c592008-05-12 21:20:43 +02005486
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005487 trace_create_file("available_filter_functions", 0444,
5488 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005489
Steven Rostedt647bcd02011-05-03 14:39:21 -04005490 trace_create_file("enabled_functions", 0444,
5491 d_tracer, NULL, &ftrace_enabled_fops);
5492
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005493 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005494
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005495#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Chen LinX1ce05002014-09-03 14:31:09 +08005496 trace_create_file("set_graph_function", 0644, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005497 NULL,
5498 &ftrace_graph_fops);
Chen LinX1ce05002014-09-03 14:31:09 +08005499 trace_create_file("set_graph_notrace", 0644, d_tracer,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005500 NULL,
5501 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005502#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5503
Steven Rostedt5072c592008-05-12 21:20:43 +02005504 return 0;
5505}
5506
Steven Rostedt9fd49322012-04-24 22:32:06 -04005507static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005508{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005509 const unsigned long *ipa = a;
5510 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005511
Steven Rostedt9fd49322012-04-24 22:32:06 -04005512 if (*ipa > *ipb)
5513 return 1;
5514 if (*ipa < *ipb)
5515 return -1;
5516 return 0;
5517}
5518
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005519static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005520 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005521 unsigned long *end)
5522{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005523 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005524 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005525 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005526 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005527 unsigned long *p;
5528 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005529 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005530 int ret = -ENOMEM;
5531
5532 count = end - start;
5533
5534 if (!count)
5535 return 0;
5536
Steven Rostedt9fd49322012-04-24 22:32:06 -04005537 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005538 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005539
Steven Rostedt706c81f2012-04-24 23:45:26 -04005540 start_pg = ftrace_allocate_pages(count);
5541 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005542 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005543
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005544 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005545
Steven Rostedt320823092011-12-16 14:42:37 -05005546 /*
5547 * Core and each module needs their own pages, as
5548 * modules will free them when they are removed.
5549 * Force a new page to be allocated for modules.
5550 */
Steven Rostedta7900872011-12-16 16:23:44 -05005551 if (!mod) {
5552 WARN_ON(ftrace_pages || ftrace_pages_start);
5553 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005554 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005555 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005556 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005557 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005558
Steven Rostedta7900872011-12-16 16:23:44 -05005559 if (WARN_ON(ftrace_pages->next)) {
5560 /* Hmm, we have free pages? */
5561 while (ftrace_pages->next)
5562 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005563 }
Steven Rostedta7900872011-12-16 16:23:44 -05005564
Steven Rostedt706c81f2012-04-24 23:45:26 -04005565 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005566 }
5567
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005568 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005569 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005570 while (p < end) {
5571 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005572 /*
5573 * Some architecture linkers will pad between
5574 * the different mcount_loc sections of different
5575 * object files to satisfy alignments.
5576 * Skip any NULL pointers.
5577 */
5578 if (!addr)
5579 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005580
5581 if (pg->index == pg->size) {
5582 /* We should have allocated enough */
5583 if (WARN_ON(!pg->next))
5584 break;
5585 pg = pg->next;
5586 }
5587
5588 rec = &pg->records[pg->index++];
5589 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005590 }
5591
Steven Rostedt706c81f2012-04-24 23:45:26 -04005592 /* We should have used all pages */
5593 WARN_ON(pg->next);
5594
5595 /* Assign the last page to ftrace_pages */
5596 ftrace_pages = pg;
5597
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005598 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005599 * We only need to disable interrupts on start up
5600 * because we are modifying code that an interrupt
5601 * may execute, and the modification is not atomic.
5602 * But for modules, nothing runs the code we modify
5603 * until we are finished with it, and there's no
5604 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005605 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005606 if (!mod)
5607 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005608 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005609 if (!mod)
5610 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005611 ret = 0;
5612 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005613 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005614
Steven Rostedta7900872011-12-16 16:23:44 -05005615 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005616}
5617
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005618struct ftrace_mod_func {
5619 struct list_head list;
5620 char *name;
5621 unsigned long ip;
5622 unsigned int size;
5623};
5624
5625struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005626 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005627 struct list_head list;
5628 struct module *mod;
5629 unsigned long start_addr;
5630 unsigned long end_addr;
5631 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005632 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005633};
5634
Steven Rostedt93eb6772009-04-15 13:24:06 -04005635#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005636
5637#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5638
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005639static LIST_HEAD(ftrace_mod_maps);
5640
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005641static int referenced_filters(struct dyn_ftrace *rec)
5642{
5643 struct ftrace_ops *ops;
5644 int cnt = 0;
5645
5646 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5647 if (ops_references_rec(ops, rec))
5648 cnt++;
5649 }
5650
5651 return cnt;
5652}
5653
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005654static void
5655clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5656{
5657 struct ftrace_func_entry *entry;
5658 struct dyn_ftrace *rec;
5659 int i;
5660
5661 if (ftrace_hash_empty(hash))
5662 return;
5663
5664 for (i = 0; i < pg->index; i++) {
5665 rec = &pg->records[i];
5666 entry = __ftrace_lookup_ip(hash, rec->ip);
5667 /*
5668 * Do not allow this rec to match again.
5669 * Yeah, it may waste some memory, but will be removed
5670 * if/when the hash is modified again.
5671 */
5672 if (entry)
5673 entry->ip = 0;
5674 }
5675}
5676
5677/* Clear any records from hashs */
5678static void clear_mod_from_hashes(struct ftrace_page *pg)
5679{
5680 struct trace_array *tr;
5681
5682 mutex_lock(&trace_types_lock);
5683 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5684 if (!tr->ops || !tr->ops->func_hash)
5685 continue;
5686 mutex_lock(&tr->ops->func_hash->regex_lock);
5687 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5688 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5689 mutex_unlock(&tr->ops->func_hash->regex_lock);
5690 }
5691 mutex_unlock(&trace_types_lock);
5692}
5693
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005694static void ftrace_free_mod_map(struct rcu_head *rcu)
5695{
5696 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5697 struct ftrace_mod_func *mod_func;
5698 struct ftrace_mod_func *n;
5699
5700 /* All the contents of mod_map are now not visible to readers */
5701 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5702 kfree(mod_func->name);
5703 list_del(&mod_func->list);
5704 kfree(mod_func);
5705 }
5706
5707 kfree(mod_map);
5708}
5709
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005710void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005711{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005712 struct ftrace_mod_map *mod_map;
5713 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005714 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005715 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005716 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005717 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005718 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005719
Steven Rostedt93eb6772009-04-15 13:24:06 -04005720 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005721
5722 if (ftrace_disabled)
5723 goto out_unlock;
5724
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005725 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5726 if (mod_map->mod == mod) {
5727 list_del_rcu(&mod_map->list);
Paul E. McKenney74401722018-11-06 18:44:52 -08005728 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005729 break;
5730 }
5731 }
5732
Steven Rostedt320823092011-12-16 14:42:37 -05005733 /*
5734 * Each module has its own ftrace_pages, remove
5735 * them from the list.
5736 */
5737 last_pg = &ftrace_pages_start;
5738 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5739 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005740 if (within_module_core(rec->ip, mod) ||
5741 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005742 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005743 * As core pages are first, the first
5744 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005745 */
Steven Rostedt320823092011-12-16 14:42:37 -05005746 if (WARN_ON(pg == ftrace_pages_start))
5747 goto out_unlock;
5748
5749 /* Check if we are deleting the last page */
5750 if (pg == ftrace_pages)
5751 ftrace_pages = next_to_ftrace_page(last_pg);
5752
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04005753 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05005754 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005755
5756 pg->next = tmp_page;
5757 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005758 } else
5759 last_pg = &pg->next;
5760 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005761 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005762 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005763
5764 for (pg = tmp_page; pg; pg = tmp_page) {
5765
5766 /* Needs to be called outside of ftrace_lock */
5767 clear_mod_from_hashes(pg);
5768
5769 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5770 free_pages((unsigned long)pg->records, order);
5771 tmp_page = pg->next;
5772 kfree(pg);
5773 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04005774}
5775
Jessica Yu7dcd1822016-02-16 17:32:33 -05005776void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005777{
5778 struct dyn_ftrace *rec;
5779 struct ftrace_page *pg;
5780
5781 mutex_lock(&ftrace_lock);
5782
5783 if (ftrace_disabled)
5784 goto out_unlock;
5785
5786 /*
5787 * If the tracing is enabled, go ahead and enable the record.
5788 *
5789 * The reason not to enable the record immediatelly is the
5790 * inherent check of ftrace_make_nop/ftrace_make_call for
5791 * correct previous instructions. Making first the NOP
5792 * conversion puts the module to the correct state, thus
5793 * passing the ftrace_make_call check.
5794 *
5795 * We also delay this to after the module code already set the
5796 * text to read-only, as we now need to set it back to read-write
5797 * so that we can modify the text.
5798 */
5799 if (ftrace_start_up)
5800 ftrace_arch_code_modify_prepare();
5801
5802 do_for_each_ftrace_rec(pg, rec) {
5803 int cnt;
5804 /*
5805 * do_for_each_ftrace_rec() is a double loop.
5806 * module text shares the pg. If a record is
5807 * not part of this module, then skip this pg,
5808 * which the "break" will do.
5809 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005810 if (!within_module_core(rec->ip, mod) &&
5811 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005812 break;
5813
5814 cnt = 0;
5815
5816 /*
5817 * When adding a module, we need to check if tracers are
5818 * currently enabled and if they are, and can trace this record,
5819 * we need to enable the module functions as well as update the
5820 * reference counts for those function records.
5821 */
5822 if (ftrace_start_up)
5823 cnt += referenced_filters(rec);
5824
5825 /* This clears FTRACE_FL_DISABLED */
5826 rec->flags = cnt;
5827
5828 if (ftrace_start_up && cnt) {
5829 int failed = __ftrace_replace_code(rec, 1);
5830 if (failed) {
5831 ftrace_bug(failed, rec);
5832 goto out_loop;
5833 }
5834 }
5835
5836 } while_for_each_ftrace_rec();
5837
5838 out_loop:
5839 if (ftrace_start_up)
5840 ftrace_arch_code_modify_post_process();
5841
5842 out_unlock:
5843 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04005844
5845 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005846}
5847
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005848void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005849{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005850 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005851 return;
5852
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005853 ftrace_process_locs(mod, mod->ftrace_callsites,
5854 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005855}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005856
5857static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5858 struct dyn_ftrace *rec)
5859{
5860 struct ftrace_mod_func *mod_func;
5861 unsigned long symsize;
5862 unsigned long offset;
5863 char str[KSYM_SYMBOL_LEN];
5864 char *modname;
5865 const char *ret;
5866
5867 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5868 if (!ret)
5869 return;
5870
5871 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5872 if (!mod_func)
5873 return;
5874
5875 mod_func->name = kstrdup(str, GFP_KERNEL);
5876 if (!mod_func->name) {
5877 kfree(mod_func);
5878 return;
5879 }
5880
5881 mod_func->ip = rec->ip - offset;
5882 mod_func->size = symsize;
5883
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005884 mod_map->num_funcs++;
5885
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005886 list_add_rcu(&mod_func->list, &mod_map->funcs);
5887}
5888
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005889static struct ftrace_mod_map *
5890allocate_ftrace_mod_map(struct module *mod,
5891 unsigned long start, unsigned long end)
5892{
5893 struct ftrace_mod_map *mod_map;
5894
5895 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5896 if (!mod_map)
5897 return NULL;
5898
5899 mod_map->mod = mod;
5900 mod_map->start_addr = start;
5901 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005902 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005903
5904 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5905
5906 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5907
5908 return mod_map;
5909}
5910
5911static const char *
5912ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5913 unsigned long addr, unsigned long *size,
5914 unsigned long *off, char *sym)
5915{
5916 struct ftrace_mod_func *found_func = NULL;
5917 struct ftrace_mod_func *mod_func;
5918
5919 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5920 if (addr >= mod_func->ip &&
5921 addr < mod_func->ip + mod_func->size) {
5922 found_func = mod_func;
5923 break;
5924 }
5925 }
5926
5927 if (found_func) {
5928 if (size)
5929 *size = found_func->size;
5930 if (off)
5931 *off = addr - found_func->ip;
5932 if (sym)
5933 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5934
5935 return found_func->name;
5936 }
5937
5938 return NULL;
5939}
5940
5941const char *
5942ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5943 unsigned long *off, char **modname, char *sym)
5944{
5945 struct ftrace_mod_map *mod_map;
5946 const char *ret = NULL;
5947
Paul E. McKenney74401722018-11-06 18:44:52 -08005948 /* mod_map is freed via call_rcu() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005949 preempt_disable();
5950 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5951 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
5952 if (ret) {
5953 if (modname)
5954 *modname = mod_map->mod->name;
5955 break;
5956 }
5957 }
5958 preempt_enable();
5959
5960 return ret;
5961}
5962
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005963int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
5964 char *type, char *name,
5965 char *module_name, int *exported)
5966{
5967 struct ftrace_mod_map *mod_map;
5968 struct ftrace_mod_func *mod_func;
5969
5970 preempt_disable();
5971 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5972
5973 if (symnum >= mod_map->num_funcs) {
5974 symnum -= mod_map->num_funcs;
5975 continue;
5976 }
5977
5978 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5979 if (symnum > 1) {
5980 symnum--;
5981 continue;
5982 }
5983
5984 *value = mod_func->ip;
5985 *type = 'T';
5986 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
5987 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
5988 *exported = 1;
5989 preempt_enable();
5990 return 0;
5991 }
5992 WARN_ON(1);
5993 break;
5994 }
5995 preempt_enable();
5996 return -ERANGE;
5997}
5998
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005999#else
6000static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6001 struct dyn_ftrace *rec) { }
6002static inline struct ftrace_mod_map *
6003allocate_ftrace_mod_map(struct module *mod,
6004 unsigned long start, unsigned long end)
6005{
6006 return NULL;
6007}
Steven Rostedt93eb6772009-04-15 13:24:06 -04006008#endif /* CONFIG_MODULES */
6009
Joel Fernandes8715b102017-10-09 12:29:31 -07006010struct ftrace_init_func {
6011 struct list_head list;
6012 unsigned long ip;
6013};
6014
6015/* Clear any init ips from hashes */
6016static void
6017clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006018{
Joel Fernandes8715b102017-10-09 12:29:31 -07006019 struct ftrace_func_entry *entry;
6020
6021 if (ftrace_hash_empty(hash))
6022 return;
6023
6024 entry = __ftrace_lookup_ip(hash, func->ip);
6025
6026 /*
6027 * Do not allow this rec to match again.
6028 * Yeah, it may waste some memory, but will be removed
6029 * if/when the hash is modified again.
6030 */
6031 if (entry)
6032 entry->ip = 0;
6033}
6034
6035static void
6036clear_func_from_hashes(struct ftrace_init_func *func)
6037{
6038 struct trace_array *tr;
6039
6040 mutex_lock(&trace_types_lock);
6041 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6042 if (!tr->ops || !tr->ops->func_hash)
6043 continue;
6044 mutex_lock(&tr->ops->func_hash->regex_lock);
6045 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6046 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6047 mutex_unlock(&tr->ops->func_hash->regex_lock);
6048 }
6049 mutex_unlock(&trace_types_lock);
6050}
6051
6052static void add_to_clear_hash_list(struct list_head *clear_list,
6053 struct dyn_ftrace *rec)
6054{
6055 struct ftrace_init_func *func;
6056
6057 func = kmalloc(sizeof(*func), GFP_KERNEL);
6058 if (!func) {
6059 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6060 return;
6061 }
6062
6063 func->ip = rec->ip;
6064 list_add(&func->list, clear_list);
6065}
6066
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006067void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006068{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006069 unsigned long start = (unsigned long)(start_ptr);
6070 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006071 struct ftrace_page **last_pg = &ftrace_pages_start;
6072 struct ftrace_page *pg;
6073 struct dyn_ftrace *rec;
6074 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006075 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07006076 struct ftrace_init_func *func, *func_next;
6077 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006078 int order;
6079
Joel Fernandes8715b102017-10-09 12:29:31 -07006080 INIT_LIST_HEAD(&clear_hash);
6081
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006082 key.ip = start;
6083 key.flags = end; /* overload flags, as it is unsigned long */
6084
6085 mutex_lock(&ftrace_lock);
6086
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006087 /*
6088 * If we are freeing module init memory, then check if
6089 * any tracer is active. If so, we need to save a mapping of
6090 * the module functions being freed with the address.
6091 */
6092 if (mod && ftrace_ops_list != &ftrace_list_end)
6093 mod_map = allocate_ftrace_mod_map(mod, start, end);
6094
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006095 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6096 if (end < pg->records[0].ip ||
6097 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6098 continue;
6099 again:
6100 rec = bsearch(&key, pg->records, pg->index,
6101 sizeof(struct dyn_ftrace),
6102 ftrace_cmp_recs);
6103 if (!rec)
6104 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006105
Joel Fernandes8715b102017-10-09 12:29:31 -07006106 /* rec will be cleared from hashes after ftrace_lock unlock */
6107 add_to_clear_hash_list(&clear_hash, rec);
6108
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006109 if (mod_map)
6110 save_ftrace_mod_rec(mod_map, rec);
6111
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006112 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04006113 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006114 if (!pg->index) {
6115 *last_pg = pg->next;
6116 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6117 free_pages((unsigned long)pg->records, order);
6118 kfree(pg);
6119 pg = container_of(last_pg, struct ftrace_page, next);
6120 if (!(*last_pg))
6121 ftrace_pages = pg;
6122 continue;
6123 }
6124 memmove(rec, rec + 1,
6125 (pg->index - (rec - pg->records)) * sizeof(*rec));
6126 /* More than one function may be in this block */
6127 goto again;
6128 }
6129 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07006130
6131 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6132 clear_func_from_hashes(func);
6133 kfree(func);
6134 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006135}
6136
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006137void __init ftrace_free_init_mem(void)
6138{
6139 void *start = (void *)(&__init_begin);
6140 void *end = (void *)(&__init_end);
6141
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006142 ftrace_free_mem(NULL, start, end);
Steven Rostedt93eb6772009-04-15 13:24:06 -04006143}
6144
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006145void __init ftrace_init(void)
6146{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006147 extern unsigned long __start_mcount_loc[];
6148 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006149 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006150 int ret;
6151
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006152 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006153 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006154 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01006155 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006156 goto failed;
6157
6158 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006159 if (!count) {
6160 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006161 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006162 }
6163
6164 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6165 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006166
6167 last_ftrace_enabled = ftrace_enabled = 1;
6168
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006169 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08006170 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006171 __stop_mcount_loc);
6172
Steven Rostedt2af15d62009-05-28 13:37:24 -04006173 set_ftrace_early_filters();
6174
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006175 return;
6176 failed:
6177 ftrace_disabled = 1;
6178}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006179
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006180/* Do nothing if arch does not support this */
6181void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6182{
6183}
6184
6185static void ftrace_update_trampoline(struct ftrace_ops *ops)
6186{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006187 arch_ftrace_update_trampoline(ops);
6188}
6189
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006190void ftrace_init_trace_array(struct trace_array *tr)
6191{
6192 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04006193 INIT_LIST_HEAD(&tr->mod_trace);
6194 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006195}
Steven Rostedt3d083392008-05-12 21:20:42 +02006196#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006197
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05006198struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04006199 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006200 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6201 FTRACE_OPS_FL_INITIALIZED |
6202 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04006203};
6204
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006205static int __init ftrace_nodyn_init(void)
6206{
6207 ftrace_enabled = 1;
6208 return 0;
6209}
Steven Rostedt6f415672012-10-05 12:13:07 -04006210core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006211
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006212static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006213static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006214static inline void ftrace_startup_all(int command) { }
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006215
Ingo Molnarc7aafc52008-05-12 21:20:45 +02006216# define ftrace_startup_sysctl() do { } while (0)
6217# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04006218
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006219static void ftrace_update_trampoline(struct ftrace_ops *ops)
6220{
6221}
6222
Steven Rostedt3d083392008-05-12 21:20:42 +02006223#endif /* CONFIG_DYNAMIC_FTRACE */
6224
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006225__init void ftrace_init_global_array_ops(struct trace_array *tr)
6226{
6227 tr->ops = &global_ops;
6228 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006229 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006230}
6231
6232void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6233{
6234 /* If we filter on pids, update to use the pid function */
6235 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6236 if (WARN_ON(tr->ops->func != ftrace_stub))
6237 printk("ftrace ops had %pS for function\n",
6238 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006239 }
6240 tr->ops->func = func;
6241 tr->ops->private = tr;
6242}
6243
6244void ftrace_reset_array_ops(struct trace_array *tr)
6245{
6246 tr->ops->func = ftrace_stub;
6247}
6248
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006249static inline void
6250__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006251 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006252{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006253 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006254 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04006255
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006256 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6257 if (bit < 0)
6258 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04006259
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006260 /*
6261 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08006262 * they must be freed after a synchronize_rcu().
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006263 */
6264 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006265
Steven Rostedt0a016402012-11-02 17:03:03 -04006266 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006267 /*
6268 * Check the following for each ops before calling their func:
6269 * if RCU flag is set, then rcu_is_watching() must be true
6270 * if PER_CPU is set, then ftrace_function_local_disable()
6271 * must be false
6272 * Otherwise test if the ip matches the ops filter
6273 *
6274 * If any of the above fails then the op->func() is not executed.
6275 */
6276 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006277 ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04006278 if (FTRACE_WARN_ON(!op->func)) {
6279 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006280 goto out;
6281 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04006282 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006283 }
Steven Rostedt0a016402012-11-02 17:03:03 -04006284 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006285out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006286 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006287 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04006288}
6289
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006290/*
6291 * Some archs only support passing ip and parent_ip. Even though
6292 * the list function ignores the op parameter, we do not want any
6293 * C side effects, where a function is called without the caller
6294 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006295 * Archs are to support both the regs and ftrace_ops at the same time.
6296 * If they support ftrace_ops, it is assumed they support regs.
6297 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09006298 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6299 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006300 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08006301 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006302 */
6303#if ARCH_SUPPORTS_FTRACE_OPS
6304static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006305 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006306{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006307 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006308}
6309#else
6310static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6311{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006312 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006313}
6314#endif
6315
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006316/*
6317 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006318 * recursion, needs RCU protection and/or requires per cpu handling, then
6319 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006320 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006321static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006322 struct ftrace_ops *op, struct pt_regs *regs)
6323{
6324 int bit;
6325
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006326 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6327 return;
6328
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006329 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6330 if (bit < 0)
6331 return;
6332
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006333 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006334
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006335 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006336
6337 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006338 trace_clear_recursion(bit);
6339}
6340
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006341/**
6342 * ftrace_ops_get_func - get the function a trampoline should call
6343 * @ops: the ops to get the function for
6344 *
6345 * Normally the mcount trampoline will call the ops->func, but there
6346 * are times that it should not. For example, if the ops does not
6347 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08006348 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006349 *
6350 * Returns the function that the trampoline should call for @ops.
6351 */
6352ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6353{
6354 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006355 * If the function does not handle recursion, needs to be RCU safe,
6356 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006357 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006358 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006359 ops->flags & FTRACE_OPS_FL_RCU)
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006360 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006361
6362 return ops->func;
6363}
6364
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006365static void
6366ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6367 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05006368{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006369 struct trace_array *tr = data;
6370 struct trace_pid_list *pid_list;
6371
6372 pid_list = rcu_dereference_sched(tr->function_pids);
6373
6374 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6375 trace_ignore_this_task(pid_list, next));
6376}
6377
Namhyung Kim1e104862017-04-17 11:44:28 +09006378static void
6379ftrace_pid_follow_sched_process_fork(void *data,
6380 struct task_struct *self,
6381 struct task_struct *task)
6382{
6383 struct trace_pid_list *pid_list;
6384 struct trace_array *tr = data;
6385
6386 pid_list = rcu_dereference_sched(tr->function_pids);
6387 trace_filter_add_remove_task(pid_list, self, task);
6388}
6389
6390static void
6391ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6392{
6393 struct trace_pid_list *pid_list;
6394 struct trace_array *tr = data;
6395
6396 pid_list = rcu_dereference_sched(tr->function_pids);
6397 trace_filter_add_remove_task(pid_list, NULL, task);
6398}
6399
6400void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6401{
6402 if (enable) {
6403 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6404 tr);
6405 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6406 tr);
6407 } else {
6408 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6409 tr);
6410 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6411 tr);
6412 }
6413}
6414
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006415static void clear_ftrace_pids(struct trace_array *tr)
6416{
6417 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05006418 int cpu;
6419
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006420 pid_list = rcu_dereference_protected(tr->function_pids,
6421 lockdep_is_held(&ftrace_lock));
6422 if (!pid_list)
6423 return;
6424
6425 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6426
6427 for_each_possible_cpu(cpu)
6428 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6429
6430 rcu_assign_pointer(tr->function_pids, NULL);
6431
6432 /* Wait till all users are no longer using pid filtering */
Paul E. McKenney74401722018-11-06 18:44:52 -08006433 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006434
6435 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05006436}
6437
Namhyung Kimd879d0b2017-04-17 11:44:27 +09006438void ftrace_clear_pids(struct trace_array *tr)
6439{
6440 mutex_lock(&ftrace_lock);
6441
6442 clear_ftrace_pids(tr);
6443
6444 mutex_unlock(&ftrace_lock);
6445}
6446
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006447static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05006448{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006449 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006450 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006451
6452 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006453 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006454
6455 mutex_unlock(&ftrace_lock);
6456}
6457
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006458/* Greater than any max PID */
6459#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6460
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006461static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006462 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006463{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006464 struct trace_pid_list *pid_list;
6465 struct trace_array *tr = m->private;
6466
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006467 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006468 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006469
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006470 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006471
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006472 if (!pid_list)
6473 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6474
6475 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006476}
6477
6478static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6479{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006480 struct trace_array *tr = m->private;
6481 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6482
6483 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006484 return NULL;
6485
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006486 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006487}
6488
6489static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006490 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006491{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006492 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006493 mutex_unlock(&ftrace_lock);
6494}
6495
6496static int fpid_show(struct seq_file *m, void *v)
6497{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006498 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006499 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006500 return 0;
6501 }
6502
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006503 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006504}
6505
6506static const struct seq_operations ftrace_pid_sops = {
6507 .start = fpid_start,
6508 .next = fpid_next,
6509 .stop = fpid_stop,
6510 .show = fpid_show,
6511};
6512
6513static int
6514ftrace_pid_open(struct inode *inode, struct file *file)
6515{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006516 struct trace_array *tr = inode->i_private;
6517 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006518 int ret = 0;
6519
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006520 if (trace_array_get(tr) < 0)
6521 return -ENODEV;
6522
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006523 if ((file->f_mode & FMODE_WRITE) &&
6524 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006525 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006526
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006527 ret = seq_open(file, &ftrace_pid_sops);
6528 if (ret < 0) {
6529 trace_array_put(tr);
6530 } else {
6531 m = file->private_data;
6532 /* copy tr over to seq ops */
6533 m->private = tr;
6534 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006535
6536 return ret;
6537}
6538
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006539static void ignore_task_cpu(void *data)
6540{
6541 struct trace_array *tr = data;
6542 struct trace_pid_list *pid_list;
6543
6544 /*
6545 * This function is called by on_each_cpu() while the
6546 * event_mutex is held.
6547 */
6548 pid_list = rcu_dereference_protected(tr->function_pids,
6549 mutex_is_locked(&ftrace_lock));
6550
6551 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6552 trace_ignore_this_task(pid_list, current));
6553}
6554
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006555static ssize_t
6556ftrace_pid_write(struct file *filp, const char __user *ubuf,
6557 size_t cnt, loff_t *ppos)
6558{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006559 struct seq_file *m = filp->private_data;
6560 struct trace_array *tr = m->private;
6561 struct trace_pid_list *filtered_pids = NULL;
6562 struct trace_pid_list *pid_list;
6563 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006564
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006565 if (!cnt)
6566 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006567
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006568 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006569
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006570 filtered_pids = rcu_dereference_protected(tr->function_pids,
6571 lockdep_is_held(&ftrace_lock));
6572
6573 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6574 if (ret < 0)
6575 goto out;
6576
6577 rcu_assign_pointer(tr->function_pids, pid_list);
6578
6579 if (filtered_pids) {
Paul E. McKenney74401722018-11-06 18:44:52 -08006580 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006581 trace_free_pid_list(filtered_pids);
6582 } else if (pid_list) {
6583 /* Register a probe to set whether to ignore the tracing of a task */
6584 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6585 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006586
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006587 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006588 * Ignoring of pids is done at task switch. But we have to
6589 * check for those tasks that are currently running.
6590 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006591 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006592 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006593
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006594 ftrace_update_pid_func();
6595 ftrace_startup_all(0);
6596 out:
6597 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006598
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006599 if (ret > 0)
6600 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05006601
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006602 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006603}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006604
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006605static int
6606ftrace_pid_release(struct inode *inode, struct file *file)
6607{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006608 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006609
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006610 trace_array_put(tr);
6611
6612 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006613}
6614
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006615static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006616 .open = ftrace_pid_open,
6617 .write = ftrace_pid_write,
6618 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006619 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006620 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006621};
6622
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006623void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006624{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006625 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006626 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006627}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006628
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04006629void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6630 struct dentry *d_tracer)
6631{
6632 /* Only the top level directory has the dyn_tracefs and profile */
6633 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6634
6635 ftrace_init_dyn_tracefs(d_tracer);
6636 ftrace_profile_tracefs(d_tracer);
6637}
6638
Steven Rostedt3d083392008-05-12 21:20:42 +02006639/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006640 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006641 *
6642 * This function should be used by panic code. It stops ftrace
6643 * but in a not so nice way. If you need to simply kill ftrace
6644 * from a non-atomic section, use ftrace_kill.
6645 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006646void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006647{
6648 ftrace_disabled = 1;
6649 ftrace_enabled = 0;
Yisheng Xie5ccba642018-02-02 10:14:49 +08006650 ftrace_trace_function = ftrace_stub;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006651}
6652
6653/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04006654 * Test if ftrace is dead or not.
6655 */
6656int ftrace_is_dead(void)
6657{
6658 return ftrace_disabled;
6659}
6660
6661/**
Steven Rostedt3d083392008-05-12 21:20:42 +02006662 * register_ftrace_function - register a function for profiling
6663 * @ops - ops structure that holds the function for profiling.
6664 *
6665 * Register a function to be called by all functions in the
6666 * kernel.
6667 *
6668 * Note: @ops->func and all the functions it calls must be labeled
6669 * with "notrace", otherwise it will go into a
6670 * recursive loop.
6671 */
6672int register_ftrace_function(struct ftrace_ops *ops)
6673{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006674 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006675
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09006676 ftrace_ops_init(ops);
6677
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006678 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006679
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006680 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04006681
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006682 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02006683
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006684 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02006685}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006686EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02006687
6688/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01006689 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02006690 * @ops - ops structure that holds the function to unregister
6691 *
6692 * Unregister a function that was added to be called by ftrace profiling.
6693 */
6694int unregister_ftrace_function(struct ftrace_ops *ops)
6695{
6696 int ret;
6697
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006698 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006699 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006700 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006701
6702 return ret;
6703}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006704EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006705
Ingo Molnare309b412008-05-12 21:20:51 +02006706int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006707ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006708 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006709 loff_t *ppos)
6710{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006711 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006712
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006713 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006714
Steven Rostedt45a4a232011-04-21 23:16:46 -04006715 if (unlikely(ftrace_disabled))
6716 goto out;
6717
6718 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006719
Li Zefana32c7762009-06-26 16:55:51 +08006720 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006721 goto out;
6722
Li Zefana32c7762009-06-26 16:55:51 +08006723 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006724
6725 if (ftrace_enabled) {
6726
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006727 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08006728 if (rcu_dereference_protected(ftrace_ops_list,
6729 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01006730 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006731
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006732 ftrace_startup_sysctl();
6733
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006734 } else {
6735 /* stopping ftrace calls (just send to ftrace_stub) */
6736 ftrace_trace_function = ftrace_stub;
6737
6738 ftrace_shutdown_sysctl();
6739 }
6740
6741 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006742 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006743 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006744}