blob: 77109b9cf733252a2497669c29bc0f6240ade5a8 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050021#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Masami Hiramatsu25f467d2019-02-24 01:50:20 +090035#include <linux/kprobes.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020036
Steven Rostedtad8d75f2009-04-14 19:39:12 -040037#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040038
Steven Rostedt2af15d62009-05-28 13:37:24 -040039#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053040
Steven Rostedt0706f1c2009-03-23 23:12:58 -040041#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040042#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020043
Steven Rostedt6912896e2008-10-23 09:33:03 -040044#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040045 ({ \
46 int ___r = cond; \
47 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040048 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040049 ___r; \
50 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040051
52#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040053 ({ \
54 int ___r = cond; \
55 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040056 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040057 ___r; \
58 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040059
Steven Rostedt8fc0c702009-02-16 15:28:00 -050060/* hash bits for specific function selection */
61#define FTRACE_HASH_BITS 7
62#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040063#define FTRACE_HASH_DEFAULT_BITS 10
64#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050065
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090066#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040067#define INIT_OPS_HASH(opsname) \
68 .func_hash = &opsname.local_hash, \
69 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040070#define ASSIGN_OPS_HASH(opsname, val) \
71 .func_hash = val, \
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)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040075#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090076#endif
77
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040078static struct ftrace_ops ftrace_list_end __read_mostly = {
79 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040080 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040081 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040082};
83
Steven Rostedt4eebcc82008-05-12 21:20:48 +020084/* ftrace_enabled is a method to turn ftrace on or off */
85int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020086static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020087
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040088/* Current function tracing op */
89struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050090/* What to set function_trace_op to */
91static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050092
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040093static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040094{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040095 struct trace_array *tr;
96
97 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
98 return false;
99
100 tr = ops->private;
101
102 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400103}
104
105static void ftrace_update_trampoline(struct ftrace_ops *ops);
106
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200107/*
108 * ftrace_disabled is set when an anomaly is discovered.
109 * ftrace_disabled is much stronger than ftrace_enabled.
110 */
111static int ftrace_disabled __read_mostly;
112
Steven Rostedt52baf112009-02-14 01:15:39 -0500113static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200114
Steven Rostedtb8489142011-05-04 09:27:52 -0400115static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200116ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400117static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200118
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400119#if ARCH_SUPPORTS_FTRACE_OPS
120static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400121 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122#else
123/* See comment below, where ftrace_ops_list_func is defined */
124static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
125#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
126#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400127
Steven Rostedt0a016402012-11-02 17:03:03 -0400128/*
129 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400130 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400131 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400132 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400133 * concurrent insertions into the ftrace_global_list.
134 *
135 * Silly Alpha and silly pointer-speculation compiler optimizations!
136 */
137#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 do
140
141/*
142 * Optimized for just a single item in the list (as that is the normal case).
143 */
144#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400145 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400146 unlikely((op) != &ftrace_list_end))
147
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900148static inline void ftrace_ops_init(struct ftrace_ops *ops)
149{
150#ifdef CONFIG_DYNAMIC_FTRACE
151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
155 }
156#endif
157}
158
Steven Rostedtea701f12012-07-20 13:08:05 -0400159/**
160 * ftrace_nr_registered_ops - return number of ops registered
161 *
162 * Returns the number of ftrace_ops registered and tracing functions
163 */
164int ftrace_nr_registered_ops(void)
165{
166 struct ftrace_ops *ops;
167 int cnt = 0;
168
169 mutex_lock(&ftrace_lock);
170
171 for (ops = ftrace_ops_list;
172 ops != &ftrace_list_end; ops = ops->next)
173 cnt++;
174
175 mutex_unlock(&ftrace_lock);
176
177 return cnt;
178}
179
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400180static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400181 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500182{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400183 struct trace_array *tr = op->private;
184
185 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500186 return;
187
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400188 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500189}
190
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200192 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200193 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200194 * This NULLs the ftrace function and in essence stops
195 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200197void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198{
Steven Rostedt3d083392008-05-12 21:20:42 +0200199 ftrace_trace_function = ftrace_stub;
200}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500202static void per_cpu_ops_disable_all(struct ftrace_ops *ops)
Jiri Olsae2484912012-02-15 15:51:48 +0100203{
204 int cpu;
205
206 for_each_possible_cpu(cpu)
207 *per_cpu_ptr(ops->disabled, cpu) = 1;
208}
209
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500210static int per_cpu_ops_alloc(struct ftrace_ops *ops)
Jiri Olsae2484912012-02-15 15:51:48 +0100211{
212 int __percpu *disabled;
213
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500214 if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
215 return -EINVAL;
216
Jiri Olsae2484912012-02-15 15:51:48 +0100217 disabled = alloc_percpu(int);
218 if (!disabled)
219 return -ENOMEM;
220
221 ops->disabled = disabled;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500222 per_cpu_ops_disable_all(ops);
Jiri Olsae2484912012-02-15 15:51:48 +0100223 return 0;
224}
225
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500226static void ftrace_sync(struct work_struct *work)
227{
228 /*
229 * This function is just a stub to implement a hard force
230 * of synchronize_sched(). This requires synchronizing
231 * tasks even in userspace and idle.
232 *
233 * Yes, function tracing is rude.
234 */
235}
236
237static void ftrace_sync_ipi(void *data)
238{
239 /* Probably not needed, but do it anyway */
240 smp_rmb();
241}
242
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500243#ifdef CONFIG_FUNCTION_GRAPH_TRACER
244static void update_function_graph_func(void);
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400245
246/* Both enabled by default (can be cleared by function_graph tracer flags */
247static bool fgraph_sleep_time = true;
248static bool fgraph_graph_time = true;
249
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500250#else
251static inline void update_function_graph_func(void) { }
252#endif
253
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100254
255static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
256{
257 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500258 * 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 +0100259 * then it needs to call the list anyway.
260 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500261 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU |
262 FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100263 return ftrace_ops_list_func;
264
265 return ftrace_ops_get_func(ops);
266}
267
Steven Rostedt2b499382011-05-03 22:49:52 -0400268static void update_ftrace_function(void)
269{
270 ftrace_func_t func;
271
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400272 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400273 * Prepare the ftrace_ops that the arch callback will use.
274 * If there's only one ftrace_ops registered, the ftrace_ops_list
275 * will point to the ops we want.
276 */
277 set_function_trace_op = ftrace_ops_list;
278
279 /* If there's no ftrace_ops registered, just call the stub function */
280 if (ftrace_ops_list == &ftrace_list_end) {
281 func = ftrace_stub;
282
283 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400284 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400285 * recursion safe and not dynamic and the arch supports passing ops,
286 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400287 */
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400288 } else if (ftrace_ops_list->next == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100289 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400290
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400291 } else {
292 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500293 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400294 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400295 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400296
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400297 update_function_graph_func();
298
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500299 /* If there's no change, then do nothing more here */
300 if (ftrace_trace_function == func)
301 return;
302
303 /*
304 * If we are using the list function, it doesn't care
305 * about the function_trace_ops.
306 */
307 if (func == ftrace_ops_list_func) {
308 ftrace_trace_function = func;
309 /*
310 * Don't even bother setting function_trace_ops,
311 * it would be racy to do so anyway.
312 */
313 return;
314 }
315
316#ifndef CONFIG_DYNAMIC_FTRACE
317 /*
318 * For static tracing, we need to be a bit more careful.
319 * The function change takes affect immediately. Thus,
320 * we need to coorditate the setting of the function_trace_ops
321 * with the setting of the ftrace_trace_function.
322 *
323 * Set the function to the list ops, which will call the
324 * function we want, albeit indirectly, but it handles the
325 * ftrace_ops and doesn't depend on function_trace_op.
326 */
327 ftrace_trace_function = ftrace_ops_list_func;
328 /*
329 * Make sure all CPUs see this. Yes this is slow, but static
330 * tracing is slow and nasty to have enabled.
331 */
332 schedule_on_each_cpu(ftrace_sync);
333 /* Now all cpus are using the list ops. */
334 function_trace_op = set_function_trace_op;
335 /* Make sure the function_trace_op is visible on all CPUs */
336 smp_wmb();
337 /* Nasty way to force a rmb on all cpus */
338 smp_call_function(ftrace_sync_ipi, NULL, 1);
339 /* OK, we are all set to update the ftrace_trace_function now! */
340#endif /* !CONFIG_DYNAMIC_FTRACE */
341
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400342 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400343}
344
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800345int using_ftrace_ops_list_func(void)
346{
347 return ftrace_trace_function == ftrace_ops_list_func;
348}
349
Steven Rostedt2b499382011-05-03 22:49:52 -0400350static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200351{
Steven Rostedt2b499382011-05-03 22:49:52 -0400352 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200353 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400354 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200355 * CPU might be walking that list. We need to make sure
356 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400357 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200358 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400359 rcu_assign_pointer(*list, ops);
360}
Steven Rostedt3d083392008-05-12 21:20:42 +0200361
Steven Rostedt2b499382011-05-03 22:49:52 -0400362static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
363{
364 struct ftrace_ops **p;
365
366 /*
367 * If we are removing the last function, then simply point
368 * to the ftrace_stub.
369 */
370 if (*list == ops && ops->next == &ftrace_list_end) {
371 *list = &ftrace_list_end;
372 return 0;
373 }
374
375 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
376 if (*p == ops)
377 break;
378
379 if (*p != ops)
380 return -1;
381
382 *p = (*p)->next;
383 return 0;
384}
385
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400386static void ftrace_update_trampoline(struct ftrace_ops *ops);
387
Steven Rostedt2b499382011-05-03 22:49:52 -0400388static int __register_ftrace_function(struct ftrace_ops *ops)
389{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500390 if (ops->flags & FTRACE_OPS_FL_DELETED)
391 return -EINVAL;
392
Steven Rostedtb8489142011-05-04 09:27:52 -0400393 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
394 return -EBUSY;
395
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900396#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400397 /*
398 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
399 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
400 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
401 */
402 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
403 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
404 return -EINVAL;
405
406 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
407 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
408#endif
409
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400410 if (!core_kernel_data((unsigned long)ops))
411 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
412
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500413 if (ops->flags & FTRACE_OPS_FL_PER_CPU) {
414 if (per_cpu_ops_alloc(ops))
Jiri Olsae2484912012-02-15 15:51:48 +0100415 return -ENOMEM;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500416 }
417
418 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400419
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400420 /* Always save the function, and reset at unregistering */
421 ops->saved_func = ops->func;
422
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400423 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400424 ops->func = ftrace_pid_func;
425
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400426 ftrace_update_trampoline(ops);
427
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400428 if (ftrace_enabled)
429 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200430
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200431 return 0;
432}
433
Ingo Molnare309b412008-05-12 21:20:51 +0200434static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200435{
Steven Rostedt2b499382011-05-03 22:49:52 -0400436 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200437
Steven Rostedtb8489142011-05-04 09:27:52 -0400438 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
439 return -EBUSY;
440
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500441 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400442
Steven Rostedt2b499382011-05-03 22:49:52 -0400443 if (ret < 0)
444 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400445
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400446 if (ftrace_enabled)
447 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200448
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400449 ops->func = ops->saved_func;
450
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500451 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200452}
453
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500454static void ftrace_update_pid_func(void)
455{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400456 struct ftrace_ops *op;
457
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400458 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500459 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900460 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500461
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400462 do_for_each_ftrace_op(op, ftrace_ops_list) {
463 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400464 op->func = ftrace_pids_enabled(op) ?
465 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400466 ftrace_update_trampoline(op);
467 }
468 } while_for_each_ftrace_op(op);
469
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400470 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500471}
472
Steven Rostedt493762f2009-03-23 17:12:36 -0400473#ifdef CONFIG_FUNCTION_PROFILER
474struct ftrace_profile {
475 struct hlist_node node;
476 unsigned long ip;
477 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400478#ifdef CONFIG_FUNCTION_GRAPH_TRACER
479 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400480 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400481#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400482};
483
484struct ftrace_profile_page {
485 struct ftrace_profile_page *next;
486 unsigned long index;
487 struct ftrace_profile records[];
488};
489
Steven Rostedtcafb1682009-03-24 20:50:39 -0400490struct ftrace_profile_stat {
491 atomic_t disabled;
492 struct hlist_head *hash;
493 struct ftrace_profile_page *pages;
494 struct ftrace_profile_page *start;
495 struct tracer_stat stat;
496};
497
Steven Rostedt493762f2009-03-23 17:12:36 -0400498#define PROFILE_RECORDS_SIZE \
499 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
500
501#define PROFILES_PER_PAGE \
502 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
503
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400504static int ftrace_profile_enabled __read_mostly;
505
506/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400507static DEFINE_MUTEX(ftrace_profile_lock);
508
Steven Rostedtcafb1682009-03-24 20:50:39 -0400509static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400510
Namhyung Kim20079eb2013-04-10 08:55:50 +0900511#define FTRACE_PROFILE_HASH_BITS 10
512#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400513
Steven Rostedt493762f2009-03-23 17:12:36 -0400514static void *
515function_stat_next(void *v, int idx)
516{
517 struct ftrace_profile *rec = v;
518 struct ftrace_profile_page *pg;
519
520 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
521
522 again:
Li Zefan0296e422009-06-26 11:15:37 +0800523 if (idx != 0)
524 rec++;
525
Steven Rostedt493762f2009-03-23 17:12:36 -0400526 if ((void *)rec >= (void *)&pg->records[pg->index]) {
527 pg = pg->next;
528 if (!pg)
529 return NULL;
530 rec = &pg->records[0];
531 if (!rec->counter)
532 goto again;
533 }
534
535 return rec;
536}
537
538static void *function_stat_start(struct tracer_stat *trace)
539{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400540 struct ftrace_profile_stat *stat =
541 container_of(trace, struct ftrace_profile_stat, stat);
542
543 if (!stat || !stat->start)
544 return NULL;
545
546 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400547}
548
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400549#ifdef CONFIG_FUNCTION_GRAPH_TRACER
550/* function graph compares on total time */
551static int function_stat_cmp(void *p1, void *p2)
552{
553 struct ftrace_profile *a = p1;
554 struct ftrace_profile *b = p2;
555
556 if (a->time < b->time)
557 return -1;
558 if (a->time > b->time)
559 return 1;
560 else
561 return 0;
562}
563#else
564/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400565static int function_stat_cmp(void *p1, void *p2)
566{
567 struct ftrace_profile *a = p1;
568 struct ftrace_profile *b = p2;
569
570 if (a->counter < b->counter)
571 return -1;
572 if (a->counter > b->counter)
573 return 1;
574 else
575 return 0;
576}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400577#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400578
579static int function_stat_headers(struct seq_file *m)
580{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400581#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100582 seq_puts(m, " Function "
583 "Hit Time Avg s^2\n"
584 " -------- "
585 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400586#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100587 seq_puts(m, " Function Hit\n"
588 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400589#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400590 return 0;
591}
592
593static int function_stat_show(struct seq_file *m, void *v)
594{
595 struct ftrace_profile *rec = v;
596 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800597 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400598#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400599 static struct trace_seq s;
600 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400601 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400602#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800603 mutex_lock(&ftrace_profile_lock);
604
605 /* we raced with function_profile_reset() */
606 if (unlikely(rec->counter == 0)) {
607 ret = -EBUSY;
608 goto out;
609 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400610
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530611#ifdef CONFIG_FUNCTION_GRAPH_TRACER
612 avg = rec->time;
613 do_div(avg, rec->counter);
614 if (tracing_thresh && (avg < tracing_thresh))
615 goto out;
616#endif
617
Steven Rostedt493762f2009-03-23 17:12:36 -0400618 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400619 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400620
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400621#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100622 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400623
Chase Douglase330b3b2010-04-26 14:02:05 -0400624 /* Sample standard deviation (s^2) */
625 if (rec->counter <= 1)
626 stddev = 0;
627 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200628 /*
629 * Apply Welford's method:
630 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
631 */
632 stddev = rec->counter * rec->time_squared -
633 rec->time * rec->time;
634
Chase Douglase330b3b2010-04-26 14:02:05 -0400635 /*
636 * Divide only 1000 for ns^2 -> us^2 conversion.
637 * trace_print_graph_duration will divide 1000 again.
638 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200639 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400640 }
641
Steven Rostedt34886c82009-03-25 21:00:47 -0400642 trace_seq_init(&s);
643 trace_print_graph_duration(rec->time, &s);
644 trace_seq_puts(&s, " ");
645 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400646 trace_seq_puts(&s, " ");
647 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400648 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400649#endif
650 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800651out:
652 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400653
Li Zefan3aaba202010-08-23 16:50:12 +0800654 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400655}
656
Steven Rostedtcafb1682009-03-24 20:50:39 -0400657static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400658{
659 struct ftrace_profile_page *pg;
660
Steven Rostedtcafb1682009-03-24 20:50:39 -0400661 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400662
663 while (pg) {
664 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
665 pg->index = 0;
666 pg = pg->next;
667 }
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400670 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
671}
672
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400674{
675 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400676 int functions;
677 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400678 int i;
679
680 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400681 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400682 return 0;
683
Steven Rostedtcafb1682009-03-24 20:50:39 -0400684 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
685 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400686 return -ENOMEM;
687
Steven Rostedt318e0a72009-03-25 20:06:34 -0400688#ifdef CONFIG_DYNAMIC_FTRACE
689 functions = ftrace_update_tot_cnt;
690#else
691 /*
692 * We do not know the number of functions that exist because
693 * dynamic tracing is what counts them. With past experience
694 * we have around 20K functions. That should be more than enough.
695 * It is highly unlikely we will execute every function in
696 * the kernel.
697 */
698 functions = 20000;
699#endif
700
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400702
Steven Rostedt318e0a72009-03-25 20:06:34 -0400703 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
704
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900705 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400706 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400707 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400708 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400709 pg = pg->next;
710 }
711
712 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400713
714 out_free:
715 pg = stat->start;
716 while (pg) {
717 unsigned long tmp = (unsigned long)pg;
718
719 pg = pg->next;
720 free_page(tmp);
721 }
722
Steven Rostedt318e0a72009-03-25 20:06:34 -0400723 stat->pages = NULL;
724 stat->start = NULL;
725
726 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400727}
728
Steven Rostedtcafb1682009-03-24 20:50:39 -0400729static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400730{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400731 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400732 int size;
733
Steven Rostedtcafb1682009-03-24 20:50:39 -0400734 stat = &per_cpu(ftrace_profile_stats, cpu);
735
736 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400737 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400738 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400739 return 0;
740 }
741
742 /*
743 * We are profiling all functions, but usually only a few thousand
744 * functions are hit. We'll make a hash of 1024 items.
745 */
746 size = FTRACE_PROFILE_HASH_SIZE;
747
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400749
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400751 return -ENOMEM;
752
Steven Rostedt318e0a72009-03-25 20:06:34 -0400753 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 if (ftrace_profile_pages_init(stat) < 0) {
755 kfree(stat->hash);
756 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400757 return -ENOMEM;
758 }
759
760 return 0;
761}
762
Steven Rostedtcafb1682009-03-24 20:50:39 -0400763static int ftrace_profile_init(void)
764{
765 int cpu;
766 int ret = 0;
767
Miao Xiec4602c12013-12-16 15:20:01 +0800768 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400769 ret = ftrace_profile_init_cpu(cpu);
770 if (ret)
771 break;
772 }
773
774 return ret;
775}
776
Steven Rostedt493762f2009-03-23 17:12:36 -0400777/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400778static struct ftrace_profile *
779ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400780{
781 struct ftrace_profile *rec;
782 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400783 unsigned long key;
784
Namhyung Kim20079eb2013-04-10 08:55:50 +0900785 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400786 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400787
788 if (hlist_empty(hhd))
789 return NULL;
790
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400791 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400792 if (rec->ip == ip)
793 return rec;
794 }
795
796 return NULL;
797}
798
Steven Rostedtcafb1682009-03-24 20:50:39 -0400799static void ftrace_add_profile(struct ftrace_profile_stat *stat,
800 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400801{
802 unsigned long key;
803
Namhyung Kim20079eb2013-04-10 08:55:50 +0900804 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400805 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400806}
807
Steven Rostedt318e0a72009-03-25 20:06:34 -0400808/*
809 * The memory is already allocated, this simply finds a new record to use.
810 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400811static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400812ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400813{
814 struct ftrace_profile *rec = NULL;
815
Steven Rostedt318e0a72009-03-25 20:06:34 -0400816 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400818 goto out;
819
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400821 * Try to find the function again since an NMI
822 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400825 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400826 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400827
Steven Rostedtcafb1682009-03-24 20:50:39 -0400828 if (stat->pages->index == PROFILES_PER_PAGE) {
829 if (!stat->pages->next)
830 goto out;
831 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400832 }
833
Steven Rostedtcafb1682009-03-24 20:50:39 -0400834 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400836 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400837
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400839 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400840
841 return rec;
842}
843
Steven Rostedt493762f2009-03-23 17:12:36 -0400844static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400845function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400846 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400847{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400849 struct ftrace_profile *rec;
850 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400851
852 if (!ftrace_profile_enabled)
853 return;
854
Steven Rostedt493762f2009-03-23 17:12:36 -0400855 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400856
Christoph Lameterbdffd892014-04-29 14:17:40 -0500857 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400858 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400859 goto out;
860
861 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400862 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400863 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400864 if (!rec)
865 goto out;
866 }
867
868 rec->counter++;
869 out:
870 local_irq_restore(flags);
871}
872
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400873#ifdef CONFIG_FUNCTION_GRAPH_TRACER
874static int profile_graph_entry(struct ftrace_graph_ent *trace)
875{
Namhyung Kim8861dd32016-08-31 11:55:29 +0900876 int index = trace->depth;
877
Steven Rostedta1e2e312011-08-09 12:50:46 -0400878 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900879
Steven Rostedt (VMware)741397d2017-08-17 16:37:25 -0400880 /* If function graph is shutting down, ret_stack can be NULL */
881 if (!current->ret_stack)
882 return 0;
883
Namhyung Kim8861dd32016-08-31 11:55:29 +0900884 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
885 current->ret_stack[index].subtime = 0;
886
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400887 return 1;
888}
889
890static void profile_graph_return(struct ftrace_graph_ret *trace)
891{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400892 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400893 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400894 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400895 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400896
897 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500898 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400899 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 goto out;
901
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400902 /* If the calltime was zero'd ignore it */
903 if (!trace->calltime)
904 goto out;
905
Steven Rostedta2a16d62009-03-24 23:17:58 -0400906 calltime = trace->rettime - trace->calltime;
907
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400908 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400909 int index;
910
911 index = trace->depth;
912
913 /* Append this call time to the parent time to subtract */
914 if (index)
915 current->ret_stack[index - 1].subtime += calltime;
916
917 if (current->ret_stack[index].subtime < calltime)
918 calltime -= current->ret_stack[index].subtime;
919 else
920 calltime = 0;
921 }
922
Steven Rostedtcafb1682009-03-24 20:50:39 -0400923 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400924 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400925 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400926 rec->time_squared += calltime * calltime;
927 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400928
Steven Rostedtcafb1682009-03-24 20:50:39 -0400929 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400930 local_irq_restore(flags);
931}
932
933static int register_ftrace_profiler(void)
934{
935 return register_ftrace_graph(&profile_graph_return,
936 &profile_graph_entry);
937}
938
939static void unregister_ftrace_profiler(void)
940{
941 unregister_ftrace_graph();
942}
943#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100944static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400945 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900946 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400947 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400948};
949
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400950static int register_ftrace_profiler(void)
951{
952 return register_ftrace_function(&ftrace_profile_ops);
953}
954
955static void unregister_ftrace_profiler(void)
956{
957 unregister_ftrace_function(&ftrace_profile_ops);
958}
959#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
960
Steven Rostedt493762f2009-03-23 17:12:36 -0400961static ssize_t
962ftrace_profile_write(struct file *filp, const char __user *ubuf,
963 size_t cnt, loff_t *ppos)
964{
965 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 int ret;
967
Peter Huewe22fe9b52011-06-07 21:58:27 +0200968 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
969 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400970 return ret;
971
972 val = !!val;
973
974 mutex_lock(&ftrace_profile_lock);
975 if (ftrace_profile_enabled ^ val) {
976 if (val) {
977 ret = ftrace_profile_init();
978 if (ret < 0) {
979 cnt = ret;
980 goto out;
981 }
982
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400983 ret = register_ftrace_profiler();
984 if (ret < 0) {
985 cnt = ret;
986 goto out;
987 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400988 ftrace_profile_enabled = 1;
989 } else {
990 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400991 /*
992 * unregister_ftrace_profiler calls stop_machine
993 * so this acts like an synchronize_sched.
994 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400995 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400996 }
997 }
998 out:
999 mutex_unlock(&ftrace_profile_lock);
1000
Jiri Olsacf8517c2009-10-23 19:36:16 -04001001 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001002
1003 return cnt;
1004}
1005
1006static ssize_t
1007ftrace_profile_read(struct file *filp, char __user *ubuf,
1008 size_t cnt, loff_t *ppos)
1009{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001010 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001011 int r;
1012
1013 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1014 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1015}
1016
1017static const struct file_operations ftrace_profile_fops = {
1018 .open = tracing_open_generic,
1019 .read = ftrace_profile_read,
1020 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001021 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001022};
1023
Steven Rostedtcafb1682009-03-24 20:50:39 -04001024/* used to initialize the real stat files */
1025static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001026 .name = "functions",
1027 .stat_start = function_stat_start,
1028 .stat_next = function_stat_next,
1029 .stat_cmp = function_stat_cmp,
1030 .stat_headers = function_stat_headers,
1031 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001032};
1033
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001034static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001035{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001036 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001037 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001038 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001039 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001040 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001041
Steven Rostedtcafb1682009-03-24 20:50:39 -04001042 for_each_possible_cpu(cpu) {
1043 stat = &per_cpu(ftrace_profile_stats, cpu);
1044
Geliang Tang6363c6b2016-03-15 22:12:34 +08001045 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -04001046 if (!name) {
1047 /*
1048 * The files created are permanent, if something happens
1049 * we still do not free memory.
1050 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001051 WARN(1,
1052 "Could not allocate stat file for cpu %d\n",
1053 cpu);
1054 return;
1055 }
1056 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001057 stat->stat.name = name;
1058 ret = register_stat_tracer(&stat->stat);
1059 if (ret) {
1060 WARN(1,
1061 "Could not register function stat for cpu %d\n",
1062 cpu);
1063 kfree(name);
1064 return;
1065 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001066 }
1067
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001068 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001069 d_tracer, NULL, &ftrace_profile_fops);
1070 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001071 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001072}
1073
1074#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001075static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001076{
1077}
1078#endif /* CONFIG_FUNCTION_PROFILER */
1079
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001080static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1081
Pratyush Anand1619dc32015-03-06 23:58:06 +05301082#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1083static int ftrace_graph_active;
1084#else
1085# define ftrace_graph_active 0
1086#endif
1087
Steven Rostedt3d083392008-05-12 21:20:42 +02001088#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001089
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001090static struct ftrace_ops *removed_ops;
1091
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001092/*
1093 * Set when doing a global update, like enabling all recs or disabling them.
1094 * It is not set when just updating a single ftrace_ops.
1095 */
1096static bool update_all_ops;
1097
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001098#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001099# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001100#endif
1101
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001102static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1103
Steven Rostedtb6887d72009-02-17 12:32:04 -05001104struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001105 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001106 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001107 unsigned long flags;
1108 unsigned long ip;
1109 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001110 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001111};
1112
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001113struct ftrace_func_entry {
1114 struct hlist_node hlist;
1115 unsigned long ip;
1116};
1117
1118struct ftrace_hash {
1119 unsigned long size_bits;
1120 struct hlist_head *buckets;
1121 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001122 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123};
1124
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001125/*
1126 * We make these constant because no one should touch them,
1127 * but they are used as the default "empty hash", to avoid allocating
1128 * it all the time. These are in a read only section such that if
1129 * anyone does try to modify it, it will cause an exception.
1130 */
1131static const struct hlist_head empty_buckets[1];
1132static const struct ftrace_hash empty_hash = {
1133 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001134};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001135#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001136
Steven Rostedt2b499382011-05-03 22:49:52 -04001137static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001138 .func = ftrace_stub,
1139 .local_hash.notrace_hash = EMPTY_HASH,
1140 .local_hash.filter_hash = EMPTY_HASH,
1141 INIT_OPS_HASH(global_ops)
1142 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001143 FTRACE_OPS_FL_INITIALIZED |
1144 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001145};
1146
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001147/*
1148 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001149 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001150 * not return true for either core_kernel_text() or
1151 * is_module_text_address().
1152 */
1153bool is_ftrace_trampoline(unsigned long addr)
1154{
1155 struct ftrace_ops *op;
1156 bool ret = false;
1157
1158 /*
1159 * Some of the ops may be dynamically allocated,
1160 * they are freed after a synchronize_sched().
1161 */
1162 preempt_disable_notrace();
1163
1164 do_for_each_ftrace_op(op, ftrace_ops_list) {
1165 /*
1166 * This is to check for dynamically allocated trampolines.
1167 * Trampolines that are in kernel text will have
1168 * core_kernel_text() return true.
1169 */
1170 if (op->trampoline && op->trampoline_size)
1171 if (addr >= op->trampoline &&
1172 addr < op->trampoline + op->trampoline_size) {
1173 ret = true;
1174 goto out;
1175 }
1176 } while_for_each_ftrace_op(op);
1177
1178 out:
1179 preempt_enable_notrace();
1180
1181 return ret;
1182}
1183
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001184struct ftrace_page {
1185 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001186 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001187 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001188 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001189};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001190
Steven Rostedta7900872011-12-16 16:23:44 -05001191#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1192#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001193
1194/* estimate from running different kernels */
1195#define NR_TO_INIT 10000
1196
1197static struct ftrace_page *ftrace_pages_start;
1198static struct ftrace_page *ftrace_pages;
1199
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001200static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001201{
1202 return !hash || !hash->count;
1203}
1204
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001205static struct ftrace_func_entry *
1206ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1207{
1208 unsigned long key;
1209 struct ftrace_func_entry *entry;
1210 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001211
Steven Rostedt06a51d92011-12-19 19:07:36 -05001212 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001213 return NULL;
1214
1215 if (hash->size_bits > 0)
1216 key = hash_long(ip, hash->size_bits);
1217 else
1218 key = 0;
1219
1220 hhd = &hash->buckets[key];
1221
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001222 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001223 if (entry->ip == ip)
1224 return entry;
1225 }
1226 return NULL;
1227}
1228
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001229static void __add_hash_entry(struct ftrace_hash *hash,
1230 struct ftrace_func_entry *entry)
1231{
1232 struct hlist_head *hhd;
1233 unsigned long key;
1234
1235 if (hash->size_bits)
1236 key = hash_long(entry->ip, hash->size_bits);
1237 else
1238 key = 0;
1239
1240 hhd = &hash->buckets[key];
1241 hlist_add_head(&entry->hlist, hhd);
1242 hash->count++;
1243}
1244
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001245static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1246{
1247 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001248
1249 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1250 if (!entry)
1251 return -ENOMEM;
1252
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001253 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001254 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001255
1256 return 0;
1257}
1258
1259static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001260free_hash_entry(struct ftrace_hash *hash,
1261 struct ftrace_func_entry *entry)
1262{
1263 hlist_del(&entry->hlist);
1264 kfree(entry);
1265 hash->count--;
1266}
1267
1268static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001269remove_hash_entry(struct ftrace_hash *hash,
1270 struct ftrace_func_entry *entry)
1271{
1272 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001273 hash->count--;
1274}
1275
1276static void ftrace_hash_clear(struct ftrace_hash *hash)
1277{
1278 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001279 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001280 struct ftrace_func_entry *entry;
1281 int size = 1 << hash->size_bits;
1282 int i;
1283
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001284 if (!hash->count)
1285 return;
1286
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001287 for (i = 0; i < size; i++) {
1288 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001289 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001290 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001291 }
1292 FTRACE_WARN_ON(hash->count);
1293}
1294
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001295static void free_ftrace_hash(struct ftrace_hash *hash)
1296{
1297 if (!hash || hash == EMPTY_HASH)
1298 return;
1299 ftrace_hash_clear(hash);
1300 kfree(hash->buckets);
1301 kfree(hash);
1302}
1303
Steven Rostedt07fd5512011-05-05 18:03:47 -04001304static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1305{
1306 struct ftrace_hash *hash;
1307
1308 hash = container_of(rcu, struct ftrace_hash, rcu);
1309 free_ftrace_hash(hash);
1310}
1311
1312static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1313{
1314 if (!hash || hash == EMPTY_HASH)
1315 return;
1316 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1317}
1318
Jiri Olsa5500fa52012-02-15 15:51:54 +01001319void ftrace_free_filter(struct ftrace_ops *ops)
1320{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001321 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001322 free_ftrace_hash(ops->func_hash->filter_hash);
1323 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001324}
1325
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1327{
1328 struct ftrace_hash *hash;
1329 int size;
1330
1331 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1332 if (!hash)
1333 return NULL;
1334
1335 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001336 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337
1338 if (!hash->buckets) {
1339 kfree(hash);
1340 return NULL;
1341 }
1342
1343 hash->size_bits = size_bits;
1344
1345 return hash;
1346}
1347
1348static struct ftrace_hash *
1349alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1350{
1351 struct ftrace_func_entry *entry;
1352 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001353 int size;
1354 int ret;
1355 int i;
1356
1357 new_hash = alloc_ftrace_hash(size_bits);
1358 if (!new_hash)
1359 return NULL;
1360
1361 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001362 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001363 return new_hash;
1364
1365 size = 1 << hash->size_bits;
1366 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001367 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001368 ret = add_hash_entry(new_hash, entry->ip);
1369 if (ret < 0)
1370 goto free_hash;
1371 }
1372 }
1373
1374 FTRACE_WARN_ON(new_hash->count != hash->count);
1375
1376 return new_hash;
1377
1378 free_hash:
1379 free_ftrace_hash(new_hash);
1380 return NULL;
1381}
1382
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001383static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001384ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001385static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001386ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001387
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001388static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1389 struct ftrace_hash *new_hash);
1390
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001391static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001392ftrace_hash_move(struct ftrace_ops *ops, int enable,
1393 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001394{
1395 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001396 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001397 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001398 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399 int size = src->count;
1400 int bits = 0;
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001401 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001402 int i;
1403
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001404 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1405 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1406 return -EINVAL;
1407
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001408 /*
1409 * If the new source is empty, just free dst and assign it
1410 * the empty_hash.
1411 */
1412 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001413 new_hash = EMPTY_HASH;
1414 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001415 }
1416
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001417 /*
1418 * Make the hash size about 1/2 the # found
1419 */
1420 for (size /= 2; size; size >>= 1)
1421 bits++;
1422
1423 /* Don't allocate too much */
1424 if (bits > FTRACE_HASH_MAX_BITS)
1425 bits = FTRACE_HASH_MAX_BITS;
1426
Steven Rostedt07fd5512011-05-05 18:03:47 -04001427 new_hash = alloc_ftrace_hash(bits);
1428 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001429 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001430
1431 size = 1 << src->size_bits;
1432 for (i = 0; i < size; i++) {
1433 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001434 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001435 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001436 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001437 }
1438 }
1439
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001440update:
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001441 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1442 if (enable) {
1443 /* IPMODIFY should be updated only when filter_hash updating */
1444 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1445 if (ret < 0) {
1446 free_ftrace_hash(new_hash);
1447 return ret;
1448 }
1449 }
1450
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001451 /*
1452 * Remove the current set, update the hash and add
1453 * them back.
1454 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001455 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001456
Steven Rostedt07fd5512011-05-05 18:03:47 -04001457 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001458
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001459 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001460
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001461 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001462}
1463
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001464static bool hash_contains_ip(unsigned long ip,
1465 struct ftrace_ops_hash *hash)
1466{
1467 /*
1468 * The function record is a match if it exists in the filter
1469 * hash and not in the notrace hash. Note, an emty hash is
1470 * considered a match for the filter hash, but an empty
1471 * notrace hash is considered not in the notrace hash.
1472 */
1473 return (ftrace_hash_empty(hash->filter_hash) ||
1474 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1475 (ftrace_hash_empty(hash->notrace_hash) ||
1476 !ftrace_lookup_ip(hash->notrace_hash, ip));
1477}
1478
Steven Rostedt265c8312009-02-13 12:43:56 -05001479/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001480 * Test the hashes for this ops to see if we want to call
1481 * the ops->func or not.
1482 *
1483 * It's a match if the ip is in the ops->filter_hash or
1484 * the filter_hash does not exist or is empty,
1485 * AND
1486 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001487 *
1488 * This needs to be called with preemption disabled as
1489 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001490 */
1491static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001492ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001493{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001494 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001495 int ret;
1496
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001497#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1498 /*
1499 * There's a small race when adding ops that the ftrace handler
1500 * that wants regs, may be called without them. We can not
1501 * allow that handler to be called if regs is NULL.
1502 */
1503 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1504 return 0;
1505#endif
1506
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001507 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1508 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001509
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001510 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001511 ret = 1;
1512 else
1513 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001514
1515 return ret;
1516}
1517
1518/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001519 * This is a double for. Do not use 'break' to break out of the loop,
1520 * you must use a goto.
1521 */
1522#define do_for_each_ftrace_rec(pg, rec) \
1523 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1524 int _____i; \
1525 for (_____i = 0; _____i < pg->index; _____i++) { \
1526 rec = &pg->records[_____i];
1527
1528#define while_for_each_ftrace_rec() \
1529 } \
1530 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301531
Steven Rostedt5855fea2011-12-16 19:27:42 -05001532
1533static int ftrace_cmp_recs(const void *a, const void *b)
1534{
Steven Rostedta650e022012-04-25 13:48:13 -04001535 const struct dyn_ftrace *key = a;
1536 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001537
Steven Rostedta650e022012-04-25 13:48:13 -04001538 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001539 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001540 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1541 return 1;
1542 return 0;
1543}
1544
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001545/**
1546 * ftrace_location_range - return the first address of a traced location
1547 * if it touches the given ip range
1548 * @start: start of range to search.
1549 * @end: end of range to search (inclusive). @end points to the last byte
1550 * to check.
1551 *
1552 * Returns rec->ip if the related ftrace location is a least partly within
1553 * the given address range. That is, the first address of the instruction
1554 * that is either a NOP or call to the function tracer. It checks the ftrace
1555 * internal tables to determine if the address belongs or not.
1556 */
1557unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001558{
1559 struct ftrace_page *pg;
1560 struct dyn_ftrace *rec;
1561 struct dyn_ftrace key;
1562
1563 key.ip = start;
1564 key.flags = end; /* overload flags, as it is unsigned long */
1565
1566 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1567 if (end < pg->records[0].ip ||
1568 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1569 continue;
1570 rec = bsearch(&key, pg->records, pg->index,
1571 sizeof(struct dyn_ftrace),
1572 ftrace_cmp_recs);
1573 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001574 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001575 }
1576
Steven Rostedt5855fea2011-12-16 19:27:42 -05001577 return 0;
1578}
1579
Steven Rostedtc88fd862011-08-16 09:53:39 -04001580/**
1581 * ftrace_location - return true if the ip giving is a traced location
1582 * @ip: the instruction pointer to check
1583 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001584 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001585 * That is, the instruction that is either a NOP or call to
1586 * the function tracer. It checks the ftrace internal tables to
1587 * determine if the address belongs or not.
1588 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001589unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001590{
Steven Rostedta650e022012-04-25 13:48:13 -04001591 return ftrace_location_range(ip, ip);
1592}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001593
Steven Rostedta650e022012-04-25 13:48:13 -04001594/**
1595 * ftrace_text_reserved - return true if range contains an ftrace location
1596 * @start: start of range to search
1597 * @end: end of range to search (inclusive). @end points to the last byte to check.
1598 *
1599 * Returns 1 if @start and @end contains a ftrace location.
1600 * That is, the instruction that is either a NOP or call to
1601 * the function tracer. It checks the ftrace internal tables to
1602 * determine if the address belongs or not.
1603 */
Sasha Levind88471c2013-01-09 18:09:20 -05001604int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001605{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001606 unsigned long ret;
1607
1608 ret = ftrace_location_range((unsigned long)start,
1609 (unsigned long)end);
1610
1611 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001612}
1613
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001614/* Test if ops registered to this rec needs regs */
1615static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1616{
1617 struct ftrace_ops *ops;
1618 bool keep_regs = false;
1619
1620 for (ops = ftrace_ops_list;
1621 ops != &ftrace_list_end; ops = ops->next) {
1622 /* pass rec in as regs to have non-NULL val */
1623 if (ftrace_ops_test(ops, rec->ip, rec)) {
1624 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1625 keep_regs = true;
1626 break;
1627 }
1628 }
1629 }
1630
1631 return keep_regs;
1632}
1633
Cheng Jian81c09ba2019-05-04 19:39:39 +08001634static struct ftrace_ops *
1635ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1636static struct ftrace_ops *
1637ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1638
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001639static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001640 int filter_hash,
1641 bool inc)
1642{
1643 struct ftrace_hash *hash;
1644 struct ftrace_hash *other_hash;
1645 struct ftrace_page *pg;
1646 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001647 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001648 int count = 0;
1649 int all = 0;
1650
1651 /* Only update if the ops has been registered */
1652 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001653 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001654
1655 /*
1656 * In the filter_hash case:
1657 * If the count is zero, we update all records.
1658 * Otherwise we just update the items in the hash.
1659 *
1660 * In the notrace_hash case:
1661 * We enable the update in the hash.
1662 * As disabling notrace means enabling the tracing,
1663 * and enabling notrace means disabling, the inc variable
1664 * gets inversed.
1665 */
1666 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001667 hash = ops->func_hash->filter_hash;
1668 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001669 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001670 all = 1;
1671 } else {
1672 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001673 hash = ops->func_hash->notrace_hash;
1674 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001675 /*
1676 * If the notrace hash has no items,
1677 * then there's nothing to do.
1678 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001679 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001680 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001681 }
1682
1683 do_for_each_ftrace_rec(pg, rec) {
1684 int in_other_hash = 0;
1685 int in_hash = 0;
1686 int match = 0;
1687
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001688 if (rec->flags & FTRACE_FL_DISABLED)
1689 continue;
1690
Steven Rostedted926f92011-05-03 13:25:24 -04001691 if (all) {
1692 /*
1693 * Only the filter_hash affects all records.
1694 * Update if the record is not in the notrace hash.
1695 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001696 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001697 match = 1;
1698 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001699 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1700 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001701
1702 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001703 * If filter_hash is set, we want to match all functions
1704 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001705 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001706 * If filter_hash is not set, then we are decrementing.
1707 * That means we match anything that is in the hash
1708 * and also in the other_hash. That is, we need to turn
1709 * off functions in the other hash because they are disabled
1710 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001711 */
1712 if (filter_hash && in_hash && !in_other_hash)
1713 match = 1;
1714 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001715 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001716 match = 1;
1717 }
1718 if (!match)
1719 continue;
1720
1721 if (inc) {
1722 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001723 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001724 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001725
1726 /*
1727 * If there's only a single callback registered to a
1728 * function, and the ops has a trampoline registered
1729 * for it, then we can call it directly.
1730 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001731 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001732 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001733 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001734 /*
1735 * If we are adding another function callback
1736 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001737 * custom trampoline in use, then we need to go
1738 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001739 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001740 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001741
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001742 /*
1743 * If any ops wants regs saved for this function
1744 * then all ops will get saved regs.
1745 */
1746 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1747 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001748 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001749 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001750 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001751 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001752
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001753 /*
1754 * If the rec had REGS enabled and the ops that is
1755 * being removed had REGS set, then see if there is
1756 * still any ops for this record that wants regs.
1757 * If not, we can stop recording them.
1758 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001759 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001760 rec->flags & FTRACE_FL_REGS &&
1761 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1762 if (!test_rec_ops_needs_regs(rec))
1763 rec->flags &= ~FTRACE_FL_REGS;
1764 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001765
1766 /*
Cheng Jian81c09ba2019-05-04 19:39:39 +08001767 * The TRAMP needs to be set only if rec count
1768 * is decremented to one, and the ops that is
1769 * left has a trampoline. As TRAMP can only be
1770 * enabled if there is only a single ops attached
1771 * to it.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001772 */
Cheng Jian81c09ba2019-05-04 19:39:39 +08001773 if (ftrace_rec_count(rec) == 1 &&
1774 ftrace_find_tramp_ops_any(rec))
1775 rec->flags |= FTRACE_FL_TRAMP;
1776 else
1777 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001778
1779 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001780 * flags will be cleared in ftrace_check_record()
1781 * if rec count is zero.
1782 */
Steven Rostedted926f92011-05-03 13:25:24 -04001783 }
1784 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001785
1786 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1787 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1788
Steven Rostedted926f92011-05-03 13:25:24 -04001789 /* Shortcut, if we handled all records, we are done. */
1790 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001791 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001792 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001793
1794 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001795}
1796
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001797static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001798 int filter_hash)
1799{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001800 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001801}
1802
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001803static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001804 int filter_hash)
1805{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001806 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001807}
1808
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001809static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1810 int filter_hash, int inc)
1811{
1812 struct ftrace_ops *op;
1813
1814 __ftrace_hash_rec_update(ops, filter_hash, inc);
1815
1816 if (ops->func_hash != &global_ops.local_hash)
1817 return;
1818
1819 /*
1820 * If the ops shares the global_ops hash, then we need to update
1821 * all ops that are enabled and use this hash.
1822 */
1823 do_for_each_ftrace_op(op, ftrace_ops_list) {
1824 /* Already done */
1825 if (op == ops)
1826 continue;
1827 if (op->func_hash == &global_ops.local_hash)
1828 __ftrace_hash_rec_update(op, filter_hash, inc);
1829 } while_for_each_ftrace_op(op);
1830}
1831
1832static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1833 int filter_hash)
1834{
1835 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1836}
1837
1838static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1839 int filter_hash)
1840{
1841 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1842}
1843
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001844/*
1845 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1846 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1847 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1848 * Note that old_hash and new_hash has below meanings
1849 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1850 * - If the hash is EMPTY_HASH, it hits nothing
1851 * - Anything else hits the recs which match the hash entries.
1852 */
1853static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1854 struct ftrace_hash *old_hash,
1855 struct ftrace_hash *new_hash)
1856{
1857 struct ftrace_page *pg;
1858 struct dyn_ftrace *rec, *end = NULL;
1859 int in_old, in_new;
1860
1861 /* Only update if the ops has been registered */
1862 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1863 return 0;
1864
1865 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1866 return 0;
1867
1868 /*
1869 * Since the IPMODIFY is a very address sensitive action, we do not
1870 * allow ftrace_ops to set all functions to new hash.
1871 */
1872 if (!new_hash || !old_hash)
1873 return -EINVAL;
1874
1875 /* Update rec->flags */
1876 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001877
1878 if (rec->flags & FTRACE_FL_DISABLED)
1879 continue;
1880
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001881 /* We need to update only differences of filter_hash */
1882 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1883 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1884 if (in_old == in_new)
1885 continue;
1886
1887 if (in_new) {
1888 /* New entries must ensure no others are using it */
1889 if (rec->flags & FTRACE_FL_IPMODIFY)
1890 goto rollback;
1891 rec->flags |= FTRACE_FL_IPMODIFY;
1892 } else /* Removed entry */
1893 rec->flags &= ~FTRACE_FL_IPMODIFY;
1894 } while_for_each_ftrace_rec();
1895
1896 return 0;
1897
1898rollback:
1899 end = rec;
1900
1901 /* Roll back what we did above */
1902 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001903
1904 if (rec->flags & FTRACE_FL_DISABLED)
1905 continue;
1906
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001907 if (rec == end)
1908 goto err_out;
1909
1910 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1911 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1912 if (in_old == in_new)
1913 continue;
1914
1915 if (in_new)
1916 rec->flags &= ~FTRACE_FL_IPMODIFY;
1917 else
1918 rec->flags |= FTRACE_FL_IPMODIFY;
1919 } while_for_each_ftrace_rec();
1920
1921err_out:
1922 return -EBUSY;
1923}
1924
1925static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1926{
1927 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1928
1929 if (ftrace_hash_empty(hash))
1930 hash = NULL;
1931
1932 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1933}
1934
1935/* Disabling always succeeds */
1936static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1937{
1938 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1939
1940 if (ftrace_hash_empty(hash))
1941 hash = NULL;
1942
1943 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1944}
1945
1946static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1947 struct ftrace_hash *new_hash)
1948{
1949 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1950
1951 if (ftrace_hash_empty(old_hash))
1952 old_hash = NULL;
1953
1954 if (ftrace_hash_empty(new_hash))
1955 new_hash = NULL;
1956
1957 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1958}
1959
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001960static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001961{
1962 int i;
1963
1964 printk(KERN_CONT "%s", fmt);
1965
1966 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1967 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1968}
1969
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001970enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001971const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001972
1973static void print_bug_type(void)
1974{
1975 switch (ftrace_bug_type) {
1976 case FTRACE_BUG_UNKNOWN:
1977 break;
1978 case FTRACE_BUG_INIT:
1979 pr_info("Initializing ftrace call sites\n");
1980 break;
1981 case FTRACE_BUG_NOP:
1982 pr_info("Setting ftrace call site to NOP\n");
1983 break;
1984 case FTRACE_BUG_CALL:
1985 pr_info("Setting ftrace call site to call ftrace function\n");
1986 break;
1987 case FTRACE_BUG_UPDATE:
1988 pr_info("Updating ftrace call site to call a different ftrace function\n");
1989 break;
1990 }
1991}
1992
Steven Rostedtc88fd862011-08-16 09:53:39 -04001993/**
1994 * ftrace_bug - report and shutdown function tracer
1995 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001996 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001997 *
1998 * The arch code that enables or disables the function tracing
1999 * can call ftrace_bug() when it has detected a problem in
2000 * modifying the code. @failed should be one of either:
2001 * EFAULT - if the problem happens on reading the @ip address
2002 * EINVAL - if what is read at @ip is not what was expected
2003 * EPERM - if the problem happens on writting to the @ip address
2004 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002005void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002006{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002007 unsigned long ip = rec ? rec->ip : 0;
2008
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002009 switch (failed) {
2010 case -EFAULT:
2011 FTRACE_WARN_ON_ONCE(1);
2012 pr_info("ftrace faulted on modifying ");
2013 print_ip_sym(ip);
2014 break;
2015 case -EINVAL:
2016 FTRACE_WARN_ON_ONCE(1);
2017 pr_info("ftrace failed to modify ");
2018 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002019 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002020 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002021 if (ftrace_expected) {
2022 print_ip_ins(" expected: ", ftrace_expected);
2023 pr_cont("\n");
2024 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002025 break;
2026 case -EPERM:
2027 FTRACE_WARN_ON_ONCE(1);
2028 pr_info("ftrace faulted on writing ");
2029 print_ip_sym(ip);
2030 break;
2031 default:
2032 FTRACE_WARN_ON_ONCE(1);
2033 pr_info("ftrace faulted on unknown error ");
2034 print_ip_sym(ip);
2035 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002036 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002037 if (rec) {
2038 struct ftrace_ops *ops = NULL;
2039
2040 pr_info("ftrace record flags: %lx\n", rec->flags);
2041 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2042 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2043 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2044 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002045 if (ops) {
2046 do {
2047 pr_cont("\ttramp: %pS (%pS)",
2048 (void *)ops->trampoline,
2049 (void *)ops->func);
2050 ops = ftrace_find_tramp_ops_next(rec, ops);
2051 } while (ops);
2052 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002053 pr_cont("\ttramp: ERROR!");
2054
2055 }
2056 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002057 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002058 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002059}
2060
Steven Rostedtc88fd862011-08-16 09:53:39 -04002061static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002062{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002063 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002064
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002065 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2066
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002067 if (rec->flags & FTRACE_FL_DISABLED)
2068 return FTRACE_UPDATE_IGNORE;
2069
Steven Rostedt982c3502008-11-15 16:31:41 -05002070 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002071 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002072 *
Steven Rostedted926f92011-05-03 13:25:24 -04002073 * If the record has a ref count, then we need to enable it
2074 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002075 *
Steven Rostedted926f92011-05-03 13:25:24 -04002076 * Otherwise we make sure its disabled.
2077 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002078 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002079 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002080 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002081 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002082 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002083
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002084 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002085 * If enabling and the REGS flag does not match the REGS_EN, or
2086 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2087 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002088 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002089 if (flag) {
2090 if (!(rec->flags & FTRACE_FL_REGS) !=
2091 !(rec->flags & FTRACE_FL_REGS_EN))
2092 flag |= FTRACE_FL_REGS;
2093
2094 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2095 !(rec->flags & FTRACE_FL_TRAMP_EN))
2096 flag |= FTRACE_FL_TRAMP;
2097 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002098
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002099 /* If the state of this record hasn't changed, then do nothing */
2100 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002101 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002102
2103 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002104 /* Save off if rec is being enabled (for return value) */
2105 flag ^= rec->flags & FTRACE_FL_ENABLED;
2106
2107 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002108 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002109 if (flag & FTRACE_FL_REGS) {
2110 if (rec->flags & FTRACE_FL_REGS)
2111 rec->flags |= FTRACE_FL_REGS_EN;
2112 else
2113 rec->flags &= ~FTRACE_FL_REGS_EN;
2114 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002115 if (flag & FTRACE_FL_TRAMP) {
2116 if (rec->flags & FTRACE_FL_TRAMP)
2117 rec->flags |= FTRACE_FL_TRAMP_EN;
2118 else
2119 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2120 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002121 }
2122
2123 /*
2124 * If this record is being updated from a nop, then
2125 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002126 * Otherwise,
2127 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002128 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002129 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002130 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002131 if (flag & FTRACE_FL_ENABLED) {
2132 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002133 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002134 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002135
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002136 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002137 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002138 }
2139
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002140 if (update) {
2141 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002142 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002143 rec->flags = 0;
2144 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002145 /*
2146 * Just disable the record, but keep the ops TRAMP
2147 * and REGS states. The _EN flags must be disabled though.
2148 */
2149 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2150 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002151 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002152
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002153 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002154 return FTRACE_UPDATE_MAKE_NOP;
2155}
2156
2157/**
2158 * ftrace_update_record, set a record that now is tracing or not
2159 * @rec: the record to update
2160 * @enable: set to 1 if the record is tracing, zero to force disable
2161 *
2162 * The records that represent all functions that can be traced need
2163 * to be updated when tracing has been enabled.
2164 */
2165int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2166{
2167 return ftrace_check_record(rec, enable, 1);
2168}
2169
2170/**
2171 * ftrace_test_record, check if the record has been enabled or not
2172 * @rec: the record to test
2173 * @enable: set to 1 to check if enabled, 0 if it is disabled
2174 *
2175 * The arch code may need to test if a record is already set to
2176 * tracing to determine how to modify the function code that it
2177 * represents.
2178 */
2179int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2180{
2181 return ftrace_check_record(rec, enable, 0);
2182}
2183
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002184static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002185ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2186{
2187 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002188 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002189
2190 do_for_each_ftrace_op(op, ftrace_ops_list) {
2191
2192 if (!op->trampoline)
2193 continue;
2194
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002195 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002196 return op;
2197 } while_for_each_ftrace_op(op);
2198
2199 return NULL;
2200}
2201
2202static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002203ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2204 struct ftrace_ops *op)
2205{
2206 unsigned long ip = rec->ip;
2207
2208 while_for_each_ftrace_op(op) {
2209
2210 if (!op->trampoline)
2211 continue;
2212
2213 if (hash_contains_ip(ip, op->func_hash))
2214 return op;
2215 }
2216
2217 return NULL;
2218}
2219
2220static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002221ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2222{
2223 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002224 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002225
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002226 /*
2227 * Need to check removed ops first.
2228 * If they are being removed, and this rec has a tramp,
2229 * and this rec is in the ops list, then it would be the
2230 * one with the tramp.
2231 */
2232 if (removed_ops) {
2233 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002234 return removed_ops;
2235 }
2236
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002237 /*
2238 * Need to find the current trampoline for a rec.
2239 * Now, a trampoline is only attached to a rec if there
2240 * was a single 'ops' attached to it. But this can be called
2241 * when we are adding another op to the rec or removing the
2242 * current one. Thus, if the op is being added, we can
2243 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002244 * yet.
2245 *
2246 * If an ops is being modified (hooking to different functions)
2247 * then we don't care about the new functions that are being
2248 * added, just the old ones (that are probably being removed).
2249 *
2250 * If we are adding an ops to a function that already is using
2251 * a trampoline, it needs to be removed (trampolines are only
2252 * for single ops connected), then an ops that is not being
2253 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002254 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002255 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002256
2257 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002258 continue;
2259
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002260 /*
2261 * If the ops is being added, it hasn't gotten to
2262 * the point to be removed from this tree yet.
2263 */
2264 if (op->flags & FTRACE_OPS_FL_ADDING)
2265 continue;
2266
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002267
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002268 /*
2269 * If the ops is being modified and is in the old
2270 * hash, then it is probably being removed from this
2271 * function.
2272 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002273 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2274 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002275 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002276 /*
2277 * If the ops is not being added or modified, and it's
2278 * in its normal filter hash, then this must be the one
2279 * we want!
2280 */
2281 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2282 hash_contains_ip(ip, op->func_hash))
2283 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002284
2285 } while_for_each_ftrace_op(op);
2286
2287 return NULL;
2288}
2289
2290static struct ftrace_ops *
2291ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2292{
2293 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002294 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002295
2296 do_for_each_ftrace_op(op, ftrace_ops_list) {
2297 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002298 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002299 return op;
2300 } while_for_each_ftrace_op(op);
2301
2302 return NULL;
2303}
2304
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002305/**
2306 * ftrace_get_addr_new - Get the call address to set to
2307 * @rec: The ftrace record descriptor
2308 *
2309 * If the record has the FTRACE_FL_REGS set, that means that it
2310 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2311 * is not not set, then it wants to convert to the normal callback.
2312 *
2313 * Returns the address of the trampoline to set to
2314 */
2315unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2316{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002317 struct ftrace_ops *ops;
2318
2319 /* Trampolines take precedence over regs */
2320 if (rec->flags & FTRACE_FL_TRAMP) {
2321 ops = ftrace_find_tramp_ops_new(rec);
2322 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002323 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2324 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002325 /* Ftrace is shutting down, return anything */
2326 return (unsigned long)FTRACE_ADDR;
2327 }
2328 return ops->trampoline;
2329 }
2330
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002331 if (rec->flags & FTRACE_FL_REGS)
2332 return (unsigned long)FTRACE_REGS_ADDR;
2333 else
2334 return (unsigned long)FTRACE_ADDR;
2335}
2336
2337/**
2338 * ftrace_get_addr_curr - Get the call address that is already there
2339 * @rec: The ftrace record descriptor
2340 *
2341 * The FTRACE_FL_REGS_EN is set when the record already points to
2342 * a function that saves all the regs. Basically the '_EN' version
2343 * represents the current state of the function.
2344 *
2345 * Returns the address of the trampoline that is currently being called
2346 */
2347unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2348{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002349 struct ftrace_ops *ops;
2350
2351 /* Trampolines take precedence over regs */
2352 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2353 ops = ftrace_find_tramp_ops_curr(rec);
2354 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002355 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2356 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002357 /* Ftrace is shutting down, return anything */
2358 return (unsigned long)FTRACE_ADDR;
2359 }
2360 return ops->trampoline;
2361 }
2362
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002363 if (rec->flags & FTRACE_FL_REGS_EN)
2364 return (unsigned long)FTRACE_REGS_ADDR;
2365 else
2366 return (unsigned long)FTRACE_ADDR;
2367}
2368
Steven Rostedtc88fd862011-08-16 09:53:39 -04002369static int
2370__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2371{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002372 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002373 unsigned long ftrace_addr;
2374 int ret;
2375
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002376 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002377
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002378 /* This needs to be done before we call ftrace_update_record */
2379 ftrace_old_addr = ftrace_get_addr_curr(rec);
2380
2381 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002382
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002383 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2384
Steven Rostedtc88fd862011-08-16 09:53:39 -04002385 switch (ret) {
2386 case FTRACE_UPDATE_IGNORE:
2387 return 0;
2388
2389 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002390 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002391 return ftrace_make_call(rec, ftrace_addr);
2392
2393 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002394 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002395 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002396
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002397 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002398 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002399 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002400 }
2401
2402 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002403}
2404
Steven Rostedte4f5d542012-04-27 09:13:18 -04002405void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002406{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002407 struct dyn_ftrace *rec;
2408 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002409 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002410
Steven Rostedt45a4a232011-04-21 23:16:46 -04002411 if (unlikely(ftrace_disabled))
2412 return;
2413
Steven Rostedt265c8312009-02-13 12:43:56 -05002414 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002415
2416 if (rec->flags & FTRACE_FL_DISABLED)
2417 continue;
2418
Steven Rostedte4f5d542012-04-27 09:13:18 -04002419 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002420 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002421 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002422 /* Stop processing */
2423 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002424 }
2425 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002426}
2427
Steven Rostedtc88fd862011-08-16 09:53:39 -04002428struct ftrace_rec_iter {
2429 struct ftrace_page *pg;
2430 int index;
2431};
2432
2433/**
2434 * ftrace_rec_iter_start, start up iterating over traced functions
2435 *
2436 * Returns an iterator handle that is used to iterate over all
2437 * the records that represent address locations where functions
2438 * are traced.
2439 *
2440 * May return NULL if no records are available.
2441 */
2442struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2443{
2444 /*
2445 * We only use a single iterator.
2446 * Protected by the ftrace_lock mutex.
2447 */
2448 static struct ftrace_rec_iter ftrace_rec_iter;
2449 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2450
2451 iter->pg = ftrace_pages_start;
2452 iter->index = 0;
2453
2454 /* Could have empty pages */
2455 while (iter->pg && !iter->pg->index)
2456 iter->pg = iter->pg->next;
2457
2458 if (!iter->pg)
2459 return NULL;
2460
2461 return iter;
2462}
2463
2464/**
2465 * ftrace_rec_iter_next, get the next record to process.
2466 * @iter: The handle to the iterator.
2467 *
2468 * Returns the next iterator after the given iterator @iter.
2469 */
2470struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2471{
2472 iter->index++;
2473
2474 if (iter->index >= iter->pg->index) {
2475 iter->pg = iter->pg->next;
2476 iter->index = 0;
2477
2478 /* Could have empty pages */
2479 while (iter->pg && !iter->pg->index)
2480 iter->pg = iter->pg->next;
2481 }
2482
2483 if (!iter->pg)
2484 return NULL;
2485
2486 return iter;
2487}
2488
2489/**
2490 * ftrace_rec_iter_record, get the record at the iterator location
2491 * @iter: The current iterator location
2492 *
2493 * Returns the record that the current @iter is at.
2494 */
2495struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2496{
2497 return &iter->pg->records[iter->index];
2498}
2499
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302500static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002501ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002502{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002503 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002504
Steven Rostedt45a4a232011-04-21 23:16:46 -04002505 if (unlikely(ftrace_disabled))
2506 return 0;
2507
Shaohua Li25aac9d2009-01-09 11:29:40 +08002508 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002509 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002510 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002511 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302512 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002513 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302514 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002515}
2516
Steven Rostedt000ab692009-02-17 13:35:06 -05002517/*
2518 * archs can override this function if they must do something
2519 * before the modifying code is performed.
2520 */
2521int __weak ftrace_arch_code_modify_prepare(void)
2522{
2523 return 0;
2524}
2525
2526/*
2527 * archs can override this function if they must do something
2528 * after the modifying code is performed.
2529 */
2530int __weak ftrace_arch_code_modify_post_process(void)
2531{
2532 return 0;
2533}
2534
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002535void ftrace_modify_all_code(int command)
2536{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002537 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002538 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002539
2540 /*
2541 * If the ftrace_caller calls a ftrace_ops func directly,
2542 * we need to make sure that it only traces functions it
2543 * expects to trace. When doing the switch of functions,
2544 * we need to update to the ftrace_ops_list_func first
2545 * before the transition between old and new calls are set,
2546 * as the ftrace_ops_list_func will check the ops hashes
2547 * to make sure the ops are having the right functions
2548 * traced.
2549 */
Petr Mladekcd210672014-02-24 17:12:21 +01002550 if (update) {
2551 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2552 if (FTRACE_WARN_ON(err))
2553 return;
2554 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002555
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002556 if (command & FTRACE_UPDATE_CALLS)
2557 ftrace_replace_code(1);
2558 else if (command & FTRACE_DISABLE_CALLS)
2559 ftrace_replace_code(0);
2560
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002561 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2562 function_trace_op = set_function_trace_op;
2563 smp_wmb();
2564 /* If irqs are disabled, we are in stop machine */
2565 if (!irqs_disabled())
2566 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002567 err = ftrace_update_ftrace_func(ftrace_trace_function);
2568 if (FTRACE_WARN_ON(err))
2569 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002570 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002571
2572 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002573 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002574 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002575 err = ftrace_disable_ftrace_graph_caller();
2576 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002577}
2578
Ingo Molnare309b412008-05-12 21:20:51 +02002579static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002580{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002581 int *command = data;
2582
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002583 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002584
Steven Rostedtc88fd862011-08-16 09:53:39 -04002585 return 0;
2586}
2587
2588/**
2589 * ftrace_run_stop_machine, go back to the stop machine method
2590 * @command: The command to tell ftrace what to do
2591 *
2592 * If an arch needs to fall back to the stop machine method, the
2593 * it can call this function.
2594 */
2595void ftrace_run_stop_machine(int command)
2596{
2597 stop_machine(__ftrace_modify_code, &command, NULL);
2598}
2599
2600/**
2601 * arch_ftrace_update_code, modify the code to trace or not trace
2602 * @command: The command that needs to be done
2603 *
2604 * Archs can override this function if it does not need to
2605 * run stop_machine() to modify code.
2606 */
2607void __weak arch_ftrace_update_code(int command)
2608{
2609 ftrace_run_stop_machine(command);
2610}
2611
2612static void ftrace_run_update_code(int command)
2613{
2614 int ret;
2615
2616 ret = ftrace_arch_code_modify_prepare();
2617 FTRACE_WARN_ON(ret);
2618 if (ret)
2619 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002620
2621 /*
2622 * By default we use stop_machine() to modify the code.
2623 * But archs can do what ever they want as long as it
2624 * is safe. The stop_machine() is the safest, but also
2625 * produces the most overhead.
2626 */
2627 arch_ftrace_update_code(command);
2628
Steven Rostedt000ab692009-02-17 13:35:06 -05002629 ret = ftrace_arch_code_modify_post_process();
2630 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002631}
2632
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002633static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002634 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002635{
2636 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002637 ops->old_hash.filter_hash = old_hash->filter_hash;
2638 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002639 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002640 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002641 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002642 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2643}
2644
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002645static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002646static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002647
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002648void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2649{
2650}
2651
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002652static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002653{
2654 free_percpu(ops->disabled);
2655}
2656
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002657static void ftrace_startup_enable(int command)
2658{
2659 if (saved_ftrace_func != ftrace_trace_function) {
2660 saved_ftrace_func = ftrace_trace_function;
2661 command |= FTRACE_UPDATE_TRACE_FUNC;
2662 }
2663
2664 if (!command || !ftrace_enabled)
2665 return;
2666
2667 ftrace_run_update_code(command);
2668}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002669
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002670static void ftrace_startup_all(int command)
2671{
2672 update_all_ops = true;
2673 ftrace_startup_enable(command);
2674 update_all_ops = false;
2675}
2676
Steven Rostedta1cd6172011-05-23 15:24:25 -04002677static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002678{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002679 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002680
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002681 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002682 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002683
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002684 ret = __register_ftrace_function(ops);
2685 if (ret)
2686 return ret;
2687
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002688 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002689
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002690 /*
2691 * Note that ftrace probes uses this to start up
2692 * and modify functions it will probe. But we still
2693 * set the ADDING flag for modification, as probes
2694 * do not have trampolines. If they add them in the
2695 * future, then the probes will need to distinguish
2696 * between adding and updating probes.
2697 */
2698 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002699
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002700 ret = ftrace_hash_ipmodify_enable(ops);
2701 if (ret < 0) {
2702 /* Rollback registration process */
2703 __unregister_ftrace_function(ops);
2704 ftrace_start_up--;
2705 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2706 return ret;
2707 }
2708
Jiri Olsa7f50d062016-03-16 15:34:33 +01002709 if (ftrace_hash_rec_enable(ops, 1))
2710 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002711
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002712 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002713
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002714 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2715
Steven Rostedta1cd6172011-05-23 15:24:25 -04002716 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002717}
2718
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002719static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002720{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002721 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002722
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002723 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002724 return -ENODEV;
2725
2726 ret = __unregister_ftrace_function(ops);
2727 if (ret)
2728 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002729
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002730 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002731 /*
2732 * Just warn in case of unbalance, no need to kill ftrace, it's not
2733 * critical but the ftrace_call callers may be never nopped again after
2734 * further ftrace uses.
2735 */
2736 WARN_ON_ONCE(ftrace_start_up < 0);
2737
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002738 /* Disabling ipmodify never fails */
2739 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002740
2741 if (ftrace_hash_rec_disable(ops, 1))
2742 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002743
Namhyung Kima737e6d2014-06-12 23:56:12 +09002744 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002745
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002746 if (saved_ftrace_func != ftrace_trace_function) {
2747 saved_ftrace_func = ftrace_trace_function;
2748 command |= FTRACE_UPDATE_TRACE_FUNC;
2749 }
2750
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002751 if (!command || !ftrace_enabled) {
2752 /*
Steven Rostedt (VMware)100553e2017-09-01 12:18:28 -04002753 * If these are dynamic or per_cpu ops, they still
2754 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002755 * not currently active, we can just free them
2756 * without synchronizing all CPUs.
2757 */
Steven Rostedt (VMware)100553e2017-09-01 12:18:28 -04002758 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2759 goto free_ops;
2760
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002761 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002762 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002763
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002764 /*
2765 * If the ops uses a trampoline, then it needs to be
2766 * tested first on update.
2767 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002768 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002769 removed_ops = ops;
2770
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002771 /* The trampoline logic checks the old hashes */
2772 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2773 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2774
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002775 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002776
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002777 /*
2778 * If there's no more ops registered with ftrace, run a
2779 * sanity check to make sure all rec flags are cleared.
2780 */
2781 if (ftrace_ops_list == &ftrace_list_end) {
2782 struct ftrace_page *pg;
2783 struct dyn_ftrace *rec;
2784
2785 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002786 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002787 pr_warn(" %pS flags:%lx\n",
2788 (void *)rec->ip, rec->flags);
2789 } while_for_each_ftrace_rec();
2790 }
2791
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002792 ops->old_hash.filter_hash = NULL;
2793 ops->old_hash.notrace_hash = NULL;
2794
2795 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002796 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002797
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002798 /*
2799 * Dynamic ops may be freed, we must make sure that all
2800 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002801 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002802 * ops.
2803 *
2804 * Again, normal synchronize_sched() is not good enough.
2805 * We need to do a hard force of sched synchronization.
2806 * This is because we use preempt_disable() to do RCU, but
2807 * the function tracers can be called where RCU is not watching
2808 * (like before user_exit()). We can not rely on the RCU
2809 * infrastructure to do the synchronization, thus we must do it
2810 * ourselves.
2811 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002812 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002813 schedule_on_each_cpu(ftrace_sync);
2814
Steven Rostedt (VMware)100553e2017-09-01 12:18:28 -04002815 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002816 arch_ftrace_trampoline_free(ops);
2817
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002818 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2819 per_cpu_ops_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
Steven Rostedt3d083392008-05-12 21:20:42 +02002859static cycle_t 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))
2884 return 0;
2885
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))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002888 return 1;
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) &&
2892 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002893 return 0;
2894
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))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002897 return 0;
2898
2899 return 1;
2900}
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;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302906 cycle_t 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 Sagarf22f9a82008-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 */
2942 if (!ftrace_code_disable(mod, p))
2943 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002944
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002945 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002946 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002947 }
2948
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002949 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002950 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002951 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002952
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002953 return 0;
2954}
2955
Steven Rostedta7900872011-12-16 16:23:44 -05002956static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002957{
Steven Rostedta7900872011-12-16 16:23:44 -05002958 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002959 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002960
Steven Rostedta7900872011-12-16 16:23:44 -05002961 if (WARN_ON(!count))
2962 return -EINVAL;
2963
2964 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002965
2966 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002967 * We want to fill as much as possible. No more than a page
2968 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002969 */
Steven Rostedta7900872011-12-16 16:23:44 -05002970 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2971 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002972
Steven Rostedta7900872011-12-16 16:23:44 -05002973 again:
2974 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2975
2976 if (!pg->records) {
2977 /* if we can't allocate this size, try something smaller */
2978 if (!order)
2979 return -ENOMEM;
2980 order >>= 1;
2981 goto again;
2982 }
2983
2984 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2985 pg->size = cnt;
2986
2987 if (cnt > count)
2988 cnt = count;
2989
2990 return cnt;
2991}
2992
2993static struct ftrace_page *
2994ftrace_allocate_pages(unsigned long num_to_init)
2995{
2996 struct ftrace_page *start_pg;
2997 struct ftrace_page *pg;
2998 int order;
2999 int cnt;
3000
3001 if (!num_to_init)
3002 return 0;
3003
3004 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3005 if (!pg)
3006 return NULL;
3007
3008 /*
3009 * Try to allocate as much as possible in one continues
3010 * location that fills in all of the space. We want to
3011 * waste as little space as possible.
3012 */
3013 for (;;) {
3014 cnt = ftrace_allocate_records(pg, num_to_init);
3015 if (cnt < 0)
3016 goto free_pages;
3017
3018 num_to_init -= cnt;
3019 if (!num_to_init)
3020 break;
3021
3022 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3023 if (!pg->next)
3024 goto free_pages;
3025
3026 pg = pg->next;
3027 }
3028
3029 return start_pg;
3030
3031 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003032 pg = start_pg;
3033 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003034 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3035 free_pages((unsigned long)pg->records, order);
3036 start_pg = pg->next;
3037 kfree(pg);
3038 pg = start_pg;
3039 }
3040 pr_info("ftrace: FAILED to allocate memory for functions\n");
3041 return NULL;
3042}
3043
Steven Rostedt5072c592008-05-12 21:20:43 +02003044#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3045
3046struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003047 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003048 loff_t func_pos;
3049 struct ftrace_page *pg;
3050 struct dyn_ftrace *func;
3051 struct ftrace_func_probe *probe;
3052 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003053 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003054 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003055 int hidx;
3056 int idx;
3057 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003058};
3059
Ingo Molnare309b412008-05-12 21:20:51 +02003060static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003061t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003062{
3063 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003064 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003065 struct hlist_head *hhd;
3066
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003067 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003068 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003069
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003070 if (iter->probe)
3071 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003072 retry:
3073 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3074 return NULL;
3075
3076 hhd = &ftrace_func_hash[iter->hidx];
3077
3078 if (hlist_empty(hhd)) {
3079 iter->hidx++;
3080 hnd = NULL;
3081 goto retry;
3082 }
3083
3084 if (!hnd)
3085 hnd = hhd->first;
3086 else {
3087 hnd = hnd->next;
3088 if (!hnd) {
3089 iter->hidx++;
3090 goto retry;
3091 }
3092 }
3093
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003094 if (WARN_ON_ONCE(!hnd))
3095 return NULL;
3096
3097 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3098
3099 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003100}
3101
3102static void *t_hash_start(struct seq_file *m, loff_t *pos)
3103{
3104 struct ftrace_iterator *iter = m->private;
3105 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003106 loff_t l;
3107
Steven Rostedt69a30832011-12-19 15:21:16 -05003108 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3109 return NULL;
3110
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003111 if (iter->func_pos > *pos)
3112 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003113
Li Zefand82d6242009-06-24 09:54:54 +08003114 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003115 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003116 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003117 if (!p)
3118 break;
3119 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003120 if (!p)
3121 return NULL;
3122
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003123 /* Only set this if we have an item */
3124 iter->flags |= FTRACE_ITER_HASH;
3125
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003126 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003127}
3128
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003129static int
3130t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003131{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003132 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003133
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003134 rec = iter->probe;
3135 if (WARN_ON_ONCE(!rec))
3136 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003137
Steven Rostedt809dcf22009-02-16 23:06:01 -05003138 if (rec->ops->print)
3139 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3140
Steven Rostedtb375a112009-09-17 00:05:58 -04003141 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003142
3143 if (rec->data)
3144 seq_printf(m, ":%p", rec->data);
3145 seq_putc(m, '\n');
3146
3147 return 0;
3148}
3149
3150static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02003151t_next(struct seq_file *m, void *v, loff_t *pos)
3152{
3153 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003154 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003155 struct dyn_ftrace *rec = NULL;
3156
Steven Rostedt45a4a232011-04-21 23:16:46 -04003157 if (unlikely(ftrace_disabled))
3158 return NULL;
3159
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003160 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003161 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003162
Steven Rostedt5072c592008-05-12 21:20:43 +02003163 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01003164 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02003165
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003166 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003167 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003168
Steven Rostedt5072c592008-05-12 21:20:43 +02003169 retry:
3170 if (iter->idx >= iter->pg->index) {
3171 if (iter->pg->next) {
3172 iter->pg = iter->pg->next;
3173 iter->idx = 0;
3174 goto retry;
3175 }
3176 } else {
3177 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05003178 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003179 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05003180
Steven Rostedt41c52c02008-05-22 11:46:33 -04003181 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003182 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003183
3184 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003185 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003186
Steven Rostedt5072c592008-05-12 21:20:43 +02003187 rec = NULL;
3188 goto retry;
3189 }
3190 }
3191
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003192 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003193 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003194
3195 iter->func = rec;
3196
3197 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003198}
3199
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003200static void reset_iter_read(struct ftrace_iterator *iter)
3201{
3202 iter->pos = 0;
3203 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03003204 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003205}
3206
3207static void *t_start(struct seq_file *m, loff_t *pos)
3208{
3209 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003210 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003211 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003212 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003213
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003214 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003215
3216 if (unlikely(ftrace_disabled))
3217 return NULL;
3218
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003219 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003220 * If an lseek was done, then reset and start from beginning.
3221 */
3222 if (*pos < iter->pos)
3223 reset_iter_read(iter);
3224
3225 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003226 * For set_ftrace_filter reading, if we have the filter
3227 * off, we can short cut and just print out that all
3228 * functions are enabled.
3229 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003230 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003231 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003232 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003233 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003234 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003235 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003236 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003237 /* reset in case of seek/pread */
3238 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003239 return iter;
3240 }
3241
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003242 if (iter->flags & FTRACE_ITER_HASH)
3243 return t_hash_start(m, pos);
3244
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003245 /*
3246 * Unfortunately, we need to restart at ftrace_pages_start
3247 * every time we let go of the ftrace_mutex. This is because
3248 * those pointers can change without the lock.
3249 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003250 iter->pg = ftrace_pages_start;
3251 iter->idx = 0;
3252 for (l = 0; l <= *pos; ) {
3253 p = t_next(m, p, &l);
3254 if (!p)
3255 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003256 }
walimis5821e1b2008-11-15 15:19:06 +08003257
Steven Rostedt69a30832011-12-19 15:21:16 -05003258 if (!p)
3259 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003260
3261 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003262}
3263
3264static void t_stop(struct seq_file *m, void *p)
3265{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003266 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003267}
3268
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003269void * __weak
3270arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3271{
3272 return NULL;
3273}
3274
3275static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3276 struct dyn_ftrace *rec)
3277{
3278 void *ptr;
3279
3280 ptr = arch_ftrace_trampoline_func(ops, rec);
3281 if (ptr)
3282 seq_printf(m, " ->%pS", ptr);
3283}
3284
Steven Rostedt5072c592008-05-12 21:20:43 +02003285static int t_show(struct seq_file *m, void *v)
3286{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003287 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003288 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003289
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003290 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003291 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003292
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003293 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003294 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003295 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003296 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003297 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003298 return 0;
3299 }
3300
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003301 rec = iter->func;
3302
Steven Rostedt5072c592008-05-12 21:20:43 +02003303 if (!rec)
3304 return 0;
3305
Steven Rostedt647bcd02011-05-03 14:39:21 -04003306 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003307 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003308 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003309
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003310 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003311 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003312 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3313 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003314 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003315 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003316 if (ops) {
3317 do {
3318 seq_printf(m, "\ttramp: %pS (%pS)",
3319 (void *)ops->trampoline,
3320 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003321 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003322 ops = ftrace_find_tramp_ops_next(rec, ops);
3323 } while (ops);
3324 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003325 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003326 } else {
3327 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003328 }
3329 }
3330
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003331 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003332
3333 return 0;
3334}
3335
James Morris88e9d342009-09-22 16:43:43 -07003336static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003337 .start = t_start,
3338 .next = t_next,
3339 .stop = t_stop,
3340 .show = t_show,
3341};
3342
Ingo Molnare309b412008-05-12 21:20:51 +02003343static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003344ftrace_avail_open(struct inode *inode, struct file *file)
3345{
3346 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003347
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003348 if (unlikely(ftrace_disabled))
3349 return -ENODEV;
3350
Jiri Olsa50e18b92012-04-25 10:23:39 +02003351 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3352 if (iter) {
3353 iter->pg = ftrace_pages_start;
3354 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003355 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003356
Jiri Olsa50e18b92012-04-25 10:23:39 +02003357 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003358}
3359
Steven Rostedt647bcd02011-05-03 14:39:21 -04003360static int
3361ftrace_enabled_open(struct inode *inode, struct file *file)
3362{
3363 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003364
Jiri Olsa50e18b92012-04-25 10:23:39 +02003365 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3366 if (iter) {
3367 iter->pg = ftrace_pages_start;
3368 iter->flags = FTRACE_ITER_ENABLED;
3369 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003370 }
3371
Jiri Olsa50e18b92012-04-25 10:23:39 +02003372 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003373}
3374
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003375/**
3376 * ftrace_regex_open - initialize function tracer filter files
3377 * @ops: The ftrace_ops that hold the hash filters
3378 * @flag: The type of filter to process
3379 * @inode: The inode, usually passed in to your open routine
3380 * @file: The file, usually passed in to your open routine
3381 *
3382 * ftrace_regex_open() initializes the filter files for the
3383 * @ops. Depending on @flag it may process the filter hash or
3384 * the notrace hash of @ops. With this called from the open
3385 * routine, you can use ftrace_filter_write() for the write
3386 * routine if @flag has FTRACE_ITER_FILTER set, or
3387 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003388 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003389 * release must call ftrace_regex_release().
3390 */
3391int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003392ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003393 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003394{
3395 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003396 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003397 int ret = 0;
3398
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003399 ftrace_ops_init(ops);
3400
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003401 if (unlikely(ftrace_disabled))
3402 return -ENODEV;
3403
Steven Rostedt5072c592008-05-12 21:20:43 +02003404 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3405 if (!iter)
3406 return -ENOMEM;
3407
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003408 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3409 kfree(iter);
3410 return -ENOMEM;
3411 }
3412
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003413 iter->ops = ops;
3414 iter->flags = flag;
3415
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003416 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003417
Steven Rostedtf45948e2011-05-02 12:29:25 -04003418 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003419 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003420 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003421 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003422
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003423 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003424 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3425
3426 if (file->f_flags & O_TRUNC)
3427 iter->hash = alloc_ftrace_hash(size_bits);
3428 else
3429 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3430
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003431 if (!iter->hash) {
3432 trace_parser_put(&iter->parser);
3433 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003434 ret = -ENOMEM;
3435 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003436 }
3437 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003438
Steven Rostedt5072c592008-05-12 21:20:43 +02003439 if (file->f_mode & FMODE_READ) {
3440 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003441
3442 ret = seq_open(file, &show_ftrace_seq_ops);
3443 if (!ret) {
3444 struct seq_file *m = file->private_data;
3445 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003446 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003447 /* Failed */
3448 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003449 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003450 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003451 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003452 } else
3453 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003454
3455 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003456 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003457
3458 return ret;
3459}
3460
Steven Rostedt41c52c02008-05-22 11:46:33 -04003461static int
3462ftrace_filter_open(struct inode *inode, struct file *file)
3463{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003464 struct ftrace_ops *ops = inode->i_private;
3465
3466 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003467 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3468 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003469}
3470
3471static int
3472ftrace_notrace_open(struct inode *inode, struct file *file)
3473{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003474 struct ftrace_ops *ops = inode->i_private;
3475
3476 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003477 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003478}
3479
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003480/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3481struct ftrace_glob {
3482 char *search;
3483 unsigned len;
3484 int type;
3485};
3486
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003487/*
3488 * If symbols in an architecture don't correspond exactly to the user-visible
3489 * name of what they represent, it is possible to define this function to
3490 * perform the necessary adjustments.
3491*/
3492char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3493{
3494 return str;
3495}
3496
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003497static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003498{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003499 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003500 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003501
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003502 str = arch_ftrace_match_adjust(str, g->search);
3503
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003504 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003505 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003506 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003507 matched = 1;
3508 break;
3509 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003510 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003511 matched = 1;
3512 break;
3513 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003514 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003515 matched = 1;
3516 break;
3517 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003518 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003519 if (slen >= g->len &&
3520 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003521 matched = 1;
3522 break;
3523 }
3524
3525 return matched;
3526}
3527
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003528static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003529enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003530{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003531 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003532 int ret = 0;
3533
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003534 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003535 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003536 /* Do nothing if it doesn't exist */
3537 if (!entry)
3538 return 0;
3539
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003540 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003541 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003542 /* Do nothing if it exists */
3543 if (entry)
3544 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003545
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003546 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003547 }
3548 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003549}
3550
Steven Rostedt64e7c442009-02-13 17:08:48 -05003551static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003552ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3553 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003554{
3555 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003556 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003557
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003558 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3559
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003560 if (mod_g) {
3561 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3562
3563 /* blank module name to match all modules */
3564 if (!mod_g->len) {
3565 /* blank module globbing: modname xor exclude_mod */
3566 if ((!exclude_mod) != (!modname))
3567 goto func_match;
3568 return 0;
3569 }
3570
3571 /* not matching the module */
3572 if (!modname || !mod_matches) {
3573 if (exclude_mod)
3574 goto func_match;
3575 else
3576 return 0;
3577 }
3578
3579 if (mod_matches && exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003580 return 0;
3581
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003582func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003583 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003584 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003585 return 1;
3586 }
3587
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003588 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003589}
3590
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003591static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003592match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003593{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003594 struct ftrace_page *pg;
3595 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003596 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003597 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3598 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3599 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003600 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003601 int ret;
Dan Carpenter198bd492017-07-12 10:35:57 +03003602 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003603
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003604 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003605 func_g.type = filter_parse_regex(func, len, &func_g.search,
3606 &clear_filter);
3607 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003608 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003609
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003610 if (mod) {
3611 mod_g.type = filter_parse_regex(mod, strlen(mod),
3612 &mod_g.search, &exclude_mod);
3613 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003614 }
3615
Steven Rostedt52baf112009-02-14 01:15:39 -05003616 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003617
3618 if (unlikely(ftrace_disabled))
3619 goto out_unlock;
3620
Steven Rostedt265c8312009-02-13 12:43:56 -05003621 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003622
3623 if (rec->flags & FTRACE_FL_DISABLED)
3624 continue;
3625
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003626 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003627 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003628 if (ret < 0) {
3629 found = ret;
3630 goto out_unlock;
3631 }
Li Zefan311d16d2009-12-08 11:15:11 +08003632 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003633 }
3634 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003635 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003636 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003637
3638 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003639}
3640
Steven Rostedt64e7c442009-02-13 17:08:48 -05003641static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003642ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003643{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003644 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003645}
3646
Steven Rostedt64e7c442009-02-13 17:08:48 -05003647
Steven Rostedtf6180772009-02-14 00:40:25 -05003648/*
3649 * We register the module command as a template to show others how
3650 * to register the a command as well.
3651 */
3652
3653static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003654ftrace_mod_callback(struct ftrace_hash *hash,
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003655 char *func, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003656{
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003657 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003658
3659 /*
3660 * cmd == 'mod' because we only registered this func
3661 * for the 'mod' ftrace_func_command.
3662 * But if you register one func with multiple commands,
3663 * you can tell which command was used by the cmd
3664 * parameter.
3665 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003666 ret = match_records(hash, func, strlen(func), module);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003667 if (!ret)
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003668 return -EINVAL;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003669 if (ret < 0)
3670 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003671 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003672}
3673
3674static struct ftrace_func_command ftrace_mod_cmd = {
3675 .name = "mod",
3676 .func = ftrace_mod_callback,
3677};
3678
3679static int __init ftrace_mod_cmd_init(void)
3680{
3681 return register_ftrace_command(&ftrace_mod_cmd);
3682}
Steven Rostedt6f415672012-10-05 12:13:07 -04003683core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003684
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003685static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003686 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003687{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003688 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003689 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003690 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003691
3692 key = hash_long(ip, FTRACE_HASH_BITS);
3693
3694 hhd = &ftrace_func_hash[key];
3695
3696 if (hlist_empty(hhd))
3697 return;
3698
3699 /*
3700 * Disable preemption for these calls to prevent a RCU grace
3701 * period. This syncs the hash iteration and freeing of items
3702 * on the hash. rcu_read_lock is too dangerous here.
3703 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003704 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003705 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003706 if (entry->ip == ip)
3707 entry->ops->func(ip, parent_ip, &entry->data);
3708 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003709 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003710}
3711
Steven Rostedtb6887d72009-02-17 12:32:04 -05003712static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003713{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003714 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003715 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003716 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003717};
3718
Steven Rostedtb6887d72009-02-17 12:32:04 -05003719static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003720
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003721static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003722{
Steven Rostedtb8489142011-05-04 09:27:52 -04003723 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003724 int i;
3725
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003726 if (ftrace_probe_registered) {
3727 /* still need to update the function call sites */
3728 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003729 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3730 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003731 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003732 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003733
3734 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3735 struct hlist_head *hhd = &ftrace_func_hash[i];
3736 if (hhd->first)
3737 break;
3738 }
3739 /* Nothing registered? */
3740 if (i == FTRACE_FUNC_HASHSIZE)
3741 return;
3742
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003743 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003744
Steven Rostedtb6887d72009-02-17 12:32:04 -05003745 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003746}
3747
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003748static bool __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003749{
3750 int i;
3751
Steven Rostedtb6887d72009-02-17 12:32:04 -05003752 if (!ftrace_probe_registered)
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003753 return false;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003754
3755 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3756 struct hlist_head *hhd = &ftrace_func_hash[i];
3757 if (hhd->first)
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003758 return false;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003759 }
3760
3761 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003762 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003763
Steven Rostedtb6887d72009-02-17 12:32:04 -05003764 ftrace_probe_registered = 0;
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003765 return true;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003766}
3767
3768
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003769static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003770{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003771 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003772 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003773 kfree(entry);
3774}
3775
Steven Rostedt59df055f2009-02-14 15:29:06 -05003776int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003777register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003778 void *data)
3779{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003780 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003781 struct ftrace_func_probe *entry;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003782 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003783 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003784 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003785 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003786 struct ftrace_page *pg;
3787 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003788 int not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003789 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003790 int count = 0;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003791 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003792
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003793 func_g.type = filter_parse_regex(glob, strlen(glob),
3794 &func_g.search, &not);
3795 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003796
Steven Rostedtb6887d72009-02-17 12:32:04 -05003797 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003798 if (WARN_ON(not))
3799 return -EINVAL;
3800
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003801 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003802
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003803 old_hash_ops.filter_hash = old_hash;
3804 /* Probes only have filters */
3805 old_hash_ops.notrace_hash = NULL;
3806
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003807 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003808 if (!hash) {
3809 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003810 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003811 }
3812
3813 if (unlikely(ftrace_disabled)) {
3814 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003815 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003816 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003817
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003818 mutex_lock(&ftrace_lock);
3819
Steven Rostedt59df055f2009-02-14 15:29:06 -05003820 do_for_each_ftrace_rec(pg, rec) {
3821
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003822 if (rec->flags & FTRACE_FL_DISABLED)
3823 continue;
3824
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003825 if (!ftrace_match_record(rec, &func_g, NULL, 0))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003826 continue;
3827
3828 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3829 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003830 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003831 if (!count)
3832 count = -ENOMEM;
3833 goto out_unlock;
3834 }
3835
3836 count++;
3837
3838 entry->data = data;
3839
3840 /*
3841 * The caller might want to do something special
3842 * for each function we find. We call the callback
3843 * to give the caller an opportunity to do so.
3844 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003845 if (ops->init) {
3846 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003847 /* caller does not like this func */
3848 kfree(entry);
3849 continue;
3850 }
3851 }
3852
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003853 ret = enter_record(hash, rec, 0);
3854 if (ret < 0) {
3855 kfree(entry);
3856 count = ret;
3857 goto out_unlock;
3858 }
3859
Steven Rostedt59df055f2009-02-14 15:29:06 -05003860 entry->ops = ops;
3861 entry->ip = rec->ip;
3862
3863 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3864 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3865
3866 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003867
3868 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003869
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003870 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003871
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003872 if (!ret)
3873 free_ftrace_hash_rcu(old_hash);
3874 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003875 count = ret;
3876
Steven Rostedt59df055f2009-02-14 15:29:06 -05003877 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003878 mutex_unlock(&ftrace_lock);
3879 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003880 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003881 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003882
3883 return count;
3884}
3885
3886enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003887 PROBE_TEST_FUNC = 1,
3888 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003889};
3890
3891static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003892__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003893 void *data, int flags)
3894{
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003895 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003896 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003897 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003898 struct ftrace_func_probe *p;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003899 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003900 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003901 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003902 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003903 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003904 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003905 char str[KSYM_SYMBOL_LEN];
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003906 int i, ret;
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003907 bool disabled;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003908
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003909 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003910 func_g.search = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003911 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003912 int not;
3913
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003914 func_g.type = filter_parse_regex(glob, strlen(glob),
3915 &func_g.search, &not);
3916 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003917
Steven Rostedtb6887d72009-02-17 12:32:04 -05003918 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003919 if (WARN_ON(not))
3920 return;
3921 }
3922
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003923 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003924
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003925 old_hash_ops.filter_hash = old_hash;
3926 /* Probes only have filters */
3927 old_hash_ops.notrace_hash = NULL;
3928
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003929 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3930 if (!hash)
3931 /* Hmm, should report this somehow */
3932 goto out_unlock;
3933
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003934 INIT_LIST_HEAD(&free_list);
3935
Steven Rostedt59df055f2009-02-14 15:29:06 -05003936 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3937 struct hlist_head *hhd = &ftrace_func_hash[i];
3938
Sasha Levinb67bfe02013-02-27 17:06:00 -08003939 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003940
3941 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003942 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003943 continue;
3944
Steven Rostedtb6887d72009-02-17 12:32:04 -05003945 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003946 continue;
3947
3948 /* do this last, since it is the most expensive */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003949 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003950 kallsyms_lookup(entry->ip, NULL, NULL,
3951 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003952 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003953 continue;
3954 }
3955
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003956 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3957 /* It is possible more than one entry had this ip */
3958 if (rec_entry)
3959 free_hash_entry(hash, rec_entry);
3960
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003961 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003962 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003963 }
3964 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003965 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003966 disabled = __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003967 /*
3968 * Remove after the disable is called. Otherwise, if the last
3969 * probe is removed, a null hash means *all enabled*.
3970 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003971 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003972
3973 /* still need to update the function call sites */
3974 if (ftrace_enabled && !disabled)
3975 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3976 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003977 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003978 if (!ret)
3979 free_ftrace_hash_rcu(old_hash);
3980
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003981 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3982 list_del(&entry->free_list);
3983 ftrace_free_entry(entry);
3984 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003985 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003986
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003987 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003988 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003989 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003990}
3991
3992void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003993unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003994 void *data)
3995{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003996 __unregister_ftrace_function_probe(glob, ops, data,
3997 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003998}
3999
4000void
Steven Rostedtb6887d72009-02-17 12:32:04 -05004001unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004002{
Steven Rostedtb6887d72009-02-17 12:32:04 -05004003 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004004}
4005
Steven Rostedtb6887d72009-02-17 12:32:04 -05004006void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004007{
Steven Rostedtb6887d72009-02-17 12:32:04 -05004008 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004009}
4010
Steven Rostedtf6180772009-02-14 00:40:25 -05004011static LIST_HEAD(ftrace_commands);
4012static DEFINE_MUTEX(ftrace_cmd_mutex);
4013
Tom Zanussi38de93a2013-10-24 08:34:18 -05004014/*
4015 * Currently we only register ftrace commands from __init, so mark this
4016 * __init too.
4017 */
4018__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004019{
4020 struct ftrace_func_command *p;
4021 int ret = 0;
4022
4023 mutex_lock(&ftrace_cmd_mutex);
4024 list_for_each_entry(p, &ftrace_commands, list) {
4025 if (strcmp(cmd->name, p->name) == 0) {
4026 ret = -EBUSY;
4027 goto out_unlock;
4028 }
4029 }
4030 list_add(&cmd->list, &ftrace_commands);
4031 out_unlock:
4032 mutex_unlock(&ftrace_cmd_mutex);
4033
4034 return ret;
4035}
4036
Tom Zanussi38de93a2013-10-24 08:34:18 -05004037/*
4038 * Currently we only unregister ftrace commands from __init, so mark
4039 * this __init too.
4040 */
4041__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004042{
4043 struct ftrace_func_command *p, *n;
4044 int ret = -ENODEV;
4045
4046 mutex_lock(&ftrace_cmd_mutex);
4047 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4048 if (strcmp(cmd->name, p->name) == 0) {
4049 ret = 0;
4050 list_del_init(&p->list);
4051 goto out_unlock;
4052 }
4053 }
4054 out_unlock:
4055 mutex_unlock(&ftrace_cmd_mutex);
4056
4057 return ret;
4058}
4059
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004060static int ftrace_process_regex(struct ftrace_hash *hash,
4061 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004062{
Steven Rostedtf6180772009-02-14 00:40:25 -05004063 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004064 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004065 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004066
4067 func = strsep(&next, ":");
4068
4069 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004070 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004071 if (!ret)
4072 ret = -EINVAL;
4073 if (ret < 0)
4074 return ret;
4075 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004076 }
4077
Steven Rostedtf6180772009-02-14 00:40:25 -05004078 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004079
4080 command = strsep(&next, ":");
4081
Steven Rostedtf6180772009-02-14 00:40:25 -05004082 mutex_lock(&ftrace_cmd_mutex);
4083 list_for_each_entry(p, &ftrace_commands, list) {
4084 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04004085 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004086 goto out_unlock;
4087 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004088 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004089 out_unlock:
4090 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004091
Steven Rostedtf6180772009-02-14 00:40:25 -05004092 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004093}
4094
Ingo Molnare309b412008-05-12 21:20:51 +02004095static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004096ftrace_regex_write(struct file *file, const char __user *ubuf,
4097 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004098{
4099 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004100 struct trace_parser *parser;
4101 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004102
Li Zefan4ba79782009-09-22 13:52:20 +08004103 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004104 return 0;
4105
Steven Rostedt5072c592008-05-12 21:20:43 +02004106 if (file->f_mode & FMODE_READ) {
4107 struct seq_file *m = file->private_data;
4108 iter = m->private;
4109 } else
4110 iter = file->private_data;
4111
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004112 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004113 return -ENODEV;
4114
4115 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004116
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004117 parser = &iter->parser;
4118 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004119
Li Zefan4ba79782009-09-22 13:52:20 +08004120 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004121 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004122 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004123 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004124 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004125 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004126 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004127 }
4128
Steven Rostedt5072c592008-05-12 21:20:43 +02004129 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004130 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004131 return ret;
4132}
4133
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004134ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004135ftrace_filter_write(struct file *file, const char __user *ubuf,
4136 size_t cnt, loff_t *ppos)
4137{
4138 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4139}
4140
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004141ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004142ftrace_notrace_write(struct file *file, const char __user *ubuf,
4143 size_t cnt, loff_t *ppos)
4144{
4145 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4146}
4147
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004148static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004149ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4150{
4151 struct ftrace_func_entry *entry;
4152
4153 if (!ftrace_location(ip))
4154 return -EINVAL;
4155
4156 if (remove) {
4157 entry = ftrace_lookup_ip(hash, ip);
4158 if (!entry)
4159 return -ENOENT;
4160 free_hash_entry(hash, entry);
4161 return 0;
4162 }
4163
4164 return add_hash_entry(hash, ip);
4165}
4166
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004167static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004168 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004169{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004170 struct ftrace_ops *op;
4171
4172 if (!ftrace_enabled)
4173 return;
4174
4175 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004176 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004177 return;
4178 }
4179
4180 /*
4181 * If this is the shared global_ops filter, then we need to
4182 * check if there is another ops that shares it, is enabled.
4183 * If so, we still need to run the modify code.
4184 */
4185 if (ops->func_hash != &global_ops.local_hash)
4186 return;
4187
4188 do_for_each_ftrace_op(op, ftrace_ops_list) {
4189 if (op->func_hash == &global_ops.local_hash &&
4190 op->flags & FTRACE_OPS_FL_ENABLED) {
4191 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4192 /* Only need to do this once */
4193 return;
4194 }
4195 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004196}
4197
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004198static int
4199ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4200 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004201{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004202 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004203 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004204 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004205 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004206 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004207
Steven Rostedt41c52c02008-05-22 11:46:33 -04004208 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004209 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004210
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004211 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004212
Steven Rostedtf45948e2011-05-02 12:29:25 -04004213 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004214 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004215 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004216 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004217
Wang Nanb972cc52014-07-15 08:40:20 +08004218 if (reset)
4219 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4220 else
4221 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4222
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004223 if (!hash) {
4224 ret = -ENOMEM;
4225 goto out_regex_unlock;
4226 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004227
Jiri Olsaac483c42012-01-02 10:04:14 +01004228 if (buf && !ftrace_match_records(hash, buf, len)) {
4229 ret = -EINVAL;
4230 goto out_regex_unlock;
4231 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004232 if (ip) {
4233 ret = ftrace_match_addr(hash, ip, remove);
4234 if (ret < 0)
4235 goto out_regex_unlock;
4236 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004237
4238 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004239 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004240 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4241 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004242 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004243 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004244 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004245 free_ftrace_hash_rcu(old_hash);
4246 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004247 mutex_unlock(&ftrace_lock);
4248
Jiri Olsaac483c42012-01-02 10:04:14 +01004249 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004250 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004251
4252 free_ftrace_hash(hash);
4253 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004254}
4255
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004256static int
4257ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4258 int reset, int enable)
4259{
4260 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4261}
4262
4263/**
4264 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4265 * @ops - the ops to set the filter with
4266 * @ip - the address to add to or remove from the filter.
4267 * @remove - non zero to remove the ip from the filter
4268 * @reset - non zero to reset all filters before applying this filter.
4269 *
4270 * Filters denote which functions should be enabled when tracing is enabled
4271 * If @ip is NULL, it failes to update filter.
4272 */
4273int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4274 int remove, int reset)
4275{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004276 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004277 return ftrace_set_addr(ops, ip, remove, reset, 1);
4278}
4279EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4280
4281static int
4282ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4283 int reset, int enable)
4284{
4285 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4286}
4287
Steven Rostedt77a2b372008-05-12 21:20:45 +02004288/**
4289 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004290 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004291 * @buf - the string that holds the function filter text.
4292 * @len - the length of the string.
4293 * @reset - non zero to reset all filters before applying this filter.
4294 *
4295 * Filters denote which functions should be enabled when tracing is enabled.
4296 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4297 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004298int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004299 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004300{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004301 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004302 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004303}
Steven Rostedt936e0742011-05-05 22:54:01 -04004304EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004305
Steven Rostedt41c52c02008-05-22 11:46:33 -04004306/**
4307 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004308 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004309 * @buf - the string that holds the function notrace text.
4310 * @len - the length of the string.
4311 * @reset - non zero to reset all filters before applying this filter.
4312 *
4313 * Notrace Filters denote which functions should not be enabled when tracing
4314 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4315 * for tracing.
4316 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004317int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004318 int len, int reset)
4319{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004320 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004321 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004322}
4323EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4324/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004325 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004326 * @buf - the string that holds the function filter text.
4327 * @len - the length of the string.
4328 * @reset - non zero to reset all filters before applying this filter.
4329 *
4330 * Filters denote which functions should be enabled when tracing is enabled.
4331 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4332 */
4333void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4334{
4335 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4336}
4337EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4338
4339/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004340 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004341 * @buf - the string that holds the function notrace text.
4342 * @len - the length of the string.
4343 * @reset - non zero to reset all filters before applying this filter.
4344 *
4345 * Notrace Filters denote which functions should not be enabled when tracing
4346 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4347 * for tracing.
4348 */
4349void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004350{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004351 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004352}
Steven Rostedt936e0742011-05-05 22:54:01 -04004353EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004354
Steven Rostedt2af15d62009-05-28 13:37:24 -04004355/*
4356 * command line interface to allow users to set filters on boot up.
4357 */
4358#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4359static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4360static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4361
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004362/* Used by function selftest to not test if filter is set */
4363bool ftrace_filter_param __initdata;
4364
Steven Rostedt2af15d62009-05-28 13:37:24 -04004365static int __init set_ftrace_notrace(char *str)
4366{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004367 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004368 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004369 return 1;
4370}
4371__setup("ftrace_notrace=", set_ftrace_notrace);
4372
4373static int __init set_ftrace_filter(char *str)
4374{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004375 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004376 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004377 return 1;
4378}
4379__setup("ftrace_filter=", set_ftrace_filter);
4380
Stefan Assmann369bc182009-10-12 22:17:21 +02004381#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004382static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004383static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004384static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004385
Stefan Assmann369bc182009-10-12 22:17:21 +02004386static int __init set_graph_function(char *str)
4387{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004388 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004389 return 1;
4390}
4391__setup("ftrace_graph_filter=", set_graph_function);
4392
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004393static int __init set_graph_notrace_function(char *str)
4394{
4395 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4396 return 1;
4397}
4398__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4399
4400static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004401{
4402 int ret;
4403 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004404 unsigned long *table = ftrace_graph_funcs;
4405 int *count = &ftrace_graph_count;
4406
4407 if (!enable) {
4408 table = ftrace_graph_notrace_funcs;
4409 count = &ftrace_graph_notrace_count;
4410 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004411
4412 while (buf) {
4413 func = strsep(&buf, ",");
4414 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004415 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004416 if (ret)
4417 printk(KERN_DEBUG "ftrace: function %s not "
4418 "traceable\n", func);
4419 }
4420}
4421#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4422
Steven Rostedt2a85a372011-12-19 21:57:44 -05004423void __init
4424ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004425{
4426 char *func;
4427
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004428 ftrace_ops_init(ops);
4429
Steven Rostedt2af15d62009-05-28 13:37:24 -04004430 while (buf) {
4431 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004432 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004433 }
4434}
4435
4436static void __init set_ftrace_early_filters(void)
4437{
4438 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004439 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004440 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004441 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004442#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4443 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004444 set_ftrace_early_graph(ftrace_graph_buf, 1);
4445 if (ftrace_graph_notrace_buf[0])
4446 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004447#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004448}
4449
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004450int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004451{
4452 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004453 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004454 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004455 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004456 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004457 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004458 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004459 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004460
Steven Rostedt5072c592008-05-12 21:20:43 +02004461 if (file->f_mode & FMODE_READ) {
4462 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004463 seq_release(inode, file);
4464 } else
4465 iter = file->private_data;
4466
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004467 parser = &iter->parser;
4468 if (trace_parser_loaded(parser)) {
4469 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004470 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004471 }
4472
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004473 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004474
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004475 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004476
Steven Rostedt058e2972011-04-29 22:35:33 -04004477 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004478 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4479
4480 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004481 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004482 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004483 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004484
Steven Rostedt058e2972011-04-29 22:35:33 -04004485 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004486 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004487 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4488 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004489 ret = ftrace_hash_move(iter->ops, filter_hash,
4490 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004491 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004492 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004493 free_ftrace_hash_rcu(old_hash);
4494 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004495 mutex_unlock(&ftrace_lock);
4496 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004497
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004498 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004499 free_ftrace_hash(iter->hash);
4500 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004501
Steven Rostedt5072c592008-05-12 21:20:43 +02004502 return 0;
4503}
4504
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004505static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004506 .open = ftrace_avail_open,
4507 .read = seq_read,
4508 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004509 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004510};
4511
Steven Rostedt647bcd02011-05-03 14:39:21 -04004512static const struct file_operations ftrace_enabled_fops = {
4513 .open = ftrace_enabled_open,
4514 .read = seq_read,
4515 .llseek = seq_lseek,
4516 .release = seq_release_private,
4517};
4518
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004519static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004520 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004521 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004522 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004523 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004524 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004525};
4526
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004527static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004528 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004529 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004530 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004531 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004532 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004533};
4534
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004535#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4536
4537static DEFINE_MUTEX(graph_lock);
4538
4539int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004540int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004541unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004542unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004543
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004544struct ftrace_graph_data {
4545 unsigned long *table;
4546 size_t size;
4547 int *count;
4548 const struct seq_operations *seq_ops;
4549};
4550
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004551static void *
Li Zefan85951842009-06-24 09:54:00 +08004552__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004553{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004554 struct ftrace_graph_data *fgd = m->private;
4555
4556 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004557 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004558 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004559}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004560
Li Zefan85951842009-06-24 09:54:00 +08004561static void *
4562g_next(struct seq_file *m, void *v, loff_t *pos)
4563{
4564 (*pos)++;
4565 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004566}
4567
4568static void *g_start(struct seq_file *m, loff_t *pos)
4569{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004570 struct ftrace_graph_data *fgd = m->private;
4571
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004572 mutex_lock(&graph_lock);
4573
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004574 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004575 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004576 return (void *)1;
4577
Li Zefan85951842009-06-24 09:54:00 +08004578 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004579}
4580
4581static void g_stop(struct seq_file *m, void *p)
4582{
4583 mutex_unlock(&graph_lock);
4584}
4585
4586static int g_show(struct seq_file *m, void *v)
4587{
4588 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004589
4590 if (!ptr)
4591 return 0;
4592
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004593 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004594 struct ftrace_graph_data *fgd = m->private;
4595
4596 if (fgd->table == ftrace_graph_funcs)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004597 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004598 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004599 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004600 return 0;
4601 }
4602
Steven Rostedtb375a112009-09-17 00:05:58 -04004603 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004604
4605 return 0;
4606}
4607
James Morris88e9d342009-09-22 16:43:43 -07004608static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004609 .start = g_start,
4610 .next = g_next,
4611 .stop = g_stop,
4612 .show = g_show,
4613};
4614
4615static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004616__ftrace_graph_open(struct inode *inode, struct file *file,
4617 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004618{
4619 int ret = 0;
4620
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004621 mutex_lock(&graph_lock);
4622 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004623 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004624 *fgd->count = 0;
4625 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004626 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004627 mutex_unlock(&graph_lock);
4628
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004629 if (file->f_mode & FMODE_READ) {
4630 ret = seq_open(file, fgd->seq_ops);
4631 if (!ret) {
4632 struct seq_file *m = file->private_data;
4633 m->private = fgd;
4634 }
4635 } else
4636 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004637
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004638 return ret;
4639}
4640
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004641static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004642ftrace_graph_open(struct inode *inode, struct file *file)
4643{
4644 struct ftrace_graph_data *fgd;
4645
4646 if (unlikely(ftrace_disabled))
4647 return -ENODEV;
4648
4649 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4650 if (fgd == NULL)
4651 return -ENOMEM;
4652
4653 fgd->table = ftrace_graph_funcs;
4654 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4655 fgd->count = &ftrace_graph_count;
4656 fgd->seq_ops = &ftrace_graph_seq_ops;
4657
4658 return __ftrace_graph_open(inode, file, fgd);
4659}
4660
4661static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004662ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4663{
4664 struct ftrace_graph_data *fgd;
4665
4666 if (unlikely(ftrace_disabled))
4667 return -ENODEV;
4668
4669 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4670 if (fgd == NULL)
4671 return -ENOMEM;
4672
4673 fgd->table = ftrace_graph_notrace_funcs;
4674 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4675 fgd->count = &ftrace_graph_notrace_count;
4676 fgd->seq_ops = &ftrace_graph_seq_ops;
4677
4678 return __ftrace_graph_open(inode, file, fgd);
4679}
4680
4681static int
Li Zefan87827112009-07-23 11:29:11 +08004682ftrace_graph_release(struct inode *inode, struct file *file)
4683{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004684 if (file->f_mode & FMODE_READ) {
4685 struct seq_file *m = file->private_data;
4686
4687 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004688 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004689 } else {
4690 kfree(file->private_data);
4691 }
4692
Li Zefan87827112009-07-23 11:29:11 +08004693 return 0;
4694}
4695
4696static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004697ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004698{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004699 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004700 struct dyn_ftrace *rec;
4701 struct ftrace_page *pg;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004702 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004703 int not;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004704 bool exists;
4705 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004706
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004707 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004708 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4709 &func_g.search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004710 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004711 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004712
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004713 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004714
Steven Rostedt52baf112009-02-14 01:15:39 -05004715 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004716
4717 if (unlikely(ftrace_disabled)) {
4718 mutex_unlock(&ftrace_lock);
4719 return -ENODEV;
4720 }
4721
Steven Rostedt265c8312009-02-13 12:43:56 -05004722 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004723
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004724 if (rec->flags & FTRACE_FL_DISABLED)
4725 continue;
4726
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004727 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004728 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004729 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004730 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004731 if (array[i] == rec->ip) {
4732 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004733 break;
4734 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004735 }
4736
4737 if (!not) {
4738 fail = 0;
4739 if (!exists) {
4740 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004741 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004742 goto out;
4743 }
4744 } else {
4745 if (exists) {
4746 array[i] = array[--(*idx)];
4747 array[*idx] = 0;
4748 fail = 0;
4749 }
4750 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004751 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004752 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004753out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004754 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004755
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004756 if (fail)
4757 return -EINVAL;
4758
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004759 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004760}
4761
4762static ssize_t
4763ftrace_graph_write(struct file *file, const char __user *ubuf,
4764 size_t cnt, loff_t *ppos)
4765{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004766 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004767 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004768 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004769
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004770 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004771 return 0;
4772
Namhyung Kim6a101082013-10-14 17:24:25 +09004773 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4774 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004775
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004776 read = trace_get_user(&parser, ubuf, cnt, ppos);
4777
Li Zefan4ba79782009-09-22 13:52:20 +08004778 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004779 parser.buffer[parser.idx] = 0;
4780
Namhyung Kim6a101082013-10-14 17:24:25 +09004781 mutex_lock(&graph_lock);
4782
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004783 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004784 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4785 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004786
4787 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004788 }
4789
Namhyung Kim6a101082013-10-14 17:24:25 +09004790 if (!ret)
4791 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004792
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004793 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004794
4795 return ret;
4796}
4797
4798static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004799 .open = ftrace_graph_open,
4800 .read = seq_read,
4801 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004802 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004803 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004804};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004805
4806static const struct file_operations ftrace_graph_notrace_fops = {
4807 .open = ftrace_graph_notrace_open,
4808 .read = seq_read,
4809 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004810 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004811 .release = ftrace_graph_release,
4812};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004813#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4814
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004815void ftrace_create_filter_files(struct ftrace_ops *ops,
4816 struct dentry *parent)
4817{
4818
4819 trace_create_file("set_ftrace_filter", 0644, parent,
4820 ops, &ftrace_filter_fops);
4821
4822 trace_create_file("set_ftrace_notrace", 0644, parent,
4823 ops, &ftrace_notrace_fops);
4824}
4825
4826/*
4827 * The name "destroy_filter_files" is really a misnomer. Although
4828 * in the future, it may actualy delete the files, but this is
4829 * really intended to make sure the ops passed in are disabled
4830 * and that when this function returns, the caller is free to
4831 * free the ops.
4832 *
4833 * The "destroy" name is only to match the "create" name that this
4834 * should be paired with.
4835 */
4836void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4837{
4838 mutex_lock(&ftrace_lock);
4839 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4840 ftrace_shutdown(ops, 0);
4841 ops->flags |= FTRACE_OPS_FL_DELETED;
Steven Rostedt (VMware)326c9e12018-12-10 23:58:01 -05004842 ftrace_free_filter(ops);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004843 mutex_unlock(&ftrace_lock);
4844}
4845
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05004846static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004847{
Steven Rostedt5072c592008-05-12 21:20:43 +02004848
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004849 trace_create_file("available_filter_functions", 0444,
4850 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004851
Steven Rostedt647bcd02011-05-03 14:39:21 -04004852 trace_create_file("enabled_functions", 0444,
4853 d_tracer, NULL, &ftrace_enabled_fops);
4854
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004855 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004856
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004857#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004858 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004859 NULL,
4860 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004861 trace_create_file("set_graph_notrace", 0444, d_tracer,
4862 NULL,
4863 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004864#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4865
Steven Rostedt5072c592008-05-12 21:20:43 +02004866 return 0;
4867}
4868
Steven Rostedt9fd49322012-04-24 22:32:06 -04004869static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004870{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004871 const unsigned long *ipa = a;
4872 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004873
Steven Rostedt9fd49322012-04-24 22:32:06 -04004874 if (*ipa > *ipb)
4875 return 1;
4876 if (*ipa < *ipb)
4877 return -1;
4878 return 0;
4879}
4880
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004881static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004882 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004883 unsigned long *end)
4884{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004885 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004886 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004887 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004888 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004889 unsigned long *p;
4890 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004891 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004892 int ret = -ENOMEM;
4893
4894 count = end - start;
4895
4896 if (!count)
4897 return 0;
4898
Steven Rostedt9fd49322012-04-24 22:32:06 -04004899 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02004900 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04004901
Steven Rostedt706c81f2012-04-24 23:45:26 -04004902 start_pg = ftrace_allocate_pages(count);
4903 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004904 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004905
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004906 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004907
Steven Rostedt32082302011-12-16 14:42:37 -05004908 /*
4909 * Core and each module needs their own pages, as
4910 * modules will free them when they are removed.
4911 * Force a new page to be allocated for modules.
4912 */
Steven Rostedta7900872011-12-16 16:23:44 -05004913 if (!mod) {
4914 WARN_ON(ftrace_pages || ftrace_pages_start);
4915 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004916 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004917 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004918 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004919 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004920
Steven Rostedta7900872011-12-16 16:23:44 -05004921 if (WARN_ON(ftrace_pages->next)) {
4922 /* Hmm, we have free pages? */
4923 while (ftrace_pages->next)
4924 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004925 }
Steven Rostedta7900872011-12-16 16:23:44 -05004926
Steven Rostedt706c81f2012-04-24 23:45:26 -04004927 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004928 }
4929
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004930 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004931 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004932 while (p < end) {
4933 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004934 /*
4935 * Some architecture linkers will pad between
4936 * the different mcount_loc sections of different
4937 * object files to satisfy alignments.
4938 * Skip any NULL pointers.
4939 */
4940 if (!addr)
4941 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004942
4943 if (pg->index == pg->size) {
4944 /* We should have allocated enough */
4945 if (WARN_ON(!pg->next))
4946 break;
4947 pg = pg->next;
4948 }
4949
4950 rec = &pg->records[pg->index++];
4951 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004952 }
4953
Steven Rostedt706c81f2012-04-24 23:45:26 -04004954 /* We should have used all pages */
4955 WARN_ON(pg->next);
4956
4957 /* Assign the last page to ftrace_pages */
4958 ftrace_pages = pg;
4959
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004960 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004961 * We only need to disable interrupts on start up
4962 * because we are modifying code that an interrupt
4963 * may execute, and the modification is not atomic.
4964 * But for modules, nothing runs the code we modify
4965 * until we are finished with it, and there's no
4966 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004967 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004968 if (!mod)
4969 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004970 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004971 if (!mod)
4972 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004973 ret = 0;
4974 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004975 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004976
Steven Rostedta7900872011-12-16 16:23:44 -05004977 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004978}
4979
Steven Rostedt93eb6772009-04-15 13:24:06 -04004980#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004981
4982#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4983
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05004984static int referenced_filters(struct dyn_ftrace *rec)
4985{
4986 struct ftrace_ops *ops;
4987 int cnt = 0;
4988
4989 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
4990 if (ops_references_rec(ops, rec))
4991 cnt++;
4992 }
4993
4994 return cnt;
4995}
4996
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004997void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004998{
4999 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05005000 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005001 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005002 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005003
Steven Rostedt93eb6772009-04-15 13:24:06 -04005004 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005005
5006 if (ftrace_disabled)
5007 goto out_unlock;
5008
Steven Rostedt32082302011-12-16 14:42:37 -05005009 /*
5010 * Each module has its own ftrace_pages, remove
5011 * them from the list.
5012 */
5013 last_pg = &ftrace_pages_start;
5014 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5015 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005016 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005017 /*
Steven Rostedt32082302011-12-16 14:42:37 -05005018 * As core pages are first, the first
5019 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005020 */
Steven Rostedt32082302011-12-16 14:42:37 -05005021 if (WARN_ON(pg == ftrace_pages_start))
5022 goto out_unlock;
5023
5024 /* Check if we are deleting the last page */
5025 if (pg == ftrace_pages)
5026 ftrace_pages = next_to_ftrace_page(last_pg);
5027
5028 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05005029 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5030 free_pages((unsigned long)pg->records, order);
5031 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05005032 } else
5033 last_pg = &pg->next;
5034 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005035 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005036 mutex_unlock(&ftrace_lock);
5037}
5038
Jessica Yu7dcd1822016-02-16 17:32:33 -05005039void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005040{
5041 struct dyn_ftrace *rec;
5042 struct ftrace_page *pg;
5043
5044 mutex_lock(&ftrace_lock);
5045
5046 if (ftrace_disabled)
5047 goto out_unlock;
5048
5049 /*
5050 * If the tracing is enabled, go ahead and enable the record.
5051 *
5052 * The reason not to enable the record immediatelly is the
5053 * inherent check of ftrace_make_nop/ftrace_make_call for
5054 * correct previous instructions. Making first the NOP
5055 * conversion puts the module to the correct state, thus
5056 * passing the ftrace_make_call check.
5057 *
5058 * We also delay this to after the module code already set the
5059 * text to read-only, as we now need to set it back to read-write
5060 * so that we can modify the text.
5061 */
5062 if (ftrace_start_up)
5063 ftrace_arch_code_modify_prepare();
5064
5065 do_for_each_ftrace_rec(pg, rec) {
5066 int cnt;
5067 /*
5068 * do_for_each_ftrace_rec() is a double loop.
5069 * module text shares the pg. If a record is
5070 * not part of this module, then skip this pg,
5071 * which the "break" will do.
5072 */
5073 if (!within_module_core(rec->ip, mod))
5074 break;
5075
5076 cnt = 0;
5077
5078 /*
5079 * When adding a module, we need to check if tracers are
5080 * currently enabled and if they are, and can trace this record,
5081 * we need to enable the module functions as well as update the
5082 * reference counts for those function records.
5083 */
5084 if (ftrace_start_up)
5085 cnt += referenced_filters(rec);
5086
5087 /* This clears FTRACE_FL_DISABLED */
5088 rec->flags = cnt;
5089
5090 if (ftrace_start_up && cnt) {
5091 int failed = __ftrace_replace_code(rec, 1);
5092 if (failed) {
5093 ftrace_bug(failed, rec);
5094 goto out_loop;
5095 }
5096 }
5097
5098 } while_for_each_ftrace_rec();
5099
5100 out_loop:
5101 if (ftrace_start_up)
5102 ftrace_arch_code_modify_post_process();
5103
5104 out_unlock:
5105 mutex_unlock(&ftrace_lock);
5106}
5107
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005108void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005109{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005110 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005111 return;
5112
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005113 ftrace_process_locs(mod, mod->ftrace_callsites,
5114 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005115}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005116#endif /* CONFIG_MODULES */
5117
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005118void __init ftrace_init(void)
5119{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005120 extern unsigned long __start_mcount_loc[];
5121 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005122 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005123 int ret;
5124
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005125 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005126 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005127 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01005128 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005129 goto failed;
5130
5131 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005132 if (!count) {
5133 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005134 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005135 }
5136
5137 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5138 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005139
5140 last_ftrace_enabled = ftrace_enabled = 1;
5141
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005142 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005143 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005144 __stop_mcount_loc);
5145
Steven Rostedt2af15d62009-05-28 13:37:24 -04005146 set_ftrace_early_filters();
5147
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005148 return;
5149 failed:
5150 ftrace_disabled = 1;
5151}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005152
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005153/* Do nothing if arch does not support this */
5154void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5155{
5156}
5157
5158static void ftrace_update_trampoline(struct ftrace_ops *ops)
5159{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005160
5161/*
5162 * Currently there's no safe way to free a trampoline when the kernel
5163 * is configured with PREEMPT. That is because a task could be preempted
5164 * when it jumped to the trampoline, it may be preempted for a long time
5165 * depending on the system load, and currently there's no way to know
5166 * when it will be off the trampoline. If the trampoline is freed
5167 * too early, when the task runs again, it will be executing on freed
5168 * memory and crash.
5169 */
5170#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005171 /* Currently, only non dynamic ops can have a trampoline */
5172 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5173 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005174#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005175
5176 arch_ftrace_update_trampoline(ops);
5177}
5178
Steven Rostedt3d083392008-05-12 21:20:42 +02005179#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005180
Steven Rostedt2b499382011-05-03 22:49:52 -04005181static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005182 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005183 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5184 FTRACE_OPS_FL_INITIALIZED |
5185 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005186};
5187
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005188static int __init ftrace_nodyn_init(void)
5189{
5190 ftrace_enabled = 1;
5191 return 0;
5192}
Steven Rostedt6f415672012-10-05 12:13:07 -04005193core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005194
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005195static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005196static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005197static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005198/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005199# define ftrace_startup(ops, command) \
5200 ({ \
5201 int ___ret = __register_ftrace_function(ops); \
5202 if (!___ret) \
5203 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5204 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005205 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005206# define ftrace_shutdown(ops, command) \
5207 ({ \
5208 int ___ret = __unregister_ftrace_function(ops); \
5209 if (!___ret) \
5210 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5211 ___ret; \
5212 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005213
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005214# define ftrace_startup_sysctl() do { } while (0)
5215# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005216
5217static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005218ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005219{
5220 return 1;
5221}
5222
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005223static void ftrace_update_trampoline(struct ftrace_ops *ops)
5224{
5225}
5226
Steven Rostedt3d083392008-05-12 21:20:42 +02005227#endif /* CONFIG_DYNAMIC_FTRACE */
5228
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005229__init void ftrace_init_global_array_ops(struct trace_array *tr)
5230{
5231 tr->ops = &global_ops;
5232 tr->ops->private = tr;
5233}
5234
5235void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5236{
5237 /* If we filter on pids, update to use the pid function */
5238 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5239 if (WARN_ON(tr->ops->func != ftrace_stub))
5240 printk("ftrace ops had %pS for function\n",
5241 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005242 }
5243 tr->ops->func = func;
5244 tr->ops->private = tr;
5245}
5246
5247void ftrace_reset_array_ops(struct trace_array *tr)
5248{
5249 tr->ops->func = ftrace_stub;
5250}
5251
Masami Hiramatsu25f467d2019-02-24 01:50:20 +09005252static nokprobe_inline void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005253__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005254 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005255{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005256 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005257 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005258
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005259 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5260 if (bit < 0)
5261 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005262
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005263 /*
5264 * Some of the ops may be dynamically allocated,
5265 * they must be freed after a synchronize_sched().
5266 */
5267 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005268
Steven Rostedt0a016402012-11-02 17:03:03 -04005269 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005270 /*
5271 * Check the following for each ops before calling their func:
5272 * if RCU flag is set, then rcu_is_watching() must be true
5273 * if PER_CPU is set, then ftrace_function_local_disable()
5274 * must be false
5275 * Otherwise test if the ip matches the ops filter
5276 *
5277 * If any of the above fails then the op->func() is not executed.
5278 */
5279 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5280 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5281 !ftrace_function_local_disabled(op)) &&
5282 ftrace_ops_test(op, ip, regs)) {
5283
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005284 if (FTRACE_WARN_ON(!op->func)) {
5285 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005286 goto out;
5287 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005288 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005289 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005290 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005291out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005292 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005293 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005294}
5295
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005296/*
5297 * Some archs only support passing ip and parent_ip. Even though
5298 * the list function ignores the op parameter, we do not want any
5299 * C side effects, where a function is called without the caller
5300 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005301 * Archs are to support both the regs and ftrace_ops at the same time.
5302 * If they support ftrace_ops, it is assumed they support regs.
5303 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005304 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5305 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005306 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08005307 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005308 */
5309#if ARCH_SUPPORTS_FTRACE_OPS
5310static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005311 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005312{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005313 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005314}
Masami Hiramatsu25f467d2019-02-24 01:50:20 +09005315NOKPROBE_SYMBOL(ftrace_ops_list_func);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005316#else
5317static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5318{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005319 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005320}
Masami Hiramatsu25f467d2019-02-24 01:50:20 +09005321NOKPROBE_SYMBOL(ftrace_ops_no_ops);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005322#endif
5323
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005324/*
5325 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005326 * recursion, needs RCU protection and/or requires per cpu handling, then
5327 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005328 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005329static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005330 struct ftrace_ops *op, struct pt_regs *regs)
5331{
5332 int bit;
5333
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005334 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5335 return;
5336
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005337 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5338 if (bit < 0)
5339 return;
5340
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005341 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005342
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005343 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5344 !ftrace_function_local_disabled(op)) {
5345 op->func(ip, parent_ip, op, regs);
5346 }
5347
5348 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005349 trace_clear_recursion(bit);
5350}
Masami Hiramatsu25f467d2019-02-24 01:50:20 +09005351NOKPROBE_SYMBOL(ftrace_ops_assist_func);
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005352
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005353/**
5354 * ftrace_ops_get_func - get the function a trampoline should call
5355 * @ops: the ops to get the function for
5356 *
5357 * Normally the mcount trampoline will call the ops->func, but there
5358 * are times that it should not. For example, if the ops does not
5359 * have its own recursion protection, then it should call the
5360 * ftrace_ops_recurs_func() instead.
5361 *
5362 * Returns the function that the trampoline should call for @ops.
5363 */
5364ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5365{
5366 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005367 * If the function does not handle recursion, needs to be RCU safe,
5368 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005369 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005370 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5371 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5372 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005373
5374 return ops->func;
5375}
5376
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005377static void
5378ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5379 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05005380{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005381 struct trace_array *tr = data;
5382 struct trace_pid_list *pid_list;
5383
5384 pid_list = rcu_dereference_sched(tr->function_pids);
5385
5386 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5387 trace_ignore_this_task(pid_list, next));
5388}
5389
5390static void clear_ftrace_pids(struct trace_array *tr)
5391{
5392 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05005393 int cpu;
5394
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005395 pid_list = rcu_dereference_protected(tr->function_pids,
5396 lockdep_is_held(&ftrace_lock));
5397 if (!pid_list)
5398 return;
5399
5400 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5401
5402 for_each_possible_cpu(cpu)
5403 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5404
5405 rcu_assign_pointer(tr->function_pids, NULL);
5406
5407 /* Wait till all users are no longer using pid filtering */
5408 synchronize_sched();
5409
5410 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05005411}
5412
Namhyung Kim7da0f8e2017-04-17 11:44:27 +09005413void ftrace_clear_pids(struct trace_array *tr)
5414{
5415 mutex_lock(&ftrace_lock);
5416
5417 clear_ftrace_pids(tr);
5418
5419 mutex_unlock(&ftrace_lock);
5420}
5421
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005422static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05005423{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005424 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005425 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005426
5427 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005428 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005429
5430 mutex_unlock(&ftrace_lock);
5431}
5432
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005433/* Greater than any max PID */
5434#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5435
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005436static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005437 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005438{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005439 struct trace_pid_list *pid_list;
5440 struct trace_array *tr = m->private;
5441
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005442 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005443 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005444
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005445 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005446
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005447 if (!pid_list)
5448 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5449
5450 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005451}
5452
5453static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5454{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005455 struct trace_array *tr = m->private;
5456 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5457
5458 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005459 return NULL;
5460
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005461 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005462}
5463
5464static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005465 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005466{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005467 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005468 mutex_unlock(&ftrace_lock);
5469}
5470
5471static int fpid_show(struct seq_file *m, void *v)
5472{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005473 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005474 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005475 return 0;
5476 }
5477
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005478 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005479}
5480
5481static const struct seq_operations ftrace_pid_sops = {
5482 .start = fpid_start,
5483 .next = fpid_next,
5484 .stop = fpid_stop,
5485 .show = fpid_show,
5486};
5487
5488static int
5489ftrace_pid_open(struct inode *inode, struct file *file)
5490{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005491 struct trace_array *tr = inode->i_private;
5492 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005493 int ret = 0;
5494
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005495 if (trace_array_get(tr) < 0)
5496 return -ENODEV;
5497
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005498 if ((file->f_mode & FMODE_WRITE) &&
5499 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005500 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005501
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005502 ret = seq_open(file, &ftrace_pid_sops);
5503 if (ret < 0) {
5504 trace_array_put(tr);
5505 } else {
5506 m = file->private_data;
5507 /* copy tr over to seq ops */
5508 m->private = tr;
5509 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005510
5511 return ret;
5512}
5513
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005514static void ignore_task_cpu(void *data)
5515{
5516 struct trace_array *tr = data;
5517 struct trace_pid_list *pid_list;
5518
5519 /*
5520 * This function is called by on_each_cpu() while the
5521 * event_mutex is held.
5522 */
5523 pid_list = rcu_dereference_protected(tr->function_pids,
5524 mutex_is_locked(&ftrace_lock));
5525
5526 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5527 trace_ignore_this_task(pid_list, current));
5528}
5529
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005530static ssize_t
5531ftrace_pid_write(struct file *filp, const char __user *ubuf,
5532 size_t cnt, loff_t *ppos)
5533{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005534 struct seq_file *m = filp->private_data;
5535 struct trace_array *tr = m->private;
5536 struct trace_pid_list *filtered_pids = NULL;
5537 struct trace_pid_list *pid_list;
5538 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005539
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005540 if (!cnt)
5541 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005542
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005543 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005544
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005545 filtered_pids = rcu_dereference_protected(tr->function_pids,
5546 lockdep_is_held(&ftrace_lock));
5547
5548 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5549 if (ret < 0)
5550 goto out;
5551
5552 rcu_assign_pointer(tr->function_pids, pid_list);
5553
5554 if (filtered_pids) {
5555 synchronize_sched();
5556 trace_free_pid_list(filtered_pids);
5557 } else if (pid_list) {
5558 /* Register a probe to set whether to ignore the tracing of a task */
5559 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5560 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005561
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005562 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005563 * Ignoring of pids is done at task switch. But we have to
5564 * check for those tasks that are currently running.
5565 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005566 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005567 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005568
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005569 ftrace_update_pid_func();
5570 ftrace_startup_all(0);
5571 out:
5572 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005573
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005574 if (ret > 0)
5575 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05005576
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005577 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005578}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005579
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005580static int
5581ftrace_pid_release(struct inode *inode, struct file *file)
5582{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005583 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005584
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005585 trace_array_put(tr);
5586
5587 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005588}
5589
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005590static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005591 .open = ftrace_pid_open,
5592 .write = ftrace_pid_write,
5593 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005594 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005595 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005596};
5597
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005598void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005599{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005600 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005601 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005602}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005603
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04005604void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5605 struct dentry *d_tracer)
5606{
5607 /* Only the top level directory has the dyn_tracefs and profile */
5608 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5609
5610 ftrace_init_dyn_tracefs(d_tracer);
5611 ftrace_profile_tracefs(d_tracer);
5612}
5613
Steven Rostedt3d083392008-05-12 21:20:42 +02005614/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005615 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005616 *
5617 * This function should be used by panic code. It stops ftrace
5618 * but in a not so nice way. If you need to simply kill ftrace
5619 * from a non-atomic section, use ftrace_kill.
5620 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005621void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005622{
5623 ftrace_disabled = 1;
5624 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005625 clear_ftrace_function();
5626}
5627
5628/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005629 * Test if ftrace is dead or not.
5630 */
5631int ftrace_is_dead(void)
5632{
5633 return ftrace_disabled;
5634}
5635
5636/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005637 * register_ftrace_function - register a function for profiling
5638 * @ops - ops structure that holds the function for profiling.
5639 *
5640 * Register a function to be called by all functions in the
5641 * kernel.
5642 *
5643 * Note: @ops->func and all the functions it calls must be labeled
5644 * with "notrace", otherwise it will go into a
5645 * recursive loop.
5646 */
5647int register_ftrace_function(struct ftrace_ops *ops)
5648{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005649 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005650
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005651 ftrace_ops_init(ops);
5652
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005653 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005654
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005655 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005656
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005657 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005658
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005659 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005660}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005661EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005662
5663/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005664 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005665 * @ops - ops structure that holds the function to unregister
5666 *
5667 * Unregister a function that was added to be called by ftrace profiling.
5668 */
5669int unregister_ftrace_function(struct ftrace_ops *ops)
5670{
5671 int ret;
5672
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005673 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005674 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005675 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005676
5677 return ret;
5678}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005679EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005680
Ingo Molnare309b412008-05-12 21:20:51 +02005681int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005682ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005683 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005684 loff_t *ppos)
5685{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005686 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005687
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005688 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005689
Steven Rostedt45a4a232011-04-21 23:16:46 -04005690 if (unlikely(ftrace_disabled))
5691 goto out;
5692
5693 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005694
Li Zefana32c7762009-06-26 16:55:51 +08005695 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005696 goto out;
5697
Li Zefana32c7762009-06-26 16:55:51 +08005698 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005699
5700 if (ftrace_enabled) {
5701
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005702 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005703 if (ftrace_ops_list != &ftrace_list_end)
5704 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005705
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05005706 ftrace_startup_sysctl();
5707
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005708 } else {
5709 /* stopping ftrace calls (just send to ftrace_stub) */
5710 ftrace_trace_function = ftrace_stub;
5711
5712 ftrace_shutdown_sysctl();
5713 }
5714
5715 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005716 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005717 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005718}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005719
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005720#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005721
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005722static struct ftrace_ops graph_ops = {
5723 .func = ftrace_stub,
5724 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5725 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005726 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005727 FTRACE_OPS_FL_STUB,
5728#ifdef FTRACE_GRAPH_TRAMP_ADDR
5729 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05005730 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005731#endif
5732 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5733};
5734
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005735void ftrace_graph_sleep_time_control(bool enable)
5736{
5737 fgraph_sleep_time = enable;
5738}
5739
5740void ftrace_graph_graph_time_control(bool enable)
5741{
5742 fgraph_graph_time = enable;
5743}
5744
Steven Rostedte49dc192008-12-02 23:50:05 -05005745int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5746{
5747 return 0;
5748}
5749
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005750/* The callbacks that hook a function */
5751trace_func_graph_ret_t ftrace_graph_return =
5752 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005753trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005754static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005755
5756/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5757static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5758{
5759 int i;
5760 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005761 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5762 struct task_struct *g, *t;
5763
5764 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5765 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5766 * sizeof(struct ftrace_ret_stack),
5767 GFP_KERNEL);
5768 if (!ret_stack_list[i]) {
5769 start = 0;
5770 end = i;
5771 ret = -ENOMEM;
5772 goto free;
5773 }
5774 }
5775
Soumya PN6112a302016-05-17 21:31:14 +05305776 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005777 do_each_thread(g, t) {
5778 if (start == end) {
5779 ret = -EAGAIN;
5780 goto unlock;
5781 }
5782
5783 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005784 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005785 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005786 t->curr_ret_stack = -1;
5787 /* Make sure the tasks see the -1 first: */
5788 smp_wmb();
5789 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005790 }
5791 } while_each_thread(g, t);
5792
5793unlock:
Soumya PN6112a302016-05-17 21:31:14 +05305794 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005795free:
5796 for (i = start; i < end; i++)
5797 kfree(ret_stack_list[i]);
5798 return ret;
5799}
5800
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005801static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02005802ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04005803 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005804{
5805 unsigned long long timestamp;
5806 int index;
5807
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005808 /*
5809 * Does the user want to count the time a function was asleep.
5810 * If so, do not update the time stamps.
5811 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005812 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005813 return;
5814
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005815 timestamp = trace_clock_local();
5816
5817 prev->ftrace_timestamp = timestamp;
5818
5819 /* only process tasks that we timestamped */
5820 if (!next->ftrace_timestamp)
5821 return;
5822
5823 /*
5824 * Update all the counters in next to make up for the
5825 * time next was sleeping.
5826 */
5827 timestamp -= next->ftrace_timestamp;
5828
5829 for (index = next->curr_ret_stack; index >= 0; index--)
5830 next->ret_stack[index].calltime += timestamp;
5831}
5832
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005833/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005834static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005835{
5836 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005837 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005838
5839 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5840 sizeof(struct ftrace_ret_stack *),
5841 GFP_KERNEL);
5842
5843 if (!ret_stack_list)
5844 return -ENOMEM;
5845
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005846 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005847 for_each_online_cpu(cpu) {
5848 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005849 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005850 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005851
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005852 do {
5853 ret = alloc_retstack_tasklist(ret_stack_list);
5854 } while (ret == -EAGAIN);
5855
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005856 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005857 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005858 if (ret)
5859 pr_info("ftrace_graph: Couldn't activate tracepoint"
5860 " probe to kernel_sched_switch\n");
5861 }
5862
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005863 kfree(ret_stack_list);
5864 return ret;
5865}
5866
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005867/*
5868 * Hibernation protection.
5869 * The state of the current task is too much unstable during
5870 * suspend/restore to disk. We want to protect against that.
5871 */
5872static int
5873ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5874 void *unused)
5875{
5876 switch (state) {
5877 case PM_HIBERNATION_PREPARE:
5878 pause_graph_tracing();
5879 break;
5880
5881 case PM_POST_HIBERNATION:
5882 unpause_graph_tracing();
5883 break;
5884 }
5885 return NOTIFY_DONE;
5886}
5887
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005888static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5889{
5890 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5891 return 0;
5892 return __ftrace_graph_entry(trace);
5893}
5894
5895/*
5896 * The function graph tracer should only trace the functions defined
5897 * by set_ftrace_filter and set_ftrace_notrace. If another function
5898 * tracer ops is registered, the graph tracer requires testing the
5899 * function against the global ops, and not just trace any function
5900 * that any ftrace_ops registered.
5901 */
5902static void update_function_graph_func(void)
5903{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005904 struct ftrace_ops *op;
5905 bool do_test = false;
5906
5907 /*
5908 * The graph and global ops share the same set of functions
5909 * to test. If any other ops is on the list, then
5910 * the graph tracing needs to test if its the function
5911 * it should call.
5912 */
5913 do_for_each_ftrace_op(op, ftrace_ops_list) {
5914 if (op != &global_ops && op != &graph_ops &&
5915 op != &ftrace_list_end) {
5916 do_test = true;
5917 /* in double loop, break out with goto */
5918 goto out;
5919 }
5920 } while_for_each_ftrace_op(op);
5921 out:
5922 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005923 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005924 else
5925 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005926}
5927
Mathias Krause8275f692014-03-30 15:31:50 +02005928static struct notifier_block ftrace_suspend_notifier = {
5929 .notifier_call = ftrace_suspend_notifier_call,
5930};
5931
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005932int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5933 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005934{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005935 int ret = 0;
5936
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005937 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005938
Steven Rostedt05ce5812009-03-24 00:18:31 -04005939 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005940 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005941 ret = -EBUSY;
5942 goto out;
5943 }
5944
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005945 register_pm_notifier(&ftrace_suspend_notifier);
5946
Steven Rostedt597af812009-04-03 15:24:12 -04005947 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005948 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005949 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005950 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005951 goto out;
5952 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005953
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005954 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005955
5956 /*
5957 * Update the indirect function to the entryfunc, and the
5958 * function that gets called to the entry_test first. Then
5959 * call the update fgraph entry function to determine if
5960 * the entryfunc should be called directly or not.
5961 */
5962 __ftrace_graph_entry = entryfunc;
5963 ftrace_graph_entry = ftrace_graph_entry_test;
5964 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005965
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005966 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005967out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005968 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005969 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005970}
5971
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005972void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005973{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005974 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005975
Steven Rostedt597af812009-04-03 15:24:12 -04005976 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005977 goto out;
5978
Steven Rostedt597af812009-04-03 15:24:12 -04005979 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005980 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005981 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005982 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005983 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005984 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005985 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005986
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005987 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005988 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005989}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005990
Steven Rostedt868baf02011-02-10 21:26:13 -05005991static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5992
5993static void
5994graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5995{
5996 atomic_set(&t->tracing_graph_pause, 0);
5997 atomic_set(&t->trace_overrun, 0);
5998 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005999 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05006000 smp_wmb();
6001 t->ret_stack = ret_stack;
6002}
6003
6004/*
6005 * Allocate a return stack for the idle task. May be the first
6006 * time through, or it may be done by CPU hotplug online.
6007 */
6008void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6009{
6010 t->curr_ret_stack = -1;
6011 /*
6012 * The idle task has no parent, it either has its own
6013 * stack or no stack at all.
6014 */
6015 if (t->ret_stack)
6016 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6017
6018 if (ftrace_graph_active) {
6019 struct ftrace_ret_stack *ret_stack;
6020
6021 ret_stack = per_cpu(idle_ret_stack, cpu);
6022 if (!ret_stack) {
6023 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6024 * sizeof(struct ftrace_ret_stack),
6025 GFP_KERNEL);
6026 if (!ret_stack)
6027 return;
6028 per_cpu(idle_ret_stack, cpu) = ret_stack;
6029 }
6030 graph_init_task(t, ret_stack);
6031 }
6032}
6033
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006034/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006035void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006036{
Steven Rostedt84047e32009-06-02 16:51:55 -04006037 /* Make sure we do not use the parent ret_stack */
6038 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05006039 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04006040
Steven Rostedt597af812009-04-03 15:24:12 -04006041 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04006042 struct ftrace_ret_stack *ret_stack;
6043
6044 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006045 * sizeof(struct ftrace_ret_stack),
6046 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04006047 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006048 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05006049 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04006050 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006051}
6052
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006053void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006054{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006055 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6056
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006057 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006058 /* NULL must become visible to IRQs before we free it: */
6059 barrier();
6060
6061 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006062}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006063#endif