blob: 84752c8e28b5286b03d9422a79a92429427681cb [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050021#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090065#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040066#define INIT_OPS_HASH(opsname) \
67 .func_hash = &opsname.local_hash, \
68 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040069#define ASSIGN_OPS_HASH(opsname, val) \
70 .func_hash = val, \
71 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090072#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040073#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040074#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090075#endif
76
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077static struct ftrace_ops ftrace_list_end __read_mostly = {
78 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040079 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040080 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040081};
82
Steven Rostedt4eebcc82008-05-12 21:20:48 +020083/* ftrace_enabled is a method to turn ftrace on or off */
84int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020085static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020086
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040087/* Current function tracing op */
88struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050089/* What to set function_trace_op to */
90static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050091
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040092static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040093{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040094 struct trace_array *tr;
95
96 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
97 return false;
98
99 tr = ops->private;
100
101 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400102}
103
104static void ftrace_update_trampoline(struct ftrace_ops *ops);
105
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200106/*
107 * ftrace_disabled is set when an anomaly is discovered.
108 * ftrace_disabled is much stronger than ftrace_enabled.
109 */
110static int ftrace_disabled __read_mostly;
111
Steven Rostedt52baf112009-02-14 01:15:39 -0500112static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200113
Steven Rostedtb8489142011-05-04 09:27:52 -0400114static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400116static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400118#if ARCH_SUPPORTS_FTRACE_OPS
119static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400120 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400121#else
122/* See comment below, where ftrace_ops_list_func is defined */
123static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
124#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
125#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400126
Steven Rostedt0a016402012-11-02 17:03:03 -0400127/*
128 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400129 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400130 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400131 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400132 * concurrent insertions into the ftrace_global_list.
133 *
134 * Silly Alpha and silly pointer-speculation compiler optimizations!
135 */
136#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400137 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400138 do
139
140/*
141 * Optimized for just a single item in the list (as that is the normal case).
142 */
143#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400144 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400145 unlikely((op) != &ftrace_list_end))
146
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900147static inline void ftrace_ops_init(struct ftrace_ops *ops)
148{
149#ifdef CONFIG_DYNAMIC_FTRACE
150 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400151 mutex_init(&ops->local_hash.regex_lock);
152 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900153 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
154 }
155#endif
156}
157
Steven Rostedtea701f12012-07-20 13:08:05 -0400158/**
159 * ftrace_nr_registered_ops - return number of ops registered
160 *
161 * Returns the number of ftrace_ops registered and tracing functions
162 */
163int ftrace_nr_registered_ops(void)
164{
165 struct ftrace_ops *ops;
166 int cnt = 0;
167
168 mutex_lock(&ftrace_lock);
169
170 for (ops = ftrace_ops_list;
171 ops != &ftrace_list_end; ops = ops->next)
172 cnt++;
173
174 mutex_unlock(&ftrace_lock);
175
176 return cnt;
177}
178
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400179static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400180 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500181{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400182 struct trace_array *tr = op->private;
183
184 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500185 return;
186
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400187 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500188}
189
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200191 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200192 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200193 * This NULLs the ftrace function and in essence stops
194 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200195 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200196void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197{
Steven Rostedt3d083392008-05-12 21:20:42 +0200198 ftrace_trace_function = ftrace_stub;
199}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200200
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500201static void per_cpu_ops_disable_all(struct ftrace_ops *ops)
Jiri Olsae2484912012-02-15 15:51:48 +0100202{
203 int cpu;
204
205 for_each_possible_cpu(cpu)
206 *per_cpu_ptr(ops->disabled, cpu) = 1;
207}
208
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500209static int per_cpu_ops_alloc(struct ftrace_ops *ops)
Jiri Olsae2484912012-02-15 15:51:48 +0100210{
211 int __percpu *disabled;
212
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500213 if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
214 return -EINVAL;
215
Jiri Olsae2484912012-02-15 15:51:48 +0100216 disabled = alloc_percpu(int);
217 if (!disabled)
218 return -ENOMEM;
219
220 ops->disabled = disabled;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500221 per_cpu_ops_disable_all(ops);
Jiri Olsae2484912012-02-15 15:51:48 +0100222 return 0;
223}
224
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500225static void ftrace_sync(struct work_struct *work)
226{
227 /*
228 * This function is just a stub to implement a hard force
229 * of synchronize_sched(). This requires synchronizing
230 * tasks even in userspace and idle.
231 *
232 * Yes, function tracing is rude.
233 */
234}
235
236static void ftrace_sync_ipi(void *data)
237{
238 /* Probably not needed, but do it anyway */
239 smp_rmb();
240}
241
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500242#ifdef CONFIG_FUNCTION_GRAPH_TRACER
243static void update_function_graph_func(void);
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400244
245/* Both enabled by default (can be cleared by function_graph tracer flags */
246static bool fgraph_sleep_time = true;
247static bool fgraph_graph_time = true;
248
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500249#else
250static inline void update_function_graph_func(void) { }
251#endif
252
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100253
254static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
255{
256 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500257 * 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 +0100258 * then it needs to call the list anyway.
259 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500260 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU |
261 FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100262 return ftrace_ops_list_func;
263
264 return ftrace_ops_get_func(ops);
265}
266
Steven Rostedt2b499382011-05-03 22:49:52 -0400267static void update_ftrace_function(void)
268{
269 ftrace_func_t func;
270
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400271 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400272 * Prepare the ftrace_ops that the arch callback will use.
273 * If there's only one ftrace_ops registered, the ftrace_ops_list
274 * will point to the ops we want.
275 */
276 set_function_trace_op = ftrace_ops_list;
277
278 /* If there's no ftrace_ops registered, just call the stub function */
279 if (ftrace_ops_list == &ftrace_list_end) {
280 func = ftrace_stub;
281
282 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400283 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400284 * recursion safe and not dynamic and the arch supports passing ops,
285 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400286 */
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400287 } else if (ftrace_ops_list->next == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100288 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400289
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400290 } else {
291 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500292 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400293 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400294 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400295
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400296 update_function_graph_func();
297
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500298 /* If there's no change, then do nothing more here */
299 if (ftrace_trace_function == func)
300 return;
301
302 /*
303 * If we are using the list function, it doesn't care
304 * about the function_trace_ops.
305 */
306 if (func == ftrace_ops_list_func) {
307 ftrace_trace_function = func;
308 /*
309 * Don't even bother setting function_trace_ops,
310 * it would be racy to do so anyway.
311 */
312 return;
313 }
314
315#ifndef CONFIG_DYNAMIC_FTRACE
316 /*
317 * For static tracing, we need to be a bit more careful.
318 * The function change takes affect immediately. Thus,
319 * we need to coorditate the setting of the function_trace_ops
320 * with the setting of the ftrace_trace_function.
321 *
322 * Set the function to the list ops, which will call the
323 * function we want, albeit indirectly, but it handles the
324 * ftrace_ops and doesn't depend on function_trace_op.
325 */
326 ftrace_trace_function = ftrace_ops_list_func;
327 /*
328 * Make sure all CPUs see this. Yes this is slow, but static
329 * tracing is slow and nasty to have enabled.
330 */
331 schedule_on_each_cpu(ftrace_sync);
332 /* Now all cpus are using the list ops. */
333 function_trace_op = set_function_trace_op;
334 /* Make sure the function_trace_op is visible on all CPUs */
335 smp_wmb();
336 /* Nasty way to force a rmb on all cpus */
337 smp_call_function(ftrace_sync_ipi, NULL, 1);
338 /* OK, we are all set to update the ftrace_trace_function now! */
339#endif /* !CONFIG_DYNAMIC_FTRACE */
340
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400341 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400342}
343
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800344int using_ftrace_ops_list_func(void)
345{
346 return ftrace_trace_function == ftrace_ops_list_func;
347}
348
Steven Rostedt2b499382011-05-03 22:49:52 -0400349static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200350{
Steven Rostedt2b499382011-05-03 22:49:52 -0400351 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200352 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400353 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200354 * CPU might be walking that list. We need to make sure
355 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400356 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200357 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400358 rcu_assign_pointer(*list, ops);
359}
Steven Rostedt3d083392008-05-12 21:20:42 +0200360
Steven Rostedt2b499382011-05-03 22:49:52 -0400361static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
362{
363 struct ftrace_ops **p;
364
365 /*
366 * If we are removing the last function, then simply point
367 * to the ftrace_stub.
368 */
369 if (*list == ops && ops->next == &ftrace_list_end) {
370 *list = &ftrace_list_end;
371 return 0;
372 }
373
374 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
375 if (*p == ops)
376 break;
377
378 if (*p != ops)
379 return -1;
380
381 *p = (*p)->next;
382 return 0;
383}
384
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400385static void ftrace_update_trampoline(struct ftrace_ops *ops);
386
Steven Rostedt2b499382011-05-03 22:49:52 -0400387static int __register_ftrace_function(struct ftrace_ops *ops)
388{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500389 if (ops->flags & FTRACE_OPS_FL_DELETED)
390 return -EINVAL;
391
Steven Rostedtb8489142011-05-04 09:27:52 -0400392 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
393 return -EBUSY;
394
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900395#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400396 /*
397 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
398 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
399 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
400 */
401 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
402 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
403 return -EINVAL;
404
405 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
406 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
407#endif
408
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400409 if (!core_kernel_data((unsigned long)ops))
410 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
411
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500412 if (ops->flags & FTRACE_OPS_FL_PER_CPU) {
413 if (per_cpu_ops_alloc(ops))
Jiri Olsae2484912012-02-15 15:51:48 +0100414 return -ENOMEM;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500415 }
416
417 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400418
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400419 /* Always save the function, and reset at unregistering */
420 ops->saved_func = ops->func;
421
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400422 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400423 ops->func = ftrace_pid_func;
424
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400425 ftrace_update_trampoline(ops);
426
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400427 if (ftrace_enabled)
428 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200429
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200430 return 0;
431}
432
Ingo Molnare309b412008-05-12 21:20:51 +0200433static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200434{
Steven Rostedt2b499382011-05-03 22:49:52 -0400435 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200436
Steven Rostedtb8489142011-05-04 09:27:52 -0400437 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
438 return -EBUSY;
439
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500440 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400441
Steven Rostedt2b499382011-05-03 22:49:52 -0400442 if (ret < 0)
443 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400444
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400445 if (ftrace_enabled)
446 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200447
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400448 ops->func = ops->saved_func;
449
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500450 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200451}
452
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500453static void ftrace_update_pid_func(void)
454{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400455 struct ftrace_ops *op;
456
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400457 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500458 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900459 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500460
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400461 do_for_each_ftrace_op(op, ftrace_ops_list) {
462 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400463 op->func = ftrace_pids_enabled(op) ?
464 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400465 ftrace_update_trampoline(op);
466 }
467 } while_for_each_ftrace_op(op);
468
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400469 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500470}
471
Steven Rostedt493762f2009-03-23 17:12:36 -0400472#ifdef CONFIG_FUNCTION_PROFILER
473struct ftrace_profile {
474 struct hlist_node node;
475 unsigned long ip;
476 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400477#ifdef CONFIG_FUNCTION_GRAPH_TRACER
478 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400479 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400480#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400481};
482
483struct ftrace_profile_page {
484 struct ftrace_profile_page *next;
485 unsigned long index;
486 struct ftrace_profile records[];
487};
488
Steven Rostedtcafb1682009-03-24 20:50:39 -0400489struct ftrace_profile_stat {
490 atomic_t disabled;
491 struct hlist_head *hash;
492 struct ftrace_profile_page *pages;
493 struct ftrace_profile_page *start;
494 struct tracer_stat stat;
495};
496
Steven Rostedt493762f2009-03-23 17:12:36 -0400497#define PROFILE_RECORDS_SIZE \
498 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
499
500#define PROFILES_PER_PAGE \
501 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
502
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400503static int ftrace_profile_enabled __read_mostly;
504
505/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400506static DEFINE_MUTEX(ftrace_profile_lock);
507
Steven Rostedtcafb1682009-03-24 20:50:39 -0400508static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400509
Namhyung Kim20079eb2013-04-10 08:55:50 +0900510#define FTRACE_PROFILE_HASH_BITS 10
511#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400512
Steven Rostedt493762f2009-03-23 17:12:36 -0400513static void *
514function_stat_next(void *v, int idx)
515{
516 struct ftrace_profile *rec = v;
517 struct ftrace_profile_page *pg;
518
519 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
520
521 again:
Li Zefan0296e422009-06-26 11:15:37 +0800522 if (idx != 0)
523 rec++;
524
Steven Rostedt493762f2009-03-23 17:12:36 -0400525 if ((void *)rec >= (void *)&pg->records[pg->index]) {
526 pg = pg->next;
527 if (!pg)
528 return NULL;
529 rec = &pg->records[0];
530 if (!rec->counter)
531 goto again;
532 }
533
534 return rec;
535}
536
537static void *function_stat_start(struct tracer_stat *trace)
538{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400539 struct ftrace_profile_stat *stat =
540 container_of(trace, struct ftrace_profile_stat, stat);
541
542 if (!stat || !stat->start)
543 return NULL;
544
545 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400546}
547
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400548#ifdef CONFIG_FUNCTION_GRAPH_TRACER
549/* function graph compares on total time */
550static int function_stat_cmp(void *p1, void *p2)
551{
552 struct ftrace_profile *a = p1;
553 struct ftrace_profile *b = p2;
554
555 if (a->time < b->time)
556 return -1;
557 if (a->time > b->time)
558 return 1;
559 else
560 return 0;
561}
562#else
563/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400564static int function_stat_cmp(void *p1, void *p2)
565{
566 struct ftrace_profile *a = p1;
567 struct ftrace_profile *b = p2;
568
569 if (a->counter < b->counter)
570 return -1;
571 if (a->counter > b->counter)
572 return 1;
573 else
574 return 0;
575}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400576#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400577
578static int function_stat_headers(struct seq_file *m)
579{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400580#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100581 seq_puts(m, " Function "
582 "Hit Time Avg s^2\n"
583 " -------- "
584 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400585#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100586 seq_puts(m, " Function Hit\n"
587 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400588#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400589 return 0;
590}
591
592static int function_stat_show(struct seq_file *m, void *v)
593{
594 struct ftrace_profile *rec = v;
595 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800596 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400597#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400598 static struct trace_seq s;
599 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400600 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400601#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800602 mutex_lock(&ftrace_profile_lock);
603
604 /* we raced with function_profile_reset() */
605 if (unlikely(rec->counter == 0)) {
606 ret = -EBUSY;
607 goto out;
608 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400609
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530610#ifdef CONFIG_FUNCTION_GRAPH_TRACER
611 avg = rec->time;
612 do_div(avg, rec->counter);
613 if (tracing_thresh && (avg < tracing_thresh))
614 goto out;
615#endif
616
Steven Rostedt493762f2009-03-23 17:12:36 -0400617 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400618 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400619
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400620#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100621 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400622
Chase Douglase330b3b2010-04-26 14:02:05 -0400623 /* Sample standard deviation (s^2) */
624 if (rec->counter <= 1)
625 stddev = 0;
626 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200627 /*
628 * Apply Welford's method:
629 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
630 */
631 stddev = rec->counter * rec->time_squared -
632 rec->time * rec->time;
633
Chase Douglase330b3b2010-04-26 14:02:05 -0400634 /*
635 * Divide only 1000 for ns^2 -> us^2 conversion.
636 * trace_print_graph_duration will divide 1000 again.
637 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200638 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400639 }
640
Steven Rostedt34886c82009-03-25 21:00:47 -0400641 trace_seq_init(&s);
642 trace_print_graph_duration(rec->time, &s);
643 trace_seq_puts(&s, " ");
644 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400645 trace_seq_puts(&s, " ");
646 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400647 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400648#endif
649 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800650out:
651 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400652
Li Zefan3aaba202010-08-23 16:50:12 +0800653 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400654}
655
Steven Rostedtcafb1682009-03-24 20:50:39 -0400656static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400657{
658 struct ftrace_profile_page *pg;
659
Steven Rostedtcafb1682009-03-24 20:50:39 -0400660 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400661
662 while (pg) {
663 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
664 pg->index = 0;
665 pg = pg->next;
666 }
667
Steven Rostedtcafb1682009-03-24 20:50:39 -0400668 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400669 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
670}
671
Steven Rostedtcafb1682009-03-24 20:50:39 -0400672int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400673{
674 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400675 int functions;
676 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400677 int i;
678
679 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400680 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 return 0;
682
Steven Rostedtcafb1682009-03-24 20:50:39 -0400683 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
684 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400685 return -ENOMEM;
686
Steven Rostedt318e0a72009-03-25 20:06:34 -0400687#ifdef CONFIG_DYNAMIC_FTRACE
688 functions = ftrace_update_tot_cnt;
689#else
690 /*
691 * We do not know the number of functions that exist because
692 * dynamic tracing is what counts them. With past experience
693 * we have around 20K functions. That should be more than enough.
694 * It is highly unlikely we will execute every function in
695 * the kernel.
696 */
697 functions = 20000;
698#endif
699
Steven Rostedtcafb1682009-03-24 20:50:39 -0400700 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400701
Steven Rostedt318e0a72009-03-25 20:06:34 -0400702 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
703
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900704 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400705 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400706 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400707 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400708 pg = pg->next;
709 }
710
711 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400712
713 out_free:
714 pg = stat->start;
715 while (pg) {
716 unsigned long tmp = (unsigned long)pg;
717
718 pg = pg->next;
719 free_page(tmp);
720 }
721
Steven Rostedt318e0a72009-03-25 20:06:34 -0400722 stat->pages = NULL;
723 stat->start = NULL;
724
725 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400726}
727
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400729{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400730 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400731 int size;
732
Steven Rostedtcafb1682009-03-24 20:50:39 -0400733 stat = &per_cpu(ftrace_profile_stats, cpu);
734
735 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400736 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400737 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400738 return 0;
739 }
740
741 /*
742 * We are profiling all functions, but usually only a few thousand
743 * functions are hit. We'll make a hash of 1024 items.
744 */
745 size = FTRACE_PROFILE_HASH_SIZE;
746
Steven Rostedtcafb1682009-03-24 20:50:39 -0400747 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400748
Steven Rostedtcafb1682009-03-24 20:50:39 -0400749 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400750 return -ENOMEM;
751
Steven Rostedt318e0a72009-03-25 20:06:34 -0400752 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400753 if (ftrace_profile_pages_init(stat) < 0) {
754 kfree(stat->hash);
755 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400756 return -ENOMEM;
757 }
758
759 return 0;
760}
761
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762static int ftrace_profile_init(void)
763{
764 int cpu;
765 int ret = 0;
766
Miao Xiec4602c12013-12-16 15:20:01 +0800767 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400768 ret = ftrace_profile_init_cpu(cpu);
769 if (ret)
770 break;
771 }
772
773 return ret;
774}
775
Steven Rostedt493762f2009-03-23 17:12:36 -0400776/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400777static struct ftrace_profile *
778ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400779{
780 struct ftrace_profile *rec;
781 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400782 unsigned long key;
783
Namhyung Kim20079eb2013-04-10 08:55:50 +0900784 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400785 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400786
787 if (hlist_empty(hhd))
788 return NULL;
789
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400790 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400791 if (rec->ip == ip)
792 return rec;
793 }
794
795 return NULL;
796}
797
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798static void ftrace_add_profile(struct ftrace_profile_stat *stat,
799 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400800{
801 unsigned long key;
802
Namhyung Kim20079eb2013-04-10 08:55:50 +0900803 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400805}
806
Steven Rostedt318e0a72009-03-25 20:06:34 -0400807/*
808 * The memory is already allocated, this simply finds a new record to use.
809 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400810static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400811ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400812{
813 struct ftrace_profile *rec = NULL;
814
Steven Rostedt318e0a72009-03-25 20:06:34 -0400815 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400817 goto out;
818
Steven Rostedt493762f2009-03-23 17:12:36 -0400819 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400820 * Try to find the function again since an NMI
821 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400822 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400823 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400824 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400825 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400826
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 if (stat->pages->index == PROFILES_PER_PAGE) {
828 if (!stat->pages->next)
829 goto out;
830 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400831 }
832
Steven Rostedtcafb1682009-03-24 20:50:39 -0400833 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400834 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400835 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400836
Steven Rostedt493762f2009-03-23 17:12:36 -0400837 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400838 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400839
840 return rec;
841}
842
Steven Rostedt493762f2009-03-23 17:12:36 -0400843static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400844function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400845 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400846{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400847 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400848 struct ftrace_profile *rec;
849 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400850
851 if (!ftrace_profile_enabled)
852 return;
853
Steven Rostedt493762f2009-03-23 17:12:36 -0400854 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400855
Christoph Lameterbdffd892014-04-29 14:17:40 -0500856 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400857 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400858 goto out;
859
860 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400861 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400862 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400863 if (!rec)
864 goto out;
865 }
866
867 rec->counter++;
868 out:
869 local_irq_restore(flags);
870}
871
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400872#ifdef CONFIG_FUNCTION_GRAPH_TRACER
873static int profile_graph_entry(struct ftrace_graph_ent *trace)
874{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400875 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400876 return 1;
877}
878
879static void profile_graph_return(struct ftrace_graph_ret *trace)
880{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400881 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400882 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400883 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400884 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400885
886 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500887 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400888 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400889 goto out;
890
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400891 /* If the calltime was zero'd ignore it */
892 if (!trace->calltime)
893 goto out;
894
Steven Rostedta2a16d62009-03-24 23:17:58 -0400895 calltime = trace->rettime - trace->calltime;
896
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400897 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400898 int index;
899
900 index = trace->depth;
901
902 /* Append this call time to the parent time to subtract */
903 if (index)
904 current->ret_stack[index - 1].subtime += calltime;
905
906 if (current->ret_stack[index].subtime < calltime)
907 calltime -= current->ret_stack[index].subtime;
908 else
909 calltime = 0;
910 }
911
Steven Rostedtcafb1682009-03-24 20:50:39 -0400912 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400913 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400914 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400915 rec->time_squared += calltime * calltime;
916 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400917
Steven Rostedtcafb1682009-03-24 20:50:39 -0400918 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400919 local_irq_restore(flags);
920}
921
922static int register_ftrace_profiler(void)
923{
924 return register_ftrace_graph(&profile_graph_return,
925 &profile_graph_entry);
926}
927
928static void unregister_ftrace_profiler(void)
929{
930 unregister_ftrace_graph();
931}
932#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100933static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400934 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900935 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400936 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400937};
938
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400939static int register_ftrace_profiler(void)
940{
941 return register_ftrace_function(&ftrace_profile_ops);
942}
943
944static void unregister_ftrace_profiler(void)
945{
946 unregister_ftrace_function(&ftrace_profile_ops);
947}
948#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
949
Steven Rostedt493762f2009-03-23 17:12:36 -0400950static ssize_t
951ftrace_profile_write(struct file *filp, const char __user *ubuf,
952 size_t cnt, loff_t *ppos)
953{
954 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400955 int ret;
956
Peter Huewe22fe9b52011-06-07 21:58:27 +0200957 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
958 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400959 return ret;
960
961 val = !!val;
962
963 mutex_lock(&ftrace_profile_lock);
964 if (ftrace_profile_enabled ^ val) {
965 if (val) {
966 ret = ftrace_profile_init();
967 if (ret < 0) {
968 cnt = ret;
969 goto out;
970 }
971
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400972 ret = register_ftrace_profiler();
973 if (ret < 0) {
974 cnt = ret;
975 goto out;
976 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400977 ftrace_profile_enabled = 1;
978 } else {
979 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400980 /*
981 * unregister_ftrace_profiler calls stop_machine
982 * so this acts like an synchronize_sched.
983 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400984 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400985 }
986 }
987 out:
988 mutex_unlock(&ftrace_profile_lock);
989
Jiri Olsacf8517c2009-10-23 19:36:16 -0400990 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400991
992 return cnt;
993}
994
995static ssize_t
996ftrace_profile_read(struct file *filp, char __user *ubuf,
997 size_t cnt, loff_t *ppos)
998{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400999 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001000 int r;
1001
1002 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1003 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1004}
1005
1006static const struct file_operations ftrace_profile_fops = {
1007 .open = tracing_open_generic,
1008 .read = ftrace_profile_read,
1009 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001010 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001011};
1012
Steven Rostedtcafb1682009-03-24 20:50:39 -04001013/* used to initialize the real stat files */
1014static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001015 .name = "functions",
1016 .stat_start = function_stat_start,
1017 .stat_next = function_stat_next,
1018 .stat_cmp = function_stat_cmp,
1019 .stat_headers = function_stat_headers,
1020 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001021};
1022
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001023static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001024{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001025 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001026 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001027 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001028 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001029 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001030
Steven Rostedtcafb1682009-03-24 20:50:39 -04001031 for_each_possible_cpu(cpu) {
1032 stat = &per_cpu(ftrace_profile_stats, cpu);
1033
Geliang Tang6363c6b2016-03-15 22:12:34 +08001034 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -04001035 if (!name) {
1036 /*
1037 * The files created are permanent, if something happens
1038 * we still do not free memory.
1039 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001040 WARN(1,
1041 "Could not allocate stat file for cpu %d\n",
1042 cpu);
1043 return;
1044 }
1045 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001046 stat->stat.name = name;
1047 ret = register_stat_tracer(&stat->stat);
1048 if (ret) {
1049 WARN(1,
1050 "Could not register function stat for cpu %d\n",
1051 cpu);
1052 kfree(name);
1053 return;
1054 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001055 }
1056
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001057 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001058 d_tracer, NULL, &ftrace_profile_fops);
1059 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001060 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001061}
1062
1063#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001064static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001065{
1066}
1067#endif /* CONFIG_FUNCTION_PROFILER */
1068
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001069static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1070
Pratyush Anand1619dc32015-03-06 23:58:06 +05301071#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1072static int ftrace_graph_active;
1073#else
1074# define ftrace_graph_active 0
1075#endif
1076
Steven Rostedt3d083392008-05-12 21:20:42 +02001077#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001078
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001079static struct ftrace_ops *removed_ops;
1080
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001081/*
1082 * Set when doing a global update, like enabling all recs or disabling them.
1083 * It is not set when just updating a single ftrace_ops.
1084 */
1085static bool update_all_ops;
1086
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001087#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001088# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001089#endif
1090
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001091static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1092
Steven Rostedtb6887d72009-02-17 12:32:04 -05001093struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001094 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001095 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001096 unsigned long flags;
1097 unsigned long ip;
1098 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001099 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001100};
1101
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001102struct ftrace_func_entry {
1103 struct hlist_node hlist;
1104 unsigned long ip;
1105};
1106
1107struct ftrace_hash {
1108 unsigned long size_bits;
1109 struct hlist_head *buckets;
1110 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001111 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001112};
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 (Red Hat)68f40962014-05-01 12:44:50 -04001189static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001190{
1191 return !hash || !hash->count;
1192}
1193
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001194static struct ftrace_func_entry *
1195ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1196{
1197 unsigned long key;
1198 struct ftrace_func_entry *entry;
1199 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001200
Steven Rostedt06a51d92011-12-19 19:07:36 -05001201 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001202 return NULL;
1203
1204 if (hash->size_bits > 0)
1205 key = hash_long(ip, hash->size_bits);
1206 else
1207 key = 0;
1208
1209 hhd = &hash->buckets[key];
1210
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001211 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001212 if (entry->ip == ip)
1213 return entry;
1214 }
1215 return NULL;
1216}
1217
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001218static void __add_hash_entry(struct ftrace_hash *hash,
1219 struct ftrace_func_entry *entry)
1220{
1221 struct hlist_head *hhd;
1222 unsigned long key;
1223
1224 if (hash->size_bits)
1225 key = hash_long(entry->ip, hash->size_bits);
1226 else
1227 key = 0;
1228
1229 hhd = &hash->buckets[key];
1230 hlist_add_head(&entry->hlist, hhd);
1231 hash->count++;
1232}
1233
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001234static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1235{
1236 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001237
1238 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1239 if (!entry)
1240 return -ENOMEM;
1241
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001242 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001243 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001244
1245 return 0;
1246}
1247
1248static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001249free_hash_entry(struct ftrace_hash *hash,
1250 struct ftrace_func_entry *entry)
1251{
1252 hlist_del(&entry->hlist);
1253 kfree(entry);
1254 hash->count--;
1255}
1256
1257static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001258remove_hash_entry(struct ftrace_hash *hash,
1259 struct ftrace_func_entry *entry)
1260{
1261 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001262 hash->count--;
1263}
1264
1265static void ftrace_hash_clear(struct ftrace_hash *hash)
1266{
1267 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001268 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001269 struct ftrace_func_entry *entry;
1270 int size = 1 << hash->size_bits;
1271 int i;
1272
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001273 if (!hash->count)
1274 return;
1275
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001276 for (i = 0; i < size; i++) {
1277 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001278 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001279 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001280 }
1281 FTRACE_WARN_ON(hash->count);
1282}
1283
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001284static void free_ftrace_hash(struct ftrace_hash *hash)
1285{
1286 if (!hash || hash == EMPTY_HASH)
1287 return;
1288 ftrace_hash_clear(hash);
1289 kfree(hash->buckets);
1290 kfree(hash);
1291}
1292
Steven Rostedt07fd5512011-05-05 18:03:47 -04001293static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1294{
1295 struct ftrace_hash *hash;
1296
1297 hash = container_of(rcu, struct ftrace_hash, rcu);
1298 free_ftrace_hash(hash);
1299}
1300
1301static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1302{
1303 if (!hash || hash == EMPTY_HASH)
1304 return;
1305 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1306}
1307
Jiri Olsa5500fa52012-02-15 15:51:54 +01001308void ftrace_free_filter(struct ftrace_ops *ops)
1309{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001310 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001311 free_ftrace_hash(ops->func_hash->filter_hash);
1312 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001313}
1314
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001315static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1316{
1317 struct ftrace_hash *hash;
1318 int size;
1319
1320 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1321 if (!hash)
1322 return NULL;
1323
1324 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001325 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326
1327 if (!hash->buckets) {
1328 kfree(hash);
1329 return NULL;
1330 }
1331
1332 hash->size_bits = size_bits;
1333
1334 return hash;
1335}
1336
1337static struct ftrace_hash *
1338alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1339{
1340 struct ftrace_func_entry *entry;
1341 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342 int size;
1343 int ret;
1344 int i;
1345
1346 new_hash = alloc_ftrace_hash(size_bits);
1347 if (!new_hash)
1348 return NULL;
1349
1350 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001351 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001352 return new_hash;
1353
1354 size = 1 << hash->size_bits;
1355 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001356 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001357 ret = add_hash_entry(new_hash, entry->ip);
1358 if (ret < 0)
1359 goto free_hash;
1360 }
1361 }
1362
1363 FTRACE_WARN_ON(new_hash->count != hash->count);
1364
1365 return new_hash;
1366
1367 free_hash:
1368 free_ftrace_hash(new_hash);
1369 return NULL;
1370}
1371
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001372static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001373ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001374static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001375ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001376
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001377static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1378 struct ftrace_hash *new_hash);
1379
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001380static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001381ftrace_hash_move(struct ftrace_ops *ops, int enable,
1382 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001383{
1384 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001385 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001386 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001387 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001388 int size = src->count;
1389 int bits = 0;
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001390 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001391 int i;
1392
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001393 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1394 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1395 return -EINVAL;
1396
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001397 /*
1398 * If the new source is empty, just free dst and assign it
1399 * the empty_hash.
1400 */
1401 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001402 new_hash = EMPTY_HASH;
1403 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001404 }
1405
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001406 /*
1407 * Make the hash size about 1/2 the # found
1408 */
1409 for (size /= 2; size; size >>= 1)
1410 bits++;
1411
1412 /* Don't allocate too much */
1413 if (bits > FTRACE_HASH_MAX_BITS)
1414 bits = FTRACE_HASH_MAX_BITS;
1415
Steven Rostedt07fd5512011-05-05 18:03:47 -04001416 new_hash = alloc_ftrace_hash(bits);
1417 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001418 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001419
1420 size = 1 << src->size_bits;
1421 for (i = 0; i < size; i++) {
1422 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001423 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001424 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001425 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001426 }
1427 }
1428
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001429update:
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001430 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1431 if (enable) {
1432 /* IPMODIFY should be updated only when filter_hash updating */
1433 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1434 if (ret < 0) {
1435 free_ftrace_hash(new_hash);
1436 return ret;
1437 }
1438 }
1439
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001440 /*
1441 * Remove the current set, update the hash and add
1442 * them back.
1443 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001444 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001445
Steven Rostedt07fd5512011-05-05 18:03:47 -04001446 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001447
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001448 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001449
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001450 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001451}
1452
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001453static bool hash_contains_ip(unsigned long ip,
1454 struct ftrace_ops_hash *hash)
1455{
1456 /*
1457 * The function record is a match if it exists in the filter
1458 * hash and not in the notrace hash. Note, an emty hash is
1459 * considered a match for the filter hash, but an empty
1460 * notrace hash is considered not in the notrace hash.
1461 */
1462 return (ftrace_hash_empty(hash->filter_hash) ||
1463 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1464 (ftrace_hash_empty(hash->notrace_hash) ||
1465 !ftrace_lookup_ip(hash->notrace_hash, ip));
1466}
1467
Steven Rostedt265c8312009-02-13 12:43:56 -05001468/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001469 * Test the hashes for this ops to see if we want to call
1470 * the ops->func or not.
1471 *
1472 * It's a match if the ip is in the ops->filter_hash or
1473 * the filter_hash does not exist or is empty,
1474 * AND
1475 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001476 *
1477 * This needs to be called with preemption disabled as
1478 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001479 */
1480static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001481ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001482{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001483 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001484 int ret;
1485
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001486#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1487 /*
1488 * There's a small race when adding ops that the ftrace handler
1489 * that wants regs, may be called without them. We can not
1490 * allow that handler to be called if regs is NULL.
1491 */
1492 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1493 return 0;
1494#endif
1495
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001496 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1497 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001498
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001499 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001500 ret = 1;
1501 else
1502 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001503
1504 return ret;
1505}
1506
1507/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001508 * This is a double for. Do not use 'break' to break out of the loop,
1509 * you must use a goto.
1510 */
1511#define do_for_each_ftrace_rec(pg, rec) \
1512 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1513 int _____i; \
1514 for (_____i = 0; _____i < pg->index; _____i++) { \
1515 rec = &pg->records[_____i];
1516
1517#define while_for_each_ftrace_rec() \
1518 } \
1519 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301520
Steven Rostedt5855fea2011-12-16 19:27:42 -05001521
1522static int ftrace_cmp_recs(const void *a, const void *b)
1523{
Steven Rostedta650e022012-04-25 13:48:13 -04001524 const struct dyn_ftrace *key = a;
1525 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001526
Steven Rostedta650e022012-04-25 13:48:13 -04001527 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001528 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001529 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1530 return 1;
1531 return 0;
1532}
1533
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001534/**
1535 * ftrace_location_range - return the first address of a traced location
1536 * if it touches the given ip range
1537 * @start: start of range to search.
1538 * @end: end of range to search (inclusive). @end points to the last byte
1539 * to check.
1540 *
1541 * Returns rec->ip if the related ftrace location is a least partly within
1542 * the given address range. That is, the first address of the instruction
1543 * that is either a NOP or call to the function tracer. It checks the ftrace
1544 * internal tables to determine if the address belongs or not.
1545 */
1546unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001547{
1548 struct ftrace_page *pg;
1549 struct dyn_ftrace *rec;
1550 struct dyn_ftrace key;
1551
1552 key.ip = start;
1553 key.flags = end; /* overload flags, as it is unsigned long */
1554
1555 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1556 if (end < pg->records[0].ip ||
1557 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1558 continue;
1559 rec = bsearch(&key, pg->records, pg->index,
1560 sizeof(struct dyn_ftrace),
1561 ftrace_cmp_recs);
1562 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001563 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001564 }
1565
Steven Rostedt5855fea2011-12-16 19:27:42 -05001566 return 0;
1567}
1568
Steven Rostedtc88fd862011-08-16 09:53:39 -04001569/**
1570 * ftrace_location - return true if the ip giving is a traced location
1571 * @ip: the instruction pointer to check
1572 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001573 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001574 * That is, the instruction that is either a NOP or call to
1575 * the function tracer. It checks the ftrace internal tables to
1576 * determine if the address belongs or not.
1577 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001578unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001579{
Steven Rostedta650e022012-04-25 13:48:13 -04001580 return ftrace_location_range(ip, ip);
1581}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001582
Steven Rostedta650e022012-04-25 13:48:13 -04001583/**
1584 * ftrace_text_reserved - return true if range contains an ftrace location
1585 * @start: start of range to search
1586 * @end: end of range to search (inclusive). @end points to the last byte to check.
1587 *
1588 * Returns 1 if @start and @end contains a ftrace location.
1589 * That is, the instruction that is either a NOP or call to
1590 * the function tracer. It checks the ftrace internal tables to
1591 * determine if the address belongs or not.
1592 */
Sasha Levind88471c2013-01-09 18:09:20 -05001593int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001594{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001595 unsigned long ret;
1596
1597 ret = ftrace_location_range((unsigned long)start,
1598 (unsigned long)end);
1599
1600 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001601}
1602
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001603/* Test if ops registered to this rec needs regs */
1604static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1605{
1606 struct ftrace_ops *ops;
1607 bool keep_regs = false;
1608
1609 for (ops = ftrace_ops_list;
1610 ops != &ftrace_list_end; ops = ops->next) {
1611 /* pass rec in as regs to have non-NULL val */
1612 if (ftrace_ops_test(ops, rec->ip, rec)) {
1613 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1614 keep_regs = true;
1615 break;
1616 }
1617 }
1618 }
1619
1620 return keep_regs;
1621}
1622
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001623static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001624 int filter_hash,
1625 bool inc)
1626{
1627 struct ftrace_hash *hash;
1628 struct ftrace_hash *other_hash;
1629 struct ftrace_page *pg;
1630 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001631 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001632 int count = 0;
1633 int all = 0;
1634
1635 /* Only update if the ops has been registered */
1636 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001637 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001638
1639 /*
1640 * In the filter_hash case:
1641 * If the count is zero, we update all records.
1642 * Otherwise we just update the items in the hash.
1643 *
1644 * In the notrace_hash case:
1645 * We enable the update in the hash.
1646 * As disabling notrace means enabling the tracing,
1647 * and enabling notrace means disabling, the inc variable
1648 * gets inversed.
1649 */
1650 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001651 hash = ops->func_hash->filter_hash;
1652 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001653 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001654 all = 1;
1655 } else {
1656 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001657 hash = ops->func_hash->notrace_hash;
1658 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001659 /*
1660 * If the notrace hash has no items,
1661 * then there's nothing to do.
1662 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001663 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001664 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001665 }
1666
1667 do_for_each_ftrace_rec(pg, rec) {
1668 int in_other_hash = 0;
1669 int in_hash = 0;
1670 int match = 0;
1671
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001672 if (rec->flags & FTRACE_FL_DISABLED)
1673 continue;
1674
Steven Rostedted926f92011-05-03 13:25:24 -04001675 if (all) {
1676 /*
1677 * Only the filter_hash affects all records.
1678 * Update if the record is not in the notrace hash.
1679 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001680 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001681 match = 1;
1682 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001683 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1684 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001685
1686 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001687 * If filter_hash is set, we want to match all functions
1688 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001689 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001690 * If filter_hash is not set, then we are decrementing.
1691 * That means we match anything that is in the hash
1692 * and also in the other_hash. That is, we need to turn
1693 * off functions in the other hash because they are disabled
1694 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001695 */
1696 if (filter_hash && in_hash && !in_other_hash)
1697 match = 1;
1698 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001699 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001700 match = 1;
1701 }
1702 if (!match)
1703 continue;
1704
1705 if (inc) {
1706 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001707 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001708 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001709
1710 /*
1711 * If there's only a single callback registered to a
1712 * function, and the ops has a trampoline registered
1713 * for it, then we can call it directly.
1714 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001715 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001716 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001717 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001718 /*
1719 * If we are adding another function callback
1720 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001721 * custom trampoline in use, then we need to go
1722 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001723 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001724 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001725
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001726 /*
1727 * If any ops wants regs saved for this function
1728 * then all ops will get saved regs.
1729 */
1730 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1731 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001732 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001733 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001734 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001735 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001736
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001737 /*
1738 * If the rec had REGS enabled and the ops that is
1739 * being removed had REGS set, then see if there is
1740 * still any ops for this record that wants regs.
1741 * If not, we can stop recording them.
1742 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001743 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001744 rec->flags & FTRACE_FL_REGS &&
1745 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1746 if (!test_rec_ops_needs_regs(rec))
1747 rec->flags &= ~FTRACE_FL_REGS;
1748 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001749
1750 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001751 * If the rec had TRAMP enabled, then it needs to
1752 * be cleared. As TRAMP can only be enabled iff
1753 * there is only a single ops attached to it.
1754 * In otherwords, always disable it on decrementing.
1755 * In the future, we may set it if rec count is
1756 * decremented to one, and the ops that is left
1757 * has a trampoline.
1758 */
1759 rec->flags &= ~FTRACE_FL_TRAMP;
1760
1761 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001762 * flags will be cleared in ftrace_check_record()
1763 * if rec count is zero.
1764 */
Steven Rostedted926f92011-05-03 13:25:24 -04001765 }
1766 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001767
1768 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1769 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1770
Steven Rostedted926f92011-05-03 13:25:24 -04001771 /* Shortcut, if we handled all records, we are done. */
1772 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001773 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001774 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001775
1776 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001777}
1778
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001779static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001780 int filter_hash)
1781{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001782 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001783}
1784
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001785static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001786 int filter_hash)
1787{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001788 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001789}
1790
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001791static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1792 int filter_hash, int inc)
1793{
1794 struct ftrace_ops *op;
1795
1796 __ftrace_hash_rec_update(ops, filter_hash, inc);
1797
1798 if (ops->func_hash != &global_ops.local_hash)
1799 return;
1800
1801 /*
1802 * If the ops shares the global_ops hash, then we need to update
1803 * all ops that are enabled and use this hash.
1804 */
1805 do_for_each_ftrace_op(op, ftrace_ops_list) {
1806 /* Already done */
1807 if (op == ops)
1808 continue;
1809 if (op->func_hash == &global_ops.local_hash)
1810 __ftrace_hash_rec_update(op, filter_hash, inc);
1811 } while_for_each_ftrace_op(op);
1812}
1813
1814static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1815 int filter_hash)
1816{
1817 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1818}
1819
1820static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1821 int filter_hash)
1822{
1823 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1824}
1825
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001826/*
1827 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1828 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1829 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1830 * Note that old_hash and new_hash has below meanings
1831 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1832 * - If the hash is EMPTY_HASH, it hits nothing
1833 * - Anything else hits the recs which match the hash entries.
1834 */
1835static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1836 struct ftrace_hash *old_hash,
1837 struct ftrace_hash *new_hash)
1838{
1839 struct ftrace_page *pg;
1840 struct dyn_ftrace *rec, *end = NULL;
1841 int in_old, in_new;
1842
1843 /* Only update if the ops has been registered */
1844 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1845 return 0;
1846
1847 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1848 return 0;
1849
1850 /*
1851 * Since the IPMODIFY is a very address sensitive action, we do not
1852 * allow ftrace_ops to set all functions to new hash.
1853 */
1854 if (!new_hash || !old_hash)
1855 return -EINVAL;
1856
1857 /* Update rec->flags */
1858 do_for_each_ftrace_rec(pg, rec) {
1859 /* We need to update only differences of filter_hash */
1860 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1861 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1862 if (in_old == in_new)
1863 continue;
1864
1865 if (in_new) {
1866 /* New entries must ensure no others are using it */
1867 if (rec->flags & FTRACE_FL_IPMODIFY)
1868 goto rollback;
1869 rec->flags |= FTRACE_FL_IPMODIFY;
1870 } else /* Removed entry */
1871 rec->flags &= ~FTRACE_FL_IPMODIFY;
1872 } while_for_each_ftrace_rec();
1873
1874 return 0;
1875
1876rollback:
1877 end = rec;
1878
1879 /* Roll back what we did above */
1880 do_for_each_ftrace_rec(pg, rec) {
1881 if (rec == end)
1882 goto err_out;
1883
1884 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1885 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1886 if (in_old == in_new)
1887 continue;
1888
1889 if (in_new)
1890 rec->flags &= ~FTRACE_FL_IPMODIFY;
1891 else
1892 rec->flags |= FTRACE_FL_IPMODIFY;
1893 } while_for_each_ftrace_rec();
1894
1895err_out:
1896 return -EBUSY;
1897}
1898
1899static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1900{
1901 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1902
1903 if (ftrace_hash_empty(hash))
1904 hash = NULL;
1905
1906 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1907}
1908
1909/* Disabling always succeeds */
1910static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1911{
1912 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1913
1914 if (ftrace_hash_empty(hash))
1915 hash = NULL;
1916
1917 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1918}
1919
1920static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1921 struct ftrace_hash *new_hash)
1922{
1923 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1924
1925 if (ftrace_hash_empty(old_hash))
1926 old_hash = NULL;
1927
1928 if (ftrace_hash_empty(new_hash))
1929 new_hash = NULL;
1930
1931 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1932}
1933
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001934static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001935{
1936 int i;
1937
1938 printk(KERN_CONT "%s", fmt);
1939
1940 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1941 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1942}
1943
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001944static struct ftrace_ops *
1945ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001946static struct ftrace_ops *
1947ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001948
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001949enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001950const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001951
1952static void print_bug_type(void)
1953{
1954 switch (ftrace_bug_type) {
1955 case FTRACE_BUG_UNKNOWN:
1956 break;
1957 case FTRACE_BUG_INIT:
1958 pr_info("Initializing ftrace call sites\n");
1959 break;
1960 case FTRACE_BUG_NOP:
1961 pr_info("Setting ftrace call site to NOP\n");
1962 break;
1963 case FTRACE_BUG_CALL:
1964 pr_info("Setting ftrace call site to call ftrace function\n");
1965 break;
1966 case FTRACE_BUG_UPDATE:
1967 pr_info("Updating ftrace call site to call a different ftrace function\n");
1968 break;
1969 }
1970}
1971
Steven Rostedtc88fd862011-08-16 09:53:39 -04001972/**
1973 * ftrace_bug - report and shutdown function tracer
1974 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001975 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001976 *
1977 * The arch code that enables or disables the function tracing
1978 * can call ftrace_bug() when it has detected a problem in
1979 * modifying the code. @failed should be one of either:
1980 * EFAULT - if the problem happens on reading the @ip address
1981 * EINVAL - if what is read at @ip is not what was expected
1982 * EPERM - if the problem happens on writting to the @ip address
1983 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001984void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001985{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001986 unsigned long ip = rec ? rec->ip : 0;
1987
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001988 switch (failed) {
1989 case -EFAULT:
1990 FTRACE_WARN_ON_ONCE(1);
1991 pr_info("ftrace faulted on modifying ");
1992 print_ip_sym(ip);
1993 break;
1994 case -EINVAL:
1995 FTRACE_WARN_ON_ONCE(1);
1996 pr_info("ftrace failed to modify ");
1997 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001998 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001999 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002000 if (ftrace_expected) {
2001 print_ip_ins(" expected: ", ftrace_expected);
2002 pr_cont("\n");
2003 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002004 break;
2005 case -EPERM:
2006 FTRACE_WARN_ON_ONCE(1);
2007 pr_info("ftrace faulted on writing ");
2008 print_ip_sym(ip);
2009 break;
2010 default:
2011 FTRACE_WARN_ON_ONCE(1);
2012 pr_info("ftrace faulted on unknown error ");
2013 print_ip_sym(ip);
2014 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002015 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002016 if (rec) {
2017 struct ftrace_ops *ops = NULL;
2018
2019 pr_info("ftrace record flags: %lx\n", rec->flags);
2020 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2021 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2022 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2023 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002024 if (ops) {
2025 do {
2026 pr_cont("\ttramp: %pS (%pS)",
2027 (void *)ops->trampoline,
2028 (void *)ops->func);
2029 ops = ftrace_find_tramp_ops_next(rec, ops);
2030 } while (ops);
2031 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002032 pr_cont("\ttramp: ERROR!");
2033
2034 }
2035 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002036 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002037 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002038}
2039
Steven Rostedtc88fd862011-08-16 09:53:39 -04002040static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002041{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002042 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002043
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002044 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2045
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002046 if (rec->flags & FTRACE_FL_DISABLED)
2047 return FTRACE_UPDATE_IGNORE;
2048
Steven Rostedt982c3502008-11-15 16:31:41 -05002049 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002050 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002051 *
Steven Rostedted926f92011-05-03 13:25:24 -04002052 * If the record has a ref count, then we need to enable it
2053 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002054 *
Steven Rostedted926f92011-05-03 13:25:24 -04002055 * Otherwise we make sure its disabled.
2056 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002057 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002058 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002059 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002060 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002061 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002062
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002063 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002064 * If enabling and the REGS flag does not match the REGS_EN, or
2065 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2066 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002067 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002068 if (flag) {
2069 if (!(rec->flags & FTRACE_FL_REGS) !=
2070 !(rec->flags & FTRACE_FL_REGS_EN))
2071 flag |= FTRACE_FL_REGS;
2072
2073 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2074 !(rec->flags & FTRACE_FL_TRAMP_EN))
2075 flag |= FTRACE_FL_TRAMP;
2076 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002077
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002078 /* If the state of this record hasn't changed, then do nothing */
2079 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002080 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002081
2082 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002083 /* Save off if rec is being enabled (for return value) */
2084 flag ^= rec->flags & FTRACE_FL_ENABLED;
2085
2086 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002087 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002088 if (flag & FTRACE_FL_REGS) {
2089 if (rec->flags & FTRACE_FL_REGS)
2090 rec->flags |= FTRACE_FL_REGS_EN;
2091 else
2092 rec->flags &= ~FTRACE_FL_REGS_EN;
2093 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002094 if (flag & FTRACE_FL_TRAMP) {
2095 if (rec->flags & FTRACE_FL_TRAMP)
2096 rec->flags |= FTRACE_FL_TRAMP_EN;
2097 else
2098 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2099 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002100 }
2101
2102 /*
2103 * If this record is being updated from a nop, then
2104 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002105 * Otherwise,
2106 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002107 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002108 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002109 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002110 if (flag & FTRACE_FL_ENABLED) {
2111 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002112 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002113 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002114
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002115 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002116 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002117 }
2118
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002119 if (update) {
2120 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002121 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002122 rec->flags = 0;
2123 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002124 /*
2125 * Just disable the record, but keep the ops TRAMP
2126 * and REGS states. The _EN flags must be disabled though.
2127 */
2128 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2129 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002130 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002131
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002132 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002133 return FTRACE_UPDATE_MAKE_NOP;
2134}
2135
2136/**
2137 * ftrace_update_record, set a record that now is tracing or not
2138 * @rec: the record to update
2139 * @enable: set to 1 if the record is tracing, zero to force disable
2140 *
2141 * The records that represent all functions that can be traced need
2142 * to be updated when tracing has been enabled.
2143 */
2144int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2145{
2146 return ftrace_check_record(rec, enable, 1);
2147}
2148
2149/**
2150 * ftrace_test_record, check if the record has been enabled or not
2151 * @rec: the record to test
2152 * @enable: set to 1 to check if enabled, 0 if it is disabled
2153 *
2154 * The arch code may need to test if a record is already set to
2155 * tracing to determine how to modify the function code that it
2156 * represents.
2157 */
2158int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2159{
2160 return ftrace_check_record(rec, enable, 0);
2161}
2162
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002163static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002164ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2165{
2166 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002167 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002168
2169 do_for_each_ftrace_op(op, ftrace_ops_list) {
2170
2171 if (!op->trampoline)
2172 continue;
2173
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002174 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002175 return op;
2176 } while_for_each_ftrace_op(op);
2177
2178 return NULL;
2179}
2180
2181static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002182ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2183 struct ftrace_ops *op)
2184{
2185 unsigned long ip = rec->ip;
2186
2187 while_for_each_ftrace_op(op) {
2188
2189 if (!op->trampoline)
2190 continue;
2191
2192 if (hash_contains_ip(ip, op->func_hash))
2193 return op;
2194 }
2195
2196 return NULL;
2197}
2198
2199static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002200ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2201{
2202 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002203 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002204
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002205 /*
2206 * Need to check removed ops first.
2207 * If they are being removed, and this rec has a tramp,
2208 * and this rec is in the ops list, then it would be the
2209 * one with the tramp.
2210 */
2211 if (removed_ops) {
2212 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002213 return removed_ops;
2214 }
2215
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002216 /*
2217 * Need to find the current trampoline for a rec.
2218 * Now, a trampoline is only attached to a rec if there
2219 * was a single 'ops' attached to it. But this can be called
2220 * when we are adding another op to the rec or removing the
2221 * current one. Thus, if the op is being added, we can
2222 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002223 * yet.
2224 *
2225 * If an ops is being modified (hooking to different functions)
2226 * then we don't care about the new functions that are being
2227 * added, just the old ones (that are probably being removed).
2228 *
2229 * If we are adding an ops to a function that already is using
2230 * a trampoline, it needs to be removed (trampolines are only
2231 * for single ops connected), then an ops that is not being
2232 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002233 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002234 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002235
2236 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002237 continue;
2238
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002239 /*
2240 * If the ops is being added, it hasn't gotten to
2241 * the point to be removed from this tree yet.
2242 */
2243 if (op->flags & FTRACE_OPS_FL_ADDING)
2244 continue;
2245
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002246
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002247 /*
2248 * If the ops is being modified and is in the old
2249 * hash, then it is probably being removed from this
2250 * function.
2251 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002252 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2253 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002254 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002255 /*
2256 * If the ops is not being added or modified, and it's
2257 * in its normal filter hash, then this must be the one
2258 * we want!
2259 */
2260 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2261 hash_contains_ip(ip, op->func_hash))
2262 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002263
2264 } while_for_each_ftrace_op(op);
2265
2266 return NULL;
2267}
2268
2269static struct ftrace_ops *
2270ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2271{
2272 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002273 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002274
2275 do_for_each_ftrace_op(op, ftrace_ops_list) {
2276 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002277 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002278 return op;
2279 } while_for_each_ftrace_op(op);
2280
2281 return NULL;
2282}
2283
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002284/**
2285 * ftrace_get_addr_new - Get the call address to set to
2286 * @rec: The ftrace record descriptor
2287 *
2288 * If the record has the FTRACE_FL_REGS set, that means that it
2289 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2290 * is not not set, then it wants to convert to the normal callback.
2291 *
2292 * Returns the address of the trampoline to set to
2293 */
2294unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2295{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002296 struct ftrace_ops *ops;
2297
2298 /* Trampolines take precedence over regs */
2299 if (rec->flags & FTRACE_FL_TRAMP) {
2300 ops = ftrace_find_tramp_ops_new(rec);
2301 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002302 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2303 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002304 /* Ftrace is shutting down, return anything */
2305 return (unsigned long)FTRACE_ADDR;
2306 }
2307 return ops->trampoline;
2308 }
2309
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002310 if (rec->flags & FTRACE_FL_REGS)
2311 return (unsigned long)FTRACE_REGS_ADDR;
2312 else
2313 return (unsigned long)FTRACE_ADDR;
2314}
2315
2316/**
2317 * ftrace_get_addr_curr - Get the call address that is already there
2318 * @rec: The ftrace record descriptor
2319 *
2320 * The FTRACE_FL_REGS_EN is set when the record already points to
2321 * a function that saves all the regs. Basically the '_EN' version
2322 * represents the current state of the function.
2323 *
2324 * Returns the address of the trampoline that is currently being called
2325 */
2326unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2327{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002328 struct ftrace_ops *ops;
2329
2330 /* Trampolines take precedence over regs */
2331 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2332 ops = ftrace_find_tramp_ops_curr(rec);
2333 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002334 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2335 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002336 /* Ftrace is shutting down, return anything */
2337 return (unsigned long)FTRACE_ADDR;
2338 }
2339 return ops->trampoline;
2340 }
2341
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002342 if (rec->flags & FTRACE_FL_REGS_EN)
2343 return (unsigned long)FTRACE_REGS_ADDR;
2344 else
2345 return (unsigned long)FTRACE_ADDR;
2346}
2347
Steven Rostedtc88fd862011-08-16 09:53:39 -04002348static int
2349__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2350{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002351 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002352 unsigned long ftrace_addr;
2353 int ret;
2354
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002355 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002356
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002357 /* This needs to be done before we call ftrace_update_record */
2358 ftrace_old_addr = ftrace_get_addr_curr(rec);
2359
2360 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002361
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002362 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2363
Steven Rostedtc88fd862011-08-16 09:53:39 -04002364 switch (ret) {
2365 case FTRACE_UPDATE_IGNORE:
2366 return 0;
2367
2368 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002369 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002370 return ftrace_make_call(rec, ftrace_addr);
2371
2372 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002373 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002374 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002375
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002376 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002377 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002378 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002379 }
2380
2381 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002382}
2383
Steven Rostedte4f5d542012-04-27 09:13:18 -04002384void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002385{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002386 struct dyn_ftrace *rec;
2387 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002388 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002389
Steven Rostedt45a4a232011-04-21 23:16:46 -04002390 if (unlikely(ftrace_disabled))
2391 return;
2392
Steven Rostedt265c8312009-02-13 12:43:56 -05002393 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002394 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002395 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002396 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002397 /* Stop processing */
2398 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002399 }
2400 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002401}
2402
Steven Rostedtc88fd862011-08-16 09:53:39 -04002403struct ftrace_rec_iter {
2404 struct ftrace_page *pg;
2405 int index;
2406};
2407
2408/**
2409 * ftrace_rec_iter_start, start up iterating over traced functions
2410 *
2411 * Returns an iterator handle that is used to iterate over all
2412 * the records that represent address locations where functions
2413 * are traced.
2414 *
2415 * May return NULL if no records are available.
2416 */
2417struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2418{
2419 /*
2420 * We only use a single iterator.
2421 * Protected by the ftrace_lock mutex.
2422 */
2423 static struct ftrace_rec_iter ftrace_rec_iter;
2424 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2425
2426 iter->pg = ftrace_pages_start;
2427 iter->index = 0;
2428
2429 /* Could have empty pages */
2430 while (iter->pg && !iter->pg->index)
2431 iter->pg = iter->pg->next;
2432
2433 if (!iter->pg)
2434 return NULL;
2435
2436 return iter;
2437}
2438
2439/**
2440 * ftrace_rec_iter_next, get the next record to process.
2441 * @iter: The handle to the iterator.
2442 *
2443 * Returns the next iterator after the given iterator @iter.
2444 */
2445struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2446{
2447 iter->index++;
2448
2449 if (iter->index >= iter->pg->index) {
2450 iter->pg = iter->pg->next;
2451 iter->index = 0;
2452
2453 /* Could have empty pages */
2454 while (iter->pg && !iter->pg->index)
2455 iter->pg = iter->pg->next;
2456 }
2457
2458 if (!iter->pg)
2459 return NULL;
2460
2461 return iter;
2462}
2463
2464/**
2465 * ftrace_rec_iter_record, get the record at the iterator location
2466 * @iter: The current iterator location
2467 *
2468 * Returns the record that the current @iter is at.
2469 */
2470struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2471{
2472 return &iter->pg->records[iter->index];
2473}
2474
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302475static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002476ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002477{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002478 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002479
Steven Rostedt45a4a232011-04-21 23:16:46 -04002480 if (unlikely(ftrace_disabled))
2481 return 0;
2482
Shaohua Li25aac9d2009-01-09 11:29:40 +08002483 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002484 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002485 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002486 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302487 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002488 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302489 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002490}
2491
Steven Rostedt000ab692009-02-17 13:35:06 -05002492/*
2493 * archs can override this function if they must do something
2494 * before the modifying code is performed.
2495 */
2496int __weak ftrace_arch_code_modify_prepare(void)
2497{
2498 return 0;
2499}
2500
2501/*
2502 * archs can override this function if they must do something
2503 * after the modifying code is performed.
2504 */
2505int __weak ftrace_arch_code_modify_post_process(void)
2506{
2507 return 0;
2508}
2509
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002510void ftrace_modify_all_code(int command)
2511{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002512 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002513 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002514
2515 /*
2516 * If the ftrace_caller calls a ftrace_ops func directly,
2517 * we need to make sure that it only traces functions it
2518 * expects to trace. When doing the switch of functions,
2519 * we need to update to the ftrace_ops_list_func first
2520 * before the transition between old and new calls are set,
2521 * as the ftrace_ops_list_func will check the ops hashes
2522 * to make sure the ops are having the right functions
2523 * traced.
2524 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002525 if (update) {
2526 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2527 if (FTRACE_WARN_ON(err))
2528 return;
2529 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002530
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002531 if (command & FTRACE_UPDATE_CALLS)
2532 ftrace_replace_code(1);
2533 else if (command & FTRACE_DISABLE_CALLS)
2534 ftrace_replace_code(0);
2535
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002536 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2537 function_trace_op = set_function_trace_op;
2538 smp_wmb();
2539 /* If irqs are disabled, we are in stop machine */
2540 if (!irqs_disabled())
2541 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002542 err = ftrace_update_ftrace_func(ftrace_trace_function);
2543 if (FTRACE_WARN_ON(err))
2544 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002545 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002546
2547 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002548 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002549 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002550 err = ftrace_disable_ftrace_graph_caller();
2551 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002552}
2553
Ingo Molnare309b412008-05-12 21:20:51 +02002554static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002555{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002556 int *command = data;
2557
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002558 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002559
Steven Rostedtc88fd862011-08-16 09:53:39 -04002560 return 0;
2561}
2562
2563/**
2564 * ftrace_run_stop_machine, go back to the stop machine method
2565 * @command: The command to tell ftrace what to do
2566 *
2567 * If an arch needs to fall back to the stop machine method, the
2568 * it can call this function.
2569 */
2570void ftrace_run_stop_machine(int command)
2571{
2572 stop_machine(__ftrace_modify_code, &command, NULL);
2573}
2574
2575/**
2576 * arch_ftrace_update_code, modify the code to trace or not trace
2577 * @command: The command that needs to be done
2578 *
2579 * Archs can override this function if it does not need to
2580 * run stop_machine() to modify code.
2581 */
2582void __weak arch_ftrace_update_code(int command)
2583{
2584 ftrace_run_stop_machine(command);
2585}
2586
2587static void ftrace_run_update_code(int command)
2588{
2589 int ret;
2590
2591 ret = ftrace_arch_code_modify_prepare();
2592 FTRACE_WARN_ON(ret);
2593 if (ret)
2594 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002595
2596 /*
2597 * By default we use stop_machine() to modify the code.
2598 * But archs can do what ever they want as long as it
2599 * is safe. The stop_machine() is the safest, but also
2600 * produces the most overhead.
2601 */
2602 arch_ftrace_update_code(command);
2603
Steven Rostedt000ab692009-02-17 13:35:06 -05002604 ret = ftrace_arch_code_modify_post_process();
2605 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002606}
2607
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002608static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002609 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002610{
2611 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002612 ops->old_hash.filter_hash = old_hash->filter_hash;
2613 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002614 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002615 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002616 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002617 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2618}
2619
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002620static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002621static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002622
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002623void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2624{
2625}
2626
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002627static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002628{
2629 free_percpu(ops->disabled);
2630}
2631
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002632static void ftrace_startup_enable(int command)
2633{
2634 if (saved_ftrace_func != ftrace_trace_function) {
2635 saved_ftrace_func = ftrace_trace_function;
2636 command |= FTRACE_UPDATE_TRACE_FUNC;
2637 }
2638
2639 if (!command || !ftrace_enabled)
2640 return;
2641
2642 ftrace_run_update_code(command);
2643}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002644
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002645static void ftrace_startup_all(int command)
2646{
2647 update_all_ops = true;
2648 ftrace_startup_enable(command);
2649 update_all_ops = false;
2650}
2651
Steven Rostedta1cd6172011-05-23 15:24:25 -04002652static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002653{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002654 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002655
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002656 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002657 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002658
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002659 ret = __register_ftrace_function(ops);
2660 if (ret)
2661 return ret;
2662
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002663 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002664
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002665 /*
2666 * Note that ftrace probes uses this to start up
2667 * and modify functions it will probe. But we still
2668 * set the ADDING flag for modification, as probes
2669 * do not have trampolines. If they add them in the
2670 * future, then the probes will need to distinguish
2671 * between adding and updating probes.
2672 */
2673 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002674
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002675 ret = ftrace_hash_ipmodify_enable(ops);
2676 if (ret < 0) {
2677 /* Rollback registration process */
2678 __unregister_ftrace_function(ops);
2679 ftrace_start_up--;
2680 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2681 return ret;
2682 }
2683
Jiri Olsa7f50d062016-03-16 15:34:33 +01002684 if (ftrace_hash_rec_enable(ops, 1))
2685 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002686
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002687 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002688
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002689 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2690
Steven Rostedta1cd6172011-05-23 15:24:25 -04002691 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002692}
2693
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002694static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002695{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002696 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002697
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002698 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002699 return -ENODEV;
2700
2701 ret = __unregister_ftrace_function(ops);
2702 if (ret)
2703 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002704
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002705 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002706 /*
2707 * Just warn in case of unbalance, no need to kill ftrace, it's not
2708 * critical but the ftrace_call callers may be never nopped again after
2709 * further ftrace uses.
2710 */
2711 WARN_ON_ONCE(ftrace_start_up < 0);
2712
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002713 /* Disabling ipmodify never fails */
2714 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002715
2716 if (ftrace_hash_rec_disable(ops, 1))
2717 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002718
Namhyung Kima737e6d2014-06-12 23:56:12 +09002719 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002720
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002721 if (saved_ftrace_func != ftrace_trace_function) {
2722 saved_ftrace_func = ftrace_trace_function;
2723 command |= FTRACE_UPDATE_TRACE_FUNC;
2724 }
2725
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002726 if (!command || !ftrace_enabled) {
2727 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002728 * If these are per_cpu ops, they still need their
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002729 * per_cpu field freed. Since, function tracing is
2730 * not currently active, we can just free them
2731 * without synchronizing all CPUs.
2732 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002733 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2734 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002735 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002736 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002737
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002738 /*
2739 * If the ops uses a trampoline, then it needs to be
2740 * tested first on update.
2741 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002742 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002743 removed_ops = ops;
2744
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002745 /* The trampoline logic checks the old hashes */
2746 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2747 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2748
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002749 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002750
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002751 /*
2752 * If there's no more ops registered with ftrace, run a
2753 * sanity check to make sure all rec flags are cleared.
2754 */
2755 if (ftrace_ops_list == &ftrace_list_end) {
2756 struct ftrace_page *pg;
2757 struct dyn_ftrace *rec;
2758
2759 do_for_each_ftrace_rec(pg, rec) {
2760 if (FTRACE_WARN_ON_ONCE(rec->flags))
2761 pr_warn(" %pS flags:%lx\n",
2762 (void *)rec->ip, rec->flags);
2763 } while_for_each_ftrace_rec();
2764 }
2765
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002766 ops->old_hash.filter_hash = NULL;
2767 ops->old_hash.notrace_hash = NULL;
2768
2769 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002770 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002771
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002772 /*
2773 * Dynamic ops may be freed, we must make sure that all
2774 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002775 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002776 * ops.
2777 *
2778 * Again, normal synchronize_sched() is not good enough.
2779 * We need to do a hard force of sched synchronization.
2780 * This is because we use preempt_disable() to do RCU, but
2781 * the function tracers can be called where RCU is not watching
2782 * (like before user_exit()). We can not rely on the RCU
2783 * infrastructure to do the synchronization, thus we must do it
2784 * ourselves.
2785 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002786 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002787 schedule_on_each_cpu(ftrace_sync);
2788
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002789 arch_ftrace_trampoline_free(ops);
2790
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002791 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2792 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002793 }
2794
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002795 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002796}
2797
Ingo Molnare309b412008-05-12 21:20:51 +02002798static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002799{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302800 int command;
2801
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002802 if (unlikely(ftrace_disabled))
2803 return;
2804
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002805 /* Force update next time */
2806 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002807 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302808 if (ftrace_start_up) {
2809 command = FTRACE_UPDATE_CALLS;
2810 if (ftrace_graph_active)
2811 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002812 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302813 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002814}
2815
Ingo Molnare309b412008-05-12 21:20:51 +02002816static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002817{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302818 int command;
2819
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002820 if (unlikely(ftrace_disabled))
2821 return;
2822
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002823 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302824 if (ftrace_start_up) {
2825 command = FTRACE_DISABLE_CALLS;
2826 if (ftrace_graph_active)
2827 command |= FTRACE_STOP_FUNC_RET;
2828 ftrace_run_update_code(command);
2829 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002830}
2831
Steven Rostedt3d083392008-05-12 21:20:42 +02002832static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002833unsigned long ftrace_update_tot_cnt;
2834
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002835static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002836{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002837 /*
2838 * Filter_hash being empty will default to trace module.
2839 * But notrace hash requires a test of individual module functions.
2840 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002841 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2842 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002843}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002844
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002845/*
2846 * Check if the current ops references the record.
2847 *
2848 * If the ops traces all functions, then it was already accounted for.
2849 * If the ops does not trace the current record function, skip it.
2850 * If the ops ignores the function via notrace filter, skip it.
2851 */
2852static inline bool
2853ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2854{
2855 /* If ops isn't enabled, ignore it */
2856 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2857 return 0;
2858
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002859 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002860 if (ops_traces_mod(ops))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002861 return 1;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002862
2863 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002864 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2865 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002866 return 0;
2867
2868 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002869 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002870 return 0;
2871
2872 return 1;
2873}
2874
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002875static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002876{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002877 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002878 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302879 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002880 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002881 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002882 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002883
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002884 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002885
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002886 /*
2887 * When a module is loaded, this function is called to convert
2888 * the calls to mcount in its text to nops, and also to create
2889 * an entry in the ftrace data. Now, if ftrace is activated
2890 * after this call, but before the module sets its text to
2891 * read-only, the modification of enabling ftrace can fail if
2892 * the read-only is done while ftrace is converting the calls.
2893 * To prevent this, the module's records are set as disabled
2894 * and will be enabled after the call to set the module's text
2895 * to read-only.
2896 */
2897 if (mod)
2898 rec_flags |= FTRACE_FL_DISABLED;
2899
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002900 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302901
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002902 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002903
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002904 /* If something went wrong, bail without enabling anything */
2905 if (unlikely(ftrace_disabled))
2906 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002907
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002908 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002909 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302910
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002911 /*
2912 * Do the initial record conversion from mcount jump
2913 * to the NOP instructions.
2914 */
2915 if (!ftrace_code_disable(mod, p))
2916 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002917
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002918 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002919 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002920 }
2921
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002922 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002923 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002924 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002925
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002926 return 0;
2927}
2928
Steven Rostedta7900872011-12-16 16:23:44 -05002929static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002930{
Steven Rostedta7900872011-12-16 16:23:44 -05002931 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002932 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002933
Steven Rostedta7900872011-12-16 16:23:44 -05002934 if (WARN_ON(!count))
2935 return -EINVAL;
2936
2937 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002938
2939 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002940 * We want to fill as much as possible. No more than a page
2941 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002942 */
Steven Rostedta7900872011-12-16 16:23:44 -05002943 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2944 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002945
Steven Rostedta7900872011-12-16 16:23:44 -05002946 again:
2947 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2948
2949 if (!pg->records) {
2950 /* if we can't allocate this size, try something smaller */
2951 if (!order)
2952 return -ENOMEM;
2953 order >>= 1;
2954 goto again;
2955 }
2956
2957 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2958 pg->size = cnt;
2959
2960 if (cnt > count)
2961 cnt = count;
2962
2963 return cnt;
2964}
2965
2966static struct ftrace_page *
2967ftrace_allocate_pages(unsigned long num_to_init)
2968{
2969 struct ftrace_page *start_pg;
2970 struct ftrace_page *pg;
2971 int order;
2972 int cnt;
2973
2974 if (!num_to_init)
2975 return 0;
2976
2977 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2978 if (!pg)
2979 return NULL;
2980
2981 /*
2982 * Try to allocate as much as possible in one continues
2983 * location that fills in all of the space. We want to
2984 * waste as little space as possible.
2985 */
2986 for (;;) {
2987 cnt = ftrace_allocate_records(pg, num_to_init);
2988 if (cnt < 0)
2989 goto free_pages;
2990
2991 num_to_init -= cnt;
2992 if (!num_to_init)
2993 break;
2994
2995 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2996 if (!pg->next)
2997 goto free_pages;
2998
2999 pg = pg->next;
3000 }
3001
3002 return start_pg;
3003
3004 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003005 pg = start_pg;
3006 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003007 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3008 free_pages((unsigned long)pg->records, order);
3009 start_pg = pg->next;
3010 kfree(pg);
3011 pg = start_pg;
3012 }
3013 pr_info("ftrace: FAILED to allocate memory for functions\n");
3014 return NULL;
3015}
3016
Steven Rostedt5072c592008-05-12 21:20:43 +02003017#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3018
3019struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003020 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003021 loff_t func_pos;
3022 struct ftrace_page *pg;
3023 struct dyn_ftrace *func;
3024 struct ftrace_func_probe *probe;
3025 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003026 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003027 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003028 int hidx;
3029 int idx;
3030 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003031};
3032
Ingo Molnare309b412008-05-12 21:20:51 +02003033static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003034t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003035{
3036 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003037 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003038 struct hlist_head *hhd;
3039
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003040 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003041 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003042
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003043 if (iter->probe)
3044 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003045 retry:
3046 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3047 return NULL;
3048
3049 hhd = &ftrace_func_hash[iter->hidx];
3050
3051 if (hlist_empty(hhd)) {
3052 iter->hidx++;
3053 hnd = NULL;
3054 goto retry;
3055 }
3056
3057 if (!hnd)
3058 hnd = hhd->first;
3059 else {
3060 hnd = hnd->next;
3061 if (!hnd) {
3062 iter->hidx++;
3063 goto retry;
3064 }
3065 }
3066
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003067 if (WARN_ON_ONCE(!hnd))
3068 return NULL;
3069
3070 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3071
3072 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003073}
3074
3075static void *t_hash_start(struct seq_file *m, loff_t *pos)
3076{
3077 struct ftrace_iterator *iter = m->private;
3078 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003079 loff_t l;
3080
Steven Rostedt69a30832011-12-19 15:21:16 -05003081 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3082 return NULL;
3083
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003084 if (iter->func_pos > *pos)
3085 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003086
Li Zefand82d6242009-06-24 09:54:54 +08003087 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003088 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003089 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003090 if (!p)
3091 break;
3092 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003093 if (!p)
3094 return NULL;
3095
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003096 /* Only set this if we have an item */
3097 iter->flags |= FTRACE_ITER_HASH;
3098
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003099 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003100}
3101
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003102static int
3103t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003104{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003105 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003106
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003107 rec = iter->probe;
3108 if (WARN_ON_ONCE(!rec))
3109 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003110
Steven Rostedt809dcf22009-02-16 23:06:01 -05003111 if (rec->ops->print)
3112 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3113
Steven Rostedtb375a112009-09-17 00:05:58 -04003114 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003115
3116 if (rec->data)
3117 seq_printf(m, ":%p", rec->data);
3118 seq_putc(m, '\n');
3119
3120 return 0;
3121}
3122
3123static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02003124t_next(struct seq_file *m, void *v, loff_t *pos)
3125{
3126 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003127 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003128 struct dyn_ftrace *rec = NULL;
3129
Steven Rostedt45a4a232011-04-21 23:16:46 -04003130 if (unlikely(ftrace_disabled))
3131 return NULL;
3132
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003133 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003134 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003135
Steven Rostedt5072c592008-05-12 21:20:43 +02003136 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01003137 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02003138
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003139 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003140 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003141
Steven Rostedt5072c592008-05-12 21:20:43 +02003142 retry:
3143 if (iter->idx >= iter->pg->index) {
3144 if (iter->pg->next) {
3145 iter->pg = iter->pg->next;
3146 iter->idx = 0;
3147 goto retry;
3148 }
3149 } else {
3150 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05003151 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003152 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05003153
Steven Rostedt41c52c02008-05-22 11:46:33 -04003154 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003155 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003156
3157 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003158 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003159
Steven Rostedt5072c592008-05-12 21:20:43 +02003160 rec = NULL;
3161 goto retry;
3162 }
3163 }
3164
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003165 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003166 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003167
3168 iter->func = rec;
3169
3170 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003171}
3172
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003173static void reset_iter_read(struct ftrace_iterator *iter)
3174{
3175 iter->pos = 0;
3176 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03003177 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003178}
3179
3180static void *t_start(struct seq_file *m, loff_t *pos)
3181{
3182 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003183 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003184 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003185 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003186
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003187 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003188
3189 if (unlikely(ftrace_disabled))
3190 return NULL;
3191
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003192 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003193 * If an lseek was done, then reset and start from beginning.
3194 */
3195 if (*pos < iter->pos)
3196 reset_iter_read(iter);
3197
3198 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003199 * For set_ftrace_filter reading, if we have the filter
3200 * off, we can short cut and just print out that all
3201 * functions are enabled.
3202 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003203 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003204 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003205 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003206 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003207 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003208 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003209 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003210 /* reset in case of seek/pread */
3211 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003212 return iter;
3213 }
3214
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003215 if (iter->flags & FTRACE_ITER_HASH)
3216 return t_hash_start(m, pos);
3217
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003218 /*
3219 * Unfortunately, we need to restart at ftrace_pages_start
3220 * every time we let go of the ftrace_mutex. This is because
3221 * those pointers can change without the lock.
3222 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003223 iter->pg = ftrace_pages_start;
3224 iter->idx = 0;
3225 for (l = 0; l <= *pos; ) {
3226 p = t_next(m, p, &l);
3227 if (!p)
3228 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003229 }
walimis5821e1b2008-11-15 15:19:06 +08003230
Steven Rostedt69a30832011-12-19 15:21:16 -05003231 if (!p)
3232 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003233
3234 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003235}
3236
3237static void t_stop(struct seq_file *m, void *p)
3238{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003239 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003240}
3241
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003242void * __weak
3243arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3244{
3245 return NULL;
3246}
3247
3248static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3249 struct dyn_ftrace *rec)
3250{
3251 void *ptr;
3252
3253 ptr = arch_ftrace_trampoline_func(ops, rec);
3254 if (ptr)
3255 seq_printf(m, " ->%pS", ptr);
3256}
3257
Steven Rostedt5072c592008-05-12 21:20:43 +02003258static int t_show(struct seq_file *m, void *v)
3259{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003260 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003261 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003262
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003263 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003264 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003265
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003266 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003267 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003268 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003269 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003270 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003271 return 0;
3272 }
3273
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003274 rec = iter->func;
3275
Steven Rostedt5072c592008-05-12 21:20:43 +02003276 if (!rec)
3277 return 0;
3278
Steven Rostedt647bcd02011-05-03 14:39:21 -04003279 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003280 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003281 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003282
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003283 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003284 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003285 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3286 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003287 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003288 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003289 if (ops) {
3290 do {
3291 seq_printf(m, "\ttramp: %pS (%pS)",
3292 (void *)ops->trampoline,
3293 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003294 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003295 ops = ftrace_find_tramp_ops_next(rec, ops);
3296 } while (ops);
3297 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003298 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003299 } else {
3300 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003301 }
3302 }
3303
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003304 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003305
3306 return 0;
3307}
3308
James Morris88e9d342009-09-22 16:43:43 -07003309static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003310 .start = t_start,
3311 .next = t_next,
3312 .stop = t_stop,
3313 .show = t_show,
3314};
3315
Ingo Molnare309b412008-05-12 21:20:51 +02003316static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003317ftrace_avail_open(struct inode *inode, struct file *file)
3318{
3319 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003320
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003321 if (unlikely(ftrace_disabled))
3322 return -ENODEV;
3323
Jiri Olsa50e18b92012-04-25 10:23:39 +02003324 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3325 if (iter) {
3326 iter->pg = ftrace_pages_start;
3327 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003328 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003329
Jiri Olsa50e18b92012-04-25 10:23:39 +02003330 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003331}
3332
Steven Rostedt647bcd02011-05-03 14:39:21 -04003333static int
3334ftrace_enabled_open(struct inode *inode, struct file *file)
3335{
3336 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003337
Jiri Olsa50e18b92012-04-25 10:23:39 +02003338 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3339 if (iter) {
3340 iter->pg = ftrace_pages_start;
3341 iter->flags = FTRACE_ITER_ENABLED;
3342 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003343 }
3344
Jiri Olsa50e18b92012-04-25 10:23:39 +02003345 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003346}
3347
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003348/**
3349 * ftrace_regex_open - initialize function tracer filter files
3350 * @ops: The ftrace_ops that hold the hash filters
3351 * @flag: The type of filter to process
3352 * @inode: The inode, usually passed in to your open routine
3353 * @file: The file, usually passed in to your open routine
3354 *
3355 * ftrace_regex_open() initializes the filter files for the
3356 * @ops. Depending on @flag it may process the filter hash or
3357 * the notrace hash of @ops. With this called from the open
3358 * routine, you can use ftrace_filter_write() for the write
3359 * routine if @flag has FTRACE_ITER_FILTER set, or
3360 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003361 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003362 * release must call ftrace_regex_release().
3363 */
3364int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003365ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003366 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003367{
3368 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003369 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003370 int ret = 0;
3371
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003372 ftrace_ops_init(ops);
3373
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003374 if (unlikely(ftrace_disabled))
3375 return -ENODEV;
3376
Steven Rostedt5072c592008-05-12 21:20:43 +02003377 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3378 if (!iter)
3379 return -ENOMEM;
3380
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003381 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3382 kfree(iter);
3383 return -ENOMEM;
3384 }
3385
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003386 iter->ops = ops;
3387 iter->flags = flag;
3388
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003389 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003390
Steven Rostedtf45948e2011-05-02 12:29:25 -04003391 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003392 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003393 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003394 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003395
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003396 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003397 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3398
3399 if (file->f_flags & O_TRUNC)
3400 iter->hash = alloc_ftrace_hash(size_bits);
3401 else
3402 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3403
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003404 if (!iter->hash) {
3405 trace_parser_put(&iter->parser);
3406 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003407 ret = -ENOMEM;
3408 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003409 }
3410 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003411
Steven Rostedt5072c592008-05-12 21:20:43 +02003412 if (file->f_mode & FMODE_READ) {
3413 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003414
3415 ret = seq_open(file, &show_ftrace_seq_ops);
3416 if (!ret) {
3417 struct seq_file *m = file->private_data;
3418 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003419 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003420 /* Failed */
3421 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003422 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003423 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003424 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003425 } else
3426 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003427
3428 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003429 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003430
3431 return ret;
3432}
3433
Steven Rostedt41c52c02008-05-22 11:46:33 -04003434static int
3435ftrace_filter_open(struct inode *inode, struct file *file)
3436{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003437 struct ftrace_ops *ops = inode->i_private;
3438
3439 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003440 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3441 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003442}
3443
3444static int
3445ftrace_notrace_open(struct inode *inode, struct file *file)
3446{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003447 struct ftrace_ops *ops = inode->i_private;
3448
3449 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003450 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003451}
3452
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003453/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3454struct ftrace_glob {
3455 char *search;
3456 unsigned len;
3457 int type;
3458};
3459
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003460/*
3461 * If symbols in an architecture don't correspond exactly to the user-visible
3462 * name of what they represent, it is possible to define this function to
3463 * perform the necessary adjustments.
3464*/
3465char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3466{
3467 return str;
3468}
3469
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003470static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003471{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003472 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003473 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003474
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003475 str = arch_ftrace_match_adjust(str, g->search);
3476
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003477 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003478 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003479 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003480 matched = 1;
3481 break;
3482 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003483 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003484 matched = 1;
3485 break;
3486 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003487 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003488 matched = 1;
3489 break;
3490 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003491 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003492 if (slen >= g->len &&
3493 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003494 matched = 1;
3495 break;
3496 }
3497
3498 return matched;
3499}
3500
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003501static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003502enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003503{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003504 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003505 int ret = 0;
3506
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003507 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003508 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003509 /* Do nothing if it doesn't exist */
3510 if (!entry)
3511 return 0;
3512
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003513 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003514 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003515 /* Do nothing if it exists */
3516 if (entry)
3517 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003518
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003519 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003520 }
3521 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003522}
3523
Steven Rostedt64e7c442009-02-13 17:08:48 -05003524static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003525ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3526 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003527{
3528 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003529 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003530
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003531 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3532
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003533 if (mod_g) {
3534 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3535
3536 /* blank module name to match all modules */
3537 if (!mod_g->len) {
3538 /* blank module globbing: modname xor exclude_mod */
3539 if ((!exclude_mod) != (!modname))
3540 goto func_match;
3541 return 0;
3542 }
3543
3544 /* not matching the module */
3545 if (!modname || !mod_matches) {
3546 if (exclude_mod)
3547 goto func_match;
3548 else
3549 return 0;
3550 }
3551
3552 if (mod_matches && exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003553 return 0;
3554
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003555func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003556 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003557 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003558 return 1;
3559 }
3560
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003561 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003562}
3563
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003564static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003565match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003566{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003567 struct ftrace_page *pg;
3568 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003569 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003570 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3571 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3572 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003573 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003574 int ret;
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003575 int clear_filter;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003576
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003577 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003578 func_g.type = filter_parse_regex(func, len, &func_g.search,
3579 &clear_filter);
3580 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003581 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003582
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003583 if (mod) {
3584 mod_g.type = filter_parse_regex(mod, strlen(mod),
3585 &mod_g.search, &exclude_mod);
3586 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003587 }
3588
Steven Rostedt52baf112009-02-14 01:15:39 -05003589 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003590
3591 if (unlikely(ftrace_disabled))
3592 goto out_unlock;
3593
Steven Rostedt265c8312009-02-13 12:43:56 -05003594 do_for_each_ftrace_rec(pg, rec) {
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003595 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003596 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003597 if (ret < 0) {
3598 found = ret;
3599 goto out_unlock;
3600 }
Li Zefan311d16d2009-12-08 11:15:11 +08003601 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003602 }
3603 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003604 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003605 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003606
3607 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003608}
3609
Steven Rostedt64e7c442009-02-13 17:08:48 -05003610static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003611ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003612{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003613 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003614}
3615
Steven Rostedt64e7c442009-02-13 17:08:48 -05003616
Steven Rostedtf6180772009-02-14 00:40:25 -05003617/*
3618 * We register the module command as a template to show others how
3619 * to register the a command as well.
3620 */
3621
3622static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003623ftrace_mod_callback(struct ftrace_hash *hash,
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003624 char *func, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003625{
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003626 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003627
3628 /*
3629 * cmd == 'mod' because we only registered this func
3630 * for the 'mod' ftrace_func_command.
3631 * But if you register one func with multiple commands,
3632 * you can tell which command was used by the cmd
3633 * parameter.
3634 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003635 ret = match_records(hash, func, strlen(func), module);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003636 if (!ret)
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003637 return -EINVAL;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003638 if (ret < 0)
3639 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003640 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003641}
3642
3643static struct ftrace_func_command ftrace_mod_cmd = {
3644 .name = "mod",
3645 .func = ftrace_mod_callback,
3646};
3647
3648static int __init ftrace_mod_cmd_init(void)
3649{
3650 return register_ftrace_command(&ftrace_mod_cmd);
3651}
Steven Rostedt6f415672012-10-05 12:13:07 -04003652core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003653
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003654static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003655 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003656{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003657 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003658 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003659 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003660
3661 key = hash_long(ip, FTRACE_HASH_BITS);
3662
3663 hhd = &ftrace_func_hash[key];
3664
3665 if (hlist_empty(hhd))
3666 return;
3667
3668 /*
3669 * Disable preemption for these calls to prevent a RCU grace
3670 * period. This syncs the hash iteration and freeing of items
3671 * on the hash. rcu_read_lock is too dangerous here.
3672 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003673 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003674 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003675 if (entry->ip == ip)
3676 entry->ops->func(ip, parent_ip, &entry->data);
3677 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003678 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003679}
3680
Steven Rostedtb6887d72009-02-17 12:32:04 -05003681static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003682{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003683 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003684 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003685 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003686};
3687
Steven Rostedtb6887d72009-02-17 12:32:04 -05003688static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003689
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003690static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003691{
Steven Rostedtb8489142011-05-04 09:27:52 -04003692 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003693 int i;
3694
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003695 if (ftrace_probe_registered) {
3696 /* still need to update the function call sites */
3697 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003698 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3699 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003700 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003701 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003702
3703 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3704 struct hlist_head *hhd = &ftrace_func_hash[i];
3705 if (hhd->first)
3706 break;
3707 }
3708 /* Nothing registered? */
3709 if (i == FTRACE_FUNC_HASHSIZE)
3710 return;
3711
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003712 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003713
Steven Rostedtb6887d72009-02-17 12:32:04 -05003714 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003715}
3716
Steven Rostedtb6887d72009-02-17 12:32:04 -05003717static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003718{
3719 int i;
3720
Steven Rostedtb6887d72009-02-17 12:32:04 -05003721 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003722 return;
3723
3724 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3725 struct hlist_head *hhd = &ftrace_func_hash[i];
3726 if (hhd->first)
3727 return;
3728 }
3729
3730 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003731 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003732
Steven Rostedtb6887d72009-02-17 12:32:04 -05003733 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003734}
3735
3736
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003737static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003738{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003739 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003740 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003741 kfree(entry);
3742}
3743
Steven Rostedt59df055f2009-02-14 15:29:06 -05003744int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003745register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003746 void *data)
3747{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003748 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003749 struct ftrace_func_probe *entry;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003750 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003751 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003752 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003753 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003754 struct ftrace_page *pg;
3755 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003756 int not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003757 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003758 int count = 0;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003759 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003760
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003761 func_g.type = filter_parse_regex(glob, strlen(glob),
3762 &func_g.search, &not);
3763 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003764
Steven Rostedtb6887d72009-02-17 12:32:04 -05003765 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003766 if (WARN_ON(not))
3767 return -EINVAL;
3768
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003769 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003770
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003771 old_hash_ops.filter_hash = old_hash;
3772 /* Probes only have filters */
3773 old_hash_ops.notrace_hash = NULL;
3774
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003775 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003776 if (!hash) {
3777 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003778 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003779 }
3780
3781 if (unlikely(ftrace_disabled)) {
3782 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003783 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003784 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003785
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003786 mutex_lock(&ftrace_lock);
3787
Steven Rostedt59df055f2009-02-14 15:29:06 -05003788 do_for_each_ftrace_rec(pg, rec) {
3789
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003790 if (!ftrace_match_record(rec, &func_g, NULL, 0))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003791 continue;
3792
3793 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3794 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003795 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003796 if (!count)
3797 count = -ENOMEM;
3798 goto out_unlock;
3799 }
3800
3801 count++;
3802
3803 entry->data = data;
3804
3805 /*
3806 * The caller might want to do something special
3807 * for each function we find. We call the callback
3808 * to give the caller an opportunity to do so.
3809 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003810 if (ops->init) {
3811 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003812 /* caller does not like this func */
3813 kfree(entry);
3814 continue;
3815 }
3816 }
3817
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003818 ret = enter_record(hash, rec, 0);
3819 if (ret < 0) {
3820 kfree(entry);
3821 count = ret;
3822 goto out_unlock;
3823 }
3824
Steven Rostedt59df055f2009-02-14 15:29:06 -05003825 entry->ops = ops;
3826 entry->ip = rec->ip;
3827
3828 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3829 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3830
3831 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003832
3833 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003834
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003835 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003836
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003837 if (!ret)
3838 free_ftrace_hash_rcu(old_hash);
3839 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003840 count = ret;
3841
Steven Rostedt59df055f2009-02-14 15:29:06 -05003842 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003843 mutex_unlock(&ftrace_lock);
3844 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003845 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003846 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003847
3848 return count;
3849}
3850
3851enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003852 PROBE_TEST_FUNC = 1,
3853 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003854};
3855
3856static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003857__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003858 void *data, int flags)
3859{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003860 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003861 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003862 struct ftrace_func_probe *p;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003863 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003864 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003865 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003866 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003867 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003868 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003869 char str[KSYM_SYMBOL_LEN];
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003870 int i, ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003871
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003872 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003873 func_g.search = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003874 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003875 int not;
3876
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003877 func_g.type = filter_parse_regex(glob, strlen(glob),
3878 &func_g.search, &not);
3879 func_g.len = strlen(func_g.search);
3880 func_g.search = glob;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003881
Steven Rostedtb6887d72009-02-17 12:32:04 -05003882 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003883 if (WARN_ON(not))
3884 return;
3885 }
3886
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003887 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003888
3889 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3890 if (!hash)
3891 /* Hmm, should report this somehow */
3892 goto out_unlock;
3893
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003894 INIT_LIST_HEAD(&free_list);
3895
Steven Rostedt59df055f2009-02-14 15:29:06 -05003896 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3897 struct hlist_head *hhd = &ftrace_func_hash[i];
3898
Sasha Levinb67bfe02013-02-27 17:06:00 -08003899 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003900
3901 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003902 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003903 continue;
3904
Steven Rostedtb6887d72009-02-17 12:32:04 -05003905 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003906 continue;
3907
3908 /* do this last, since it is the most expensive */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003909 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003910 kallsyms_lookup(entry->ip, NULL, NULL,
3911 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003912 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003913 continue;
3914 }
3915
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003916 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3917 /* It is possible more than one entry had this ip */
3918 if (rec_entry)
3919 free_hash_entry(hash, rec_entry);
3920
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003921 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003922 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003923 }
3924 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003925 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003926 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003927 /*
3928 * Remove after the disable is called. Otherwise, if the last
3929 * probe is removed, a null hash means *all enabled*.
3930 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003931 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003932 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003933 if (!ret)
3934 free_ftrace_hash_rcu(old_hash);
3935
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003936 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3937 list_del(&entry->free_list);
3938 ftrace_free_entry(entry);
3939 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003940 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003941
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003942 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003943 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003944 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003945}
3946
3947void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003948unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003949 void *data)
3950{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003951 __unregister_ftrace_function_probe(glob, ops, data,
3952 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003953}
3954
3955void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003956unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003957{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003958 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003959}
3960
Steven Rostedtb6887d72009-02-17 12:32:04 -05003961void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003962{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003963 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003964}
3965
Steven Rostedtf6180772009-02-14 00:40:25 -05003966static LIST_HEAD(ftrace_commands);
3967static DEFINE_MUTEX(ftrace_cmd_mutex);
3968
Tom Zanussi38de93a2013-10-24 08:34:18 -05003969/*
3970 * Currently we only register ftrace commands from __init, so mark this
3971 * __init too.
3972 */
3973__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003974{
3975 struct ftrace_func_command *p;
3976 int ret = 0;
3977
3978 mutex_lock(&ftrace_cmd_mutex);
3979 list_for_each_entry(p, &ftrace_commands, list) {
3980 if (strcmp(cmd->name, p->name) == 0) {
3981 ret = -EBUSY;
3982 goto out_unlock;
3983 }
3984 }
3985 list_add(&cmd->list, &ftrace_commands);
3986 out_unlock:
3987 mutex_unlock(&ftrace_cmd_mutex);
3988
3989 return ret;
3990}
3991
Tom Zanussi38de93a2013-10-24 08:34:18 -05003992/*
3993 * Currently we only unregister ftrace commands from __init, so mark
3994 * this __init too.
3995 */
3996__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003997{
3998 struct ftrace_func_command *p, *n;
3999 int ret = -ENODEV;
4000
4001 mutex_lock(&ftrace_cmd_mutex);
4002 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4003 if (strcmp(cmd->name, p->name) == 0) {
4004 ret = 0;
4005 list_del_init(&p->list);
4006 goto out_unlock;
4007 }
4008 }
4009 out_unlock:
4010 mutex_unlock(&ftrace_cmd_mutex);
4011
4012 return ret;
4013}
4014
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004015static int ftrace_process_regex(struct ftrace_hash *hash,
4016 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004017{
Steven Rostedtf6180772009-02-14 00:40:25 -05004018 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004019 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004020 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004021
4022 func = strsep(&next, ":");
4023
4024 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004025 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004026 if (!ret)
4027 ret = -EINVAL;
4028 if (ret < 0)
4029 return ret;
4030 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004031 }
4032
Steven Rostedtf6180772009-02-14 00:40:25 -05004033 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004034
4035 command = strsep(&next, ":");
4036
Steven Rostedtf6180772009-02-14 00:40:25 -05004037 mutex_lock(&ftrace_cmd_mutex);
4038 list_for_each_entry(p, &ftrace_commands, list) {
4039 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04004040 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004041 goto out_unlock;
4042 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004043 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004044 out_unlock:
4045 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004046
Steven Rostedtf6180772009-02-14 00:40:25 -05004047 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004048}
4049
Ingo Molnare309b412008-05-12 21:20:51 +02004050static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004051ftrace_regex_write(struct file *file, const char __user *ubuf,
4052 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004053{
4054 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004055 struct trace_parser *parser;
4056 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004057
Li Zefan4ba79782009-09-22 13:52:20 +08004058 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004059 return 0;
4060
Steven Rostedt5072c592008-05-12 21:20:43 +02004061 if (file->f_mode & FMODE_READ) {
4062 struct seq_file *m = file->private_data;
4063 iter = m->private;
4064 } else
4065 iter = file->private_data;
4066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004067 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004068 return -ENODEV;
4069
4070 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004071
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004072 parser = &iter->parser;
4073 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004074
Li Zefan4ba79782009-09-22 13:52:20 +08004075 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004076 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004077 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004078 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004079 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004080 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004081 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004082 }
4083
Steven Rostedt5072c592008-05-12 21:20:43 +02004084 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004085 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004086 return ret;
4087}
4088
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004089ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004090ftrace_filter_write(struct file *file, const char __user *ubuf,
4091 size_t cnt, loff_t *ppos)
4092{
4093 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4094}
4095
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004096ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004097ftrace_notrace_write(struct file *file, const char __user *ubuf,
4098 size_t cnt, loff_t *ppos)
4099{
4100 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4101}
4102
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004103static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004104ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4105{
4106 struct ftrace_func_entry *entry;
4107
4108 if (!ftrace_location(ip))
4109 return -EINVAL;
4110
4111 if (remove) {
4112 entry = ftrace_lookup_ip(hash, ip);
4113 if (!entry)
4114 return -ENOENT;
4115 free_hash_entry(hash, entry);
4116 return 0;
4117 }
4118
4119 return add_hash_entry(hash, ip);
4120}
4121
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004122static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004123 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004124{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004125 struct ftrace_ops *op;
4126
4127 if (!ftrace_enabled)
4128 return;
4129
4130 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004131 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004132 return;
4133 }
4134
4135 /*
4136 * If this is the shared global_ops filter, then we need to
4137 * check if there is another ops that shares it, is enabled.
4138 * If so, we still need to run the modify code.
4139 */
4140 if (ops->func_hash != &global_ops.local_hash)
4141 return;
4142
4143 do_for_each_ftrace_op(op, ftrace_ops_list) {
4144 if (op->func_hash == &global_ops.local_hash &&
4145 op->flags & FTRACE_OPS_FL_ENABLED) {
4146 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4147 /* Only need to do this once */
4148 return;
4149 }
4150 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004151}
4152
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004153static int
4154ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4155 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004156{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004157 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004158 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004159 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004160 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004161 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004162
Steven Rostedt41c52c02008-05-22 11:46:33 -04004163 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004164 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004165
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004166 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004167
Steven Rostedtf45948e2011-05-02 12:29:25 -04004168 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004169 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004170 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004171 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004172
Wang Nanb972cc52014-07-15 08:40:20 +08004173 if (reset)
4174 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4175 else
4176 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4177
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004178 if (!hash) {
4179 ret = -ENOMEM;
4180 goto out_regex_unlock;
4181 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004182
Jiri Olsaac483c42012-01-02 10:04:14 +01004183 if (buf && !ftrace_match_records(hash, buf, len)) {
4184 ret = -EINVAL;
4185 goto out_regex_unlock;
4186 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004187 if (ip) {
4188 ret = ftrace_match_addr(hash, ip, remove);
4189 if (ret < 0)
4190 goto out_regex_unlock;
4191 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004192
4193 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004194 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004195 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4196 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004197 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004198 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004199 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004200 free_ftrace_hash_rcu(old_hash);
4201 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004202 mutex_unlock(&ftrace_lock);
4203
Jiri Olsaac483c42012-01-02 10:04:14 +01004204 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004205 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004206
4207 free_ftrace_hash(hash);
4208 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004209}
4210
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004211static int
4212ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4213 int reset, int enable)
4214{
4215 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4216}
4217
4218/**
4219 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4220 * @ops - the ops to set the filter with
4221 * @ip - the address to add to or remove from the filter.
4222 * @remove - non zero to remove the ip from the filter
4223 * @reset - non zero to reset all filters before applying this filter.
4224 *
4225 * Filters denote which functions should be enabled when tracing is enabled
4226 * If @ip is NULL, it failes to update filter.
4227 */
4228int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4229 int remove, int reset)
4230{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004231 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004232 return ftrace_set_addr(ops, ip, remove, reset, 1);
4233}
4234EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4235
4236static int
4237ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4238 int reset, int enable)
4239{
4240 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4241}
4242
Steven Rostedt77a2b372008-05-12 21:20:45 +02004243/**
4244 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004245 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004246 * @buf - the string that holds the function filter text.
4247 * @len - the length of the string.
4248 * @reset - non zero to reset all filters before applying this filter.
4249 *
4250 * Filters denote which functions should be enabled when tracing is enabled.
4251 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4252 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004253int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004254 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004255{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004256 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004257 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004258}
Steven Rostedt936e0742011-05-05 22:54:01 -04004259EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004260
Steven Rostedt41c52c02008-05-22 11:46:33 -04004261/**
4262 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004263 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004264 * @buf - the string that holds the function notrace text.
4265 * @len - the length of the string.
4266 * @reset - non zero to reset all filters before applying this filter.
4267 *
4268 * Notrace Filters denote which functions should not be enabled when tracing
4269 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4270 * for tracing.
4271 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004272int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004273 int len, int reset)
4274{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004275 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004276 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004277}
4278EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4279/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004280 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004281 * @buf - the string that holds the function filter text.
4282 * @len - the length of the string.
4283 * @reset - non zero to reset all filters before applying this filter.
4284 *
4285 * Filters denote which functions should be enabled when tracing is enabled.
4286 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4287 */
4288void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4289{
4290 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4291}
4292EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4293
4294/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004295 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004296 * @buf - the string that holds the function notrace text.
4297 * @len - the length of the string.
4298 * @reset - non zero to reset all filters before applying this filter.
4299 *
4300 * Notrace Filters denote which functions should not be enabled when tracing
4301 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4302 * for tracing.
4303 */
4304void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004305{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004306 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004307}
Steven Rostedt936e0742011-05-05 22:54:01 -04004308EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004309
Steven Rostedt2af15d62009-05-28 13:37:24 -04004310/*
4311 * command line interface to allow users to set filters on boot up.
4312 */
4313#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4314static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4315static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4316
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004317/* Used by function selftest to not test if filter is set */
4318bool ftrace_filter_param __initdata;
4319
Steven Rostedt2af15d62009-05-28 13:37:24 -04004320static int __init set_ftrace_notrace(char *str)
4321{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004322 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004323 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004324 return 1;
4325}
4326__setup("ftrace_notrace=", set_ftrace_notrace);
4327
4328static int __init set_ftrace_filter(char *str)
4329{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004330 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004331 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004332 return 1;
4333}
4334__setup("ftrace_filter=", set_ftrace_filter);
4335
Stefan Assmann369bc182009-10-12 22:17:21 +02004336#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004337static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004338static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004339static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004340
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004341static unsigned long save_global_trampoline;
4342static unsigned long save_global_flags;
4343
Stefan Assmann369bc182009-10-12 22:17:21 +02004344static int __init set_graph_function(char *str)
4345{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004346 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004347 return 1;
4348}
4349__setup("ftrace_graph_filter=", set_graph_function);
4350
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004351static int __init set_graph_notrace_function(char *str)
4352{
4353 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4354 return 1;
4355}
4356__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4357
4358static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004359{
4360 int ret;
4361 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004362 unsigned long *table = ftrace_graph_funcs;
4363 int *count = &ftrace_graph_count;
4364
4365 if (!enable) {
4366 table = ftrace_graph_notrace_funcs;
4367 count = &ftrace_graph_notrace_count;
4368 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004369
4370 while (buf) {
4371 func = strsep(&buf, ",");
4372 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004373 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004374 if (ret)
4375 printk(KERN_DEBUG "ftrace: function %s not "
4376 "traceable\n", func);
4377 }
4378}
4379#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4380
Steven Rostedt2a85a372011-12-19 21:57:44 -05004381void __init
4382ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004383{
4384 char *func;
4385
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004386 ftrace_ops_init(ops);
4387
Steven Rostedt2af15d62009-05-28 13:37:24 -04004388 while (buf) {
4389 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004390 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004391 }
4392}
4393
4394static void __init set_ftrace_early_filters(void)
4395{
4396 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004397 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004398 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004399 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004400#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4401 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004402 set_ftrace_early_graph(ftrace_graph_buf, 1);
4403 if (ftrace_graph_notrace_buf[0])
4404 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004405#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004406}
4407
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004408int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004409{
4410 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004411 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004412 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004413 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004414 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004415 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004416 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004417 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004418
Steven Rostedt5072c592008-05-12 21:20:43 +02004419 if (file->f_mode & FMODE_READ) {
4420 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004421 seq_release(inode, file);
4422 } else
4423 iter = file->private_data;
4424
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004425 parser = &iter->parser;
4426 if (trace_parser_loaded(parser)) {
4427 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004428 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004429 }
4430
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004431 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004432
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004433 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004434
Steven Rostedt058e2972011-04-29 22:35:33 -04004435 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004436 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4437
4438 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004439 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004440 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004441 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004442
Steven Rostedt058e2972011-04-29 22:35:33 -04004443 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004444 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004445 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4446 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004447 ret = ftrace_hash_move(iter->ops, filter_hash,
4448 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004449 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004450 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004451 free_ftrace_hash_rcu(old_hash);
4452 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004453 mutex_unlock(&ftrace_lock);
4454 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004455
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004456 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004457 free_ftrace_hash(iter->hash);
4458 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004459
Steven Rostedt5072c592008-05-12 21:20:43 +02004460 return 0;
4461}
4462
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004463static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004464 .open = ftrace_avail_open,
4465 .read = seq_read,
4466 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004467 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004468};
4469
Steven Rostedt647bcd02011-05-03 14:39:21 -04004470static const struct file_operations ftrace_enabled_fops = {
4471 .open = ftrace_enabled_open,
4472 .read = seq_read,
4473 .llseek = seq_lseek,
4474 .release = seq_release_private,
4475};
4476
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004477static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004478 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004479 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004480 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004481 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004482 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004483};
4484
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004485static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004486 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004487 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004488 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004489 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004490 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004491};
4492
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004493#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4494
4495static DEFINE_MUTEX(graph_lock);
4496
4497int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004498int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004499unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004500unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004501
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004502struct ftrace_graph_data {
4503 unsigned long *table;
4504 size_t size;
4505 int *count;
4506 const struct seq_operations *seq_ops;
4507};
4508
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004509static void *
Li Zefan85951842009-06-24 09:54:00 +08004510__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004511{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004512 struct ftrace_graph_data *fgd = m->private;
4513
4514 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004515 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004516 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004517}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004518
Li Zefan85951842009-06-24 09:54:00 +08004519static void *
4520g_next(struct seq_file *m, void *v, loff_t *pos)
4521{
4522 (*pos)++;
4523 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004524}
4525
4526static void *g_start(struct seq_file *m, loff_t *pos)
4527{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004528 struct ftrace_graph_data *fgd = m->private;
4529
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004530 mutex_lock(&graph_lock);
4531
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004532 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004533 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004534 return (void *)1;
4535
Li Zefan85951842009-06-24 09:54:00 +08004536 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004537}
4538
4539static void g_stop(struct seq_file *m, void *p)
4540{
4541 mutex_unlock(&graph_lock);
4542}
4543
4544static int g_show(struct seq_file *m, void *v)
4545{
4546 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004547
4548 if (!ptr)
4549 return 0;
4550
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004551 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004552 struct ftrace_graph_data *fgd = m->private;
4553
4554 if (fgd->table == ftrace_graph_funcs)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004555 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004556 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004557 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004558 return 0;
4559 }
4560
Steven Rostedtb375a112009-09-17 00:05:58 -04004561 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004562
4563 return 0;
4564}
4565
James Morris88e9d342009-09-22 16:43:43 -07004566static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004567 .start = g_start,
4568 .next = g_next,
4569 .stop = g_stop,
4570 .show = g_show,
4571};
4572
4573static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004574__ftrace_graph_open(struct inode *inode, struct file *file,
4575 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004576{
4577 int ret = 0;
4578
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004579 mutex_lock(&graph_lock);
4580 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004581 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004582 *fgd->count = 0;
4583 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004584 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004585 mutex_unlock(&graph_lock);
4586
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004587 if (file->f_mode & FMODE_READ) {
4588 ret = seq_open(file, fgd->seq_ops);
4589 if (!ret) {
4590 struct seq_file *m = file->private_data;
4591 m->private = fgd;
4592 }
4593 } else
4594 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004595
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004596 return ret;
4597}
4598
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004599static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004600ftrace_graph_open(struct inode *inode, struct file *file)
4601{
4602 struct ftrace_graph_data *fgd;
4603
4604 if (unlikely(ftrace_disabled))
4605 return -ENODEV;
4606
4607 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4608 if (fgd == NULL)
4609 return -ENOMEM;
4610
4611 fgd->table = ftrace_graph_funcs;
4612 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4613 fgd->count = &ftrace_graph_count;
4614 fgd->seq_ops = &ftrace_graph_seq_ops;
4615
4616 return __ftrace_graph_open(inode, file, fgd);
4617}
4618
4619static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004620ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4621{
4622 struct ftrace_graph_data *fgd;
4623
4624 if (unlikely(ftrace_disabled))
4625 return -ENODEV;
4626
4627 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4628 if (fgd == NULL)
4629 return -ENOMEM;
4630
4631 fgd->table = ftrace_graph_notrace_funcs;
4632 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4633 fgd->count = &ftrace_graph_notrace_count;
4634 fgd->seq_ops = &ftrace_graph_seq_ops;
4635
4636 return __ftrace_graph_open(inode, file, fgd);
4637}
4638
4639static int
Li Zefan87827112009-07-23 11:29:11 +08004640ftrace_graph_release(struct inode *inode, struct file *file)
4641{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004642 if (file->f_mode & FMODE_READ) {
4643 struct seq_file *m = file->private_data;
4644
4645 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004646 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004647 } else {
4648 kfree(file->private_data);
4649 }
4650
Li Zefan87827112009-07-23 11:29:11 +08004651 return 0;
4652}
4653
4654static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004655ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004656{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004657 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004658 struct dyn_ftrace *rec;
4659 struct ftrace_page *pg;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004660 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004661 int not;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004662 bool exists;
4663 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004664
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004665 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004666 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4667 &func_g.search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004668 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004669 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004670
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004671 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004672
Steven Rostedt52baf112009-02-14 01:15:39 -05004673 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004674
4675 if (unlikely(ftrace_disabled)) {
4676 mutex_unlock(&ftrace_lock);
4677 return -ENODEV;
4678 }
4679
Steven Rostedt265c8312009-02-13 12:43:56 -05004680 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004681
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004682 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004683 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004684 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004685 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004686 if (array[i] == rec->ip) {
4687 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004688 break;
4689 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004690 }
4691
4692 if (!not) {
4693 fail = 0;
4694 if (!exists) {
4695 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004696 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004697 goto out;
4698 }
4699 } else {
4700 if (exists) {
4701 array[i] = array[--(*idx)];
4702 array[*idx] = 0;
4703 fail = 0;
4704 }
4705 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004706 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004707 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004708out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004709 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004710
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004711 if (fail)
4712 return -EINVAL;
4713
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004714 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004715}
4716
4717static ssize_t
4718ftrace_graph_write(struct file *file, const char __user *ubuf,
4719 size_t cnt, loff_t *ppos)
4720{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004721 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004722 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004723 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004724
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004725 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004726 return 0;
4727
Namhyung Kim6a101082013-10-14 17:24:25 +09004728 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4729 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004730
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004731 read = trace_get_user(&parser, ubuf, cnt, ppos);
4732
Li Zefan4ba79782009-09-22 13:52:20 +08004733 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004734 parser.buffer[parser.idx] = 0;
4735
Namhyung Kim6a101082013-10-14 17:24:25 +09004736 mutex_lock(&graph_lock);
4737
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004738 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004739 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4740 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004741
4742 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004743 }
4744
Namhyung Kim6a101082013-10-14 17:24:25 +09004745 if (!ret)
4746 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004747
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004748 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004749
4750 return ret;
4751}
4752
4753static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004754 .open = ftrace_graph_open,
4755 .read = seq_read,
4756 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004757 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004758 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004759};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004760
4761static const struct file_operations ftrace_graph_notrace_fops = {
4762 .open = ftrace_graph_notrace_open,
4763 .read = seq_read,
4764 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004765 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004766 .release = ftrace_graph_release,
4767};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004768#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4769
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004770void ftrace_create_filter_files(struct ftrace_ops *ops,
4771 struct dentry *parent)
4772{
4773
4774 trace_create_file("set_ftrace_filter", 0644, parent,
4775 ops, &ftrace_filter_fops);
4776
4777 trace_create_file("set_ftrace_notrace", 0644, parent,
4778 ops, &ftrace_notrace_fops);
4779}
4780
4781/*
4782 * The name "destroy_filter_files" is really a misnomer. Although
4783 * in the future, it may actualy delete the files, but this is
4784 * really intended to make sure the ops passed in are disabled
4785 * and that when this function returns, the caller is free to
4786 * free the ops.
4787 *
4788 * The "destroy" name is only to match the "create" name that this
4789 * should be paired with.
4790 */
4791void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4792{
4793 mutex_lock(&ftrace_lock);
4794 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4795 ftrace_shutdown(ops, 0);
4796 ops->flags |= FTRACE_OPS_FL_DELETED;
4797 mutex_unlock(&ftrace_lock);
4798}
4799
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05004800static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004801{
Steven Rostedt5072c592008-05-12 21:20:43 +02004802
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004803 trace_create_file("available_filter_functions", 0444,
4804 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004805
Steven Rostedt647bcd02011-05-03 14:39:21 -04004806 trace_create_file("enabled_functions", 0444,
4807 d_tracer, NULL, &ftrace_enabled_fops);
4808
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004809 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004810
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004811#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004812 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004813 NULL,
4814 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004815 trace_create_file("set_graph_notrace", 0444, d_tracer,
4816 NULL,
4817 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004818#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4819
Steven Rostedt5072c592008-05-12 21:20:43 +02004820 return 0;
4821}
4822
Steven Rostedt9fd49322012-04-24 22:32:06 -04004823static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004824{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004825 const unsigned long *ipa = a;
4826 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004827
Steven Rostedt9fd49322012-04-24 22:32:06 -04004828 if (*ipa > *ipb)
4829 return 1;
4830 if (*ipa < *ipb)
4831 return -1;
4832 return 0;
4833}
4834
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004835static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004836 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004837 unsigned long *end)
4838{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004839 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004840 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004841 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004842 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004843 unsigned long *p;
4844 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004845 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004846 int ret = -ENOMEM;
4847
4848 count = end - start;
4849
4850 if (!count)
4851 return 0;
4852
Steven Rostedt9fd49322012-04-24 22:32:06 -04004853 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02004854 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04004855
Steven Rostedt706c81f2012-04-24 23:45:26 -04004856 start_pg = ftrace_allocate_pages(count);
4857 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004858 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004859
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004860 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004861
Steven Rostedt320823092011-12-16 14:42:37 -05004862 /*
4863 * Core and each module needs their own pages, as
4864 * modules will free them when they are removed.
4865 * Force a new page to be allocated for modules.
4866 */
Steven Rostedta7900872011-12-16 16:23:44 -05004867 if (!mod) {
4868 WARN_ON(ftrace_pages || ftrace_pages_start);
4869 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004870 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004871 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004872 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004873 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004874
Steven Rostedta7900872011-12-16 16:23:44 -05004875 if (WARN_ON(ftrace_pages->next)) {
4876 /* Hmm, we have free pages? */
4877 while (ftrace_pages->next)
4878 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004879 }
Steven Rostedta7900872011-12-16 16:23:44 -05004880
Steven Rostedt706c81f2012-04-24 23:45:26 -04004881 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004882 }
4883
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004884 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004885 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004886 while (p < end) {
4887 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004888 /*
4889 * Some architecture linkers will pad between
4890 * the different mcount_loc sections of different
4891 * object files to satisfy alignments.
4892 * Skip any NULL pointers.
4893 */
4894 if (!addr)
4895 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004896
4897 if (pg->index == pg->size) {
4898 /* We should have allocated enough */
4899 if (WARN_ON(!pg->next))
4900 break;
4901 pg = pg->next;
4902 }
4903
4904 rec = &pg->records[pg->index++];
4905 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004906 }
4907
Steven Rostedt706c81f2012-04-24 23:45:26 -04004908 /* We should have used all pages */
4909 WARN_ON(pg->next);
4910
4911 /* Assign the last page to ftrace_pages */
4912 ftrace_pages = pg;
4913
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004914 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004915 * We only need to disable interrupts on start up
4916 * because we are modifying code that an interrupt
4917 * may execute, and the modification is not atomic.
4918 * But for modules, nothing runs the code we modify
4919 * until we are finished with it, and there's no
4920 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004921 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004922 if (!mod)
4923 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004924 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004925 if (!mod)
4926 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004927 ret = 0;
4928 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004929 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004930
Steven Rostedta7900872011-12-16 16:23:44 -05004931 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004932}
4933
Steven Rostedt93eb6772009-04-15 13:24:06 -04004934#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004935
4936#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4937
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05004938static int referenced_filters(struct dyn_ftrace *rec)
4939{
4940 struct ftrace_ops *ops;
4941 int cnt = 0;
4942
4943 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
4944 if (ops_references_rec(ops, rec))
4945 cnt++;
4946 }
4947
4948 return cnt;
4949}
4950
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004951void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004952{
4953 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004954 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004955 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004956 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004957
Steven Rostedt93eb6772009-04-15 13:24:06 -04004958 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004959
4960 if (ftrace_disabled)
4961 goto out_unlock;
4962
Steven Rostedt320823092011-12-16 14:42:37 -05004963 /*
4964 * Each module has its own ftrace_pages, remove
4965 * them from the list.
4966 */
4967 last_pg = &ftrace_pages_start;
4968 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4969 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004970 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004971 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004972 * As core pages are first, the first
4973 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004974 */
Steven Rostedt320823092011-12-16 14:42:37 -05004975 if (WARN_ON(pg == ftrace_pages_start))
4976 goto out_unlock;
4977
4978 /* Check if we are deleting the last page */
4979 if (pg == ftrace_pages)
4980 ftrace_pages = next_to_ftrace_page(last_pg);
4981
4982 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004983 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4984 free_pages((unsigned long)pg->records, order);
4985 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004986 } else
4987 last_pg = &pg->next;
4988 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004989 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004990 mutex_unlock(&ftrace_lock);
4991}
4992
Jessica Yu7dcd1822016-02-16 17:32:33 -05004993void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05004994{
4995 struct dyn_ftrace *rec;
4996 struct ftrace_page *pg;
4997
4998 mutex_lock(&ftrace_lock);
4999
5000 if (ftrace_disabled)
5001 goto out_unlock;
5002
5003 /*
5004 * If the tracing is enabled, go ahead and enable the record.
5005 *
5006 * The reason not to enable the record immediatelly is the
5007 * inherent check of ftrace_make_nop/ftrace_make_call for
5008 * correct previous instructions. Making first the NOP
5009 * conversion puts the module to the correct state, thus
5010 * passing the ftrace_make_call check.
5011 *
5012 * We also delay this to after the module code already set the
5013 * text to read-only, as we now need to set it back to read-write
5014 * so that we can modify the text.
5015 */
5016 if (ftrace_start_up)
5017 ftrace_arch_code_modify_prepare();
5018
5019 do_for_each_ftrace_rec(pg, rec) {
5020 int cnt;
5021 /*
5022 * do_for_each_ftrace_rec() is a double loop.
5023 * module text shares the pg. If a record is
5024 * not part of this module, then skip this pg,
5025 * which the "break" will do.
5026 */
5027 if (!within_module_core(rec->ip, mod))
5028 break;
5029
5030 cnt = 0;
5031
5032 /*
5033 * When adding a module, we need to check if tracers are
5034 * currently enabled and if they are, and can trace this record,
5035 * we need to enable the module functions as well as update the
5036 * reference counts for those function records.
5037 */
5038 if (ftrace_start_up)
5039 cnt += referenced_filters(rec);
5040
5041 /* This clears FTRACE_FL_DISABLED */
5042 rec->flags = cnt;
5043
5044 if (ftrace_start_up && cnt) {
5045 int failed = __ftrace_replace_code(rec, 1);
5046 if (failed) {
5047 ftrace_bug(failed, rec);
5048 goto out_loop;
5049 }
5050 }
5051
5052 } while_for_each_ftrace_rec();
5053
5054 out_loop:
5055 if (ftrace_start_up)
5056 ftrace_arch_code_modify_post_process();
5057
5058 out_unlock:
5059 mutex_unlock(&ftrace_lock);
5060}
5061
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005062void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005063{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005064 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005065 return;
5066
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005067 ftrace_process_locs(mod, mod->ftrace_callsites,
5068 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005069}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005070#endif /* CONFIG_MODULES */
5071
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005072void __init ftrace_init(void)
5073{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005074 extern unsigned long __start_mcount_loc[];
5075 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005076 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005077 int ret;
5078
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005079 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005080 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005081 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01005082 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005083 goto failed;
5084
5085 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005086 if (!count) {
5087 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005088 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005089 }
5090
5091 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5092 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005093
5094 last_ftrace_enabled = ftrace_enabled = 1;
5095
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005096 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005097 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005098 __stop_mcount_loc);
5099
Steven Rostedt2af15d62009-05-28 13:37:24 -04005100 set_ftrace_early_filters();
5101
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005102 return;
5103 failed:
5104 ftrace_disabled = 1;
5105}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005106
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005107/* Do nothing if arch does not support this */
5108void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5109{
5110}
5111
5112static void ftrace_update_trampoline(struct ftrace_ops *ops)
5113{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005114
5115/*
5116 * Currently there's no safe way to free a trampoline when the kernel
5117 * is configured with PREEMPT. That is because a task could be preempted
5118 * when it jumped to the trampoline, it may be preempted for a long time
5119 * depending on the system load, and currently there's no way to know
5120 * when it will be off the trampoline. If the trampoline is freed
5121 * too early, when the task runs again, it will be executing on freed
5122 * memory and crash.
5123 */
5124#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005125 /* Currently, only non dynamic ops can have a trampoline */
5126 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5127 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005128#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005129
5130 arch_ftrace_update_trampoline(ops);
5131}
5132
Steven Rostedt3d083392008-05-12 21:20:42 +02005133#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005134
Steven Rostedt2b499382011-05-03 22:49:52 -04005135static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005136 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005137 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5138 FTRACE_OPS_FL_INITIALIZED |
5139 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005140};
5141
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005142static int __init ftrace_nodyn_init(void)
5143{
5144 ftrace_enabled = 1;
5145 return 0;
5146}
Steven Rostedt6f415672012-10-05 12:13:07 -04005147core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005148
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005149static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005150static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005151static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005152/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005153# define ftrace_startup(ops, command) \
5154 ({ \
5155 int ___ret = __register_ftrace_function(ops); \
5156 if (!___ret) \
5157 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5158 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005159 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005160# define ftrace_shutdown(ops, command) \
5161 ({ \
5162 int ___ret = __unregister_ftrace_function(ops); \
5163 if (!___ret) \
5164 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5165 ___ret; \
5166 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005167
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005168# define ftrace_startup_sysctl() do { } while (0)
5169# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005170
5171static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005172ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005173{
5174 return 1;
5175}
5176
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005177static void ftrace_update_trampoline(struct ftrace_ops *ops)
5178{
5179}
5180
Steven Rostedt3d083392008-05-12 21:20:42 +02005181#endif /* CONFIG_DYNAMIC_FTRACE */
5182
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005183__init void ftrace_init_global_array_ops(struct trace_array *tr)
5184{
5185 tr->ops = &global_ops;
5186 tr->ops->private = tr;
5187}
5188
5189void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5190{
5191 /* If we filter on pids, update to use the pid function */
5192 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5193 if (WARN_ON(tr->ops->func != ftrace_stub))
5194 printk("ftrace ops had %pS for function\n",
5195 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005196 }
5197 tr->ops->func = func;
5198 tr->ops->private = tr;
5199}
5200
5201void ftrace_reset_array_ops(struct trace_array *tr)
5202{
5203 tr->ops->func = ftrace_stub;
5204}
5205
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005206static inline void
5207__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005208 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005209{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005210 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005211 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005212
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005213 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5214 if (bit < 0)
5215 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005216
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005217 /*
5218 * Some of the ops may be dynamically allocated,
5219 * they must be freed after a synchronize_sched().
5220 */
5221 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005222
Steven Rostedt0a016402012-11-02 17:03:03 -04005223 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005224 /*
5225 * Check the following for each ops before calling their func:
5226 * if RCU flag is set, then rcu_is_watching() must be true
5227 * if PER_CPU is set, then ftrace_function_local_disable()
5228 * must be false
5229 * Otherwise test if the ip matches the ops filter
5230 *
5231 * If any of the above fails then the op->func() is not executed.
5232 */
5233 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5234 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5235 !ftrace_function_local_disabled(op)) &&
5236 ftrace_ops_test(op, ip, regs)) {
5237
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005238 if (FTRACE_WARN_ON(!op->func)) {
5239 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005240 goto out;
5241 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005242 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005243 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005244 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005245out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005246 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005247 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005248}
5249
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005250/*
5251 * Some archs only support passing ip and parent_ip. Even though
5252 * the list function ignores the op parameter, we do not want any
5253 * C side effects, where a function is called without the caller
5254 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005255 * Archs are to support both the regs and ftrace_ops at the same time.
5256 * If they support ftrace_ops, it is assumed they support regs.
5257 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005258 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5259 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005260 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08005261 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005262 */
5263#if ARCH_SUPPORTS_FTRACE_OPS
5264static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005265 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005266{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005267 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005268}
5269#else
5270static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5271{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005272 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005273}
5274#endif
5275
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005276/*
5277 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005278 * recursion, needs RCU protection and/or requires per cpu handling, then
5279 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005280 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005281static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005282 struct ftrace_ops *op, struct pt_regs *regs)
5283{
5284 int bit;
5285
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005286 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5287 return;
5288
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005289 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5290 if (bit < 0)
5291 return;
5292
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005293 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005294
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005295 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5296 !ftrace_function_local_disabled(op)) {
5297 op->func(ip, parent_ip, op, regs);
5298 }
5299
5300 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005301 trace_clear_recursion(bit);
5302}
5303
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005304/**
5305 * ftrace_ops_get_func - get the function a trampoline should call
5306 * @ops: the ops to get the function for
5307 *
5308 * Normally the mcount trampoline will call the ops->func, but there
5309 * are times that it should not. For example, if the ops does not
5310 * have its own recursion protection, then it should call the
5311 * ftrace_ops_recurs_func() instead.
5312 *
5313 * Returns the function that the trampoline should call for @ops.
5314 */
5315ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5316{
5317 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005318 * If the function does not handle recursion, needs to be RCU safe,
5319 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005320 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005321 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5322 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5323 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005324
5325 return ops->func;
5326}
5327
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005328static void
5329ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5330 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05005331{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005332 struct trace_array *tr = data;
5333 struct trace_pid_list *pid_list;
5334
5335 pid_list = rcu_dereference_sched(tr->function_pids);
5336
5337 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5338 trace_ignore_this_task(pid_list, next));
5339}
5340
5341static void clear_ftrace_pids(struct trace_array *tr)
5342{
5343 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05005344 int cpu;
5345
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005346 pid_list = rcu_dereference_protected(tr->function_pids,
5347 lockdep_is_held(&ftrace_lock));
5348 if (!pid_list)
5349 return;
5350
5351 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5352
5353 for_each_possible_cpu(cpu)
5354 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5355
5356 rcu_assign_pointer(tr->function_pids, NULL);
5357
5358 /* Wait till all users are no longer using pid filtering */
5359 synchronize_sched();
5360
5361 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05005362}
5363
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005364static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05005365{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005366 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005367 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005368
5369 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005370 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005371
5372 mutex_unlock(&ftrace_lock);
5373}
5374
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005375/* Greater than any max PID */
5376#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5377
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005378static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005379 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005380{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005381 struct trace_pid_list *pid_list;
5382 struct trace_array *tr = m->private;
5383
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005384 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005385 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005386
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005387 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005388
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005389 if (!pid_list)
5390 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5391
5392 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005393}
5394
5395static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5396{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005397 struct trace_array *tr = m->private;
5398 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5399
5400 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005401 return NULL;
5402
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005403 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005404}
5405
5406static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005407 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005408{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005409 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005410 mutex_unlock(&ftrace_lock);
5411}
5412
5413static int fpid_show(struct seq_file *m, void *v)
5414{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005415 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005416 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005417 return 0;
5418 }
5419
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005420 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005421}
5422
5423static const struct seq_operations ftrace_pid_sops = {
5424 .start = fpid_start,
5425 .next = fpid_next,
5426 .stop = fpid_stop,
5427 .show = fpid_show,
5428};
5429
5430static int
5431ftrace_pid_open(struct inode *inode, struct file *file)
5432{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005433 struct trace_array *tr = inode->i_private;
5434 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005435 int ret = 0;
5436
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005437 if (trace_array_get(tr) < 0)
5438 return -ENODEV;
5439
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005440 if ((file->f_mode & FMODE_WRITE) &&
5441 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005442 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005443
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005444 ret = seq_open(file, &ftrace_pid_sops);
5445 if (ret < 0) {
5446 trace_array_put(tr);
5447 } else {
5448 m = file->private_data;
5449 /* copy tr over to seq ops */
5450 m->private = tr;
5451 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005452
5453 return ret;
5454}
5455
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005456static void ignore_task_cpu(void *data)
5457{
5458 struct trace_array *tr = data;
5459 struct trace_pid_list *pid_list;
5460
5461 /*
5462 * This function is called by on_each_cpu() while the
5463 * event_mutex is held.
5464 */
5465 pid_list = rcu_dereference_protected(tr->function_pids,
5466 mutex_is_locked(&ftrace_lock));
5467
5468 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5469 trace_ignore_this_task(pid_list, current));
5470}
5471
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005472static ssize_t
5473ftrace_pid_write(struct file *filp, const char __user *ubuf,
5474 size_t cnt, loff_t *ppos)
5475{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005476 struct seq_file *m = filp->private_data;
5477 struct trace_array *tr = m->private;
5478 struct trace_pid_list *filtered_pids = NULL;
5479 struct trace_pid_list *pid_list;
5480 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005481
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005482 if (!cnt)
5483 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005484
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005485 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005486
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005487 filtered_pids = rcu_dereference_protected(tr->function_pids,
5488 lockdep_is_held(&ftrace_lock));
5489
5490 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5491 if (ret < 0)
5492 goto out;
5493
5494 rcu_assign_pointer(tr->function_pids, pid_list);
5495
5496 if (filtered_pids) {
5497 synchronize_sched();
5498 trace_free_pid_list(filtered_pids);
5499 } else if (pid_list) {
5500 /* Register a probe to set whether to ignore the tracing of a task */
5501 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5502 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005503
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005504 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005505 * Ignoring of pids is done at task switch. But we have to
5506 * check for those tasks that are currently running.
5507 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005508 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005509 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005510
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005511 ftrace_update_pid_func();
5512 ftrace_startup_all(0);
5513 out:
5514 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005515
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005516 if (ret > 0)
5517 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05005518
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005519 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005520}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005521
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005522static int
5523ftrace_pid_release(struct inode *inode, struct file *file)
5524{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005525 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005526
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005527 trace_array_put(tr);
5528
5529 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005530}
5531
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005532static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005533 .open = ftrace_pid_open,
5534 .write = ftrace_pid_write,
5535 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005536 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005537 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005538};
5539
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005540void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005541{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005542 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005543 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005544}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005545
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04005546void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5547 struct dentry *d_tracer)
5548{
5549 /* Only the top level directory has the dyn_tracefs and profile */
5550 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5551
5552 ftrace_init_dyn_tracefs(d_tracer);
5553 ftrace_profile_tracefs(d_tracer);
5554}
5555
Steven Rostedt3d083392008-05-12 21:20:42 +02005556/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005557 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005558 *
5559 * This function should be used by panic code. It stops ftrace
5560 * but in a not so nice way. If you need to simply kill ftrace
5561 * from a non-atomic section, use ftrace_kill.
5562 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005563void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005564{
5565 ftrace_disabled = 1;
5566 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005567 clear_ftrace_function();
5568}
5569
5570/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005571 * Test if ftrace is dead or not.
5572 */
5573int ftrace_is_dead(void)
5574{
5575 return ftrace_disabled;
5576}
5577
5578/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005579 * register_ftrace_function - register a function for profiling
5580 * @ops - ops structure that holds the function for profiling.
5581 *
5582 * Register a function to be called by all functions in the
5583 * kernel.
5584 *
5585 * Note: @ops->func and all the functions it calls must be labeled
5586 * with "notrace", otherwise it will go into a
5587 * recursive loop.
5588 */
5589int register_ftrace_function(struct ftrace_ops *ops)
5590{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005591 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005592
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005593 ftrace_ops_init(ops);
5594
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005595 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005596
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005597 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005598
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005599 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005600
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005601 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005602}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005603EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005604
5605/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005606 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005607 * @ops - ops structure that holds the function to unregister
5608 *
5609 * Unregister a function that was added to be called by ftrace profiling.
5610 */
5611int unregister_ftrace_function(struct ftrace_ops *ops)
5612{
5613 int ret;
5614
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005615 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005616 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005617 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005618
5619 return ret;
5620}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005621EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005622
Ingo Molnare309b412008-05-12 21:20:51 +02005623int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005624ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005625 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005626 loff_t *ppos)
5627{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005628 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005629
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005630 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005631
Steven Rostedt45a4a232011-04-21 23:16:46 -04005632 if (unlikely(ftrace_disabled))
5633 goto out;
5634
5635 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005636
Li Zefana32c7762009-06-26 16:55:51 +08005637 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005638 goto out;
5639
Li Zefana32c7762009-06-26 16:55:51 +08005640 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005641
5642 if (ftrace_enabled) {
5643
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005644 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005645 if (ftrace_ops_list != &ftrace_list_end)
5646 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005647
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05005648 ftrace_startup_sysctl();
5649
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005650 } else {
5651 /* stopping ftrace calls (just send to ftrace_stub) */
5652 ftrace_trace_function = ftrace_stub;
5653
5654 ftrace_shutdown_sysctl();
5655 }
5656
5657 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005658 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005659 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005660}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005661
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005662#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005663
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005664static struct ftrace_ops graph_ops = {
5665 .func = ftrace_stub,
5666 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5667 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005668 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005669 FTRACE_OPS_FL_STUB,
5670#ifdef FTRACE_GRAPH_TRAMP_ADDR
5671 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05005672 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005673#endif
5674 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5675};
5676
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005677void ftrace_graph_sleep_time_control(bool enable)
5678{
5679 fgraph_sleep_time = enable;
5680}
5681
5682void ftrace_graph_graph_time_control(bool enable)
5683{
5684 fgraph_graph_time = enable;
5685}
5686
Steven Rostedte49dc192008-12-02 23:50:05 -05005687int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5688{
5689 return 0;
5690}
5691
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005692/* The callbacks that hook a function */
5693trace_func_graph_ret_t ftrace_graph_return =
5694 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005695trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005696static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005697
5698/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5699static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5700{
5701 int i;
5702 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005703 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5704 struct task_struct *g, *t;
5705
5706 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5707 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5708 * sizeof(struct ftrace_ret_stack),
5709 GFP_KERNEL);
5710 if (!ret_stack_list[i]) {
5711 start = 0;
5712 end = i;
5713 ret = -ENOMEM;
5714 goto free;
5715 }
5716 }
5717
Soumya PN6112a302016-05-17 21:31:14 +05305718 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005719 do_each_thread(g, t) {
5720 if (start == end) {
5721 ret = -EAGAIN;
5722 goto unlock;
5723 }
5724
5725 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005726 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005727 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005728 t->curr_ret_stack = -1;
5729 /* Make sure the tasks see the -1 first: */
5730 smp_wmb();
5731 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005732 }
5733 } while_each_thread(g, t);
5734
5735unlock:
Soumya PN6112a302016-05-17 21:31:14 +05305736 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005737free:
5738 for (i = start; i < end; i++)
5739 kfree(ret_stack_list[i]);
5740 return ret;
5741}
5742
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005743static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02005744ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04005745 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005746{
5747 unsigned long long timestamp;
5748 int index;
5749
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005750 /*
5751 * Does the user want to count the time a function was asleep.
5752 * If so, do not update the time stamps.
5753 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005754 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005755 return;
5756
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005757 timestamp = trace_clock_local();
5758
5759 prev->ftrace_timestamp = timestamp;
5760
5761 /* only process tasks that we timestamped */
5762 if (!next->ftrace_timestamp)
5763 return;
5764
5765 /*
5766 * Update all the counters in next to make up for the
5767 * time next was sleeping.
5768 */
5769 timestamp -= next->ftrace_timestamp;
5770
5771 for (index = next->curr_ret_stack; index >= 0; index--)
5772 next->ret_stack[index].calltime += timestamp;
5773}
5774
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005775/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005776static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005777{
5778 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005779 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005780
5781 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5782 sizeof(struct ftrace_ret_stack *),
5783 GFP_KERNEL);
5784
5785 if (!ret_stack_list)
5786 return -ENOMEM;
5787
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005788 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005789 for_each_online_cpu(cpu) {
5790 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005791 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005792 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005793
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005794 do {
5795 ret = alloc_retstack_tasklist(ret_stack_list);
5796 } while (ret == -EAGAIN);
5797
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005798 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005799 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005800 if (ret)
5801 pr_info("ftrace_graph: Couldn't activate tracepoint"
5802 " probe to kernel_sched_switch\n");
5803 }
5804
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005805 kfree(ret_stack_list);
5806 return ret;
5807}
5808
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005809/*
5810 * Hibernation protection.
5811 * The state of the current task is too much unstable during
5812 * suspend/restore to disk. We want to protect against that.
5813 */
5814static int
5815ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5816 void *unused)
5817{
5818 switch (state) {
5819 case PM_HIBERNATION_PREPARE:
5820 pause_graph_tracing();
5821 break;
5822
5823 case PM_POST_HIBERNATION:
5824 unpause_graph_tracing();
5825 break;
5826 }
5827 return NOTIFY_DONE;
5828}
5829
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005830static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5831{
5832 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5833 return 0;
5834 return __ftrace_graph_entry(trace);
5835}
5836
5837/*
5838 * The function graph tracer should only trace the functions defined
5839 * by set_ftrace_filter and set_ftrace_notrace. If another function
5840 * tracer ops is registered, the graph tracer requires testing the
5841 * function against the global ops, and not just trace any function
5842 * that any ftrace_ops registered.
5843 */
5844static void update_function_graph_func(void)
5845{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005846 struct ftrace_ops *op;
5847 bool do_test = false;
5848
5849 /*
5850 * The graph and global ops share the same set of functions
5851 * to test. If any other ops is on the list, then
5852 * the graph tracing needs to test if its the function
5853 * it should call.
5854 */
5855 do_for_each_ftrace_op(op, ftrace_ops_list) {
5856 if (op != &global_ops && op != &graph_ops &&
5857 op != &ftrace_list_end) {
5858 do_test = true;
5859 /* in double loop, break out with goto */
5860 goto out;
5861 }
5862 } while_for_each_ftrace_op(op);
5863 out:
5864 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005865 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005866 else
5867 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005868}
5869
Mathias Krause8275f692014-03-30 15:31:50 +02005870static struct notifier_block ftrace_suspend_notifier = {
5871 .notifier_call = ftrace_suspend_notifier_call,
5872};
5873
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005874int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5875 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005876{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005877 int ret = 0;
5878
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005879 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005880
Steven Rostedt05ce5812009-03-24 00:18:31 -04005881 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005882 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005883 ret = -EBUSY;
5884 goto out;
5885 }
5886
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005887 register_pm_notifier(&ftrace_suspend_notifier);
5888
Steven Rostedt597af812009-04-03 15:24:12 -04005889 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005890 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005891 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005892 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005893 goto out;
5894 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005895
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005896 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005897
5898 /*
5899 * Update the indirect function to the entryfunc, and the
5900 * function that gets called to the entry_test first. Then
5901 * call the update fgraph entry function to determine if
5902 * the entryfunc should be called directly or not.
5903 */
5904 __ftrace_graph_entry = entryfunc;
5905 ftrace_graph_entry = ftrace_graph_entry_test;
5906 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005907
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005908 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005909out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005910 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005911 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005912}
5913
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005914void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005915{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005916 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005917
Steven Rostedt597af812009-04-03 15:24:12 -04005918 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005919 goto out;
5920
Steven Rostedt597af812009-04-03 15:24:12 -04005921 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005922 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005923 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005924 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005925 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005926 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005927 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005928
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005929#ifdef CONFIG_DYNAMIC_FTRACE
5930 /*
5931 * Function graph does not allocate the trampoline, but
5932 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5933 * if one was used.
5934 */
5935 global_ops.trampoline = save_global_trampoline;
5936 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5937 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5938#endif
5939
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005940 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005941 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005942}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005943
Steven Rostedt868baf02011-02-10 21:26:13 -05005944static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5945
5946static void
5947graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5948{
5949 atomic_set(&t->tracing_graph_pause, 0);
5950 atomic_set(&t->trace_overrun, 0);
5951 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005952 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005953 smp_wmb();
5954 t->ret_stack = ret_stack;
5955}
5956
5957/*
5958 * Allocate a return stack for the idle task. May be the first
5959 * time through, or it may be done by CPU hotplug online.
5960 */
5961void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5962{
5963 t->curr_ret_stack = -1;
5964 /*
5965 * The idle task has no parent, it either has its own
5966 * stack or no stack at all.
5967 */
5968 if (t->ret_stack)
5969 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5970
5971 if (ftrace_graph_active) {
5972 struct ftrace_ret_stack *ret_stack;
5973
5974 ret_stack = per_cpu(idle_ret_stack, cpu);
5975 if (!ret_stack) {
5976 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5977 * sizeof(struct ftrace_ret_stack),
5978 GFP_KERNEL);
5979 if (!ret_stack)
5980 return;
5981 per_cpu(idle_ret_stack, cpu) = ret_stack;
5982 }
5983 graph_init_task(t, ret_stack);
5984 }
5985}
5986
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005987/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005988void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005989{
Steven Rostedt84047e32009-06-02 16:51:55 -04005990 /* Make sure we do not use the parent ret_stack */
5991 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005992 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005993
Steven Rostedt597af812009-04-03 15:24:12 -04005994 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005995 struct ftrace_ret_stack *ret_stack;
5996
5997 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005998 * sizeof(struct ftrace_ret_stack),
5999 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04006000 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006001 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05006002 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04006003 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006004}
6005
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006006void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006007{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006008 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6009
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006010 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006011 /* NULL must become visible to IRQs before we free it: */
6012 barrier();
6013
6014 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006015}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006016#endif