blob: e77a6c92620f37e14c38cc0b4f77589d12130f66 [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>
Masami Hiramatsufabe38a2019-02-24 01:50:20 +090036#include <linux/kprobes.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020037
Steven Rostedtad8d75f2009-04-14 19:39:12 -040038#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040039
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -040040#include <asm/sections.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040041#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053042
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050043#include "ftrace_internal.h"
Steven Rostedt0706f1c2009-03-23 23:12:58 -040044#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040045#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020046
Steven Rostedt6912896e2008-10-23 09:33:03 -040047#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ({ \
49 int ___r = cond; \
50 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040051 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ___r; \
53 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040054
55#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ({ \
57 int ___r = cond; \
58 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040059 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040060 ___r; \
61 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040062
Steven Rostedt8fc0c702009-02-16 15:28:00 -050063/* hash bits for specific function selection */
64#define FTRACE_HASH_BITS 7
65#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040066#define FTRACE_HASH_DEFAULT_BITS 10
67#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050068
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090069#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040070#define INIT_OPS_HASH(opsname) \
71 .func_hash = &opsname.local_hash, \
72 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090073#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040074#define INIT_OPS_HASH(opsname)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090075#endif
76
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -050077enum {
78 FTRACE_MODIFY_ENABLE_FL = (1 << 0),
79 FTRACE_MODIFY_MAY_SLEEP_FL = (1 << 1),
80};
81
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -050082struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040083 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040084 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040085 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040086};
87
Steven Rostedt4eebcc82008-05-12 21:20:48 +020088/* ftrace_enabled is a method to turn ftrace on or off */
89int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020090static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020091
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040092/* Current function tracing op */
93struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050094/* What to set function_trace_op to */
95static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050096
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040097static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040098{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040099 struct trace_array *tr;
100
101 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
102 return false;
103
104 tr = ops->private;
105
106 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400107}
108
109static void ftrace_update_trampoline(struct ftrace_ops *ops);
110
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200111/*
112 * ftrace_disabled is set when an anomaly is discovered.
113 * ftrace_disabled is much stronger than ftrace_enabled.
114 */
115static int ftrace_disabled __read_mostly;
116
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500117DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200118
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500119struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200120ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500121struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200122
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400123#if ARCH_SUPPORTS_FTRACE_OPS
124static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400125 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400126#else
127/* See comment below, where ftrace_ops_list_func is defined */
128static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
129#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
130#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400131
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900132static inline void ftrace_ops_init(struct ftrace_ops *ops)
133{
134#ifdef CONFIG_DYNAMIC_FTRACE
135 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400136 mutex_init(&ops->local_hash.regex_lock);
137 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900138 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
139 }
140#endif
141}
142
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400143static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400144 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500145{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400146 struct trace_array *tr = op->private;
147
148 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500149 return;
150
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400151 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500152}
153
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500154static void ftrace_sync(struct work_struct *work)
155{
156 /*
157 * This function is just a stub to implement a hard force
Paul E. McKenney74401722018-11-06 18:44:52 -0800158 * of synchronize_rcu(). This requires synchronizing
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500159 * tasks even in userspace and idle.
160 *
161 * Yes, function tracing is rude.
162 */
163}
164
165static void ftrace_sync_ipi(void *data)
166{
167 /* Probably not needed, but do it anyway */
168 smp_rmb();
169}
170
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100171static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
172{
173 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500174 * 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 +0100175 * then it needs to call the list anyway.
176 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +0200177 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
178 FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100179 return ftrace_ops_list_func;
180
181 return ftrace_ops_get_func(ops);
182}
183
Steven Rostedt2b499382011-05-03 22:49:52 -0400184static void update_ftrace_function(void)
185{
186 ftrace_func_t func;
187
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400188 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400189 * Prepare the ftrace_ops that the arch callback will use.
190 * If there's only one ftrace_ops registered, the ftrace_ops_list
191 * will point to the ops we want.
192 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800193 set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
194 lockdep_is_held(&ftrace_lock));
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400195
196 /* If there's no ftrace_ops registered, just call the stub function */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800197 if (set_function_trace_op == &ftrace_list_end) {
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400198 func = ftrace_stub;
199
200 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400201 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400202 * recursion safe and not dynamic and the arch supports passing ops,
203 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400204 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800205 } else if (rcu_dereference_protected(ftrace_ops_list->next,
206 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100207 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400208
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400209 } else {
210 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500211 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400212 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400213 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400214
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400215 update_function_graph_func();
216
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500217 /* If there's no change, then do nothing more here */
218 if (ftrace_trace_function == func)
219 return;
220
221 /*
222 * If we are using the list function, it doesn't care
223 * about the function_trace_ops.
224 */
225 if (func == ftrace_ops_list_func) {
226 ftrace_trace_function = func;
227 /*
228 * Don't even bother setting function_trace_ops,
229 * it would be racy to do so anyway.
230 */
231 return;
232 }
233
234#ifndef CONFIG_DYNAMIC_FTRACE
235 /*
236 * For static tracing, we need to be a bit more careful.
237 * The function change takes affect immediately. Thus,
238 * we need to coorditate the setting of the function_trace_ops
239 * with the setting of the ftrace_trace_function.
240 *
241 * Set the function to the list ops, which will call the
242 * function we want, albeit indirectly, but it handles the
243 * ftrace_ops and doesn't depend on function_trace_op.
244 */
245 ftrace_trace_function = ftrace_ops_list_func;
246 /*
247 * Make sure all CPUs see this. Yes this is slow, but static
248 * tracing is slow and nasty to have enabled.
249 */
250 schedule_on_each_cpu(ftrace_sync);
251 /* Now all cpus are using the list ops. */
252 function_trace_op = set_function_trace_op;
253 /* Make sure the function_trace_op is visible on all CPUs */
254 smp_wmb();
255 /* Nasty way to force a rmb on all cpus */
256 smp_call_function(ftrace_sync_ipi, NULL, 1);
257 /* OK, we are all set to update the ftrace_trace_function now! */
258#endif /* !CONFIG_DYNAMIC_FTRACE */
259
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400260 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400261}
262
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800263static void add_ftrace_ops(struct ftrace_ops __rcu **list,
264 struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200265{
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800266 rcu_assign_pointer(ops->next, *list);
267
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200268 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400269 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200270 * CPU might be walking that list. We need to make sure
271 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400272 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200273 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400274 rcu_assign_pointer(*list, ops);
275}
Steven Rostedt3d083392008-05-12 21:20:42 +0200276
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800277static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
278 struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400279{
280 struct ftrace_ops **p;
281
282 /*
283 * If we are removing the last function, then simply point
284 * to the ftrace_stub.
285 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +0800286 if (rcu_dereference_protected(*list,
287 lockdep_is_held(&ftrace_lock)) == ops &&
288 rcu_dereference_protected(ops->next,
289 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt2b499382011-05-03 22:49:52 -0400290 *list = &ftrace_list_end;
291 return 0;
292 }
293
294 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
295 if (*p == ops)
296 break;
297
298 if (*p != ops)
299 return -1;
300
301 *p = (*p)->next;
302 return 0;
303}
304
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400305static void ftrace_update_trampoline(struct ftrace_ops *ops);
306
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500307int __register_ftrace_function(struct ftrace_ops *ops)
Steven Rostedt2b499382011-05-03 22:49:52 -0400308{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500309 if (ops->flags & FTRACE_OPS_FL_DELETED)
310 return -EINVAL;
311
Steven Rostedtb8489142011-05-04 09:27:52 -0400312 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
313 return -EBUSY;
314
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900315#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400316 /*
317 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
318 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
319 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
320 */
321 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
322 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
323 return -EINVAL;
324
325 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
326 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
327#endif
328
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400329 if (!core_kernel_data((unsigned long)ops))
330 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
331
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500332 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400333
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400334 /* Always save the function, and reset at unregistering */
335 ops->saved_func = ops->func;
336
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400337 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400338 ops->func = ftrace_pid_func;
339
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400340 ftrace_update_trampoline(ops);
341
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400342 if (ftrace_enabled)
343 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200344
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200345 return 0;
346}
347
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -0500348int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200349{
Steven Rostedt2b499382011-05-03 22:49:52 -0400350 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200351
Steven Rostedtb8489142011-05-04 09:27:52 -0400352 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
353 return -EBUSY;
354
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500355 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400356
Steven Rostedt2b499382011-05-03 22:49:52 -0400357 if (ret < 0)
358 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400359
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400360 if (ftrace_enabled)
361 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200362
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400363 ops->func = ops->saved_func;
364
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500365 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200366}
367
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500368static void ftrace_update_pid_func(void)
369{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400370 struct ftrace_ops *op;
371
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400372 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500373 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900374 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500375
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400376 do_for_each_ftrace_op(op, ftrace_ops_list) {
377 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400378 op->func = ftrace_pids_enabled(op) ?
379 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400380 ftrace_update_trampoline(op);
381 }
382 } while_for_each_ftrace_op(op);
383
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400384 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500385}
386
Steven Rostedt493762f2009-03-23 17:12:36 -0400387#ifdef CONFIG_FUNCTION_PROFILER
388struct ftrace_profile {
389 struct hlist_node node;
390 unsigned long ip;
391 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400392#ifdef CONFIG_FUNCTION_GRAPH_TRACER
393 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400394 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400395#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400396};
397
398struct ftrace_profile_page {
399 struct ftrace_profile_page *next;
400 unsigned long index;
401 struct ftrace_profile records[];
402};
403
Steven Rostedtcafb1682009-03-24 20:50:39 -0400404struct ftrace_profile_stat {
405 atomic_t disabled;
406 struct hlist_head *hash;
407 struct ftrace_profile_page *pages;
408 struct ftrace_profile_page *start;
409 struct tracer_stat stat;
410};
411
Steven Rostedt493762f2009-03-23 17:12:36 -0400412#define PROFILE_RECORDS_SIZE \
413 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
414
415#define PROFILES_PER_PAGE \
416 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
417
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400418static int ftrace_profile_enabled __read_mostly;
419
420/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400421static DEFINE_MUTEX(ftrace_profile_lock);
422
Steven Rostedtcafb1682009-03-24 20:50:39 -0400423static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400424
Namhyung Kim20079eb2013-04-10 08:55:50 +0900425#define FTRACE_PROFILE_HASH_BITS 10
426#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400427
Steven Rostedt493762f2009-03-23 17:12:36 -0400428static void *
429function_stat_next(void *v, int idx)
430{
431 struct ftrace_profile *rec = v;
432 struct ftrace_profile_page *pg;
433
434 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
435
436 again:
Li Zefan0296e422009-06-26 11:15:37 +0800437 if (idx != 0)
438 rec++;
439
Steven Rostedt493762f2009-03-23 17:12:36 -0400440 if ((void *)rec >= (void *)&pg->records[pg->index]) {
441 pg = pg->next;
442 if (!pg)
443 return NULL;
444 rec = &pg->records[0];
445 if (!rec->counter)
446 goto again;
447 }
448
449 return rec;
450}
451
452static void *function_stat_start(struct tracer_stat *trace)
453{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400454 struct ftrace_profile_stat *stat =
455 container_of(trace, struct ftrace_profile_stat, stat);
456
457 if (!stat || !stat->start)
458 return NULL;
459
460 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400461}
462
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400463#ifdef CONFIG_FUNCTION_GRAPH_TRACER
464/* function graph compares on total time */
465static int function_stat_cmp(void *p1, void *p2)
466{
467 struct ftrace_profile *a = p1;
468 struct ftrace_profile *b = p2;
469
470 if (a->time < b->time)
471 return -1;
472 if (a->time > b->time)
473 return 1;
474 else
475 return 0;
476}
477#else
478/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400479static int function_stat_cmp(void *p1, void *p2)
480{
481 struct ftrace_profile *a = p1;
482 struct ftrace_profile *b = p2;
483
484 if (a->counter < b->counter)
485 return -1;
486 if (a->counter > b->counter)
487 return 1;
488 else
489 return 0;
490}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400491#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400492
493static int function_stat_headers(struct seq_file *m)
494{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400495#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100496 seq_puts(m, " Function "
497 "Hit Time Avg s^2\n"
498 " -------- "
499 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400500#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100501 seq_puts(m, " Function Hit\n"
502 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400503#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400504 return 0;
505}
506
507static int function_stat_show(struct seq_file *m, void *v)
508{
509 struct ftrace_profile *rec = v;
510 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800511 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400512#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400513 static struct trace_seq s;
514 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400515 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400516#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800517 mutex_lock(&ftrace_profile_lock);
518
519 /* we raced with function_profile_reset() */
520 if (unlikely(rec->counter == 0)) {
521 ret = -EBUSY;
522 goto out;
523 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400524
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530525#ifdef CONFIG_FUNCTION_GRAPH_TRACER
526 avg = rec->time;
527 do_div(avg, rec->counter);
528 if (tracing_thresh && (avg < tracing_thresh))
529 goto out;
530#endif
531
Steven Rostedt493762f2009-03-23 17:12:36 -0400532 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400533 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400534
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400535#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100536 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400537
Chase Douglase330b3b2010-04-26 14:02:05 -0400538 /* Sample standard deviation (s^2) */
539 if (rec->counter <= 1)
540 stddev = 0;
541 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200542 /*
543 * Apply Welford's method:
544 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
545 */
546 stddev = rec->counter * rec->time_squared -
547 rec->time * rec->time;
548
Chase Douglase330b3b2010-04-26 14:02:05 -0400549 /*
550 * Divide only 1000 for ns^2 -> us^2 conversion.
551 * trace_print_graph_duration will divide 1000 again.
552 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200553 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400554 }
555
Steven Rostedt34886c82009-03-25 21:00:47 -0400556 trace_seq_init(&s);
557 trace_print_graph_duration(rec->time, &s);
558 trace_seq_puts(&s, " ");
559 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400560 trace_seq_puts(&s, " ");
561 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400562 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400563#endif
564 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800565out:
566 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400567
Li Zefan3aaba202010-08-23 16:50:12 +0800568 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400569}
570
Steven Rostedtcafb1682009-03-24 20:50:39 -0400571static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400572{
573 struct ftrace_profile_page *pg;
574
Steven Rostedtcafb1682009-03-24 20:50:39 -0400575 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400576
577 while (pg) {
578 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
579 pg->index = 0;
580 pg = pg->next;
581 }
582
Steven Rostedtcafb1682009-03-24 20:50:39 -0400583 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400584 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
585}
586
Steven Rostedtcafb1682009-03-24 20:50:39 -0400587int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400588{
589 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400590 int functions;
591 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400592 int i;
593
594 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400595 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400596 return 0;
597
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
599 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400600 return -ENOMEM;
601
Steven Rostedt318e0a72009-03-25 20:06:34 -0400602#ifdef CONFIG_DYNAMIC_FTRACE
603 functions = ftrace_update_tot_cnt;
604#else
605 /*
606 * We do not know the number of functions that exist because
607 * dynamic tracing is what counts them. With past experience
608 * we have around 20K functions. That should be more than enough.
609 * It is highly unlikely we will execute every function in
610 * the kernel.
611 */
612 functions = 20000;
613#endif
614
Steven Rostedtcafb1682009-03-24 20:50:39 -0400615 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400616
Steven Rostedt318e0a72009-03-25 20:06:34 -0400617 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
618
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900619 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400620 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400621 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400622 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 pg = pg->next;
624 }
625
626 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400627
628 out_free:
629 pg = stat->start;
630 while (pg) {
631 unsigned long tmp = (unsigned long)pg;
632
633 pg = pg->next;
634 free_page(tmp);
635 }
636
Steven Rostedt318e0a72009-03-25 20:06:34 -0400637 stat->pages = NULL;
638 stat->start = NULL;
639
640 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400641}
642
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400644{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400645 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400646 int size;
647
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648 stat = &per_cpu(ftrace_profile_stats, cpu);
649
650 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400651 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400653 return 0;
654 }
655
656 /*
657 * We are profiling all functions, but usually only a few thousand
658 * functions are hit. We'll make a hash of 1024 items.
659 */
660 size = FTRACE_PROFILE_HASH_SIZE;
661
Kees Cook6396bb22018-06-12 14:03:40 -0700662 stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400663
Steven Rostedtcafb1682009-03-24 20:50:39 -0400664 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400665 return -ENOMEM;
666
Steven Rostedt318e0a72009-03-25 20:06:34 -0400667 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400668 if (ftrace_profile_pages_init(stat) < 0) {
669 kfree(stat->hash);
670 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400671 return -ENOMEM;
672 }
673
674 return 0;
675}
676
Steven Rostedtcafb1682009-03-24 20:50:39 -0400677static int ftrace_profile_init(void)
678{
679 int cpu;
680 int ret = 0;
681
Miao Xiec4602c12013-12-16 15:20:01 +0800682 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400683 ret = ftrace_profile_init_cpu(cpu);
684 if (ret)
685 break;
686 }
687
688 return ret;
689}
690
Steven Rostedt493762f2009-03-23 17:12:36 -0400691/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692static struct ftrace_profile *
693ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400694{
695 struct ftrace_profile *rec;
696 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400697 unsigned long key;
698
Namhyung Kim20079eb2013-04-10 08:55:50 +0900699 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400700 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400701
702 if (hlist_empty(hhd))
703 return NULL;
704
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400705 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400706 if (rec->ip == ip)
707 return rec;
708 }
709
710 return NULL;
711}
712
Steven Rostedtcafb1682009-03-24 20:50:39 -0400713static void ftrace_add_profile(struct ftrace_profile_stat *stat,
714 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400715{
716 unsigned long key;
717
Namhyung Kim20079eb2013-04-10 08:55:50 +0900718 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400720}
721
Steven Rostedt318e0a72009-03-25 20:06:34 -0400722/*
723 * The memory is already allocated, this simply finds a new record to use.
724 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400725static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400726ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400727{
728 struct ftrace_profile *rec = NULL;
729
Steven Rostedt318e0a72009-03-25 20:06:34 -0400730 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400731 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400732 goto out;
733
Steven Rostedt493762f2009-03-23 17:12:36 -0400734 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400735 * Try to find the function again since an NMI
736 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400737 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400738 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400739 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400740 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400741
Steven Rostedtcafb1682009-03-24 20:50:39 -0400742 if (stat->pages->index == PROFILES_PER_PAGE) {
743 if (!stat->pages->next)
744 goto out;
745 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400746 }
747
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400751
Steven Rostedt493762f2009-03-23 17:12:36 -0400752 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400753 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400754
755 return rec;
756}
757
Steven Rostedt493762f2009-03-23 17:12:36 -0400758static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400759function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400760 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400761{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400763 struct ftrace_profile *rec;
764 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400765
766 if (!ftrace_profile_enabled)
767 return;
768
Steven Rostedt493762f2009-03-23 17:12:36 -0400769 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770
Christoph Lameterbdffd892014-04-29 14:17:40 -0500771 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400772 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773 goto out;
774
775 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400776 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400777 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400778 if (!rec)
779 goto out;
780 }
781
782 rec->counter++;
783 out:
784 local_irq_restore(flags);
785}
786
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400787#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt (VMware)e73e6792018-11-15 12:35:13 -0500788static bool fgraph_graph_time = true;
789
790void ftrace_graph_graph_time_control(bool enable)
791{
792 fgraph_graph_time = enable;
793}
794
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400795static int profile_graph_entry(struct ftrace_graph_ent *trace)
796{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500797 struct ftrace_ret_stack *ret_stack;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900798
Steven Rostedta1e2e312011-08-09 12:50:46 -0400799 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900800
Steven Rostedt (VMware)a8f0f9e2017-08-17 16:37:25 -0400801 /* If function graph is shutting down, ret_stack can be NULL */
802 if (!current->ret_stack)
803 return 0;
804
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500805 ret_stack = ftrace_graph_get_ret_stack(current, 0);
806 if (ret_stack)
807 ret_stack->subtime = 0;
Namhyung Kim8861dd32016-08-31 11:55:29 +0900808
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400809 return 1;
810}
811
812static void profile_graph_return(struct ftrace_graph_ret *trace)
813{
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500814 struct ftrace_ret_stack *ret_stack;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400816 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400817 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400818 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400819
820 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500821 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400822 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400823 goto out;
824
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400825 /* If the calltime was zero'd ignore it */
826 if (!trace->calltime)
827 goto out;
828
Steven Rostedta2a16d62009-03-24 23:17:58 -0400829 calltime = trace->rettime - trace->calltime;
830
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400831 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400832
833 /* Append this call time to the parent time to subtract */
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500834 ret_stack = ftrace_graph_get_ret_stack(current, 1);
835 if (ret_stack)
836 ret_stack->subtime += calltime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400837
Steven Rostedt (VMware)b0e21a62018-11-19 20:54:08 -0500838 ret_stack = ftrace_graph_get_ret_stack(current, 0);
839 if (ret_stack && ret_stack->subtime < calltime)
840 calltime -= ret_stack->subtime;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400841 else
842 calltime = 0;
843 }
844
Steven Rostedtcafb1682009-03-24 20:50:39 -0400845 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400846 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400847 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400848 rec->time_squared += calltime * calltime;
849 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400850
Steven Rostedtcafb1682009-03-24 20:50:39 -0400851 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400852 local_irq_restore(flags);
853}
854
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500855static struct fgraph_ops fprofiler_ops = {
856 .entryfunc = &profile_graph_entry,
857 .retfunc = &profile_graph_return,
858};
859
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400860static int register_ftrace_profiler(void)
861{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500862 return register_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400863}
864
865static void unregister_ftrace_profiler(void)
866{
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500867 unregister_ftrace_graph(&fprofiler_ops);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400868}
869#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100870static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400871 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900872 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400873 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400874};
875
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400876static int register_ftrace_profiler(void)
877{
878 return register_ftrace_function(&ftrace_profile_ops);
879}
880
881static void unregister_ftrace_profiler(void)
882{
883 unregister_ftrace_function(&ftrace_profile_ops);
884}
885#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
886
Steven Rostedt493762f2009-03-23 17:12:36 -0400887static ssize_t
888ftrace_profile_write(struct file *filp, const char __user *ubuf,
889 size_t cnt, loff_t *ppos)
890{
891 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400892 int ret;
893
Peter Huewe22fe9b52011-06-07 21:58:27 +0200894 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
895 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400896 return ret;
897
898 val = !!val;
899
900 mutex_lock(&ftrace_profile_lock);
901 if (ftrace_profile_enabled ^ val) {
902 if (val) {
903 ret = ftrace_profile_init();
904 if (ret < 0) {
905 cnt = ret;
906 goto out;
907 }
908
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400909 ret = register_ftrace_profiler();
910 if (ret < 0) {
911 cnt = ret;
912 goto out;
913 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400914 ftrace_profile_enabled = 1;
915 } else {
916 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400917 /*
918 * unregister_ftrace_profiler calls stop_machine
Paul E. McKenney74401722018-11-06 18:44:52 -0800919 * so this acts like an synchronize_rcu.
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400920 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400921 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400922 }
923 }
924 out:
925 mutex_unlock(&ftrace_profile_lock);
926
Jiri Olsacf8517c2009-10-23 19:36:16 -0400927 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400928
929 return cnt;
930}
931
932static ssize_t
933ftrace_profile_read(struct file *filp, char __user *ubuf,
934 size_t cnt, loff_t *ppos)
935{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400936 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400937 int r;
938
939 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
940 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
941}
942
943static const struct file_operations ftrace_profile_fops = {
944 .open = tracing_open_generic,
945 .read = ftrace_profile_read,
946 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200947 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400948};
949
Steven Rostedtcafb1682009-03-24 20:50:39 -0400950/* used to initialize the real stat files */
951static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400952 .name = "functions",
953 .stat_start = function_stat_start,
954 .stat_next = function_stat_next,
955 .stat_cmp = function_stat_cmp,
956 .stat_headers = function_stat_headers,
957 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400958};
959
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500960static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400961{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400962 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400963 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400964 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400965 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400966 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400967
Steven Rostedtcafb1682009-03-24 20:50:39 -0400968 for_each_possible_cpu(cpu) {
969 stat = &per_cpu(ftrace_profile_stats, cpu);
970
Geliang Tang6363c6b2016-03-15 22:12:34 +0800971 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400972 if (!name) {
973 /*
974 * The files created are permanent, if something happens
975 * we still do not free memory.
976 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400977 WARN(1,
978 "Could not allocate stat file for cpu %d\n",
979 cpu);
980 return;
981 }
982 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400983 stat->stat.name = name;
984 ret = register_stat_tracer(&stat->stat);
985 if (ret) {
986 WARN(1,
987 "Could not register function stat for cpu %d\n",
988 cpu);
989 kfree(name);
990 return;
991 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400992 }
993
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500994 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -0400995 d_tracer, NULL, &ftrace_profile_fops);
996 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -0700997 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -0400998}
999
1000#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001001static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001002{
1003}
1004#endif /* CONFIG_FUNCTION_PROFILER */
1005
Steven Rostedt3d083392008-05-12 21:20:42 +02001006#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001007
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001008static struct ftrace_ops *removed_ops;
1009
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001010/*
1011 * Set when doing a global update, like enabling all recs or disabling them.
1012 * It is not set when just updating a single ftrace_ops.
1013 */
1014static bool update_all_ops;
1015
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001016#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001017# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001018#endif
1019
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001020struct ftrace_func_entry {
1021 struct hlist_node hlist;
1022 unsigned long ip;
1023};
1024
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001025struct ftrace_func_probe {
1026 struct ftrace_probe_ops *probe_ops;
1027 struct ftrace_ops ops;
1028 struct trace_array *tr;
1029 struct list_head list;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04001030 void *data;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04001031 int ref;
1032};
1033
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001034/*
1035 * We make these constant because no one should touch them,
1036 * but they are used as the default "empty hash", to avoid allocating
1037 * it all the time. These are in a read only section such that if
1038 * anyone does try to modify it, it will cause an exception.
1039 */
1040static const struct hlist_head empty_buckets[1];
1041static const struct ftrace_hash empty_hash = {
1042 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001043};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001044#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001045
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001046struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001047 .func = ftrace_stub,
1048 .local_hash.notrace_hash = EMPTY_HASH,
1049 .local_hash.filter_hash = EMPTY_HASH,
1050 INIT_OPS_HASH(global_ops)
1051 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001052 FTRACE_OPS_FL_INITIALIZED |
1053 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001054};
1055
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001056/*
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001057 * Used by the stack undwinder to know about dynamic ftrace trampolines.
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001058 */
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001059struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001060{
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001061 struct ftrace_ops *op = NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001062
1063 /*
1064 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08001065 * they are freed after a synchronize_rcu().
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001066 */
1067 preempt_disable_notrace();
1068
1069 do_for_each_ftrace_op(op, ftrace_ops_list) {
1070 /*
1071 * This is to check for dynamically allocated trampolines.
1072 * Trampolines that are in kernel text will have
1073 * core_kernel_text() return true.
1074 */
1075 if (op->trampoline && op->trampoline_size)
1076 if (addr >= op->trampoline &&
1077 addr < op->trampoline + op->trampoline_size) {
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001078 preempt_enable_notrace();
1079 return op;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001080 }
1081 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001082 preempt_enable_notrace();
1083
Steven Rostedt (VMware)6be7fa32018-01-22 22:32:51 -05001084 return NULL;
1085}
1086
1087/*
1088 * This is used by __kernel_text_address() to return true if the
1089 * address is on a dynamically allocated trampoline that would
1090 * not return true for either core_kernel_text() or
1091 * is_module_text_address().
1092 */
1093bool is_ftrace_trampoline(unsigned long addr)
1094{
1095 return ftrace_ops_trampoline(addr) != NULL;
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001096}
1097
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001098struct ftrace_page {
1099 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001100 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001101 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001102 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001103};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001104
Steven Rostedta7900872011-12-16 16:23:44 -05001105#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1106#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001107
1108/* estimate from running different kernels */
1109#define NR_TO_INIT 10000
1110
1111static struct ftrace_page *ftrace_pages_start;
1112static struct ftrace_page *ftrace_pages;
1113
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001114static __always_inline unsigned long
1115ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1116{
1117 if (hash->size_bits > 0)
1118 return hash_long(ip, hash->size_bits);
1119
1120 return 0;
1121}
1122
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001123/* Only use this function if ftrace_hash_empty() has already been tested */
1124static __always_inline struct ftrace_func_entry *
1125__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001126{
1127 unsigned long key;
1128 struct ftrace_func_entry *entry;
1129 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001130
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001131 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001132 hhd = &hash->buckets[key];
1133
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001134 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001135 if (entry->ip == ip)
1136 return entry;
1137 }
1138 return NULL;
1139}
1140
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001141/**
1142 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1143 * @hash: The hash to look at
1144 * @ip: The instruction pointer to test
1145 *
1146 * Search a given @hash to see if a given instruction pointer (@ip)
1147 * exists in it.
1148 *
1149 * Returns the entry that holds the @ip if found. NULL otherwise.
1150 */
1151struct ftrace_func_entry *
1152ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1153{
1154 if (ftrace_hash_empty(hash))
1155 return NULL;
1156
1157 return __ftrace_lookup_ip(hash, ip);
1158}
1159
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001160static void __add_hash_entry(struct ftrace_hash *hash,
1161 struct ftrace_func_entry *entry)
1162{
1163 struct hlist_head *hhd;
1164 unsigned long key;
1165
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001166 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001167 hhd = &hash->buckets[key];
1168 hlist_add_head(&entry->hlist, hhd);
1169 hash->count++;
1170}
1171
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001172static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1173{
1174 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001175
1176 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1177 if (!entry)
1178 return -ENOMEM;
1179
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001180 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001181 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001182
1183 return 0;
1184}
1185
1186static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001187free_hash_entry(struct ftrace_hash *hash,
1188 struct ftrace_func_entry *entry)
1189{
1190 hlist_del(&entry->hlist);
1191 kfree(entry);
1192 hash->count--;
1193}
1194
1195static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001196remove_hash_entry(struct ftrace_hash *hash,
1197 struct ftrace_func_entry *entry)
1198{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001199 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001200 hash->count--;
1201}
1202
1203static void ftrace_hash_clear(struct ftrace_hash *hash)
1204{
1205 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001206 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001207 struct ftrace_func_entry *entry;
1208 int size = 1 << hash->size_bits;
1209 int i;
1210
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001211 if (!hash->count)
1212 return;
1213
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001214 for (i = 0; i < size; i++) {
1215 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001216 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001217 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001218 }
1219 FTRACE_WARN_ON(hash->count);
1220}
1221
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001222static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1223{
1224 list_del(&ftrace_mod->list);
1225 kfree(ftrace_mod->module);
1226 kfree(ftrace_mod->func);
1227 kfree(ftrace_mod);
1228}
1229
1230static void clear_ftrace_mod_list(struct list_head *head)
1231{
1232 struct ftrace_mod_load *p, *n;
1233
1234 /* stack tracer isn't supported yet */
1235 if (!head)
1236 return;
1237
1238 mutex_lock(&ftrace_lock);
1239 list_for_each_entry_safe(p, n, head, list)
1240 free_ftrace_mod(p);
1241 mutex_unlock(&ftrace_lock);
1242}
1243
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001244static void free_ftrace_hash(struct ftrace_hash *hash)
1245{
1246 if (!hash || hash == EMPTY_HASH)
1247 return;
1248 ftrace_hash_clear(hash);
1249 kfree(hash->buckets);
1250 kfree(hash);
1251}
1252
Steven Rostedt07fd5512011-05-05 18:03:47 -04001253static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1254{
1255 struct ftrace_hash *hash;
1256
1257 hash = container_of(rcu, struct ftrace_hash, rcu);
1258 free_ftrace_hash(hash);
1259}
1260
1261static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1262{
1263 if (!hash || hash == EMPTY_HASH)
1264 return;
Paul E. McKenney74401722018-11-06 18:44:52 -08001265 call_rcu(&hash->rcu, __free_ftrace_hash_rcu);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001266}
1267
Jiri Olsa5500fa52012-02-15 15:51:54 +01001268void ftrace_free_filter(struct ftrace_ops *ops)
1269{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001270 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001271 free_ftrace_hash(ops->func_hash->filter_hash);
1272 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001273}
1274
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001275static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1276{
1277 struct ftrace_hash *hash;
1278 int size;
1279
1280 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1281 if (!hash)
1282 return NULL;
1283
1284 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001285 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001286
1287 if (!hash->buckets) {
1288 kfree(hash);
1289 return NULL;
1290 }
1291
1292 hash->size_bits = size_bits;
1293
1294 return hash;
1295}
1296
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04001297
1298static int ftrace_add_mod(struct trace_array *tr,
1299 const char *func, const char *module,
1300 int enable)
1301{
1302 struct ftrace_mod_load *ftrace_mod;
1303 struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1304
1305 ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1306 if (!ftrace_mod)
1307 return -ENOMEM;
1308
1309 ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1310 ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1311 ftrace_mod->enable = enable;
1312
1313 if (!ftrace_mod->func || !ftrace_mod->module)
1314 goto out_free;
1315
1316 list_add(&ftrace_mod->list, mod_head);
1317
1318 return 0;
1319
1320 out_free:
1321 free_ftrace_mod(ftrace_mod);
1322
1323 return -ENOMEM;
1324}
1325
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326static struct ftrace_hash *
1327alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1328{
1329 struct ftrace_func_entry *entry;
1330 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001331 int size;
1332 int ret;
1333 int i;
1334
1335 new_hash = alloc_ftrace_hash(size_bits);
1336 if (!new_hash)
1337 return NULL;
1338
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001339 if (hash)
1340 new_hash->flags = hash->flags;
1341
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001343 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001344 return new_hash;
1345
1346 size = 1 << hash->size_bits;
1347 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001348 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001349 ret = add_hash_entry(new_hash, entry->ip);
1350 if (ret < 0)
1351 goto free_hash;
1352 }
1353 }
1354
1355 FTRACE_WARN_ON(new_hash->count != hash->count);
1356
1357 return new_hash;
1358
1359 free_hash:
1360 free_ftrace_hash(new_hash);
1361 return NULL;
1362}
1363
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001364static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001365ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001366static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001367ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001368
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001369static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1370 struct ftrace_hash *new_hash);
1371
Namhyung Kim3e278c02017-01-20 11:44:45 +09001372static struct ftrace_hash *
1373__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001374{
1375 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001376 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001377 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001378 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001379 int size = src->count;
1380 int bits = 0;
1381 int i;
1382
1383 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001384 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385 */
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001386 if (ftrace_hash_empty(src))
Namhyung Kim3e278c02017-01-20 11:44:45 +09001387 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001388
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001389 /*
1390 * Make the hash size about 1/2 the # found
1391 */
1392 for (size /= 2; size; size >>= 1)
1393 bits++;
1394
1395 /* Don't allocate too much */
1396 if (bits > FTRACE_HASH_MAX_BITS)
1397 bits = FTRACE_HASH_MAX_BITS;
1398
Steven Rostedt07fd5512011-05-05 18:03:47 -04001399 new_hash = alloc_ftrace_hash(bits);
1400 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001401 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001402
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001403 new_hash->flags = src->flags;
1404
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001405 size = 1 << src->size_bits;
1406 for (i = 0; i < size; i++) {
1407 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001408 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001409 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001410 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001411 }
1412 }
1413
Namhyung Kim3e278c02017-01-20 11:44:45 +09001414 return new_hash;
1415}
1416
1417static int
1418ftrace_hash_move(struct ftrace_ops *ops, int enable,
1419 struct ftrace_hash **dst, struct ftrace_hash *src)
1420{
1421 struct ftrace_hash *new_hash;
1422 int ret;
1423
1424 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1425 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1426 return -EINVAL;
1427
1428 new_hash = __ftrace_hash_move(src);
1429 if (!new_hash)
1430 return -ENOMEM;
1431
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001432 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1433 if (enable) {
1434 /* IPMODIFY should be updated only when filter_hash updating */
1435 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1436 if (ret < 0) {
1437 free_ftrace_hash(new_hash);
1438 return ret;
1439 }
1440 }
1441
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001442 /*
1443 * Remove the current set, update the hash and add
1444 * them back.
1445 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001446 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001447
Steven Rostedt07fd5512011-05-05 18:03:47 -04001448 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001449
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001450 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001451
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001452 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001453}
1454
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001455static bool hash_contains_ip(unsigned long ip,
1456 struct ftrace_ops_hash *hash)
1457{
1458 /*
1459 * The function record is a match if it exists in the filter
1460 * hash and not in the notrace hash. Note, an emty hash is
1461 * considered a match for the filter hash, but an empty
1462 * notrace hash is considered not in the notrace hash.
1463 */
1464 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001465 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001466 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001467 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001468}
1469
Steven Rostedt265c8312009-02-13 12:43:56 -05001470/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001471 * Test the hashes for this ops to see if we want to call
1472 * the ops->func or not.
1473 *
1474 * It's a match if the ip is in the ops->filter_hash or
1475 * the filter_hash does not exist or is empty,
1476 * AND
1477 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001478 *
1479 * This needs to be called with preemption disabled as
Paul E. McKenney74401722018-11-06 18:44:52 -08001480 * the hashes are freed with call_rcu().
Steven Rostedtb8489142011-05-04 09:27:52 -04001481 */
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05001482int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001483ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001484{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001485 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001486 int ret;
1487
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001488#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1489 /*
1490 * There's a small race when adding ops that the ftrace handler
1491 * that wants regs, may be called without them. We can not
1492 * allow that handler to be called if regs is NULL.
1493 */
1494 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1495 return 0;
1496#endif
1497
Chunyan Zhangf86f4182017-06-07 16:12:51 +08001498 rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1499 rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001500
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001501 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001502 ret = 1;
1503 else
1504 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001505
1506 return ret;
1507}
1508
1509/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001510 * This is a double for. Do not use 'break' to break out of the loop,
1511 * you must use a goto.
1512 */
1513#define do_for_each_ftrace_rec(pg, rec) \
1514 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1515 int _____i; \
1516 for (_____i = 0; _____i < pg->index; _____i++) { \
1517 rec = &pg->records[_____i];
1518
1519#define while_for_each_ftrace_rec() \
1520 } \
1521 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301522
Steven Rostedt5855fea2011-12-16 19:27:42 -05001523
1524static int ftrace_cmp_recs(const void *a, const void *b)
1525{
Steven Rostedta650e022012-04-25 13:48:13 -04001526 const struct dyn_ftrace *key = a;
1527 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001528
Steven Rostedta650e022012-04-25 13:48:13 -04001529 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001530 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001531 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1532 return 1;
1533 return 0;
1534}
1535
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001536/**
1537 * ftrace_location_range - return the first address of a traced location
1538 * if it touches the given ip range
1539 * @start: start of range to search.
1540 * @end: end of range to search (inclusive). @end points to the last byte
1541 * to check.
1542 *
1543 * Returns rec->ip if the related ftrace location is a least partly within
1544 * the given address range. That is, the first address of the instruction
1545 * that is either a NOP or call to the function tracer. It checks the ftrace
1546 * internal tables to determine if the address belongs or not.
1547 */
1548unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001549{
1550 struct ftrace_page *pg;
1551 struct dyn_ftrace *rec;
1552 struct dyn_ftrace key;
1553
1554 key.ip = start;
1555 key.flags = end; /* overload flags, as it is unsigned long */
1556
1557 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1558 if (end < pg->records[0].ip ||
1559 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1560 continue;
1561 rec = bsearch(&key, pg->records, pg->index,
1562 sizeof(struct dyn_ftrace),
1563 ftrace_cmp_recs);
1564 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001565 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001566 }
1567
Steven Rostedt5855fea2011-12-16 19:27:42 -05001568 return 0;
1569}
1570
Steven Rostedtc88fd862011-08-16 09:53:39 -04001571/**
1572 * ftrace_location - return true if the ip giving is a traced location
1573 * @ip: the instruction pointer to check
1574 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001575 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001576 * That is, the instruction that is either a NOP or call to
1577 * the function tracer. It checks the ftrace internal tables to
1578 * determine if the address belongs or not.
1579 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001580unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001581{
Steven Rostedta650e022012-04-25 13:48:13 -04001582 return ftrace_location_range(ip, ip);
1583}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001584
Steven Rostedta650e022012-04-25 13:48:13 -04001585/**
1586 * ftrace_text_reserved - return true if range contains an ftrace location
1587 * @start: start of range to search
1588 * @end: end of range to search (inclusive). @end points to the last byte to check.
1589 *
1590 * Returns 1 if @start and @end contains a ftrace location.
1591 * That is, the instruction that is either a NOP or call to
1592 * the function tracer. It checks the ftrace internal tables to
1593 * determine if the address belongs or not.
1594 */
Sasha Levind88471c2013-01-09 18:09:20 -05001595int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001596{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001597 unsigned long ret;
1598
1599 ret = ftrace_location_range((unsigned long)start,
1600 (unsigned long)end);
1601
1602 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001603}
1604
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001605/* Test if ops registered to this rec needs regs */
1606static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1607{
1608 struct ftrace_ops *ops;
1609 bool keep_regs = false;
1610
1611 for (ops = ftrace_ops_list;
1612 ops != &ftrace_list_end; ops = ops->next) {
1613 /* pass rec in as regs to have non-NULL val */
1614 if (ftrace_ops_test(ops, rec->ip, rec)) {
1615 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1616 keep_regs = true;
1617 break;
1618 }
1619 }
1620 }
1621
1622 return keep_regs;
1623}
1624
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001625static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001626 int filter_hash,
1627 bool inc)
1628{
1629 struct ftrace_hash *hash;
1630 struct ftrace_hash *other_hash;
1631 struct ftrace_page *pg;
1632 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001633 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001634 int count = 0;
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001635 int all = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001636
1637 /* Only update if the ops has been registered */
1638 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001639 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001640
1641 /*
1642 * In the filter_hash case:
1643 * If the count is zero, we update all records.
1644 * Otherwise we just update the items in the hash.
1645 *
1646 * In the notrace_hash case:
1647 * We enable the update in the hash.
1648 * As disabling notrace means enabling the tracing,
1649 * and enabling notrace means disabling, the inc variable
1650 * gets inversed.
1651 */
1652 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001653 hash = ops->func_hash->filter_hash;
1654 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001655 if (ftrace_hash_empty(hash))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04001656 all = true;
Steven Rostedted926f92011-05-03 13:25:24 -04001657 } else {
1658 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001659 hash = ops->func_hash->notrace_hash;
1660 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001661 /*
1662 * If the notrace hash has no items,
1663 * then there's nothing to do.
1664 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001665 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001666 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001667 }
1668
1669 do_for_each_ftrace_rec(pg, rec) {
1670 int in_other_hash = 0;
1671 int in_hash = 0;
1672 int match = 0;
1673
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001674 if (rec->flags & FTRACE_FL_DISABLED)
1675 continue;
1676
Steven Rostedted926f92011-05-03 13:25:24 -04001677 if (all) {
1678 /*
1679 * Only the filter_hash affects all records.
1680 * Update if the record is not in the notrace hash.
1681 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001682 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001683 match = 1;
1684 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001685 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1686 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001687
1688 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001689 * If filter_hash is set, we want to match all functions
1690 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001691 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001692 * If filter_hash is not set, then we are decrementing.
1693 * That means we match anything that is in the hash
1694 * and also in the other_hash. That is, we need to turn
1695 * off functions in the other hash because they are disabled
1696 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001697 */
1698 if (filter_hash && in_hash && !in_other_hash)
1699 match = 1;
1700 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001701 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001702 match = 1;
1703 }
1704 if (!match)
1705 continue;
1706
1707 if (inc) {
1708 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001709 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001710 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001711
1712 /*
1713 * If there's only a single callback registered to a
1714 * function, and the ops has a trampoline registered
1715 * for it, then we can call it directly.
1716 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001717 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001718 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001719 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001720 /*
1721 * If we are adding another function callback
1722 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001723 * custom trampoline in use, then we need to go
1724 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001725 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001726 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001727
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001728 /*
1729 * If any ops wants regs saved for this function
1730 * then all ops will get saved regs.
1731 */
1732 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1733 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001734 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001735 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001736 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001737 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001738
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001739 /*
1740 * If the rec had REGS enabled and the ops that is
1741 * being removed had REGS set, then see if there is
1742 * still any ops for this record that wants regs.
1743 * If not, we can stop recording them.
1744 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001745 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001746 rec->flags & FTRACE_FL_REGS &&
1747 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1748 if (!test_rec_ops_needs_regs(rec))
1749 rec->flags &= ~FTRACE_FL_REGS;
1750 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001751
1752 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001753 * If the rec had TRAMP enabled, then it needs to
1754 * be cleared. As TRAMP can only be enabled iff
1755 * there is only a single ops attached to it.
1756 * In otherwords, always disable it on decrementing.
1757 * In the future, we may set it if rec count is
1758 * decremented to one, and the ops that is left
1759 * has a trampoline.
1760 */
1761 rec->flags &= ~FTRACE_FL_TRAMP;
1762
1763 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001764 * flags will be cleared in ftrace_check_record()
1765 * if rec count is zero.
1766 */
Steven Rostedted926f92011-05-03 13:25:24 -04001767 }
1768 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001769
1770 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1771 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1772
Steven Rostedted926f92011-05-03 13:25:24 -04001773 /* Shortcut, if we handled all records, we are done. */
1774 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001775 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001776 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001777
1778 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001779}
1780
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001781static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001782 int filter_hash)
1783{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001784 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001785}
1786
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001787static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001788 int filter_hash)
1789{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001790 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001791}
1792
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001793static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1794 int filter_hash, int inc)
1795{
1796 struct ftrace_ops *op;
1797
1798 __ftrace_hash_rec_update(ops, filter_hash, inc);
1799
1800 if (ops->func_hash != &global_ops.local_hash)
1801 return;
1802
1803 /*
1804 * If the ops shares the global_ops hash, then we need to update
1805 * all ops that are enabled and use this hash.
1806 */
1807 do_for_each_ftrace_op(op, ftrace_ops_list) {
1808 /* Already done */
1809 if (op == ops)
1810 continue;
1811 if (op->func_hash == &global_ops.local_hash)
1812 __ftrace_hash_rec_update(op, filter_hash, inc);
1813 } while_for_each_ftrace_op(op);
1814}
1815
1816static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1817 int filter_hash)
1818{
1819 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1820}
1821
1822static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1823 int filter_hash)
1824{
1825 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1826}
1827
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001828/*
1829 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1830 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1831 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1832 * Note that old_hash and new_hash has below meanings
1833 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1834 * - If the hash is EMPTY_HASH, it hits nothing
1835 * - Anything else hits the recs which match the hash entries.
1836 */
1837static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1838 struct ftrace_hash *old_hash,
1839 struct ftrace_hash *new_hash)
1840{
1841 struct ftrace_page *pg;
1842 struct dyn_ftrace *rec, *end = NULL;
1843 int in_old, in_new;
1844
1845 /* Only update if the ops has been registered */
1846 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1847 return 0;
1848
1849 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1850 return 0;
1851
1852 /*
1853 * Since the IPMODIFY is a very address sensitive action, we do not
1854 * allow ftrace_ops to set all functions to new hash.
1855 */
1856 if (!new_hash || !old_hash)
1857 return -EINVAL;
1858
1859 /* Update rec->flags */
1860 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001861
1862 if (rec->flags & FTRACE_FL_DISABLED)
1863 continue;
1864
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001865 /* We need to update only differences of filter_hash */
1866 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1867 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1868 if (in_old == in_new)
1869 continue;
1870
1871 if (in_new) {
1872 /* New entries must ensure no others are using it */
1873 if (rec->flags & FTRACE_FL_IPMODIFY)
1874 goto rollback;
1875 rec->flags |= FTRACE_FL_IPMODIFY;
1876 } else /* Removed entry */
1877 rec->flags &= ~FTRACE_FL_IPMODIFY;
1878 } while_for_each_ftrace_rec();
1879
1880 return 0;
1881
1882rollback:
1883 end = rec;
1884
1885 /* Roll back what we did above */
1886 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001887
1888 if (rec->flags & FTRACE_FL_DISABLED)
1889 continue;
1890
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001891 if (rec == end)
1892 goto err_out;
1893
1894 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1895 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1896 if (in_old == in_new)
1897 continue;
1898
1899 if (in_new)
1900 rec->flags &= ~FTRACE_FL_IPMODIFY;
1901 else
1902 rec->flags |= FTRACE_FL_IPMODIFY;
1903 } while_for_each_ftrace_rec();
1904
1905err_out:
1906 return -EBUSY;
1907}
1908
1909static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1910{
1911 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1912
1913 if (ftrace_hash_empty(hash))
1914 hash = NULL;
1915
1916 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1917}
1918
1919/* Disabling always succeeds */
1920static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1921{
1922 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1923
1924 if (ftrace_hash_empty(hash))
1925 hash = NULL;
1926
1927 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1928}
1929
1930static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1931 struct ftrace_hash *new_hash)
1932{
1933 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1934
1935 if (ftrace_hash_empty(old_hash))
1936 old_hash = NULL;
1937
1938 if (ftrace_hash_empty(new_hash))
1939 new_hash = NULL;
1940
1941 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1942}
1943
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001944static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001945{
1946 int i;
1947
1948 printk(KERN_CONT "%s", fmt);
1949
1950 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1951 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1952}
1953
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001954static struct ftrace_ops *
1955ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001956static struct ftrace_ops *
1957ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001958
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001959enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001960const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001961
1962static void print_bug_type(void)
1963{
1964 switch (ftrace_bug_type) {
1965 case FTRACE_BUG_UNKNOWN:
1966 break;
1967 case FTRACE_BUG_INIT:
1968 pr_info("Initializing ftrace call sites\n");
1969 break;
1970 case FTRACE_BUG_NOP:
1971 pr_info("Setting ftrace call site to NOP\n");
1972 break;
1973 case FTRACE_BUG_CALL:
1974 pr_info("Setting ftrace call site to call ftrace function\n");
1975 break;
1976 case FTRACE_BUG_UPDATE:
1977 pr_info("Updating ftrace call site to call a different ftrace function\n");
1978 break;
1979 }
1980}
1981
Steven Rostedtc88fd862011-08-16 09:53:39 -04001982/**
1983 * ftrace_bug - report and shutdown function tracer
1984 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001985 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001986 *
1987 * The arch code that enables or disables the function tracing
1988 * can call ftrace_bug() when it has detected a problem in
1989 * modifying the code. @failed should be one of either:
1990 * EFAULT - if the problem happens on reading the @ip address
1991 * EINVAL - if what is read at @ip is not what was expected
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05301992 * EPERM - if the problem happens on writing to the @ip address
Steven Rostedtc88fd862011-08-16 09:53:39 -04001993 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001994void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001995{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001996 unsigned long ip = rec ? rec->ip : 0;
1997
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001998 switch (failed) {
1999 case -EFAULT:
2000 FTRACE_WARN_ON_ONCE(1);
2001 pr_info("ftrace faulted on modifying ");
2002 print_ip_sym(ip);
2003 break;
2004 case -EINVAL:
2005 FTRACE_WARN_ON_ONCE(1);
2006 pr_info("ftrace failed to modify ");
2007 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002008 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002009 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002010 if (ftrace_expected) {
2011 print_ip_ins(" expected: ", ftrace_expected);
2012 pr_cont("\n");
2013 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002014 break;
2015 case -EPERM:
2016 FTRACE_WARN_ON_ONCE(1);
2017 pr_info("ftrace faulted on writing ");
2018 print_ip_sym(ip);
2019 break;
2020 default:
2021 FTRACE_WARN_ON_ONCE(1);
2022 pr_info("ftrace faulted on unknown error ");
2023 print_ip_sym(ip);
2024 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002025 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002026 if (rec) {
2027 struct ftrace_ops *ops = NULL;
2028
2029 pr_info("ftrace record flags: %lx\n", rec->flags);
2030 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2031 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2032 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2033 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002034 if (ops) {
2035 do {
2036 pr_cont("\ttramp: %pS (%pS)",
2037 (void *)ops->trampoline,
2038 (void *)ops->func);
2039 ops = ftrace_find_tramp_ops_next(rec, ops);
2040 } while (ops);
2041 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002042 pr_cont("\ttramp: ERROR!");
2043
2044 }
2045 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002046 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002047 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002048}
2049
Steven Rostedtc88fd862011-08-16 09:53:39 -04002050static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002051{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002052 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002053
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002054 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2055
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002056 if (rec->flags & FTRACE_FL_DISABLED)
2057 return FTRACE_UPDATE_IGNORE;
2058
Steven Rostedt982c3502008-11-15 16:31:41 -05002059 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002060 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002061 *
Steven Rostedted926f92011-05-03 13:25:24 -04002062 * If the record has a ref count, then we need to enable it
2063 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002064 *
Steven Rostedted926f92011-05-03 13:25:24 -04002065 * Otherwise we make sure its disabled.
2066 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002067 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002068 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002069 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002070 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002071 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002072
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002073 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002074 * If enabling and the REGS flag does not match the REGS_EN, or
2075 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2076 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002077 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002078 if (flag) {
2079 if (!(rec->flags & FTRACE_FL_REGS) !=
2080 !(rec->flags & FTRACE_FL_REGS_EN))
2081 flag |= FTRACE_FL_REGS;
2082
2083 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2084 !(rec->flags & FTRACE_FL_TRAMP_EN))
2085 flag |= FTRACE_FL_TRAMP;
2086 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002087
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002088 /* If the state of this record hasn't changed, then do nothing */
2089 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002090 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002091
2092 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002093 /* Save off if rec is being enabled (for return value) */
2094 flag ^= rec->flags & FTRACE_FL_ENABLED;
2095
2096 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002097 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002098 if (flag & FTRACE_FL_REGS) {
2099 if (rec->flags & FTRACE_FL_REGS)
2100 rec->flags |= FTRACE_FL_REGS_EN;
2101 else
2102 rec->flags &= ~FTRACE_FL_REGS_EN;
2103 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002104 if (flag & FTRACE_FL_TRAMP) {
2105 if (rec->flags & FTRACE_FL_TRAMP)
2106 rec->flags |= FTRACE_FL_TRAMP_EN;
2107 else
2108 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2109 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002110 }
2111
2112 /*
2113 * If this record is being updated from a nop, then
2114 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002115 * Otherwise,
2116 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002117 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002118 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002119 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002120 if (flag & FTRACE_FL_ENABLED) {
2121 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002122 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002123 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002124
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002125 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002126 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002127 }
2128
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002129 if (update) {
2130 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002131 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002132 rec->flags = 0;
2133 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002134 /*
2135 * Just disable the record, but keep the ops TRAMP
2136 * and REGS states. The _EN flags must be disabled though.
2137 */
2138 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2139 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002140 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002141
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002142 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002143 return FTRACE_UPDATE_MAKE_NOP;
2144}
2145
2146/**
2147 * ftrace_update_record, set a record that now is tracing or not
2148 * @rec: the record to update
2149 * @enable: set to 1 if the record is tracing, zero to force disable
2150 *
2151 * The records that represent all functions that can be traced need
2152 * to be updated when tracing has been enabled.
2153 */
2154int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2155{
2156 return ftrace_check_record(rec, enable, 1);
2157}
2158
2159/**
2160 * ftrace_test_record, check if the record has been enabled or not
2161 * @rec: the record to test
2162 * @enable: set to 1 to check if enabled, 0 if it is disabled
2163 *
2164 * The arch code may need to test if a record is already set to
2165 * tracing to determine how to modify the function code that it
2166 * represents.
2167 */
2168int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2169{
2170 return ftrace_check_record(rec, enable, 0);
2171}
2172
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002173static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002174ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2175{
2176 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002177 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002178
2179 do_for_each_ftrace_op(op, ftrace_ops_list) {
2180
2181 if (!op->trampoline)
2182 continue;
2183
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002184 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002185 return op;
2186 } while_for_each_ftrace_op(op);
2187
2188 return NULL;
2189}
2190
2191static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002192ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2193 struct ftrace_ops *op)
2194{
2195 unsigned long ip = rec->ip;
2196
2197 while_for_each_ftrace_op(op) {
2198
2199 if (!op->trampoline)
2200 continue;
2201
2202 if (hash_contains_ip(ip, op->func_hash))
2203 return op;
2204 }
2205
2206 return NULL;
2207}
2208
2209static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002210ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2211{
2212 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002213 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002214
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002215 /*
2216 * Need to check removed ops first.
2217 * If they are being removed, and this rec has a tramp,
2218 * and this rec is in the ops list, then it would be the
2219 * one with the tramp.
2220 */
2221 if (removed_ops) {
2222 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002223 return removed_ops;
2224 }
2225
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002226 /*
2227 * Need to find the current trampoline for a rec.
2228 * Now, a trampoline is only attached to a rec if there
2229 * was a single 'ops' attached to it. But this can be called
2230 * when we are adding another op to the rec or removing the
2231 * current one. Thus, if the op is being added, we can
2232 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002233 * yet.
2234 *
2235 * If an ops is being modified (hooking to different functions)
2236 * then we don't care about the new functions that are being
2237 * added, just the old ones (that are probably being removed).
2238 *
2239 * If we are adding an ops to a function that already is using
2240 * a trampoline, it needs to be removed (trampolines are only
2241 * for single ops connected), then an ops that is not being
2242 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002243 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002244 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002245
2246 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002247 continue;
2248
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002249 /*
2250 * If the ops is being added, it hasn't gotten to
2251 * the point to be removed from this tree yet.
2252 */
2253 if (op->flags & FTRACE_OPS_FL_ADDING)
2254 continue;
2255
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002256
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002257 /*
2258 * If the ops is being modified and is in the old
2259 * hash, then it is probably being removed from this
2260 * function.
2261 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002262 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2263 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002264 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002265 /*
2266 * If the ops is not being added or modified, and it's
2267 * in its normal filter hash, then this must be the one
2268 * we want!
2269 */
2270 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2271 hash_contains_ip(ip, op->func_hash))
2272 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002273
2274 } while_for_each_ftrace_op(op);
2275
2276 return NULL;
2277}
2278
2279static struct ftrace_ops *
2280ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2281{
2282 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002283 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002284
2285 do_for_each_ftrace_op(op, ftrace_ops_list) {
2286 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002287 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002288 return op;
2289 } while_for_each_ftrace_op(op);
2290
2291 return NULL;
2292}
2293
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002294/**
2295 * ftrace_get_addr_new - Get the call address to set to
2296 * @rec: The ftrace record descriptor
2297 *
2298 * If the record has the FTRACE_FL_REGS set, that means that it
2299 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2300 * is not not set, then it wants to convert to the normal callback.
2301 *
2302 * Returns the address of the trampoline to set to
2303 */
2304unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2305{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002306 struct ftrace_ops *ops;
2307
2308 /* Trampolines take precedence over regs */
2309 if (rec->flags & FTRACE_FL_TRAMP) {
2310 ops = ftrace_find_tramp_ops_new(rec);
2311 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002312 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2313 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002314 /* Ftrace is shutting down, return anything */
2315 return (unsigned long)FTRACE_ADDR;
2316 }
2317 return ops->trampoline;
2318 }
2319
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002320 if (rec->flags & FTRACE_FL_REGS)
2321 return (unsigned long)FTRACE_REGS_ADDR;
2322 else
2323 return (unsigned long)FTRACE_ADDR;
2324}
2325
2326/**
2327 * ftrace_get_addr_curr - Get the call address that is already there
2328 * @rec: The ftrace record descriptor
2329 *
2330 * The FTRACE_FL_REGS_EN is set when the record already points to
2331 * a function that saves all the regs. Basically the '_EN' version
2332 * represents the current state of the function.
2333 *
2334 * Returns the address of the trampoline that is currently being called
2335 */
2336unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2337{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002338 struct ftrace_ops *ops;
2339
2340 /* Trampolines take precedence over regs */
2341 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2342 ops = ftrace_find_tramp_ops_curr(rec);
2343 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002344 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2345 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002346 /* Ftrace is shutting down, return anything */
2347 return (unsigned long)FTRACE_ADDR;
2348 }
2349 return ops->trampoline;
2350 }
2351
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002352 if (rec->flags & FTRACE_FL_REGS_EN)
2353 return (unsigned long)FTRACE_REGS_ADDR;
2354 else
2355 return (unsigned long)FTRACE_ADDR;
2356}
2357
Steven Rostedtc88fd862011-08-16 09:53:39 -04002358static int
2359__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2360{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002361 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002362 unsigned long ftrace_addr;
2363 int ret;
2364
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002365 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002366
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002367 /* This needs to be done before we call ftrace_update_record */
2368 ftrace_old_addr = ftrace_get_addr_curr(rec);
2369
2370 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002371
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002372 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2373
Steven Rostedtc88fd862011-08-16 09:53:39 -04002374 switch (ret) {
2375 case FTRACE_UPDATE_IGNORE:
2376 return 0;
2377
2378 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002379 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002380 return ftrace_make_call(rec, ftrace_addr);
2381
2382 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002383 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002384 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002385
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002386 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002387 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002388 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002389 }
2390
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05302391 return -1; /* unknown ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002392}
2393
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002394void __weak ftrace_replace_code(int mod_flags)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002395{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002396 struct dyn_ftrace *rec;
2397 struct ftrace_page *pg;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002398 int enable = mod_flags & FTRACE_MODIFY_ENABLE_FL;
2399 int schedulable = mod_flags & FTRACE_MODIFY_MAY_SLEEP_FL;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002400 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002401
Steven Rostedt45a4a232011-04-21 23:16:46 -04002402 if (unlikely(ftrace_disabled))
2403 return;
2404
Steven Rostedt265c8312009-02-13 12:43:56 -05002405 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002406
2407 if (rec->flags & FTRACE_FL_DISABLED)
2408 continue;
2409
Steven Rostedte4f5d542012-04-27 09:13:18 -04002410 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002411 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002412 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002413 /* Stop processing */
2414 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002415 }
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002416 if (schedulable)
2417 cond_resched();
Steven Rostedt265c8312009-02-13 12:43:56 -05002418 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002419}
2420
Steven Rostedtc88fd862011-08-16 09:53:39 -04002421struct ftrace_rec_iter {
2422 struct ftrace_page *pg;
2423 int index;
2424};
2425
2426/**
2427 * ftrace_rec_iter_start, start up iterating over traced functions
2428 *
2429 * Returns an iterator handle that is used to iterate over all
2430 * the records that represent address locations where functions
2431 * are traced.
2432 *
2433 * May return NULL if no records are available.
2434 */
2435struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2436{
2437 /*
2438 * We only use a single iterator.
2439 * Protected by the ftrace_lock mutex.
2440 */
2441 static struct ftrace_rec_iter ftrace_rec_iter;
2442 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2443
2444 iter->pg = ftrace_pages_start;
2445 iter->index = 0;
2446
2447 /* Could have empty pages */
2448 while (iter->pg && !iter->pg->index)
2449 iter->pg = iter->pg->next;
2450
2451 if (!iter->pg)
2452 return NULL;
2453
2454 return iter;
2455}
2456
2457/**
2458 * ftrace_rec_iter_next, get the next record to process.
2459 * @iter: The handle to the iterator.
2460 *
2461 * Returns the next iterator after the given iterator @iter.
2462 */
2463struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2464{
2465 iter->index++;
2466
2467 if (iter->index >= iter->pg->index) {
2468 iter->pg = iter->pg->next;
2469 iter->index = 0;
2470
2471 /* Could have empty pages */
2472 while (iter->pg && !iter->pg->index)
2473 iter->pg = iter->pg->next;
2474 }
2475
2476 if (!iter->pg)
2477 return NULL;
2478
2479 return iter;
2480}
2481
2482/**
2483 * ftrace_rec_iter_record, get the record at the iterator location
2484 * @iter: The current iterator location
2485 *
2486 * Returns the record that the current @iter is at.
2487 */
2488struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2489{
2490 return &iter->pg->records[iter->index];
2491}
2492
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302493static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002494ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002495{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002496 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002497
Steven Rostedt45a4a232011-04-21 23:16:46 -04002498 if (unlikely(ftrace_disabled))
2499 return 0;
2500
Shaohua Li25aac9d2009-01-09 11:29:40 +08002501 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002502 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002503 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002504 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302505 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002506 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302507 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002508}
2509
Steven Rostedt000ab692009-02-17 13:35:06 -05002510/*
2511 * archs can override this function if they must do something
2512 * before the modifying code is performed.
2513 */
2514int __weak ftrace_arch_code_modify_prepare(void)
2515{
2516 return 0;
2517}
2518
2519/*
2520 * archs can override this function if they must do something
2521 * after the modifying code is performed.
2522 */
2523int __weak ftrace_arch_code_modify_post_process(void)
2524{
2525 return 0;
2526}
2527
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002528void ftrace_modify_all_code(int command)
2529{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002530 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002531 int mod_flags = 0;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002532 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002533
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002534 if (command & FTRACE_MAY_SLEEP)
2535 mod_flags = FTRACE_MODIFY_MAY_SLEEP_FL;
2536
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002537 /*
2538 * If the ftrace_caller calls a ftrace_ops func directly,
2539 * we need to make sure that it only traces functions it
2540 * expects to trace. When doing the switch of functions,
2541 * we need to update to the ftrace_ops_list_func first
2542 * before the transition between old and new calls are set,
2543 * as the ftrace_ops_list_func will check the ops hashes
2544 * to make sure the ops are having the right functions
2545 * traced.
2546 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002547 if (update) {
2548 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2549 if (FTRACE_WARN_ON(err))
2550 return;
2551 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002552
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002553 if (command & FTRACE_UPDATE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002554 ftrace_replace_code(mod_flags | FTRACE_MODIFY_ENABLE_FL);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002555 else if (command & FTRACE_DISABLE_CALLS)
Steven Rostedt (VMware)a0572f62018-12-05 12:48:53 -05002556 ftrace_replace_code(mod_flags);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002557
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002558 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2559 function_trace_op = set_function_trace_op;
2560 smp_wmb();
2561 /* If irqs are disabled, we are in stop machine */
2562 if (!irqs_disabled())
2563 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002564 err = ftrace_update_ftrace_func(ftrace_trace_function);
2565 if (FTRACE_WARN_ON(err))
2566 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002567 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002568
2569 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002570 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002571 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002572 err = ftrace_disable_ftrace_graph_caller();
2573 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002574}
2575
Ingo Molnare309b412008-05-12 21:20:51 +02002576static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002577{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002578 int *command = data;
2579
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002580 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002581
Steven Rostedtc88fd862011-08-16 09:53:39 -04002582 return 0;
2583}
2584
2585/**
2586 * ftrace_run_stop_machine, go back to the stop machine method
2587 * @command: The command to tell ftrace what to do
2588 *
2589 * If an arch needs to fall back to the stop machine method, the
2590 * it can call this function.
2591 */
2592void ftrace_run_stop_machine(int command)
2593{
2594 stop_machine(__ftrace_modify_code, &command, NULL);
2595}
2596
2597/**
2598 * arch_ftrace_update_code, modify the code to trace or not trace
2599 * @command: The command that needs to be done
2600 *
2601 * Archs can override this function if it does not need to
2602 * run stop_machine() to modify code.
2603 */
2604void __weak arch_ftrace_update_code(int command)
2605{
2606 ftrace_run_stop_machine(command);
2607}
2608
2609static void ftrace_run_update_code(int command)
2610{
2611 int ret;
2612
2613 ret = ftrace_arch_code_modify_prepare();
2614 FTRACE_WARN_ON(ret);
2615 if (ret)
2616 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002617
2618 /*
2619 * By default we use stop_machine() to modify the code.
2620 * But archs can do what ever they want as long as it
2621 * is safe. The stop_machine() is the safest, but also
2622 * produces the most overhead.
2623 */
2624 arch_ftrace_update_code(command);
2625
Steven Rostedt000ab692009-02-17 13:35:06 -05002626 ret = ftrace_arch_code_modify_post_process();
2627 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002628}
2629
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002630static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002631 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002632{
2633 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002634 ops->old_hash.filter_hash = old_hash->filter_hash;
2635 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002636 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002637 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002638 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002639 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2640}
2641
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002642static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002643static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002644
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002645void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2646{
2647}
2648
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002649static void ftrace_startup_enable(int command)
2650{
2651 if (saved_ftrace_func != ftrace_trace_function) {
2652 saved_ftrace_func = ftrace_trace_function;
2653 command |= FTRACE_UPDATE_TRACE_FUNC;
2654 }
2655
2656 if (!command || !ftrace_enabled)
2657 return;
2658
2659 ftrace_run_update_code(command);
2660}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002661
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002662static void ftrace_startup_all(int command)
2663{
2664 update_all_ops = true;
2665 ftrace_startup_enable(command);
2666 update_all_ops = false;
2667}
2668
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002669int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002670{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002671 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002672
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002673 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002674 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002675
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002676 ret = __register_ftrace_function(ops);
2677 if (ret)
2678 return ret;
2679
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002680 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002681
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002682 /*
2683 * Note that ftrace probes uses this to start up
2684 * and modify functions it will probe. But we still
2685 * set the ADDING flag for modification, as probes
2686 * do not have trampolines. If they add them in the
2687 * future, then the probes will need to distinguish
2688 * between adding and updating probes.
2689 */
2690 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002691
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002692 ret = ftrace_hash_ipmodify_enable(ops);
2693 if (ret < 0) {
2694 /* Rollback registration process */
2695 __unregister_ftrace_function(ops);
2696 ftrace_start_up--;
2697 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2698 return ret;
2699 }
2700
Jiri Olsa7f50d062016-03-16 15:34:33 +01002701 if (ftrace_hash_rec_enable(ops, 1))
2702 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002703
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002704 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002705
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002706 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2707
Steven Rostedta1cd6172011-05-23 15:24:25 -04002708 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002709}
2710
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05002711int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002712{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002713 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002714
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002715 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002716 return -ENODEV;
2717
2718 ret = __unregister_ftrace_function(ops);
2719 if (ret)
2720 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002721
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002722 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002723 /*
2724 * Just warn in case of unbalance, no need to kill ftrace, it's not
2725 * critical but the ftrace_call callers may be never nopped again after
2726 * further ftrace uses.
2727 */
2728 WARN_ON_ONCE(ftrace_start_up < 0);
2729
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002730 /* Disabling ipmodify never fails */
2731 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002732
2733 if (ftrace_hash_rec_disable(ops, 1))
2734 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002735
Namhyung Kima737e6d2014-06-12 23:56:12 +09002736 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002737
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002738 if (saved_ftrace_func != ftrace_trace_function) {
2739 saved_ftrace_func = ftrace_trace_function;
2740 command |= FTRACE_UPDATE_TRACE_FUNC;
2741 }
2742
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002743 if (!command || !ftrace_enabled) {
2744 /*
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002745 * If these are dynamic or per_cpu ops, they still
2746 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002747 * not currently active, we can just free them
2748 * without synchronizing all CPUs.
2749 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002750 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002751 goto free_ops;
2752
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002753 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002754 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002755
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002756 /*
2757 * If the ops uses a trampoline, then it needs to be
2758 * tested first on update.
2759 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002760 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002761 removed_ops = ops;
2762
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002763 /* The trampoline logic checks the old hashes */
2764 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2765 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2766
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002767 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002768
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002769 /*
2770 * If there's no more ops registered with ftrace, run a
2771 * sanity check to make sure all rec flags are cleared.
2772 */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08002773 if (rcu_dereference_protected(ftrace_ops_list,
2774 lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002775 struct ftrace_page *pg;
2776 struct dyn_ftrace *rec;
2777
2778 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002779 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002780 pr_warn(" %pS flags:%lx\n",
2781 (void *)rec->ip, rec->flags);
2782 } while_for_each_ftrace_rec();
2783 }
2784
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002785 ops->old_hash.filter_hash = NULL;
2786 ops->old_hash.notrace_hash = NULL;
2787
2788 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002789 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002790
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002791 /*
2792 * Dynamic ops may be freed, we must make sure that all
2793 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002794 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002795 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002796 */
Peter Zijlstrab3a88802017-10-11 09:45:32 +02002797 if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002798 /*
2799 * We need to do a hard force of sched synchronization.
2800 * This is because we use preempt_disable() to do RCU, but
2801 * the function tracers can be called where RCU is not watching
2802 * (like before user_exit()). We can not rely on the RCU
2803 * infrastructure to do the synchronization, thus we must do it
2804 * ourselves.
2805 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002806 schedule_on_each_cpu(ftrace_sync);
2807
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002808 /*
2809 * When the kernel is preeptive, tasks can be preempted
2810 * while on a ftrace trampoline. Just scheduling a task on
2811 * a CPU is not good enough to flush them. Calling
2812 * synchornize_rcu_tasks() will wait for those tasks to
2813 * execute and either schedule voluntarily or enter user space.
2814 */
2815 if (IS_ENABLED(CONFIG_PREEMPT))
2816 synchronize_rcu_tasks();
2817
Steven Rostedt (VMware)edb096e2017-09-01 12:18:28 -04002818 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002819 arch_ftrace_trampoline_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002820 }
2821
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002822 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002823}
2824
Ingo Molnare309b412008-05-12 21:20:51 +02002825static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002826{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302827 int command;
2828
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002829 if (unlikely(ftrace_disabled))
2830 return;
2831
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002832 /* Force update next time */
2833 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002834 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302835 if (ftrace_start_up) {
2836 command = FTRACE_UPDATE_CALLS;
2837 if (ftrace_graph_active)
2838 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002839 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302840 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002841}
2842
Ingo Molnare309b412008-05-12 21:20:51 +02002843static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002844{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302845 int command;
2846
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002847 if (unlikely(ftrace_disabled))
2848 return;
2849
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002850 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302851 if (ftrace_start_up) {
2852 command = FTRACE_DISABLE_CALLS;
2853 if (ftrace_graph_active)
2854 command |= FTRACE_STOP_FUNC_RET;
2855 ftrace_run_update_code(command);
2856 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002857}
2858
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002859static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002860unsigned long ftrace_update_tot_cnt;
2861
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002862static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002863{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002864 /*
2865 * Filter_hash being empty will default to trace module.
2866 * But notrace hash requires a test of individual module functions.
2867 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002868 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2869 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002870}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002871
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002872/*
2873 * Check if the current ops references the record.
2874 *
2875 * If the ops traces all functions, then it was already accounted for.
2876 * If the ops does not trace the current record function, skip it.
2877 * If the ops ignores the function via notrace filter, skip it.
2878 */
2879static inline bool
2880ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2881{
2882 /* If ops isn't enabled, ignore it */
2883 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002884 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002885
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002886 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002887 if (ops_traces_mod(ops))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002888 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002889
2890 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002891 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002892 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002893 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002894
2895 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002896 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002897 return false;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002898
Gustavo A. R. Silva44ec3ec2018-08-01 20:00:56 -05002899 return true;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002900}
2901
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002902static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002903{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002904 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002905 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002906 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002907 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002908 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002909 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002910
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002911 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002912
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002913 /*
2914 * When a module is loaded, this function is called to convert
2915 * the calls to mcount in its text to nops, and also to create
2916 * an entry in the ftrace data. Now, if ftrace is activated
2917 * after this call, but before the module sets its text to
2918 * read-only, the modification of enabling ftrace can fail if
2919 * the read-only is done while ftrace is converting the calls.
2920 * To prevent this, the module's records are set as disabled
2921 * and will be enabled after the call to set the module's text
2922 * to read-only.
2923 */
2924 if (mod)
2925 rec_flags |= FTRACE_FL_DISABLED;
2926
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002927 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302928
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002929 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002930
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002931 /* If something went wrong, bail without enabling anything */
2932 if (unlikely(ftrace_disabled))
2933 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002934
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002935 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002936 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302937
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002938 /*
2939 * Do the initial record conversion from mcount jump
2940 * to the NOP instructions.
2941 */
Vasily Gorbikcbdaeaf2019-06-05 13:11:58 +02002942 if (!__is_defined(CC_USING_NOP_MCOUNT) &&
2943 !ftrace_code_disable(mod, p))
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002944 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002945
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002946 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002947 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002948 }
2949
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002950 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002951 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002952 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002953
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002954 return 0;
2955}
2956
Steven Rostedta7900872011-12-16 16:23:44 -05002957static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002958{
Steven Rostedta7900872011-12-16 16:23:44 -05002959 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002960 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002961
Steven Rostedta7900872011-12-16 16:23:44 -05002962 if (WARN_ON(!count))
2963 return -EINVAL;
2964
2965 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002966
2967 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002968 * We want to fill as much as possible. No more than a page
2969 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002970 */
Steven Rostedta7900872011-12-16 16:23:44 -05002971 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2972 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002973
Steven Rostedta7900872011-12-16 16:23:44 -05002974 again:
2975 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2976
2977 if (!pg->records) {
2978 /* if we can't allocate this size, try something smaller */
2979 if (!order)
2980 return -ENOMEM;
2981 order >>= 1;
2982 goto again;
2983 }
2984
2985 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2986 pg->size = cnt;
2987
2988 if (cnt > count)
2989 cnt = count;
2990
2991 return cnt;
2992}
2993
2994static struct ftrace_page *
2995ftrace_allocate_pages(unsigned long num_to_init)
2996{
2997 struct ftrace_page *start_pg;
2998 struct ftrace_page *pg;
2999 int order;
3000 int cnt;
3001
3002 if (!num_to_init)
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05303003 return NULL;
Steven Rostedta7900872011-12-16 16:23:44 -05003004
3005 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3006 if (!pg)
3007 return NULL;
3008
3009 /*
3010 * Try to allocate as much as possible in one continues
3011 * location that fills in all of the space. We want to
3012 * waste as little space as possible.
3013 */
3014 for (;;) {
3015 cnt = ftrace_allocate_records(pg, num_to_init);
3016 if (cnt < 0)
3017 goto free_pages;
3018
3019 num_to_init -= cnt;
3020 if (!num_to_init)
3021 break;
3022
3023 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3024 if (!pg->next)
3025 goto free_pages;
3026
3027 pg = pg->next;
3028 }
3029
3030 return start_pg;
3031
3032 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003033 pg = start_pg;
3034 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003035 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3036 free_pages((unsigned long)pg->records, order);
3037 start_pg = pg->next;
3038 kfree(pg);
3039 pg = start_pg;
3040 }
3041 pr_info("ftrace: FAILED to allocate memory for functions\n");
3042 return NULL;
3043}
3044
Steven Rostedt5072c592008-05-12 21:20:43 +02003045#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3046
3047struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003048 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003049 loff_t func_pos;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003050 loff_t mod_pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003051 struct ftrace_page *pg;
3052 struct dyn_ftrace *func;
3053 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003054 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003055 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003056 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003057 struct ftrace_ops *ops;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003058 struct trace_array *tr;
3059 struct list_head *mod_list;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003060 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003061 int idx;
3062 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003063};
3064
Ingo Molnare309b412008-05-12 21:20:51 +02003065static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003066t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003067{
3068 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04003069 struct trace_array *tr = iter->ops->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003070 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003071 struct ftrace_hash *hash;
3072 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003073 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003074 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003075 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003076
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003077 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003078 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003079
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003080 if (!tr)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003081 return NULL;
3082
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003083 func_probes = &tr->func_probes;
3084 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003085 return NULL;
3086
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003087 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003088 next = func_probes->next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003089 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003090 }
3091
3092 if (iter->probe_entry)
3093 hnd = &iter->probe_entry->hlist;
3094
3095 hash = iter->probe->ops.func_hash->filter_hash;
3096 size = 1 << hash->size_bits;
3097
3098 retry:
3099 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003100 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003101 return NULL;
3102 next = iter->probe->list.next;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003103 iter->probe = list_entry(next, struct ftrace_func_probe, list);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003104 hash = iter->probe->ops.func_hash->filter_hash;
3105 size = 1 << hash->size_bits;
3106 iter->pidx = 0;
3107 }
3108
3109 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003110
3111 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003112 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003113 hnd = NULL;
3114 goto retry;
3115 }
3116
3117 if (!hnd)
3118 hnd = hhd->first;
3119 else {
3120 hnd = hnd->next;
3121 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003122 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003123 goto retry;
3124 }
3125 }
3126
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003127 if (WARN_ON_ONCE(!hnd))
3128 return NULL;
3129
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003130 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003131
3132 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003133}
3134
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003135static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003136{
3137 struct ftrace_iterator *iter = m->private;
3138 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003139 loff_t l;
3140
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003141 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003142 return NULL;
3143
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003144 if (iter->mod_pos > *pos)
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003145 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003146
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003147 iter->probe = NULL;
3148 iter->probe_entry = NULL;
3149 iter->pidx = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003150 for (l = 0; l <= (*pos - iter->mod_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003151 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003152 if (!p)
3153 break;
3154 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003155 if (!p)
3156 return NULL;
3157
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003158 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003159 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003160
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003161 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003162}
3163
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003164static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003165t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003166{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003167 struct ftrace_func_entry *probe_entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003168 struct ftrace_probe_ops *probe_ops;
3169 struct ftrace_func_probe *probe;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003170
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003171 probe = iter->probe;
3172 probe_entry = iter->probe_entry;
3173
3174 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003175 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003176
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003177 probe_ops = probe->probe_ops;
Steven Rostedt809dcf22009-02-16 23:06:01 -05003178
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003179 if (probe_ops->print)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04003180 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003181
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04003182 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3183 (void *)probe_ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003184
3185 return 0;
3186}
3187
3188static void *
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003189t_mod_next(struct seq_file *m, loff_t *pos)
3190{
3191 struct ftrace_iterator *iter = m->private;
3192 struct trace_array *tr = iter->tr;
3193
3194 (*pos)++;
3195 iter->pos = *pos;
3196
3197 iter->mod_list = iter->mod_list->next;
3198
3199 if (iter->mod_list == &tr->mod_trace ||
3200 iter->mod_list == &tr->mod_notrace) {
3201 iter->flags &= ~FTRACE_ITER_MOD;
3202 return NULL;
3203 }
3204
3205 iter->mod_pos = *pos;
3206
3207 return iter;
3208}
3209
3210static void *t_mod_start(struct seq_file *m, loff_t *pos)
3211{
3212 struct ftrace_iterator *iter = m->private;
3213 void *p = NULL;
3214 loff_t l;
3215
3216 if (iter->func_pos > *pos)
3217 return NULL;
3218
3219 iter->mod_pos = iter->func_pos;
3220
3221 /* probes are only available if tr is set */
3222 if (!iter->tr)
3223 return NULL;
3224
3225 for (l = 0; l <= (*pos - iter->func_pos); ) {
3226 p = t_mod_next(m, &l);
3227 if (!p)
3228 break;
3229 }
3230 if (!p) {
3231 iter->flags &= ~FTRACE_ITER_MOD;
3232 return t_probe_start(m, pos);
3233 }
3234
3235 /* Only set this if we have an item */
3236 iter->flags |= FTRACE_ITER_MOD;
3237
3238 return iter;
3239}
3240
3241static int
3242t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3243{
3244 struct ftrace_mod_load *ftrace_mod;
3245 struct trace_array *tr = iter->tr;
3246
3247 if (WARN_ON_ONCE(!iter->mod_list) ||
3248 iter->mod_list == &tr->mod_trace ||
3249 iter->mod_list == &tr->mod_notrace)
3250 return -EIO;
3251
3252 ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3253
3254 if (ftrace_mod->func)
3255 seq_printf(m, "%s", ftrace_mod->func);
3256 else
3257 seq_putc(m, '*');
3258
3259 seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3260
3261 return 0;
3262}
3263
3264static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003265t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003266{
3267 struct ftrace_iterator *iter = m->private;
3268 struct dyn_ftrace *rec = NULL;
3269
3270 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003271
Steven Rostedt5072c592008-05-12 21:20:43 +02003272 retry:
3273 if (iter->idx >= iter->pg->index) {
3274 if (iter->pg->next) {
3275 iter->pg = iter->pg->next;
3276 iter->idx = 0;
3277 goto retry;
3278 }
3279 } else {
3280 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003281 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3282 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003283
3284 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003285 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003286
Steven Rostedt5072c592008-05-12 21:20:43 +02003287 rec = NULL;
3288 goto retry;
3289 }
3290 }
3291
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003292 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003293 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003294
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003295 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003296 iter->func = rec;
3297
3298 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003299}
3300
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003301static void *
3302t_next(struct seq_file *m, void *v, loff_t *pos)
3303{
3304 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003305 loff_t l = *pos; /* t_probe_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003306 void *ret;
3307
3308 if (unlikely(ftrace_disabled))
3309 return NULL;
3310
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003311 if (iter->flags & FTRACE_ITER_PROBE)
3312 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003313
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003314 if (iter->flags & FTRACE_ITER_MOD)
3315 return t_mod_next(m, pos);
3316
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003317 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003318 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003319 (*pos)++;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003320 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003321 }
3322
3323 ret = t_func_next(m, pos);
3324
3325 if (!ret)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003326 return t_mod_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003327
3328 return ret;
3329}
3330
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003331static void reset_iter_read(struct ftrace_iterator *iter)
3332{
3333 iter->pos = 0;
3334 iter->func_pos = 0;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003335 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
Steven Rostedt5072c592008-05-12 21:20:43 +02003336}
3337
3338static void *t_start(struct seq_file *m, loff_t *pos)
3339{
3340 struct ftrace_iterator *iter = m->private;
3341 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003342 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003343
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003344 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003345
3346 if (unlikely(ftrace_disabled))
3347 return NULL;
3348
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003349 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003350 * If an lseek was done, then reset and start from beginning.
3351 */
3352 if (*pos < iter->pos)
3353 reset_iter_read(iter);
3354
3355 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003356 * For set_ftrace_filter reading, if we have the filter
3357 * off, we can short cut and just print out that all
3358 * functions are enabled.
3359 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003360 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3361 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003362 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003363 if (*pos > 0)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003364 return t_mod_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003365 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003366 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003367 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003368 return iter;
3369 }
3370
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003371 if (iter->flags & FTRACE_ITER_MOD)
3372 return t_mod_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003373
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003374 /*
3375 * Unfortunately, we need to restart at ftrace_pages_start
3376 * every time we let go of the ftrace_mutex. This is because
3377 * those pointers can change without the lock.
3378 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003379 iter->pg = ftrace_pages_start;
3380 iter->idx = 0;
3381 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003382 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003383 if (!p)
3384 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003385 }
walimis5821e1b2008-11-15 15:19:06 +08003386
Steven Rostedt69a30832011-12-19 15:21:16 -05003387 if (!p)
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003388 return t_mod_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003389
3390 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003391}
3392
3393static void t_stop(struct seq_file *m, void *p)
3394{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003395 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003396}
3397
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003398void * __weak
3399arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3400{
3401 return NULL;
3402}
3403
3404static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3405 struct dyn_ftrace *rec)
3406{
3407 void *ptr;
3408
3409 ptr = arch_ftrace_trampoline_func(ops, rec);
3410 if (ptr)
3411 seq_printf(m, " ->%pS", ptr);
3412}
3413
Steven Rostedt5072c592008-05-12 21:20:43 +02003414static int t_show(struct seq_file *m, void *v)
3415{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003416 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003417 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003418
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003419 if (iter->flags & FTRACE_ITER_PROBE)
3420 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003421
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003422 if (iter->flags & FTRACE_ITER_MOD)
3423 return t_mod_show(m, iter);
3424
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003425 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003426 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003427 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003428 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003429 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003430 return 0;
3431 }
3432
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003433 rec = iter->func;
3434
Steven Rostedt5072c592008-05-12 21:20:43 +02003435 if (!rec)
3436 return 0;
3437
Steven Rostedt647bcd02011-05-03 14:39:21 -04003438 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003439 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003440 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003441
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003442 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003443 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003444 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3445 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003446 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003447 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003448 if (ops) {
3449 do {
3450 seq_printf(m, "\ttramp: %pS (%pS)",
3451 (void *)ops->trampoline,
3452 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003453 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003454 ops = ftrace_find_tramp_ops_next(rec, ops);
3455 } while (ops);
3456 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003457 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003458 } else {
3459 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003460 }
3461 }
3462
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003463 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003464
3465 return 0;
3466}
3467
James Morris88e9d342009-09-22 16:43:43 -07003468static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003469 .start = t_start,
3470 .next = t_next,
3471 .stop = t_stop,
3472 .show = t_show,
3473};
3474
Ingo Molnare309b412008-05-12 21:20:51 +02003475static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003476ftrace_avail_open(struct inode *inode, struct file *file)
3477{
3478 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003479
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003480 if (unlikely(ftrace_disabled))
3481 return -ENODEV;
3482
Jiri Olsa50e18b92012-04-25 10:23:39 +02003483 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003484 if (!iter)
3485 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003486
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003487 iter->pg = ftrace_pages_start;
3488 iter->ops = &global_ops;
3489
3490 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003491}
3492
Steven Rostedt647bcd02011-05-03 14:39:21 -04003493static int
3494ftrace_enabled_open(struct inode *inode, struct file *file)
3495{
3496 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003497
Jiri Olsa50e18b92012-04-25 10:23:39 +02003498 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003499 if (!iter)
3500 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003501
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003502 iter->pg = ftrace_pages_start;
3503 iter->flags = FTRACE_ITER_ENABLED;
3504 iter->ops = &global_ops;
3505
3506 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003507}
3508
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003509/**
3510 * ftrace_regex_open - initialize function tracer filter files
3511 * @ops: The ftrace_ops that hold the hash filters
3512 * @flag: The type of filter to process
3513 * @inode: The inode, usually passed in to your open routine
3514 * @file: The file, usually passed in to your open routine
3515 *
3516 * ftrace_regex_open() initializes the filter files for the
3517 * @ops. Depending on @flag it may process the filter hash or
3518 * the notrace hash of @ops. With this called from the open
3519 * routine, you can use ftrace_filter_write() for the write
3520 * routine if @flag has FTRACE_ITER_FILTER set, or
3521 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003522 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003523 * release must call ftrace_regex_release().
3524 */
3525int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003526ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003527 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003528{
3529 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003530 struct ftrace_hash *hash;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003531 struct list_head *mod_head;
3532 struct trace_array *tr = ops->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003533 int ret = 0;
3534
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003535 ftrace_ops_init(ops);
3536
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003537 if (unlikely(ftrace_disabled))
3538 return -ENODEV;
3539
Steven Rostedt5072c592008-05-12 21:20:43 +02003540 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3541 if (!iter)
3542 return -ENOMEM;
3543
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003544 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3545 kfree(iter);
3546 return -ENOMEM;
3547 }
3548
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003549 iter->ops = ops;
3550 iter->flags = flag;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003551 iter->tr = tr;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003552
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003553 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003554
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003555 if (flag & FTRACE_ITER_NOTRACE) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003556 hash = ops->func_hash->notrace_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003557 mod_head = tr ? &tr->mod_notrace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003558 } else {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003559 hash = ops->func_hash->filter_hash;
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003560 mod_head = tr ? &tr->mod_trace : NULL;
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003561 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003562
Steven Rostedt (VMware)5985ea82017-06-23 16:05:11 -04003563 iter->mod_list = mod_head;
3564
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003565 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003566 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3567
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003568 if (file->f_flags & O_TRUNC) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003569 iter->hash = alloc_ftrace_hash(size_bits);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003570 clear_ftrace_mod_list(mod_head);
3571 } else {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003572 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003573 }
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003574
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003575 if (!iter->hash) {
3576 trace_parser_put(&iter->parser);
3577 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003578 ret = -ENOMEM;
3579 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003580 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003581 } else
3582 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003583
Steven Rostedt5072c592008-05-12 21:20:43 +02003584 if (file->f_mode & FMODE_READ) {
3585 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003586
3587 ret = seq_open(file, &show_ftrace_seq_ops);
3588 if (!ret) {
3589 struct seq_file *m = file->private_data;
3590 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003591 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003592 /* Failed */
3593 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003594 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003595 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003596 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003597 } else
3598 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003599
3600 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003601 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003602
3603 return ret;
3604}
3605
Steven Rostedt41c52c02008-05-22 11:46:33 -04003606static int
3607ftrace_filter_open(struct inode *inode, struct file *file)
3608{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003609 struct ftrace_ops *ops = inode->i_private;
3610
3611 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003612 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003613 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003614}
3615
3616static int
3617ftrace_notrace_open(struct inode *inode, struct file *file)
3618{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003619 struct ftrace_ops *ops = inode->i_private;
3620
3621 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003622 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003623}
3624
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003625/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3626struct ftrace_glob {
3627 char *search;
3628 unsigned len;
3629 int type;
3630};
3631
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003632/*
3633 * If symbols in an architecture don't correspond exactly to the user-visible
3634 * name of what they represent, it is possible to define this function to
3635 * perform the necessary adjustments.
3636*/
3637char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3638{
3639 return str;
3640}
3641
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003642static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003643{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003644 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003645 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003646
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003647 str = arch_ftrace_match_adjust(str, g->search);
3648
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003649 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003650 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003651 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003652 matched = 1;
3653 break;
3654 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003655 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003656 matched = 1;
3657 break;
3658 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003659 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003660 matched = 1;
3661 break;
3662 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003663 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003664 if (slen >= g->len &&
3665 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003666 matched = 1;
3667 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003668 case MATCH_GLOB:
3669 if (glob_match(g->search, str))
3670 matched = 1;
3671 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003672 }
3673
3674 return matched;
3675}
3676
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003677static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003678enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003679{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003680 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003681 int ret = 0;
3682
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003683 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003684 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003685 /* Do nothing if it doesn't exist */
3686 if (!entry)
3687 return 0;
3688
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003689 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003690 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003691 /* Do nothing if it exists */
3692 if (entry)
3693 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003694
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003695 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003696 }
3697 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003698}
3699
Steven Rostedt64e7c442009-02-13 17:08:48 -05003700static int
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003701add_rec_by_index(struct ftrace_hash *hash, struct ftrace_glob *func_g,
3702 int clear_filter)
3703{
3704 long index = simple_strtoul(func_g->search, NULL, 0);
3705 struct ftrace_page *pg;
3706 struct dyn_ftrace *rec;
3707
3708 /* The index starts at 1 */
3709 if (--index < 0)
3710 return 0;
3711
3712 do_for_each_ftrace_rec(pg, rec) {
3713 if (pg->index <= index) {
3714 index -= pg->index;
3715 /* this is a double loop, break goes to the next page */
3716 break;
3717 }
3718 rec = &pg->records[index];
3719 enter_record(hash, rec, clear_filter);
3720 return 1;
3721 } while_for_each_ftrace_rec();
3722 return 0;
3723}
3724
3725static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003726ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3727 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003728{
3729 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003730 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003731
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003732 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3733
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003734 if (mod_g) {
3735 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3736
3737 /* blank module name to match all modules */
3738 if (!mod_g->len) {
3739 /* blank module globbing: modname xor exclude_mod */
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003740 if (!exclude_mod != !modname)
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003741 goto func_match;
3742 return 0;
3743 }
3744
Steven Rostedt (VMware)77c0edd2017-05-03 11:41:44 -04003745 /*
3746 * exclude_mod is set to trace everything but the given
3747 * module. If it is set and the module matches, then
3748 * return 0. If it is not set, and the module doesn't match
3749 * also return 0. Otherwise, check the function to see if
3750 * that matches.
3751 */
3752 if (!mod_matches == !exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003753 return 0;
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003754func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003755 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003756 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003757 return 1;
3758 }
3759
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003760 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003761}
3762
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003763static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003764match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003765{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003766 struct ftrace_page *pg;
3767 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003768 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003769 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3770 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3771 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003772 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003773 int ret;
Dan Carpenter2e028c42017-07-12 10:35:57 +03003774 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003775
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003776 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003777 func_g.type = filter_parse_regex(func, len, &func_g.search,
3778 &clear_filter);
3779 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003780 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003781
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003782 if (mod) {
3783 mod_g.type = filter_parse_regex(mod, strlen(mod),
3784 &mod_g.search, &exclude_mod);
3785 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003786 }
3787
Steven Rostedt52baf112009-02-14 01:15:39 -05003788 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003789
3790 if (unlikely(ftrace_disabled))
3791 goto out_unlock;
3792
Steven Rostedt (VMware)f79b3f32019-02-11 15:00:48 -05003793 if (func_g.type == MATCH_INDEX) {
3794 found = add_rec_by_index(hash, &func_g, clear_filter);
3795 goto out_unlock;
3796 }
3797
Steven Rostedt265c8312009-02-13 12:43:56 -05003798 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003799
3800 if (rec->flags & FTRACE_FL_DISABLED)
3801 continue;
3802
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003803 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003804 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003805 if (ret < 0) {
3806 found = ret;
3807 goto out_unlock;
3808 }
Li Zefan311d16d2009-12-08 11:15:11 +08003809 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003810 }
3811 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003812 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003813 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003814
3815 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003816}
3817
Steven Rostedt64e7c442009-02-13 17:08:48 -05003818static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003819ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003820{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003821 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003822}
3823
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003824static void ftrace_ops_update_code(struct ftrace_ops *ops,
3825 struct ftrace_ops_hash *old_hash)
3826{
3827 struct ftrace_ops *op;
3828
3829 if (!ftrace_enabled)
3830 return;
3831
3832 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3833 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3834 return;
3835 }
3836
3837 /*
3838 * If this is the shared global_ops filter, then we need to
3839 * check if there is another ops that shares it, is enabled.
3840 * If so, we still need to run the modify code.
3841 */
3842 if (ops->func_hash != &global_ops.local_hash)
3843 return;
3844
3845 do_for_each_ftrace_op(op, ftrace_ops_list) {
3846 if (op->func_hash == &global_ops.local_hash &&
3847 op->flags & FTRACE_OPS_FL_ENABLED) {
3848 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3849 /* Only need to do this once */
3850 return;
3851 }
3852 } while_for_each_ftrace_op(op);
3853}
3854
3855static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3856 struct ftrace_hash **orig_hash,
3857 struct ftrace_hash *hash,
3858 int enable)
3859{
3860 struct ftrace_ops_hash old_hash_ops;
3861 struct ftrace_hash *old_hash;
3862 int ret;
3863
3864 old_hash = *orig_hash;
3865 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3866 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3867 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3868 if (!ret) {
3869 ftrace_ops_update_code(ops, &old_hash_ops);
3870 free_ftrace_hash_rcu(old_hash);
3871 }
3872 return ret;
3873}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003874
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003875static bool module_exists(const char *module)
3876{
3877 /* All modules have the symbol __this_module */
Rasmus Villemoes0f5e5a32019-03-20 09:17:57 +01003878 static const char this_mod[] = "__this_module";
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003879 char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003880 unsigned long val;
3881 int n;
3882
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003883 n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003884
Salvatore Mesoraca419e9fe2018-03-30 10:53:08 +02003885 if (n > sizeof(modname) - 1)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003886 return false;
3887
3888 val = module_kallsyms_lookup_name(modname);
3889 return val != 0;
3890}
3891
3892static int cache_mod(struct trace_array *tr,
3893 const char *func, char *module, int enable)
3894{
3895 struct ftrace_mod_load *ftrace_mod, *n;
3896 struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3897 int ret;
3898
3899 mutex_lock(&ftrace_lock);
3900
3901 /* We do not cache inverse filters */
3902 if (func[0] == '!') {
3903 func++;
3904 ret = -EINVAL;
3905
3906 /* Look to remove this hash */
3907 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3908 if (strcmp(ftrace_mod->module, module) != 0)
3909 continue;
3910
3911 /* no func matches all */
Dan Carpenter44925df2017-07-12 10:33:40 +03003912 if (strcmp(func, "*") == 0 ||
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04003913 (ftrace_mod->func &&
3914 strcmp(ftrace_mod->func, func) == 0)) {
3915 ret = 0;
3916 free_ftrace_mod(ftrace_mod);
3917 continue;
3918 }
3919 }
3920 goto out;
3921 }
3922
3923 ret = -EINVAL;
3924 /* We only care about modules that have not been loaded yet */
3925 if (module_exists(module))
3926 goto out;
3927
3928 /* Save this string off, and execute it when the module is loaded */
3929 ret = ftrace_add_mod(tr, func, module, enable);
3930 out:
3931 mutex_unlock(&ftrace_lock);
3932
3933 return ret;
3934}
3935
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003936static int
3937ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3938 int reset, int enable);
3939
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02003940#ifdef CONFIG_MODULES
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003941static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3942 char *mod, bool enable)
3943{
3944 struct ftrace_mod_load *ftrace_mod, *n;
3945 struct ftrace_hash **orig_hash, *new_hash;
3946 LIST_HEAD(process_mods);
3947 char *func;
3948 int ret;
3949
3950 mutex_lock(&ops->func_hash->regex_lock);
3951
3952 if (enable)
3953 orig_hash = &ops->func_hash->filter_hash;
3954 else
3955 orig_hash = &ops->func_hash->notrace_hash;
3956
3957 new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
3958 *orig_hash);
3959 if (!new_hash)
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04003960 goto out; /* warn? */
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003961
3962 mutex_lock(&ftrace_lock);
3963
3964 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3965
3966 if (strcmp(ftrace_mod->module, mod) != 0)
3967 continue;
3968
3969 if (ftrace_mod->func)
3970 func = kstrdup(ftrace_mod->func, GFP_KERNEL);
3971 else
3972 func = kstrdup("*", GFP_KERNEL);
3973
3974 if (!func) /* warn? */
3975 continue;
3976
3977 list_del(&ftrace_mod->list);
3978 list_add(&ftrace_mod->list, &process_mods);
3979
3980 /* Use the newly allocated func, as it may be "*" */
3981 kfree(ftrace_mod->func);
3982 ftrace_mod->func = func;
3983 }
3984
3985 mutex_unlock(&ftrace_lock);
3986
3987 list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
3988
3989 func = ftrace_mod->func;
3990
3991 /* Grabs ftrace_lock, which is why we have this extra step */
3992 match_records(new_hash, func, strlen(func), mod);
3993 free_ftrace_mod(ftrace_mod);
3994 }
3995
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04003996 if (enable && list_empty(head))
3997 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
3998
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04003999 mutex_lock(&ftrace_lock);
4000
4001 ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4002 new_hash, enable);
4003 mutex_unlock(&ftrace_lock);
4004
Steven Rostedt (VMware)3b58a3c2017-06-28 09:09:38 -04004005 out:
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004006 mutex_unlock(&ops->func_hash->regex_lock);
4007
4008 free_ftrace_hash(new_hash);
4009}
4010
4011static void process_cached_mods(const char *mod_name)
4012{
4013 struct trace_array *tr;
4014 char *mod;
4015
4016 mod = kstrdup(mod_name, GFP_KERNEL);
4017 if (!mod)
4018 return;
4019
4020 mutex_lock(&trace_types_lock);
4021 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4022 if (!list_empty(&tr->mod_trace))
4023 process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4024 if (!list_empty(&tr->mod_notrace))
4025 process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4026 }
4027 mutex_unlock(&trace_types_lock);
4028
4029 kfree(mod);
4030}
Arnd Bergmann69449bbd2017-07-10 10:44:03 +02004031#endif
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04004032
Steven Rostedtf6180772009-02-14 00:40:25 -05004033/*
4034 * We register the module command as a template to show others how
4035 * to register the a command as well.
4036 */
4037
4038static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004039ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004040 char *func_orig, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05004041{
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004042 char *func;
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03004043 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05004044
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004045 /* match_records() modifies func, and we need the original */
4046 func = kstrdup(func_orig, GFP_KERNEL);
4047 if (!func)
4048 return -ENOMEM;
4049
Steven Rostedtf6180772009-02-14 00:40:25 -05004050 /*
4051 * cmd == 'mod' because we only registered this func
4052 * for the 'mod' ftrace_func_command.
4053 * But if you register one func with multiple commands,
4054 * you can tell which command was used by the cmd
4055 * parameter.
4056 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03004057 ret = match_records(hash, func, strlen(func), module);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004058 kfree(func);
4059
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004060 if (!ret)
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04004061 return cache_mod(tr, func_orig, module, enable);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004062 if (ret < 0)
4063 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004064 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05004065}
4066
4067static struct ftrace_func_command ftrace_mod_cmd = {
4068 .name = "mod",
4069 .func = ftrace_mod_callback,
4070};
4071
4072static int __init ftrace_mod_cmd_init(void)
4073{
4074 return register_ftrace_command(&ftrace_mod_cmd);
4075}
Steven Rostedt6f415672012-10-05 12:13:07 -04004076core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05004077
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004078static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004079 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004080{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004081 struct ftrace_probe_ops *probe_ops;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004082 struct ftrace_func_probe *probe;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004083
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004084 probe = container_of(op, struct ftrace_func_probe, ops);
4085 probe_ops = probe->probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004086
4087 /*
4088 * Disable preemption for these calls to prevent a RCU grace
4089 * period. This syncs the hash iteration and freeing of items
4090 * on the hash. rcu_read_lock is too dangerous here.
4091 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04004092 preempt_disable_notrace();
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004093 probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
Steven Rostedt5168ae52010-06-03 09:36:50 -04004094 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05004095}
4096
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004097struct ftrace_func_map {
4098 struct ftrace_func_entry entry;
4099 void *data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004100};
4101
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004102struct ftrace_func_mapper {
4103 struct ftrace_hash hash;
4104};
Steven Rostedt59df055f2009-02-14 15:29:06 -05004105
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004106/**
4107 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4108 *
4109 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4110 */
4111struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004112{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004113 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004114
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004115 /*
4116 * The mapper is simply a ftrace_hash, but since the entries
4117 * in the hash are not ftrace_func_entry type, we define it
4118 * as a separate structure.
4119 */
4120 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4121 return (struct ftrace_func_mapper *)hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004122}
4123
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004124/**
4125 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4126 * @mapper: The mapper that has the ip maps
4127 * @ip: the instruction pointer to find the data for
4128 *
4129 * Returns the data mapped to @ip if found otherwise NULL. The return
4130 * is actually the address of the mapper data pointer. The address is
4131 * returned for use cases where the data is no bigger than a long, and
4132 * the user can use the data pointer as its data instead of having to
4133 * allocate more memory for the reference.
4134 */
4135void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4136 unsigned long ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004137{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004138 struct ftrace_func_entry *entry;
4139 struct ftrace_func_map *map;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004140
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004141 entry = ftrace_lookup_ip(&mapper->hash, ip);
4142 if (!entry)
4143 return NULL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004144
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004145 map = (struct ftrace_func_map *)entry;
4146 return &map->data;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004147}
4148
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004149/**
4150 * ftrace_func_mapper_add_ip - Map some data to an ip
4151 * @mapper: The mapper that has the ip maps
4152 * @ip: The instruction pointer address to map @data to
4153 * @data: The data to map to @ip
4154 *
4155 * Returns 0 on succes otherwise an error.
4156 */
4157int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4158 unsigned long ip, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004159{
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004160 struct ftrace_func_entry *entry;
4161 struct ftrace_func_map *map;
4162
4163 entry = ftrace_lookup_ip(&mapper->hash, ip);
4164 if (entry)
4165 return -EBUSY;
4166
4167 map = kmalloc(sizeof(*map), GFP_KERNEL);
4168 if (!map)
4169 return -ENOMEM;
4170
4171 map->entry.ip = ip;
4172 map->data = data;
4173
4174 __add_hash_entry(&mapper->hash, &map->entry);
4175
4176 return 0;
4177}
4178
4179/**
4180 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4181 * @mapper: The mapper that has the ip maps
4182 * @ip: The instruction pointer address to remove the data from
4183 *
4184 * Returns the data if it is found, otherwise NULL.
4185 * Note, if the data pointer is used as the data itself, (see
4186 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4187 * if the data pointer was set to zero.
4188 */
4189void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4190 unsigned long ip)
4191{
4192 struct ftrace_func_entry *entry;
4193 struct ftrace_func_map *map;
4194 void *data;
4195
4196 entry = ftrace_lookup_ip(&mapper->hash, ip);
4197 if (!entry)
4198 return NULL;
4199
4200 map = (struct ftrace_func_map *)entry;
4201 data = map->data;
4202
4203 remove_hash_entry(&mapper->hash, entry);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004204 kfree(entry);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04004205
4206 return data;
4207}
4208
4209/**
4210 * free_ftrace_func_mapper - free a mapping of ips and data
4211 * @mapper: The mapper that has the ip maps
4212 * @free_func: A function to be called on each data item.
4213 *
4214 * This is used to free the function mapper. The @free_func is optional
4215 * and can be used if the data needs to be freed as well.
4216 */
4217void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4218 ftrace_mapper_func free_func)
4219{
4220 struct ftrace_func_entry *entry;
4221 struct ftrace_func_map *map;
4222 struct hlist_head *hhd;
4223 int size = 1 << mapper->hash.size_bits;
4224 int i;
4225
4226 if (free_func && mapper->hash.count) {
4227 for (i = 0; i < size; i++) {
4228 hhd = &mapper->hash.buckets[i];
4229 hlist_for_each_entry(entry, hhd, hlist) {
4230 map = (struct ftrace_func_map *)entry;
4231 free_func(map);
4232 }
4233 }
4234 }
4235 free_ftrace_hash(&mapper->hash);
4236}
4237
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004238static void release_probe(struct ftrace_func_probe *probe)
4239{
4240 struct ftrace_probe_ops *probe_ops;
4241
4242 mutex_lock(&ftrace_lock);
4243
4244 WARN_ON(probe->ref <= 0);
4245
4246 /* Subtract the ref that was used to protect this instance */
4247 probe->ref--;
4248
4249 if (!probe->ref) {
4250 probe_ops = probe->probe_ops;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004251 /*
4252 * Sending zero as ip tells probe_ops to free
4253 * the probe->data itself
4254 */
4255 if (probe_ops->free)
4256 probe_ops->free(probe_ops, probe->tr, 0, probe->data);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004257 list_del(&probe->list);
4258 kfree(probe);
4259 }
4260 mutex_unlock(&ftrace_lock);
4261}
4262
4263static void acquire_probe_locked(struct ftrace_func_probe *probe)
4264{
4265 /*
4266 * Add one ref to keep it from being freed when releasing the
4267 * ftrace_lock mutex.
4268 */
4269 probe->ref++;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004270}
4271
Steven Rostedt59df055f2009-02-14 15:29:06 -05004272int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004273register_ftrace_function_probe(char *glob, struct trace_array *tr,
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004274 struct ftrace_probe_ops *probe_ops,
4275 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004276{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004277 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004278 struct ftrace_func_probe *probe;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004279 struct ftrace_hash **orig_hash;
4280 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004281 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004282 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004283 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004284 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004285 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004286
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004287 if (WARN_ON(!tr))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004288 return -EINVAL;
4289
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004290 /* We do not support '!' for function probes */
4291 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004292 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004293
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004294
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004295 mutex_lock(&ftrace_lock);
4296 /* Check if the probe_ops is already registered */
4297 list_for_each_entry(probe, &tr->func_probes, list) {
4298 if (probe->probe_ops == probe_ops)
4299 break;
4300 }
4301 if (&probe->list == &tr->func_probes) {
4302 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4303 if (!probe) {
4304 mutex_unlock(&ftrace_lock);
4305 return -ENOMEM;
4306 }
4307 probe->probe_ops = probe_ops;
4308 probe->ops.func = function_trace_probe_call;
4309 probe->tr = tr;
4310 ftrace_ops_init(&probe->ops);
4311 list_add(&probe->list, &tr->func_probes);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004312 }
4313
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004314 acquire_probe_locked(probe);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004315
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004316 mutex_unlock(&ftrace_lock);
4317
4318 mutex_lock(&probe->ops.func_hash->regex_lock);
4319
4320 orig_hash = &probe->ops.func_hash->filter_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004321 old_hash = *orig_hash;
4322 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4323
4324 ret = ftrace_match_records(hash, glob, strlen(glob));
4325
4326 /* Nothing found? */
4327 if (!ret)
4328 ret = -EINVAL;
4329
4330 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004331 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004332
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004333 size = 1 << hash->size_bits;
4334 for (i = 0; i < size; i++) {
4335 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4336 if (ftrace_lookup_ip(old_hash, entry->ip))
4337 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004338 /*
4339 * The caller might want to do something special
4340 * for each function we find. We call the callback
4341 * to give the caller an opportunity to do so.
4342 */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004343 if (probe_ops->init) {
4344 ret = probe_ops->init(probe_ops, tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004345 entry->ip, data,
4346 &probe->data);
4347 if (ret < 0) {
4348 if (probe_ops->free && count)
4349 probe_ops->free(probe_ops, tr,
4350 0, probe->data);
4351 probe->data = NULL;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004352 goto out;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004353 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004354 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004355 count++;
4356 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004357 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004358
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004359 mutex_lock(&ftrace_lock);
4360
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004361 if (!count) {
4362 /* Nothing was added? */
4363 ret = -EINVAL;
4364 goto out_unlock;
4365 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05004366
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004367 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4368 hash, 1);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004369 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004370 goto err_unlock;
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004371
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004372 /* One ref for each new function traced */
4373 probe->ref += count;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004374
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004375 if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4376 ret = ftrace_startup(&probe->ops, 0);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004377
Steven Rostedt59df055f2009-02-14 15:29:06 -05004378 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004379 mutex_unlock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004380
4381 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004382 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004383 out:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004384 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004385 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004386
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004387 release_probe(probe);
4388
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004389 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004390
4391 err_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004392 if (!probe_ops->free || !count)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004393 goto out_unlock;
4394
4395 /* Failed to do the move, need to call the free functions */
4396 for (i = 0; i < size; i++) {
4397 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4398 if (ftrace_lookup_ip(old_hash, entry->ip))
4399 continue;
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004400 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004401 }
4402 }
4403 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004404}
4405
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004406int
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004407unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4408 struct ftrace_probe_ops *probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004409{
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004410 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004411 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004412 struct ftrace_func_probe *probe;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004413 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004414 struct ftrace_hash **orig_hash;
4415 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004416 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004417 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004418 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004419 char str[KSYM_SYMBOL_LEN];
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004420 int count = 0;
4421 int i, ret = -ENODEV;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004422 int size;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004423
Naveen N. Raocbab5672017-05-16 23:21:25 +05304424 if (!glob || !strlen(glob) || !strcmp(glob, "*"))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004425 func_g.search = NULL;
Naveen N. Raocbab5672017-05-16 23:21:25 +05304426 else {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004427 int not;
4428
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004429 func_g.type = filter_parse_regex(glob, strlen(glob),
4430 &func_g.search, &not);
4431 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004432
Steven Rostedtb6887d72009-02-17 12:32:04 -05004433 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004434 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004435 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004436 }
4437
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004438 mutex_lock(&ftrace_lock);
4439 /* Check if the probe_ops is already registered */
4440 list_for_each_entry(probe, &tr->func_probes, list) {
4441 if (probe->probe_ops == probe_ops)
4442 break;
4443 }
4444 if (&probe->list == &tr->func_probes)
4445 goto err_unlock_ftrace;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004446
4447 ret = -EINVAL;
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004448 if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4449 goto err_unlock_ftrace;
4450
4451 acquire_probe_locked(probe);
4452
4453 mutex_unlock(&ftrace_lock);
4454
4455 mutex_lock(&probe->ops.func_hash->regex_lock);
4456
4457 orig_hash = &probe->ops.func_hash->filter_hash;
4458 old_hash = *orig_hash;
4459
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004460 if (ftrace_hash_empty(old_hash))
4461 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004462
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004463 old_hash_ops.filter_hash = old_hash;
4464 /* Probes only have filters */
4465 old_hash_ops.notrace_hash = NULL;
4466
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004467 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004468 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004469 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004470 goto out_unlock;
4471
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004472 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004473
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004474 size = 1 << hash->size_bits;
4475 for (i = 0; i < size; i++) {
4476 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004477
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004478 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004479 kallsyms_lookup(entry->ip, NULL, NULL,
4480 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004481 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004482 continue;
4483 }
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004484 count++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004485 remove_hash_entry(hash, entry);
4486 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004487 }
4488 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004489
4490 /* Nothing found? */
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004491 if (!count) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004492 ret = -EINVAL;
4493 goto out_unlock;
4494 }
4495
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004496 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004497
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004498 WARN_ON(probe->ref < count);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004499
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004500 probe->ref -= count;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004501
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004502 if (ftrace_hash_empty(hash))
4503 ftrace_shutdown(&probe->ops, 0);
4504
4505 ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004506 hash, 1);
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004507
4508 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004509 if (ftrace_enabled && !ftrace_hash_empty(hash))
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004510 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)82cc4fc2017-04-14 17:45:45 -04004511 &old_hash_ops);
Paul E. McKenney74401722018-11-06 18:44:52 -08004512 synchronize_rcu();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004513
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004514 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4515 hlist_del(&entry->hlist);
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004516 if (probe_ops->free)
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04004517 probe_ops->free(probe_ops, tr, entry->ip, probe->data);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004518 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004519 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004520 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004521
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004522 out_unlock:
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004523 mutex_unlock(&probe->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004524 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004525
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004526 release_probe(probe);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004527
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004528 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004529
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04004530 err_unlock_ftrace:
4531 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004532 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004533}
4534
Naveen N. Raoa0e63692017-05-16 23:21:26 +05304535void clear_ftrace_function_probes(struct trace_array *tr)
4536{
4537 struct ftrace_func_probe *probe, *n;
4538
4539 list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4540 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4541}
4542
Steven Rostedtf6180772009-02-14 00:40:25 -05004543static LIST_HEAD(ftrace_commands);
4544static DEFINE_MUTEX(ftrace_cmd_mutex);
4545
Tom Zanussi38de93a2013-10-24 08:34:18 -05004546/*
4547 * Currently we only register ftrace commands from __init, so mark this
4548 * __init too.
4549 */
4550__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004551{
4552 struct ftrace_func_command *p;
4553 int ret = 0;
4554
4555 mutex_lock(&ftrace_cmd_mutex);
4556 list_for_each_entry(p, &ftrace_commands, list) {
4557 if (strcmp(cmd->name, p->name) == 0) {
4558 ret = -EBUSY;
4559 goto out_unlock;
4560 }
4561 }
4562 list_add(&cmd->list, &ftrace_commands);
4563 out_unlock:
4564 mutex_unlock(&ftrace_cmd_mutex);
4565
4566 return ret;
4567}
4568
Tom Zanussi38de93a2013-10-24 08:34:18 -05004569/*
4570 * Currently we only unregister ftrace commands from __init, so mark
4571 * this __init too.
4572 */
4573__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004574{
4575 struct ftrace_func_command *p, *n;
4576 int ret = -ENODEV;
4577
4578 mutex_lock(&ftrace_cmd_mutex);
4579 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4580 if (strcmp(cmd->name, p->name) == 0) {
4581 ret = 0;
4582 list_del_init(&p->list);
4583 goto out_unlock;
4584 }
4585 }
4586 out_unlock:
4587 mutex_unlock(&ftrace_cmd_mutex);
4588
4589 return ret;
4590}
4591
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004592static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004593 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004594{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004595 struct ftrace_hash *hash = iter->hash;
Steven Rostedt (VMware)d2afd57a2017-04-20 11:31:35 -04004596 struct trace_array *tr = iter->ops->private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004597 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004598 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004599 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004600
4601 func = strsep(&next, ":");
4602
4603 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004604 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004605 if (!ret)
4606 ret = -EINVAL;
4607 if (ret < 0)
4608 return ret;
4609 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004610 }
4611
Steven Rostedtf6180772009-02-14 00:40:25 -05004612 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004613
4614 command = strsep(&next, ":");
4615
Steven Rostedtf6180772009-02-14 00:40:25 -05004616 mutex_lock(&ftrace_cmd_mutex);
4617 list_for_each_entry(p, &ftrace_commands, list) {
4618 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004619 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004620 goto out_unlock;
4621 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004622 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004623 out_unlock:
4624 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004625
Steven Rostedtf6180772009-02-14 00:40:25 -05004626 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004627}
4628
Ingo Molnare309b412008-05-12 21:20:51 +02004629static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004630ftrace_regex_write(struct file *file, const char __user *ubuf,
4631 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004632{
4633 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004634 struct trace_parser *parser;
4635 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004636
Li Zefan4ba79782009-09-22 13:52:20 +08004637 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004638 return 0;
4639
Steven Rostedt5072c592008-05-12 21:20:43 +02004640 if (file->f_mode & FMODE_READ) {
4641 struct seq_file *m = file->private_data;
4642 iter = m->private;
4643 } else
4644 iter = file->private_data;
4645
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004646 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004647 return -ENODEV;
4648
4649 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004650
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004651 parser = &iter->parser;
4652 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004653
Li Zefan4ba79782009-09-22 13:52:20 +08004654 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004655 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004656 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004657 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004658 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004659 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004660 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004661 }
4662
Steven Rostedt5072c592008-05-12 21:20:43 +02004663 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004664 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004665 return ret;
4666}
4667
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004668ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004669ftrace_filter_write(struct file *file, const char __user *ubuf,
4670 size_t cnt, loff_t *ppos)
4671{
4672 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4673}
4674
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004675ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004676ftrace_notrace_write(struct file *file, const char __user *ubuf,
4677 size_t cnt, loff_t *ppos)
4678{
4679 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4680}
4681
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004682static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004683ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4684{
4685 struct ftrace_func_entry *entry;
4686
4687 if (!ftrace_location(ip))
4688 return -EINVAL;
4689
4690 if (remove) {
4691 entry = ftrace_lookup_ip(hash, ip);
4692 if (!entry)
4693 return -ENOENT;
4694 free_hash_entry(hash, entry);
4695 return 0;
4696 }
4697
4698 return add_hash_entry(hash, ip);
4699}
4700
4701static int
4702ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4703 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004704{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004705 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004706 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004707 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004708
Steven Rostedt41c52c02008-05-22 11:46:33 -04004709 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004710 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004711
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004712 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004713
Steven Rostedtf45948e2011-05-02 12:29:25 -04004714 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004715 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004716 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004717 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004718
Wang Nanb972cc52014-07-15 08:40:20 +08004719 if (reset)
4720 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4721 else
4722 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4723
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004724 if (!hash) {
4725 ret = -ENOMEM;
4726 goto out_regex_unlock;
4727 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004728
Jiri Olsaac483c42012-01-02 10:04:14 +01004729 if (buf && !ftrace_match_records(hash, buf, len)) {
4730 ret = -EINVAL;
4731 goto out_regex_unlock;
4732 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004733 if (ip) {
4734 ret = ftrace_match_addr(hash, ip, remove);
4735 if (ret < 0)
4736 goto out_regex_unlock;
4737 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004738
4739 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004740 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004741 mutex_unlock(&ftrace_lock);
4742
Jiri Olsaac483c42012-01-02 10:04:14 +01004743 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004744 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004745
4746 free_ftrace_hash(hash);
4747 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004748}
4749
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004750static int
4751ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4752 int reset, int enable)
4753{
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05304754 return ftrace_set_hash(ops, NULL, 0, ip, remove, reset, enable);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004755}
4756
4757/**
4758 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4759 * @ops - the ops to set the filter with
4760 * @ip - the address to add to or remove from the filter.
4761 * @remove - non zero to remove the ip from the filter
4762 * @reset - non zero to reset all filters before applying this filter.
4763 *
4764 * Filters denote which functions should be enabled when tracing is enabled
4765 * If @ip is NULL, it failes to update filter.
4766 */
4767int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4768 int remove, int reset)
4769{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004770 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004771 return ftrace_set_addr(ops, ip, remove, reset, 1);
4772}
4773EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4774
Joel Fernandesd032ae82016-11-15 12:31:20 -08004775/**
4776 * ftrace_ops_set_global_filter - setup ops to use global filters
4777 * @ops - the ops which will use the global filters
4778 *
4779 * ftrace users who need global function trace filtering should call this.
4780 * It can set the global filter only if ops were not initialized before.
4781 */
4782void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4783{
4784 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4785 return;
4786
4787 ftrace_ops_init(ops);
4788 ops->func_hash = &global_ops.local_hash;
4789}
4790EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4791
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004792static int
4793ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4794 int reset, int enable)
4795{
4796 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4797}
4798
Steven Rostedt77a2b372008-05-12 21:20:45 +02004799/**
4800 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004801 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004802 * @buf - the string that holds the function filter text.
4803 * @len - the length of the string.
4804 * @reset - non zero to reset all filters before applying this filter.
4805 *
4806 * Filters denote which functions should be enabled when tracing is enabled.
4807 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4808 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004809int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004810 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004811{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004812 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004813 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004814}
Steven Rostedt936e0742011-05-05 22:54:01 -04004815EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004816
Steven Rostedt41c52c02008-05-22 11:46:33 -04004817/**
4818 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004819 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004820 * @buf - the string that holds the function notrace text.
4821 * @len - the length of the string.
4822 * @reset - non zero to reset all filters before applying this filter.
4823 *
4824 * Notrace Filters denote which functions should not be enabled when tracing
4825 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4826 * for tracing.
4827 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004828int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004829 int len, int reset)
4830{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004831 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004832 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004833}
4834EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4835/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004836 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004837 * @buf - the string that holds the function filter text.
4838 * @len - the length of the string.
4839 * @reset - non zero to reset all filters before applying this filter.
4840 *
4841 * Filters denote which functions should be enabled when tracing is enabled.
4842 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4843 */
4844void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4845{
4846 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4847}
4848EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4849
4850/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004851 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004852 * @buf - the string that holds the function notrace text.
4853 * @len - the length of the string.
4854 * @reset - non zero to reset all filters before applying this filter.
4855 *
4856 * Notrace Filters denote which functions should not be enabled when tracing
4857 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4858 * for tracing.
4859 */
4860void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004861{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004862 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004863}
Steven Rostedt936e0742011-05-05 22:54:01 -04004864EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004865
Steven Rostedt2af15d62009-05-28 13:37:24 -04004866/*
4867 * command line interface to allow users to set filters on boot up.
4868 */
4869#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4870static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4871static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4872
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004873/* Used by function selftest to not test if filter is set */
4874bool ftrace_filter_param __initdata;
4875
Steven Rostedt2af15d62009-05-28 13:37:24 -04004876static int __init set_ftrace_notrace(char *str)
4877{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004878 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004879 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004880 return 1;
4881}
4882__setup("ftrace_notrace=", set_ftrace_notrace);
4883
4884static int __init set_ftrace_filter(char *str)
4885{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004886 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004887 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004888 return 1;
4889}
4890__setup("ftrace_filter=", set_ftrace_filter);
4891
Stefan Assmann369bc182009-10-12 22:17:21 +02004892#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004893static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004894static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004895static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004896
Stefan Assmann369bc182009-10-12 22:17:21 +02004897static int __init set_graph_function(char *str)
4898{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004899 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004900 return 1;
4901}
4902__setup("ftrace_graph_filter=", set_graph_function);
4903
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004904static int __init set_graph_notrace_function(char *str)
4905{
4906 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4907 return 1;
4908}
4909__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4910
Todd Brandt65a50c652017-03-02 16:12:15 -08004911static int __init set_graph_max_depth_function(char *str)
4912{
4913 if (!str)
4914 return 0;
4915 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4916 return 1;
4917}
4918__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4919
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004920static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004921{
4922 int ret;
4923 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004924 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004925
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004926 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4927 if (WARN_ON(!hash))
4928 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004929
4930 while (buf) {
4931 func = strsep(&buf, ",");
4932 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004933 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004934 if (ret)
4935 printk(KERN_DEBUG "ftrace: function %s not "
4936 "traceable\n", func);
4937 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004938
4939 if (enable)
4940 ftrace_graph_hash = hash;
4941 else
4942 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02004943}
4944#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4945
Steven Rostedt2a85a372011-12-19 21:57:44 -05004946void __init
4947ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004948{
4949 char *func;
4950
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004951 ftrace_ops_init(ops);
4952
Steven Rostedt2af15d62009-05-28 13:37:24 -04004953 while (buf) {
4954 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004955 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004956 }
4957}
4958
4959static void __init set_ftrace_early_filters(void)
4960{
4961 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004962 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004963 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004964 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004965#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4966 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004967 set_ftrace_early_graph(ftrace_graph_buf, 1);
4968 if (ftrace_graph_notrace_buf[0])
4969 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004970#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004971}
4972
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004973int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004974{
4975 struct seq_file *m = (struct seq_file *)file->private_data;
4976 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004977 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004978 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004979 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004980 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004981
Steven Rostedt5072c592008-05-12 21:20:43 +02004982 if (file->f_mode & FMODE_READ) {
4983 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004984 seq_release(inode, file);
4985 } else
4986 iter = file->private_data;
4987
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004988 parser = &iter->parser;
4989 if (trace_parser_loaded(parser)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004990 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004991 }
4992
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004993 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004994
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004995 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004996
Steven Rostedt058e2972011-04-29 22:35:33 -04004997 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004998 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4999
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005000 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005001 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedt (VMware)69d71872017-07-05 09:45:43 -04005002 if (iter->tr && !list_empty(&iter->tr->mod_trace))
Steven Rostedt (VMware)8c08f0d2017-06-26 11:47:31 -04005003 iter->hash->flags |= FTRACE_HASH_FL_MOD;
5004 } else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005005 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005006
Steven Rostedt058e2972011-04-29 22:35:33 -04005007 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04005008 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5009 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04005010 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04005011 } else {
5012 /* For read only, the hash is the ops hash */
5013 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04005014 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09005015
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005016 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04005017 free_ftrace_hash(iter->hash);
5018 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04005019
Steven Rostedt5072c592008-05-12 21:20:43 +02005020 return 0;
5021}
5022
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005023static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005024 .open = ftrace_avail_open,
5025 .read = seq_read,
5026 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08005027 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02005028};
5029
Steven Rostedt647bcd02011-05-03 14:39:21 -04005030static const struct file_operations ftrace_enabled_fops = {
5031 .open = ftrace_enabled_open,
5032 .read = seq_read,
5033 .llseek = seq_lseek,
5034 .release = seq_release_private,
5035};
5036
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005037static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02005038 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005039 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02005040 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005041 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005042 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02005043};
5044
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005045static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04005046 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08005047 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005048 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005049 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04005050 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04005051};
5052
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005053#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5054
5055static DEFINE_MUTEX(graph_lock);
5056
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005057struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
5058struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
5059
5060enum graph_filter_type {
5061 GRAPH_FILTER_NOTRACE = 0,
5062 GRAPH_FILTER_FUNCTION,
5063};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005064
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005065#define FTRACE_GRAPH_EMPTY ((void *)1)
5066
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005067struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005068 struct ftrace_hash *hash;
5069 struct ftrace_func_entry *entry;
5070 int idx; /* for hash table iteration */
5071 enum graph_filter_type type;
5072 struct ftrace_hash *new_hash;
5073 const struct seq_operations *seq_ops;
5074 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005075};
5076
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005077static void *
Li Zefan85951842009-06-24 09:54:00 +08005078__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005079{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005080 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005081 struct ftrace_func_entry *entry = fgd->entry;
5082 struct hlist_head *head;
5083 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005084
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005085 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005086 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005087
5088 if (entry) {
5089 hlist_for_each_entry_continue(entry, hlist) {
5090 fgd->entry = entry;
5091 return entry;
5092 }
5093
5094 idx++;
5095 }
5096
5097 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5098 head = &fgd->hash->buckets[i];
5099 hlist_for_each_entry(entry, head, hlist) {
5100 fgd->entry = entry;
5101 fgd->idx = i;
5102 return entry;
5103 }
5104 }
5105 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08005106}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005107
Li Zefan85951842009-06-24 09:54:00 +08005108static void *
5109g_next(struct seq_file *m, void *v, loff_t *pos)
5110{
5111 (*pos)++;
5112 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005113}
5114
5115static void *g_start(struct seq_file *m, loff_t *pos)
5116{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005117 struct ftrace_graph_data *fgd = m->private;
5118
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005119 mutex_lock(&graph_lock);
5120
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005121 if (fgd->type == GRAPH_FILTER_FUNCTION)
5122 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5123 lockdep_is_held(&graph_lock));
5124 else
5125 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5126 lockdep_is_held(&graph_lock));
5127
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005128 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005129 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005130 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005131
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005132 fgd->idx = 0;
5133 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08005134 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005135}
5136
5137static void g_stop(struct seq_file *m, void *p)
5138{
5139 mutex_unlock(&graph_lock);
5140}
5141
5142static int g_show(struct seq_file *m, void *v)
5143{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005144 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005145
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005146 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005147 return 0;
5148
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05005149 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09005150 struct ftrace_graph_data *fgd = m->private;
5151
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005152 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005153 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09005154 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005155 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005156 return 0;
5157 }
5158
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005159 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005160
5161 return 0;
5162}
5163
James Morris88e9d342009-09-22 16:43:43 -07005164static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005165 .start = g_start,
5166 .next = g_next,
5167 .stop = g_stop,
5168 .show = g_show,
5169};
5170
5171static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005172__ftrace_graph_open(struct inode *inode, struct file *file,
5173 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005174{
5175 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005176 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005177
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005178 if (file->f_mode & FMODE_WRITE) {
5179 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5180
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005181 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5182 return -ENOMEM;
5183
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005184 if (file->f_flags & O_TRUNC)
5185 new_hash = alloc_ftrace_hash(size_bits);
5186 else
5187 new_hash = alloc_and_copy_ftrace_hash(size_bits,
5188 fgd->hash);
5189 if (!new_hash) {
5190 ret = -ENOMEM;
5191 goto out;
5192 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005193 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005194
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005195 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005196 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005197 if (!ret) {
5198 struct seq_file *m = file->private_data;
5199 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005200 } else {
5201 /* Failed */
5202 free_ftrace_hash(new_hash);
5203 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005204 }
5205 } else
5206 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08005207
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005208out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005209 if (ret < 0 && file->f_mode & FMODE_WRITE)
5210 trace_parser_put(&fgd->parser);
5211
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005212 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005213
5214 /*
5215 * All uses of fgd->hash must be taken with the graph_lock
5216 * held. The graph_lock is going to be released, so force
5217 * fgd->hash to be reinitialized when it is taken again.
5218 */
5219 fgd->hash = NULL;
5220
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005221 return ret;
5222}
5223
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005224static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005225ftrace_graph_open(struct inode *inode, struct file *file)
5226{
5227 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005228 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005229
5230 if (unlikely(ftrace_disabled))
5231 return -ENODEV;
5232
5233 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5234 if (fgd == NULL)
5235 return -ENOMEM;
5236
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005237 mutex_lock(&graph_lock);
5238
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005239 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5240 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005241 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005242 fgd->seq_ops = &ftrace_graph_seq_ops;
5243
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005244 ret = __ftrace_graph_open(inode, file, fgd);
5245 if (ret < 0)
5246 kfree(fgd);
5247
5248 mutex_unlock(&graph_lock);
5249 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005250}
5251
5252static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005253ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5254{
5255 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005256 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005257
5258 if (unlikely(ftrace_disabled))
5259 return -ENODEV;
5260
5261 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5262 if (fgd == NULL)
5263 return -ENOMEM;
5264
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005265 mutex_lock(&graph_lock);
5266
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05005267 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5268 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005269 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005270 fgd->seq_ops = &ftrace_graph_seq_ops;
5271
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005272 ret = __ftrace_graph_open(inode, file, fgd);
5273 if (ret < 0)
5274 kfree(fgd);
5275
5276 mutex_unlock(&graph_lock);
5277 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005278}
5279
5280static int
Li Zefan87827112009-07-23 11:29:11 +08005281ftrace_graph_release(struct inode *inode, struct file *file)
5282{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005283 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005284 struct ftrace_hash *old_hash, *new_hash;
5285 struct trace_parser *parser;
5286 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005287
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005288 if (file->f_mode & FMODE_READ) {
5289 struct seq_file *m = file->private_data;
5290
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005291 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08005292 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005293 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005294 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005295 }
5296
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005297
5298 if (file->f_mode & FMODE_WRITE) {
5299
5300 parser = &fgd->parser;
5301
5302 if (trace_parser_loaded((parser))) {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005303 ret = ftrace_graph_set_hash(fgd->new_hash,
5304 parser->buffer);
5305 }
5306
5307 trace_parser_put(parser);
5308
5309 new_hash = __ftrace_hash_move(fgd->new_hash);
5310 if (!new_hash) {
5311 ret = -ENOMEM;
5312 goto out;
5313 }
5314
5315 mutex_lock(&graph_lock);
5316
5317 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5318 old_hash = rcu_dereference_protected(ftrace_graph_hash,
5319 lockdep_is_held(&graph_lock));
5320 rcu_assign_pointer(ftrace_graph_hash, new_hash);
5321 } else {
5322 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5323 lockdep_is_held(&graph_lock));
5324 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5325 }
5326
5327 mutex_unlock(&graph_lock);
5328
5329 /* Wait till all users are no longer using the old hash */
Paul E. McKenney74401722018-11-06 18:44:52 -08005330 synchronize_rcu();
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005331
5332 free_ftrace_hash(old_hash);
5333 }
5334
5335 out:
Luis Henriquesf9797c22017-05-25 16:20:38 +01005336 free_ftrace_hash(fgd->new_hash);
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005337 kfree(fgd);
5338
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005339 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005340}
5341
5342static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005343ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005344{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005345 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005346 struct dyn_ftrace *rec;
5347 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005348 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005349 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005350 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005351
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005352 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005353 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5354 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005355
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005356 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005357
Steven Rostedt52baf112009-02-14 01:15:39 -05005358 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005359
5360 if (unlikely(ftrace_disabled)) {
5361 mutex_unlock(&ftrace_lock);
5362 return -ENODEV;
5363 }
5364
Steven Rostedt265c8312009-02-13 12:43:56 -05005365 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005366
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005367 if (rec->flags & FTRACE_FL_DISABLED)
5368 continue;
5369
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005370 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005371 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005372
5373 if (!not) {
5374 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005375
5376 if (entry)
5377 continue;
5378 if (add_hash_entry(hash, rec->ip) < 0)
5379 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005380 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005381 if (entry) {
5382 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005383 fail = 0;
5384 }
5385 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005386 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005387 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005388out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005389 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005390
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005391 if (fail)
5392 return -EINVAL;
5393
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005394 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005395}
5396
5397static ssize_t
5398ftrace_graph_write(struct file *file, const char __user *ubuf,
5399 size_t cnt, loff_t *ppos)
5400{
Namhyung Kim6a101082013-10-14 17:24:25 +09005401 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005402 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005403 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005404
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005405 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005406 return 0;
5407
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005408 /* Read mode uses seq functions */
5409 if (file->f_mode & FMODE_READ) {
5410 struct seq_file *m = file->private_data;
5411 fgd = m->private;
5412 }
5413
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005414 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005415
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005416 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005417
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005418 if (read >= 0 && trace_parser_loaded(parser) &&
5419 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005420
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005421 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005422 parser->buffer);
5423 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005424 }
5425
Namhyung Kim6a101082013-10-14 17:24:25 +09005426 if (!ret)
5427 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005428
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005429 return ret;
5430}
5431
5432static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005433 .open = ftrace_graph_open,
5434 .read = seq_read,
5435 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005436 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005437 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005438};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005439
5440static const struct file_operations ftrace_graph_notrace_fops = {
5441 .open = ftrace_graph_notrace_open,
5442 .read = seq_read,
5443 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005444 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005445 .release = ftrace_graph_release,
5446};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005447#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5448
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005449void ftrace_create_filter_files(struct ftrace_ops *ops,
5450 struct dentry *parent)
5451{
5452
5453 trace_create_file("set_ftrace_filter", 0644, parent,
5454 ops, &ftrace_filter_fops);
5455
5456 trace_create_file("set_ftrace_notrace", 0644, parent,
5457 ops, &ftrace_notrace_fops);
5458}
5459
5460/*
5461 * The name "destroy_filter_files" is really a misnomer. Although
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05305462 * in the future, it may actually delete the files, but this is
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005463 * really intended to make sure the ops passed in are disabled
5464 * and that when this function returns, the caller is free to
5465 * free the ops.
5466 *
5467 * The "destroy" name is only to match the "create" name that this
5468 * should be paired with.
5469 */
5470void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5471{
5472 mutex_lock(&ftrace_lock);
5473 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5474 ftrace_shutdown(ops, 0);
5475 ops->flags |= FTRACE_OPS_FL_DELETED;
Steven Rostedt (VMware)2840f842018-12-10 23:58:01 -05005476 ftrace_free_filter(ops);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005477 mutex_unlock(&ftrace_lock);
5478}
5479
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005480static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005481{
Steven Rostedt5072c592008-05-12 21:20:43 +02005482
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005483 trace_create_file("available_filter_functions", 0444,
5484 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005485
Steven Rostedt647bcd02011-05-03 14:39:21 -04005486 trace_create_file("enabled_functions", 0444,
5487 d_tracer, NULL, &ftrace_enabled_fops);
5488
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005489 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005490
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005491#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Chen LinX1ce05002014-09-03 14:31:09 +08005492 trace_create_file("set_graph_function", 0644, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005493 NULL,
5494 &ftrace_graph_fops);
Chen LinX1ce05002014-09-03 14:31:09 +08005495 trace_create_file("set_graph_notrace", 0644, d_tracer,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005496 NULL,
5497 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005498#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5499
Steven Rostedt5072c592008-05-12 21:20:43 +02005500 return 0;
5501}
5502
Steven Rostedt9fd49322012-04-24 22:32:06 -04005503static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005504{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005505 const unsigned long *ipa = a;
5506 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005507
Steven Rostedt9fd49322012-04-24 22:32:06 -04005508 if (*ipa > *ipb)
5509 return 1;
5510 if (*ipa < *ipb)
5511 return -1;
5512 return 0;
5513}
5514
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005515static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005516 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005517 unsigned long *end)
5518{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005519 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005520 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005521 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005522 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005523 unsigned long *p;
5524 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005525 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005526 int ret = -ENOMEM;
5527
5528 count = end - start;
5529
5530 if (!count)
5531 return 0;
5532
Steven Rostedt9fd49322012-04-24 22:32:06 -04005533 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005534 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005535
Steven Rostedt706c81f2012-04-24 23:45:26 -04005536 start_pg = ftrace_allocate_pages(count);
5537 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005538 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005539
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005540 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005541
Steven Rostedt320823092011-12-16 14:42:37 -05005542 /*
5543 * Core and each module needs their own pages, as
5544 * modules will free them when they are removed.
5545 * Force a new page to be allocated for modules.
5546 */
Steven Rostedta7900872011-12-16 16:23:44 -05005547 if (!mod) {
5548 WARN_ON(ftrace_pages || ftrace_pages_start);
5549 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005550 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005551 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005552 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005553 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005554
Steven Rostedta7900872011-12-16 16:23:44 -05005555 if (WARN_ON(ftrace_pages->next)) {
5556 /* Hmm, we have free pages? */
5557 while (ftrace_pages->next)
5558 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005559 }
Steven Rostedta7900872011-12-16 16:23:44 -05005560
Steven Rostedt706c81f2012-04-24 23:45:26 -04005561 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005562 }
5563
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005564 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005565 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005566 while (p < end) {
5567 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005568 /*
5569 * Some architecture linkers will pad between
5570 * the different mcount_loc sections of different
5571 * object files to satisfy alignments.
5572 * Skip any NULL pointers.
5573 */
5574 if (!addr)
5575 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005576
5577 if (pg->index == pg->size) {
5578 /* We should have allocated enough */
5579 if (WARN_ON(!pg->next))
5580 break;
5581 pg = pg->next;
5582 }
5583
5584 rec = &pg->records[pg->index++];
5585 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005586 }
5587
Steven Rostedt706c81f2012-04-24 23:45:26 -04005588 /* We should have used all pages */
5589 WARN_ON(pg->next);
5590
5591 /* Assign the last page to ftrace_pages */
5592 ftrace_pages = pg;
5593
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005594 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005595 * We only need to disable interrupts on start up
5596 * because we are modifying code that an interrupt
5597 * may execute, and the modification is not atomic.
5598 * But for modules, nothing runs the code we modify
5599 * until we are finished with it, and there's no
5600 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005601 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005602 if (!mod)
5603 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005604 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005605 if (!mod)
5606 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005607 ret = 0;
5608 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005609 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005610
Steven Rostedta7900872011-12-16 16:23:44 -05005611 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005612}
5613
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005614struct ftrace_mod_func {
5615 struct list_head list;
5616 char *name;
5617 unsigned long ip;
5618 unsigned int size;
5619};
5620
5621struct ftrace_mod_map {
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005622 struct rcu_head rcu;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005623 struct list_head list;
5624 struct module *mod;
5625 unsigned long start_addr;
5626 unsigned long end_addr;
5627 struct list_head funcs;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005628 unsigned int num_funcs;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005629};
5630
Steven Rostedt93eb6772009-04-15 13:24:06 -04005631#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005632
5633#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5634
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005635static LIST_HEAD(ftrace_mod_maps);
5636
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005637static int referenced_filters(struct dyn_ftrace *rec)
5638{
5639 struct ftrace_ops *ops;
5640 int cnt = 0;
5641
5642 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5643 if (ops_references_rec(ops, rec))
5644 cnt++;
5645 }
5646
5647 return cnt;
5648}
5649
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005650static void
5651clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5652{
5653 struct ftrace_func_entry *entry;
5654 struct dyn_ftrace *rec;
5655 int i;
5656
5657 if (ftrace_hash_empty(hash))
5658 return;
5659
5660 for (i = 0; i < pg->index; i++) {
5661 rec = &pg->records[i];
5662 entry = __ftrace_lookup_ip(hash, rec->ip);
5663 /*
5664 * Do not allow this rec to match again.
5665 * Yeah, it may waste some memory, but will be removed
5666 * if/when the hash is modified again.
5667 */
5668 if (entry)
5669 entry->ip = 0;
5670 }
5671}
5672
5673/* Clear any records from hashs */
5674static void clear_mod_from_hashes(struct ftrace_page *pg)
5675{
5676 struct trace_array *tr;
5677
5678 mutex_lock(&trace_types_lock);
5679 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5680 if (!tr->ops || !tr->ops->func_hash)
5681 continue;
5682 mutex_lock(&tr->ops->func_hash->regex_lock);
5683 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5684 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5685 mutex_unlock(&tr->ops->func_hash->regex_lock);
5686 }
5687 mutex_unlock(&trace_types_lock);
5688}
5689
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005690static void ftrace_free_mod_map(struct rcu_head *rcu)
5691{
5692 struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5693 struct ftrace_mod_func *mod_func;
5694 struct ftrace_mod_func *n;
5695
5696 /* All the contents of mod_map are now not visible to readers */
5697 list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5698 kfree(mod_func->name);
5699 list_del(&mod_func->list);
5700 kfree(mod_func);
5701 }
5702
5703 kfree(mod_map);
5704}
5705
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005706void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005707{
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005708 struct ftrace_mod_map *mod_map;
5709 struct ftrace_mod_map *n;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005710 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005711 struct ftrace_page **last_pg;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005712 struct ftrace_page *tmp_page = NULL;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005713 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005714 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005715
Steven Rostedt93eb6772009-04-15 13:24:06 -04005716 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005717
5718 if (ftrace_disabled)
5719 goto out_unlock;
5720
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005721 list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5722 if (mod_map->mod == mod) {
5723 list_del_rcu(&mod_map->list);
Paul E. McKenney74401722018-11-06 18:44:52 -08005724 call_rcu(&mod_map->rcu, ftrace_free_mod_map);
Steven Rostedt (VMware)6aa69782017-09-05 19:20:16 -04005725 break;
5726 }
5727 }
5728
Steven Rostedt320823092011-12-16 14:42:37 -05005729 /*
5730 * Each module has its own ftrace_pages, remove
5731 * them from the list.
5732 */
5733 last_pg = &ftrace_pages_start;
5734 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5735 rec = &pg->records[0];
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005736 if (within_module_core(rec->ip, mod) ||
5737 within_module_init(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005738 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005739 * As core pages are first, the first
5740 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005741 */
Steven Rostedt320823092011-12-16 14:42:37 -05005742 if (WARN_ON(pg == ftrace_pages_start))
5743 goto out_unlock;
5744
5745 /* Check if we are deleting the last page */
5746 if (pg == ftrace_pages)
5747 ftrace_pages = next_to_ftrace_page(last_pg);
5748
Steven Rostedt (VMware)83dd1492017-06-27 11:04:40 -04005749 ftrace_update_tot_cnt -= pg->index;
Steven Rostedt320823092011-12-16 14:42:37 -05005750 *last_pg = pg->next;
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005751
5752 pg->next = tmp_page;
5753 tmp_page = pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005754 } else
5755 last_pg = &pg->next;
5756 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005757 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005758 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)2a5bfe42017-08-31 17:36:51 -04005759
5760 for (pg = tmp_page; pg; pg = tmp_page) {
5761
5762 /* Needs to be called outside of ftrace_lock */
5763 clear_mod_from_hashes(pg);
5764
5765 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5766 free_pages((unsigned long)pg->records, order);
5767 tmp_page = pg->next;
5768 kfree(pg);
5769 }
Steven Rostedt93eb6772009-04-15 13:24:06 -04005770}
5771
Jessica Yu7dcd1822016-02-16 17:32:33 -05005772void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005773{
5774 struct dyn_ftrace *rec;
5775 struct ftrace_page *pg;
5776
5777 mutex_lock(&ftrace_lock);
5778
5779 if (ftrace_disabled)
5780 goto out_unlock;
5781
5782 /*
5783 * If the tracing is enabled, go ahead and enable the record.
5784 *
Hariprasad Kelam9efb85c2019-03-24 00:05:23 +05305785 * The reason not to enable the record immediately is the
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005786 * inherent check of ftrace_make_nop/ftrace_make_call for
5787 * correct previous instructions. Making first the NOP
5788 * conversion puts the module to the correct state, thus
5789 * passing the ftrace_make_call check.
5790 *
5791 * We also delay this to after the module code already set the
5792 * text to read-only, as we now need to set it back to read-write
5793 * so that we can modify the text.
5794 */
5795 if (ftrace_start_up)
5796 ftrace_arch_code_modify_prepare();
5797
5798 do_for_each_ftrace_rec(pg, rec) {
5799 int cnt;
5800 /*
5801 * do_for_each_ftrace_rec() is a double loop.
5802 * module text shares the pg. If a record is
5803 * not part of this module, then skip this pg,
5804 * which the "break" will do.
5805 */
Steven Rostedt (VMware)3e234282017-03-03 18:00:22 -05005806 if (!within_module_core(rec->ip, mod) &&
5807 !within_module_init(rec->ip, mod))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005808 break;
5809
5810 cnt = 0;
5811
5812 /*
5813 * When adding a module, we need to check if tracers are
5814 * currently enabled and if they are, and can trace this record,
5815 * we need to enable the module functions as well as update the
5816 * reference counts for those function records.
5817 */
5818 if (ftrace_start_up)
5819 cnt += referenced_filters(rec);
5820
5821 /* This clears FTRACE_FL_DISABLED */
5822 rec->flags = cnt;
5823
5824 if (ftrace_start_up && cnt) {
5825 int failed = __ftrace_replace_code(rec, 1);
5826 if (failed) {
5827 ftrace_bug(failed, rec);
5828 goto out_loop;
5829 }
5830 }
5831
5832 } while_for_each_ftrace_rec();
5833
5834 out_loop:
5835 if (ftrace_start_up)
5836 ftrace_arch_code_modify_post_process();
5837
5838 out_unlock:
5839 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)d7fbf8d2017-06-26 10:57:21 -04005840
5841 process_cached_mods(mod->name);
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005842}
5843
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005844void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005845{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005846 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005847 return;
5848
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005849 ftrace_process_locs(mod, mod->ftrace_callsites,
5850 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005851}
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005852
5853static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5854 struct dyn_ftrace *rec)
5855{
5856 struct ftrace_mod_func *mod_func;
5857 unsigned long symsize;
5858 unsigned long offset;
5859 char str[KSYM_SYMBOL_LEN];
5860 char *modname;
5861 const char *ret;
5862
5863 ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5864 if (!ret)
5865 return;
5866
5867 mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5868 if (!mod_func)
5869 return;
5870
5871 mod_func->name = kstrdup(str, GFP_KERNEL);
5872 if (!mod_func->name) {
5873 kfree(mod_func);
5874 return;
5875 }
5876
5877 mod_func->ip = rec->ip - offset;
5878 mod_func->size = symsize;
5879
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005880 mod_map->num_funcs++;
5881
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005882 list_add_rcu(&mod_func->list, &mod_map->funcs);
5883}
5884
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005885static struct ftrace_mod_map *
5886allocate_ftrace_mod_map(struct module *mod,
5887 unsigned long start, unsigned long end)
5888{
5889 struct ftrace_mod_map *mod_map;
5890
5891 mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5892 if (!mod_map)
5893 return NULL;
5894
5895 mod_map->mod = mod;
5896 mod_map->start_addr = start;
5897 mod_map->end_addr = end;
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005898 mod_map->num_funcs = 0;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005899
5900 INIT_LIST_HEAD_RCU(&mod_map->funcs);
5901
5902 list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5903
5904 return mod_map;
5905}
5906
5907static const char *
5908ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5909 unsigned long addr, unsigned long *size,
5910 unsigned long *off, char *sym)
5911{
5912 struct ftrace_mod_func *found_func = NULL;
5913 struct ftrace_mod_func *mod_func;
5914
5915 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5916 if (addr >= mod_func->ip &&
5917 addr < mod_func->ip + mod_func->size) {
5918 found_func = mod_func;
5919 break;
5920 }
5921 }
5922
5923 if (found_func) {
5924 if (size)
5925 *size = found_func->size;
5926 if (off)
5927 *off = addr - found_func->ip;
5928 if (sym)
5929 strlcpy(sym, found_func->name, KSYM_NAME_LEN);
5930
5931 return found_func->name;
5932 }
5933
5934 return NULL;
5935}
5936
5937const char *
5938ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
5939 unsigned long *off, char **modname, char *sym)
5940{
5941 struct ftrace_mod_map *mod_map;
5942 const char *ret = NULL;
5943
Paul E. McKenney74401722018-11-06 18:44:52 -08005944 /* mod_map is freed via call_rcu() */
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005945 preempt_disable();
5946 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5947 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
5948 if (ret) {
5949 if (modname)
5950 *modname = mod_map->mod->name;
5951 break;
5952 }
5953 }
5954 preempt_enable();
5955
5956 return ret;
5957}
5958
Steven Rostedt (VMware)6171a032017-09-06 08:40:41 -04005959int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
5960 char *type, char *name,
5961 char *module_name, int *exported)
5962{
5963 struct ftrace_mod_map *mod_map;
5964 struct ftrace_mod_func *mod_func;
5965
5966 preempt_disable();
5967 list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
5968
5969 if (symnum >= mod_map->num_funcs) {
5970 symnum -= mod_map->num_funcs;
5971 continue;
5972 }
5973
5974 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5975 if (symnum > 1) {
5976 symnum--;
5977 continue;
5978 }
5979
5980 *value = mod_func->ip;
5981 *type = 'T';
5982 strlcpy(name, mod_func->name, KSYM_NAME_LEN);
5983 strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
5984 *exported = 1;
5985 preempt_enable();
5986 return 0;
5987 }
5988 WARN_ON(1);
5989 break;
5990 }
5991 preempt_enable();
5992 return -ERANGE;
5993}
5994
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04005995#else
5996static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5997 struct dyn_ftrace *rec) { }
5998static inline struct ftrace_mod_map *
5999allocate_ftrace_mod_map(struct module *mod,
6000 unsigned long start, unsigned long end)
6001{
6002 return NULL;
6003}
Steven Rostedt93eb6772009-04-15 13:24:06 -04006004#endif /* CONFIG_MODULES */
6005
Joel Fernandes8715b102017-10-09 12:29:31 -07006006struct ftrace_init_func {
6007 struct list_head list;
6008 unsigned long ip;
6009};
6010
6011/* Clear any init ips from hashes */
6012static void
6013clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006014{
Joel Fernandes8715b102017-10-09 12:29:31 -07006015 struct ftrace_func_entry *entry;
6016
6017 if (ftrace_hash_empty(hash))
6018 return;
6019
6020 entry = __ftrace_lookup_ip(hash, func->ip);
6021
6022 /*
6023 * Do not allow this rec to match again.
6024 * Yeah, it may waste some memory, but will be removed
6025 * if/when the hash is modified again.
6026 */
6027 if (entry)
6028 entry->ip = 0;
6029}
6030
6031static void
6032clear_func_from_hashes(struct ftrace_init_func *func)
6033{
6034 struct trace_array *tr;
6035
6036 mutex_lock(&trace_types_lock);
6037 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6038 if (!tr->ops || !tr->ops->func_hash)
6039 continue;
6040 mutex_lock(&tr->ops->func_hash->regex_lock);
6041 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6042 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6043 mutex_unlock(&tr->ops->func_hash->regex_lock);
6044 }
6045 mutex_unlock(&trace_types_lock);
6046}
6047
6048static void add_to_clear_hash_list(struct list_head *clear_list,
6049 struct dyn_ftrace *rec)
6050{
6051 struct ftrace_init_func *func;
6052
6053 func = kmalloc(sizeof(*func), GFP_KERNEL);
6054 if (!func) {
6055 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6056 return;
6057 }
6058
6059 func->ip = rec->ip;
6060 list_add(&func->list, clear_list);
6061}
6062
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006063void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006064{
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006065 unsigned long start = (unsigned long)(start_ptr);
6066 unsigned long end = (unsigned long)(end_ptr);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006067 struct ftrace_page **last_pg = &ftrace_pages_start;
6068 struct ftrace_page *pg;
6069 struct dyn_ftrace *rec;
6070 struct dyn_ftrace key;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006071 struct ftrace_mod_map *mod_map = NULL;
Joel Fernandes8715b102017-10-09 12:29:31 -07006072 struct ftrace_init_func *func, *func_next;
6073 struct list_head clear_hash;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006074 int order;
6075
Joel Fernandes8715b102017-10-09 12:29:31 -07006076 INIT_LIST_HEAD(&clear_hash);
6077
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006078 key.ip = start;
6079 key.flags = end; /* overload flags, as it is unsigned long */
6080
6081 mutex_lock(&ftrace_lock);
6082
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006083 /*
6084 * If we are freeing module init memory, then check if
6085 * any tracer is active. If so, we need to save a mapping of
6086 * the module functions being freed with the address.
6087 */
6088 if (mod && ftrace_ops_list != &ftrace_list_end)
6089 mod_map = allocate_ftrace_mod_map(mod, start, end);
6090
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006091 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6092 if (end < pg->records[0].ip ||
6093 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6094 continue;
6095 again:
6096 rec = bsearch(&key, pg->records, pg->index,
6097 sizeof(struct dyn_ftrace),
6098 ftrace_cmp_recs);
6099 if (!rec)
6100 continue;
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006101
Joel Fernandes8715b102017-10-09 12:29:31 -07006102 /* rec will be cleared from hashes after ftrace_lock unlock */
6103 add_to_clear_hash_list(&clear_hash, rec);
6104
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006105 if (mod_map)
6106 save_ftrace_mod_rec(mod_map, rec);
6107
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006108 pg->index--;
Steven Rostedt (VMware)4ec78462017-06-28 11:57:03 -04006109 ftrace_update_tot_cnt--;
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006110 if (!pg->index) {
6111 *last_pg = pg->next;
6112 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6113 free_pages((unsigned long)pg->records, order);
6114 kfree(pg);
6115 pg = container_of(last_pg, struct ftrace_page, next);
6116 if (!(*last_pg))
6117 ftrace_pages = pg;
6118 continue;
6119 }
6120 memmove(rec, rec + 1,
6121 (pg->index - (rec - pg->records)) * sizeof(*rec));
6122 /* More than one function may be in this block */
6123 goto again;
6124 }
6125 mutex_unlock(&ftrace_lock);
Joel Fernandes8715b102017-10-09 12:29:31 -07006126
6127 list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6128 clear_func_from_hashes(func);
6129 kfree(func);
6130 }
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05006131}
6132
Steven Rostedt (VMware)6cafbe12017-06-20 10:44:58 -04006133void __init ftrace_free_init_mem(void)
6134{
6135 void *start = (void *)(&__init_begin);
6136 void *end = (void *)(&__init_end);
6137
Steven Rostedt (VMware)aba4b5c2017-09-01 08:35:38 -04006138 ftrace_free_mem(NULL, start, end);
Steven Rostedt93eb6772009-04-15 13:24:06 -04006139}
6140
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006141void __init ftrace_init(void)
6142{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01006143 extern unsigned long __start_mcount_loc[];
6144 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006145 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006146 int ret;
6147
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006148 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01006149 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006150 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01006151 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006152 goto failed;
6153
6154 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006155 if (!count) {
6156 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006157 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01006158 }
6159
6160 pr_info("ftrace: allocating %ld entries in %ld pages\n",
6161 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006162
6163 last_ftrace_enabled = ftrace_enabled = 1;
6164
Jiri Olsa5cb084b2009-10-13 16:33:53 -04006165 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08006166 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006167 __stop_mcount_loc);
6168
Steven Rostedt2af15d62009-05-28 13:37:24 -04006169 set_ftrace_early_filters();
6170
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006171 return;
6172 failed:
6173 ftrace_disabled = 1;
6174}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04006175
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006176/* Do nothing if arch does not support this */
6177void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6178{
6179}
6180
6181static void ftrace_update_trampoline(struct ftrace_ops *ops)
6182{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006183 arch_ftrace_update_trampoline(ops);
6184}
6185
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006186void ftrace_init_trace_array(struct trace_array *tr)
6187{
6188 INIT_LIST_HEAD(&tr->func_probes);
Steven Rostedt (VMware)673feb92017-06-23 15:26:26 -04006189 INIT_LIST_HEAD(&tr->mod_trace);
6190 INIT_LIST_HEAD(&tr->mod_notrace);
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006191}
Steven Rostedt3d083392008-05-12 21:20:42 +02006192#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006193
Steven Rostedt (VMware)3306fc4a2018-11-15 12:32:38 -05006194struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04006195 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006196 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6197 FTRACE_OPS_FL_INITIALIZED |
6198 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04006199};
6200
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006201static int __init ftrace_nodyn_init(void)
6202{
6203 ftrace_enabled = 1;
6204 return 0;
6205}
Steven Rostedt6f415672012-10-05 12:13:07 -04006206core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01006207
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05006208static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006209static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006210static inline void ftrace_startup_all(int command) { }
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006211
Ingo Molnarc7aafc52008-05-12 21:20:45 +02006212# define ftrace_startup_sysctl() do { } while (0)
6213# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04006214
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006215static void ftrace_update_trampoline(struct ftrace_ops *ops)
6216{
6217}
6218
Steven Rostedt3d083392008-05-12 21:20:42 +02006219#endif /* CONFIG_DYNAMIC_FTRACE */
6220
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006221__init void ftrace_init_global_array_ops(struct trace_array *tr)
6222{
6223 tr->ops = &global_ops;
6224 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04006225 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006226}
6227
6228void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6229{
6230 /* If we filter on pids, update to use the pid function */
6231 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6232 if (WARN_ON(tr->ops->func != ftrace_stub))
6233 printk("ftrace ops had %pS for function\n",
6234 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006235 }
6236 tr->ops->func = func;
6237 tr->ops->private = tr;
6238}
6239
6240void ftrace_reset_array_ops(struct trace_array *tr)
6241{
6242 tr->ops->func = ftrace_stub;
6243}
6244
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006245static nokprobe_inline void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006246__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006247 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04006248{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006249 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006250 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04006251
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006252 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6253 if (bit < 0)
6254 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04006255
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006256 /*
6257 * Some of the ops may be dynamically allocated,
Paul E. McKenney74401722018-11-06 18:44:52 -08006258 * they must be freed after a synchronize_rcu().
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006259 */
6260 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006261
Steven Rostedt0a016402012-11-02 17:03:03 -04006262 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (VMware)2fa717a2019-04-11 11:46:13 -04006263 /* Stub functions don't need to be called nor tested */
6264 if (op->flags & FTRACE_OPS_FL_STUB)
6265 continue;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006266 /*
6267 * Check the following for each ops before calling their func:
6268 * if RCU flag is set, then rcu_is_watching() must be true
6269 * if PER_CPU is set, then ftrace_function_local_disable()
6270 * must be false
6271 * Otherwise test if the ip matches the ops filter
6272 *
6273 * If any of the above fails then the op->func() is not executed.
6274 */
6275 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05006276 ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04006277 if (FTRACE_WARN_ON(!op->func)) {
6278 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006279 goto out;
6280 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04006281 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006282 }
Steven Rostedt0a016402012-11-02 17:03:03 -04006283 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05006284out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006285 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04006286 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04006287}
6288
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006289/*
6290 * Some archs only support passing ip and parent_ip. Even though
6291 * the list function ignores the op parameter, we do not want any
6292 * C side effects, where a function is called without the caller
6293 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006294 * Archs are to support both the regs and ftrace_ops at the same time.
6295 * If they support ftrace_ops, it is assumed they support regs.
6296 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09006297 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6298 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04006299 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08006300 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006301 */
6302#if ARCH_SUPPORTS_FTRACE_OPS
6303static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04006304 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006305{
Steven Rostedta1e2e312011-08-09 12:50:46 -04006306 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006307}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006308NOKPROBE_SYMBOL(ftrace_ops_list_func);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006309#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}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006314NOKPROBE_SYMBOL(ftrace_ops_no_ops);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04006315#endif
6316
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006317/*
6318 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006319 * recursion, needs RCU protection and/or requires per cpu handling, then
6320 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006321 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006322static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006323 struct ftrace_ops *op, struct pt_regs *regs)
6324{
6325 int bit;
6326
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006327 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
6328 return;
6329
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006330 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
6331 if (bit < 0)
6332 return;
6333
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006334 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006335
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006336 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006337
6338 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006339 trace_clear_recursion(bit);
6340}
Masami Hiramatsufabe38a2019-02-24 01:50:20 +09006341NOKPROBE_SYMBOL(ftrace_ops_assist_func);
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04006342
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006343/**
6344 * ftrace_ops_get_func - get the function a trampoline should call
6345 * @ops: the ops to get the function for
6346 *
6347 * Normally the mcount trampoline will call the ops->func, but there
6348 * are times that it should not. For example, if the ops does not
6349 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08006350 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006351 *
6352 * Returns the function that the trampoline should call for @ops.
6353 */
6354ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6355{
6356 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006357 * If the function does not handle recursion, needs to be RCU safe,
6358 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006359 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006360 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
Peter Zijlstrab3a88802017-10-11 09:45:32 +02006361 ops->flags & FTRACE_OPS_FL_RCU)
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05006362 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04006363
6364 return ops->func;
6365}
6366
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006367static void
6368ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6369 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05006370{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006371 struct trace_array *tr = data;
6372 struct trace_pid_list *pid_list;
6373
6374 pid_list = rcu_dereference_sched(tr->function_pids);
6375
6376 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6377 trace_ignore_this_task(pid_list, next));
6378}
6379
Namhyung Kim1e104862017-04-17 11:44:28 +09006380static void
6381ftrace_pid_follow_sched_process_fork(void *data,
6382 struct task_struct *self,
6383 struct task_struct *task)
6384{
6385 struct trace_pid_list *pid_list;
6386 struct trace_array *tr = data;
6387
6388 pid_list = rcu_dereference_sched(tr->function_pids);
6389 trace_filter_add_remove_task(pid_list, self, task);
6390}
6391
6392static void
6393ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6394{
6395 struct trace_pid_list *pid_list;
6396 struct trace_array *tr = data;
6397
6398 pid_list = rcu_dereference_sched(tr->function_pids);
6399 trace_filter_add_remove_task(pid_list, NULL, task);
6400}
6401
6402void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6403{
6404 if (enable) {
6405 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6406 tr);
6407 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6408 tr);
6409 } else {
6410 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6411 tr);
6412 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
6413 tr);
6414 }
6415}
6416
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006417static void clear_ftrace_pids(struct trace_array *tr)
6418{
6419 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05006420 int cpu;
6421
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006422 pid_list = rcu_dereference_protected(tr->function_pids,
6423 lockdep_is_held(&ftrace_lock));
6424 if (!pid_list)
6425 return;
6426
6427 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6428
6429 for_each_possible_cpu(cpu)
6430 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6431
6432 rcu_assign_pointer(tr->function_pids, NULL);
6433
6434 /* Wait till all users are no longer using pid filtering */
Paul E. McKenney74401722018-11-06 18:44:52 -08006435 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006436
6437 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05006438}
6439
Namhyung Kimd879d0b2017-04-17 11:44:27 +09006440void ftrace_clear_pids(struct trace_array *tr)
6441{
6442 mutex_lock(&ftrace_lock);
6443
6444 clear_ftrace_pids(tr);
6445
6446 mutex_unlock(&ftrace_lock);
6447}
6448
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006449static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05006450{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006451 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006452 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006453
6454 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04006455 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006456
6457 mutex_unlock(&ftrace_lock);
6458}
6459
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006460/* Greater than any max PID */
6461#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
6462
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006463static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006464 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006465{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006466 struct trace_pid_list *pid_list;
6467 struct trace_array *tr = m->private;
6468
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006469 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006470 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006471
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006472 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006473
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006474 if (!pid_list)
6475 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6476
6477 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006478}
6479
6480static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6481{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006482 struct trace_array *tr = m->private;
6483 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6484
6485 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006486 return NULL;
6487
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006488 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006489}
6490
6491static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006492 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006493{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006494 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006495 mutex_unlock(&ftrace_lock);
6496}
6497
6498static int fpid_show(struct seq_file *m, void *v)
6499{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006500 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01006501 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006502 return 0;
6503 }
6504
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006505 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006506}
6507
6508static const struct seq_operations ftrace_pid_sops = {
6509 .start = fpid_start,
6510 .next = fpid_next,
6511 .stop = fpid_stop,
6512 .show = fpid_show,
6513};
6514
6515static int
6516ftrace_pid_open(struct inode *inode, struct file *file)
6517{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006518 struct trace_array *tr = inode->i_private;
6519 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006520 int ret = 0;
6521
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006522 if (trace_array_get(tr) < 0)
6523 return -ENODEV;
6524
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006525 if ((file->f_mode & FMODE_WRITE) &&
6526 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006527 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006528
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006529 ret = seq_open(file, &ftrace_pid_sops);
6530 if (ret < 0) {
6531 trace_array_put(tr);
6532 } else {
6533 m = file->private_data;
6534 /* copy tr over to seq ops */
6535 m->private = tr;
6536 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006537
6538 return ret;
6539}
6540
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006541static void ignore_task_cpu(void *data)
6542{
6543 struct trace_array *tr = data;
6544 struct trace_pid_list *pid_list;
6545
6546 /*
6547 * This function is called by on_each_cpu() while the
6548 * event_mutex is held.
6549 */
6550 pid_list = rcu_dereference_protected(tr->function_pids,
6551 mutex_is_locked(&ftrace_lock));
6552
6553 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6554 trace_ignore_this_task(pid_list, current));
6555}
6556
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006557static ssize_t
6558ftrace_pid_write(struct file *filp, const char __user *ubuf,
6559 size_t cnt, loff_t *ppos)
6560{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006561 struct seq_file *m = filp->private_data;
6562 struct trace_array *tr = m->private;
6563 struct trace_pid_list *filtered_pids = NULL;
6564 struct trace_pid_list *pid_list;
6565 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006566
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006567 if (!cnt)
6568 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006569
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006570 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006571
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006572 filtered_pids = rcu_dereference_protected(tr->function_pids,
6573 lockdep_is_held(&ftrace_lock));
6574
6575 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6576 if (ret < 0)
6577 goto out;
6578
6579 rcu_assign_pointer(tr->function_pids, pid_list);
6580
6581 if (filtered_pids) {
Paul E. McKenney74401722018-11-06 18:44:52 -08006582 synchronize_rcu();
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006583 trace_free_pid_list(filtered_pids);
6584 } else if (pid_list) {
6585 /* Register a probe to set whether to ignore the tracing of a task */
6586 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6587 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006588
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006589 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006590 * Ignoring of pids is done at task switch. But we have to
6591 * check for those tasks that are currently running.
6592 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006593 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006594 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006595
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006596 ftrace_update_pid_func();
6597 ftrace_startup_all(0);
6598 out:
6599 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006600
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006601 if (ret > 0)
6602 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05006603
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006604 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006605}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006606
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006607static int
6608ftrace_pid_release(struct inode *inode, struct file *file)
6609{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006610 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006611
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006612 trace_array_put(tr);
6613
6614 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006615}
6616
Steven Rostedt5e2336a2009-03-05 21:44:55 -05006617static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006618 .open = ftrace_pid_open,
6619 .write = ftrace_pid_write,
6620 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05006621 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04006622 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006623};
6624
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006625void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006626{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01006627 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04006628 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006629}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05006630
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04006631void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6632 struct dentry *d_tracer)
6633{
6634 /* Only the top level directory has the dyn_tracefs and profile */
6635 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6636
6637 ftrace_init_dyn_tracefs(d_tracer);
6638 ftrace_profile_tracefs(d_tracer);
6639}
6640
Steven Rostedt3d083392008-05-12 21:20:42 +02006641/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006642 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006643 *
6644 * This function should be used by panic code. It stops ftrace
6645 * but in a not so nice way. If you need to simply kill ftrace
6646 * from a non-atomic section, use ftrace_kill.
6647 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04006648void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006649{
6650 ftrace_disabled = 1;
6651 ftrace_enabled = 0;
Yisheng Xie5ccba642018-02-02 10:14:49 +08006652 ftrace_trace_function = ftrace_stub;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04006653}
6654
6655/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04006656 * Test if ftrace is dead or not.
6657 */
6658int ftrace_is_dead(void)
6659{
6660 return ftrace_disabled;
6661}
6662
6663/**
Steven Rostedt3d083392008-05-12 21:20:42 +02006664 * register_ftrace_function - register a function for profiling
6665 * @ops - ops structure that holds the function for profiling.
6666 *
6667 * Register a function to be called by all functions in the
6668 * kernel.
6669 *
6670 * Note: @ops->func and all the functions it calls must be labeled
6671 * with "notrace", otherwise it will go into a
6672 * recursive loop.
6673 */
6674int register_ftrace_function(struct ftrace_ops *ops)
6675{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006676 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006677
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09006678 ftrace_ops_init(ops);
6679
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006680 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006681
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006682 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04006683
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006684 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02006685
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006686 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02006687}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006688EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02006689
6690/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01006691 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02006692 * @ops - ops structure that holds the function to unregister
6693 *
6694 * Unregister a function that was added to be called by ftrace profiling.
6695 */
6696int unregister_ftrace_function(struct ftrace_ops *ops)
6697{
6698 int ret;
6699
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006700 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006701 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006702 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006703
6704 return ret;
6705}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006706EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006707
Ingo Molnare309b412008-05-12 21:20:51 +02006708int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006709ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006710 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006711 loff_t *ppos)
6712{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006713 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006714
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006715 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006716
Steven Rostedt45a4a232011-04-21 23:16:46 -04006717 if (unlikely(ftrace_disabled))
6718 goto out;
6719
6720 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006721
Li Zefana32c7762009-06-26 16:55:51 +08006722 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006723 goto out;
6724
Li Zefana32c7762009-06-26 16:55:51 +08006725 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006726
6727 if (ftrace_enabled) {
6728
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006729 /* we are starting ftrace again */
Chunyan Zhangf86f4182017-06-07 16:12:51 +08006730 if (rcu_dereference_protected(ftrace_ops_list,
6731 lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
Jan Kiszka5000c412013-03-26 17:53:03 +01006732 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006733
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006734 ftrace_startup_sysctl();
6735
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006736 } else {
6737 /* stopping ftrace calls (just send to ftrace_stub) */
6738 ftrace_trace_function = ftrace_stub;
6739
6740 ftrace_shutdown_sysctl();
6741 }
6742
6743 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006744 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006745 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006746}