blob: 37b0e948d9242ab5fce4536dfa72e058a6e52ac9 [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{
Namhyung Kim8861dd32016-08-31 11:55:29 +0900875 int index = trace->depth;
876
Steven Rostedta1e2e312011-08-09 12:50:46 -0400877 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900878
879 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
880 current->ret_stack[index].subtime = 0;
881
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400882 return 1;
883}
884
885static void profile_graph_return(struct ftrace_graph_ret *trace)
886{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400887 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400888 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400889 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400890 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400891
892 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500893 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400894 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400895 goto out;
896
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400897 /* If the calltime was zero'd ignore it */
898 if (!trace->calltime)
899 goto out;
900
Steven Rostedta2a16d62009-03-24 23:17:58 -0400901 calltime = trace->rettime - trace->calltime;
902
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400903 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400904 int index;
905
906 index = trace->depth;
907
908 /* Append this call time to the parent time to subtract */
909 if (index)
910 current->ret_stack[index - 1].subtime += calltime;
911
912 if (current->ret_stack[index].subtime < calltime)
913 calltime -= current->ret_stack[index].subtime;
914 else
915 calltime = 0;
916 }
917
Steven Rostedtcafb1682009-03-24 20:50:39 -0400918 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400919 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400920 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400921 rec->time_squared += calltime * calltime;
922 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400923
Steven Rostedtcafb1682009-03-24 20:50:39 -0400924 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400925 local_irq_restore(flags);
926}
927
928static int register_ftrace_profiler(void)
929{
930 return register_ftrace_graph(&profile_graph_return,
931 &profile_graph_entry);
932}
933
934static void unregister_ftrace_profiler(void)
935{
936 unregister_ftrace_graph();
937}
938#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100939static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400940 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900941 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400942 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400943};
944
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400945static int register_ftrace_profiler(void)
946{
947 return register_ftrace_function(&ftrace_profile_ops);
948}
949
950static void unregister_ftrace_profiler(void)
951{
952 unregister_ftrace_function(&ftrace_profile_ops);
953}
954#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
955
Steven Rostedt493762f2009-03-23 17:12:36 -0400956static ssize_t
957ftrace_profile_write(struct file *filp, const char __user *ubuf,
958 size_t cnt, loff_t *ppos)
959{
960 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400961 int ret;
962
Peter Huewe22fe9b52011-06-07 21:58:27 +0200963 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
964 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400965 return ret;
966
967 val = !!val;
968
969 mutex_lock(&ftrace_profile_lock);
970 if (ftrace_profile_enabled ^ val) {
971 if (val) {
972 ret = ftrace_profile_init();
973 if (ret < 0) {
974 cnt = ret;
975 goto out;
976 }
977
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400978 ret = register_ftrace_profiler();
979 if (ret < 0) {
980 cnt = ret;
981 goto out;
982 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400983 ftrace_profile_enabled = 1;
984 } else {
985 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400986 /*
987 * unregister_ftrace_profiler calls stop_machine
988 * so this acts like an synchronize_sched.
989 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400990 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400991 }
992 }
993 out:
994 mutex_unlock(&ftrace_profile_lock);
995
Jiri Olsacf8517c2009-10-23 19:36:16 -0400996 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400997
998 return cnt;
999}
1000
1001static ssize_t
1002ftrace_profile_read(struct file *filp, char __user *ubuf,
1003 size_t cnt, loff_t *ppos)
1004{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001005 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001006 int r;
1007
1008 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1009 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1010}
1011
1012static const struct file_operations ftrace_profile_fops = {
1013 .open = tracing_open_generic,
1014 .read = ftrace_profile_read,
1015 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001016 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001017};
1018
Steven Rostedtcafb1682009-03-24 20:50:39 -04001019/* used to initialize the real stat files */
1020static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001021 .name = "functions",
1022 .stat_start = function_stat_start,
1023 .stat_next = function_stat_next,
1024 .stat_cmp = function_stat_cmp,
1025 .stat_headers = function_stat_headers,
1026 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001027};
1028
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001029static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001030{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001031 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001032 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001033 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001034 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001035 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001036
Steven Rostedtcafb1682009-03-24 20:50:39 -04001037 for_each_possible_cpu(cpu) {
1038 stat = &per_cpu(ftrace_profile_stats, cpu);
1039
Geliang Tang6363c6b2016-03-15 22:12:34 +08001040 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -04001041 if (!name) {
1042 /*
1043 * The files created are permanent, if something happens
1044 * we still do not free memory.
1045 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001046 WARN(1,
1047 "Could not allocate stat file for cpu %d\n",
1048 cpu);
1049 return;
1050 }
1051 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001052 stat->stat.name = name;
1053 ret = register_stat_tracer(&stat->stat);
1054 if (ret) {
1055 WARN(1,
1056 "Could not register function stat for cpu %d\n",
1057 cpu);
1058 kfree(name);
1059 return;
1060 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001061 }
1062
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001063 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001064 d_tracer, NULL, &ftrace_profile_fops);
1065 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001066 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001067}
1068
1069#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001070static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001071{
1072}
1073#endif /* CONFIG_FUNCTION_PROFILER */
1074
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001075static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1076
Pratyush Anand1619dc32015-03-06 23:58:06 +05301077#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1078static int ftrace_graph_active;
1079#else
1080# define ftrace_graph_active 0
1081#endif
1082
Steven Rostedt3d083392008-05-12 21:20:42 +02001083#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001084
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001085static struct ftrace_ops *removed_ops;
1086
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001087/*
1088 * Set when doing a global update, like enabling all recs or disabling them.
1089 * It is not set when just updating a single ftrace_ops.
1090 */
1091static bool update_all_ops;
1092
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001093#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001094# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001095#endif
1096
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001097static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1098
Steven Rostedtb6887d72009-02-17 12:32:04 -05001099struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001100 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001101 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001102 unsigned long flags;
1103 unsigned long ip;
1104 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001105 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001106};
1107
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001108struct ftrace_func_entry {
1109 struct hlist_node hlist;
1110 unsigned long ip;
1111};
1112
1113struct ftrace_hash {
1114 unsigned long size_bits;
1115 struct hlist_head *buckets;
1116 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001117 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001118};
1119
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001120/*
1121 * We make these constant because no one should touch them,
1122 * but they are used as the default "empty hash", to avoid allocating
1123 * it all the time. These are in a read only section such that if
1124 * anyone does try to modify it, it will cause an exception.
1125 */
1126static const struct hlist_head empty_buckets[1];
1127static const struct ftrace_hash empty_hash = {
1128 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001129};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001130#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001131
Steven Rostedt2b499382011-05-03 22:49:52 -04001132static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001133 .func = ftrace_stub,
1134 .local_hash.notrace_hash = EMPTY_HASH,
1135 .local_hash.filter_hash = EMPTY_HASH,
1136 INIT_OPS_HASH(global_ops)
1137 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001138 FTRACE_OPS_FL_INITIALIZED |
1139 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001140};
1141
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001142/*
1143 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001144 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001145 * not return true for either core_kernel_text() or
1146 * is_module_text_address().
1147 */
1148bool is_ftrace_trampoline(unsigned long addr)
1149{
1150 struct ftrace_ops *op;
1151 bool ret = false;
1152
1153 /*
1154 * Some of the ops may be dynamically allocated,
1155 * they are freed after a synchronize_sched().
1156 */
1157 preempt_disable_notrace();
1158
1159 do_for_each_ftrace_op(op, ftrace_ops_list) {
1160 /*
1161 * This is to check for dynamically allocated trampolines.
1162 * Trampolines that are in kernel text will have
1163 * core_kernel_text() return true.
1164 */
1165 if (op->trampoline && op->trampoline_size)
1166 if (addr >= op->trampoline &&
1167 addr < op->trampoline + op->trampoline_size) {
1168 ret = true;
1169 goto out;
1170 }
1171 } while_for_each_ftrace_op(op);
1172
1173 out:
1174 preempt_enable_notrace();
1175
1176 return ret;
1177}
1178
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001179struct ftrace_page {
1180 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001181 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001182 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001183 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001184};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001185
Steven Rostedta7900872011-12-16 16:23:44 -05001186#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1187#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001188
1189/* estimate from running different kernels */
1190#define NR_TO_INIT 10000
1191
1192static struct ftrace_page *ftrace_pages_start;
1193static struct ftrace_page *ftrace_pages;
1194
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001195static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001196{
1197 return !hash || !hash->count;
1198}
1199
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001200static struct ftrace_func_entry *
1201ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1202{
1203 unsigned long key;
1204 struct ftrace_func_entry *entry;
1205 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001206
Steven Rostedt06a51d92011-12-19 19:07:36 -05001207 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001208 return NULL;
1209
1210 if (hash->size_bits > 0)
1211 key = hash_long(ip, hash->size_bits);
1212 else
1213 key = 0;
1214
1215 hhd = &hash->buckets[key];
1216
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001217 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001218 if (entry->ip == ip)
1219 return entry;
1220 }
1221 return NULL;
1222}
1223
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001224static void __add_hash_entry(struct ftrace_hash *hash,
1225 struct ftrace_func_entry *entry)
1226{
1227 struct hlist_head *hhd;
1228 unsigned long key;
1229
1230 if (hash->size_bits)
1231 key = hash_long(entry->ip, hash->size_bits);
1232 else
1233 key = 0;
1234
1235 hhd = &hash->buckets[key];
1236 hlist_add_head(&entry->hlist, hhd);
1237 hash->count++;
1238}
1239
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001240static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1241{
1242 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001243
1244 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1245 if (!entry)
1246 return -ENOMEM;
1247
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001248 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001249 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001250
1251 return 0;
1252}
1253
1254static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001255free_hash_entry(struct ftrace_hash *hash,
1256 struct ftrace_func_entry *entry)
1257{
1258 hlist_del(&entry->hlist);
1259 kfree(entry);
1260 hash->count--;
1261}
1262
1263static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001264remove_hash_entry(struct ftrace_hash *hash,
1265 struct ftrace_func_entry *entry)
1266{
1267 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001268 hash->count--;
1269}
1270
1271static void ftrace_hash_clear(struct ftrace_hash *hash)
1272{
1273 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001274 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001275 struct ftrace_func_entry *entry;
1276 int size = 1 << hash->size_bits;
1277 int i;
1278
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001279 if (!hash->count)
1280 return;
1281
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001282 for (i = 0; i < size; i++) {
1283 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001284 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001285 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001286 }
1287 FTRACE_WARN_ON(hash->count);
1288}
1289
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001290static void free_ftrace_hash(struct ftrace_hash *hash)
1291{
1292 if (!hash || hash == EMPTY_HASH)
1293 return;
1294 ftrace_hash_clear(hash);
1295 kfree(hash->buckets);
1296 kfree(hash);
1297}
1298
Steven Rostedt07fd5512011-05-05 18:03:47 -04001299static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1300{
1301 struct ftrace_hash *hash;
1302
1303 hash = container_of(rcu, struct ftrace_hash, rcu);
1304 free_ftrace_hash(hash);
1305}
1306
1307static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1308{
1309 if (!hash || hash == EMPTY_HASH)
1310 return;
1311 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1312}
1313
Jiri Olsa5500fa52012-02-15 15:51:54 +01001314void ftrace_free_filter(struct ftrace_ops *ops)
1315{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001316 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001317 free_ftrace_hash(ops->func_hash->filter_hash);
1318 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001319}
1320
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001321static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1322{
1323 struct ftrace_hash *hash;
1324 int size;
1325
1326 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1327 if (!hash)
1328 return NULL;
1329
1330 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001331 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001332
1333 if (!hash->buckets) {
1334 kfree(hash);
1335 return NULL;
1336 }
1337
1338 hash->size_bits = size_bits;
1339
1340 return hash;
1341}
1342
1343static struct ftrace_hash *
1344alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1345{
1346 struct ftrace_func_entry *entry;
1347 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001348 int size;
1349 int ret;
1350 int i;
1351
1352 new_hash = alloc_ftrace_hash(size_bits);
1353 if (!new_hash)
1354 return NULL;
1355
1356 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001357 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001358 return new_hash;
1359
1360 size = 1 << hash->size_bits;
1361 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001362 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001363 ret = add_hash_entry(new_hash, entry->ip);
1364 if (ret < 0)
1365 goto free_hash;
1366 }
1367 }
1368
1369 FTRACE_WARN_ON(new_hash->count != hash->count);
1370
1371 return new_hash;
1372
1373 free_hash:
1374 free_ftrace_hash(new_hash);
1375 return NULL;
1376}
1377
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001378static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001379ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001380static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001381ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001382
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001383static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1384 struct ftrace_hash *new_hash);
1385
Namhyung Kim3e278c02017-01-20 11:44:45 +09001386static struct ftrace_hash *
1387__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001388{
1389 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001390 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001391 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001392 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001393 int size = src->count;
1394 int bits = 0;
1395 int i;
1396
1397 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001398 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399 */
Namhyung Kim3e278c02017-01-20 11:44:45 +09001400 if (!src->count)
1401 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001402
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001403 /*
1404 * Make the hash size about 1/2 the # found
1405 */
1406 for (size /= 2; size; size >>= 1)
1407 bits++;
1408
1409 /* Don't allocate too much */
1410 if (bits > FTRACE_HASH_MAX_BITS)
1411 bits = FTRACE_HASH_MAX_BITS;
1412
Steven Rostedt07fd5512011-05-05 18:03:47 -04001413 new_hash = alloc_ftrace_hash(bits);
1414 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001415 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001416
1417 size = 1 << src->size_bits;
1418 for (i = 0; i < size; i++) {
1419 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001420 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001421 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001422 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001423 }
1424 }
1425
Namhyung Kim3e278c02017-01-20 11:44:45 +09001426 return new_hash;
1427}
1428
1429static int
1430ftrace_hash_move(struct ftrace_ops *ops, int enable,
1431 struct ftrace_hash **dst, struct ftrace_hash *src)
1432{
1433 struct ftrace_hash *new_hash;
1434 int ret;
1435
1436 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1437 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1438 return -EINVAL;
1439
1440 new_hash = __ftrace_hash_move(src);
1441 if (!new_hash)
1442 return -ENOMEM;
1443
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001444 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1445 if (enable) {
1446 /* IPMODIFY should be updated only when filter_hash updating */
1447 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1448 if (ret < 0) {
1449 free_ftrace_hash(new_hash);
1450 return ret;
1451 }
1452 }
1453
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001454 /*
1455 * Remove the current set, update the hash and add
1456 * them back.
1457 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001458 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001459
Steven Rostedt07fd5512011-05-05 18:03:47 -04001460 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001461
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001462 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001463
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001464 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001465}
1466
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001467static bool hash_contains_ip(unsigned long ip,
1468 struct ftrace_ops_hash *hash)
1469{
1470 /*
1471 * The function record is a match if it exists in the filter
1472 * hash and not in the notrace hash. Note, an emty hash is
1473 * considered a match for the filter hash, but an empty
1474 * notrace hash is considered not in the notrace hash.
1475 */
1476 return (ftrace_hash_empty(hash->filter_hash) ||
1477 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1478 (ftrace_hash_empty(hash->notrace_hash) ||
1479 !ftrace_lookup_ip(hash->notrace_hash, ip));
1480}
1481
Steven Rostedt265c8312009-02-13 12:43:56 -05001482/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001483 * Test the hashes for this ops to see if we want to call
1484 * the ops->func or not.
1485 *
1486 * It's a match if the ip is in the ops->filter_hash or
1487 * the filter_hash does not exist or is empty,
1488 * AND
1489 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001490 *
1491 * This needs to be called with preemption disabled as
1492 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001493 */
1494static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001495ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001496{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001497 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001498 int ret;
1499
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001500#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1501 /*
1502 * There's a small race when adding ops that the ftrace handler
1503 * that wants regs, may be called without them. We can not
1504 * allow that handler to be called if regs is NULL.
1505 */
1506 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1507 return 0;
1508#endif
1509
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001510 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1511 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001512
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001513 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001514 ret = 1;
1515 else
1516 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001517
1518 return ret;
1519}
1520
1521/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001522 * This is a double for. Do not use 'break' to break out of the loop,
1523 * you must use a goto.
1524 */
1525#define do_for_each_ftrace_rec(pg, rec) \
1526 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1527 int _____i; \
1528 for (_____i = 0; _____i < pg->index; _____i++) { \
1529 rec = &pg->records[_____i];
1530
1531#define while_for_each_ftrace_rec() \
1532 } \
1533 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301534
Steven Rostedt5855fea2011-12-16 19:27:42 -05001535
1536static int ftrace_cmp_recs(const void *a, const void *b)
1537{
Steven Rostedta650e022012-04-25 13:48:13 -04001538 const struct dyn_ftrace *key = a;
1539 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001540
Steven Rostedta650e022012-04-25 13:48:13 -04001541 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001542 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001543 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1544 return 1;
1545 return 0;
1546}
1547
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001548/**
1549 * ftrace_location_range - return the first address of a traced location
1550 * if it touches the given ip range
1551 * @start: start of range to search.
1552 * @end: end of range to search (inclusive). @end points to the last byte
1553 * to check.
1554 *
1555 * Returns rec->ip if the related ftrace location is a least partly within
1556 * the given address range. That is, the first address of the instruction
1557 * that is either a NOP or call to the function tracer. It checks the ftrace
1558 * internal tables to determine if the address belongs or not.
1559 */
1560unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001561{
1562 struct ftrace_page *pg;
1563 struct dyn_ftrace *rec;
1564 struct dyn_ftrace key;
1565
1566 key.ip = start;
1567 key.flags = end; /* overload flags, as it is unsigned long */
1568
1569 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1570 if (end < pg->records[0].ip ||
1571 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1572 continue;
1573 rec = bsearch(&key, pg->records, pg->index,
1574 sizeof(struct dyn_ftrace),
1575 ftrace_cmp_recs);
1576 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001577 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001578 }
1579
Steven Rostedt5855fea2011-12-16 19:27:42 -05001580 return 0;
1581}
1582
Steven Rostedtc88fd862011-08-16 09:53:39 -04001583/**
1584 * ftrace_location - return true if the ip giving is a traced location
1585 * @ip: the instruction pointer to check
1586 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001587 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001588 * That is, the instruction that is either a NOP or call to
1589 * the function tracer. It checks the ftrace internal tables to
1590 * determine if the address belongs or not.
1591 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001592unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001593{
Steven Rostedta650e022012-04-25 13:48:13 -04001594 return ftrace_location_range(ip, ip);
1595}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001596
Steven Rostedta650e022012-04-25 13:48:13 -04001597/**
1598 * ftrace_text_reserved - return true if range contains an ftrace location
1599 * @start: start of range to search
1600 * @end: end of range to search (inclusive). @end points to the last byte to check.
1601 *
1602 * Returns 1 if @start and @end contains a ftrace location.
1603 * That is, the instruction that is either a NOP or call to
1604 * the function tracer. It checks the ftrace internal tables to
1605 * determine if the address belongs or not.
1606 */
Sasha Levind88471c2013-01-09 18:09:20 -05001607int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001608{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001609 unsigned long ret;
1610
1611 ret = ftrace_location_range((unsigned long)start,
1612 (unsigned long)end);
1613
1614 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001615}
1616
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001617/* Test if ops registered to this rec needs regs */
1618static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1619{
1620 struct ftrace_ops *ops;
1621 bool keep_regs = false;
1622
1623 for (ops = ftrace_ops_list;
1624 ops != &ftrace_list_end; ops = ops->next) {
1625 /* pass rec in as regs to have non-NULL val */
1626 if (ftrace_ops_test(ops, rec->ip, rec)) {
1627 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1628 keep_regs = true;
1629 break;
1630 }
1631 }
1632 }
1633
1634 return keep_regs;
1635}
1636
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001637static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001638 int filter_hash,
1639 bool inc)
1640{
1641 struct ftrace_hash *hash;
1642 struct ftrace_hash *other_hash;
1643 struct ftrace_page *pg;
1644 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001645 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001646 int count = 0;
1647 int all = 0;
1648
1649 /* Only update if the ops has been registered */
1650 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001651 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001652
1653 /*
1654 * In the filter_hash case:
1655 * If the count is zero, we update all records.
1656 * Otherwise we just update the items in the hash.
1657 *
1658 * In the notrace_hash case:
1659 * We enable the update in the hash.
1660 * As disabling notrace means enabling the tracing,
1661 * and enabling notrace means disabling, the inc variable
1662 * gets inversed.
1663 */
1664 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001665 hash = ops->func_hash->filter_hash;
1666 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001667 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001668 all = 1;
1669 } else {
1670 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001671 hash = ops->func_hash->notrace_hash;
1672 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001673 /*
1674 * If the notrace hash has no items,
1675 * then there's nothing to do.
1676 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001677 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001678 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001679 }
1680
1681 do_for_each_ftrace_rec(pg, rec) {
1682 int in_other_hash = 0;
1683 int in_hash = 0;
1684 int match = 0;
1685
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001686 if (rec->flags & FTRACE_FL_DISABLED)
1687 continue;
1688
Steven Rostedted926f92011-05-03 13:25:24 -04001689 if (all) {
1690 /*
1691 * Only the filter_hash affects all records.
1692 * Update if the record is not in the notrace hash.
1693 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001694 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001695 match = 1;
1696 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001697 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1698 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001699
1700 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001701 * If filter_hash is set, we want to match all functions
1702 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001703 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001704 * If filter_hash is not set, then we are decrementing.
1705 * That means we match anything that is in the hash
1706 * and also in the other_hash. That is, we need to turn
1707 * off functions in the other hash because they are disabled
1708 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001709 */
1710 if (filter_hash && in_hash && !in_other_hash)
1711 match = 1;
1712 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001713 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001714 match = 1;
1715 }
1716 if (!match)
1717 continue;
1718
1719 if (inc) {
1720 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001721 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001722 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001723
1724 /*
1725 * If there's only a single callback registered to a
1726 * function, and the ops has a trampoline registered
1727 * for it, then we can call it directly.
1728 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001729 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001730 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001731 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001732 /*
1733 * If we are adding another function callback
1734 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001735 * custom trampoline in use, then we need to go
1736 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001737 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001738 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001739
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001740 /*
1741 * If any ops wants regs saved for this function
1742 * then all ops will get saved regs.
1743 */
1744 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1745 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001746 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001747 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001748 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001749 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001750
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001751 /*
1752 * If the rec had REGS enabled and the ops that is
1753 * being removed had REGS set, then see if there is
1754 * still any ops for this record that wants regs.
1755 * If not, we can stop recording them.
1756 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001757 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001758 rec->flags & FTRACE_FL_REGS &&
1759 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1760 if (!test_rec_ops_needs_regs(rec))
1761 rec->flags &= ~FTRACE_FL_REGS;
1762 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001763
1764 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001765 * If the rec had TRAMP enabled, then it needs to
1766 * be cleared. As TRAMP can only be enabled iff
1767 * there is only a single ops attached to it.
1768 * In otherwords, always disable it on decrementing.
1769 * In the future, we may set it if rec count is
1770 * decremented to one, and the ops that is left
1771 * has a trampoline.
1772 */
1773 rec->flags &= ~FTRACE_FL_TRAMP;
1774
1775 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001776 * flags will be cleared in ftrace_check_record()
1777 * if rec count is zero.
1778 */
Steven Rostedted926f92011-05-03 13:25:24 -04001779 }
1780 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001781
1782 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1783 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1784
Steven Rostedted926f92011-05-03 13:25:24 -04001785 /* Shortcut, if we handled all records, we are done. */
1786 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001787 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001788 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001789
1790 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001791}
1792
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001793static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001794 int filter_hash)
1795{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001796 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001797}
1798
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001799static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001800 int filter_hash)
1801{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001802 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001803}
1804
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001805static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1806 int filter_hash, int inc)
1807{
1808 struct ftrace_ops *op;
1809
1810 __ftrace_hash_rec_update(ops, filter_hash, inc);
1811
1812 if (ops->func_hash != &global_ops.local_hash)
1813 return;
1814
1815 /*
1816 * If the ops shares the global_ops hash, then we need to update
1817 * all ops that are enabled and use this hash.
1818 */
1819 do_for_each_ftrace_op(op, ftrace_ops_list) {
1820 /* Already done */
1821 if (op == ops)
1822 continue;
1823 if (op->func_hash == &global_ops.local_hash)
1824 __ftrace_hash_rec_update(op, filter_hash, inc);
1825 } while_for_each_ftrace_op(op);
1826}
1827
1828static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1829 int filter_hash)
1830{
1831 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1832}
1833
1834static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1835 int filter_hash)
1836{
1837 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1838}
1839
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001840/*
1841 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1842 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1843 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1844 * Note that old_hash and new_hash has below meanings
1845 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1846 * - If the hash is EMPTY_HASH, it hits nothing
1847 * - Anything else hits the recs which match the hash entries.
1848 */
1849static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1850 struct ftrace_hash *old_hash,
1851 struct ftrace_hash *new_hash)
1852{
1853 struct ftrace_page *pg;
1854 struct dyn_ftrace *rec, *end = NULL;
1855 int in_old, in_new;
1856
1857 /* Only update if the ops has been registered */
1858 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1859 return 0;
1860
1861 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1862 return 0;
1863
1864 /*
1865 * Since the IPMODIFY is a very address sensitive action, we do not
1866 * allow ftrace_ops to set all functions to new hash.
1867 */
1868 if (!new_hash || !old_hash)
1869 return -EINVAL;
1870
1871 /* Update rec->flags */
1872 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001873
1874 if (rec->flags & FTRACE_FL_DISABLED)
1875 continue;
1876
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001877 /* We need to update only differences of filter_hash */
1878 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1879 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1880 if (in_old == in_new)
1881 continue;
1882
1883 if (in_new) {
1884 /* New entries must ensure no others are using it */
1885 if (rec->flags & FTRACE_FL_IPMODIFY)
1886 goto rollback;
1887 rec->flags |= FTRACE_FL_IPMODIFY;
1888 } else /* Removed entry */
1889 rec->flags &= ~FTRACE_FL_IPMODIFY;
1890 } while_for_each_ftrace_rec();
1891
1892 return 0;
1893
1894rollback:
1895 end = rec;
1896
1897 /* Roll back what we did above */
1898 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001899
1900 if (rec->flags & FTRACE_FL_DISABLED)
1901 continue;
1902
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001903 if (rec == end)
1904 goto err_out;
1905
1906 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1907 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1908 if (in_old == in_new)
1909 continue;
1910
1911 if (in_new)
1912 rec->flags &= ~FTRACE_FL_IPMODIFY;
1913 else
1914 rec->flags |= FTRACE_FL_IPMODIFY;
1915 } while_for_each_ftrace_rec();
1916
1917err_out:
1918 return -EBUSY;
1919}
1920
1921static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1922{
1923 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1924
1925 if (ftrace_hash_empty(hash))
1926 hash = NULL;
1927
1928 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1929}
1930
1931/* Disabling always succeeds */
1932static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1933{
1934 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1935
1936 if (ftrace_hash_empty(hash))
1937 hash = NULL;
1938
1939 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1940}
1941
1942static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1943 struct ftrace_hash *new_hash)
1944{
1945 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1946
1947 if (ftrace_hash_empty(old_hash))
1948 old_hash = NULL;
1949
1950 if (ftrace_hash_empty(new_hash))
1951 new_hash = NULL;
1952
1953 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1954}
1955
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001956static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001957{
1958 int i;
1959
1960 printk(KERN_CONT "%s", fmt);
1961
1962 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1963 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1964}
1965
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001966static struct ftrace_ops *
1967ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001968static struct ftrace_ops *
1969ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001970
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001971enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001972const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001973
1974static void print_bug_type(void)
1975{
1976 switch (ftrace_bug_type) {
1977 case FTRACE_BUG_UNKNOWN:
1978 break;
1979 case FTRACE_BUG_INIT:
1980 pr_info("Initializing ftrace call sites\n");
1981 break;
1982 case FTRACE_BUG_NOP:
1983 pr_info("Setting ftrace call site to NOP\n");
1984 break;
1985 case FTRACE_BUG_CALL:
1986 pr_info("Setting ftrace call site to call ftrace function\n");
1987 break;
1988 case FTRACE_BUG_UPDATE:
1989 pr_info("Updating ftrace call site to call a different ftrace function\n");
1990 break;
1991 }
1992}
1993
Steven Rostedtc88fd862011-08-16 09:53:39 -04001994/**
1995 * ftrace_bug - report and shutdown function tracer
1996 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001997 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001998 *
1999 * The arch code that enables or disables the function tracing
2000 * can call ftrace_bug() when it has detected a problem in
2001 * modifying the code. @failed should be one of either:
2002 * EFAULT - if the problem happens on reading the @ip address
2003 * EINVAL - if what is read at @ip is not what was expected
2004 * EPERM - if the problem happens on writting to the @ip address
2005 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002006void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002007{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002008 unsigned long ip = rec ? rec->ip : 0;
2009
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002010 switch (failed) {
2011 case -EFAULT:
2012 FTRACE_WARN_ON_ONCE(1);
2013 pr_info("ftrace faulted on modifying ");
2014 print_ip_sym(ip);
2015 break;
2016 case -EINVAL:
2017 FTRACE_WARN_ON_ONCE(1);
2018 pr_info("ftrace failed to modify ");
2019 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002020 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002021 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002022 if (ftrace_expected) {
2023 print_ip_ins(" expected: ", ftrace_expected);
2024 pr_cont("\n");
2025 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002026 break;
2027 case -EPERM:
2028 FTRACE_WARN_ON_ONCE(1);
2029 pr_info("ftrace faulted on writing ");
2030 print_ip_sym(ip);
2031 break;
2032 default:
2033 FTRACE_WARN_ON_ONCE(1);
2034 pr_info("ftrace faulted on unknown error ");
2035 print_ip_sym(ip);
2036 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002037 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002038 if (rec) {
2039 struct ftrace_ops *ops = NULL;
2040
2041 pr_info("ftrace record flags: %lx\n", rec->flags);
2042 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2043 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2044 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2045 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002046 if (ops) {
2047 do {
2048 pr_cont("\ttramp: %pS (%pS)",
2049 (void *)ops->trampoline,
2050 (void *)ops->func);
2051 ops = ftrace_find_tramp_ops_next(rec, ops);
2052 } while (ops);
2053 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002054 pr_cont("\ttramp: ERROR!");
2055
2056 }
2057 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002058 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002059 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002060}
2061
Steven Rostedtc88fd862011-08-16 09:53:39 -04002062static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002063{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002064 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002065
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002066 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2067
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002068 if (rec->flags & FTRACE_FL_DISABLED)
2069 return FTRACE_UPDATE_IGNORE;
2070
Steven Rostedt982c3502008-11-15 16:31:41 -05002071 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002072 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002073 *
Steven Rostedted926f92011-05-03 13:25:24 -04002074 * If the record has a ref count, then we need to enable it
2075 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002076 *
Steven Rostedted926f92011-05-03 13:25:24 -04002077 * Otherwise we make sure its disabled.
2078 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002079 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002080 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002081 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002082 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002083 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002084
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002085 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002086 * If enabling and the REGS flag does not match the REGS_EN, or
2087 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2088 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002089 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002090 if (flag) {
2091 if (!(rec->flags & FTRACE_FL_REGS) !=
2092 !(rec->flags & FTRACE_FL_REGS_EN))
2093 flag |= FTRACE_FL_REGS;
2094
2095 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2096 !(rec->flags & FTRACE_FL_TRAMP_EN))
2097 flag |= FTRACE_FL_TRAMP;
2098 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002099
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002100 /* If the state of this record hasn't changed, then do nothing */
2101 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002102 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002103
2104 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002105 /* Save off if rec is being enabled (for return value) */
2106 flag ^= rec->flags & FTRACE_FL_ENABLED;
2107
2108 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002109 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002110 if (flag & FTRACE_FL_REGS) {
2111 if (rec->flags & FTRACE_FL_REGS)
2112 rec->flags |= FTRACE_FL_REGS_EN;
2113 else
2114 rec->flags &= ~FTRACE_FL_REGS_EN;
2115 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002116 if (flag & FTRACE_FL_TRAMP) {
2117 if (rec->flags & FTRACE_FL_TRAMP)
2118 rec->flags |= FTRACE_FL_TRAMP_EN;
2119 else
2120 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2121 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002122 }
2123
2124 /*
2125 * If this record is being updated from a nop, then
2126 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002127 * Otherwise,
2128 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002129 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002130 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002131 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002132 if (flag & FTRACE_FL_ENABLED) {
2133 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002134 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002135 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002136
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002137 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002138 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002139 }
2140
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002141 if (update) {
2142 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002143 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002144 rec->flags = 0;
2145 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002146 /*
2147 * Just disable the record, but keep the ops TRAMP
2148 * and REGS states. The _EN flags must be disabled though.
2149 */
2150 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2151 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002152 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002153
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002154 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002155 return FTRACE_UPDATE_MAKE_NOP;
2156}
2157
2158/**
2159 * ftrace_update_record, set a record that now is tracing or not
2160 * @rec: the record to update
2161 * @enable: set to 1 if the record is tracing, zero to force disable
2162 *
2163 * The records that represent all functions that can be traced need
2164 * to be updated when tracing has been enabled.
2165 */
2166int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2167{
2168 return ftrace_check_record(rec, enable, 1);
2169}
2170
2171/**
2172 * ftrace_test_record, check if the record has been enabled or not
2173 * @rec: the record to test
2174 * @enable: set to 1 to check if enabled, 0 if it is disabled
2175 *
2176 * The arch code may need to test if a record is already set to
2177 * tracing to determine how to modify the function code that it
2178 * represents.
2179 */
2180int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2181{
2182 return ftrace_check_record(rec, enable, 0);
2183}
2184
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002185static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002186ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2187{
2188 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002189 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002190
2191 do_for_each_ftrace_op(op, ftrace_ops_list) {
2192
2193 if (!op->trampoline)
2194 continue;
2195
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002196 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002197 return op;
2198 } while_for_each_ftrace_op(op);
2199
2200 return NULL;
2201}
2202
2203static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002204ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2205 struct ftrace_ops *op)
2206{
2207 unsigned long ip = rec->ip;
2208
2209 while_for_each_ftrace_op(op) {
2210
2211 if (!op->trampoline)
2212 continue;
2213
2214 if (hash_contains_ip(ip, op->func_hash))
2215 return op;
2216 }
2217
2218 return NULL;
2219}
2220
2221static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002222ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2223{
2224 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002225 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002226
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002227 /*
2228 * Need to check removed ops first.
2229 * If they are being removed, and this rec has a tramp,
2230 * and this rec is in the ops list, then it would be the
2231 * one with the tramp.
2232 */
2233 if (removed_ops) {
2234 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002235 return removed_ops;
2236 }
2237
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002238 /*
2239 * Need to find the current trampoline for a rec.
2240 * Now, a trampoline is only attached to a rec if there
2241 * was a single 'ops' attached to it. But this can be called
2242 * when we are adding another op to the rec or removing the
2243 * current one. Thus, if the op is being added, we can
2244 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002245 * yet.
2246 *
2247 * If an ops is being modified (hooking to different functions)
2248 * then we don't care about the new functions that are being
2249 * added, just the old ones (that are probably being removed).
2250 *
2251 * If we are adding an ops to a function that already is using
2252 * a trampoline, it needs to be removed (trampolines are only
2253 * for single ops connected), then an ops that is not being
2254 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002255 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002256 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002257
2258 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002259 continue;
2260
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002261 /*
2262 * If the ops is being added, it hasn't gotten to
2263 * the point to be removed from this tree yet.
2264 */
2265 if (op->flags & FTRACE_OPS_FL_ADDING)
2266 continue;
2267
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002268
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002269 /*
2270 * If the ops is being modified and is in the old
2271 * hash, then it is probably being removed from this
2272 * function.
2273 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002274 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2275 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002276 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002277 /*
2278 * If the ops is not being added or modified, and it's
2279 * in its normal filter hash, then this must be the one
2280 * we want!
2281 */
2282 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2283 hash_contains_ip(ip, op->func_hash))
2284 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002285
2286 } while_for_each_ftrace_op(op);
2287
2288 return NULL;
2289}
2290
2291static struct ftrace_ops *
2292ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2293{
2294 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002295 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002296
2297 do_for_each_ftrace_op(op, ftrace_ops_list) {
2298 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002299 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002300 return op;
2301 } while_for_each_ftrace_op(op);
2302
2303 return NULL;
2304}
2305
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002306/**
2307 * ftrace_get_addr_new - Get the call address to set to
2308 * @rec: The ftrace record descriptor
2309 *
2310 * If the record has the FTRACE_FL_REGS set, that means that it
2311 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2312 * is not not set, then it wants to convert to the normal callback.
2313 *
2314 * Returns the address of the trampoline to set to
2315 */
2316unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2317{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002318 struct ftrace_ops *ops;
2319
2320 /* Trampolines take precedence over regs */
2321 if (rec->flags & FTRACE_FL_TRAMP) {
2322 ops = ftrace_find_tramp_ops_new(rec);
2323 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002324 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2325 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002326 /* Ftrace is shutting down, return anything */
2327 return (unsigned long)FTRACE_ADDR;
2328 }
2329 return ops->trampoline;
2330 }
2331
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002332 if (rec->flags & FTRACE_FL_REGS)
2333 return (unsigned long)FTRACE_REGS_ADDR;
2334 else
2335 return (unsigned long)FTRACE_ADDR;
2336}
2337
2338/**
2339 * ftrace_get_addr_curr - Get the call address that is already there
2340 * @rec: The ftrace record descriptor
2341 *
2342 * The FTRACE_FL_REGS_EN is set when the record already points to
2343 * a function that saves all the regs. Basically the '_EN' version
2344 * represents the current state of the function.
2345 *
2346 * Returns the address of the trampoline that is currently being called
2347 */
2348unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2349{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002350 struct ftrace_ops *ops;
2351
2352 /* Trampolines take precedence over regs */
2353 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2354 ops = ftrace_find_tramp_ops_curr(rec);
2355 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002356 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2357 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002358 /* Ftrace is shutting down, return anything */
2359 return (unsigned long)FTRACE_ADDR;
2360 }
2361 return ops->trampoline;
2362 }
2363
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002364 if (rec->flags & FTRACE_FL_REGS_EN)
2365 return (unsigned long)FTRACE_REGS_ADDR;
2366 else
2367 return (unsigned long)FTRACE_ADDR;
2368}
2369
Steven Rostedtc88fd862011-08-16 09:53:39 -04002370static int
2371__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2372{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002373 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002374 unsigned long ftrace_addr;
2375 int ret;
2376
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002377 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002378
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002379 /* This needs to be done before we call ftrace_update_record */
2380 ftrace_old_addr = ftrace_get_addr_curr(rec);
2381
2382 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002383
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002384 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2385
Steven Rostedtc88fd862011-08-16 09:53:39 -04002386 switch (ret) {
2387 case FTRACE_UPDATE_IGNORE:
2388 return 0;
2389
2390 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002391 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002392 return ftrace_make_call(rec, ftrace_addr);
2393
2394 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002395 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002396 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002397
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002398 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002399 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002400 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002401 }
2402
2403 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002404}
2405
Steven Rostedte4f5d542012-04-27 09:13:18 -04002406void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002407{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002408 struct dyn_ftrace *rec;
2409 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002410 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002411
Steven Rostedt45a4a232011-04-21 23:16:46 -04002412 if (unlikely(ftrace_disabled))
2413 return;
2414
Steven Rostedt265c8312009-02-13 12:43:56 -05002415 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002416
2417 if (rec->flags & FTRACE_FL_DISABLED)
2418 continue;
2419
Steven Rostedte4f5d542012-04-27 09:13:18 -04002420 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002421 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002422 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002423 /* Stop processing */
2424 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002425 }
2426 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002427}
2428
Steven Rostedtc88fd862011-08-16 09:53:39 -04002429struct ftrace_rec_iter {
2430 struct ftrace_page *pg;
2431 int index;
2432};
2433
2434/**
2435 * ftrace_rec_iter_start, start up iterating over traced functions
2436 *
2437 * Returns an iterator handle that is used to iterate over all
2438 * the records that represent address locations where functions
2439 * are traced.
2440 *
2441 * May return NULL if no records are available.
2442 */
2443struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2444{
2445 /*
2446 * We only use a single iterator.
2447 * Protected by the ftrace_lock mutex.
2448 */
2449 static struct ftrace_rec_iter ftrace_rec_iter;
2450 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2451
2452 iter->pg = ftrace_pages_start;
2453 iter->index = 0;
2454
2455 /* Could have empty pages */
2456 while (iter->pg && !iter->pg->index)
2457 iter->pg = iter->pg->next;
2458
2459 if (!iter->pg)
2460 return NULL;
2461
2462 return iter;
2463}
2464
2465/**
2466 * ftrace_rec_iter_next, get the next record to process.
2467 * @iter: The handle to the iterator.
2468 *
2469 * Returns the next iterator after the given iterator @iter.
2470 */
2471struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2472{
2473 iter->index++;
2474
2475 if (iter->index >= iter->pg->index) {
2476 iter->pg = iter->pg->next;
2477 iter->index = 0;
2478
2479 /* Could have empty pages */
2480 while (iter->pg && !iter->pg->index)
2481 iter->pg = iter->pg->next;
2482 }
2483
2484 if (!iter->pg)
2485 return NULL;
2486
2487 return iter;
2488}
2489
2490/**
2491 * ftrace_rec_iter_record, get the record at the iterator location
2492 * @iter: The current iterator location
2493 *
2494 * Returns the record that the current @iter is at.
2495 */
2496struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2497{
2498 return &iter->pg->records[iter->index];
2499}
2500
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302501static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002502ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002503{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002504 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002505
Steven Rostedt45a4a232011-04-21 23:16:46 -04002506 if (unlikely(ftrace_disabled))
2507 return 0;
2508
Shaohua Li25aac9d2009-01-09 11:29:40 +08002509 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002510 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002511 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002512 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302513 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002514 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302515 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002516}
2517
Steven Rostedt000ab692009-02-17 13:35:06 -05002518/*
2519 * archs can override this function if they must do something
2520 * before the modifying code is performed.
2521 */
2522int __weak ftrace_arch_code_modify_prepare(void)
2523{
2524 return 0;
2525}
2526
2527/*
2528 * archs can override this function if they must do something
2529 * after the modifying code is performed.
2530 */
2531int __weak ftrace_arch_code_modify_post_process(void)
2532{
2533 return 0;
2534}
2535
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002536void ftrace_modify_all_code(int command)
2537{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002538 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002539 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002540
2541 /*
2542 * If the ftrace_caller calls a ftrace_ops func directly,
2543 * we need to make sure that it only traces functions it
2544 * expects to trace. When doing the switch of functions,
2545 * we need to update to the ftrace_ops_list_func first
2546 * before the transition between old and new calls are set,
2547 * as the ftrace_ops_list_func will check the ops hashes
2548 * to make sure the ops are having the right functions
2549 * traced.
2550 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002551 if (update) {
2552 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2553 if (FTRACE_WARN_ON(err))
2554 return;
2555 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002556
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002557 if (command & FTRACE_UPDATE_CALLS)
2558 ftrace_replace_code(1);
2559 else if (command & FTRACE_DISABLE_CALLS)
2560 ftrace_replace_code(0);
2561
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002562 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2563 function_trace_op = set_function_trace_op;
2564 smp_wmb();
2565 /* If irqs are disabled, we are in stop machine */
2566 if (!irqs_disabled())
2567 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002568 err = ftrace_update_ftrace_func(ftrace_trace_function);
2569 if (FTRACE_WARN_ON(err))
2570 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002571 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002572
2573 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002574 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002575 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002576 err = ftrace_disable_ftrace_graph_caller();
2577 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002578}
2579
Ingo Molnare309b412008-05-12 21:20:51 +02002580static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002581{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002582 int *command = data;
2583
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002584 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002585
Steven Rostedtc88fd862011-08-16 09:53:39 -04002586 return 0;
2587}
2588
2589/**
2590 * ftrace_run_stop_machine, go back to the stop machine method
2591 * @command: The command to tell ftrace what to do
2592 *
2593 * If an arch needs to fall back to the stop machine method, the
2594 * it can call this function.
2595 */
2596void ftrace_run_stop_machine(int command)
2597{
2598 stop_machine(__ftrace_modify_code, &command, NULL);
2599}
2600
2601/**
2602 * arch_ftrace_update_code, modify the code to trace or not trace
2603 * @command: The command that needs to be done
2604 *
2605 * Archs can override this function if it does not need to
2606 * run stop_machine() to modify code.
2607 */
2608void __weak arch_ftrace_update_code(int command)
2609{
2610 ftrace_run_stop_machine(command);
2611}
2612
2613static void ftrace_run_update_code(int command)
2614{
2615 int ret;
2616
2617 ret = ftrace_arch_code_modify_prepare();
2618 FTRACE_WARN_ON(ret);
2619 if (ret)
2620 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002621
2622 /*
2623 * By default we use stop_machine() to modify the code.
2624 * But archs can do what ever they want as long as it
2625 * is safe. The stop_machine() is the safest, but also
2626 * produces the most overhead.
2627 */
2628 arch_ftrace_update_code(command);
2629
Steven Rostedt000ab692009-02-17 13:35:06 -05002630 ret = ftrace_arch_code_modify_post_process();
2631 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002632}
2633
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002634static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002635 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002636{
2637 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002638 ops->old_hash.filter_hash = old_hash->filter_hash;
2639 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002640 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002641 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002642 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002643 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2644}
2645
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002646static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002647static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002648
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002649void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2650{
2651}
2652
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002653static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002654{
2655 free_percpu(ops->disabled);
2656}
2657
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002658static void ftrace_startup_enable(int command)
2659{
2660 if (saved_ftrace_func != ftrace_trace_function) {
2661 saved_ftrace_func = ftrace_trace_function;
2662 command |= FTRACE_UPDATE_TRACE_FUNC;
2663 }
2664
2665 if (!command || !ftrace_enabled)
2666 return;
2667
2668 ftrace_run_update_code(command);
2669}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002670
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002671static void ftrace_startup_all(int command)
2672{
2673 update_all_ops = true;
2674 ftrace_startup_enable(command);
2675 update_all_ops = false;
2676}
2677
Steven Rostedta1cd6172011-05-23 15:24:25 -04002678static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002679{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002680 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002681
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002682 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002683 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002684
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002685 ret = __register_ftrace_function(ops);
2686 if (ret)
2687 return ret;
2688
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002689 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002690
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002691 /*
2692 * Note that ftrace probes uses this to start up
2693 * and modify functions it will probe. But we still
2694 * set the ADDING flag for modification, as probes
2695 * do not have trampolines. If they add them in the
2696 * future, then the probes will need to distinguish
2697 * between adding and updating probes.
2698 */
2699 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002700
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002701 ret = ftrace_hash_ipmodify_enable(ops);
2702 if (ret < 0) {
2703 /* Rollback registration process */
2704 __unregister_ftrace_function(ops);
2705 ftrace_start_up--;
2706 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2707 return ret;
2708 }
2709
Jiri Olsa7f50d062016-03-16 15:34:33 +01002710 if (ftrace_hash_rec_enable(ops, 1))
2711 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002712
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002713 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002714
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002715 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2716
Steven Rostedta1cd6172011-05-23 15:24:25 -04002717 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002718}
2719
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002720static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002721{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002722 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002723
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002724 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002725 return -ENODEV;
2726
2727 ret = __unregister_ftrace_function(ops);
2728 if (ret)
2729 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002730
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002731 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002732 /*
2733 * Just warn in case of unbalance, no need to kill ftrace, it's not
2734 * critical but the ftrace_call callers may be never nopped again after
2735 * further ftrace uses.
2736 */
2737 WARN_ON_ONCE(ftrace_start_up < 0);
2738
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002739 /* Disabling ipmodify never fails */
2740 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002741
2742 if (ftrace_hash_rec_disable(ops, 1))
2743 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002744
Namhyung Kima737e6d2014-06-12 23:56:12 +09002745 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002746
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002747 if (saved_ftrace_func != ftrace_trace_function) {
2748 saved_ftrace_func = ftrace_trace_function;
2749 command |= FTRACE_UPDATE_TRACE_FUNC;
2750 }
2751
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002752 if (!command || !ftrace_enabled) {
2753 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002754 * If these are per_cpu ops, they still need their
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002755 * per_cpu field freed. Since, function tracing is
2756 * not currently active, we can just free them
2757 * without synchronizing all CPUs.
2758 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002759 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2760 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002761 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002762 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002763
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002764 /*
2765 * If the ops uses a trampoline, then it needs to be
2766 * tested first on update.
2767 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002768 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002769 removed_ops = ops;
2770
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002771 /* The trampoline logic checks the old hashes */
2772 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2773 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2774
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002775 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002776
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002777 /*
2778 * If there's no more ops registered with ftrace, run a
2779 * sanity check to make sure all rec flags are cleared.
2780 */
2781 if (ftrace_ops_list == &ftrace_list_end) {
2782 struct ftrace_page *pg;
2783 struct dyn_ftrace *rec;
2784
2785 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002786 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002787 pr_warn(" %pS flags:%lx\n",
2788 (void *)rec->ip, rec->flags);
2789 } while_for_each_ftrace_rec();
2790 }
2791
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002792 ops->old_hash.filter_hash = NULL;
2793 ops->old_hash.notrace_hash = NULL;
2794
2795 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002796 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002797
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002798 /*
2799 * Dynamic ops may be freed, we must make sure that all
2800 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002801 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002802 * ops.
2803 *
2804 * Again, normal synchronize_sched() is not good enough.
2805 * We need to do a hard force of sched synchronization.
2806 * This is because we use preempt_disable() to do RCU, but
2807 * the function tracers can be called where RCU is not watching
2808 * (like before user_exit()). We can not rely on the RCU
2809 * infrastructure to do the synchronization, thus we must do it
2810 * ourselves.
2811 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002812 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002813 schedule_on_each_cpu(ftrace_sync);
2814
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002815 arch_ftrace_trampoline_free(ops);
2816
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002817 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2818 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002819 }
2820
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002821 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002822}
2823
Ingo Molnare309b412008-05-12 21:20:51 +02002824static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002825{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302826 int command;
2827
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002828 if (unlikely(ftrace_disabled))
2829 return;
2830
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002831 /* Force update next time */
2832 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002833 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302834 if (ftrace_start_up) {
2835 command = FTRACE_UPDATE_CALLS;
2836 if (ftrace_graph_active)
2837 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002838 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302839 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002840}
2841
Ingo Molnare309b412008-05-12 21:20:51 +02002842static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002843{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302844 int command;
2845
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002846 if (unlikely(ftrace_disabled))
2847 return;
2848
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002849 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302850 if (ftrace_start_up) {
2851 command = FTRACE_DISABLE_CALLS;
2852 if (ftrace_graph_active)
2853 command |= FTRACE_STOP_FUNC_RET;
2854 ftrace_run_update_code(command);
2855 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002856}
2857
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002858static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002859unsigned long ftrace_update_tot_cnt;
2860
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002861static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002862{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002863 /*
2864 * Filter_hash being empty will default to trace module.
2865 * But notrace hash requires a test of individual module functions.
2866 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002867 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2868 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002869}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002870
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002871/*
2872 * Check if the current ops references the record.
2873 *
2874 * If the ops traces all functions, then it was already accounted for.
2875 * If the ops does not trace the current record function, skip it.
2876 * If the ops ignores the function via notrace filter, skip it.
2877 */
2878static inline bool
2879ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2880{
2881 /* If ops isn't enabled, ignore it */
2882 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2883 return 0;
2884
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002885 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002886 if (ops_traces_mod(ops))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002887 return 1;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002888
2889 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002890 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2891 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002892 return 0;
2893
2894 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002895 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002896 return 0;
2897
2898 return 1;
2899}
2900
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002901static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002902{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002903 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002904 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002905 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002906 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002907 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002908 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002909
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002910 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002911
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002912 /*
2913 * When a module is loaded, this function is called to convert
2914 * the calls to mcount in its text to nops, and also to create
2915 * an entry in the ftrace data. Now, if ftrace is activated
2916 * after this call, but before the module sets its text to
2917 * read-only, the modification of enabling ftrace can fail if
2918 * the read-only is done while ftrace is converting the calls.
2919 * To prevent this, the module's records are set as disabled
2920 * and will be enabled after the call to set the module's text
2921 * to read-only.
2922 */
2923 if (mod)
2924 rec_flags |= FTRACE_FL_DISABLED;
2925
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002926 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302927
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002928 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002929
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002930 /* If something went wrong, bail without enabling anything */
2931 if (unlikely(ftrace_disabled))
2932 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002933
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002934 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002935 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302936
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002937 /*
2938 * Do the initial record conversion from mcount jump
2939 * to the NOP instructions.
2940 */
2941 if (!ftrace_code_disable(mod, p))
2942 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002943
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002944 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002945 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002946 }
2947
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002948 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002949 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002950 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002951
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002952 return 0;
2953}
2954
Steven Rostedta7900872011-12-16 16:23:44 -05002955static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002956{
Steven Rostedta7900872011-12-16 16:23:44 -05002957 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002958 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002959
Steven Rostedta7900872011-12-16 16:23:44 -05002960 if (WARN_ON(!count))
2961 return -EINVAL;
2962
2963 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002964
2965 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002966 * We want to fill as much as possible. No more than a page
2967 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002968 */
Steven Rostedta7900872011-12-16 16:23:44 -05002969 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2970 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002971
Steven Rostedta7900872011-12-16 16:23:44 -05002972 again:
2973 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2974
2975 if (!pg->records) {
2976 /* if we can't allocate this size, try something smaller */
2977 if (!order)
2978 return -ENOMEM;
2979 order >>= 1;
2980 goto again;
2981 }
2982
2983 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2984 pg->size = cnt;
2985
2986 if (cnt > count)
2987 cnt = count;
2988
2989 return cnt;
2990}
2991
2992static struct ftrace_page *
2993ftrace_allocate_pages(unsigned long num_to_init)
2994{
2995 struct ftrace_page *start_pg;
2996 struct ftrace_page *pg;
2997 int order;
2998 int cnt;
2999
3000 if (!num_to_init)
3001 return 0;
3002
3003 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3004 if (!pg)
3005 return NULL;
3006
3007 /*
3008 * Try to allocate as much as possible in one continues
3009 * location that fills in all of the space. We want to
3010 * waste as little space as possible.
3011 */
3012 for (;;) {
3013 cnt = ftrace_allocate_records(pg, num_to_init);
3014 if (cnt < 0)
3015 goto free_pages;
3016
3017 num_to_init -= cnt;
3018 if (!num_to_init)
3019 break;
3020
3021 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3022 if (!pg->next)
3023 goto free_pages;
3024
3025 pg = pg->next;
3026 }
3027
3028 return start_pg;
3029
3030 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003031 pg = start_pg;
3032 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003033 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3034 free_pages((unsigned long)pg->records, order);
3035 start_pg = pg->next;
3036 kfree(pg);
3037 pg = start_pg;
3038 }
3039 pr_info("ftrace: FAILED to allocate memory for functions\n");
3040 return NULL;
3041}
3042
Steven Rostedt5072c592008-05-12 21:20:43 +02003043#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3044
3045struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003046 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003047 loff_t func_pos;
3048 struct ftrace_page *pg;
3049 struct dyn_ftrace *func;
3050 struct ftrace_func_probe *probe;
3051 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003052 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003053 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003054 int hidx;
3055 int idx;
3056 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003057};
3058
Ingo Molnare309b412008-05-12 21:20:51 +02003059static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003060t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003061{
3062 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003063 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003064 struct hlist_head *hhd;
3065
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003066 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003067 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003068
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003069 if (iter->probe)
3070 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003071 retry:
3072 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3073 return NULL;
3074
3075 hhd = &ftrace_func_hash[iter->hidx];
3076
3077 if (hlist_empty(hhd)) {
3078 iter->hidx++;
3079 hnd = NULL;
3080 goto retry;
3081 }
3082
3083 if (!hnd)
3084 hnd = hhd->first;
3085 else {
3086 hnd = hnd->next;
3087 if (!hnd) {
3088 iter->hidx++;
3089 goto retry;
3090 }
3091 }
3092
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003093 if (WARN_ON_ONCE(!hnd))
3094 return NULL;
3095
3096 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3097
3098 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003099}
3100
3101static void *t_hash_start(struct seq_file *m, loff_t *pos)
3102{
3103 struct ftrace_iterator *iter = m->private;
3104 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003105 loff_t l;
3106
Steven Rostedt69a30832011-12-19 15:21:16 -05003107 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3108 return NULL;
3109
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003110 if (iter->func_pos > *pos)
3111 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003112
Li Zefand82d6242009-06-24 09:54:54 +08003113 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003114 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003115 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003116 if (!p)
3117 break;
3118 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003119 if (!p)
3120 return NULL;
3121
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003122 /* Only set this if we have an item */
3123 iter->flags |= FTRACE_ITER_HASH;
3124
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003125 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003126}
3127
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003128static int
3129t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003130{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003131 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003132
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003133 rec = iter->probe;
3134 if (WARN_ON_ONCE(!rec))
3135 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003136
Steven Rostedt809dcf22009-02-16 23:06:01 -05003137 if (rec->ops->print)
3138 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3139
Steven Rostedtb375a112009-09-17 00:05:58 -04003140 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003141
3142 if (rec->data)
3143 seq_printf(m, ":%p", rec->data);
3144 seq_putc(m, '\n');
3145
3146 return 0;
3147}
3148
3149static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02003150t_next(struct seq_file *m, void *v, loff_t *pos)
3151{
3152 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003153 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003154 struct dyn_ftrace *rec = NULL;
3155
Steven Rostedt45a4a232011-04-21 23:16:46 -04003156 if (unlikely(ftrace_disabled))
3157 return NULL;
3158
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003159 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003160 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003161
Steven Rostedt5072c592008-05-12 21:20:43 +02003162 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01003163 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02003164
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003165 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003166 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003167
Steven Rostedt5072c592008-05-12 21:20:43 +02003168 retry:
3169 if (iter->idx >= iter->pg->index) {
3170 if (iter->pg->next) {
3171 iter->pg = iter->pg->next;
3172 iter->idx = 0;
3173 goto retry;
3174 }
3175 } else {
3176 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05003177 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003178 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05003179
Steven Rostedt41c52c02008-05-22 11:46:33 -04003180 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003181 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003182
3183 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003184 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003185
Steven Rostedt5072c592008-05-12 21:20:43 +02003186 rec = NULL;
3187 goto retry;
3188 }
3189 }
3190
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003191 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003192 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003193
3194 iter->func = rec;
3195
3196 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003197}
3198
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003199static void reset_iter_read(struct ftrace_iterator *iter)
3200{
3201 iter->pos = 0;
3202 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03003203 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003204}
3205
3206static void *t_start(struct seq_file *m, loff_t *pos)
3207{
3208 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003209 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003210 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003211 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003212
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003213 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003214
3215 if (unlikely(ftrace_disabled))
3216 return NULL;
3217
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003218 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003219 * If an lseek was done, then reset and start from beginning.
3220 */
3221 if (*pos < iter->pos)
3222 reset_iter_read(iter);
3223
3224 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003225 * For set_ftrace_filter reading, if we have the filter
3226 * off, we can short cut and just print out that all
3227 * functions are enabled.
3228 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003229 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003230 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003231 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003232 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003233 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003234 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003235 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003236 /* reset in case of seek/pread */
3237 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003238 return iter;
3239 }
3240
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003241 if (iter->flags & FTRACE_ITER_HASH)
3242 return t_hash_start(m, pos);
3243
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003244 /*
3245 * Unfortunately, we need to restart at ftrace_pages_start
3246 * every time we let go of the ftrace_mutex. This is because
3247 * those pointers can change without the lock.
3248 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003249 iter->pg = ftrace_pages_start;
3250 iter->idx = 0;
3251 for (l = 0; l <= *pos; ) {
3252 p = t_next(m, p, &l);
3253 if (!p)
3254 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003255 }
walimis5821e1b2008-11-15 15:19:06 +08003256
Steven Rostedt69a30832011-12-19 15:21:16 -05003257 if (!p)
3258 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003259
3260 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003261}
3262
3263static void t_stop(struct seq_file *m, void *p)
3264{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003265 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003266}
3267
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003268void * __weak
3269arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3270{
3271 return NULL;
3272}
3273
3274static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3275 struct dyn_ftrace *rec)
3276{
3277 void *ptr;
3278
3279 ptr = arch_ftrace_trampoline_func(ops, rec);
3280 if (ptr)
3281 seq_printf(m, " ->%pS", ptr);
3282}
3283
Steven Rostedt5072c592008-05-12 21:20:43 +02003284static int t_show(struct seq_file *m, void *v)
3285{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003286 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003287 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003288
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003289 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003290 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003291
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003292 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003293 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003294 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003295 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003296 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003297 return 0;
3298 }
3299
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003300 rec = iter->func;
3301
Steven Rostedt5072c592008-05-12 21:20:43 +02003302 if (!rec)
3303 return 0;
3304
Steven Rostedt647bcd02011-05-03 14:39:21 -04003305 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003306 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003307 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003308
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003309 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003310 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003311 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3312 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003313 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003314 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003315 if (ops) {
3316 do {
3317 seq_printf(m, "\ttramp: %pS (%pS)",
3318 (void *)ops->trampoline,
3319 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003320 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003321 ops = ftrace_find_tramp_ops_next(rec, ops);
3322 } while (ops);
3323 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003324 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003325 } else {
3326 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003327 }
3328 }
3329
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003330 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003331
3332 return 0;
3333}
3334
James Morris88e9d342009-09-22 16:43:43 -07003335static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003336 .start = t_start,
3337 .next = t_next,
3338 .stop = t_stop,
3339 .show = t_show,
3340};
3341
Ingo Molnare309b412008-05-12 21:20:51 +02003342static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003343ftrace_avail_open(struct inode *inode, struct file *file)
3344{
3345 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003346
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003347 if (unlikely(ftrace_disabled))
3348 return -ENODEV;
3349
Jiri Olsa50e18b92012-04-25 10:23:39 +02003350 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3351 if (iter) {
3352 iter->pg = ftrace_pages_start;
3353 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003354 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003355
Jiri Olsa50e18b92012-04-25 10:23:39 +02003356 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003357}
3358
Steven Rostedt647bcd02011-05-03 14:39:21 -04003359static int
3360ftrace_enabled_open(struct inode *inode, struct file *file)
3361{
3362 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003363
Jiri Olsa50e18b92012-04-25 10:23:39 +02003364 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3365 if (iter) {
3366 iter->pg = ftrace_pages_start;
3367 iter->flags = FTRACE_ITER_ENABLED;
3368 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003369 }
3370
Jiri Olsa50e18b92012-04-25 10:23:39 +02003371 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003372}
3373
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003374/**
3375 * ftrace_regex_open - initialize function tracer filter files
3376 * @ops: The ftrace_ops that hold the hash filters
3377 * @flag: The type of filter to process
3378 * @inode: The inode, usually passed in to your open routine
3379 * @file: The file, usually passed in to your open routine
3380 *
3381 * ftrace_regex_open() initializes the filter files for the
3382 * @ops. Depending on @flag it may process the filter hash or
3383 * the notrace hash of @ops. With this called from the open
3384 * routine, you can use ftrace_filter_write() for the write
3385 * routine if @flag has FTRACE_ITER_FILTER set, or
3386 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003387 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003388 * release must call ftrace_regex_release().
3389 */
3390int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003391ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003392 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003393{
3394 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003395 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003396 int ret = 0;
3397
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003398 ftrace_ops_init(ops);
3399
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003400 if (unlikely(ftrace_disabled))
3401 return -ENODEV;
3402
Steven Rostedt5072c592008-05-12 21:20:43 +02003403 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3404 if (!iter)
3405 return -ENOMEM;
3406
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003407 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3408 kfree(iter);
3409 return -ENOMEM;
3410 }
3411
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003412 iter->ops = ops;
3413 iter->flags = flag;
3414
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003415 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003416
Steven Rostedtf45948e2011-05-02 12:29:25 -04003417 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003418 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003419 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003420 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003421
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003422 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003423 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3424
3425 if (file->f_flags & O_TRUNC)
3426 iter->hash = alloc_ftrace_hash(size_bits);
3427 else
3428 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3429
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003430 if (!iter->hash) {
3431 trace_parser_put(&iter->parser);
3432 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003433 ret = -ENOMEM;
3434 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003435 }
3436 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003437
Steven Rostedt5072c592008-05-12 21:20:43 +02003438 if (file->f_mode & FMODE_READ) {
3439 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003440
3441 ret = seq_open(file, &show_ftrace_seq_ops);
3442 if (!ret) {
3443 struct seq_file *m = file->private_data;
3444 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003445 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003446 /* Failed */
3447 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003448 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003449 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003450 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003451 } else
3452 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003453
3454 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003455 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003456
3457 return ret;
3458}
3459
Steven Rostedt41c52c02008-05-22 11:46:33 -04003460static int
3461ftrace_filter_open(struct inode *inode, struct file *file)
3462{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003463 struct ftrace_ops *ops = inode->i_private;
3464
3465 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003466 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3467 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003468}
3469
3470static int
3471ftrace_notrace_open(struct inode *inode, struct file *file)
3472{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003473 struct ftrace_ops *ops = inode->i_private;
3474
3475 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003476 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003477}
3478
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003479/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3480struct ftrace_glob {
3481 char *search;
3482 unsigned len;
3483 int type;
3484};
3485
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003486/*
3487 * If symbols in an architecture don't correspond exactly to the user-visible
3488 * name of what they represent, it is possible to define this function to
3489 * perform the necessary adjustments.
3490*/
3491char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3492{
3493 return str;
3494}
3495
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003496static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003497{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003498 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003499 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003500
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003501 str = arch_ftrace_match_adjust(str, g->search);
3502
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003503 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003504 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003505 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003506 matched = 1;
3507 break;
3508 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003509 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003510 matched = 1;
3511 break;
3512 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003513 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003514 matched = 1;
3515 break;
3516 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003517 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003518 if (slen >= g->len &&
3519 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003520 matched = 1;
3521 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003522 case MATCH_GLOB:
3523 if (glob_match(g->search, str))
3524 matched = 1;
3525 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003526 }
3527
3528 return matched;
3529}
3530
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003531static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003532enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003533{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003534 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003535 int ret = 0;
3536
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003537 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003538 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003539 /* Do nothing if it doesn't exist */
3540 if (!entry)
3541 return 0;
3542
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003543 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003544 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003545 /* Do nothing if it exists */
3546 if (entry)
3547 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003548
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003549 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003550 }
3551 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003552}
3553
Steven Rostedt64e7c442009-02-13 17:08:48 -05003554static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003555ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3556 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003557{
3558 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003559 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003560
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003561 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3562
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003563 if (mod_g) {
3564 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3565
3566 /* blank module name to match all modules */
3567 if (!mod_g->len) {
3568 /* blank module globbing: modname xor exclude_mod */
3569 if ((!exclude_mod) != (!modname))
3570 goto func_match;
3571 return 0;
3572 }
3573
3574 /* not matching the module */
3575 if (!modname || !mod_matches) {
3576 if (exclude_mod)
3577 goto func_match;
3578 else
3579 return 0;
3580 }
3581
3582 if (mod_matches && exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003583 return 0;
3584
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003585func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003586 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003587 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003588 return 1;
3589 }
3590
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003591 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003592}
3593
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003594static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003595match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003596{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003597 struct ftrace_page *pg;
3598 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003599 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003600 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3601 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3602 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003603 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003604 int ret;
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003605 int clear_filter;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003606
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003607 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003608 func_g.type = filter_parse_regex(func, len, &func_g.search,
3609 &clear_filter);
3610 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003611 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003612
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003613 if (mod) {
3614 mod_g.type = filter_parse_regex(mod, strlen(mod),
3615 &mod_g.search, &exclude_mod);
3616 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003617 }
3618
Steven Rostedt52baf112009-02-14 01:15:39 -05003619 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003620
3621 if (unlikely(ftrace_disabled))
3622 goto out_unlock;
3623
Steven Rostedt265c8312009-02-13 12:43:56 -05003624 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003625
3626 if (rec->flags & FTRACE_FL_DISABLED)
3627 continue;
3628
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003629 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003630 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003631 if (ret < 0) {
3632 found = ret;
3633 goto out_unlock;
3634 }
Li Zefan311d16d2009-12-08 11:15:11 +08003635 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003636 }
3637 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003638 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003639 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003640
3641 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003642}
3643
Steven Rostedt64e7c442009-02-13 17:08:48 -05003644static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003645ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003646{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003647 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003648}
3649
Steven Rostedt64e7c442009-02-13 17:08:48 -05003650
Steven Rostedtf6180772009-02-14 00:40:25 -05003651/*
3652 * We register the module command as a template to show others how
3653 * to register the a command as well.
3654 */
3655
3656static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003657ftrace_mod_callback(struct ftrace_hash *hash,
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003658 char *func, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003659{
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003660 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003661
3662 /*
3663 * cmd == 'mod' because we only registered this func
3664 * for the 'mod' ftrace_func_command.
3665 * But if you register one func with multiple commands,
3666 * you can tell which command was used by the cmd
3667 * parameter.
3668 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003669 ret = match_records(hash, func, strlen(func), module);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003670 if (!ret)
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003671 return -EINVAL;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003672 if (ret < 0)
3673 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003674 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003675}
3676
3677static struct ftrace_func_command ftrace_mod_cmd = {
3678 .name = "mod",
3679 .func = ftrace_mod_callback,
3680};
3681
3682static int __init ftrace_mod_cmd_init(void)
3683{
3684 return register_ftrace_command(&ftrace_mod_cmd);
3685}
Steven Rostedt6f415672012-10-05 12:13:07 -04003686core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003687
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003688static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003689 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003690{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003691 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003692 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003693 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003694
3695 key = hash_long(ip, FTRACE_HASH_BITS);
3696
3697 hhd = &ftrace_func_hash[key];
3698
3699 if (hlist_empty(hhd))
3700 return;
3701
3702 /*
3703 * Disable preemption for these calls to prevent a RCU grace
3704 * period. This syncs the hash iteration and freeing of items
3705 * on the hash. rcu_read_lock is too dangerous here.
3706 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003707 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003708 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003709 if (entry->ip == ip)
3710 entry->ops->func(ip, parent_ip, &entry->data);
3711 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003712 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003713}
3714
Steven Rostedtb6887d72009-02-17 12:32:04 -05003715static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003716{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003717 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003718 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003719 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003720};
3721
Steven Rostedtb6887d72009-02-17 12:32:04 -05003722static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003723
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003724static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003725{
Steven Rostedtb8489142011-05-04 09:27:52 -04003726 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003727 int i;
3728
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003729 if (ftrace_probe_registered) {
3730 /* still need to update the function call sites */
3731 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003732 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3733 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003734 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003735 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003736
3737 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3738 struct hlist_head *hhd = &ftrace_func_hash[i];
3739 if (hhd->first)
3740 break;
3741 }
3742 /* Nothing registered? */
3743 if (i == FTRACE_FUNC_HASHSIZE)
3744 return;
3745
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003746 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003747
Steven Rostedtb6887d72009-02-17 12:32:04 -05003748 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003749}
3750
Steven Rostedtb6887d72009-02-17 12:32:04 -05003751static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003752{
3753 int i;
3754
Steven Rostedtb6887d72009-02-17 12:32:04 -05003755 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003756 return;
3757
3758 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3759 struct hlist_head *hhd = &ftrace_func_hash[i];
3760 if (hhd->first)
3761 return;
3762 }
3763
3764 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003765 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003766
Steven Rostedtb6887d72009-02-17 12:32:04 -05003767 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003768}
3769
3770
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003771static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003772{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003773 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003774 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003775 kfree(entry);
3776}
3777
Steven Rostedt59df055f2009-02-14 15:29:06 -05003778int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003779register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003780 void *data)
3781{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003782 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003783 struct ftrace_func_probe *entry;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003784 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003785 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003786 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003787 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003788 struct ftrace_page *pg;
3789 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003790 int not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003791 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003792 int count = 0;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003793 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003794
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003795 func_g.type = filter_parse_regex(glob, strlen(glob),
3796 &func_g.search, &not);
3797 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003798
Steven Rostedtb6887d72009-02-17 12:32:04 -05003799 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003800 if (WARN_ON(not))
3801 return -EINVAL;
3802
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003803 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003804
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003805 old_hash_ops.filter_hash = old_hash;
3806 /* Probes only have filters */
3807 old_hash_ops.notrace_hash = NULL;
3808
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003809 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003810 if (!hash) {
3811 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003812 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003813 }
3814
3815 if (unlikely(ftrace_disabled)) {
3816 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003817 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003818 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003819
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003820 mutex_lock(&ftrace_lock);
3821
Steven Rostedt59df055f2009-02-14 15:29:06 -05003822 do_for_each_ftrace_rec(pg, rec) {
3823
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003824 if (rec->flags & FTRACE_FL_DISABLED)
3825 continue;
3826
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003827 if (!ftrace_match_record(rec, &func_g, NULL, 0))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003828 continue;
3829
3830 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3831 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003832 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003833 if (!count)
3834 count = -ENOMEM;
3835 goto out_unlock;
3836 }
3837
3838 count++;
3839
3840 entry->data = data;
3841
3842 /*
3843 * The caller might want to do something special
3844 * for each function we find. We call the callback
3845 * to give the caller an opportunity to do so.
3846 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003847 if (ops->init) {
3848 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003849 /* caller does not like this func */
3850 kfree(entry);
3851 continue;
3852 }
3853 }
3854
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003855 ret = enter_record(hash, rec, 0);
3856 if (ret < 0) {
3857 kfree(entry);
3858 count = ret;
3859 goto out_unlock;
3860 }
3861
Steven Rostedt59df055f2009-02-14 15:29:06 -05003862 entry->ops = ops;
3863 entry->ip = rec->ip;
3864
3865 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3866 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3867
3868 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003869
3870 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003871
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003872 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003873
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003874 if (!ret)
3875 free_ftrace_hash_rcu(old_hash);
3876 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003877 count = ret;
3878
Steven Rostedt59df055f2009-02-14 15:29:06 -05003879 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003880 mutex_unlock(&ftrace_lock);
3881 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003882 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003883 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003884
3885 return count;
3886}
3887
3888enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003889 PROBE_TEST_FUNC = 1,
3890 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003891};
3892
3893static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003894__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003895 void *data, int flags)
3896{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003897 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003898 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003899 struct ftrace_func_probe *p;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003900 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003901 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003902 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003903 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003904 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003905 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003906 char str[KSYM_SYMBOL_LEN];
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003907 int i, ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003908
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003909 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003910 func_g.search = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003911 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003912 int not;
3913
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003914 func_g.type = filter_parse_regex(glob, strlen(glob),
3915 &func_g.search, &not);
3916 func_g.len = strlen(func_g.search);
3917 func_g.search = glob;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003918
Steven Rostedtb6887d72009-02-17 12:32:04 -05003919 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003920 if (WARN_ON(not))
3921 return;
3922 }
3923
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003924 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003925
3926 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3927 if (!hash)
3928 /* Hmm, should report this somehow */
3929 goto out_unlock;
3930
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003931 INIT_LIST_HEAD(&free_list);
3932
Steven Rostedt59df055f2009-02-14 15:29:06 -05003933 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3934 struct hlist_head *hhd = &ftrace_func_hash[i];
3935
Sasha Levinb67bfe02013-02-27 17:06:00 -08003936 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003937
3938 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003939 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003940 continue;
3941
Steven Rostedtb6887d72009-02-17 12:32:04 -05003942 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003943 continue;
3944
3945 /* do this last, since it is the most expensive */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003946 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003947 kallsyms_lookup(entry->ip, NULL, NULL,
3948 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003949 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003950 continue;
3951 }
3952
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003953 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3954 /* It is possible more than one entry had this ip */
3955 if (rec_entry)
3956 free_hash_entry(hash, rec_entry);
3957
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003958 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003959 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003960 }
3961 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003962 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003963 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003964 /*
3965 * Remove after the disable is called. Otherwise, if the last
3966 * probe is removed, a null hash means *all enabled*.
3967 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003968 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003969 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003970 if (!ret)
3971 free_ftrace_hash_rcu(old_hash);
3972
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003973 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3974 list_del(&entry->free_list);
3975 ftrace_free_entry(entry);
3976 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003977 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003978
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003979 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003980 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003981 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003982}
3983
3984void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003985unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003986 void *data)
3987{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003988 __unregister_ftrace_function_probe(glob, ops, data,
3989 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003990}
3991
3992void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003993unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003994{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003995 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003996}
3997
Steven Rostedtb6887d72009-02-17 12:32:04 -05003998void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003999{
Steven Rostedtb6887d72009-02-17 12:32:04 -05004000 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004001}
4002
Steven Rostedtf6180772009-02-14 00:40:25 -05004003static LIST_HEAD(ftrace_commands);
4004static DEFINE_MUTEX(ftrace_cmd_mutex);
4005
Tom Zanussi38de93a2013-10-24 08:34:18 -05004006/*
4007 * Currently we only register ftrace commands from __init, so mark this
4008 * __init too.
4009 */
4010__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004011{
4012 struct ftrace_func_command *p;
4013 int ret = 0;
4014
4015 mutex_lock(&ftrace_cmd_mutex);
4016 list_for_each_entry(p, &ftrace_commands, list) {
4017 if (strcmp(cmd->name, p->name) == 0) {
4018 ret = -EBUSY;
4019 goto out_unlock;
4020 }
4021 }
4022 list_add(&cmd->list, &ftrace_commands);
4023 out_unlock:
4024 mutex_unlock(&ftrace_cmd_mutex);
4025
4026 return ret;
4027}
4028
Tom Zanussi38de93a2013-10-24 08:34:18 -05004029/*
4030 * Currently we only unregister ftrace commands from __init, so mark
4031 * this __init too.
4032 */
4033__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004034{
4035 struct ftrace_func_command *p, *n;
4036 int ret = -ENODEV;
4037
4038 mutex_lock(&ftrace_cmd_mutex);
4039 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4040 if (strcmp(cmd->name, p->name) == 0) {
4041 ret = 0;
4042 list_del_init(&p->list);
4043 goto out_unlock;
4044 }
4045 }
4046 out_unlock:
4047 mutex_unlock(&ftrace_cmd_mutex);
4048
4049 return ret;
4050}
4051
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004052static int ftrace_process_regex(struct ftrace_hash *hash,
4053 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004054{
Steven Rostedtf6180772009-02-14 00:40:25 -05004055 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004056 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004057 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004058
4059 func = strsep(&next, ":");
4060
4061 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004062 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004063 if (!ret)
4064 ret = -EINVAL;
4065 if (ret < 0)
4066 return ret;
4067 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004068 }
4069
Steven Rostedtf6180772009-02-14 00:40:25 -05004070 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004071
4072 command = strsep(&next, ":");
4073
Steven Rostedtf6180772009-02-14 00:40:25 -05004074 mutex_lock(&ftrace_cmd_mutex);
4075 list_for_each_entry(p, &ftrace_commands, list) {
4076 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04004077 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004078 goto out_unlock;
4079 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004080 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004081 out_unlock:
4082 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004083
Steven Rostedtf6180772009-02-14 00:40:25 -05004084 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004085}
4086
Ingo Molnare309b412008-05-12 21:20:51 +02004087static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004088ftrace_regex_write(struct file *file, const char __user *ubuf,
4089 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004090{
4091 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004092 struct trace_parser *parser;
4093 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004094
Li Zefan4ba79782009-09-22 13:52:20 +08004095 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004096 return 0;
4097
Steven Rostedt5072c592008-05-12 21:20:43 +02004098 if (file->f_mode & FMODE_READ) {
4099 struct seq_file *m = file->private_data;
4100 iter = m->private;
4101 } else
4102 iter = file->private_data;
4103
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004104 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004105 return -ENODEV;
4106
4107 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004108
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004109 parser = &iter->parser;
4110 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004111
Li Zefan4ba79782009-09-22 13:52:20 +08004112 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004113 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004114 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004115 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004116 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004117 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004118 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004119 }
4120
Steven Rostedt5072c592008-05-12 21:20:43 +02004121 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004122 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004123 return ret;
4124}
4125
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004126ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004127ftrace_filter_write(struct file *file, const char __user *ubuf,
4128 size_t cnt, loff_t *ppos)
4129{
4130 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4131}
4132
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004133ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004134ftrace_notrace_write(struct file *file, const char __user *ubuf,
4135 size_t cnt, loff_t *ppos)
4136{
4137 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4138}
4139
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004140static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004141ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4142{
4143 struct ftrace_func_entry *entry;
4144
4145 if (!ftrace_location(ip))
4146 return -EINVAL;
4147
4148 if (remove) {
4149 entry = ftrace_lookup_ip(hash, ip);
4150 if (!entry)
4151 return -ENOENT;
4152 free_hash_entry(hash, entry);
4153 return 0;
4154 }
4155
4156 return add_hash_entry(hash, ip);
4157}
4158
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004159static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004160 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004161{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004162 struct ftrace_ops *op;
4163
4164 if (!ftrace_enabled)
4165 return;
4166
4167 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004168 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004169 return;
4170 }
4171
4172 /*
4173 * If this is the shared global_ops filter, then we need to
4174 * check if there is another ops that shares it, is enabled.
4175 * If so, we still need to run the modify code.
4176 */
4177 if (ops->func_hash != &global_ops.local_hash)
4178 return;
4179
4180 do_for_each_ftrace_op(op, ftrace_ops_list) {
4181 if (op->func_hash == &global_ops.local_hash &&
4182 op->flags & FTRACE_OPS_FL_ENABLED) {
4183 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4184 /* Only need to do this once */
4185 return;
4186 }
4187 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004188}
4189
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004190static int
4191ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4192 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004193{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004194 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004195 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004196 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004197 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004198 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004199
Steven Rostedt41c52c02008-05-22 11:46:33 -04004200 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004201 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004202
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004203 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004204
Steven Rostedtf45948e2011-05-02 12:29:25 -04004205 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004206 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004207 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004208 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004209
Wang Nanb972cc52014-07-15 08:40:20 +08004210 if (reset)
4211 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4212 else
4213 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4214
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004215 if (!hash) {
4216 ret = -ENOMEM;
4217 goto out_regex_unlock;
4218 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004219
Jiri Olsaac483c42012-01-02 10:04:14 +01004220 if (buf && !ftrace_match_records(hash, buf, len)) {
4221 ret = -EINVAL;
4222 goto out_regex_unlock;
4223 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004224 if (ip) {
4225 ret = ftrace_match_addr(hash, ip, remove);
4226 if (ret < 0)
4227 goto out_regex_unlock;
4228 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004229
4230 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004231 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004232 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4233 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004234 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004235 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004236 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004237 free_ftrace_hash_rcu(old_hash);
4238 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004239 mutex_unlock(&ftrace_lock);
4240
Jiri Olsaac483c42012-01-02 10:04:14 +01004241 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004242 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004243
4244 free_ftrace_hash(hash);
4245 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004246}
4247
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004248static int
4249ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4250 int reset, int enable)
4251{
4252 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4253}
4254
4255/**
4256 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4257 * @ops - the ops to set the filter with
4258 * @ip - the address to add to or remove from the filter.
4259 * @remove - non zero to remove the ip from the filter
4260 * @reset - non zero to reset all filters before applying this filter.
4261 *
4262 * Filters denote which functions should be enabled when tracing is enabled
4263 * If @ip is NULL, it failes to update filter.
4264 */
4265int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4266 int remove, int reset)
4267{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004268 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004269 return ftrace_set_addr(ops, ip, remove, reset, 1);
4270}
4271EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4272
Joel Fernandesd032ae82016-11-15 12:31:20 -08004273/**
4274 * ftrace_ops_set_global_filter - setup ops to use global filters
4275 * @ops - the ops which will use the global filters
4276 *
4277 * ftrace users who need global function trace filtering should call this.
4278 * It can set the global filter only if ops were not initialized before.
4279 */
4280void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4281{
4282 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4283 return;
4284
4285 ftrace_ops_init(ops);
4286 ops->func_hash = &global_ops.local_hash;
4287}
4288EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4289
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004290static int
4291ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4292 int reset, int enable)
4293{
4294 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4295}
4296
Steven Rostedt77a2b372008-05-12 21:20:45 +02004297/**
4298 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004299 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004300 * @buf - the string that holds the function filter text.
4301 * @len - the length of the string.
4302 * @reset - non zero to reset all filters before applying this filter.
4303 *
4304 * Filters denote which functions should be enabled when tracing is enabled.
4305 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4306 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004307int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004308 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004309{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004310 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004311 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004312}
Steven Rostedt936e0742011-05-05 22:54:01 -04004313EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004314
Steven Rostedt41c52c02008-05-22 11:46:33 -04004315/**
4316 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004317 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004318 * @buf - the string that holds the function notrace text.
4319 * @len - the length of the string.
4320 * @reset - non zero to reset all filters before applying this filter.
4321 *
4322 * Notrace Filters denote which functions should not be enabled when tracing
4323 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4324 * for tracing.
4325 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004326int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004327 int len, int reset)
4328{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004329 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004330 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004331}
4332EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4333/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004334 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004335 * @buf - the string that holds the function filter text.
4336 * @len - the length of the string.
4337 * @reset - non zero to reset all filters before applying this filter.
4338 *
4339 * Filters denote which functions should be enabled when tracing is enabled.
4340 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4341 */
4342void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4343{
4344 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4345}
4346EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4347
4348/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004349 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004350 * @buf - the string that holds the function notrace text.
4351 * @len - the length of the string.
4352 * @reset - non zero to reset all filters before applying this filter.
4353 *
4354 * Notrace Filters denote which functions should not be enabled when tracing
4355 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4356 * for tracing.
4357 */
4358void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004359{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004360 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004361}
Steven Rostedt936e0742011-05-05 22:54:01 -04004362EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004363
Steven Rostedt2af15d62009-05-28 13:37:24 -04004364/*
4365 * command line interface to allow users to set filters on boot up.
4366 */
4367#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4368static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4369static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4370
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004371/* Used by function selftest to not test if filter is set */
4372bool ftrace_filter_param __initdata;
4373
Steven Rostedt2af15d62009-05-28 13:37:24 -04004374static int __init set_ftrace_notrace(char *str)
4375{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004376 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004377 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004378 return 1;
4379}
4380__setup("ftrace_notrace=", set_ftrace_notrace);
4381
4382static int __init set_ftrace_filter(char *str)
4383{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004384 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004385 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004386 return 1;
4387}
4388__setup("ftrace_filter=", set_ftrace_filter);
4389
Stefan Assmann369bc182009-10-12 22:17:21 +02004390#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004391static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004392static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004393static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004394
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004395static unsigned long save_global_trampoline;
4396static unsigned long save_global_flags;
4397
Stefan Assmann369bc182009-10-12 22:17:21 +02004398static int __init set_graph_function(char *str)
4399{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004400 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004401 return 1;
4402}
4403__setup("ftrace_graph_filter=", set_graph_function);
4404
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004405static int __init set_graph_notrace_function(char *str)
4406{
4407 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4408 return 1;
4409}
4410__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4411
4412static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004413{
4414 int ret;
4415 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004416 unsigned long *table = ftrace_graph_funcs;
4417 int *count = &ftrace_graph_count;
4418
4419 if (!enable) {
4420 table = ftrace_graph_notrace_funcs;
4421 count = &ftrace_graph_notrace_count;
4422 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004423
4424 while (buf) {
4425 func = strsep(&buf, ",");
4426 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004427 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004428 if (ret)
4429 printk(KERN_DEBUG "ftrace: function %s not "
4430 "traceable\n", func);
4431 }
4432}
4433#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4434
Steven Rostedt2a85a372011-12-19 21:57:44 -05004435void __init
4436ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004437{
4438 char *func;
4439
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004440 ftrace_ops_init(ops);
4441
Steven Rostedt2af15d62009-05-28 13:37:24 -04004442 while (buf) {
4443 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004444 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004445 }
4446}
4447
4448static void __init set_ftrace_early_filters(void)
4449{
4450 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004451 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004452 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004453 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004454#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4455 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004456 set_ftrace_early_graph(ftrace_graph_buf, 1);
4457 if (ftrace_graph_notrace_buf[0])
4458 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004459#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004460}
4461
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004462int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004463{
4464 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004465 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004466 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004467 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004468 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004469 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004470 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004471 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004472
Steven Rostedt5072c592008-05-12 21:20:43 +02004473 if (file->f_mode & FMODE_READ) {
4474 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004475 seq_release(inode, file);
4476 } else
4477 iter = file->private_data;
4478
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004479 parser = &iter->parser;
4480 if (trace_parser_loaded(parser)) {
4481 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004482 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004483 }
4484
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004485 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004486
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004487 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004488
Steven Rostedt058e2972011-04-29 22:35:33 -04004489 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004490 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4491
4492 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004493 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004494 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004495 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004496
Steven Rostedt058e2972011-04-29 22:35:33 -04004497 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004498 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004499 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4500 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004501 ret = ftrace_hash_move(iter->ops, filter_hash,
4502 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004503 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004504 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004505 free_ftrace_hash_rcu(old_hash);
4506 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004507 mutex_unlock(&ftrace_lock);
4508 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004509
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004510 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004511 free_ftrace_hash(iter->hash);
4512 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004513
Steven Rostedt5072c592008-05-12 21:20:43 +02004514 return 0;
4515}
4516
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004517static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004518 .open = ftrace_avail_open,
4519 .read = seq_read,
4520 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004521 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004522};
4523
Steven Rostedt647bcd02011-05-03 14:39:21 -04004524static const struct file_operations ftrace_enabled_fops = {
4525 .open = ftrace_enabled_open,
4526 .read = seq_read,
4527 .llseek = seq_lseek,
4528 .release = seq_release_private,
4529};
4530
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004531static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004532 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004533 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004534 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004535 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004536 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004537};
4538
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004539static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004540 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004541 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004542 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004543 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004544 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004545};
4546
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004547#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4548
4549static DEFINE_MUTEX(graph_lock);
4550
4551int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004552int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004553unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004554unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004555
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004556struct ftrace_graph_data {
4557 unsigned long *table;
4558 size_t size;
4559 int *count;
4560 const struct seq_operations *seq_ops;
4561};
4562
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004563static void *
Li Zefan85951842009-06-24 09:54:00 +08004564__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004565{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004566 struct ftrace_graph_data *fgd = m->private;
4567
4568 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004569 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004570 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004571}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004572
Li Zefan85951842009-06-24 09:54:00 +08004573static void *
4574g_next(struct seq_file *m, void *v, loff_t *pos)
4575{
4576 (*pos)++;
4577 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004578}
4579
4580static void *g_start(struct seq_file *m, loff_t *pos)
4581{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004582 struct ftrace_graph_data *fgd = m->private;
4583
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004584 mutex_lock(&graph_lock);
4585
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004586 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004587 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004588 return (void *)1;
4589
Li Zefan85951842009-06-24 09:54:00 +08004590 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004591}
4592
4593static void g_stop(struct seq_file *m, void *p)
4594{
4595 mutex_unlock(&graph_lock);
4596}
4597
4598static int g_show(struct seq_file *m, void *v)
4599{
4600 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004601
4602 if (!ptr)
4603 return 0;
4604
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004605 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004606 struct ftrace_graph_data *fgd = m->private;
4607
4608 if (fgd->table == ftrace_graph_funcs)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004609 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004610 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004611 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004612 return 0;
4613 }
4614
Steven Rostedtb375a112009-09-17 00:05:58 -04004615 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004616
4617 return 0;
4618}
4619
James Morris88e9d342009-09-22 16:43:43 -07004620static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004621 .start = g_start,
4622 .next = g_next,
4623 .stop = g_stop,
4624 .show = g_show,
4625};
4626
4627static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004628__ftrace_graph_open(struct inode *inode, struct file *file,
4629 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004630{
4631 int ret = 0;
4632
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004633 mutex_lock(&graph_lock);
4634 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004635 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004636 *fgd->count = 0;
4637 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004638 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004639 mutex_unlock(&graph_lock);
4640
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004641 if (file->f_mode & FMODE_READ) {
4642 ret = seq_open(file, fgd->seq_ops);
4643 if (!ret) {
4644 struct seq_file *m = file->private_data;
4645 m->private = fgd;
4646 }
4647 } else
4648 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004649
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004650 return ret;
4651}
4652
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004653static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004654ftrace_graph_open(struct inode *inode, struct file *file)
4655{
4656 struct ftrace_graph_data *fgd;
4657
4658 if (unlikely(ftrace_disabled))
4659 return -ENODEV;
4660
4661 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4662 if (fgd == NULL)
4663 return -ENOMEM;
4664
4665 fgd->table = ftrace_graph_funcs;
4666 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4667 fgd->count = &ftrace_graph_count;
4668 fgd->seq_ops = &ftrace_graph_seq_ops;
4669
4670 return __ftrace_graph_open(inode, file, fgd);
4671}
4672
4673static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004674ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4675{
4676 struct ftrace_graph_data *fgd;
4677
4678 if (unlikely(ftrace_disabled))
4679 return -ENODEV;
4680
4681 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4682 if (fgd == NULL)
4683 return -ENOMEM;
4684
4685 fgd->table = ftrace_graph_notrace_funcs;
4686 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4687 fgd->count = &ftrace_graph_notrace_count;
4688 fgd->seq_ops = &ftrace_graph_seq_ops;
4689
4690 return __ftrace_graph_open(inode, file, fgd);
4691}
4692
4693static int
Li Zefan87827112009-07-23 11:29:11 +08004694ftrace_graph_release(struct inode *inode, struct file *file)
4695{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004696 if (file->f_mode & FMODE_READ) {
4697 struct seq_file *m = file->private_data;
4698
4699 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004700 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004701 } else {
4702 kfree(file->private_data);
4703 }
4704
Li Zefan87827112009-07-23 11:29:11 +08004705 return 0;
4706}
4707
4708static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004709ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004710{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004711 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004712 struct dyn_ftrace *rec;
4713 struct ftrace_page *pg;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004714 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004715 int not;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004716 bool exists;
4717 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004718
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004719 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004720 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4721 &func_g.search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004722 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004723 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004724
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004725 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004726
Steven Rostedt52baf112009-02-14 01:15:39 -05004727 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004728
4729 if (unlikely(ftrace_disabled)) {
4730 mutex_unlock(&ftrace_lock);
4731 return -ENODEV;
4732 }
4733
Steven Rostedt265c8312009-02-13 12:43:56 -05004734 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004735
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004736 if (rec->flags & FTRACE_FL_DISABLED)
4737 continue;
4738
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004739 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004740 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004741 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004742 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004743 if (array[i] == rec->ip) {
4744 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004745 break;
4746 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004747 }
4748
4749 if (!not) {
4750 fail = 0;
4751 if (!exists) {
4752 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004753 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004754 goto out;
4755 }
4756 } else {
4757 if (exists) {
4758 array[i] = array[--(*idx)];
4759 array[*idx] = 0;
4760 fail = 0;
4761 }
4762 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004763 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004764 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004765out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004766 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004767
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004768 if (fail)
4769 return -EINVAL;
4770
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004771 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004772}
4773
4774static ssize_t
4775ftrace_graph_write(struct file *file, const char __user *ubuf,
4776 size_t cnt, loff_t *ppos)
4777{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004778 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004779 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004780 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004781
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004782 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004783 return 0;
4784
Namhyung Kim6a101082013-10-14 17:24:25 +09004785 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4786 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004787
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004788 read = trace_get_user(&parser, ubuf, cnt, ppos);
4789
Li Zefan4ba79782009-09-22 13:52:20 +08004790 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004791 parser.buffer[parser.idx] = 0;
4792
Namhyung Kim6a101082013-10-14 17:24:25 +09004793 mutex_lock(&graph_lock);
4794
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004795 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004796 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4797 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004798
4799 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004800 }
4801
Namhyung Kim6a101082013-10-14 17:24:25 +09004802 if (!ret)
4803 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004804
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004805 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004806
4807 return ret;
4808}
4809
4810static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004811 .open = ftrace_graph_open,
4812 .read = seq_read,
4813 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004814 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004815 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004816};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004817
4818static const struct file_operations ftrace_graph_notrace_fops = {
4819 .open = ftrace_graph_notrace_open,
4820 .read = seq_read,
4821 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004822 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004823 .release = ftrace_graph_release,
4824};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004825#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4826
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004827void ftrace_create_filter_files(struct ftrace_ops *ops,
4828 struct dentry *parent)
4829{
4830
4831 trace_create_file("set_ftrace_filter", 0644, parent,
4832 ops, &ftrace_filter_fops);
4833
4834 trace_create_file("set_ftrace_notrace", 0644, parent,
4835 ops, &ftrace_notrace_fops);
4836}
4837
4838/*
4839 * The name "destroy_filter_files" is really a misnomer. Although
4840 * in the future, it may actualy delete the files, but this is
4841 * really intended to make sure the ops passed in are disabled
4842 * and that when this function returns, the caller is free to
4843 * free the ops.
4844 *
4845 * The "destroy" name is only to match the "create" name that this
4846 * should be paired with.
4847 */
4848void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4849{
4850 mutex_lock(&ftrace_lock);
4851 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4852 ftrace_shutdown(ops, 0);
4853 ops->flags |= FTRACE_OPS_FL_DELETED;
4854 mutex_unlock(&ftrace_lock);
4855}
4856
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05004857static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004858{
Steven Rostedt5072c592008-05-12 21:20:43 +02004859
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004860 trace_create_file("available_filter_functions", 0444,
4861 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004862
Steven Rostedt647bcd02011-05-03 14:39:21 -04004863 trace_create_file("enabled_functions", 0444,
4864 d_tracer, NULL, &ftrace_enabled_fops);
4865
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004866 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004867
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004868#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004869 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004870 NULL,
4871 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004872 trace_create_file("set_graph_notrace", 0444, d_tracer,
4873 NULL,
4874 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004875#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4876
Steven Rostedt5072c592008-05-12 21:20:43 +02004877 return 0;
4878}
4879
Steven Rostedt9fd49322012-04-24 22:32:06 -04004880static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004881{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004882 const unsigned long *ipa = a;
4883 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004884
Steven Rostedt9fd49322012-04-24 22:32:06 -04004885 if (*ipa > *ipb)
4886 return 1;
4887 if (*ipa < *ipb)
4888 return -1;
4889 return 0;
4890}
4891
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004892static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004893 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004894 unsigned long *end)
4895{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004896 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004897 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004898 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004899 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004900 unsigned long *p;
4901 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004902 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004903 int ret = -ENOMEM;
4904
4905 count = end - start;
4906
4907 if (!count)
4908 return 0;
4909
Steven Rostedt9fd49322012-04-24 22:32:06 -04004910 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02004911 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04004912
Steven Rostedt706c81f2012-04-24 23:45:26 -04004913 start_pg = ftrace_allocate_pages(count);
4914 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004915 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004916
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004917 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004918
Steven Rostedt320823092011-12-16 14:42:37 -05004919 /*
4920 * Core and each module needs their own pages, as
4921 * modules will free them when they are removed.
4922 * Force a new page to be allocated for modules.
4923 */
Steven Rostedta7900872011-12-16 16:23:44 -05004924 if (!mod) {
4925 WARN_ON(ftrace_pages || ftrace_pages_start);
4926 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004927 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004928 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004929 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004930 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004931
Steven Rostedta7900872011-12-16 16:23:44 -05004932 if (WARN_ON(ftrace_pages->next)) {
4933 /* Hmm, we have free pages? */
4934 while (ftrace_pages->next)
4935 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004936 }
Steven Rostedta7900872011-12-16 16:23:44 -05004937
Steven Rostedt706c81f2012-04-24 23:45:26 -04004938 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004939 }
4940
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004941 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004942 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004943 while (p < end) {
4944 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004945 /*
4946 * Some architecture linkers will pad between
4947 * the different mcount_loc sections of different
4948 * object files to satisfy alignments.
4949 * Skip any NULL pointers.
4950 */
4951 if (!addr)
4952 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004953
4954 if (pg->index == pg->size) {
4955 /* We should have allocated enough */
4956 if (WARN_ON(!pg->next))
4957 break;
4958 pg = pg->next;
4959 }
4960
4961 rec = &pg->records[pg->index++];
4962 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004963 }
4964
Steven Rostedt706c81f2012-04-24 23:45:26 -04004965 /* We should have used all pages */
4966 WARN_ON(pg->next);
4967
4968 /* Assign the last page to ftrace_pages */
4969 ftrace_pages = pg;
4970
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004971 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004972 * We only need to disable interrupts on start up
4973 * because we are modifying code that an interrupt
4974 * may execute, and the modification is not atomic.
4975 * But for modules, nothing runs the code we modify
4976 * until we are finished with it, and there's no
4977 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004978 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004979 if (!mod)
4980 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004981 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004982 if (!mod)
4983 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004984 ret = 0;
4985 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004986 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004987
Steven Rostedta7900872011-12-16 16:23:44 -05004988 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004989}
4990
Steven Rostedt93eb6772009-04-15 13:24:06 -04004991#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004992
4993#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4994
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05004995static int referenced_filters(struct dyn_ftrace *rec)
4996{
4997 struct ftrace_ops *ops;
4998 int cnt = 0;
4999
5000 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5001 if (ops_references_rec(ops, rec))
5002 cnt++;
5003 }
5004
5005 return cnt;
5006}
5007
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005008void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005009{
5010 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005011 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005012 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005013 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005014
Steven Rostedt93eb6772009-04-15 13:24:06 -04005015 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005016
5017 if (ftrace_disabled)
5018 goto out_unlock;
5019
Steven Rostedt320823092011-12-16 14:42:37 -05005020 /*
5021 * Each module has its own ftrace_pages, remove
5022 * them from the list.
5023 */
5024 last_pg = &ftrace_pages_start;
5025 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5026 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005027 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005028 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005029 * As core pages are first, the first
5030 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005031 */
Steven Rostedt320823092011-12-16 14:42:37 -05005032 if (WARN_ON(pg == ftrace_pages_start))
5033 goto out_unlock;
5034
5035 /* Check if we are deleting the last page */
5036 if (pg == ftrace_pages)
5037 ftrace_pages = next_to_ftrace_page(last_pg);
5038
5039 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05005040 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5041 free_pages((unsigned long)pg->records, order);
5042 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05005043 } else
5044 last_pg = &pg->next;
5045 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005046 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005047 mutex_unlock(&ftrace_lock);
5048}
5049
Jessica Yu7dcd1822016-02-16 17:32:33 -05005050void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005051{
5052 struct dyn_ftrace *rec;
5053 struct ftrace_page *pg;
5054
5055 mutex_lock(&ftrace_lock);
5056
5057 if (ftrace_disabled)
5058 goto out_unlock;
5059
5060 /*
5061 * If the tracing is enabled, go ahead and enable the record.
5062 *
5063 * The reason not to enable the record immediatelly is the
5064 * inherent check of ftrace_make_nop/ftrace_make_call for
5065 * correct previous instructions. Making first the NOP
5066 * conversion puts the module to the correct state, thus
5067 * passing the ftrace_make_call check.
5068 *
5069 * We also delay this to after the module code already set the
5070 * text to read-only, as we now need to set it back to read-write
5071 * so that we can modify the text.
5072 */
5073 if (ftrace_start_up)
5074 ftrace_arch_code_modify_prepare();
5075
5076 do_for_each_ftrace_rec(pg, rec) {
5077 int cnt;
5078 /*
5079 * do_for_each_ftrace_rec() is a double loop.
5080 * module text shares the pg. If a record is
5081 * not part of this module, then skip this pg,
5082 * which the "break" will do.
5083 */
5084 if (!within_module_core(rec->ip, mod))
5085 break;
5086
5087 cnt = 0;
5088
5089 /*
5090 * When adding a module, we need to check if tracers are
5091 * currently enabled and if they are, and can trace this record,
5092 * we need to enable the module functions as well as update the
5093 * reference counts for those function records.
5094 */
5095 if (ftrace_start_up)
5096 cnt += referenced_filters(rec);
5097
5098 /* This clears FTRACE_FL_DISABLED */
5099 rec->flags = cnt;
5100
5101 if (ftrace_start_up && cnt) {
5102 int failed = __ftrace_replace_code(rec, 1);
5103 if (failed) {
5104 ftrace_bug(failed, rec);
5105 goto out_loop;
5106 }
5107 }
5108
5109 } while_for_each_ftrace_rec();
5110
5111 out_loop:
5112 if (ftrace_start_up)
5113 ftrace_arch_code_modify_post_process();
5114
5115 out_unlock:
5116 mutex_unlock(&ftrace_lock);
5117}
5118
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005119void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005120{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005121 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005122 return;
5123
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005124 ftrace_process_locs(mod, mod->ftrace_callsites,
5125 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005126}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005127#endif /* CONFIG_MODULES */
5128
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005129void __init ftrace_init(void)
5130{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005131 extern unsigned long __start_mcount_loc[];
5132 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005133 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005134 int ret;
5135
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005136 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005137 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005138 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01005139 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005140 goto failed;
5141
5142 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005143 if (!count) {
5144 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005145 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005146 }
5147
5148 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5149 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005150
5151 last_ftrace_enabled = ftrace_enabled = 1;
5152
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005153 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005154 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005155 __stop_mcount_loc);
5156
Steven Rostedt2af15d62009-05-28 13:37:24 -04005157 set_ftrace_early_filters();
5158
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005159 return;
5160 failed:
5161 ftrace_disabled = 1;
5162}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005163
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005164/* Do nothing if arch does not support this */
5165void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5166{
5167}
5168
5169static void ftrace_update_trampoline(struct ftrace_ops *ops)
5170{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005171
5172/*
5173 * Currently there's no safe way to free a trampoline when the kernel
5174 * is configured with PREEMPT. That is because a task could be preempted
5175 * when it jumped to the trampoline, it may be preempted for a long time
5176 * depending on the system load, and currently there's no way to know
5177 * when it will be off the trampoline. If the trampoline is freed
5178 * too early, when the task runs again, it will be executing on freed
5179 * memory and crash.
5180 */
5181#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005182 /* Currently, only non dynamic ops can have a trampoline */
5183 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5184 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005185#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005186
5187 arch_ftrace_update_trampoline(ops);
5188}
5189
Steven Rostedt3d083392008-05-12 21:20:42 +02005190#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005191
Steven Rostedt2b499382011-05-03 22:49:52 -04005192static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005193 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005194 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5195 FTRACE_OPS_FL_INITIALIZED |
5196 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005197};
5198
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005199static int __init ftrace_nodyn_init(void)
5200{
5201 ftrace_enabled = 1;
5202 return 0;
5203}
Steven Rostedt6f415672012-10-05 12:13:07 -04005204core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005205
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005206static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005207static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005208static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005209/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005210# define ftrace_startup(ops, command) \
5211 ({ \
5212 int ___ret = __register_ftrace_function(ops); \
5213 if (!___ret) \
5214 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5215 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005216 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005217# define ftrace_shutdown(ops, command) \
5218 ({ \
5219 int ___ret = __unregister_ftrace_function(ops); \
5220 if (!___ret) \
5221 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5222 ___ret; \
5223 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005224
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005225# define ftrace_startup_sysctl() do { } while (0)
5226# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005227
5228static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005229ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005230{
5231 return 1;
5232}
5233
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005234static void ftrace_update_trampoline(struct ftrace_ops *ops)
5235{
5236}
5237
Steven Rostedt3d083392008-05-12 21:20:42 +02005238#endif /* CONFIG_DYNAMIC_FTRACE */
5239
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005240__init void ftrace_init_global_array_ops(struct trace_array *tr)
5241{
5242 tr->ops = &global_ops;
5243 tr->ops->private = tr;
5244}
5245
5246void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5247{
5248 /* If we filter on pids, update to use the pid function */
5249 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5250 if (WARN_ON(tr->ops->func != ftrace_stub))
5251 printk("ftrace ops had %pS for function\n",
5252 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005253 }
5254 tr->ops->func = func;
5255 tr->ops->private = tr;
5256}
5257
5258void ftrace_reset_array_ops(struct trace_array *tr)
5259{
5260 tr->ops->func = ftrace_stub;
5261}
5262
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005263static inline void
5264__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005265 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005266{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005267 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005268 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005269
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005270 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5271 if (bit < 0)
5272 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005273
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005274 /*
5275 * Some of the ops may be dynamically allocated,
5276 * they must be freed after a synchronize_sched().
5277 */
5278 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005279
Steven Rostedt0a016402012-11-02 17:03:03 -04005280 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005281 /*
5282 * Check the following for each ops before calling their func:
5283 * if RCU flag is set, then rcu_is_watching() must be true
5284 * if PER_CPU is set, then ftrace_function_local_disable()
5285 * must be false
5286 * Otherwise test if the ip matches the ops filter
5287 *
5288 * If any of the above fails then the op->func() is not executed.
5289 */
5290 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5291 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5292 !ftrace_function_local_disabled(op)) &&
5293 ftrace_ops_test(op, ip, regs)) {
5294
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005295 if (FTRACE_WARN_ON(!op->func)) {
5296 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005297 goto out;
5298 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005299 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005300 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005301 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005302out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005303 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005304 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005305}
5306
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005307/*
5308 * Some archs only support passing ip and parent_ip. Even though
5309 * the list function ignores the op parameter, we do not want any
5310 * C side effects, where a function is called without the caller
5311 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005312 * Archs are to support both the regs and ftrace_ops at the same time.
5313 * If they support ftrace_ops, it is assumed they support regs.
5314 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005315 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5316 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005317 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08005318 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005319 */
5320#if ARCH_SUPPORTS_FTRACE_OPS
5321static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005322 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005323{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005324 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005325}
5326#else
5327static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5328{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005329 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005330}
5331#endif
5332
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005333/*
5334 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005335 * recursion, needs RCU protection and/or requires per cpu handling, then
5336 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005337 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005338static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005339 struct ftrace_ops *op, struct pt_regs *regs)
5340{
5341 int bit;
5342
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005343 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5344 return;
5345
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005346 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5347 if (bit < 0)
5348 return;
5349
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005350 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005351
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005352 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5353 !ftrace_function_local_disabled(op)) {
5354 op->func(ip, parent_ip, op, regs);
5355 }
5356
5357 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005358 trace_clear_recursion(bit);
5359}
5360
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005361/**
5362 * ftrace_ops_get_func - get the function a trampoline should call
5363 * @ops: the ops to get the function for
5364 *
5365 * Normally the mcount trampoline will call the ops->func, but there
5366 * are times that it should not. For example, if the ops does not
5367 * have its own recursion protection, then it should call the
5368 * ftrace_ops_recurs_func() instead.
5369 *
5370 * Returns the function that the trampoline should call for @ops.
5371 */
5372ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5373{
5374 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005375 * If the function does not handle recursion, needs to be RCU safe,
5376 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005377 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005378 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5379 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5380 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005381
5382 return ops->func;
5383}
5384
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005385static void
5386ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5387 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05005388{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005389 struct trace_array *tr = data;
5390 struct trace_pid_list *pid_list;
5391
5392 pid_list = rcu_dereference_sched(tr->function_pids);
5393
5394 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5395 trace_ignore_this_task(pid_list, next));
5396}
5397
5398static void clear_ftrace_pids(struct trace_array *tr)
5399{
5400 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05005401 int cpu;
5402
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005403 pid_list = rcu_dereference_protected(tr->function_pids,
5404 lockdep_is_held(&ftrace_lock));
5405 if (!pid_list)
5406 return;
5407
5408 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5409
5410 for_each_possible_cpu(cpu)
5411 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5412
5413 rcu_assign_pointer(tr->function_pids, NULL);
5414
5415 /* Wait till all users are no longer using pid filtering */
5416 synchronize_sched();
5417
5418 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05005419}
5420
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005421static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05005422{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005423 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005424 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005425
5426 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005427 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005428
5429 mutex_unlock(&ftrace_lock);
5430}
5431
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005432/* Greater than any max PID */
5433#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5434
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005435static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005436 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005437{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005438 struct trace_pid_list *pid_list;
5439 struct trace_array *tr = m->private;
5440
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005441 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005442 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005443
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005444 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005445
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005446 if (!pid_list)
5447 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5448
5449 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005450}
5451
5452static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5453{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005454 struct trace_array *tr = m->private;
5455 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5456
5457 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005458 return NULL;
5459
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005460 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005461}
5462
5463static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005464 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005465{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005466 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005467 mutex_unlock(&ftrace_lock);
5468}
5469
5470static int fpid_show(struct seq_file *m, void *v)
5471{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005472 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005473 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005474 return 0;
5475 }
5476
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005477 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005478}
5479
5480static const struct seq_operations ftrace_pid_sops = {
5481 .start = fpid_start,
5482 .next = fpid_next,
5483 .stop = fpid_stop,
5484 .show = fpid_show,
5485};
5486
5487static int
5488ftrace_pid_open(struct inode *inode, struct file *file)
5489{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005490 struct trace_array *tr = inode->i_private;
5491 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005492 int ret = 0;
5493
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005494 if (trace_array_get(tr) < 0)
5495 return -ENODEV;
5496
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005497 if ((file->f_mode & FMODE_WRITE) &&
5498 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005499 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005500
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005501 ret = seq_open(file, &ftrace_pid_sops);
5502 if (ret < 0) {
5503 trace_array_put(tr);
5504 } else {
5505 m = file->private_data;
5506 /* copy tr over to seq ops */
5507 m->private = tr;
5508 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005509
5510 return ret;
5511}
5512
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005513static void ignore_task_cpu(void *data)
5514{
5515 struct trace_array *tr = data;
5516 struct trace_pid_list *pid_list;
5517
5518 /*
5519 * This function is called by on_each_cpu() while the
5520 * event_mutex is held.
5521 */
5522 pid_list = rcu_dereference_protected(tr->function_pids,
5523 mutex_is_locked(&ftrace_lock));
5524
5525 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5526 trace_ignore_this_task(pid_list, current));
5527}
5528
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005529static ssize_t
5530ftrace_pid_write(struct file *filp, const char __user *ubuf,
5531 size_t cnt, loff_t *ppos)
5532{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005533 struct seq_file *m = filp->private_data;
5534 struct trace_array *tr = m->private;
5535 struct trace_pid_list *filtered_pids = NULL;
5536 struct trace_pid_list *pid_list;
5537 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005538
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005539 if (!cnt)
5540 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005541
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005542 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005543
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005544 filtered_pids = rcu_dereference_protected(tr->function_pids,
5545 lockdep_is_held(&ftrace_lock));
5546
5547 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5548 if (ret < 0)
5549 goto out;
5550
5551 rcu_assign_pointer(tr->function_pids, pid_list);
5552
5553 if (filtered_pids) {
5554 synchronize_sched();
5555 trace_free_pid_list(filtered_pids);
5556 } else if (pid_list) {
5557 /* Register a probe to set whether to ignore the tracing of a task */
5558 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5559 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005560
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005561 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005562 * Ignoring of pids is done at task switch. But we have to
5563 * check for those tasks that are currently running.
5564 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005565 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005566 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005567
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005568 ftrace_update_pid_func();
5569 ftrace_startup_all(0);
5570 out:
5571 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005572
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005573 if (ret > 0)
5574 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05005575
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005576 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005577}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005578
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005579static int
5580ftrace_pid_release(struct inode *inode, struct file *file)
5581{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005582 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005583
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005584 trace_array_put(tr);
5585
5586 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005587}
5588
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005589static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005590 .open = ftrace_pid_open,
5591 .write = ftrace_pid_write,
5592 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005593 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005594 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005595};
5596
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005597void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005598{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005599 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005600 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005601}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005602
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04005603void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5604 struct dentry *d_tracer)
5605{
5606 /* Only the top level directory has the dyn_tracefs and profile */
5607 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5608
5609 ftrace_init_dyn_tracefs(d_tracer);
5610 ftrace_profile_tracefs(d_tracer);
5611}
5612
Steven Rostedt3d083392008-05-12 21:20:42 +02005613/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005614 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005615 *
5616 * This function should be used by panic code. It stops ftrace
5617 * but in a not so nice way. If you need to simply kill ftrace
5618 * from a non-atomic section, use ftrace_kill.
5619 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005620void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005621{
5622 ftrace_disabled = 1;
5623 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005624 clear_ftrace_function();
5625}
5626
5627/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005628 * Test if ftrace is dead or not.
5629 */
5630int ftrace_is_dead(void)
5631{
5632 return ftrace_disabled;
5633}
5634
5635/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005636 * register_ftrace_function - register a function for profiling
5637 * @ops - ops structure that holds the function for profiling.
5638 *
5639 * Register a function to be called by all functions in the
5640 * kernel.
5641 *
5642 * Note: @ops->func and all the functions it calls must be labeled
5643 * with "notrace", otherwise it will go into a
5644 * recursive loop.
5645 */
5646int register_ftrace_function(struct ftrace_ops *ops)
5647{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005648 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005649
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005650 ftrace_ops_init(ops);
5651
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005652 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005653
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005654 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005655
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005656 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005657
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005658 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005659}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005660EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005661
5662/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005663 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005664 * @ops - ops structure that holds the function to unregister
5665 *
5666 * Unregister a function that was added to be called by ftrace profiling.
5667 */
5668int unregister_ftrace_function(struct ftrace_ops *ops)
5669{
5670 int ret;
5671
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005672 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005673 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005674 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005675
5676 return ret;
5677}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005678EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005679
Ingo Molnare309b412008-05-12 21:20:51 +02005680int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005681ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005682 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005683 loff_t *ppos)
5684{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005685 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005686
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005687 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005688
Steven Rostedt45a4a232011-04-21 23:16:46 -04005689 if (unlikely(ftrace_disabled))
5690 goto out;
5691
5692 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005693
Li Zefana32c7762009-06-26 16:55:51 +08005694 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005695 goto out;
5696
Li Zefana32c7762009-06-26 16:55:51 +08005697 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005698
5699 if (ftrace_enabled) {
5700
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005701 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005702 if (ftrace_ops_list != &ftrace_list_end)
5703 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005704
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05005705 ftrace_startup_sysctl();
5706
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005707 } else {
5708 /* stopping ftrace calls (just send to ftrace_stub) */
5709 ftrace_trace_function = ftrace_stub;
5710
5711 ftrace_shutdown_sysctl();
5712 }
5713
5714 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005715 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005716 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005717}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005718
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005719#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005720
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005721static struct ftrace_ops graph_ops = {
5722 .func = ftrace_stub,
5723 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5724 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005725 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005726 FTRACE_OPS_FL_STUB,
5727#ifdef FTRACE_GRAPH_TRAMP_ADDR
5728 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05005729 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005730#endif
5731 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5732};
5733
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005734void ftrace_graph_sleep_time_control(bool enable)
5735{
5736 fgraph_sleep_time = enable;
5737}
5738
5739void ftrace_graph_graph_time_control(bool enable)
5740{
5741 fgraph_graph_time = enable;
5742}
5743
Steven Rostedte49dc192008-12-02 23:50:05 -05005744int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5745{
5746 return 0;
5747}
5748
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005749/* The callbacks that hook a function */
5750trace_func_graph_ret_t ftrace_graph_return =
5751 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005752trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005753static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005754
5755/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5756static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5757{
5758 int i;
5759 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005760 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5761 struct task_struct *g, *t;
5762
5763 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5764 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5765 * sizeof(struct ftrace_ret_stack),
5766 GFP_KERNEL);
5767 if (!ret_stack_list[i]) {
5768 start = 0;
5769 end = i;
5770 ret = -ENOMEM;
5771 goto free;
5772 }
5773 }
5774
Soumya PN6112a302016-05-17 21:31:14 +05305775 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005776 do_each_thread(g, t) {
5777 if (start == end) {
5778 ret = -EAGAIN;
5779 goto unlock;
5780 }
5781
5782 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005783 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005784 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005785 t->curr_ret_stack = -1;
5786 /* Make sure the tasks see the -1 first: */
5787 smp_wmb();
5788 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005789 }
5790 } while_each_thread(g, t);
5791
5792unlock:
Soumya PN6112a302016-05-17 21:31:14 +05305793 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005794free:
5795 for (i = start; i < end; i++)
5796 kfree(ret_stack_list[i]);
5797 return ret;
5798}
5799
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005800static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02005801ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04005802 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005803{
5804 unsigned long long timestamp;
5805 int index;
5806
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005807 /*
5808 * Does the user want to count the time a function was asleep.
5809 * If so, do not update the time stamps.
5810 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005811 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005812 return;
5813
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005814 timestamp = trace_clock_local();
5815
5816 prev->ftrace_timestamp = timestamp;
5817
5818 /* only process tasks that we timestamped */
5819 if (!next->ftrace_timestamp)
5820 return;
5821
5822 /*
5823 * Update all the counters in next to make up for the
5824 * time next was sleeping.
5825 */
5826 timestamp -= next->ftrace_timestamp;
5827
5828 for (index = next->curr_ret_stack; index >= 0; index--)
5829 next->ret_stack[index].calltime += timestamp;
5830}
5831
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005832/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005833static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005834{
5835 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005836 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005837
5838 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5839 sizeof(struct ftrace_ret_stack *),
5840 GFP_KERNEL);
5841
5842 if (!ret_stack_list)
5843 return -ENOMEM;
5844
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005845 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005846 for_each_online_cpu(cpu) {
5847 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005848 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005849 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005850
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005851 do {
5852 ret = alloc_retstack_tasklist(ret_stack_list);
5853 } while (ret == -EAGAIN);
5854
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005855 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005856 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005857 if (ret)
5858 pr_info("ftrace_graph: Couldn't activate tracepoint"
5859 " probe to kernel_sched_switch\n");
5860 }
5861
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005862 kfree(ret_stack_list);
5863 return ret;
5864}
5865
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005866/*
5867 * Hibernation protection.
5868 * The state of the current task is too much unstable during
5869 * suspend/restore to disk. We want to protect against that.
5870 */
5871static int
5872ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5873 void *unused)
5874{
5875 switch (state) {
5876 case PM_HIBERNATION_PREPARE:
5877 pause_graph_tracing();
5878 break;
5879
5880 case PM_POST_HIBERNATION:
5881 unpause_graph_tracing();
5882 break;
5883 }
5884 return NOTIFY_DONE;
5885}
5886
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005887static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5888{
5889 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5890 return 0;
5891 return __ftrace_graph_entry(trace);
5892}
5893
5894/*
5895 * The function graph tracer should only trace the functions defined
5896 * by set_ftrace_filter and set_ftrace_notrace. If another function
5897 * tracer ops is registered, the graph tracer requires testing the
5898 * function against the global ops, and not just trace any function
5899 * that any ftrace_ops registered.
5900 */
5901static void update_function_graph_func(void)
5902{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005903 struct ftrace_ops *op;
5904 bool do_test = false;
5905
5906 /*
5907 * The graph and global ops share the same set of functions
5908 * to test. If any other ops is on the list, then
5909 * the graph tracing needs to test if its the function
5910 * it should call.
5911 */
5912 do_for_each_ftrace_op(op, ftrace_ops_list) {
5913 if (op != &global_ops && op != &graph_ops &&
5914 op != &ftrace_list_end) {
5915 do_test = true;
5916 /* in double loop, break out with goto */
5917 goto out;
5918 }
5919 } while_for_each_ftrace_op(op);
5920 out:
5921 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005922 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005923 else
5924 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005925}
5926
Mathias Krause8275f692014-03-30 15:31:50 +02005927static struct notifier_block ftrace_suspend_notifier = {
5928 .notifier_call = ftrace_suspend_notifier_call,
5929};
5930
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005931int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5932 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005933{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005934 int ret = 0;
5935
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005936 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005937
Steven Rostedt05ce5812009-03-24 00:18:31 -04005938 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005939 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005940 ret = -EBUSY;
5941 goto out;
5942 }
5943
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005944 register_pm_notifier(&ftrace_suspend_notifier);
5945
Steven Rostedt597af812009-04-03 15:24:12 -04005946 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005947 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005948 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005949 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005950 goto out;
5951 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005952
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005953 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005954
5955 /*
5956 * Update the indirect function to the entryfunc, and the
5957 * function that gets called to the entry_test first. Then
5958 * call the update fgraph entry function to determine if
5959 * the entryfunc should be called directly or not.
5960 */
5961 __ftrace_graph_entry = entryfunc;
5962 ftrace_graph_entry = ftrace_graph_entry_test;
5963 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005964
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005965 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005966out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005967 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005968 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005969}
5970
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005971void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005972{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005973 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005974
Steven Rostedt597af812009-04-03 15:24:12 -04005975 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005976 goto out;
5977
Steven Rostedt597af812009-04-03 15:24:12 -04005978 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005979 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005980 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005981 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005982 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005983 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005984 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005985
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005986#ifdef CONFIG_DYNAMIC_FTRACE
5987 /*
5988 * Function graph does not allocate the trampoline, but
5989 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5990 * if one was used.
5991 */
5992 global_ops.trampoline = save_global_trampoline;
5993 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5994 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5995#endif
5996
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005997 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005998 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005999}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006000
Steven Rostedt868baf02011-02-10 21:26:13 -05006001static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6002
6003static void
6004graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6005{
6006 atomic_set(&t->tracing_graph_pause, 0);
6007 atomic_set(&t->trace_overrun, 0);
6008 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006009 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05006010 smp_wmb();
6011 t->ret_stack = ret_stack;
6012}
6013
6014/*
6015 * Allocate a return stack for the idle task. May be the first
6016 * time through, or it may be done by CPU hotplug online.
6017 */
6018void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6019{
6020 t->curr_ret_stack = -1;
6021 /*
6022 * The idle task has no parent, it either has its own
6023 * stack or no stack at all.
6024 */
6025 if (t->ret_stack)
6026 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6027
6028 if (ftrace_graph_active) {
6029 struct ftrace_ret_stack *ret_stack;
6030
6031 ret_stack = per_cpu(idle_ret_stack, cpu);
6032 if (!ret_stack) {
6033 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6034 * sizeof(struct ftrace_ret_stack),
6035 GFP_KERNEL);
6036 if (!ret_stack)
6037 return;
6038 per_cpu(idle_ret_stack, cpu) = ret_stack;
6039 }
6040 graph_init_task(t, ret_stack);
6041 }
6042}
6043
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006044/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006045void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006046{
Steven Rostedt84047e32009-06-02 16:51:55 -04006047 /* Make sure we do not use the parent ret_stack */
6048 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05006049 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04006050
Steven Rostedt597af812009-04-03 15:24:12 -04006051 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04006052 struct ftrace_ret_stack *ret_stack;
6053
6054 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006055 * sizeof(struct ftrace_ret_stack),
6056 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04006057 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006058 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05006059 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04006060 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006061}
6062
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006063void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006064{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006065 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6066
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006067 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006068 /* NULL must become visible to IRQs before we free it: */
6069 barrier();
6070
6071 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006072}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006073#endif