blob: 527c4d3e8d7f8e4d0f5e017389f04fedf3bc6f79 [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>
Ingo Molnar29930022017-02-08 18:51:36 +010018#include <linux/sched/task.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020019#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020020#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080021#include <linux/suspend.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050022#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020023#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010024#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020025#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050026#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040027#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010028#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020029#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020031#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050032#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020033#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050034#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080035#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020036
Steven Rostedtad8d75f2009-04-14 19:39:12 -040037#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040038
Steven 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
880 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
881 current->ret_stack[index].subtime = 0;
882
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400883 return 1;
884}
885
886static void profile_graph_return(struct ftrace_graph_ret *trace)
887{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400888 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400889 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400890 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400891 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400892
893 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500894 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400895 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400896 goto out;
897
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400898 /* If the calltime was zero'd ignore it */
899 if (!trace->calltime)
900 goto out;
901
Steven Rostedta2a16d62009-03-24 23:17:58 -0400902 calltime = trace->rettime - trace->calltime;
903
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400904 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400905 int index;
906
907 index = trace->depth;
908
909 /* Append this call time to the parent time to subtract */
910 if (index)
911 current->ret_stack[index - 1].subtime += calltime;
912
913 if (current->ret_stack[index].subtime < calltime)
914 calltime -= current->ret_stack[index].subtime;
915 else
916 calltime = 0;
917 }
918
Steven Rostedtcafb1682009-03-24 20:50:39 -0400919 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400920 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400921 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400922 rec->time_squared += calltime * calltime;
923 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400924
Steven Rostedtcafb1682009-03-24 20:50:39 -0400925 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400926 local_irq_restore(flags);
927}
928
929static int register_ftrace_profiler(void)
930{
931 return register_ftrace_graph(&profile_graph_return,
932 &profile_graph_entry);
933}
934
935static void unregister_ftrace_profiler(void)
936{
937 unregister_ftrace_graph();
938}
939#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100940static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400941 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900942 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400943 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400944};
945
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400946static int register_ftrace_profiler(void)
947{
948 return register_ftrace_function(&ftrace_profile_ops);
949}
950
951static void unregister_ftrace_profiler(void)
952{
953 unregister_ftrace_function(&ftrace_profile_ops);
954}
955#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
956
Steven Rostedt493762f2009-03-23 17:12:36 -0400957static ssize_t
958ftrace_profile_write(struct file *filp, const char __user *ubuf,
959 size_t cnt, loff_t *ppos)
960{
961 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400962 int ret;
963
Peter Huewe22fe9b52011-06-07 21:58:27 +0200964 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
965 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 return ret;
967
968 val = !!val;
969
970 mutex_lock(&ftrace_profile_lock);
971 if (ftrace_profile_enabled ^ val) {
972 if (val) {
973 ret = ftrace_profile_init();
974 if (ret < 0) {
975 cnt = ret;
976 goto out;
977 }
978
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400979 ret = register_ftrace_profiler();
980 if (ret < 0) {
981 cnt = ret;
982 goto out;
983 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400984 ftrace_profile_enabled = 1;
985 } else {
986 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400987 /*
988 * unregister_ftrace_profiler calls stop_machine
989 * so this acts like an synchronize_sched.
990 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400991 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400992 }
993 }
994 out:
995 mutex_unlock(&ftrace_profile_lock);
996
Jiri Olsacf8517c2009-10-23 19:36:16 -0400997 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400998
999 return cnt;
1000}
1001
1002static ssize_t
1003ftrace_profile_read(struct file *filp, char __user *ubuf,
1004 size_t cnt, loff_t *ppos)
1005{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001006 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001007 int r;
1008
1009 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1010 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1011}
1012
1013static const struct file_operations ftrace_profile_fops = {
1014 .open = tracing_open_generic,
1015 .read = ftrace_profile_read,
1016 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001017 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001018};
1019
Steven Rostedtcafb1682009-03-24 20:50:39 -04001020/* used to initialize the real stat files */
1021static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001022 .name = "functions",
1023 .stat_start = function_stat_start,
1024 .stat_next = function_stat_next,
1025 .stat_cmp = function_stat_cmp,
1026 .stat_headers = function_stat_headers,
1027 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001028};
1029
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001030static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001031{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001032 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001033 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001034 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001035 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001036 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001037
Steven Rostedtcafb1682009-03-24 20:50:39 -04001038 for_each_possible_cpu(cpu) {
1039 stat = &per_cpu(ftrace_profile_stats, cpu);
1040
Geliang Tang6363c6b2016-03-15 22:12:34 +08001041 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -04001042 if (!name) {
1043 /*
1044 * The files created are permanent, if something happens
1045 * we still do not free memory.
1046 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001047 WARN(1,
1048 "Could not allocate stat file for cpu %d\n",
1049 cpu);
1050 return;
1051 }
1052 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001053 stat->stat.name = name;
1054 ret = register_stat_tracer(&stat->stat);
1055 if (ret) {
1056 WARN(1,
1057 "Could not register function stat for cpu %d\n",
1058 cpu);
1059 kfree(name);
1060 return;
1061 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001062 }
1063
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001064 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001065 d_tracer, NULL, &ftrace_profile_fops);
1066 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001067 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001068}
1069
1070#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001071static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001072{
1073}
1074#endif /* CONFIG_FUNCTION_PROFILER */
1075
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001076static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1077
Pratyush Anand1619dc32015-03-06 23:58:06 +05301078#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1079static int ftrace_graph_active;
1080#else
1081# define ftrace_graph_active 0
1082#endif
1083
Steven Rostedt3d083392008-05-12 21:20:42 +02001084#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001085
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001086static struct ftrace_ops *removed_ops;
1087
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001088/*
1089 * Set when doing a global update, like enabling all recs or disabling them.
1090 * It is not set when just updating a single ftrace_ops.
1091 */
1092static bool update_all_ops;
1093
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001094#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001095# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001096#endif
1097
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001098static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1099
Steven Rostedtb6887d72009-02-17 12:32:04 -05001100struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001101 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001102 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001103 unsigned long flags;
1104 unsigned long ip;
1105 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001106 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001107};
1108
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001109struct ftrace_func_entry {
1110 struct hlist_node hlist;
1111 unsigned long ip;
1112};
1113
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001114/*
1115 * We make these constant because no one should touch them,
1116 * but they are used as the default "empty hash", to avoid allocating
1117 * it all the time. These are in a read only section such that if
1118 * anyone does try to modify it, it will cause an exception.
1119 */
1120static const struct hlist_head empty_buckets[1];
1121static const struct ftrace_hash empty_hash = {
1122 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001124#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001125
Steven Rostedt2b499382011-05-03 22:49:52 -04001126static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001127 .func = ftrace_stub,
1128 .local_hash.notrace_hash = EMPTY_HASH,
1129 .local_hash.filter_hash = EMPTY_HASH,
1130 INIT_OPS_HASH(global_ops)
1131 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001132 FTRACE_OPS_FL_INITIALIZED |
1133 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001134};
1135
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001136/*
1137 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001138 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001139 * not return true for either core_kernel_text() or
1140 * is_module_text_address().
1141 */
1142bool is_ftrace_trampoline(unsigned long addr)
1143{
1144 struct ftrace_ops *op;
1145 bool ret = false;
1146
1147 /*
1148 * Some of the ops may be dynamically allocated,
1149 * they are freed after a synchronize_sched().
1150 */
1151 preempt_disable_notrace();
1152
1153 do_for_each_ftrace_op(op, ftrace_ops_list) {
1154 /*
1155 * This is to check for dynamically allocated trampolines.
1156 * Trampolines that are in kernel text will have
1157 * core_kernel_text() return true.
1158 */
1159 if (op->trampoline && op->trampoline_size)
1160 if (addr >= op->trampoline &&
1161 addr < op->trampoline + op->trampoline_size) {
1162 ret = true;
1163 goto out;
1164 }
1165 } while_for_each_ftrace_op(op);
1166
1167 out:
1168 preempt_enable_notrace();
1169
1170 return ret;
1171}
1172
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001173struct ftrace_page {
1174 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001175 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001176 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001177 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001178};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001179
Steven Rostedta7900872011-12-16 16:23:44 -05001180#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1181#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001182
1183/* estimate from running different kernels */
1184#define NR_TO_INIT 10000
1185
1186static struct ftrace_page *ftrace_pages_start;
1187static struct ftrace_page *ftrace_pages;
1188
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001189static __always_inline unsigned long
1190ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1191{
1192 if (hash->size_bits > 0)
1193 return hash_long(ip, hash->size_bits);
1194
1195 return 0;
1196}
1197
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001198/* Only use this function if ftrace_hash_empty() has already been tested */
1199static __always_inline struct ftrace_func_entry *
1200__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001201{
1202 unsigned long key;
1203 struct ftrace_func_entry *entry;
1204 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001205
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001206 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001207 hhd = &hash->buckets[key];
1208
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001209 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210 if (entry->ip == ip)
1211 return entry;
1212 }
1213 return NULL;
1214}
1215
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001216/**
1217 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1218 * @hash: The hash to look at
1219 * @ip: The instruction pointer to test
1220 *
1221 * Search a given @hash to see if a given instruction pointer (@ip)
1222 * exists in it.
1223 *
1224 * Returns the entry that holds the @ip if found. NULL otherwise.
1225 */
1226struct ftrace_func_entry *
1227ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1228{
1229 if (ftrace_hash_empty(hash))
1230 return NULL;
1231
1232 return __ftrace_lookup_ip(hash, ip);
1233}
1234
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001235static void __add_hash_entry(struct ftrace_hash *hash,
1236 struct ftrace_func_entry *entry)
1237{
1238 struct hlist_head *hhd;
1239 unsigned long key;
1240
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001241 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001242 hhd = &hash->buckets[key];
1243 hlist_add_head(&entry->hlist, hhd);
1244 hash->count++;
1245}
1246
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001247static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1248{
1249 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001250
1251 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1252 if (!entry)
1253 return -ENOMEM;
1254
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001255 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001256 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001257
1258 return 0;
1259}
1260
1261static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001262free_hash_entry(struct ftrace_hash *hash,
1263 struct ftrace_func_entry *entry)
1264{
1265 hlist_del(&entry->hlist);
1266 kfree(entry);
1267 hash->count--;
1268}
1269
1270static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001271remove_hash_entry(struct ftrace_hash *hash,
1272 struct ftrace_func_entry *entry)
1273{
1274 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001275 hash->count--;
1276}
1277
1278static void ftrace_hash_clear(struct ftrace_hash *hash)
1279{
1280 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001281 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001282 struct ftrace_func_entry *entry;
1283 int size = 1 << hash->size_bits;
1284 int i;
1285
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001286 if (!hash->count)
1287 return;
1288
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001289 for (i = 0; i < size; i++) {
1290 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001291 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001292 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001293 }
1294 FTRACE_WARN_ON(hash->count);
1295}
1296
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001297static void free_ftrace_hash(struct ftrace_hash *hash)
1298{
1299 if (!hash || hash == EMPTY_HASH)
1300 return;
1301 ftrace_hash_clear(hash);
1302 kfree(hash->buckets);
1303 kfree(hash);
1304}
1305
Steven Rostedt07fd5512011-05-05 18:03:47 -04001306static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1307{
1308 struct ftrace_hash *hash;
1309
1310 hash = container_of(rcu, struct ftrace_hash, rcu);
1311 free_ftrace_hash(hash);
1312}
1313
1314static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1315{
1316 if (!hash || hash == EMPTY_HASH)
1317 return;
1318 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1319}
1320
Jiri Olsa5500fa52012-02-15 15:51:54 +01001321void ftrace_free_filter(struct ftrace_ops *ops)
1322{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001323 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001324 free_ftrace_hash(ops->func_hash->filter_hash);
1325 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001326}
1327
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1329{
1330 struct ftrace_hash *hash;
1331 int size;
1332
1333 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1334 if (!hash)
1335 return NULL;
1336
1337 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001338 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001339
1340 if (!hash->buckets) {
1341 kfree(hash);
1342 return NULL;
1343 }
1344
1345 hash->size_bits = size_bits;
1346
1347 return hash;
1348}
1349
1350static struct ftrace_hash *
1351alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1352{
1353 struct ftrace_func_entry *entry;
1354 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001355 int size;
1356 int ret;
1357 int i;
1358
1359 new_hash = alloc_ftrace_hash(size_bits);
1360 if (!new_hash)
1361 return NULL;
1362
1363 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001364 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001365 return new_hash;
1366
1367 size = 1 << hash->size_bits;
1368 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001369 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001370 ret = add_hash_entry(new_hash, entry->ip);
1371 if (ret < 0)
1372 goto free_hash;
1373 }
1374 }
1375
1376 FTRACE_WARN_ON(new_hash->count != hash->count);
1377
1378 return new_hash;
1379
1380 free_hash:
1381 free_ftrace_hash(new_hash);
1382 return NULL;
1383}
1384
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001385static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001386ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001387static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001388ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001389
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001390static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1391 struct ftrace_hash *new_hash);
1392
Namhyung Kim3e278c02017-01-20 11:44:45 +09001393static struct ftrace_hash *
1394__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001395{
1396 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001397 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001398 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001399 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001400 int size = src->count;
1401 int bits = 0;
1402 int i;
1403
1404 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001405 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001406 */
Namhyung Kim3e278c02017-01-20 11:44:45 +09001407 if (!src->count)
1408 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001409
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001410 /*
1411 * Make the hash size about 1/2 the # found
1412 */
1413 for (size /= 2; size; size >>= 1)
1414 bits++;
1415
1416 /* Don't allocate too much */
1417 if (bits > FTRACE_HASH_MAX_BITS)
1418 bits = FTRACE_HASH_MAX_BITS;
1419
Steven Rostedt07fd5512011-05-05 18:03:47 -04001420 new_hash = alloc_ftrace_hash(bits);
1421 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001422 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001423
1424 size = 1 << src->size_bits;
1425 for (i = 0; i < size; i++) {
1426 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001427 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001428 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001429 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001430 }
1431 }
1432
Namhyung Kim3e278c02017-01-20 11:44:45 +09001433 return new_hash;
1434}
1435
1436static int
1437ftrace_hash_move(struct ftrace_ops *ops, int enable,
1438 struct ftrace_hash **dst, struct ftrace_hash *src)
1439{
1440 struct ftrace_hash *new_hash;
1441 int ret;
1442
1443 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1444 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1445 return -EINVAL;
1446
1447 new_hash = __ftrace_hash_move(src);
1448 if (!new_hash)
1449 return -ENOMEM;
1450
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001451 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1452 if (enable) {
1453 /* IPMODIFY should be updated only when filter_hash updating */
1454 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1455 if (ret < 0) {
1456 free_ftrace_hash(new_hash);
1457 return ret;
1458 }
1459 }
1460
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001461 /*
1462 * Remove the current set, update the hash and add
1463 * them back.
1464 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001465 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001466
Steven Rostedt07fd5512011-05-05 18:03:47 -04001467 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001468
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001469 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001470
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001471 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001472}
1473
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001474static bool hash_contains_ip(unsigned long ip,
1475 struct ftrace_ops_hash *hash)
1476{
1477 /*
1478 * The function record is a match if it exists in the filter
1479 * hash and not in the notrace hash. Note, an emty hash is
1480 * considered a match for the filter hash, but an empty
1481 * notrace hash is considered not in the notrace hash.
1482 */
1483 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001484 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001485 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001486 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001487}
1488
Steven Rostedt265c8312009-02-13 12:43:56 -05001489/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001490 * Test the hashes for this ops to see if we want to call
1491 * the ops->func or not.
1492 *
1493 * It's a match if the ip is in the ops->filter_hash or
1494 * the filter_hash does not exist or is empty,
1495 * AND
1496 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001497 *
1498 * This needs to be called with preemption disabled as
1499 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001500 */
1501static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001502ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001503{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001504 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001505 int ret;
1506
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001507#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1508 /*
1509 * There's a small race when adding ops that the ftrace handler
1510 * that wants regs, may be called without them. We can not
1511 * allow that handler to be called if regs is NULL.
1512 */
1513 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1514 return 0;
1515#endif
1516
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001517 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1518 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001519
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001520 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001521 ret = 1;
1522 else
1523 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001524
1525 return ret;
1526}
1527
1528/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001529 * This is a double for. Do not use 'break' to break out of the loop,
1530 * you must use a goto.
1531 */
1532#define do_for_each_ftrace_rec(pg, rec) \
1533 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1534 int _____i; \
1535 for (_____i = 0; _____i < pg->index; _____i++) { \
1536 rec = &pg->records[_____i];
1537
1538#define while_for_each_ftrace_rec() \
1539 } \
1540 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301541
Steven Rostedt5855fea2011-12-16 19:27:42 -05001542
1543static int ftrace_cmp_recs(const void *a, const void *b)
1544{
Steven Rostedta650e022012-04-25 13:48:13 -04001545 const struct dyn_ftrace *key = a;
1546 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001547
Steven Rostedta650e022012-04-25 13:48:13 -04001548 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001549 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001550 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1551 return 1;
1552 return 0;
1553}
1554
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001555/**
1556 * ftrace_location_range - return the first address of a traced location
1557 * if it touches the given ip range
1558 * @start: start of range to search.
1559 * @end: end of range to search (inclusive). @end points to the last byte
1560 * to check.
1561 *
1562 * Returns rec->ip if the related ftrace location is a least partly within
1563 * the given address range. That is, the first address of the instruction
1564 * that is either a NOP or call to the function tracer. It checks the ftrace
1565 * internal tables to determine if the address belongs or not.
1566 */
1567unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001568{
1569 struct ftrace_page *pg;
1570 struct dyn_ftrace *rec;
1571 struct dyn_ftrace key;
1572
1573 key.ip = start;
1574 key.flags = end; /* overload flags, as it is unsigned long */
1575
1576 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1577 if (end < pg->records[0].ip ||
1578 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1579 continue;
1580 rec = bsearch(&key, pg->records, pg->index,
1581 sizeof(struct dyn_ftrace),
1582 ftrace_cmp_recs);
1583 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001584 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001585 }
1586
Steven Rostedt5855fea2011-12-16 19:27:42 -05001587 return 0;
1588}
1589
Steven Rostedtc88fd862011-08-16 09:53:39 -04001590/**
1591 * ftrace_location - return true if the ip giving is a traced location
1592 * @ip: the instruction pointer to check
1593 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001594 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001595 * That is, the instruction that is either a NOP or call to
1596 * the function tracer. It checks the ftrace internal tables to
1597 * determine if the address belongs or not.
1598 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001599unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001600{
Steven Rostedta650e022012-04-25 13:48:13 -04001601 return ftrace_location_range(ip, ip);
1602}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001603
Steven Rostedta650e022012-04-25 13:48:13 -04001604/**
1605 * ftrace_text_reserved - return true if range contains an ftrace location
1606 * @start: start of range to search
1607 * @end: end of range to search (inclusive). @end points to the last byte to check.
1608 *
1609 * Returns 1 if @start and @end contains a ftrace location.
1610 * That is, the instruction that is either a NOP or call to
1611 * the function tracer. It checks the ftrace internal tables to
1612 * determine if the address belongs or not.
1613 */
Sasha Levind88471c2013-01-09 18:09:20 -05001614int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001615{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001616 unsigned long ret;
1617
1618 ret = ftrace_location_range((unsigned long)start,
1619 (unsigned long)end);
1620
1621 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001622}
1623
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001624/* Test if ops registered to this rec needs regs */
1625static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1626{
1627 struct ftrace_ops *ops;
1628 bool keep_regs = false;
1629
1630 for (ops = ftrace_ops_list;
1631 ops != &ftrace_list_end; ops = ops->next) {
1632 /* pass rec in as regs to have non-NULL val */
1633 if (ftrace_ops_test(ops, rec->ip, rec)) {
1634 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1635 keep_regs = true;
1636 break;
1637 }
1638 }
1639 }
1640
1641 return keep_regs;
1642}
1643
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001644static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001645 int filter_hash,
1646 bool inc)
1647{
1648 struct ftrace_hash *hash;
1649 struct ftrace_hash *other_hash;
1650 struct ftrace_page *pg;
1651 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001652 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001653 int count = 0;
1654 int all = 0;
1655
1656 /* Only update if the ops has been registered */
1657 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001658 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001659
1660 /*
1661 * In the filter_hash case:
1662 * If the count is zero, we update all records.
1663 * Otherwise we just update the items in the hash.
1664 *
1665 * In the notrace_hash case:
1666 * We enable the update in the hash.
1667 * As disabling notrace means enabling the tracing,
1668 * and enabling notrace means disabling, the inc variable
1669 * gets inversed.
1670 */
1671 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001672 hash = ops->func_hash->filter_hash;
1673 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001674 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001675 all = 1;
1676 } else {
1677 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001678 hash = ops->func_hash->notrace_hash;
1679 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001680 /*
1681 * If the notrace hash has no items,
1682 * then there's nothing to do.
1683 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001684 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001685 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001686 }
1687
1688 do_for_each_ftrace_rec(pg, rec) {
1689 int in_other_hash = 0;
1690 int in_hash = 0;
1691 int match = 0;
1692
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001693 if (rec->flags & FTRACE_FL_DISABLED)
1694 continue;
1695
Steven Rostedted926f92011-05-03 13:25:24 -04001696 if (all) {
1697 /*
1698 * Only the filter_hash affects all records.
1699 * Update if the record is not in the notrace hash.
1700 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001701 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001702 match = 1;
1703 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001704 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1705 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001706
1707 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001708 * If filter_hash is set, we want to match all functions
1709 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001710 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001711 * If filter_hash is not set, then we are decrementing.
1712 * That means we match anything that is in the hash
1713 * and also in the other_hash. That is, we need to turn
1714 * off functions in the other hash because they are disabled
1715 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001716 */
1717 if (filter_hash && in_hash && !in_other_hash)
1718 match = 1;
1719 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001720 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001721 match = 1;
1722 }
1723 if (!match)
1724 continue;
1725
1726 if (inc) {
1727 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001728 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001729 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001730
1731 /*
1732 * If there's only a single callback registered to a
1733 * function, and the ops has a trampoline registered
1734 * for it, then we can call it directly.
1735 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001736 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001737 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001738 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001739 /*
1740 * If we are adding another function callback
1741 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001742 * custom trampoline in use, then we need to go
1743 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001744 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001745 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001746
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001747 /*
1748 * If any ops wants regs saved for this function
1749 * then all ops will get saved regs.
1750 */
1751 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1752 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001753 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001754 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001755 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001756 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001757
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001758 /*
1759 * If the rec had REGS enabled and the ops that is
1760 * being removed had REGS set, then see if there is
1761 * still any ops for this record that wants regs.
1762 * If not, we can stop recording them.
1763 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001764 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001765 rec->flags & FTRACE_FL_REGS &&
1766 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1767 if (!test_rec_ops_needs_regs(rec))
1768 rec->flags &= ~FTRACE_FL_REGS;
1769 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001770
1771 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001772 * If the rec had TRAMP enabled, then it needs to
1773 * be cleared. As TRAMP can only be enabled iff
1774 * there is only a single ops attached to it.
1775 * In otherwords, always disable it on decrementing.
1776 * In the future, we may set it if rec count is
1777 * decremented to one, and the ops that is left
1778 * has a trampoline.
1779 */
1780 rec->flags &= ~FTRACE_FL_TRAMP;
1781
1782 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001783 * flags will be cleared in ftrace_check_record()
1784 * if rec count is zero.
1785 */
Steven Rostedted926f92011-05-03 13:25:24 -04001786 }
1787 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001788
1789 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1790 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1791
Steven Rostedted926f92011-05-03 13:25:24 -04001792 /* Shortcut, if we handled all records, we are done. */
1793 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001794 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001795 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001796
1797 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001798}
1799
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001800static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001801 int filter_hash)
1802{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001803 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001804}
1805
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001806static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001807 int filter_hash)
1808{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001809 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001810}
1811
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001812static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1813 int filter_hash, int inc)
1814{
1815 struct ftrace_ops *op;
1816
1817 __ftrace_hash_rec_update(ops, filter_hash, inc);
1818
1819 if (ops->func_hash != &global_ops.local_hash)
1820 return;
1821
1822 /*
1823 * If the ops shares the global_ops hash, then we need to update
1824 * all ops that are enabled and use this hash.
1825 */
1826 do_for_each_ftrace_op(op, ftrace_ops_list) {
1827 /* Already done */
1828 if (op == ops)
1829 continue;
1830 if (op->func_hash == &global_ops.local_hash)
1831 __ftrace_hash_rec_update(op, filter_hash, inc);
1832 } while_for_each_ftrace_op(op);
1833}
1834
1835static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1836 int filter_hash)
1837{
1838 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1839}
1840
1841static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1842 int filter_hash)
1843{
1844 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1845}
1846
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001847/*
1848 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1849 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1850 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1851 * Note that old_hash and new_hash has below meanings
1852 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1853 * - If the hash is EMPTY_HASH, it hits nothing
1854 * - Anything else hits the recs which match the hash entries.
1855 */
1856static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1857 struct ftrace_hash *old_hash,
1858 struct ftrace_hash *new_hash)
1859{
1860 struct ftrace_page *pg;
1861 struct dyn_ftrace *rec, *end = NULL;
1862 int in_old, in_new;
1863
1864 /* Only update if the ops has been registered */
1865 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1866 return 0;
1867
1868 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1869 return 0;
1870
1871 /*
1872 * Since the IPMODIFY is a very address sensitive action, we do not
1873 * allow ftrace_ops to set all functions to new hash.
1874 */
1875 if (!new_hash || !old_hash)
1876 return -EINVAL;
1877
1878 /* Update rec->flags */
1879 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001880
1881 if (rec->flags & FTRACE_FL_DISABLED)
1882 continue;
1883
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001884 /* We need to update only differences of filter_hash */
1885 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1886 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1887 if (in_old == in_new)
1888 continue;
1889
1890 if (in_new) {
1891 /* New entries must ensure no others are using it */
1892 if (rec->flags & FTRACE_FL_IPMODIFY)
1893 goto rollback;
1894 rec->flags |= FTRACE_FL_IPMODIFY;
1895 } else /* Removed entry */
1896 rec->flags &= ~FTRACE_FL_IPMODIFY;
1897 } while_for_each_ftrace_rec();
1898
1899 return 0;
1900
1901rollback:
1902 end = rec;
1903
1904 /* Roll back what we did above */
1905 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001906
1907 if (rec->flags & FTRACE_FL_DISABLED)
1908 continue;
1909
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001910 if (rec == end)
1911 goto err_out;
1912
1913 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1914 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1915 if (in_old == in_new)
1916 continue;
1917
1918 if (in_new)
1919 rec->flags &= ~FTRACE_FL_IPMODIFY;
1920 else
1921 rec->flags |= FTRACE_FL_IPMODIFY;
1922 } while_for_each_ftrace_rec();
1923
1924err_out:
1925 return -EBUSY;
1926}
1927
1928static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1929{
1930 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1931
1932 if (ftrace_hash_empty(hash))
1933 hash = NULL;
1934
1935 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1936}
1937
1938/* Disabling always succeeds */
1939static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1940{
1941 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1942
1943 if (ftrace_hash_empty(hash))
1944 hash = NULL;
1945
1946 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1947}
1948
1949static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1950 struct ftrace_hash *new_hash)
1951{
1952 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1953
1954 if (ftrace_hash_empty(old_hash))
1955 old_hash = NULL;
1956
1957 if (ftrace_hash_empty(new_hash))
1958 new_hash = NULL;
1959
1960 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1961}
1962
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001963static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001964{
1965 int i;
1966
1967 printk(KERN_CONT "%s", fmt);
1968
1969 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1970 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1971}
1972
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001973static struct ftrace_ops *
1974ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001975static struct ftrace_ops *
1976ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001977
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001978enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001979const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001980
1981static void print_bug_type(void)
1982{
1983 switch (ftrace_bug_type) {
1984 case FTRACE_BUG_UNKNOWN:
1985 break;
1986 case FTRACE_BUG_INIT:
1987 pr_info("Initializing ftrace call sites\n");
1988 break;
1989 case FTRACE_BUG_NOP:
1990 pr_info("Setting ftrace call site to NOP\n");
1991 break;
1992 case FTRACE_BUG_CALL:
1993 pr_info("Setting ftrace call site to call ftrace function\n");
1994 break;
1995 case FTRACE_BUG_UPDATE:
1996 pr_info("Updating ftrace call site to call a different ftrace function\n");
1997 break;
1998 }
1999}
2000
Steven Rostedtc88fd862011-08-16 09:53:39 -04002001/**
2002 * ftrace_bug - report and shutdown function tracer
2003 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002004 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04002005 *
2006 * The arch code that enables or disables the function tracing
2007 * can call ftrace_bug() when it has detected a problem in
2008 * modifying the code. @failed should be one of either:
2009 * EFAULT - if the problem happens on reading the @ip address
2010 * EINVAL - if what is read at @ip is not what was expected
2011 * EPERM - if the problem happens on writting to the @ip address
2012 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002013void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002014{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002015 unsigned long ip = rec ? rec->ip : 0;
2016
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002017 switch (failed) {
2018 case -EFAULT:
2019 FTRACE_WARN_ON_ONCE(1);
2020 pr_info("ftrace faulted on modifying ");
2021 print_ip_sym(ip);
2022 break;
2023 case -EINVAL:
2024 FTRACE_WARN_ON_ONCE(1);
2025 pr_info("ftrace failed to modify ");
2026 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002027 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002028 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002029 if (ftrace_expected) {
2030 print_ip_ins(" expected: ", ftrace_expected);
2031 pr_cont("\n");
2032 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002033 break;
2034 case -EPERM:
2035 FTRACE_WARN_ON_ONCE(1);
2036 pr_info("ftrace faulted on writing ");
2037 print_ip_sym(ip);
2038 break;
2039 default:
2040 FTRACE_WARN_ON_ONCE(1);
2041 pr_info("ftrace faulted on unknown error ");
2042 print_ip_sym(ip);
2043 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002044 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002045 if (rec) {
2046 struct ftrace_ops *ops = NULL;
2047
2048 pr_info("ftrace record flags: %lx\n", rec->flags);
2049 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2050 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2051 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2052 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002053 if (ops) {
2054 do {
2055 pr_cont("\ttramp: %pS (%pS)",
2056 (void *)ops->trampoline,
2057 (void *)ops->func);
2058 ops = ftrace_find_tramp_ops_next(rec, ops);
2059 } while (ops);
2060 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002061 pr_cont("\ttramp: ERROR!");
2062
2063 }
2064 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002065 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002066 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002067}
2068
Steven Rostedtc88fd862011-08-16 09:53:39 -04002069static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002070{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002071 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002072
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002073 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2074
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002075 if (rec->flags & FTRACE_FL_DISABLED)
2076 return FTRACE_UPDATE_IGNORE;
2077
Steven Rostedt982c3502008-11-15 16:31:41 -05002078 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002079 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002080 *
Steven Rostedted926f92011-05-03 13:25:24 -04002081 * If the record has a ref count, then we need to enable it
2082 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002083 *
Steven Rostedted926f92011-05-03 13:25:24 -04002084 * Otherwise we make sure its disabled.
2085 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002086 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002087 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002088 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002089 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002090 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002091
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002092 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002093 * If enabling and the REGS flag does not match the REGS_EN, or
2094 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2095 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002096 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002097 if (flag) {
2098 if (!(rec->flags & FTRACE_FL_REGS) !=
2099 !(rec->flags & FTRACE_FL_REGS_EN))
2100 flag |= FTRACE_FL_REGS;
2101
2102 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2103 !(rec->flags & FTRACE_FL_TRAMP_EN))
2104 flag |= FTRACE_FL_TRAMP;
2105 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002106
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002107 /* If the state of this record hasn't changed, then do nothing */
2108 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002109 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002110
2111 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002112 /* Save off if rec is being enabled (for return value) */
2113 flag ^= rec->flags & FTRACE_FL_ENABLED;
2114
2115 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002116 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002117 if (flag & FTRACE_FL_REGS) {
2118 if (rec->flags & FTRACE_FL_REGS)
2119 rec->flags |= FTRACE_FL_REGS_EN;
2120 else
2121 rec->flags &= ~FTRACE_FL_REGS_EN;
2122 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002123 if (flag & FTRACE_FL_TRAMP) {
2124 if (rec->flags & FTRACE_FL_TRAMP)
2125 rec->flags |= FTRACE_FL_TRAMP_EN;
2126 else
2127 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2128 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002129 }
2130
2131 /*
2132 * If this record is being updated from a nop, then
2133 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002134 * Otherwise,
2135 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002136 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002137 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002138 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002139 if (flag & FTRACE_FL_ENABLED) {
2140 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002141 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002142 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002143
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002144 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002145 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002146 }
2147
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002148 if (update) {
2149 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002150 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002151 rec->flags = 0;
2152 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002153 /*
2154 * Just disable the record, but keep the ops TRAMP
2155 * and REGS states. The _EN flags must be disabled though.
2156 */
2157 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2158 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002159 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002160
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002161 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002162 return FTRACE_UPDATE_MAKE_NOP;
2163}
2164
2165/**
2166 * ftrace_update_record, set a record that now is tracing or not
2167 * @rec: the record to update
2168 * @enable: set to 1 if the record is tracing, zero to force disable
2169 *
2170 * The records that represent all functions that can be traced need
2171 * to be updated when tracing has been enabled.
2172 */
2173int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2174{
2175 return ftrace_check_record(rec, enable, 1);
2176}
2177
2178/**
2179 * ftrace_test_record, check if the record has been enabled or not
2180 * @rec: the record to test
2181 * @enable: set to 1 to check if enabled, 0 if it is disabled
2182 *
2183 * The arch code may need to test if a record is already set to
2184 * tracing to determine how to modify the function code that it
2185 * represents.
2186 */
2187int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2188{
2189 return ftrace_check_record(rec, enable, 0);
2190}
2191
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002192static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002193ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2194{
2195 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002196 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002197
2198 do_for_each_ftrace_op(op, ftrace_ops_list) {
2199
2200 if (!op->trampoline)
2201 continue;
2202
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002203 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002204 return op;
2205 } while_for_each_ftrace_op(op);
2206
2207 return NULL;
2208}
2209
2210static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002211ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2212 struct ftrace_ops *op)
2213{
2214 unsigned long ip = rec->ip;
2215
2216 while_for_each_ftrace_op(op) {
2217
2218 if (!op->trampoline)
2219 continue;
2220
2221 if (hash_contains_ip(ip, op->func_hash))
2222 return op;
2223 }
2224
2225 return NULL;
2226}
2227
2228static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002229ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2230{
2231 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002232 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002233
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002234 /*
2235 * Need to check removed ops first.
2236 * If they are being removed, and this rec has a tramp,
2237 * and this rec is in the ops list, then it would be the
2238 * one with the tramp.
2239 */
2240 if (removed_ops) {
2241 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002242 return removed_ops;
2243 }
2244
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002245 /*
2246 * Need to find the current trampoline for a rec.
2247 * Now, a trampoline is only attached to a rec if there
2248 * was a single 'ops' attached to it. But this can be called
2249 * when we are adding another op to the rec or removing the
2250 * current one. Thus, if the op is being added, we can
2251 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002252 * yet.
2253 *
2254 * If an ops is being modified (hooking to different functions)
2255 * then we don't care about the new functions that are being
2256 * added, just the old ones (that are probably being removed).
2257 *
2258 * If we are adding an ops to a function that already is using
2259 * a trampoline, it needs to be removed (trampolines are only
2260 * for single ops connected), then an ops that is not being
2261 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002262 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002263 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002264
2265 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002266 continue;
2267
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002268 /*
2269 * If the ops is being added, it hasn't gotten to
2270 * the point to be removed from this tree yet.
2271 */
2272 if (op->flags & FTRACE_OPS_FL_ADDING)
2273 continue;
2274
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002275
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002276 /*
2277 * If the ops is being modified and is in the old
2278 * hash, then it is probably being removed from this
2279 * function.
2280 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002281 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2282 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002283 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002284 /*
2285 * If the ops is not being added or modified, and it's
2286 * in its normal filter hash, then this must be the one
2287 * we want!
2288 */
2289 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2290 hash_contains_ip(ip, op->func_hash))
2291 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002292
2293 } while_for_each_ftrace_op(op);
2294
2295 return NULL;
2296}
2297
2298static struct ftrace_ops *
2299ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2300{
2301 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002302 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002303
2304 do_for_each_ftrace_op(op, ftrace_ops_list) {
2305 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002306 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002307 return op;
2308 } while_for_each_ftrace_op(op);
2309
2310 return NULL;
2311}
2312
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002313/**
2314 * ftrace_get_addr_new - Get the call address to set to
2315 * @rec: The ftrace record descriptor
2316 *
2317 * If the record has the FTRACE_FL_REGS set, that means that it
2318 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2319 * is not not set, then it wants to convert to the normal callback.
2320 *
2321 * Returns the address of the trampoline to set to
2322 */
2323unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2324{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002325 struct ftrace_ops *ops;
2326
2327 /* Trampolines take precedence over regs */
2328 if (rec->flags & FTRACE_FL_TRAMP) {
2329 ops = ftrace_find_tramp_ops_new(rec);
2330 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002331 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2332 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002333 /* Ftrace is shutting down, return anything */
2334 return (unsigned long)FTRACE_ADDR;
2335 }
2336 return ops->trampoline;
2337 }
2338
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002339 if (rec->flags & FTRACE_FL_REGS)
2340 return (unsigned long)FTRACE_REGS_ADDR;
2341 else
2342 return (unsigned long)FTRACE_ADDR;
2343}
2344
2345/**
2346 * ftrace_get_addr_curr - Get the call address that is already there
2347 * @rec: The ftrace record descriptor
2348 *
2349 * The FTRACE_FL_REGS_EN is set when the record already points to
2350 * a function that saves all the regs. Basically the '_EN' version
2351 * represents the current state of the function.
2352 *
2353 * Returns the address of the trampoline that is currently being called
2354 */
2355unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2356{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002357 struct ftrace_ops *ops;
2358
2359 /* Trampolines take precedence over regs */
2360 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2361 ops = ftrace_find_tramp_ops_curr(rec);
2362 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002363 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2364 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002365 /* Ftrace is shutting down, return anything */
2366 return (unsigned long)FTRACE_ADDR;
2367 }
2368 return ops->trampoline;
2369 }
2370
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002371 if (rec->flags & FTRACE_FL_REGS_EN)
2372 return (unsigned long)FTRACE_REGS_ADDR;
2373 else
2374 return (unsigned long)FTRACE_ADDR;
2375}
2376
Steven Rostedtc88fd862011-08-16 09:53:39 -04002377static int
2378__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2379{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002380 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002381 unsigned long ftrace_addr;
2382 int ret;
2383
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002384 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002385
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002386 /* This needs to be done before we call ftrace_update_record */
2387 ftrace_old_addr = ftrace_get_addr_curr(rec);
2388
2389 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002390
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002391 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2392
Steven Rostedtc88fd862011-08-16 09:53:39 -04002393 switch (ret) {
2394 case FTRACE_UPDATE_IGNORE:
2395 return 0;
2396
2397 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002398 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002399 return ftrace_make_call(rec, ftrace_addr);
2400
2401 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002402 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002403 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002404
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002405 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002406 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002407 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002408 }
2409
2410 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002411}
2412
Steven Rostedte4f5d542012-04-27 09:13:18 -04002413void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002414{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002415 struct dyn_ftrace *rec;
2416 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002417 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002418
Steven Rostedt45a4a232011-04-21 23:16:46 -04002419 if (unlikely(ftrace_disabled))
2420 return;
2421
Steven Rostedt265c8312009-02-13 12:43:56 -05002422 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002423
2424 if (rec->flags & FTRACE_FL_DISABLED)
2425 continue;
2426
Steven Rostedte4f5d542012-04-27 09:13:18 -04002427 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002428 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002429 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002430 /* Stop processing */
2431 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002432 }
2433 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002434}
2435
Steven Rostedtc88fd862011-08-16 09:53:39 -04002436struct ftrace_rec_iter {
2437 struct ftrace_page *pg;
2438 int index;
2439};
2440
2441/**
2442 * ftrace_rec_iter_start, start up iterating over traced functions
2443 *
2444 * Returns an iterator handle that is used to iterate over all
2445 * the records that represent address locations where functions
2446 * are traced.
2447 *
2448 * May return NULL if no records are available.
2449 */
2450struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2451{
2452 /*
2453 * We only use a single iterator.
2454 * Protected by the ftrace_lock mutex.
2455 */
2456 static struct ftrace_rec_iter ftrace_rec_iter;
2457 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2458
2459 iter->pg = ftrace_pages_start;
2460 iter->index = 0;
2461
2462 /* Could have empty pages */
2463 while (iter->pg && !iter->pg->index)
2464 iter->pg = iter->pg->next;
2465
2466 if (!iter->pg)
2467 return NULL;
2468
2469 return iter;
2470}
2471
2472/**
2473 * ftrace_rec_iter_next, get the next record to process.
2474 * @iter: The handle to the iterator.
2475 *
2476 * Returns the next iterator after the given iterator @iter.
2477 */
2478struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2479{
2480 iter->index++;
2481
2482 if (iter->index >= iter->pg->index) {
2483 iter->pg = iter->pg->next;
2484 iter->index = 0;
2485
2486 /* Could have empty pages */
2487 while (iter->pg && !iter->pg->index)
2488 iter->pg = iter->pg->next;
2489 }
2490
2491 if (!iter->pg)
2492 return NULL;
2493
2494 return iter;
2495}
2496
2497/**
2498 * ftrace_rec_iter_record, get the record at the iterator location
2499 * @iter: The current iterator location
2500 *
2501 * Returns the record that the current @iter is at.
2502 */
2503struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2504{
2505 return &iter->pg->records[iter->index];
2506}
2507
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302508static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002509ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002510{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002511 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002512
Steven Rostedt45a4a232011-04-21 23:16:46 -04002513 if (unlikely(ftrace_disabled))
2514 return 0;
2515
Shaohua Li25aac9d2009-01-09 11:29:40 +08002516 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002517 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002518 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002519 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302520 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002521 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302522 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002523}
2524
Steven Rostedt000ab692009-02-17 13:35:06 -05002525/*
2526 * archs can override this function if they must do something
2527 * before the modifying code is performed.
2528 */
2529int __weak ftrace_arch_code_modify_prepare(void)
2530{
2531 return 0;
2532}
2533
2534/*
2535 * archs can override this function if they must do something
2536 * after the modifying code is performed.
2537 */
2538int __weak ftrace_arch_code_modify_post_process(void)
2539{
2540 return 0;
2541}
2542
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002543void ftrace_modify_all_code(int command)
2544{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002545 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002546 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002547
2548 /*
2549 * If the ftrace_caller calls a ftrace_ops func directly,
2550 * we need to make sure that it only traces functions it
2551 * expects to trace. When doing the switch of functions,
2552 * we need to update to the ftrace_ops_list_func first
2553 * before the transition between old and new calls are set,
2554 * as the ftrace_ops_list_func will check the ops hashes
2555 * to make sure the ops are having the right functions
2556 * traced.
2557 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002558 if (update) {
2559 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2560 if (FTRACE_WARN_ON(err))
2561 return;
2562 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002563
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002564 if (command & FTRACE_UPDATE_CALLS)
2565 ftrace_replace_code(1);
2566 else if (command & FTRACE_DISABLE_CALLS)
2567 ftrace_replace_code(0);
2568
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002569 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2570 function_trace_op = set_function_trace_op;
2571 smp_wmb();
2572 /* If irqs are disabled, we are in stop machine */
2573 if (!irqs_disabled())
2574 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002575 err = ftrace_update_ftrace_func(ftrace_trace_function);
2576 if (FTRACE_WARN_ON(err))
2577 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002578 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002579
2580 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002581 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002582 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002583 err = ftrace_disable_ftrace_graph_caller();
2584 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002585}
2586
Ingo Molnare309b412008-05-12 21:20:51 +02002587static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002588{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002589 int *command = data;
2590
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002591 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002592
Steven Rostedtc88fd862011-08-16 09:53:39 -04002593 return 0;
2594}
2595
2596/**
2597 * ftrace_run_stop_machine, go back to the stop machine method
2598 * @command: The command to tell ftrace what to do
2599 *
2600 * If an arch needs to fall back to the stop machine method, the
2601 * it can call this function.
2602 */
2603void ftrace_run_stop_machine(int command)
2604{
2605 stop_machine(__ftrace_modify_code, &command, NULL);
2606}
2607
2608/**
2609 * arch_ftrace_update_code, modify the code to trace or not trace
2610 * @command: The command that needs to be done
2611 *
2612 * Archs can override this function if it does not need to
2613 * run stop_machine() to modify code.
2614 */
2615void __weak arch_ftrace_update_code(int command)
2616{
2617 ftrace_run_stop_machine(command);
2618}
2619
2620static void ftrace_run_update_code(int command)
2621{
2622 int ret;
2623
2624 ret = ftrace_arch_code_modify_prepare();
2625 FTRACE_WARN_ON(ret);
2626 if (ret)
2627 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002628
2629 /*
2630 * By default we use stop_machine() to modify the code.
2631 * But archs can do what ever they want as long as it
2632 * is safe. The stop_machine() is the safest, but also
2633 * produces the most overhead.
2634 */
2635 arch_ftrace_update_code(command);
2636
Steven Rostedt000ab692009-02-17 13:35:06 -05002637 ret = ftrace_arch_code_modify_post_process();
2638 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002639}
2640
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002641static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002642 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002643{
2644 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002645 ops->old_hash.filter_hash = old_hash->filter_hash;
2646 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002647 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002648 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002649 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002650 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2651}
2652
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002653static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002654static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002655
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002656void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2657{
2658}
2659
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002660static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002661{
2662 free_percpu(ops->disabled);
2663}
2664
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002665static void ftrace_startup_enable(int command)
2666{
2667 if (saved_ftrace_func != ftrace_trace_function) {
2668 saved_ftrace_func = ftrace_trace_function;
2669 command |= FTRACE_UPDATE_TRACE_FUNC;
2670 }
2671
2672 if (!command || !ftrace_enabled)
2673 return;
2674
2675 ftrace_run_update_code(command);
2676}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002677
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002678static void ftrace_startup_all(int command)
2679{
2680 update_all_ops = true;
2681 ftrace_startup_enable(command);
2682 update_all_ops = false;
2683}
2684
Steven Rostedta1cd6172011-05-23 15:24:25 -04002685static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002686{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002687 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002688
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002689 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002690 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002691
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002692 ret = __register_ftrace_function(ops);
2693 if (ret)
2694 return ret;
2695
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002696 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002697
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002698 /*
2699 * Note that ftrace probes uses this to start up
2700 * and modify functions it will probe. But we still
2701 * set the ADDING flag for modification, as probes
2702 * do not have trampolines. If they add them in the
2703 * future, then the probes will need to distinguish
2704 * between adding and updating probes.
2705 */
2706 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002707
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002708 ret = ftrace_hash_ipmodify_enable(ops);
2709 if (ret < 0) {
2710 /* Rollback registration process */
2711 __unregister_ftrace_function(ops);
2712 ftrace_start_up--;
2713 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2714 return ret;
2715 }
2716
Jiri Olsa7f50d062016-03-16 15:34:33 +01002717 if (ftrace_hash_rec_enable(ops, 1))
2718 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002719
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002720 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002721
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002722 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2723
Steven Rostedta1cd6172011-05-23 15:24:25 -04002724 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002725}
2726
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002727static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002728{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002729 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002730
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002731 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002732 return -ENODEV;
2733
2734 ret = __unregister_ftrace_function(ops);
2735 if (ret)
2736 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002737
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002738 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002739 /*
2740 * Just warn in case of unbalance, no need to kill ftrace, it's not
2741 * critical but the ftrace_call callers may be never nopped again after
2742 * further ftrace uses.
2743 */
2744 WARN_ON_ONCE(ftrace_start_up < 0);
2745
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002746 /* Disabling ipmodify never fails */
2747 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002748
2749 if (ftrace_hash_rec_disable(ops, 1))
2750 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002751
Namhyung Kima737e6d2014-06-12 23:56:12 +09002752 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002753
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002754 if (saved_ftrace_func != ftrace_trace_function) {
2755 saved_ftrace_func = ftrace_trace_function;
2756 command |= FTRACE_UPDATE_TRACE_FUNC;
2757 }
2758
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002759 if (!command || !ftrace_enabled) {
2760 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002761 * If these are per_cpu ops, they still need their
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002762 * per_cpu field freed. Since, function tracing is
2763 * not currently active, we can just free them
2764 * without synchronizing all CPUs.
2765 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002766 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2767 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002768 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002769 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002770
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002771 /*
2772 * If the ops uses a trampoline, then it needs to be
2773 * tested first on update.
2774 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002775 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002776 removed_ops = ops;
2777
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002778 /* The trampoline logic checks the old hashes */
2779 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2780 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2781
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002782 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002783
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002784 /*
2785 * If there's no more ops registered with ftrace, run a
2786 * sanity check to make sure all rec flags are cleared.
2787 */
2788 if (ftrace_ops_list == &ftrace_list_end) {
2789 struct ftrace_page *pg;
2790 struct dyn_ftrace *rec;
2791
2792 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002793 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002794 pr_warn(" %pS flags:%lx\n",
2795 (void *)rec->ip, rec->flags);
2796 } while_for_each_ftrace_rec();
2797 }
2798
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002799 ops->old_hash.filter_hash = NULL;
2800 ops->old_hash.notrace_hash = NULL;
2801
2802 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002803 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002804
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002805 /*
2806 * Dynamic ops may be freed, we must make sure that all
2807 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002808 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002809 * ops.
2810 *
2811 * Again, normal synchronize_sched() is not good enough.
2812 * We need to do a hard force of sched synchronization.
2813 * This is because we use preempt_disable() to do RCU, but
2814 * the function tracers can be called where RCU is not watching
2815 * (like before user_exit()). We can not rely on the RCU
2816 * infrastructure to do the synchronization, thus we must do it
2817 * ourselves.
2818 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002819 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002820 schedule_on_each_cpu(ftrace_sync);
2821
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002822 arch_ftrace_trampoline_free(ops);
2823
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002824 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2825 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002826 }
2827
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002828 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002829}
2830
Ingo Molnare309b412008-05-12 21:20:51 +02002831static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002832{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302833 int command;
2834
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002835 if (unlikely(ftrace_disabled))
2836 return;
2837
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002838 /* Force update next time */
2839 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002840 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302841 if (ftrace_start_up) {
2842 command = FTRACE_UPDATE_CALLS;
2843 if (ftrace_graph_active)
2844 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002845 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302846 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002847}
2848
Ingo Molnare309b412008-05-12 21:20:51 +02002849static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002850{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302851 int command;
2852
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002853 if (unlikely(ftrace_disabled))
2854 return;
2855
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002856 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302857 if (ftrace_start_up) {
2858 command = FTRACE_DISABLE_CALLS;
2859 if (ftrace_graph_active)
2860 command |= FTRACE_STOP_FUNC_RET;
2861 ftrace_run_update_code(command);
2862 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002863}
2864
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002865static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002866unsigned long ftrace_update_tot_cnt;
2867
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002868static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002869{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002870 /*
2871 * Filter_hash being empty will default to trace module.
2872 * But notrace hash requires a test of individual module functions.
2873 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002874 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2875 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002876}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002877
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002878/*
2879 * Check if the current ops references the record.
2880 *
2881 * If the ops traces all functions, then it was already accounted for.
2882 * If the ops does not trace the current record function, skip it.
2883 * If the ops ignores the function via notrace filter, skip it.
2884 */
2885static inline bool
2886ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2887{
2888 /* If ops isn't enabled, ignore it */
2889 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2890 return 0;
2891
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002892 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002893 if (ops_traces_mod(ops))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002894 return 1;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002895
2896 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002897 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002898 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002899 return 0;
2900
2901 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002902 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002903 return 0;
2904
2905 return 1;
2906}
2907
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002908static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002909{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002910 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002911 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002912 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002913 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002914 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002915 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002916
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002917 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002918
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002919 /*
2920 * When a module is loaded, this function is called to convert
2921 * the calls to mcount in its text to nops, and also to create
2922 * an entry in the ftrace data. Now, if ftrace is activated
2923 * after this call, but before the module sets its text to
2924 * read-only, the modification of enabling ftrace can fail if
2925 * the read-only is done while ftrace is converting the calls.
2926 * To prevent this, the module's records are set as disabled
2927 * and will be enabled after the call to set the module's text
2928 * to read-only.
2929 */
2930 if (mod)
2931 rec_flags |= FTRACE_FL_DISABLED;
2932
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002933 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302934
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002935 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002936
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002937 /* If something went wrong, bail without enabling anything */
2938 if (unlikely(ftrace_disabled))
2939 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002940
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002941 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002942 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302943
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002944 /*
2945 * Do the initial record conversion from mcount jump
2946 * to the NOP instructions.
2947 */
2948 if (!ftrace_code_disable(mod, p))
2949 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002950
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002951 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002952 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002953 }
2954
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002955 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002956 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002957 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002958
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002959 return 0;
2960}
2961
Steven Rostedta7900872011-12-16 16:23:44 -05002962static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002963{
Steven Rostedta7900872011-12-16 16:23:44 -05002964 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002965 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002966
Steven Rostedta7900872011-12-16 16:23:44 -05002967 if (WARN_ON(!count))
2968 return -EINVAL;
2969
2970 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002971
2972 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002973 * We want to fill as much as possible. No more than a page
2974 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002975 */
Steven Rostedta7900872011-12-16 16:23:44 -05002976 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2977 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002978
Steven Rostedta7900872011-12-16 16:23:44 -05002979 again:
2980 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2981
2982 if (!pg->records) {
2983 /* if we can't allocate this size, try something smaller */
2984 if (!order)
2985 return -ENOMEM;
2986 order >>= 1;
2987 goto again;
2988 }
2989
2990 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2991 pg->size = cnt;
2992
2993 if (cnt > count)
2994 cnt = count;
2995
2996 return cnt;
2997}
2998
2999static struct ftrace_page *
3000ftrace_allocate_pages(unsigned long num_to_init)
3001{
3002 struct ftrace_page *start_pg;
3003 struct ftrace_page *pg;
3004 int order;
3005 int cnt;
3006
3007 if (!num_to_init)
3008 return 0;
3009
3010 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3011 if (!pg)
3012 return NULL;
3013
3014 /*
3015 * Try to allocate as much as possible in one continues
3016 * location that fills in all of the space. We want to
3017 * waste as little space as possible.
3018 */
3019 for (;;) {
3020 cnt = ftrace_allocate_records(pg, num_to_init);
3021 if (cnt < 0)
3022 goto free_pages;
3023
3024 num_to_init -= cnt;
3025 if (!num_to_init)
3026 break;
3027
3028 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3029 if (!pg->next)
3030 goto free_pages;
3031
3032 pg = pg->next;
3033 }
3034
3035 return start_pg;
3036
3037 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003038 pg = start_pg;
3039 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003040 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3041 free_pages((unsigned long)pg->records, order);
3042 start_pg = pg->next;
3043 kfree(pg);
3044 pg = start_pg;
3045 }
3046 pr_info("ftrace: FAILED to allocate memory for functions\n");
3047 return NULL;
3048}
3049
Steven Rostedt5072c592008-05-12 21:20:43 +02003050#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3051
3052struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003053 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003054 loff_t func_pos;
3055 struct ftrace_page *pg;
3056 struct dyn_ftrace *func;
3057 struct ftrace_func_probe *probe;
3058 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003059 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003060 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003061 int hidx;
3062 int idx;
3063 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003064};
3065
Ingo Molnare309b412008-05-12 21:20:51 +02003066static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003067t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003068{
3069 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003070 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003071 struct hlist_head *hhd;
3072
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003073 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003074 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003075
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003076 if (iter->probe)
3077 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003078 retry:
3079 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3080 return NULL;
3081
3082 hhd = &ftrace_func_hash[iter->hidx];
3083
3084 if (hlist_empty(hhd)) {
3085 iter->hidx++;
3086 hnd = NULL;
3087 goto retry;
3088 }
3089
3090 if (!hnd)
3091 hnd = hhd->first;
3092 else {
3093 hnd = hnd->next;
3094 if (!hnd) {
3095 iter->hidx++;
3096 goto retry;
3097 }
3098 }
3099
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003100 if (WARN_ON_ONCE(!hnd))
3101 return NULL;
3102
3103 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3104
3105 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003106}
3107
3108static void *t_hash_start(struct seq_file *m, loff_t *pos)
3109{
3110 struct ftrace_iterator *iter = m->private;
3111 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003112 loff_t l;
3113
Steven Rostedt69a30832011-12-19 15:21:16 -05003114 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3115 return NULL;
3116
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003117 if (iter->func_pos > *pos)
3118 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003119
Li Zefand82d6242009-06-24 09:54:54 +08003120 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003121 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003122 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003123 if (!p)
3124 break;
3125 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003126 if (!p)
3127 return NULL;
3128
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003129 /* Only set this if we have an item */
3130 iter->flags |= FTRACE_ITER_HASH;
3131
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003132 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003133}
3134
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003135static int
3136t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003137{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003138 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003139
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003140 rec = iter->probe;
3141 if (WARN_ON_ONCE(!rec))
3142 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003143
Steven Rostedt809dcf22009-02-16 23:06:01 -05003144 if (rec->ops->print)
3145 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3146
Steven Rostedtb375a112009-09-17 00:05:58 -04003147 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003148
3149 if (rec->data)
3150 seq_printf(m, ":%p", rec->data);
3151 seq_putc(m, '\n');
3152
3153 return 0;
3154}
3155
3156static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02003157t_next(struct seq_file *m, void *v, loff_t *pos)
3158{
3159 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003160 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003161 struct dyn_ftrace *rec = NULL;
3162
Steven Rostedt45a4a232011-04-21 23:16:46 -04003163 if (unlikely(ftrace_disabled))
3164 return NULL;
3165
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003166 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003167 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003168
Steven Rostedt5072c592008-05-12 21:20:43 +02003169 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01003170 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02003171
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003172 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003173 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003174
Steven Rostedt5072c592008-05-12 21:20:43 +02003175 retry:
3176 if (iter->idx >= iter->pg->index) {
3177 if (iter->pg->next) {
3178 iter->pg = iter->pg->next;
3179 iter->idx = 0;
3180 goto retry;
3181 }
3182 } else {
3183 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05003184 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003185 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05003186
Steven Rostedt41c52c02008-05-22 11:46:33 -04003187 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003188 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003189
3190 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003191 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003192
Steven Rostedt5072c592008-05-12 21:20:43 +02003193 rec = NULL;
3194 goto retry;
3195 }
3196 }
3197
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003198 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003199 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003200
3201 iter->func = rec;
3202
3203 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003204}
3205
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003206static void reset_iter_read(struct ftrace_iterator *iter)
3207{
3208 iter->pos = 0;
3209 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03003210 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003211}
3212
3213static void *t_start(struct seq_file *m, loff_t *pos)
3214{
3215 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003216 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003217 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003218 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003219
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003220 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003221
3222 if (unlikely(ftrace_disabled))
3223 return NULL;
3224
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003225 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003226 * If an lseek was done, then reset and start from beginning.
3227 */
3228 if (*pos < iter->pos)
3229 reset_iter_read(iter);
3230
3231 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003232 * For set_ftrace_filter reading, if we have the filter
3233 * off, we can short cut and just print out that all
3234 * functions are enabled.
3235 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003236 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003237 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003238 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003239 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003240 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003241 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003242 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003243 /* reset in case of seek/pread */
3244 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003245 return iter;
3246 }
3247
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003248 if (iter->flags & FTRACE_ITER_HASH)
3249 return t_hash_start(m, pos);
3250
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003251 /*
3252 * Unfortunately, we need to restart at ftrace_pages_start
3253 * every time we let go of the ftrace_mutex. This is because
3254 * those pointers can change without the lock.
3255 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003256 iter->pg = ftrace_pages_start;
3257 iter->idx = 0;
3258 for (l = 0; l <= *pos; ) {
3259 p = t_next(m, p, &l);
3260 if (!p)
3261 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003262 }
walimis5821e1b2008-11-15 15:19:06 +08003263
Steven Rostedt69a30832011-12-19 15:21:16 -05003264 if (!p)
3265 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003266
3267 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003268}
3269
3270static void t_stop(struct seq_file *m, void *p)
3271{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003272 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003273}
3274
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003275void * __weak
3276arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3277{
3278 return NULL;
3279}
3280
3281static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3282 struct dyn_ftrace *rec)
3283{
3284 void *ptr;
3285
3286 ptr = arch_ftrace_trampoline_func(ops, rec);
3287 if (ptr)
3288 seq_printf(m, " ->%pS", ptr);
3289}
3290
Steven Rostedt5072c592008-05-12 21:20:43 +02003291static int t_show(struct seq_file *m, void *v)
3292{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003293 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003294 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003295
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003296 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003297 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003298
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003299 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003300 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003301 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003302 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003303 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003304 return 0;
3305 }
3306
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003307 rec = iter->func;
3308
Steven Rostedt5072c592008-05-12 21:20:43 +02003309 if (!rec)
3310 return 0;
3311
Steven Rostedt647bcd02011-05-03 14:39:21 -04003312 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003313 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003314 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003315
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003316 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003317 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003318 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3319 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003320 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003321 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003322 if (ops) {
3323 do {
3324 seq_printf(m, "\ttramp: %pS (%pS)",
3325 (void *)ops->trampoline,
3326 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003327 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003328 ops = ftrace_find_tramp_ops_next(rec, ops);
3329 } while (ops);
3330 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003331 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003332 } else {
3333 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003334 }
3335 }
3336
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003337 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003338
3339 return 0;
3340}
3341
James Morris88e9d342009-09-22 16:43:43 -07003342static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003343 .start = t_start,
3344 .next = t_next,
3345 .stop = t_stop,
3346 .show = t_show,
3347};
3348
Ingo Molnare309b412008-05-12 21:20:51 +02003349static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003350ftrace_avail_open(struct inode *inode, struct file *file)
3351{
3352 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003353
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003354 if (unlikely(ftrace_disabled))
3355 return -ENODEV;
3356
Jiri Olsa50e18b92012-04-25 10:23:39 +02003357 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003358 if (!iter)
3359 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003360
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003361 iter->pg = ftrace_pages_start;
3362 iter->ops = &global_ops;
3363
3364 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003365}
3366
Steven Rostedt647bcd02011-05-03 14:39:21 -04003367static int
3368ftrace_enabled_open(struct inode *inode, struct file *file)
3369{
3370 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003371
Jiri Olsa50e18b92012-04-25 10:23:39 +02003372 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003373 if (!iter)
3374 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003375
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003376 iter->pg = ftrace_pages_start;
3377 iter->flags = FTRACE_ITER_ENABLED;
3378 iter->ops = &global_ops;
3379
3380 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003381}
3382
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003383/**
3384 * ftrace_regex_open - initialize function tracer filter files
3385 * @ops: The ftrace_ops that hold the hash filters
3386 * @flag: The type of filter to process
3387 * @inode: The inode, usually passed in to your open routine
3388 * @file: The file, usually passed in to your open routine
3389 *
3390 * ftrace_regex_open() initializes the filter files for the
3391 * @ops. Depending on @flag it may process the filter hash or
3392 * the notrace hash of @ops. With this called from the open
3393 * routine, you can use ftrace_filter_write() for the write
3394 * routine if @flag has FTRACE_ITER_FILTER set, or
3395 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003396 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003397 * release must call ftrace_regex_release().
3398 */
3399int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003400ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003401 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003402{
3403 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003404 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003405 int ret = 0;
3406
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003407 ftrace_ops_init(ops);
3408
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003409 if (unlikely(ftrace_disabled))
3410 return -ENODEV;
3411
Steven Rostedt5072c592008-05-12 21:20:43 +02003412 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3413 if (!iter)
3414 return -ENOMEM;
3415
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003416 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3417 kfree(iter);
3418 return -ENOMEM;
3419 }
3420
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003421 iter->ops = ops;
3422 iter->flags = flag;
3423
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003424 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003425
Steven Rostedtf45948e2011-05-02 12:29:25 -04003426 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003427 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003428 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003429 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003430
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003431 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003432 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3433
3434 if (file->f_flags & O_TRUNC)
3435 iter->hash = alloc_ftrace_hash(size_bits);
3436 else
3437 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3438
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003439 if (!iter->hash) {
3440 trace_parser_put(&iter->parser);
3441 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003442 ret = -ENOMEM;
3443 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003444 }
3445 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003446
Steven Rostedt5072c592008-05-12 21:20:43 +02003447 if (file->f_mode & FMODE_READ) {
3448 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003449
3450 ret = seq_open(file, &show_ftrace_seq_ops);
3451 if (!ret) {
3452 struct seq_file *m = file->private_data;
3453 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003454 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003455 /* Failed */
3456 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003457 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003458 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003459 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003460 } else
3461 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003462
3463 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003464 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003465
3466 return ret;
3467}
3468
Steven Rostedt41c52c02008-05-22 11:46:33 -04003469static int
3470ftrace_filter_open(struct inode *inode, struct file *file)
3471{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003472 struct ftrace_ops *ops = inode->i_private;
3473
3474 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003475 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3476 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003477}
3478
3479static int
3480ftrace_notrace_open(struct inode *inode, struct file *file)
3481{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003482 struct ftrace_ops *ops = inode->i_private;
3483
3484 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003485 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003486}
3487
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003488/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3489struct ftrace_glob {
3490 char *search;
3491 unsigned len;
3492 int type;
3493};
3494
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003495/*
3496 * If symbols in an architecture don't correspond exactly to the user-visible
3497 * name of what they represent, it is possible to define this function to
3498 * perform the necessary adjustments.
3499*/
3500char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3501{
3502 return str;
3503}
3504
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003505static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003506{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003507 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003508 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003509
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003510 str = arch_ftrace_match_adjust(str, g->search);
3511
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003512 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003513 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003514 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003515 matched = 1;
3516 break;
3517 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003518 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003519 matched = 1;
3520 break;
3521 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003522 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003523 matched = 1;
3524 break;
3525 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003526 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003527 if (slen >= g->len &&
3528 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003529 matched = 1;
3530 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003531 case MATCH_GLOB:
3532 if (glob_match(g->search, str))
3533 matched = 1;
3534 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003535 }
3536
3537 return matched;
3538}
3539
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003540static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003541enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003542{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003543 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003544 int ret = 0;
3545
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003546 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003547 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003548 /* Do nothing if it doesn't exist */
3549 if (!entry)
3550 return 0;
3551
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003552 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003553 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003554 /* Do nothing if it exists */
3555 if (entry)
3556 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003557
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003558 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003559 }
3560 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003561}
3562
Steven Rostedt64e7c442009-02-13 17:08:48 -05003563static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003564ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3565 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003566{
3567 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003568 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003569
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003570 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3571
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003572 if (mod_g) {
3573 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3574
3575 /* blank module name to match all modules */
3576 if (!mod_g->len) {
3577 /* blank module globbing: modname xor exclude_mod */
3578 if ((!exclude_mod) != (!modname))
3579 goto func_match;
3580 return 0;
3581 }
3582
3583 /* not matching the module */
3584 if (!modname || !mod_matches) {
3585 if (exclude_mod)
3586 goto func_match;
3587 else
3588 return 0;
3589 }
3590
3591 if (mod_matches && exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003592 return 0;
3593
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003594func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003595 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003596 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003597 return 1;
3598 }
3599
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003600 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003601}
3602
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003603static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003604match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003605{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003606 struct ftrace_page *pg;
3607 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003608 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003609 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3610 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3611 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003612 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003613 int ret;
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003614 int clear_filter;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003615
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003616 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003617 func_g.type = filter_parse_regex(func, len, &func_g.search,
3618 &clear_filter);
3619 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003620 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003621
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003622 if (mod) {
3623 mod_g.type = filter_parse_regex(mod, strlen(mod),
3624 &mod_g.search, &exclude_mod);
3625 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003626 }
3627
Steven Rostedt52baf112009-02-14 01:15:39 -05003628 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003629
3630 if (unlikely(ftrace_disabled))
3631 goto out_unlock;
3632
Steven Rostedt265c8312009-02-13 12:43:56 -05003633 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003634
3635 if (rec->flags & FTRACE_FL_DISABLED)
3636 continue;
3637
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003638 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003639 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003640 if (ret < 0) {
3641 found = ret;
3642 goto out_unlock;
3643 }
Li Zefan311d16d2009-12-08 11:15:11 +08003644 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003645 }
3646 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003647 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003648 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003649
3650 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003651}
3652
Steven Rostedt64e7c442009-02-13 17:08:48 -05003653static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003654ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003655{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003656 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003657}
3658
Steven Rostedt64e7c442009-02-13 17:08:48 -05003659
Steven Rostedtf6180772009-02-14 00:40:25 -05003660/*
3661 * We register the module command as a template to show others how
3662 * to register the a command as well.
3663 */
3664
3665static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003666ftrace_mod_callback(struct ftrace_hash *hash,
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003667 char *func, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003668{
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003669 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003670
3671 /*
3672 * cmd == 'mod' because we only registered this func
3673 * for the 'mod' ftrace_func_command.
3674 * But if you register one func with multiple commands,
3675 * you can tell which command was used by the cmd
3676 * parameter.
3677 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003678 ret = match_records(hash, func, strlen(func), module);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003679 if (!ret)
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003680 return -EINVAL;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003681 if (ret < 0)
3682 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003683 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003684}
3685
3686static struct ftrace_func_command ftrace_mod_cmd = {
3687 .name = "mod",
3688 .func = ftrace_mod_callback,
3689};
3690
3691static int __init ftrace_mod_cmd_init(void)
3692{
3693 return register_ftrace_command(&ftrace_mod_cmd);
3694}
Steven Rostedt6f415672012-10-05 12:13:07 -04003695core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003696
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003697static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003698 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003699{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003700 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003701 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003702 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003703
3704 key = hash_long(ip, FTRACE_HASH_BITS);
3705
3706 hhd = &ftrace_func_hash[key];
3707
3708 if (hlist_empty(hhd))
3709 return;
3710
3711 /*
3712 * Disable preemption for these calls to prevent a RCU grace
3713 * period. This syncs the hash iteration and freeing of items
3714 * on the hash. rcu_read_lock is too dangerous here.
3715 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003716 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003717 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003718 if (entry->ip == ip)
3719 entry->ops->func(ip, parent_ip, &entry->data);
3720 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003721 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003722}
3723
Steven Rostedtb6887d72009-02-17 12:32:04 -05003724static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003725{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003726 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003727 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003728 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003729};
3730
Steven Rostedtb6887d72009-02-17 12:32:04 -05003731static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003732
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003733static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003734{
Steven Rostedtb8489142011-05-04 09:27:52 -04003735 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003736 int i;
3737
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003738 if (ftrace_probe_registered) {
3739 /* still need to update the function call sites */
3740 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003741 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3742 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003743 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003744 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003745
3746 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3747 struct hlist_head *hhd = &ftrace_func_hash[i];
3748 if (hhd->first)
3749 break;
3750 }
3751 /* Nothing registered? */
3752 if (i == FTRACE_FUNC_HASHSIZE)
3753 return;
3754
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003755 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003756
Steven Rostedtb6887d72009-02-17 12:32:04 -05003757 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003758}
3759
Steven Rostedtb6887d72009-02-17 12:32:04 -05003760static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003761{
3762 int i;
3763
Steven Rostedtb6887d72009-02-17 12:32:04 -05003764 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003765 return;
3766
3767 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3768 struct hlist_head *hhd = &ftrace_func_hash[i];
3769 if (hhd->first)
3770 return;
3771 }
3772
3773 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003774 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003775
Steven Rostedtb6887d72009-02-17 12:32:04 -05003776 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003777}
3778
3779
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003780static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003781{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003782 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003783 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003784 kfree(entry);
3785}
3786
Steven Rostedt59df055f2009-02-14 15:29:06 -05003787int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003788register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003789 void *data)
3790{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003791 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003792 struct ftrace_func_probe *entry;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003793 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003794 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003795 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003796 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003797 struct ftrace_page *pg;
3798 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003799 int not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003800 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003801 int count = 0;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003802 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003803
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003804 func_g.type = filter_parse_regex(glob, strlen(glob),
3805 &func_g.search, &not);
3806 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003807
Steven Rostedtb6887d72009-02-17 12:32:04 -05003808 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003809 if (WARN_ON(not))
3810 return -EINVAL;
3811
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003812 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003813
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003814 old_hash_ops.filter_hash = old_hash;
3815 /* Probes only have filters */
3816 old_hash_ops.notrace_hash = NULL;
3817
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003818 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003819 if (!hash) {
3820 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003821 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003822 }
3823
3824 if (unlikely(ftrace_disabled)) {
3825 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003826 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003827 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003828
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003829 mutex_lock(&ftrace_lock);
3830
Steven Rostedt59df055f2009-02-14 15:29:06 -05003831 do_for_each_ftrace_rec(pg, rec) {
3832
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003833 if (rec->flags & FTRACE_FL_DISABLED)
3834 continue;
3835
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003836 if (!ftrace_match_record(rec, &func_g, NULL, 0))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003837 continue;
3838
3839 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3840 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003841 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003842 if (!count)
3843 count = -ENOMEM;
3844 goto out_unlock;
3845 }
3846
3847 count++;
3848
3849 entry->data = data;
3850
3851 /*
3852 * The caller might want to do something special
3853 * for each function we find. We call the callback
3854 * to give the caller an opportunity to do so.
3855 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003856 if (ops->init) {
3857 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003858 /* caller does not like this func */
3859 kfree(entry);
3860 continue;
3861 }
3862 }
3863
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003864 ret = enter_record(hash, rec, 0);
3865 if (ret < 0) {
3866 kfree(entry);
3867 count = ret;
3868 goto out_unlock;
3869 }
3870
Steven Rostedt59df055f2009-02-14 15:29:06 -05003871 entry->ops = ops;
3872 entry->ip = rec->ip;
3873
3874 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3875 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3876
3877 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003878
3879 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003880
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003881 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003882
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003883 if (!ret)
3884 free_ftrace_hash_rcu(old_hash);
3885 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003886 count = ret;
3887
Steven Rostedt59df055f2009-02-14 15:29:06 -05003888 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003889 mutex_unlock(&ftrace_lock);
3890 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003891 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003892 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003893
3894 return count;
3895}
3896
3897enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003898 PROBE_TEST_FUNC = 1,
3899 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003900};
3901
3902static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003903__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003904 void *data, int flags)
3905{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003906 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003907 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003908 struct ftrace_func_probe *p;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003909 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003910 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003911 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003912 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003913 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003914 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003915 char str[KSYM_SYMBOL_LEN];
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003916 int i, ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003917
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003918 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003919 func_g.search = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003920 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003921 int not;
3922
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003923 func_g.type = filter_parse_regex(glob, strlen(glob),
3924 &func_g.search, &not);
3925 func_g.len = strlen(func_g.search);
3926 func_g.search = glob;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003927
Steven Rostedtb6887d72009-02-17 12:32:04 -05003928 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003929 if (WARN_ON(not))
3930 return;
3931 }
3932
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003933 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003934
3935 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3936 if (!hash)
3937 /* Hmm, should report this somehow */
3938 goto out_unlock;
3939
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003940 INIT_LIST_HEAD(&free_list);
3941
Steven Rostedt59df055f2009-02-14 15:29:06 -05003942 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3943 struct hlist_head *hhd = &ftrace_func_hash[i];
3944
Sasha Levinb67bfe02013-02-27 17:06:00 -08003945 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003946
3947 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003948 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003949 continue;
3950
Steven Rostedtb6887d72009-02-17 12:32:04 -05003951 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003952 continue;
3953
3954 /* do this last, since it is the most expensive */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003955 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003956 kallsyms_lookup(entry->ip, NULL, NULL,
3957 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003958 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003959 continue;
3960 }
3961
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003962 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3963 /* It is possible more than one entry had this ip */
3964 if (rec_entry)
3965 free_hash_entry(hash, rec_entry);
3966
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003967 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003968 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003969 }
3970 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003971 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003972 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003973 /*
3974 * Remove after the disable is called. Otherwise, if the last
3975 * probe is removed, a null hash means *all enabled*.
3976 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003977 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003978 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003979 if (!ret)
3980 free_ftrace_hash_rcu(old_hash);
3981
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003982 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3983 list_del(&entry->free_list);
3984 ftrace_free_entry(entry);
3985 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003986 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003987
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003988 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003989 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003990 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003991}
3992
3993void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003994unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003995 void *data)
3996{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003997 __unregister_ftrace_function_probe(glob, ops, data,
3998 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003999}
4000
4001void
Steven Rostedtb6887d72009-02-17 12:32:04 -05004002unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004003{
Steven Rostedtb6887d72009-02-17 12:32:04 -05004004 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004005}
4006
Steven Rostedtb6887d72009-02-17 12:32:04 -05004007void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004008{
Steven Rostedtb6887d72009-02-17 12:32:04 -05004009 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004010}
4011
Steven Rostedtf6180772009-02-14 00:40:25 -05004012static LIST_HEAD(ftrace_commands);
4013static DEFINE_MUTEX(ftrace_cmd_mutex);
4014
Tom Zanussi38de93a2013-10-24 08:34:18 -05004015/*
4016 * Currently we only register ftrace commands from __init, so mark this
4017 * __init too.
4018 */
4019__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004020{
4021 struct ftrace_func_command *p;
4022 int ret = 0;
4023
4024 mutex_lock(&ftrace_cmd_mutex);
4025 list_for_each_entry(p, &ftrace_commands, list) {
4026 if (strcmp(cmd->name, p->name) == 0) {
4027 ret = -EBUSY;
4028 goto out_unlock;
4029 }
4030 }
4031 list_add(&cmd->list, &ftrace_commands);
4032 out_unlock:
4033 mutex_unlock(&ftrace_cmd_mutex);
4034
4035 return ret;
4036}
4037
Tom Zanussi38de93a2013-10-24 08:34:18 -05004038/*
4039 * Currently we only unregister ftrace commands from __init, so mark
4040 * this __init too.
4041 */
4042__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004043{
4044 struct ftrace_func_command *p, *n;
4045 int ret = -ENODEV;
4046
4047 mutex_lock(&ftrace_cmd_mutex);
4048 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4049 if (strcmp(cmd->name, p->name) == 0) {
4050 ret = 0;
4051 list_del_init(&p->list);
4052 goto out_unlock;
4053 }
4054 }
4055 out_unlock:
4056 mutex_unlock(&ftrace_cmd_mutex);
4057
4058 return ret;
4059}
4060
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004061static int ftrace_process_regex(struct ftrace_hash *hash,
4062 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004063{
Steven Rostedtf6180772009-02-14 00:40:25 -05004064 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004065 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004066 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004067
4068 func = strsep(&next, ":");
4069
4070 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004071 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004072 if (!ret)
4073 ret = -EINVAL;
4074 if (ret < 0)
4075 return ret;
4076 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004077 }
4078
Steven Rostedtf6180772009-02-14 00:40:25 -05004079 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004080
4081 command = strsep(&next, ":");
4082
Steven Rostedtf6180772009-02-14 00:40:25 -05004083 mutex_lock(&ftrace_cmd_mutex);
4084 list_for_each_entry(p, &ftrace_commands, list) {
4085 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04004086 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004087 goto out_unlock;
4088 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004089 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004090 out_unlock:
4091 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004092
Steven Rostedtf6180772009-02-14 00:40:25 -05004093 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004094}
4095
Ingo Molnare309b412008-05-12 21:20:51 +02004096static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004097ftrace_regex_write(struct file *file, const char __user *ubuf,
4098 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004099{
4100 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004101 struct trace_parser *parser;
4102 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004103
Li Zefan4ba79782009-09-22 13:52:20 +08004104 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004105 return 0;
4106
Steven Rostedt5072c592008-05-12 21:20:43 +02004107 if (file->f_mode & FMODE_READ) {
4108 struct seq_file *m = file->private_data;
4109 iter = m->private;
4110 } else
4111 iter = file->private_data;
4112
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004113 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004114 return -ENODEV;
4115
4116 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004117
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004118 parser = &iter->parser;
4119 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004120
Li Zefan4ba79782009-09-22 13:52:20 +08004121 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004122 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004123 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004124 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004125 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004126 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004127 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004128 }
4129
Steven Rostedt5072c592008-05-12 21:20:43 +02004130 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004131 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004132 return ret;
4133}
4134
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004135ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004136ftrace_filter_write(struct file *file, const char __user *ubuf,
4137 size_t cnt, loff_t *ppos)
4138{
4139 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4140}
4141
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004142ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004143ftrace_notrace_write(struct file *file, const char __user *ubuf,
4144 size_t cnt, loff_t *ppos)
4145{
4146 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4147}
4148
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004149static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004150ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4151{
4152 struct ftrace_func_entry *entry;
4153
4154 if (!ftrace_location(ip))
4155 return -EINVAL;
4156
4157 if (remove) {
4158 entry = ftrace_lookup_ip(hash, ip);
4159 if (!entry)
4160 return -ENOENT;
4161 free_hash_entry(hash, entry);
4162 return 0;
4163 }
4164
4165 return add_hash_entry(hash, ip);
4166}
4167
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004168static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004169 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004170{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004171 struct ftrace_ops *op;
4172
4173 if (!ftrace_enabled)
4174 return;
4175
4176 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004177 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004178 return;
4179 }
4180
4181 /*
4182 * If this is the shared global_ops filter, then we need to
4183 * check if there is another ops that shares it, is enabled.
4184 * If so, we still need to run the modify code.
4185 */
4186 if (ops->func_hash != &global_ops.local_hash)
4187 return;
4188
4189 do_for_each_ftrace_op(op, ftrace_ops_list) {
4190 if (op->func_hash == &global_ops.local_hash &&
4191 op->flags & FTRACE_OPS_FL_ENABLED) {
4192 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4193 /* Only need to do this once */
4194 return;
4195 }
4196 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004197}
4198
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004199static int
4200ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4201 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004202{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004203 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004204 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004205 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004206 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004207 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004208
Steven Rostedt41c52c02008-05-22 11:46:33 -04004209 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004210 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004211
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004212 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004213
Steven Rostedtf45948e2011-05-02 12:29:25 -04004214 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004215 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004216 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004217 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004218
Wang Nanb972cc52014-07-15 08:40:20 +08004219 if (reset)
4220 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4221 else
4222 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4223
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004224 if (!hash) {
4225 ret = -ENOMEM;
4226 goto out_regex_unlock;
4227 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004228
Jiri Olsaac483c42012-01-02 10:04:14 +01004229 if (buf && !ftrace_match_records(hash, buf, len)) {
4230 ret = -EINVAL;
4231 goto out_regex_unlock;
4232 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004233 if (ip) {
4234 ret = ftrace_match_addr(hash, ip, remove);
4235 if (ret < 0)
4236 goto out_regex_unlock;
4237 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004238
4239 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004240 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004241 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4242 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004243 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004244 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004245 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004246 free_ftrace_hash_rcu(old_hash);
4247 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004248 mutex_unlock(&ftrace_lock);
4249
Jiri Olsaac483c42012-01-02 10:04:14 +01004250 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004251 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004252
4253 free_ftrace_hash(hash);
4254 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004255}
4256
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004257static int
4258ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4259 int reset, int enable)
4260{
4261 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4262}
4263
4264/**
4265 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4266 * @ops - the ops to set the filter with
4267 * @ip - the address to add to or remove from the filter.
4268 * @remove - non zero to remove the ip from the filter
4269 * @reset - non zero to reset all filters before applying this filter.
4270 *
4271 * Filters denote which functions should be enabled when tracing is enabled
4272 * If @ip is NULL, it failes to update filter.
4273 */
4274int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4275 int remove, int reset)
4276{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004277 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004278 return ftrace_set_addr(ops, ip, remove, reset, 1);
4279}
4280EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4281
Joel Fernandesd032ae82016-11-15 12:31:20 -08004282/**
4283 * ftrace_ops_set_global_filter - setup ops to use global filters
4284 * @ops - the ops which will use the global filters
4285 *
4286 * ftrace users who need global function trace filtering should call this.
4287 * It can set the global filter only if ops were not initialized before.
4288 */
4289void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4290{
4291 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4292 return;
4293
4294 ftrace_ops_init(ops);
4295 ops->func_hash = &global_ops.local_hash;
4296}
4297EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4298
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004299static int
4300ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4301 int reset, int enable)
4302{
4303 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4304}
4305
Steven Rostedt77a2b372008-05-12 21:20:45 +02004306/**
4307 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004308 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004309 * @buf - the string that holds the function filter text.
4310 * @len - the length of the string.
4311 * @reset - non zero to reset all filters before applying this filter.
4312 *
4313 * Filters denote which functions should be enabled when tracing is enabled.
4314 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4315 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004316int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004317 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004318{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004319 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004320 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004321}
Steven Rostedt936e0742011-05-05 22:54:01 -04004322EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004323
Steven Rostedt41c52c02008-05-22 11:46:33 -04004324/**
4325 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004326 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004327 * @buf - the string that holds the function notrace text.
4328 * @len - the length of the string.
4329 * @reset - non zero to reset all filters before applying this filter.
4330 *
4331 * Notrace Filters denote which functions should not be enabled when tracing
4332 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4333 * for tracing.
4334 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004335int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004336 int len, int reset)
4337{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004338 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004339 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004340}
4341EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4342/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004343 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004344 * @buf - the string that holds the function filter text.
4345 * @len - the length of the string.
4346 * @reset - non zero to reset all filters before applying this filter.
4347 *
4348 * Filters denote which functions should be enabled when tracing is enabled.
4349 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4350 */
4351void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4352{
4353 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4354}
4355EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4356
4357/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004358 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004359 * @buf - the string that holds the function notrace text.
4360 * @len - the length of the string.
4361 * @reset - non zero to reset all filters before applying this filter.
4362 *
4363 * Notrace Filters denote which functions should not be enabled when tracing
4364 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4365 * for tracing.
4366 */
4367void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004368{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004369 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004370}
Steven Rostedt936e0742011-05-05 22:54:01 -04004371EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004372
Steven Rostedt2af15d62009-05-28 13:37:24 -04004373/*
4374 * command line interface to allow users to set filters on boot up.
4375 */
4376#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4377static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4378static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4379
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004380/* Used by function selftest to not test if filter is set */
4381bool ftrace_filter_param __initdata;
4382
Steven Rostedt2af15d62009-05-28 13:37:24 -04004383static int __init set_ftrace_notrace(char *str)
4384{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004385 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004386 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004387 return 1;
4388}
4389__setup("ftrace_notrace=", set_ftrace_notrace);
4390
4391static int __init set_ftrace_filter(char *str)
4392{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004393 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004394 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004395 return 1;
4396}
4397__setup("ftrace_filter=", set_ftrace_filter);
4398
Stefan Assmann369bc182009-10-12 22:17:21 +02004399#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004400static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004401static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004402static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004403
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004404static unsigned long save_global_trampoline;
4405static unsigned long save_global_flags;
4406
Stefan Assmann369bc182009-10-12 22:17:21 +02004407static int __init set_graph_function(char *str)
4408{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004409 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004410 return 1;
4411}
4412__setup("ftrace_graph_filter=", set_graph_function);
4413
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004414static int __init set_graph_notrace_function(char *str)
4415{
4416 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4417 return 1;
4418}
4419__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4420
Todd Brandt65a50c652017-03-02 16:12:15 -08004421static int __init set_graph_max_depth_function(char *str)
4422{
4423 if (!str)
4424 return 0;
4425 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4426 return 1;
4427}
4428__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4429
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004430static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004431{
4432 int ret;
4433 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004434 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004435
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004436 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4437 if (WARN_ON(!hash))
4438 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004439
4440 while (buf) {
4441 func = strsep(&buf, ",");
4442 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004443 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004444 if (ret)
4445 printk(KERN_DEBUG "ftrace: function %s not "
4446 "traceable\n", func);
4447 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004448
4449 if (enable)
4450 ftrace_graph_hash = hash;
4451 else
4452 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02004453}
4454#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4455
Steven Rostedt2a85a372011-12-19 21:57:44 -05004456void __init
4457ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004458{
4459 char *func;
4460
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004461 ftrace_ops_init(ops);
4462
Steven Rostedt2af15d62009-05-28 13:37:24 -04004463 while (buf) {
4464 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004465 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004466 }
4467}
4468
4469static void __init set_ftrace_early_filters(void)
4470{
4471 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004472 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004473 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004474 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004475#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4476 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004477 set_ftrace_early_graph(ftrace_graph_buf, 1);
4478 if (ftrace_graph_notrace_buf[0])
4479 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004480#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004481}
4482
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004483int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004484{
4485 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004486 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004487 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004488 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004489 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004490 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004491 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004492 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004493
Steven Rostedt5072c592008-05-12 21:20:43 +02004494 if (file->f_mode & FMODE_READ) {
4495 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004496 seq_release(inode, file);
4497 } else
4498 iter = file->private_data;
4499
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004500 parser = &iter->parser;
4501 if (trace_parser_loaded(parser)) {
4502 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004503 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004504 }
4505
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004506 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004507
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004508 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004509
Steven Rostedt058e2972011-04-29 22:35:33 -04004510 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004511 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4512
4513 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004514 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004515 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004516 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004517
Steven Rostedt058e2972011-04-29 22:35:33 -04004518 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004519 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004520 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4521 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004522 ret = ftrace_hash_move(iter->ops, filter_hash,
4523 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004524 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004525 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004526 free_ftrace_hash_rcu(old_hash);
4527 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004528 mutex_unlock(&ftrace_lock);
4529 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004530
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004531 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004532 free_ftrace_hash(iter->hash);
4533 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004534
Steven Rostedt5072c592008-05-12 21:20:43 +02004535 return 0;
4536}
4537
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004538static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004539 .open = ftrace_avail_open,
4540 .read = seq_read,
4541 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004542 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004543};
4544
Steven Rostedt647bcd02011-05-03 14:39:21 -04004545static const struct file_operations ftrace_enabled_fops = {
4546 .open = ftrace_enabled_open,
4547 .read = seq_read,
4548 .llseek = seq_lseek,
4549 .release = seq_release_private,
4550};
4551
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004552static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004553 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004554 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004555 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004556 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004557 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004558};
4559
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004560static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004561 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004562 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004563 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004564 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004565 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004566};
4567
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004568#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4569
4570static DEFINE_MUTEX(graph_lock);
4571
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004572struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
4573struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
4574
4575enum graph_filter_type {
4576 GRAPH_FILTER_NOTRACE = 0,
4577 GRAPH_FILTER_FUNCTION,
4578};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004579
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004580#define FTRACE_GRAPH_EMPTY ((void *)1)
4581
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004582struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004583 struct ftrace_hash *hash;
4584 struct ftrace_func_entry *entry;
4585 int idx; /* for hash table iteration */
4586 enum graph_filter_type type;
4587 struct ftrace_hash *new_hash;
4588 const struct seq_operations *seq_ops;
4589 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004590};
4591
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004592static void *
Li Zefan85951842009-06-24 09:54:00 +08004593__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004594{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004595 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004596 struct ftrace_func_entry *entry = fgd->entry;
4597 struct hlist_head *head;
4598 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004599
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004600 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004601 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004602
4603 if (entry) {
4604 hlist_for_each_entry_continue(entry, hlist) {
4605 fgd->entry = entry;
4606 return entry;
4607 }
4608
4609 idx++;
4610 }
4611
4612 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
4613 head = &fgd->hash->buckets[i];
4614 hlist_for_each_entry(entry, head, hlist) {
4615 fgd->entry = entry;
4616 fgd->idx = i;
4617 return entry;
4618 }
4619 }
4620 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08004621}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004622
Li Zefan85951842009-06-24 09:54:00 +08004623static void *
4624g_next(struct seq_file *m, void *v, loff_t *pos)
4625{
4626 (*pos)++;
4627 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004628}
4629
4630static void *g_start(struct seq_file *m, loff_t *pos)
4631{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004632 struct ftrace_graph_data *fgd = m->private;
4633
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004634 mutex_lock(&graph_lock);
4635
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004636 if (fgd->type == GRAPH_FILTER_FUNCTION)
4637 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4638 lockdep_is_held(&graph_lock));
4639 else
4640 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4641 lockdep_is_held(&graph_lock));
4642
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004643 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004644 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004645 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004646
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004647 fgd->idx = 0;
4648 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08004649 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004650}
4651
4652static void g_stop(struct seq_file *m, void *p)
4653{
4654 mutex_unlock(&graph_lock);
4655}
4656
4657static int g_show(struct seq_file *m, void *v)
4658{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004659 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004660
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004661 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004662 return 0;
4663
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004664 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004665 struct ftrace_graph_data *fgd = m->private;
4666
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004667 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004668 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004669 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004670 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004671 return 0;
4672 }
4673
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004674 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004675
4676 return 0;
4677}
4678
James Morris88e9d342009-09-22 16:43:43 -07004679static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004680 .start = g_start,
4681 .next = g_next,
4682 .stop = g_stop,
4683 .show = g_show,
4684};
4685
4686static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004687__ftrace_graph_open(struct inode *inode, struct file *file,
4688 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004689{
4690 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004691 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004692
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004693 if (file->f_mode & FMODE_WRITE) {
4694 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
4695
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004696 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
4697 return -ENOMEM;
4698
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004699 if (file->f_flags & O_TRUNC)
4700 new_hash = alloc_ftrace_hash(size_bits);
4701 else
4702 new_hash = alloc_and_copy_ftrace_hash(size_bits,
4703 fgd->hash);
4704 if (!new_hash) {
4705 ret = -ENOMEM;
4706 goto out;
4707 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004708 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004709
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004710 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004711 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004712 if (!ret) {
4713 struct seq_file *m = file->private_data;
4714 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004715 } else {
4716 /* Failed */
4717 free_ftrace_hash(new_hash);
4718 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004719 }
4720 } else
4721 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004722
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004723out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004724 if (ret < 0 && file->f_mode & FMODE_WRITE)
4725 trace_parser_put(&fgd->parser);
4726
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004727 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004728
4729 /*
4730 * All uses of fgd->hash must be taken with the graph_lock
4731 * held. The graph_lock is going to be released, so force
4732 * fgd->hash to be reinitialized when it is taken again.
4733 */
4734 fgd->hash = NULL;
4735
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004736 return ret;
4737}
4738
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004739static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004740ftrace_graph_open(struct inode *inode, struct file *file)
4741{
4742 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004743 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004744
4745 if (unlikely(ftrace_disabled))
4746 return -ENODEV;
4747
4748 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4749 if (fgd == NULL)
4750 return -ENOMEM;
4751
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004752 mutex_lock(&graph_lock);
4753
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004754 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4755 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004756 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004757 fgd->seq_ops = &ftrace_graph_seq_ops;
4758
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004759 ret = __ftrace_graph_open(inode, file, fgd);
4760 if (ret < 0)
4761 kfree(fgd);
4762
4763 mutex_unlock(&graph_lock);
4764 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004765}
4766
4767static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004768ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4769{
4770 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004771 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004772
4773 if (unlikely(ftrace_disabled))
4774 return -ENODEV;
4775
4776 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4777 if (fgd == NULL)
4778 return -ENOMEM;
4779
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004780 mutex_lock(&graph_lock);
4781
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004782 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4783 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004784 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004785 fgd->seq_ops = &ftrace_graph_seq_ops;
4786
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004787 ret = __ftrace_graph_open(inode, file, fgd);
4788 if (ret < 0)
4789 kfree(fgd);
4790
4791 mutex_unlock(&graph_lock);
4792 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004793}
4794
4795static int
Li Zefan87827112009-07-23 11:29:11 +08004796ftrace_graph_release(struct inode *inode, struct file *file)
4797{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004798 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004799 struct ftrace_hash *old_hash, *new_hash;
4800 struct trace_parser *parser;
4801 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004802
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004803 if (file->f_mode & FMODE_READ) {
4804 struct seq_file *m = file->private_data;
4805
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004806 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08004807 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004808 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004809 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004810 }
4811
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004812
4813 if (file->f_mode & FMODE_WRITE) {
4814
4815 parser = &fgd->parser;
4816
4817 if (trace_parser_loaded((parser))) {
4818 parser->buffer[parser->idx] = 0;
4819 ret = ftrace_graph_set_hash(fgd->new_hash,
4820 parser->buffer);
4821 }
4822
4823 trace_parser_put(parser);
4824
4825 new_hash = __ftrace_hash_move(fgd->new_hash);
4826 if (!new_hash) {
4827 ret = -ENOMEM;
4828 goto out;
4829 }
4830
4831 mutex_lock(&graph_lock);
4832
4833 if (fgd->type == GRAPH_FILTER_FUNCTION) {
4834 old_hash = rcu_dereference_protected(ftrace_graph_hash,
4835 lockdep_is_held(&graph_lock));
4836 rcu_assign_pointer(ftrace_graph_hash, new_hash);
4837 } else {
4838 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4839 lockdep_is_held(&graph_lock));
4840 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
4841 }
4842
4843 mutex_unlock(&graph_lock);
4844
4845 /* Wait till all users are no longer using the old hash */
4846 synchronize_sched();
4847
4848 free_ftrace_hash(old_hash);
4849 }
4850
4851 out:
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004852 kfree(fgd->new_hash);
4853 kfree(fgd);
4854
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004855 return ret;
Li Zefan87827112009-07-23 11:29:11 +08004856}
4857
4858static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004859ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004860{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004861 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004862 struct dyn_ftrace *rec;
4863 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004864 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004865 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004866 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004867
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004868 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004869 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4870 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004871
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004872 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004873
Steven Rostedt52baf112009-02-14 01:15:39 -05004874 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004875
4876 if (unlikely(ftrace_disabled)) {
4877 mutex_unlock(&ftrace_lock);
4878 return -ENODEV;
4879 }
4880
Steven Rostedt265c8312009-02-13 12:43:56 -05004881 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004882
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004883 if (rec->flags & FTRACE_FL_DISABLED)
4884 continue;
4885
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004886 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004887 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004888
4889 if (!not) {
4890 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004891
4892 if (entry)
4893 continue;
4894 if (add_hash_entry(hash, rec->ip) < 0)
4895 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004896 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004897 if (entry) {
4898 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004899 fail = 0;
4900 }
4901 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004902 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004903 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004904out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004905 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004906
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004907 if (fail)
4908 return -EINVAL;
4909
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004910 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004911}
4912
4913static ssize_t
4914ftrace_graph_write(struct file *file, const char __user *ubuf,
4915 size_t cnt, loff_t *ppos)
4916{
Namhyung Kim6a101082013-10-14 17:24:25 +09004917 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004918 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004919 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004920
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004921 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004922 return 0;
4923
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05004924 /* Read mode uses seq functions */
4925 if (file->f_mode & FMODE_READ) {
4926 struct seq_file *m = file->private_data;
4927 fgd = m->private;
4928 }
4929
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004930 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004931
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004932 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004933
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004934 if (read >= 0 && trace_parser_loaded(parser) &&
4935 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09004936
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004937 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004938 parser->buffer);
4939 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004940 }
4941
Namhyung Kim6a101082013-10-14 17:24:25 +09004942 if (!ret)
4943 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004944
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004945 return ret;
4946}
4947
4948static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004949 .open = ftrace_graph_open,
4950 .read = seq_read,
4951 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004952 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004953 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004954};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004955
4956static const struct file_operations ftrace_graph_notrace_fops = {
4957 .open = ftrace_graph_notrace_open,
4958 .read = seq_read,
4959 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004960 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004961 .release = ftrace_graph_release,
4962};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004963#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4964
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004965void ftrace_create_filter_files(struct ftrace_ops *ops,
4966 struct dentry *parent)
4967{
4968
4969 trace_create_file("set_ftrace_filter", 0644, parent,
4970 ops, &ftrace_filter_fops);
4971
4972 trace_create_file("set_ftrace_notrace", 0644, parent,
4973 ops, &ftrace_notrace_fops);
4974}
4975
4976/*
4977 * The name "destroy_filter_files" is really a misnomer. Although
4978 * in the future, it may actualy delete the files, but this is
4979 * really intended to make sure the ops passed in are disabled
4980 * and that when this function returns, the caller is free to
4981 * free the ops.
4982 *
4983 * The "destroy" name is only to match the "create" name that this
4984 * should be paired with.
4985 */
4986void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4987{
4988 mutex_lock(&ftrace_lock);
4989 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4990 ftrace_shutdown(ops, 0);
4991 ops->flags |= FTRACE_OPS_FL_DELETED;
4992 mutex_unlock(&ftrace_lock);
4993}
4994
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05004995static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004996{
Steven Rostedt5072c592008-05-12 21:20:43 +02004997
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004998 trace_create_file("available_filter_functions", 0444,
4999 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005000
Steven Rostedt647bcd02011-05-03 14:39:21 -04005001 trace_create_file("enabled_functions", 0444,
5002 d_tracer, NULL, &ftrace_enabled_fops);
5003
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005004 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005005
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005006#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005007 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005008 NULL,
5009 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005010 trace_create_file("set_graph_notrace", 0444, d_tracer,
5011 NULL,
5012 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005013#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5014
Steven Rostedt5072c592008-05-12 21:20:43 +02005015 return 0;
5016}
5017
Steven Rostedt9fd49322012-04-24 22:32:06 -04005018static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005019{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005020 const unsigned long *ipa = a;
5021 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005022
Steven Rostedt9fd49322012-04-24 22:32:06 -04005023 if (*ipa > *ipb)
5024 return 1;
5025 if (*ipa < *ipb)
5026 return -1;
5027 return 0;
5028}
5029
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005030static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005031 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005032 unsigned long *end)
5033{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005034 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005035 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005036 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005037 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005038 unsigned long *p;
5039 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005040 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005041 int ret = -ENOMEM;
5042
5043 count = end - start;
5044
5045 if (!count)
5046 return 0;
5047
Steven Rostedt9fd49322012-04-24 22:32:06 -04005048 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005049 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005050
Steven Rostedt706c81f2012-04-24 23:45:26 -04005051 start_pg = ftrace_allocate_pages(count);
5052 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005053 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005054
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005055 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005056
Steven Rostedt320823092011-12-16 14:42:37 -05005057 /*
5058 * Core and each module needs their own pages, as
5059 * modules will free them when they are removed.
5060 * Force a new page to be allocated for modules.
5061 */
Steven Rostedta7900872011-12-16 16:23:44 -05005062 if (!mod) {
5063 WARN_ON(ftrace_pages || ftrace_pages_start);
5064 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005065 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005066 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005067 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005068 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005069
Steven Rostedta7900872011-12-16 16:23:44 -05005070 if (WARN_ON(ftrace_pages->next)) {
5071 /* Hmm, we have free pages? */
5072 while (ftrace_pages->next)
5073 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005074 }
Steven Rostedta7900872011-12-16 16:23:44 -05005075
Steven Rostedt706c81f2012-04-24 23:45:26 -04005076 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005077 }
5078
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005079 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005080 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005081 while (p < end) {
5082 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005083 /*
5084 * Some architecture linkers will pad between
5085 * the different mcount_loc sections of different
5086 * object files to satisfy alignments.
5087 * Skip any NULL pointers.
5088 */
5089 if (!addr)
5090 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005091
5092 if (pg->index == pg->size) {
5093 /* We should have allocated enough */
5094 if (WARN_ON(!pg->next))
5095 break;
5096 pg = pg->next;
5097 }
5098
5099 rec = &pg->records[pg->index++];
5100 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005101 }
5102
Steven Rostedt706c81f2012-04-24 23:45:26 -04005103 /* We should have used all pages */
5104 WARN_ON(pg->next);
5105
5106 /* Assign the last page to ftrace_pages */
5107 ftrace_pages = pg;
5108
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005109 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005110 * We only need to disable interrupts on start up
5111 * because we are modifying code that an interrupt
5112 * may execute, and the modification is not atomic.
5113 * But for modules, nothing runs the code we modify
5114 * until we are finished with it, and there's no
5115 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005116 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005117 if (!mod)
5118 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005119 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005120 if (!mod)
5121 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005122 ret = 0;
5123 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005124 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005125
Steven Rostedta7900872011-12-16 16:23:44 -05005126 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005127}
5128
Steven Rostedt93eb6772009-04-15 13:24:06 -04005129#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005130
5131#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5132
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005133static int referenced_filters(struct dyn_ftrace *rec)
5134{
5135 struct ftrace_ops *ops;
5136 int cnt = 0;
5137
5138 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5139 if (ops_references_rec(ops, rec))
5140 cnt++;
5141 }
5142
5143 return cnt;
5144}
5145
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005146void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005147{
5148 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005149 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005150 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005151 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005152
Steven Rostedt93eb6772009-04-15 13:24:06 -04005153 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005154
5155 if (ftrace_disabled)
5156 goto out_unlock;
5157
Steven Rostedt320823092011-12-16 14:42:37 -05005158 /*
5159 * Each module has its own ftrace_pages, remove
5160 * them from the list.
5161 */
5162 last_pg = &ftrace_pages_start;
5163 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5164 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005165 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005166 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005167 * As core pages are first, the first
5168 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005169 */
Steven Rostedt320823092011-12-16 14:42:37 -05005170 if (WARN_ON(pg == ftrace_pages_start))
5171 goto out_unlock;
5172
5173 /* Check if we are deleting the last page */
5174 if (pg == ftrace_pages)
5175 ftrace_pages = next_to_ftrace_page(last_pg);
5176
5177 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05005178 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5179 free_pages((unsigned long)pg->records, order);
5180 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05005181 } else
5182 last_pg = &pg->next;
5183 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005184 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005185 mutex_unlock(&ftrace_lock);
5186}
5187
Jessica Yu7dcd1822016-02-16 17:32:33 -05005188void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005189{
5190 struct dyn_ftrace *rec;
5191 struct ftrace_page *pg;
5192
5193 mutex_lock(&ftrace_lock);
5194
5195 if (ftrace_disabled)
5196 goto out_unlock;
5197
5198 /*
5199 * If the tracing is enabled, go ahead and enable the record.
5200 *
5201 * The reason not to enable the record immediatelly is the
5202 * inherent check of ftrace_make_nop/ftrace_make_call for
5203 * correct previous instructions. Making first the NOP
5204 * conversion puts the module to the correct state, thus
5205 * passing the ftrace_make_call check.
5206 *
5207 * We also delay this to after the module code already set the
5208 * text to read-only, as we now need to set it back to read-write
5209 * so that we can modify the text.
5210 */
5211 if (ftrace_start_up)
5212 ftrace_arch_code_modify_prepare();
5213
5214 do_for_each_ftrace_rec(pg, rec) {
5215 int cnt;
5216 /*
5217 * do_for_each_ftrace_rec() is a double loop.
5218 * module text shares the pg. If a record is
5219 * not part of this module, then skip this pg,
5220 * which the "break" will do.
5221 */
5222 if (!within_module_core(rec->ip, mod))
5223 break;
5224
5225 cnt = 0;
5226
5227 /*
5228 * When adding a module, we need to check if tracers are
5229 * currently enabled and if they are, and can trace this record,
5230 * we need to enable the module functions as well as update the
5231 * reference counts for those function records.
5232 */
5233 if (ftrace_start_up)
5234 cnt += referenced_filters(rec);
5235
5236 /* This clears FTRACE_FL_DISABLED */
5237 rec->flags = cnt;
5238
5239 if (ftrace_start_up && cnt) {
5240 int failed = __ftrace_replace_code(rec, 1);
5241 if (failed) {
5242 ftrace_bug(failed, rec);
5243 goto out_loop;
5244 }
5245 }
5246
5247 } while_for_each_ftrace_rec();
5248
5249 out_loop:
5250 if (ftrace_start_up)
5251 ftrace_arch_code_modify_post_process();
5252
5253 out_unlock:
5254 mutex_unlock(&ftrace_lock);
5255}
5256
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005257void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005258{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005259 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005260 return;
5261
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005262 ftrace_process_locs(mod, mod->ftrace_callsites,
5263 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005264}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005265#endif /* CONFIG_MODULES */
5266
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005267void ftrace_free_mem(void *start_ptr, void *end_ptr)
5268{
5269 unsigned long start = (unsigned long)start_ptr;
5270 unsigned long end = (unsigned long)end_ptr;
5271 struct ftrace_page **last_pg = &ftrace_pages_start;
5272 struct ftrace_page *pg;
5273 struct dyn_ftrace *rec;
5274 struct dyn_ftrace key;
5275 int order;
5276
5277 key.ip = start;
5278 key.flags = end; /* overload flags, as it is unsigned long */
5279
5280 mutex_lock(&ftrace_lock);
5281
5282 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
5283 if (end < pg->records[0].ip ||
5284 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
5285 continue;
5286 again:
5287 rec = bsearch(&key, pg->records, pg->index,
5288 sizeof(struct dyn_ftrace),
5289 ftrace_cmp_recs);
5290 if (!rec)
5291 continue;
5292 pg->index--;
5293 if (!pg->index) {
5294 *last_pg = pg->next;
5295 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5296 free_pages((unsigned long)pg->records, order);
5297 kfree(pg);
5298 pg = container_of(last_pg, struct ftrace_page, next);
5299 if (!(*last_pg))
5300 ftrace_pages = pg;
5301 continue;
5302 }
5303 memmove(rec, rec + 1,
5304 (pg->index - (rec - pg->records)) * sizeof(*rec));
5305 /* More than one function may be in this block */
5306 goto again;
5307 }
5308 mutex_unlock(&ftrace_lock);
5309}
5310
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005311void __init ftrace_init(void)
5312{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005313 extern unsigned long __start_mcount_loc[];
5314 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005315 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005316 int ret;
5317
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005318 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005319 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005320 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01005321 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005322 goto failed;
5323
5324 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005325 if (!count) {
5326 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005327 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005328 }
5329
5330 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5331 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005332
5333 last_ftrace_enabled = ftrace_enabled = 1;
5334
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005335 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005336 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005337 __stop_mcount_loc);
5338
Steven Rostedt2af15d62009-05-28 13:37:24 -04005339 set_ftrace_early_filters();
5340
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005341 return;
5342 failed:
5343 ftrace_disabled = 1;
5344}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005345
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005346/* Do nothing if arch does not support this */
5347void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5348{
5349}
5350
5351static void ftrace_update_trampoline(struct ftrace_ops *ops)
5352{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005353
5354/*
5355 * Currently there's no safe way to free a trampoline when the kernel
5356 * is configured with PREEMPT. That is because a task could be preempted
5357 * when it jumped to the trampoline, it may be preempted for a long time
5358 * depending on the system load, and currently there's no way to know
5359 * when it will be off the trampoline. If the trampoline is freed
5360 * too early, when the task runs again, it will be executing on freed
5361 * memory and crash.
5362 */
5363#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005364 /* Currently, only non dynamic ops can have a trampoline */
5365 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5366 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005367#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005368
5369 arch_ftrace_update_trampoline(ops);
5370}
5371
Steven Rostedt3d083392008-05-12 21:20:42 +02005372#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005373
Steven Rostedt2b499382011-05-03 22:49:52 -04005374static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005375 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005376 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5377 FTRACE_OPS_FL_INITIALIZED |
5378 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005379};
5380
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005381static int __init ftrace_nodyn_init(void)
5382{
5383 ftrace_enabled = 1;
5384 return 0;
5385}
Steven Rostedt6f415672012-10-05 12:13:07 -04005386core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005387
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005388static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005389static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005390static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005391/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005392# define ftrace_startup(ops, command) \
5393 ({ \
5394 int ___ret = __register_ftrace_function(ops); \
5395 if (!___ret) \
5396 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5397 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005398 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005399# define ftrace_shutdown(ops, command) \
5400 ({ \
5401 int ___ret = __unregister_ftrace_function(ops); \
5402 if (!___ret) \
5403 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5404 ___ret; \
5405 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005406
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005407# define ftrace_startup_sysctl() do { } while (0)
5408# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005409
5410static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005411ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005412{
5413 return 1;
5414}
5415
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005416static void ftrace_update_trampoline(struct ftrace_ops *ops)
5417{
5418}
5419
Steven Rostedt3d083392008-05-12 21:20:42 +02005420#endif /* CONFIG_DYNAMIC_FTRACE */
5421
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005422__init void ftrace_init_global_array_ops(struct trace_array *tr)
5423{
5424 tr->ops = &global_ops;
5425 tr->ops->private = tr;
5426}
5427
5428void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5429{
5430 /* If we filter on pids, update to use the pid function */
5431 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5432 if (WARN_ON(tr->ops->func != ftrace_stub))
5433 printk("ftrace ops had %pS for function\n",
5434 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005435 }
5436 tr->ops->func = func;
5437 tr->ops->private = tr;
5438}
5439
5440void ftrace_reset_array_ops(struct trace_array *tr)
5441{
5442 tr->ops->func = ftrace_stub;
5443}
5444
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005445static inline void
5446__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005447 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005448{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005449 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005450 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005451
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005452 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5453 if (bit < 0)
5454 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005455
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005456 /*
5457 * Some of the ops may be dynamically allocated,
5458 * they must be freed after a synchronize_sched().
5459 */
5460 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005461
Steven Rostedt0a016402012-11-02 17:03:03 -04005462 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005463 /*
5464 * Check the following for each ops before calling their func:
5465 * if RCU flag is set, then rcu_is_watching() must be true
5466 * if PER_CPU is set, then ftrace_function_local_disable()
5467 * must be false
5468 * Otherwise test if the ip matches the ops filter
5469 *
5470 * If any of the above fails then the op->func() is not executed.
5471 */
5472 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5473 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5474 !ftrace_function_local_disabled(op)) &&
5475 ftrace_ops_test(op, ip, regs)) {
5476
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005477 if (FTRACE_WARN_ON(!op->func)) {
5478 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005479 goto out;
5480 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005481 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005482 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005483 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005484out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005485 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005486 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005487}
5488
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005489/*
5490 * Some archs only support passing ip and parent_ip. Even though
5491 * the list function ignores the op parameter, we do not want any
5492 * C side effects, where a function is called without the caller
5493 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005494 * Archs are to support both the regs and ftrace_ops at the same time.
5495 * If they support ftrace_ops, it is assumed they support regs.
5496 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005497 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5498 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005499 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08005500 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005501 */
5502#if ARCH_SUPPORTS_FTRACE_OPS
5503static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005504 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005505{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005506 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005507}
5508#else
5509static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5510{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005511 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005512}
5513#endif
5514
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005515/*
5516 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005517 * recursion, needs RCU protection and/or requires per cpu handling, then
5518 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005519 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005520static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005521 struct ftrace_ops *op, struct pt_regs *regs)
5522{
5523 int bit;
5524
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005525 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5526 return;
5527
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005528 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5529 if (bit < 0)
5530 return;
5531
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005532 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005533
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005534 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5535 !ftrace_function_local_disabled(op)) {
5536 op->func(ip, parent_ip, op, regs);
5537 }
5538
5539 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005540 trace_clear_recursion(bit);
5541}
5542
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005543/**
5544 * ftrace_ops_get_func - get the function a trampoline should call
5545 * @ops: the ops to get the function for
5546 *
5547 * Normally the mcount trampoline will call the ops->func, but there
5548 * are times that it should not. For example, if the ops does not
5549 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08005550 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005551 *
5552 * Returns the function that the trampoline should call for @ops.
5553 */
5554ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5555{
5556 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005557 * If the function does not handle recursion, needs to be RCU safe,
5558 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005559 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005560 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5561 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5562 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005563
5564 return ops->func;
5565}
5566
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005567static void
5568ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5569 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05005570{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005571 struct trace_array *tr = data;
5572 struct trace_pid_list *pid_list;
5573
5574 pid_list = rcu_dereference_sched(tr->function_pids);
5575
5576 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5577 trace_ignore_this_task(pid_list, next));
5578}
5579
5580static void clear_ftrace_pids(struct trace_array *tr)
5581{
5582 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05005583 int cpu;
5584
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005585 pid_list = rcu_dereference_protected(tr->function_pids,
5586 lockdep_is_held(&ftrace_lock));
5587 if (!pid_list)
5588 return;
5589
5590 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5591
5592 for_each_possible_cpu(cpu)
5593 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5594
5595 rcu_assign_pointer(tr->function_pids, NULL);
5596
5597 /* Wait till all users are no longer using pid filtering */
5598 synchronize_sched();
5599
5600 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05005601}
5602
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005603static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05005604{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005605 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005606 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005607
5608 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005609 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005610
5611 mutex_unlock(&ftrace_lock);
5612}
5613
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005614/* Greater than any max PID */
5615#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5616
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005617static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005618 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005619{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005620 struct trace_pid_list *pid_list;
5621 struct trace_array *tr = m->private;
5622
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005623 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005624 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005625
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005626 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005627
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005628 if (!pid_list)
5629 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5630
5631 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005632}
5633
5634static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5635{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005636 struct trace_array *tr = m->private;
5637 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5638
5639 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005640 return NULL;
5641
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005642 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005643}
5644
5645static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005646 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005647{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005648 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005649 mutex_unlock(&ftrace_lock);
5650}
5651
5652static int fpid_show(struct seq_file *m, void *v)
5653{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005654 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005655 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005656 return 0;
5657 }
5658
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005659 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005660}
5661
5662static const struct seq_operations ftrace_pid_sops = {
5663 .start = fpid_start,
5664 .next = fpid_next,
5665 .stop = fpid_stop,
5666 .show = fpid_show,
5667};
5668
5669static int
5670ftrace_pid_open(struct inode *inode, struct file *file)
5671{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005672 struct trace_array *tr = inode->i_private;
5673 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005674 int ret = 0;
5675
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005676 if (trace_array_get(tr) < 0)
5677 return -ENODEV;
5678
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005679 if ((file->f_mode & FMODE_WRITE) &&
5680 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005681 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005682
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005683 ret = seq_open(file, &ftrace_pid_sops);
5684 if (ret < 0) {
5685 trace_array_put(tr);
5686 } else {
5687 m = file->private_data;
5688 /* copy tr over to seq ops */
5689 m->private = tr;
5690 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005691
5692 return ret;
5693}
5694
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005695static void ignore_task_cpu(void *data)
5696{
5697 struct trace_array *tr = data;
5698 struct trace_pid_list *pid_list;
5699
5700 /*
5701 * This function is called by on_each_cpu() while the
5702 * event_mutex is held.
5703 */
5704 pid_list = rcu_dereference_protected(tr->function_pids,
5705 mutex_is_locked(&ftrace_lock));
5706
5707 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5708 trace_ignore_this_task(pid_list, current));
5709}
5710
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005711static ssize_t
5712ftrace_pid_write(struct file *filp, const char __user *ubuf,
5713 size_t cnt, loff_t *ppos)
5714{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005715 struct seq_file *m = filp->private_data;
5716 struct trace_array *tr = m->private;
5717 struct trace_pid_list *filtered_pids = NULL;
5718 struct trace_pid_list *pid_list;
5719 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005720
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005721 if (!cnt)
5722 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005723
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005724 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005725
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005726 filtered_pids = rcu_dereference_protected(tr->function_pids,
5727 lockdep_is_held(&ftrace_lock));
5728
5729 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5730 if (ret < 0)
5731 goto out;
5732
5733 rcu_assign_pointer(tr->function_pids, pid_list);
5734
5735 if (filtered_pids) {
5736 synchronize_sched();
5737 trace_free_pid_list(filtered_pids);
5738 } else if (pid_list) {
5739 /* Register a probe to set whether to ignore the tracing of a task */
5740 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5741 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005742
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005743 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005744 * Ignoring of pids is done at task switch. But we have to
5745 * check for those tasks that are currently running.
5746 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005747 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005748 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005749
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005750 ftrace_update_pid_func();
5751 ftrace_startup_all(0);
5752 out:
5753 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005754
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005755 if (ret > 0)
5756 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05005757
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005758 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005759}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005760
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005761static int
5762ftrace_pid_release(struct inode *inode, struct file *file)
5763{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005764 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005765
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005766 trace_array_put(tr);
5767
5768 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005769}
5770
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005771static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005772 .open = ftrace_pid_open,
5773 .write = ftrace_pid_write,
5774 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005775 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005776 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005777};
5778
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005779void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005780{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005781 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005782 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005783}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005784
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04005785void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5786 struct dentry *d_tracer)
5787{
5788 /* Only the top level directory has the dyn_tracefs and profile */
5789 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5790
5791 ftrace_init_dyn_tracefs(d_tracer);
5792 ftrace_profile_tracefs(d_tracer);
5793}
5794
Steven Rostedt3d083392008-05-12 21:20:42 +02005795/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005796 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005797 *
5798 * This function should be used by panic code. It stops ftrace
5799 * but in a not so nice way. If you need to simply kill ftrace
5800 * from a non-atomic section, use ftrace_kill.
5801 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005802void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005803{
5804 ftrace_disabled = 1;
5805 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005806 clear_ftrace_function();
5807}
5808
5809/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005810 * Test if ftrace is dead or not.
5811 */
5812int ftrace_is_dead(void)
5813{
5814 return ftrace_disabled;
5815}
5816
5817/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005818 * register_ftrace_function - register a function for profiling
5819 * @ops - ops structure that holds the function for profiling.
5820 *
5821 * Register a function to be called by all functions in the
5822 * kernel.
5823 *
5824 * Note: @ops->func and all the functions it calls must be labeled
5825 * with "notrace", otherwise it will go into a
5826 * recursive loop.
5827 */
5828int register_ftrace_function(struct ftrace_ops *ops)
5829{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005830 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005831
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005832 ftrace_ops_init(ops);
5833
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005834 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005835
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005836 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005837
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005838 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005839
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005840 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005841}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005842EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005843
5844/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005845 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005846 * @ops - ops structure that holds the function to unregister
5847 *
5848 * Unregister a function that was added to be called by ftrace profiling.
5849 */
5850int unregister_ftrace_function(struct ftrace_ops *ops)
5851{
5852 int ret;
5853
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005854 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005855 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005856 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005857
5858 return ret;
5859}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005860EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005861
Ingo Molnare309b412008-05-12 21:20:51 +02005862int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005863ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005864 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005865 loff_t *ppos)
5866{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005867 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005868
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005869 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005870
Steven Rostedt45a4a232011-04-21 23:16:46 -04005871 if (unlikely(ftrace_disabled))
5872 goto out;
5873
5874 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005875
Li Zefana32c7762009-06-26 16:55:51 +08005876 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005877 goto out;
5878
Li Zefana32c7762009-06-26 16:55:51 +08005879 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005880
5881 if (ftrace_enabled) {
5882
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005883 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005884 if (ftrace_ops_list != &ftrace_list_end)
5885 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005886
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05005887 ftrace_startup_sysctl();
5888
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005889 } else {
5890 /* stopping ftrace calls (just send to ftrace_stub) */
5891 ftrace_trace_function = ftrace_stub;
5892
5893 ftrace_shutdown_sysctl();
5894 }
5895
5896 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005897 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005898 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005899}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005900
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005901#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005902
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005903static struct ftrace_ops graph_ops = {
5904 .func = ftrace_stub,
5905 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5906 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005907 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005908 FTRACE_OPS_FL_STUB,
5909#ifdef FTRACE_GRAPH_TRAMP_ADDR
5910 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05005911 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005912#endif
5913 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5914};
5915
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005916void ftrace_graph_sleep_time_control(bool enable)
5917{
5918 fgraph_sleep_time = enable;
5919}
5920
5921void ftrace_graph_graph_time_control(bool enable)
5922{
5923 fgraph_graph_time = enable;
5924}
5925
Steven Rostedte49dc192008-12-02 23:50:05 -05005926int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5927{
5928 return 0;
5929}
5930
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005931/* The callbacks that hook a function */
5932trace_func_graph_ret_t ftrace_graph_return =
5933 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005934trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005935static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005936
5937/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5938static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5939{
5940 int i;
5941 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005942 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5943 struct task_struct *g, *t;
5944
5945 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5946 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5947 * sizeof(struct ftrace_ret_stack),
5948 GFP_KERNEL);
5949 if (!ret_stack_list[i]) {
5950 start = 0;
5951 end = i;
5952 ret = -ENOMEM;
5953 goto free;
5954 }
5955 }
5956
Soumya PN6112a302016-05-17 21:31:14 +05305957 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005958 do_each_thread(g, t) {
5959 if (start == end) {
5960 ret = -EAGAIN;
5961 goto unlock;
5962 }
5963
5964 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005965 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005966 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005967 t->curr_ret_stack = -1;
5968 /* Make sure the tasks see the -1 first: */
5969 smp_wmb();
5970 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005971 }
5972 } while_each_thread(g, t);
5973
5974unlock:
Soumya PN6112a302016-05-17 21:31:14 +05305975 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005976free:
5977 for (i = start; i < end; i++)
5978 kfree(ret_stack_list[i]);
5979 return ret;
5980}
5981
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005982static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02005983ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04005984 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005985{
5986 unsigned long long timestamp;
5987 int index;
5988
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005989 /*
5990 * Does the user want to count the time a function was asleep.
5991 * If so, do not update the time stamps.
5992 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005993 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005994 return;
5995
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005996 timestamp = trace_clock_local();
5997
5998 prev->ftrace_timestamp = timestamp;
5999
6000 /* only process tasks that we timestamped */
6001 if (!next->ftrace_timestamp)
6002 return;
6003
6004 /*
6005 * Update all the counters in next to make up for the
6006 * time next was sleeping.
6007 */
6008 timestamp -= next->ftrace_timestamp;
6009
6010 for (index = next->curr_ret_stack; index >= 0; index--)
6011 next->ret_stack[index].calltime += timestamp;
6012}
6013
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006014/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006015static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006016{
6017 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006018 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006019
6020 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
6021 sizeof(struct ftrace_ret_stack *),
6022 GFP_KERNEL);
6023
6024 if (!ret_stack_list)
6025 return -ENOMEM;
6026
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006027 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04006028 for_each_online_cpu(cpu) {
6029 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05006030 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04006031 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006032
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006033 do {
6034 ret = alloc_retstack_tasklist(ret_stack_list);
6035 } while (ret == -EAGAIN);
6036
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006037 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04006038 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006039 if (ret)
6040 pr_info("ftrace_graph: Couldn't activate tracepoint"
6041 " probe to kernel_sched_switch\n");
6042 }
6043
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006044 kfree(ret_stack_list);
6045 return ret;
6046}
6047
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006048/*
6049 * Hibernation protection.
6050 * The state of the current task is too much unstable during
6051 * suspend/restore to disk. We want to protect against that.
6052 */
6053static int
6054ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6055 void *unused)
6056{
6057 switch (state) {
6058 case PM_HIBERNATION_PREPARE:
6059 pause_graph_tracing();
6060 break;
6061
6062 case PM_POST_HIBERNATION:
6063 unpause_graph_tracing();
6064 break;
6065 }
6066 return NOTIFY_DONE;
6067}
6068
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006069static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6070{
6071 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6072 return 0;
6073 return __ftrace_graph_entry(trace);
6074}
6075
6076/*
6077 * The function graph tracer should only trace the functions defined
6078 * by set_ftrace_filter and set_ftrace_notrace. If another function
6079 * tracer ops is registered, the graph tracer requires testing the
6080 * function against the global ops, and not just trace any function
6081 * that any ftrace_ops registered.
6082 */
6083static void update_function_graph_func(void)
6084{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006085 struct ftrace_ops *op;
6086 bool do_test = false;
6087
6088 /*
6089 * The graph and global ops share the same set of functions
6090 * to test. If any other ops is on the list, then
6091 * the graph tracing needs to test if its the function
6092 * it should call.
6093 */
6094 do_for_each_ftrace_op(op, ftrace_ops_list) {
6095 if (op != &global_ops && op != &graph_ops &&
6096 op != &ftrace_list_end) {
6097 do_test = true;
6098 /* in double loop, break out with goto */
6099 goto out;
6100 }
6101 } while_for_each_ftrace_op(op);
6102 out:
6103 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006104 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006105 else
6106 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006107}
6108
Mathias Krause8275f692014-03-30 15:31:50 +02006109static struct notifier_block ftrace_suspend_notifier = {
6110 .notifier_call = ftrace_suspend_notifier_call,
6111};
6112
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006113int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6114 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006115{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006116 int ret = 0;
6117
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006118 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006119
Steven Rostedt05ce5812009-03-24 00:18:31 -04006120 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04006121 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04006122 ret = -EBUSY;
6123 goto out;
6124 }
6125
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006126 register_pm_notifier(&ftrace_suspend_notifier);
6127
Steven Rostedt597af812009-04-03 15:24:12 -04006128 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006129 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006130 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04006131 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006132 goto out;
6133 }
Steven Rostedte53a6312008-11-26 00:16:25 -05006134
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006135 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006136
6137 /*
6138 * Update the indirect function to the entryfunc, and the
6139 * function that gets called to the entry_test first. Then
6140 * call the update fgraph entry function to determine if
6141 * the entryfunc should be called directly or not.
6142 */
6143 __ftrace_graph_entry = entryfunc;
6144 ftrace_graph_entry = ftrace_graph_entry_test;
6145 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05006146
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006147 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006148out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006149 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006150 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006151}
6152
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006153void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006154{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006155 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006156
Steven Rostedt597af812009-04-03 15:24:12 -04006157 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006158 goto out;
6159
Steven Rostedt597af812009-04-03 15:24:12 -04006160 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006161 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006162 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006163 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006164 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006165 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04006166 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006167
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006168#ifdef CONFIG_DYNAMIC_FTRACE
6169 /*
6170 * Function graph does not allocate the trampoline, but
6171 * other global_ops do. We need to reset the ALLOC_TRAMP flag
6172 * if one was used.
6173 */
6174 global_ops.trampoline = save_global_trampoline;
6175 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
6176 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
6177#endif
6178
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006179 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006180 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006181}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006182
Steven Rostedt868baf02011-02-10 21:26:13 -05006183static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6184
6185static void
6186graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6187{
6188 atomic_set(&t->tracing_graph_pause, 0);
6189 atomic_set(&t->trace_overrun, 0);
6190 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006191 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05006192 smp_wmb();
6193 t->ret_stack = ret_stack;
6194}
6195
6196/*
6197 * Allocate a return stack for the idle task. May be the first
6198 * time through, or it may be done by CPU hotplug online.
6199 */
6200void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6201{
6202 t->curr_ret_stack = -1;
6203 /*
6204 * The idle task has no parent, it either has its own
6205 * stack or no stack at all.
6206 */
6207 if (t->ret_stack)
6208 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6209
6210 if (ftrace_graph_active) {
6211 struct ftrace_ret_stack *ret_stack;
6212
6213 ret_stack = per_cpu(idle_ret_stack, cpu);
6214 if (!ret_stack) {
6215 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6216 * sizeof(struct ftrace_ret_stack),
6217 GFP_KERNEL);
6218 if (!ret_stack)
6219 return;
6220 per_cpu(idle_ret_stack, cpu) = ret_stack;
6221 }
6222 graph_init_task(t, ret_stack);
6223 }
6224}
6225
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006226/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006227void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006228{
Steven Rostedt84047e32009-06-02 16:51:55 -04006229 /* Make sure we do not use the parent ret_stack */
6230 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05006231 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04006232
Steven Rostedt597af812009-04-03 15:24:12 -04006233 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04006234 struct ftrace_ret_stack *ret_stack;
6235
6236 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006237 * sizeof(struct ftrace_ret_stack),
6238 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04006239 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006240 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05006241 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04006242 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006243}
6244
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006245void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006246{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006247 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6248
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006249 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006250 /* NULL must become visible to IRQs before we free it: */
6251 barrier();
6252
6253 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006254}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006255#endif