blob: ea208e93f000b5595e9834b8d2e0bb94cc0659b9 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
Ingo Molnar29930022017-02-08 18:51:36 +010018#include <linux/sched/task.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020019#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020020#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080021#include <linux/suspend.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050022#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020023#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010024#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020025#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050026#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040027#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010028#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020029#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020031#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050032#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020033#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050034#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080035#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020036
Steven Rostedtad8d75f2009-04-14 19:39:12 -040037#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040038
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -040039#include <asm/sections.h>
Steven Rostedt2af15d62009-05-28 13:37:24 -040040#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053041
Steven Rostedt0706f1c2009-03-23 23:12:58 -040042#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040043#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020044
Steven Rostedt6912896e2008-10-23 09:33:03 -040045#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040046 ({ \
47 int ___r = cond; \
48 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040049 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040050 ___r; \
51 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040052
53#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040054 ({ \
55 int ___r = cond; \
56 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040057 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040058 ___r; \
59 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040060
Steven Rostedt8fc0c702009-02-16 15:28:00 -050061/* hash bits for specific function selection */
62#define FTRACE_HASH_BITS 7
63#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040064#define FTRACE_HASH_DEFAULT_BITS 10
65#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040068#define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040071#define ASSIGN_OPS_HASH(opsname, val) \
72 .func_hash = val, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090074#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040075#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040076#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090077#endif
78
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040079static struct ftrace_ops ftrace_list_end __read_mostly = {
80 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040081 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040082 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040083};
84
Steven Rostedt4eebcc82008-05-12 21:20:48 +020085/* ftrace_enabled is a method to turn ftrace on or off */
86int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020087static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020088
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040089/* Current function tracing op */
90struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050091/* What to set function_trace_op to */
92static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050093
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040094static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040095{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040096 struct trace_array *tr;
97
98 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
99 return false;
100
101 tr = ops->private;
102
103 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400104}
105
106static void ftrace_update_trampoline(struct ftrace_ops *ops);
107
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200108/*
109 * ftrace_disabled is set when an anomaly is discovered.
110 * ftrace_disabled is much stronger than ftrace_enabled.
111 */
112static int ftrace_disabled __read_mostly;
113
Steven Rostedt52baf112009-02-14 01:15:39 -0500114static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200115
Steven Rostedtb8489142011-05-04 09:27:52 -0400116static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400118static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200119
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400120#if ARCH_SUPPORTS_FTRACE_OPS
121static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400122 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400123#else
124/* See comment below, where ftrace_ops_list_func is defined */
125static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
126#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
127#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400128
Steven Rostedt0a016402012-11-02 17:03:03 -0400129/*
130 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400131 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400132 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400133 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400134 * concurrent insertions into the ftrace_global_list.
135 *
136 * Silly Alpha and silly pointer-speculation compiler optimizations!
137 */
138#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400139 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400140 do
141
142/*
143 * Optimized for just a single item in the list (as that is the normal case).
144 */
145#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400146 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400147 unlikely((op) != &ftrace_list_end))
148
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900149static inline void ftrace_ops_init(struct ftrace_ops *ops)
150{
151#ifdef CONFIG_DYNAMIC_FTRACE
152 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400153 mutex_init(&ops->local_hash.regex_lock);
154 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900155 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
156 }
157#endif
158}
159
Steven Rostedtea701f12012-07-20 13:08:05 -0400160/**
161 * ftrace_nr_registered_ops - return number of ops registered
162 *
163 * Returns the number of ftrace_ops registered and tracing functions
164 */
165int ftrace_nr_registered_ops(void)
166{
167 struct ftrace_ops *ops;
168 int cnt = 0;
169
170 mutex_lock(&ftrace_lock);
171
172 for (ops = ftrace_ops_list;
173 ops != &ftrace_list_end; ops = ops->next)
174 cnt++;
175
176 mutex_unlock(&ftrace_lock);
177
178 return cnt;
179}
180
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400181static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400182 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500183{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400184 struct trace_array *tr = op->private;
185
186 if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500187 return;
188
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400189 op->saved_func(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500190}
191
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200192/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200193 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200194 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200195 * This NULLs the ftrace function and in essence stops
196 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200197 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200198void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200199{
Steven Rostedt3d083392008-05-12 21:20:42 +0200200 ftrace_trace_function = ftrace_stub;
201}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200202
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500203static void per_cpu_ops_disable_all(struct ftrace_ops *ops)
Jiri Olsae2484912012-02-15 15:51:48 +0100204{
205 int cpu;
206
207 for_each_possible_cpu(cpu)
208 *per_cpu_ptr(ops->disabled, cpu) = 1;
209}
210
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500211static int per_cpu_ops_alloc(struct ftrace_ops *ops)
Jiri Olsae2484912012-02-15 15:51:48 +0100212{
213 int __percpu *disabled;
214
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500215 if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
216 return -EINVAL;
217
Jiri Olsae2484912012-02-15 15:51:48 +0100218 disabled = alloc_percpu(int);
219 if (!disabled)
220 return -ENOMEM;
221
222 ops->disabled = disabled;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500223 per_cpu_ops_disable_all(ops);
Jiri Olsae2484912012-02-15 15:51:48 +0100224 return 0;
225}
226
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500227static void ftrace_sync(struct work_struct *work)
228{
229 /*
230 * This function is just a stub to implement a hard force
231 * of synchronize_sched(). This requires synchronizing
232 * tasks even in userspace and idle.
233 *
234 * Yes, function tracing is rude.
235 */
236}
237
238static void ftrace_sync_ipi(void *data)
239{
240 /* Probably not needed, but do it anyway */
241 smp_rmb();
242}
243
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500244#ifdef CONFIG_FUNCTION_GRAPH_TRACER
245static void update_function_graph_func(void);
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400246
247/* Both enabled by default (can be cleared by function_graph tracer flags */
248static bool fgraph_sleep_time = true;
249static bool fgraph_graph_time = true;
250
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500251#else
252static inline void update_function_graph_func(void) { }
253#endif
254
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100255
256static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
257{
258 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500259 * 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 +0100260 * then it needs to call the list anyway.
261 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500262 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU |
263 FTRACE_OPS_FL_RCU) || FTRACE_FORCE_LIST_FUNC)
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100264 return ftrace_ops_list_func;
265
266 return ftrace_ops_get_func(ops);
267}
268
Steven Rostedt2b499382011-05-03 22:49:52 -0400269static void update_ftrace_function(void)
270{
271 ftrace_func_t func;
272
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400273 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400274 * Prepare the ftrace_ops that the arch callback will use.
275 * If there's only one ftrace_ops registered, the ftrace_ops_list
276 * will point to the ops we want.
277 */
278 set_function_trace_op = ftrace_ops_list;
279
280 /* If there's no ftrace_ops registered, just call the stub function */
281 if (ftrace_ops_list == &ftrace_list_end) {
282 func = ftrace_stub;
283
284 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400285 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400286 * recursion safe and not dynamic and the arch supports passing ops,
287 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400288 */
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400289 } else if (ftrace_ops_list->next == &ftrace_list_end) {
Steven Rostedt (Red Hat)00ccbf22015-02-19 15:56:14 +0100290 func = ftrace_ops_get_list_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400291
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400292 } else {
293 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500294 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400295 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400296 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400297
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400298 update_function_graph_func();
299
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500300 /* If there's no change, then do nothing more here */
301 if (ftrace_trace_function == func)
302 return;
303
304 /*
305 * If we are using the list function, it doesn't care
306 * about the function_trace_ops.
307 */
308 if (func == ftrace_ops_list_func) {
309 ftrace_trace_function = func;
310 /*
311 * Don't even bother setting function_trace_ops,
312 * it would be racy to do so anyway.
313 */
314 return;
315 }
316
317#ifndef CONFIG_DYNAMIC_FTRACE
318 /*
319 * For static tracing, we need to be a bit more careful.
320 * The function change takes affect immediately. Thus,
321 * we need to coorditate the setting of the function_trace_ops
322 * with the setting of the ftrace_trace_function.
323 *
324 * Set the function to the list ops, which will call the
325 * function we want, albeit indirectly, but it handles the
326 * ftrace_ops and doesn't depend on function_trace_op.
327 */
328 ftrace_trace_function = ftrace_ops_list_func;
329 /*
330 * Make sure all CPUs see this. Yes this is slow, but static
331 * tracing is slow and nasty to have enabled.
332 */
333 schedule_on_each_cpu(ftrace_sync);
334 /* Now all cpus are using the list ops. */
335 function_trace_op = set_function_trace_op;
336 /* Make sure the function_trace_op is visible on all CPUs */
337 smp_wmb();
338 /* Nasty way to force a rmb on all cpus */
339 smp_call_function(ftrace_sync_ipi, NULL, 1);
340 /* OK, we are all set to update the ftrace_trace_function now! */
341#endif /* !CONFIG_DYNAMIC_FTRACE */
342
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400343 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400344}
345
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800346int using_ftrace_ops_list_func(void)
347{
348 return ftrace_trace_function == ftrace_ops_list_func;
349}
350
Steven Rostedt2b499382011-05-03 22:49:52 -0400351static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200352{
Steven Rostedt2b499382011-05-03 22:49:52 -0400353 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200354 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400355 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200356 * CPU might be walking that list. We need to make sure
357 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400358 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200359 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400360 rcu_assign_pointer(*list, ops);
361}
Steven Rostedt3d083392008-05-12 21:20:42 +0200362
Steven Rostedt2b499382011-05-03 22:49:52 -0400363static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
364{
365 struct ftrace_ops **p;
366
367 /*
368 * If we are removing the last function, then simply point
369 * to the ftrace_stub.
370 */
371 if (*list == ops && ops->next == &ftrace_list_end) {
372 *list = &ftrace_list_end;
373 return 0;
374 }
375
376 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
377 if (*p == ops)
378 break;
379
380 if (*p != ops)
381 return -1;
382
383 *p = (*p)->next;
384 return 0;
385}
386
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400387static void ftrace_update_trampoline(struct ftrace_ops *ops);
388
Steven Rostedt2b499382011-05-03 22:49:52 -0400389static int __register_ftrace_function(struct ftrace_ops *ops)
390{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500391 if (ops->flags & FTRACE_OPS_FL_DELETED)
392 return -EINVAL;
393
Steven Rostedtb8489142011-05-04 09:27:52 -0400394 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
395 return -EBUSY;
396
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900397#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400398 /*
399 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
400 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
401 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
402 */
403 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
404 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
405 return -EINVAL;
406
407 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
408 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
409#endif
410
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400411 if (!core_kernel_data((unsigned long)ops))
412 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
413
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500414 if (ops->flags & FTRACE_OPS_FL_PER_CPU) {
415 if (per_cpu_ops_alloc(ops))
Jiri Olsae2484912012-02-15 15:51:48 +0100416 return -ENOMEM;
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500417 }
418
419 add_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400420
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400421 /* Always save the function, and reset at unregistering */
422 ops->saved_func = ops->func;
423
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400424 if (ftrace_pids_enabled(ops))
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400425 ops->func = ftrace_pid_func;
426
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400427 ftrace_update_trampoline(ops);
428
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400429 if (ftrace_enabled)
430 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200431
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200432 return 0;
433}
434
Ingo Molnare309b412008-05-12 21:20:51 +0200435static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200436{
Steven Rostedt2b499382011-05-03 22:49:52 -0400437 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200438
Steven Rostedtb8489142011-05-04 09:27:52 -0400439 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
440 return -EBUSY;
441
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -0500442 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400443
Steven Rostedt2b499382011-05-03 22:49:52 -0400444 if (ret < 0)
445 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400446
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400447 if (ftrace_enabled)
448 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200449
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400450 ops->func = ops->saved_func;
451
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500452 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200453}
454
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500455static void ftrace_update_pid_func(void)
456{
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400457 struct ftrace_ops *op;
458
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400459 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500460 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900461 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500462
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400463 do_for_each_ftrace_op(op, ftrace_ops_list) {
464 if (op->flags & FTRACE_OPS_FL_PID) {
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400465 op->func = ftrace_pids_enabled(op) ?
466 ftrace_pid_func : op->saved_func;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400467 ftrace_update_trampoline(op);
468 }
469 } while_for_each_ftrace_op(op);
470
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400471 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500472}
473
Steven Rostedt493762f2009-03-23 17:12:36 -0400474#ifdef CONFIG_FUNCTION_PROFILER
475struct ftrace_profile {
476 struct hlist_node node;
477 unsigned long ip;
478 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400479#ifdef CONFIG_FUNCTION_GRAPH_TRACER
480 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400481 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400482#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400483};
484
485struct ftrace_profile_page {
486 struct ftrace_profile_page *next;
487 unsigned long index;
488 struct ftrace_profile records[];
489};
490
Steven Rostedtcafb1682009-03-24 20:50:39 -0400491struct ftrace_profile_stat {
492 atomic_t disabled;
493 struct hlist_head *hash;
494 struct ftrace_profile_page *pages;
495 struct ftrace_profile_page *start;
496 struct tracer_stat stat;
497};
498
Steven Rostedt493762f2009-03-23 17:12:36 -0400499#define PROFILE_RECORDS_SIZE \
500 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
501
502#define PROFILES_PER_PAGE \
503 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
504
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400505static int ftrace_profile_enabled __read_mostly;
506
507/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400508static DEFINE_MUTEX(ftrace_profile_lock);
509
Steven Rostedtcafb1682009-03-24 20:50:39 -0400510static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400511
Namhyung Kim20079eb2013-04-10 08:55:50 +0900512#define FTRACE_PROFILE_HASH_BITS 10
513#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400514
Steven Rostedt493762f2009-03-23 17:12:36 -0400515static void *
516function_stat_next(void *v, int idx)
517{
518 struct ftrace_profile *rec = v;
519 struct ftrace_profile_page *pg;
520
521 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
522
523 again:
Li Zefan0296e422009-06-26 11:15:37 +0800524 if (idx != 0)
525 rec++;
526
Steven Rostedt493762f2009-03-23 17:12:36 -0400527 if ((void *)rec >= (void *)&pg->records[pg->index]) {
528 pg = pg->next;
529 if (!pg)
530 return NULL;
531 rec = &pg->records[0];
532 if (!rec->counter)
533 goto again;
534 }
535
536 return rec;
537}
538
539static void *function_stat_start(struct tracer_stat *trace)
540{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400541 struct ftrace_profile_stat *stat =
542 container_of(trace, struct ftrace_profile_stat, stat);
543
544 if (!stat || !stat->start)
545 return NULL;
546
547 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400548}
549
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400550#ifdef CONFIG_FUNCTION_GRAPH_TRACER
551/* function graph compares on total time */
552static int function_stat_cmp(void *p1, void *p2)
553{
554 struct ftrace_profile *a = p1;
555 struct ftrace_profile *b = p2;
556
557 if (a->time < b->time)
558 return -1;
559 if (a->time > b->time)
560 return 1;
561 else
562 return 0;
563}
564#else
565/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400566static int function_stat_cmp(void *p1, void *p2)
567{
568 struct ftrace_profile *a = p1;
569 struct ftrace_profile *b = p2;
570
571 if (a->counter < b->counter)
572 return -1;
573 if (a->counter > b->counter)
574 return 1;
575 else
576 return 0;
577}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400578#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400579
580static int function_stat_headers(struct seq_file *m)
581{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400582#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100583 seq_puts(m, " Function "
584 "Hit Time Avg s^2\n"
585 " -------- "
586 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400587#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100588 seq_puts(m, " Function Hit\n"
589 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400590#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400591 return 0;
592}
593
594static int function_stat_show(struct seq_file *m, void *v)
595{
596 struct ftrace_profile *rec = v;
597 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800598 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400599#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400600 static struct trace_seq s;
601 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400602 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400603#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800604 mutex_lock(&ftrace_profile_lock);
605
606 /* we raced with function_profile_reset() */
607 if (unlikely(rec->counter == 0)) {
608 ret = -EBUSY;
609 goto out;
610 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400611
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530612#ifdef CONFIG_FUNCTION_GRAPH_TRACER
613 avg = rec->time;
614 do_div(avg, rec->counter);
615 if (tracing_thresh && (avg < tracing_thresh))
616 goto out;
617#endif
618
Steven Rostedt493762f2009-03-23 17:12:36 -0400619 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400620 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400621
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400622#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100623 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400624
Chase Douglase330b3b2010-04-26 14:02:05 -0400625 /* Sample standard deviation (s^2) */
626 if (rec->counter <= 1)
627 stddev = 0;
628 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200629 /*
630 * Apply Welford's method:
631 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
632 */
633 stddev = rec->counter * rec->time_squared -
634 rec->time * rec->time;
635
Chase Douglase330b3b2010-04-26 14:02:05 -0400636 /*
637 * Divide only 1000 for ns^2 -> us^2 conversion.
638 * trace_print_graph_duration will divide 1000 again.
639 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200640 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400641 }
642
Steven Rostedt34886c82009-03-25 21:00:47 -0400643 trace_seq_init(&s);
644 trace_print_graph_duration(rec->time, &s);
645 trace_seq_puts(&s, " ");
646 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400647 trace_seq_puts(&s, " ");
648 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400649 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400650#endif
651 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800652out:
653 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400654
Li Zefan3aaba202010-08-23 16:50:12 +0800655 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400656}
657
Steven Rostedtcafb1682009-03-24 20:50:39 -0400658static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400659{
660 struct ftrace_profile_page *pg;
661
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400663
664 while (pg) {
665 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
666 pg->index = 0;
667 pg = pg->next;
668 }
669
Steven Rostedtcafb1682009-03-24 20:50:39 -0400670 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400671 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
672}
673
Steven Rostedtcafb1682009-03-24 20:50:39 -0400674int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400675{
676 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400677 int functions;
678 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400679 int i;
680
681 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400682 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400683 return 0;
684
Steven Rostedtcafb1682009-03-24 20:50:39 -0400685 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
686 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400687 return -ENOMEM;
688
Steven Rostedt318e0a72009-03-25 20:06:34 -0400689#ifdef CONFIG_DYNAMIC_FTRACE
690 functions = ftrace_update_tot_cnt;
691#else
692 /*
693 * We do not know the number of functions that exist because
694 * dynamic tracing is what counts them. With past experience
695 * we have around 20K functions. That should be more than enough.
696 * It is highly unlikely we will execute every function in
697 * the kernel.
698 */
699 functions = 20000;
700#endif
701
Steven Rostedtcafb1682009-03-24 20:50:39 -0400702 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400703
Steven Rostedt318e0a72009-03-25 20:06:34 -0400704 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
705
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900706 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400707 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400708 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400709 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400710 pg = pg->next;
711 }
712
713 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400714
715 out_free:
716 pg = stat->start;
717 while (pg) {
718 unsigned long tmp = (unsigned long)pg;
719
720 pg = pg->next;
721 free_page(tmp);
722 }
723
Steven Rostedt318e0a72009-03-25 20:06:34 -0400724 stat->pages = NULL;
725 stat->start = NULL;
726
727 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400728}
729
Steven Rostedtcafb1682009-03-24 20:50:39 -0400730static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400731{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400732 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400733 int size;
734
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 stat = &per_cpu(ftrace_profile_stats, cpu);
736
737 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400738 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400740 return 0;
741 }
742
743 /*
744 * We are profiling all functions, but usually only a few thousand
745 * functions are hit. We'll make a hash of 1024 items.
746 */
747 size = FTRACE_PROFILE_HASH_SIZE;
748
Steven Rostedtcafb1682009-03-24 20:50:39 -0400749 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400750
Steven Rostedtcafb1682009-03-24 20:50:39 -0400751 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400752 return -ENOMEM;
753
Steven Rostedt318e0a72009-03-25 20:06:34 -0400754 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400755 if (ftrace_profile_pages_init(stat) < 0) {
756 kfree(stat->hash);
757 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400758 return -ENOMEM;
759 }
760
761 return 0;
762}
763
Steven Rostedtcafb1682009-03-24 20:50:39 -0400764static int ftrace_profile_init(void)
765{
766 int cpu;
767 int ret = 0;
768
Miao Xiec4602c12013-12-16 15:20:01 +0800769 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770 ret = ftrace_profile_init_cpu(cpu);
771 if (ret)
772 break;
773 }
774
775 return ret;
776}
777
Steven Rostedt493762f2009-03-23 17:12:36 -0400778/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779static struct ftrace_profile *
780ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400781{
782 struct ftrace_profile *rec;
783 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400784 unsigned long key;
785
Namhyung Kim20079eb2013-04-10 08:55:50 +0900786 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400787 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400788
789 if (hlist_empty(hhd))
790 return NULL;
791
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400792 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 if (rec->ip == ip)
794 return rec;
795 }
796
797 return NULL;
798}
799
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800static void ftrace_add_profile(struct ftrace_profile_stat *stat,
801 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400802{
803 unsigned long key;
804
Namhyung Kim20079eb2013-04-10 08:55:50 +0900805 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400806 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400807}
808
Steven Rostedt318e0a72009-03-25 20:06:34 -0400809/*
810 * The memory is already allocated, this simply finds a new record to use.
811 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400812static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400813ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400814{
815 struct ftrace_profile *rec = NULL;
816
Steven Rostedt318e0a72009-03-25 20:06:34 -0400817 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400818 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400819 goto out;
820
Steven Rostedt493762f2009-03-23 17:12:36 -0400821 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400822 * Try to find the function again since an NMI
823 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400824 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400825 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400826 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400828
Steven Rostedtcafb1682009-03-24 20:50:39 -0400829 if (stat->pages->index == PROFILES_PER_PAGE) {
830 if (!stat->pages->next)
831 goto out;
832 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400833 }
834
Steven Rostedtcafb1682009-03-24 20:50:39 -0400835 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400837 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400838
Steven Rostedt493762f2009-03-23 17:12:36 -0400839 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400840 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400841
842 return rec;
843}
844
Steven Rostedt493762f2009-03-23 17:12:36 -0400845static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400846function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400847 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400848{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400849 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400850 struct ftrace_profile *rec;
851 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400852
853 if (!ftrace_profile_enabled)
854 return;
855
Steven Rostedt493762f2009-03-23 17:12:36 -0400856 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400857
Christoph Lameterbdffd892014-04-29 14:17:40 -0500858 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400859 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400860 goto out;
861
862 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400863 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400864 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400865 if (!rec)
866 goto out;
867 }
868
869 rec->counter++;
870 out:
871 local_irq_restore(flags);
872}
873
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400874#ifdef CONFIG_FUNCTION_GRAPH_TRACER
875static int profile_graph_entry(struct ftrace_graph_ent *trace)
876{
Namhyung Kim8861dd32016-08-31 11:55:29 +0900877 int index = trace->depth;
878
Steven Rostedta1e2e312011-08-09 12:50:46 -0400879 function_profile_call(trace->func, 0, NULL, NULL);
Namhyung Kim8861dd32016-08-31 11:55:29 +0900880
881 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
882 current->ret_stack[index].subtime = 0;
883
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400884 return 1;
885}
886
887static void profile_graph_return(struct ftrace_graph_ret *trace)
888{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400889 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400890 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400891 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400892 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400893
894 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500895 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400896 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400897 goto out;
898
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400899 /* If the calltime was zero'd ignore it */
900 if (!trace->calltime)
901 goto out;
902
Steven Rostedta2a16d62009-03-24 23:17:58 -0400903 calltime = trace->rettime - trace->calltime;
904
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400905 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400906 int index;
907
908 index = trace->depth;
909
910 /* Append this call time to the parent time to subtract */
911 if (index)
912 current->ret_stack[index - 1].subtime += calltime;
913
914 if (current->ret_stack[index].subtime < calltime)
915 calltime -= current->ret_stack[index].subtime;
916 else
917 calltime = 0;
918 }
919
Steven Rostedtcafb1682009-03-24 20:50:39 -0400920 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400921 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400922 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400923 rec->time_squared += calltime * calltime;
924 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400925
Steven Rostedtcafb1682009-03-24 20:50:39 -0400926 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400927 local_irq_restore(flags);
928}
929
930static int register_ftrace_profiler(void)
931{
932 return register_ftrace_graph(&profile_graph_return,
933 &profile_graph_entry);
934}
935
936static void unregister_ftrace_profiler(void)
937{
938 unregister_ftrace_graph();
939}
940#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100941static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400942 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900943 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400944 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400945};
946
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400947static int register_ftrace_profiler(void)
948{
949 return register_ftrace_function(&ftrace_profile_ops);
950}
951
952static void unregister_ftrace_profiler(void)
953{
954 unregister_ftrace_function(&ftrace_profile_ops);
955}
956#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
957
Steven Rostedt493762f2009-03-23 17:12:36 -0400958static ssize_t
959ftrace_profile_write(struct file *filp, const char __user *ubuf,
960 size_t cnt, loff_t *ppos)
961{
962 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400963 int ret;
964
Peter Huewe22fe9b52011-06-07 21:58:27 +0200965 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
966 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400967 return ret;
968
969 val = !!val;
970
971 mutex_lock(&ftrace_profile_lock);
972 if (ftrace_profile_enabled ^ val) {
973 if (val) {
974 ret = ftrace_profile_init();
975 if (ret < 0) {
976 cnt = ret;
977 goto out;
978 }
979
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400980 ret = register_ftrace_profiler();
981 if (ret < 0) {
982 cnt = ret;
983 goto out;
984 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400985 ftrace_profile_enabled = 1;
986 } else {
987 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400988 /*
989 * unregister_ftrace_profiler calls stop_machine
990 * so this acts like an synchronize_sched.
991 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400992 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400993 }
994 }
995 out:
996 mutex_unlock(&ftrace_profile_lock);
997
Jiri Olsacf8517c2009-10-23 19:36:16 -0400998 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400999
1000 return cnt;
1001}
1002
1003static ssize_t
1004ftrace_profile_read(struct file *filp, char __user *ubuf,
1005 size_t cnt, loff_t *ppos)
1006{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001007 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001008 int r;
1009
1010 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1011 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1012}
1013
1014static const struct file_operations ftrace_profile_fops = {
1015 .open = tracing_open_generic,
1016 .read = ftrace_profile_read,
1017 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001018 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001019};
1020
Steven Rostedtcafb1682009-03-24 20:50:39 -04001021/* used to initialize the real stat files */
1022static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001023 .name = "functions",
1024 .stat_start = function_stat_start,
1025 .stat_next = function_stat_next,
1026 .stat_cmp = function_stat_cmp,
1027 .stat_headers = function_stat_headers,
1028 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001029};
1030
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001031static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001032{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001033 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001034 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001035 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001036 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001037 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001038
Steven Rostedtcafb1682009-03-24 20:50:39 -04001039 for_each_possible_cpu(cpu) {
1040 stat = &per_cpu(ftrace_profile_stats, cpu);
1041
Geliang Tang6363c6b2016-03-15 22:12:34 +08001042 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -04001043 if (!name) {
1044 /*
1045 * The files created are permanent, if something happens
1046 * we still do not free memory.
1047 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001048 WARN(1,
1049 "Could not allocate stat file for cpu %d\n",
1050 cpu);
1051 return;
1052 }
1053 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001054 stat->stat.name = name;
1055 ret = register_stat_tracer(&stat->stat);
1056 if (ret) {
1057 WARN(1,
1058 "Could not register function stat for cpu %d\n",
1059 cpu);
1060 kfree(name);
1061 return;
1062 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001063 }
1064
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001065 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001066 d_tracer, NULL, &ftrace_profile_fops);
1067 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001068 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001069}
1070
1071#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001072static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001073{
1074}
1075#endif /* CONFIG_FUNCTION_PROFILER */
1076
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001077static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1078
Pratyush Anand1619dc32015-03-06 23:58:06 +05301079#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1080static int ftrace_graph_active;
1081#else
1082# define ftrace_graph_active 0
1083#endif
1084
Steven Rostedt3d083392008-05-12 21:20:42 +02001085#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001086
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001087static struct ftrace_ops *removed_ops;
1088
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001089/*
1090 * Set when doing a global update, like enabling all recs or disabling them.
1091 * It is not set when just updating a single ftrace_ops.
1092 */
1093static bool update_all_ops;
1094
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001095#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001096# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001097#endif
1098
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001099struct ftrace_func_entry {
1100 struct hlist_node hlist;
1101 unsigned long ip;
1102};
1103
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001104/*
1105 * We make these constant because no one should touch them,
1106 * but they are used as the default "empty hash", to avoid allocating
1107 * it all the time. These are in a read only section such that if
1108 * anyone does try to modify it, it will cause an exception.
1109 */
1110static const struct hlist_head empty_buckets[1];
1111static const struct ftrace_hash empty_hash = {
1112 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001113};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001114#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001115
Steven Rostedt2b499382011-05-03 22:49:52 -04001116static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001117 .func = ftrace_stub,
1118 .local_hash.notrace_hash = EMPTY_HASH,
1119 .local_hash.filter_hash = EMPTY_HASH,
1120 INIT_OPS_HASH(global_ops)
1121 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001122 FTRACE_OPS_FL_INITIALIZED |
1123 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001124};
1125
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001126/*
1127 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001128 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001129 * not return true for either core_kernel_text() or
1130 * is_module_text_address().
1131 */
1132bool is_ftrace_trampoline(unsigned long addr)
1133{
1134 struct ftrace_ops *op;
1135 bool ret = false;
1136
1137 /*
1138 * Some of the ops may be dynamically allocated,
1139 * they are freed after a synchronize_sched().
1140 */
1141 preempt_disable_notrace();
1142
1143 do_for_each_ftrace_op(op, ftrace_ops_list) {
1144 /*
1145 * This is to check for dynamically allocated trampolines.
1146 * Trampolines that are in kernel text will have
1147 * core_kernel_text() return true.
1148 */
1149 if (op->trampoline && op->trampoline_size)
1150 if (addr >= op->trampoline &&
1151 addr < op->trampoline + op->trampoline_size) {
1152 ret = true;
1153 goto out;
1154 }
1155 } while_for_each_ftrace_op(op);
1156
1157 out:
1158 preempt_enable_notrace();
1159
1160 return ret;
1161}
1162
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001163struct ftrace_page {
1164 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001165 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001166 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001167 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001168};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001169
Steven Rostedta7900872011-12-16 16:23:44 -05001170#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1171#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001172
1173/* estimate from running different kernels */
1174#define NR_TO_INIT 10000
1175
1176static struct ftrace_page *ftrace_pages_start;
1177static struct ftrace_page *ftrace_pages;
1178
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001179static __always_inline unsigned long
1180ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1181{
1182 if (hash->size_bits > 0)
1183 return hash_long(ip, hash->size_bits);
1184
1185 return 0;
1186}
1187
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001188/* Only use this function if ftrace_hash_empty() has already been tested */
1189static __always_inline struct ftrace_func_entry *
1190__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001191{
1192 unsigned long key;
1193 struct ftrace_func_entry *entry;
1194 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001195
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001196 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001197 hhd = &hash->buckets[key];
1198
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001199 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001200 if (entry->ip == ip)
1201 return entry;
1202 }
1203 return NULL;
1204}
1205
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001206/**
1207 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1208 * @hash: The hash to look at
1209 * @ip: The instruction pointer to test
1210 *
1211 * Search a given @hash to see if a given instruction pointer (@ip)
1212 * exists in it.
1213 *
1214 * Returns the entry that holds the @ip if found. NULL otherwise.
1215 */
1216struct ftrace_func_entry *
1217ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1218{
1219 if (ftrace_hash_empty(hash))
1220 return NULL;
1221
1222 return __ftrace_lookup_ip(hash, ip);
1223}
1224
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001225static void __add_hash_entry(struct ftrace_hash *hash,
1226 struct ftrace_func_entry *entry)
1227{
1228 struct hlist_head *hhd;
1229 unsigned long key;
1230
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001231 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001232 hhd = &hash->buckets[key];
1233 hlist_add_head(&entry->hlist, hhd);
1234 hash->count++;
1235}
1236
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001237static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1238{
1239 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001240
1241 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1242 if (!entry)
1243 return -ENOMEM;
1244
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001245 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001246 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001247
1248 return 0;
1249}
1250
1251static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001252free_hash_entry(struct ftrace_hash *hash,
1253 struct ftrace_func_entry *entry)
1254{
1255 hlist_del(&entry->hlist);
1256 kfree(entry);
1257 hash->count--;
1258}
1259
1260static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001261remove_hash_entry(struct ftrace_hash *hash,
1262 struct ftrace_func_entry *entry)
1263{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001264 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001265 hash->count--;
1266}
1267
1268static void ftrace_hash_clear(struct ftrace_hash *hash)
1269{
1270 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001271 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001272 struct ftrace_func_entry *entry;
1273 int size = 1 << hash->size_bits;
1274 int i;
1275
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276 if (!hash->count)
1277 return;
1278
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001279 for (i = 0; i < size; i++) {
1280 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001281 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001282 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001283 }
1284 FTRACE_WARN_ON(hash->count);
1285}
1286
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001287static void free_ftrace_hash(struct ftrace_hash *hash)
1288{
1289 if (!hash || hash == EMPTY_HASH)
1290 return;
1291 ftrace_hash_clear(hash);
1292 kfree(hash->buckets);
1293 kfree(hash);
1294}
1295
Steven Rostedt07fd5512011-05-05 18:03:47 -04001296static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1297{
1298 struct ftrace_hash *hash;
1299
1300 hash = container_of(rcu, struct ftrace_hash, rcu);
1301 free_ftrace_hash(hash);
1302}
1303
1304static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1305{
1306 if (!hash || hash == EMPTY_HASH)
1307 return;
1308 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1309}
1310
Jiri Olsa5500fa52012-02-15 15:51:54 +01001311void ftrace_free_filter(struct ftrace_ops *ops)
1312{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001313 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001314 free_ftrace_hash(ops->func_hash->filter_hash);
1315 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001316}
1317
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001318static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1319{
1320 struct ftrace_hash *hash;
1321 int size;
1322
1323 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1324 if (!hash)
1325 return NULL;
1326
1327 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001328 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001329
1330 if (!hash->buckets) {
1331 kfree(hash);
1332 return NULL;
1333 }
1334
1335 hash->size_bits = size_bits;
1336
1337 return hash;
1338}
1339
1340static struct ftrace_hash *
1341alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1342{
1343 struct ftrace_func_entry *entry;
1344 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001345 int size;
1346 int ret;
1347 int i;
1348
1349 new_hash = alloc_ftrace_hash(size_bits);
1350 if (!new_hash)
1351 return NULL;
1352
1353 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001354 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001355 return new_hash;
1356
1357 size = 1 << hash->size_bits;
1358 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001359 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001360 ret = add_hash_entry(new_hash, entry->ip);
1361 if (ret < 0)
1362 goto free_hash;
1363 }
1364 }
1365
1366 FTRACE_WARN_ON(new_hash->count != hash->count);
1367
1368 return new_hash;
1369
1370 free_hash:
1371 free_ftrace_hash(new_hash);
1372 return NULL;
1373}
1374
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001375static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001376ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001377static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001378ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001379
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001380static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1381 struct ftrace_hash *new_hash);
1382
Namhyung Kim3e278c02017-01-20 11:44:45 +09001383static struct ftrace_hash *
1384__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385{
1386 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001387 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001388 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001389 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001390 int size = src->count;
1391 int bits = 0;
1392 int i;
1393
1394 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001395 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001396 */
Namhyung Kim3e278c02017-01-20 11:44:45 +09001397 if (!src->count)
1398 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001399
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001400 /*
1401 * Make the hash size about 1/2 the # found
1402 */
1403 for (size /= 2; size; size >>= 1)
1404 bits++;
1405
1406 /* Don't allocate too much */
1407 if (bits > FTRACE_HASH_MAX_BITS)
1408 bits = FTRACE_HASH_MAX_BITS;
1409
Steven Rostedt07fd5512011-05-05 18:03:47 -04001410 new_hash = alloc_ftrace_hash(bits);
1411 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001412 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001413
1414 size = 1 << src->size_bits;
1415 for (i = 0; i < size; i++) {
1416 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001417 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001418 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001419 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001420 }
1421 }
1422
Namhyung Kim3e278c02017-01-20 11:44:45 +09001423 return new_hash;
1424}
1425
1426static int
1427ftrace_hash_move(struct ftrace_ops *ops, int enable,
1428 struct ftrace_hash **dst, struct ftrace_hash *src)
1429{
1430 struct ftrace_hash *new_hash;
1431 int ret;
1432
1433 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1434 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1435 return -EINVAL;
1436
1437 new_hash = __ftrace_hash_move(src);
1438 if (!new_hash)
1439 return -ENOMEM;
1440
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001441 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1442 if (enable) {
1443 /* IPMODIFY should be updated only when filter_hash updating */
1444 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1445 if (ret < 0) {
1446 free_ftrace_hash(new_hash);
1447 return ret;
1448 }
1449 }
1450
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001451 /*
1452 * Remove the current set, update the hash and add
1453 * them back.
1454 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001455 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001456
Steven Rostedt07fd5512011-05-05 18:03:47 -04001457 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001458
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001459 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001460
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001461 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001462}
1463
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001464static bool hash_contains_ip(unsigned long ip,
1465 struct ftrace_ops_hash *hash)
1466{
1467 /*
1468 * The function record is a match if it exists in the filter
1469 * hash and not in the notrace hash. Note, an emty hash is
1470 * considered a match for the filter hash, but an empty
1471 * notrace hash is considered not in the notrace hash.
1472 */
1473 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001474 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001475 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001476 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001477}
1478
Steven Rostedt265c8312009-02-13 12:43:56 -05001479/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001480 * Test the hashes for this ops to see if we want to call
1481 * the ops->func or not.
1482 *
1483 * It's a match if the ip is in the ops->filter_hash or
1484 * the filter_hash does not exist or is empty,
1485 * AND
1486 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001487 *
1488 * This needs to be called with preemption disabled as
1489 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001490 */
1491static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001492ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001493{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001494 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001495 int ret;
1496
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001497#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1498 /*
1499 * There's a small race when adding ops that the ftrace handler
1500 * that wants regs, may be called without them. We can not
1501 * allow that handler to be called if regs is NULL.
1502 */
1503 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1504 return 0;
1505#endif
1506
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001507 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1508 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001509
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001510 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001511 ret = 1;
1512 else
1513 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001514
1515 return ret;
1516}
1517
1518/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001519 * This is a double for. Do not use 'break' to break out of the loop,
1520 * you must use a goto.
1521 */
1522#define do_for_each_ftrace_rec(pg, rec) \
1523 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1524 int _____i; \
1525 for (_____i = 0; _____i < pg->index; _____i++) { \
1526 rec = &pg->records[_____i];
1527
1528#define while_for_each_ftrace_rec() \
1529 } \
1530 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301531
Steven Rostedt5855fea2011-12-16 19:27:42 -05001532
1533static int ftrace_cmp_recs(const void *a, const void *b)
1534{
Steven Rostedta650e022012-04-25 13:48:13 -04001535 const struct dyn_ftrace *key = a;
1536 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001537
Steven Rostedta650e022012-04-25 13:48:13 -04001538 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001539 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001540 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1541 return 1;
1542 return 0;
1543}
1544
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001545/**
1546 * ftrace_location_range - return the first address of a traced location
1547 * if it touches the given ip range
1548 * @start: start of range to search.
1549 * @end: end of range to search (inclusive). @end points to the last byte
1550 * to check.
1551 *
1552 * Returns rec->ip if the related ftrace location is a least partly within
1553 * the given address range. That is, the first address of the instruction
1554 * that is either a NOP or call to the function tracer. It checks the ftrace
1555 * internal tables to determine if the address belongs or not.
1556 */
1557unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001558{
1559 struct ftrace_page *pg;
1560 struct dyn_ftrace *rec;
1561 struct dyn_ftrace key;
1562
1563 key.ip = start;
1564 key.flags = end; /* overload flags, as it is unsigned long */
1565
1566 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1567 if (end < pg->records[0].ip ||
1568 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1569 continue;
1570 rec = bsearch(&key, pg->records, pg->index,
1571 sizeof(struct dyn_ftrace),
1572 ftrace_cmp_recs);
1573 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001574 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001575 }
1576
Steven Rostedt5855fea2011-12-16 19:27:42 -05001577 return 0;
1578}
1579
Steven Rostedtc88fd862011-08-16 09:53:39 -04001580/**
1581 * ftrace_location - return true if the ip giving is a traced location
1582 * @ip: the instruction pointer to check
1583 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001584 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001585 * That is, the instruction that is either a NOP or call to
1586 * the function tracer. It checks the ftrace internal tables to
1587 * determine if the address belongs or not.
1588 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001589unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001590{
Steven Rostedta650e022012-04-25 13:48:13 -04001591 return ftrace_location_range(ip, ip);
1592}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001593
Steven Rostedta650e022012-04-25 13:48:13 -04001594/**
1595 * ftrace_text_reserved - return true if range contains an ftrace location
1596 * @start: start of range to search
1597 * @end: end of range to search (inclusive). @end points to the last byte to check.
1598 *
1599 * Returns 1 if @start and @end contains a ftrace location.
1600 * That is, the instruction that is either a NOP or call to
1601 * the function tracer. It checks the ftrace internal tables to
1602 * determine if the address belongs or not.
1603 */
Sasha Levind88471c2013-01-09 18:09:20 -05001604int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001605{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001606 unsigned long ret;
1607
1608 ret = ftrace_location_range((unsigned long)start,
1609 (unsigned long)end);
1610
1611 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001612}
1613
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001614/* Test if ops registered to this rec needs regs */
1615static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1616{
1617 struct ftrace_ops *ops;
1618 bool keep_regs = false;
1619
1620 for (ops = ftrace_ops_list;
1621 ops != &ftrace_list_end; ops = ops->next) {
1622 /* pass rec in as regs to have non-NULL val */
1623 if (ftrace_ops_test(ops, rec->ip, rec)) {
1624 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1625 keep_regs = true;
1626 break;
1627 }
1628 }
1629 }
1630
1631 return keep_regs;
1632}
1633
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001634static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001635 int filter_hash,
1636 bool inc)
1637{
1638 struct ftrace_hash *hash;
1639 struct ftrace_hash *other_hash;
1640 struct ftrace_page *pg;
1641 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001642 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001643 int count = 0;
1644 int all = 0;
1645
1646 /* Only update if the ops has been registered */
1647 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001648 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001649
1650 /*
1651 * In the filter_hash case:
1652 * If the count is zero, we update all records.
1653 * Otherwise we just update the items in the hash.
1654 *
1655 * In the notrace_hash case:
1656 * We enable the update in the hash.
1657 * As disabling notrace means enabling the tracing,
1658 * and enabling notrace means disabling, the inc variable
1659 * gets inversed.
1660 */
1661 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001662 hash = ops->func_hash->filter_hash;
1663 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001664 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001665 all = 1;
1666 } else {
1667 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001668 hash = ops->func_hash->notrace_hash;
1669 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001670 /*
1671 * If the notrace hash has no items,
1672 * then there's nothing to do.
1673 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001674 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001675 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001676 }
1677
1678 do_for_each_ftrace_rec(pg, rec) {
1679 int in_other_hash = 0;
1680 int in_hash = 0;
1681 int match = 0;
1682
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001683 if (rec->flags & FTRACE_FL_DISABLED)
1684 continue;
1685
Steven Rostedted926f92011-05-03 13:25:24 -04001686 if (all) {
1687 /*
1688 * Only the filter_hash affects all records.
1689 * Update if the record is not in the notrace hash.
1690 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001691 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001692 match = 1;
1693 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001694 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1695 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001696
1697 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001698 * If filter_hash is set, we want to match all functions
1699 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001700 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001701 * If filter_hash is not set, then we are decrementing.
1702 * That means we match anything that is in the hash
1703 * and also in the other_hash. That is, we need to turn
1704 * off functions in the other hash because they are disabled
1705 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001706 */
1707 if (filter_hash && in_hash && !in_other_hash)
1708 match = 1;
1709 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001710 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001711 match = 1;
1712 }
1713 if (!match)
1714 continue;
1715
1716 if (inc) {
1717 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001718 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001719 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001720
1721 /*
1722 * If there's only a single callback registered to a
1723 * function, and the ops has a trampoline registered
1724 * for it, then we can call it directly.
1725 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001726 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001727 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001728 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001729 /*
1730 * If we are adding another function callback
1731 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001732 * custom trampoline in use, then we need to go
1733 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001734 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001735 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001736
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001737 /*
1738 * If any ops wants regs saved for this function
1739 * then all ops will get saved regs.
1740 */
1741 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1742 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001743 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001744 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001745 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001746 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001747
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001748 /*
1749 * If the rec had REGS enabled and the ops that is
1750 * being removed had REGS set, then see if there is
1751 * still any ops for this record that wants regs.
1752 * If not, we can stop recording them.
1753 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001754 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001755 rec->flags & FTRACE_FL_REGS &&
1756 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1757 if (!test_rec_ops_needs_regs(rec))
1758 rec->flags &= ~FTRACE_FL_REGS;
1759 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001760
1761 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001762 * If the rec had TRAMP enabled, then it needs to
1763 * be cleared. As TRAMP can only be enabled iff
1764 * there is only a single ops attached to it.
1765 * In otherwords, always disable it on decrementing.
1766 * In the future, we may set it if rec count is
1767 * decremented to one, and the ops that is left
1768 * has a trampoline.
1769 */
1770 rec->flags &= ~FTRACE_FL_TRAMP;
1771
1772 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001773 * flags will be cleared in ftrace_check_record()
1774 * if rec count is zero.
1775 */
Steven Rostedted926f92011-05-03 13:25:24 -04001776 }
1777 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001778
1779 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1780 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1781
Steven Rostedted926f92011-05-03 13:25:24 -04001782 /* Shortcut, if we handled all records, we are done. */
1783 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001784 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001785 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001786
1787 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001788}
1789
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001790static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001791 int filter_hash)
1792{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001793 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001794}
1795
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001796static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001797 int filter_hash)
1798{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001799 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001800}
1801
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001802static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1803 int filter_hash, int inc)
1804{
1805 struct ftrace_ops *op;
1806
1807 __ftrace_hash_rec_update(ops, filter_hash, inc);
1808
1809 if (ops->func_hash != &global_ops.local_hash)
1810 return;
1811
1812 /*
1813 * If the ops shares the global_ops hash, then we need to update
1814 * all ops that are enabled and use this hash.
1815 */
1816 do_for_each_ftrace_op(op, ftrace_ops_list) {
1817 /* Already done */
1818 if (op == ops)
1819 continue;
1820 if (op->func_hash == &global_ops.local_hash)
1821 __ftrace_hash_rec_update(op, filter_hash, inc);
1822 } while_for_each_ftrace_op(op);
1823}
1824
1825static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1826 int filter_hash)
1827{
1828 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1829}
1830
1831static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1832 int filter_hash)
1833{
1834 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1835}
1836
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001837/*
1838 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1839 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1840 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1841 * Note that old_hash and new_hash has below meanings
1842 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1843 * - If the hash is EMPTY_HASH, it hits nothing
1844 * - Anything else hits the recs which match the hash entries.
1845 */
1846static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1847 struct ftrace_hash *old_hash,
1848 struct ftrace_hash *new_hash)
1849{
1850 struct ftrace_page *pg;
1851 struct dyn_ftrace *rec, *end = NULL;
1852 int in_old, in_new;
1853
1854 /* Only update if the ops has been registered */
1855 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1856 return 0;
1857
1858 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1859 return 0;
1860
1861 /*
1862 * Since the IPMODIFY is a very address sensitive action, we do not
1863 * allow ftrace_ops to set all functions to new hash.
1864 */
1865 if (!new_hash || !old_hash)
1866 return -EINVAL;
1867
1868 /* Update rec->flags */
1869 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001870
1871 if (rec->flags & FTRACE_FL_DISABLED)
1872 continue;
1873
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001874 /* We need to update only differences of filter_hash */
1875 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1876 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1877 if (in_old == in_new)
1878 continue;
1879
1880 if (in_new) {
1881 /* New entries must ensure no others are using it */
1882 if (rec->flags & FTRACE_FL_IPMODIFY)
1883 goto rollback;
1884 rec->flags |= FTRACE_FL_IPMODIFY;
1885 } else /* Removed entry */
1886 rec->flags &= ~FTRACE_FL_IPMODIFY;
1887 } while_for_each_ftrace_rec();
1888
1889 return 0;
1890
1891rollback:
1892 end = rec;
1893
1894 /* Roll back what we did above */
1895 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001896
1897 if (rec->flags & FTRACE_FL_DISABLED)
1898 continue;
1899
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001900 if (rec == end)
1901 goto err_out;
1902
1903 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1904 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1905 if (in_old == in_new)
1906 continue;
1907
1908 if (in_new)
1909 rec->flags &= ~FTRACE_FL_IPMODIFY;
1910 else
1911 rec->flags |= FTRACE_FL_IPMODIFY;
1912 } while_for_each_ftrace_rec();
1913
1914err_out:
1915 return -EBUSY;
1916}
1917
1918static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1919{
1920 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1921
1922 if (ftrace_hash_empty(hash))
1923 hash = NULL;
1924
1925 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1926}
1927
1928/* Disabling always succeeds */
1929static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1930{
1931 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1932
1933 if (ftrace_hash_empty(hash))
1934 hash = NULL;
1935
1936 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1937}
1938
1939static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1940 struct ftrace_hash *new_hash)
1941{
1942 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1943
1944 if (ftrace_hash_empty(old_hash))
1945 old_hash = NULL;
1946
1947 if (ftrace_hash_empty(new_hash))
1948 new_hash = NULL;
1949
1950 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1951}
1952
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001953static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001954{
1955 int i;
1956
1957 printk(KERN_CONT "%s", fmt);
1958
1959 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1960 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1961}
1962
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001963static struct ftrace_ops *
1964ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001965static struct ftrace_ops *
1966ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001967
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001968enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001969const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001970
1971static void print_bug_type(void)
1972{
1973 switch (ftrace_bug_type) {
1974 case FTRACE_BUG_UNKNOWN:
1975 break;
1976 case FTRACE_BUG_INIT:
1977 pr_info("Initializing ftrace call sites\n");
1978 break;
1979 case FTRACE_BUG_NOP:
1980 pr_info("Setting ftrace call site to NOP\n");
1981 break;
1982 case FTRACE_BUG_CALL:
1983 pr_info("Setting ftrace call site to call ftrace function\n");
1984 break;
1985 case FTRACE_BUG_UPDATE:
1986 pr_info("Updating ftrace call site to call a different ftrace function\n");
1987 break;
1988 }
1989}
1990
Steven Rostedtc88fd862011-08-16 09:53:39 -04001991/**
1992 * ftrace_bug - report and shutdown function tracer
1993 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001994 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001995 *
1996 * The arch code that enables or disables the function tracing
1997 * can call ftrace_bug() when it has detected a problem in
1998 * modifying the code. @failed should be one of either:
1999 * EFAULT - if the problem happens on reading the @ip address
2000 * EINVAL - if what is read at @ip is not what was expected
2001 * EPERM - if the problem happens on writting to the @ip address
2002 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002003void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002004{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002005 unsigned long ip = rec ? rec->ip : 0;
2006
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002007 switch (failed) {
2008 case -EFAULT:
2009 FTRACE_WARN_ON_ONCE(1);
2010 pr_info("ftrace faulted on modifying ");
2011 print_ip_sym(ip);
2012 break;
2013 case -EINVAL:
2014 FTRACE_WARN_ON_ONCE(1);
2015 pr_info("ftrace failed to modify ");
2016 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002017 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002018 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002019 if (ftrace_expected) {
2020 print_ip_ins(" expected: ", ftrace_expected);
2021 pr_cont("\n");
2022 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002023 break;
2024 case -EPERM:
2025 FTRACE_WARN_ON_ONCE(1);
2026 pr_info("ftrace faulted on writing ");
2027 print_ip_sym(ip);
2028 break;
2029 default:
2030 FTRACE_WARN_ON_ONCE(1);
2031 pr_info("ftrace faulted on unknown error ");
2032 print_ip_sym(ip);
2033 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002034 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002035 if (rec) {
2036 struct ftrace_ops *ops = NULL;
2037
2038 pr_info("ftrace record flags: %lx\n", rec->flags);
2039 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2040 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2041 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2042 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002043 if (ops) {
2044 do {
2045 pr_cont("\ttramp: %pS (%pS)",
2046 (void *)ops->trampoline,
2047 (void *)ops->func);
2048 ops = ftrace_find_tramp_ops_next(rec, ops);
2049 } while (ops);
2050 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002051 pr_cont("\ttramp: ERROR!");
2052
2053 }
2054 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002055 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002056 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002057}
2058
Steven Rostedtc88fd862011-08-16 09:53:39 -04002059static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002060{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002061 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002062
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002063 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2064
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002065 if (rec->flags & FTRACE_FL_DISABLED)
2066 return FTRACE_UPDATE_IGNORE;
2067
Steven Rostedt982c3502008-11-15 16:31:41 -05002068 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002069 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002070 *
Steven Rostedted926f92011-05-03 13:25:24 -04002071 * If the record has a ref count, then we need to enable it
2072 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002073 *
Steven Rostedted926f92011-05-03 13:25:24 -04002074 * Otherwise we make sure its disabled.
2075 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002076 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002077 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002078 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002079 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002080 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002081
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002082 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002083 * If enabling and the REGS flag does not match the REGS_EN, or
2084 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2085 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002086 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002087 if (flag) {
2088 if (!(rec->flags & FTRACE_FL_REGS) !=
2089 !(rec->flags & FTRACE_FL_REGS_EN))
2090 flag |= FTRACE_FL_REGS;
2091
2092 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2093 !(rec->flags & FTRACE_FL_TRAMP_EN))
2094 flag |= FTRACE_FL_TRAMP;
2095 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002096
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002097 /* If the state of this record hasn't changed, then do nothing */
2098 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002099 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002100
2101 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002102 /* Save off if rec is being enabled (for return value) */
2103 flag ^= rec->flags & FTRACE_FL_ENABLED;
2104
2105 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002106 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002107 if (flag & FTRACE_FL_REGS) {
2108 if (rec->flags & FTRACE_FL_REGS)
2109 rec->flags |= FTRACE_FL_REGS_EN;
2110 else
2111 rec->flags &= ~FTRACE_FL_REGS_EN;
2112 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002113 if (flag & FTRACE_FL_TRAMP) {
2114 if (rec->flags & FTRACE_FL_TRAMP)
2115 rec->flags |= FTRACE_FL_TRAMP_EN;
2116 else
2117 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2118 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002119 }
2120
2121 /*
2122 * If this record is being updated from a nop, then
2123 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002124 * Otherwise,
2125 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002126 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002127 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002128 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002129 if (flag & FTRACE_FL_ENABLED) {
2130 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002131 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002132 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002133
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002134 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002135 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002136 }
2137
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002138 if (update) {
2139 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002140 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002141 rec->flags = 0;
2142 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002143 /*
2144 * Just disable the record, but keep the ops TRAMP
2145 * and REGS states. The _EN flags must be disabled though.
2146 */
2147 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2148 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002149 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002150
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002151 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002152 return FTRACE_UPDATE_MAKE_NOP;
2153}
2154
2155/**
2156 * ftrace_update_record, set a record that now is tracing or not
2157 * @rec: the record to update
2158 * @enable: set to 1 if the record is tracing, zero to force disable
2159 *
2160 * The records that represent all functions that can be traced need
2161 * to be updated when tracing has been enabled.
2162 */
2163int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2164{
2165 return ftrace_check_record(rec, enable, 1);
2166}
2167
2168/**
2169 * ftrace_test_record, check if the record has been enabled or not
2170 * @rec: the record to test
2171 * @enable: set to 1 to check if enabled, 0 if it is disabled
2172 *
2173 * The arch code may need to test if a record is already set to
2174 * tracing to determine how to modify the function code that it
2175 * represents.
2176 */
2177int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2178{
2179 return ftrace_check_record(rec, enable, 0);
2180}
2181
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002182static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002183ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2184{
2185 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002186 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002187
2188 do_for_each_ftrace_op(op, ftrace_ops_list) {
2189
2190 if (!op->trampoline)
2191 continue;
2192
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002193 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002194 return op;
2195 } while_for_each_ftrace_op(op);
2196
2197 return NULL;
2198}
2199
2200static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002201ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2202 struct ftrace_ops *op)
2203{
2204 unsigned long ip = rec->ip;
2205
2206 while_for_each_ftrace_op(op) {
2207
2208 if (!op->trampoline)
2209 continue;
2210
2211 if (hash_contains_ip(ip, op->func_hash))
2212 return op;
2213 }
2214
2215 return NULL;
2216}
2217
2218static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002219ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2220{
2221 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002222 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002223
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002224 /*
2225 * Need to check removed ops first.
2226 * If they are being removed, and this rec has a tramp,
2227 * and this rec is in the ops list, then it would be the
2228 * one with the tramp.
2229 */
2230 if (removed_ops) {
2231 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002232 return removed_ops;
2233 }
2234
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002235 /*
2236 * Need to find the current trampoline for a rec.
2237 * Now, a trampoline is only attached to a rec if there
2238 * was a single 'ops' attached to it. But this can be called
2239 * when we are adding another op to the rec or removing the
2240 * current one. Thus, if the op is being added, we can
2241 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002242 * yet.
2243 *
2244 * If an ops is being modified (hooking to different functions)
2245 * then we don't care about the new functions that are being
2246 * added, just the old ones (that are probably being removed).
2247 *
2248 * If we are adding an ops to a function that already is using
2249 * a trampoline, it needs to be removed (trampolines are only
2250 * for single ops connected), then an ops that is not being
2251 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002252 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002253 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002254
2255 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002256 continue;
2257
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002258 /*
2259 * If the ops is being added, it hasn't gotten to
2260 * the point to be removed from this tree yet.
2261 */
2262 if (op->flags & FTRACE_OPS_FL_ADDING)
2263 continue;
2264
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002265
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002266 /*
2267 * If the ops is being modified and is in the old
2268 * hash, then it is probably being removed from this
2269 * function.
2270 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002271 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2272 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002273 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002274 /*
2275 * If the ops is not being added or modified, and it's
2276 * in its normal filter hash, then this must be the one
2277 * we want!
2278 */
2279 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2280 hash_contains_ip(ip, op->func_hash))
2281 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002282
2283 } while_for_each_ftrace_op(op);
2284
2285 return NULL;
2286}
2287
2288static struct ftrace_ops *
2289ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2290{
2291 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002292 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002293
2294 do_for_each_ftrace_op(op, ftrace_ops_list) {
2295 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002296 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002297 return op;
2298 } while_for_each_ftrace_op(op);
2299
2300 return NULL;
2301}
2302
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002303/**
2304 * ftrace_get_addr_new - Get the call address to set to
2305 * @rec: The ftrace record descriptor
2306 *
2307 * If the record has the FTRACE_FL_REGS set, that means that it
2308 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2309 * is not not set, then it wants to convert to the normal callback.
2310 *
2311 * Returns the address of the trampoline to set to
2312 */
2313unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2314{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002315 struct ftrace_ops *ops;
2316
2317 /* Trampolines take precedence over regs */
2318 if (rec->flags & FTRACE_FL_TRAMP) {
2319 ops = ftrace_find_tramp_ops_new(rec);
2320 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002321 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2322 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002323 /* Ftrace is shutting down, return anything */
2324 return (unsigned long)FTRACE_ADDR;
2325 }
2326 return ops->trampoline;
2327 }
2328
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002329 if (rec->flags & FTRACE_FL_REGS)
2330 return (unsigned long)FTRACE_REGS_ADDR;
2331 else
2332 return (unsigned long)FTRACE_ADDR;
2333}
2334
2335/**
2336 * ftrace_get_addr_curr - Get the call address that is already there
2337 * @rec: The ftrace record descriptor
2338 *
2339 * The FTRACE_FL_REGS_EN is set when the record already points to
2340 * a function that saves all the regs. Basically the '_EN' version
2341 * represents the current state of the function.
2342 *
2343 * Returns the address of the trampoline that is currently being called
2344 */
2345unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2346{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002347 struct ftrace_ops *ops;
2348
2349 /* Trampolines take precedence over regs */
2350 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2351 ops = ftrace_find_tramp_ops_curr(rec);
2352 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002353 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2354 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002355 /* Ftrace is shutting down, return anything */
2356 return (unsigned long)FTRACE_ADDR;
2357 }
2358 return ops->trampoline;
2359 }
2360
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002361 if (rec->flags & FTRACE_FL_REGS_EN)
2362 return (unsigned long)FTRACE_REGS_ADDR;
2363 else
2364 return (unsigned long)FTRACE_ADDR;
2365}
2366
Steven Rostedtc88fd862011-08-16 09:53:39 -04002367static int
2368__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2369{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002370 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002371 unsigned long ftrace_addr;
2372 int ret;
2373
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002374 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002375
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002376 /* This needs to be done before we call ftrace_update_record */
2377 ftrace_old_addr = ftrace_get_addr_curr(rec);
2378
2379 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002380
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002381 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2382
Steven Rostedtc88fd862011-08-16 09:53:39 -04002383 switch (ret) {
2384 case FTRACE_UPDATE_IGNORE:
2385 return 0;
2386
2387 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002388 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002389 return ftrace_make_call(rec, ftrace_addr);
2390
2391 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002392 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002393 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002394
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002395 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002396 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002397 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002398 }
2399
2400 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002401}
2402
Steven Rostedte4f5d542012-04-27 09:13:18 -04002403void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002404{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002405 struct dyn_ftrace *rec;
2406 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002407 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002408
Steven Rostedt45a4a232011-04-21 23:16:46 -04002409 if (unlikely(ftrace_disabled))
2410 return;
2411
Steven Rostedt265c8312009-02-13 12:43:56 -05002412 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002413
2414 if (rec->flags & FTRACE_FL_DISABLED)
2415 continue;
2416
Steven Rostedte4f5d542012-04-27 09:13:18 -04002417 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002418 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002419 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002420 /* Stop processing */
2421 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002422 }
2423 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002424}
2425
Steven Rostedtc88fd862011-08-16 09:53:39 -04002426struct ftrace_rec_iter {
2427 struct ftrace_page *pg;
2428 int index;
2429};
2430
2431/**
2432 * ftrace_rec_iter_start, start up iterating over traced functions
2433 *
2434 * Returns an iterator handle that is used to iterate over all
2435 * the records that represent address locations where functions
2436 * are traced.
2437 *
2438 * May return NULL if no records are available.
2439 */
2440struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2441{
2442 /*
2443 * We only use a single iterator.
2444 * Protected by the ftrace_lock mutex.
2445 */
2446 static struct ftrace_rec_iter ftrace_rec_iter;
2447 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2448
2449 iter->pg = ftrace_pages_start;
2450 iter->index = 0;
2451
2452 /* Could have empty pages */
2453 while (iter->pg && !iter->pg->index)
2454 iter->pg = iter->pg->next;
2455
2456 if (!iter->pg)
2457 return NULL;
2458
2459 return iter;
2460}
2461
2462/**
2463 * ftrace_rec_iter_next, get the next record to process.
2464 * @iter: The handle to the iterator.
2465 *
2466 * Returns the next iterator after the given iterator @iter.
2467 */
2468struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2469{
2470 iter->index++;
2471
2472 if (iter->index >= iter->pg->index) {
2473 iter->pg = iter->pg->next;
2474 iter->index = 0;
2475
2476 /* Could have empty pages */
2477 while (iter->pg && !iter->pg->index)
2478 iter->pg = iter->pg->next;
2479 }
2480
2481 if (!iter->pg)
2482 return NULL;
2483
2484 return iter;
2485}
2486
2487/**
2488 * ftrace_rec_iter_record, get the record at the iterator location
2489 * @iter: The current iterator location
2490 *
2491 * Returns the record that the current @iter is at.
2492 */
2493struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2494{
2495 return &iter->pg->records[iter->index];
2496}
2497
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302498static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002499ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002500{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002501 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002502
Steven Rostedt45a4a232011-04-21 23:16:46 -04002503 if (unlikely(ftrace_disabled))
2504 return 0;
2505
Shaohua Li25aac9d2009-01-09 11:29:40 +08002506 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002507 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002508 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002509 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302510 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002511 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302512 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002513}
2514
Steven Rostedt000ab692009-02-17 13:35:06 -05002515/*
2516 * archs can override this function if they must do something
2517 * before the modifying code is performed.
2518 */
2519int __weak ftrace_arch_code_modify_prepare(void)
2520{
2521 return 0;
2522}
2523
2524/*
2525 * archs can override this function if they must do something
2526 * after the modifying code is performed.
2527 */
2528int __weak ftrace_arch_code_modify_post_process(void)
2529{
2530 return 0;
2531}
2532
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002533void ftrace_modify_all_code(int command)
2534{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002535 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002536 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002537
2538 /*
2539 * If the ftrace_caller calls a ftrace_ops func directly,
2540 * we need to make sure that it only traces functions it
2541 * expects to trace. When doing the switch of functions,
2542 * we need to update to the ftrace_ops_list_func first
2543 * before the transition between old and new calls are set,
2544 * as the ftrace_ops_list_func will check the ops hashes
2545 * to make sure the ops are having the right functions
2546 * traced.
2547 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002548 if (update) {
2549 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2550 if (FTRACE_WARN_ON(err))
2551 return;
2552 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002553
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002554 if (command & FTRACE_UPDATE_CALLS)
2555 ftrace_replace_code(1);
2556 else if (command & FTRACE_DISABLE_CALLS)
2557 ftrace_replace_code(0);
2558
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002559 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2560 function_trace_op = set_function_trace_op;
2561 smp_wmb();
2562 /* If irqs are disabled, we are in stop machine */
2563 if (!irqs_disabled())
2564 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002565 err = ftrace_update_ftrace_func(ftrace_trace_function);
2566 if (FTRACE_WARN_ON(err))
2567 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002568 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002569
2570 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002571 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002572 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002573 err = ftrace_disable_ftrace_graph_caller();
2574 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002575}
2576
Ingo Molnare309b412008-05-12 21:20:51 +02002577static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002578{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002579 int *command = data;
2580
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002581 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002582
Steven Rostedtc88fd862011-08-16 09:53:39 -04002583 return 0;
2584}
2585
2586/**
2587 * ftrace_run_stop_machine, go back to the stop machine method
2588 * @command: The command to tell ftrace what to do
2589 *
2590 * If an arch needs to fall back to the stop machine method, the
2591 * it can call this function.
2592 */
2593void ftrace_run_stop_machine(int command)
2594{
2595 stop_machine(__ftrace_modify_code, &command, NULL);
2596}
2597
2598/**
2599 * arch_ftrace_update_code, modify the code to trace or not trace
2600 * @command: The command that needs to be done
2601 *
2602 * Archs can override this function if it does not need to
2603 * run stop_machine() to modify code.
2604 */
2605void __weak arch_ftrace_update_code(int command)
2606{
2607 ftrace_run_stop_machine(command);
2608}
2609
2610static void ftrace_run_update_code(int command)
2611{
2612 int ret;
2613
2614 ret = ftrace_arch_code_modify_prepare();
2615 FTRACE_WARN_ON(ret);
2616 if (ret)
2617 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002618
2619 /*
2620 * By default we use stop_machine() to modify the code.
2621 * But archs can do what ever they want as long as it
2622 * is safe. The stop_machine() is the safest, but also
2623 * produces the most overhead.
2624 */
2625 arch_ftrace_update_code(command);
2626
Steven Rostedt000ab692009-02-17 13:35:06 -05002627 ret = ftrace_arch_code_modify_post_process();
2628 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002629}
2630
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002631static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002632 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002633{
2634 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002635 ops->old_hash.filter_hash = old_hash->filter_hash;
2636 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002637 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002638 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002639 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002640 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2641}
2642
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002643static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002644static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002645
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002646void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2647{
2648}
2649
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002650static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002651{
2652 free_percpu(ops->disabled);
2653}
2654
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002655static void ftrace_startup_enable(int command)
2656{
2657 if (saved_ftrace_func != ftrace_trace_function) {
2658 saved_ftrace_func = ftrace_trace_function;
2659 command |= FTRACE_UPDATE_TRACE_FUNC;
2660 }
2661
2662 if (!command || !ftrace_enabled)
2663 return;
2664
2665 ftrace_run_update_code(command);
2666}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002667
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002668static void ftrace_startup_all(int command)
2669{
2670 update_all_ops = true;
2671 ftrace_startup_enable(command);
2672 update_all_ops = false;
2673}
2674
Steven Rostedta1cd6172011-05-23 15:24:25 -04002675static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002676{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002677 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002678
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002679 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002680 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002681
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002682 ret = __register_ftrace_function(ops);
2683 if (ret)
2684 return ret;
2685
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002686 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002687
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002688 /*
2689 * Note that ftrace probes uses this to start up
2690 * and modify functions it will probe. But we still
2691 * set the ADDING flag for modification, as probes
2692 * do not have trampolines. If they add them in the
2693 * future, then the probes will need to distinguish
2694 * between adding and updating probes.
2695 */
2696 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002697
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002698 ret = ftrace_hash_ipmodify_enable(ops);
2699 if (ret < 0) {
2700 /* Rollback registration process */
2701 __unregister_ftrace_function(ops);
2702 ftrace_start_up--;
2703 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2704 return ret;
2705 }
2706
Jiri Olsa7f50d062016-03-16 15:34:33 +01002707 if (ftrace_hash_rec_enable(ops, 1))
2708 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002709
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002710 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002711
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002712 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2713
Steven Rostedta1cd6172011-05-23 15:24:25 -04002714 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002715}
2716
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002717static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002718{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002719 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002720
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002721 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002722 return -ENODEV;
2723
2724 ret = __unregister_ftrace_function(ops);
2725 if (ret)
2726 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002727
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002728 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002729 /*
2730 * Just warn in case of unbalance, no need to kill ftrace, it's not
2731 * critical but the ftrace_call callers may be never nopped again after
2732 * further ftrace uses.
2733 */
2734 WARN_ON_ONCE(ftrace_start_up < 0);
2735
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002736 /* Disabling ipmodify never fails */
2737 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002738
2739 if (ftrace_hash_rec_disable(ops, 1))
2740 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002741
Namhyung Kima737e6d2014-06-12 23:56:12 +09002742 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002743
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002744 if (saved_ftrace_func != ftrace_trace_function) {
2745 saved_ftrace_func = ftrace_trace_function;
2746 command |= FTRACE_UPDATE_TRACE_FUNC;
2747 }
2748
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002749 if (!command || !ftrace_enabled) {
2750 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002751 * If these are per_cpu ops, they still need their
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002752 * per_cpu field freed. Since, function tracing is
2753 * not currently active, we can just free them
2754 * without synchronizing all CPUs.
2755 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002756 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2757 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002758 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002759 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002760
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002761 /*
2762 * If the ops uses a trampoline, then it needs to be
2763 * tested first on update.
2764 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002765 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002766 removed_ops = ops;
2767
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002768 /* The trampoline logic checks the old hashes */
2769 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2770 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2771
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002772 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002773
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002774 /*
2775 * If there's no more ops registered with ftrace, run a
2776 * sanity check to make sure all rec flags are cleared.
2777 */
2778 if (ftrace_ops_list == &ftrace_list_end) {
2779 struct ftrace_page *pg;
2780 struct dyn_ftrace *rec;
2781
2782 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002783 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002784 pr_warn(" %pS flags:%lx\n",
2785 (void *)rec->ip, rec->flags);
2786 } while_for_each_ftrace_rec();
2787 }
2788
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002789 ops->old_hash.filter_hash = NULL;
2790 ops->old_hash.notrace_hash = NULL;
2791
2792 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002793 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002794
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002795 /*
2796 * Dynamic ops may be freed, we must make sure that all
2797 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002798 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002799 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002800 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002801 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002802 /*
2803 * We need to do a hard force of sched synchronization.
2804 * This is because we use preempt_disable() to do RCU, but
2805 * the function tracers can be called where RCU is not watching
2806 * (like before user_exit()). We can not rely on the RCU
2807 * infrastructure to do the synchronization, thus we must do it
2808 * ourselves.
2809 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002810 schedule_on_each_cpu(ftrace_sync);
2811
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002812 /*
2813 * When the kernel is preeptive, tasks can be preempted
2814 * while on a ftrace trampoline. Just scheduling a task on
2815 * a CPU is not good enough to flush them. Calling
2816 * synchornize_rcu_tasks() will wait for those tasks to
2817 * execute and either schedule voluntarily or enter user space.
2818 */
2819 if (IS_ENABLED(CONFIG_PREEMPT))
2820 synchronize_rcu_tasks();
2821
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002822 arch_ftrace_trampoline_free(ops);
2823
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002824 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2825 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002826 }
2827
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002828 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002829}
2830
Ingo Molnare309b412008-05-12 21:20:51 +02002831static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002832{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302833 int command;
2834
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002835 if (unlikely(ftrace_disabled))
2836 return;
2837
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002838 /* Force update next time */
2839 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002840 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302841 if (ftrace_start_up) {
2842 command = FTRACE_UPDATE_CALLS;
2843 if (ftrace_graph_active)
2844 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002845 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302846 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002847}
2848
Ingo Molnare309b412008-05-12 21:20:51 +02002849static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002850{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302851 int command;
2852
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002853 if (unlikely(ftrace_disabled))
2854 return;
2855
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002856 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302857 if (ftrace_start_up) {
2858 command = FTRACE_DISABLE_CALLS;
2859 if (ftrace_graph_active)
2860 command |= FTRACE_STOP_FUNC_RET;
2861 ftrace_run_update_code(command);
2862 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002863}
2864
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002865static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002866unsigned long ftrace_update_tot_cnt;
2867
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002868static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002869{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002870 /*
2871 * Filter_hash being empty will default to trace module.
2872 * But notrace hash requires a test of individual module functions.
2873 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002874 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2875 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002876}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002877
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002878/*
2879 * Check if the current ops references the record.
2880 *
2881 * If the ops traces all functions, then it was already accounted for.
2882 * If the ops does not trace the current record function, skip it.
2883 * If the ops ignores the function via notrace filter, skip it.
2884 */
2885static inline bool
2886ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2887{
2888 /* If ops isn't enabled, ignore it */
2889 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2890 return 0;
2891
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002892 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002893 if (ops_traces_mod(ops))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002894 return 1;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002895
2896 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002897 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002898 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002899 return 0;
2900
2901 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002902 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002903 return 0;
2904
2905 return 1;
2906}
2907
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002908static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002909{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002910 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002911 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002912 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002913 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002914 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002915 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002916
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002917 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002918
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002919 /*
2920 * When a module is loaded, this function is called to convert
2921 * the calls to mcount in its text to nops, and also to create
2922 * an entry in the ftrace data. Now, if ftrace is activated
2923 * after this call, but before the module sets its text to
2924 * read-only, the modification of enabling ftrace can fail if
2925 * the read-only is done while ftrace is converting the calls.
2926 * To prevent this, the module's records are set as disabled
2927 * and will be enabled after the call to set the module's text
2928 * to read-only.
2929 */
2930 if (mod)
2931 rec_flags |= FTRACE_FL_DISABLED;
2932
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002933 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302934
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002935 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002936
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002937 /* If something went wrong, bail without enabling anything */
2938 if (unlikely(ftrace_disabled))
2939 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002940
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002941 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002942 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302943
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002944 /*
2945 * Do the initial record conversion from mcount jump
2946 * to the NOP instructions.
2947 */
2948 if (!ftrace_code_disable(mod, p))
2949 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002950
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002951 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002952 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002953 }
2954
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002955 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002956 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002957 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002958
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002959 return 0;
2960}
2961
Steven Rostedta7900872011-12-16 16:23:44 -05002962static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002963{
Steven Rostedta7900872011-12-16 16:23:44 -05002964 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002965 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002966
Steven Rostedta7900872011-12-16 16:23:44 -05002967 if (WARN_ON(!count))
2968 return -EINVAL;
2969
2970 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002971
2972 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002973 * We want to fill as much as possible. No more than a page
2974 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002975 */
Steven Rostedta7900872011-12-16 16:23:44 -05002976 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2977 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002978
Steven Rostedta7900872011-12-16 16:23:44 -05002979 again:
2980 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2981
2982 if (!pg->records) {
2983 /* if we can't allocate this size, try something smaller */
2984 if (!order)
2985 return -ENOMEM;
2986 order >>= 1;
2987 goto again;
2988 }
2989
2990 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2991 pg->size = cnt;
2992
2993 if (cnt > count)
2994 cnt = count;
2995
2996 return cnt;
2997}
2998
2999static struct ftrace_page *
3000ftrace_allocate_pages(unsigned long num_to_init)
3001{
3002 struct ftrace_page *start_pg;
3003 struct ftrace_page *pg;
3004 int order;
3005 int cnt;
3006
3007 if (!num_to_init)
3008 return 0;
3009
3010 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3011 if (!pg)
3012 return NULL;
3013
3014 /*
3015 * Try to allocate as much as possible in one continues
3016 * location that fills in all of the space. We want to
3017 * waste as little space as possible.
3018 */
3019 for (;;) {
3020 cnt = ftrace_allocate_records(pg, num_to_init);
3021 if (cnt < 0)
3022 goto free_pages;
3023
3024 num_to_init -= cnt;
3025 if (!num_to_init)
3026 break;
3027
3028 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3029 if (!pg->next)
3030 goto free_pages;
3031
3032 pg = pg->next;
3033 }
3034
3035 return start_pg;
3036
3037 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003038 pg = start_pg;
3039 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003040 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3041 free_pages((unsigned long)pg->records, order);
3042 start_pg = pg->next;
3043 kfree(pg);
3044 pg = start_pg;
3045 }
3046 pr_info("ftrace: FAILED to allocate memory for functions\n");
3047 return NULL;
3048}
3049
Steven Rostedt5072c592008-05-12 21:20:43 +02003050#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3051
3052struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003053 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003054 loff_t func_pos;
3055 struct ftrace_page *pg;
3056 struct dyn_ftrace *func;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003057 struct ftrace_probe_ops *probe;
3058 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003059 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003060 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003061 struct ftrace_ops *ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003062 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003063 int idx;
3064 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003065};
3066
Ingo Molnare309b412008-05-12 21:20:51 +02003067static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003068t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003069{
3070 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003071 struct trace_array *tr = global_ops.private;
3072 struct list_head *func_probes;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003073 struct ftrace_hash *hash;
3074 struct list_head *next;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003075 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003076 struct hlist_head *hhd;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003077 int size;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003078
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003079 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003080 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003081
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003082 if (!tr)
3083 return NULL;
3084
3085 func_probes = &tr->func_probes;
3086 if (list_empty(func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003087 return NULL;
3088
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003089 if (!iter->probe) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003090 next = func_probes->next;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003091 iter->probe = list_entry(next, struct ftrace_probe_ops, list);
3092 }
3093
3094 if (iter->probe_entry)
3095 hnd = &iter->probe_entry->hlist;
3096
3097 hash = iter->probe->ops.func_hash->filter_hash;
3098 size = 1 << hash->size_bits;
3099
3100 retry:
3101 if (iter->pidx >= size) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003102 if (iter->probe->list.next == func_probes)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003103 return NULL;
3104 next = iter->probe->list.next;
3105 iter->probe = list_entry(next, struct ftrace_probe_ops, list);
3106 hash = iter->probe->ops.func_hash->filter_hash;
3107 size = 1 << hash->size_bits;
3108 iter->pidx = 0;
3109 }
3110
3111 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003112
3113 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003114 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003115 hnd = NULL;
3116 goto retry;
3117 }
3118
3119 if (!hnd)
3120 hnd = hhd->first;
3121 else {
3122 hnd = hnd->next;
3123 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003124 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003125 goto retry;
3126 }
3127 }
3128
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003129 if (WARN_ON_ONCE(!hnd))
3130 return NULL;
3131
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003132 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003133
3134 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003135}
3136
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003137static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003138{
3139 struct ftrace_iterator *iter = m->private;
3140 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003141 loff_t l;
3142
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003143 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003144 return NULL;
3145
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003146 if (iter->func_pos > *pos)
3147 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003148
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003149 iter->probe = NULL;
3150 iter->probe_entry = NULL;
3151 iter->pidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003152 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003153 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003154 if (!p)
3155 break;
3156 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003157 if (!p)
3158 return NULL;
3159
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003160 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003161 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003162
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003163 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003164}
3165
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003166static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003167t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003168{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003169 struct ftrace_probe_ops *probe;
3170 struct ftrace_func_entry *probe_entry;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003171
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003172 probe = iter->probe;
3173 probe_entry = iter->probe_entry;
3174
3175 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003176 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003177
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003178 if (probe->print)
3179 return probe->print(m, probe_entry->ip, probe, NULL);
Steven Rostedt809dcf22009-02-16 23:06:01 -05003180
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003181 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip, (void *)probe->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003182
3183 return 0;
3184}
3185
3186static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003187t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003188{
3189 struct ftrace_iterator *iter = m->private;
3190 struct dyn_ftrace *rec = NULL;
3191
3192 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003193
Steven Rostedt5072c592008-05-12 21:20:43 +02003194 retry:
3195 if (iter->idx >= iter->pg->index) {
3196 if (iter->pg->next) {
3197 iter->pg = iter->pg->next;
3198 iter->idx = 0;
3199 goto retry;
3200 }
3201 } else {
3202 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003203 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3204 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003205
3206 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003207 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003208
Steven Rostedt5072c592008-05-12 21:20:43 +02003209 rec = NULL;
3210 goto retry;
3211 }
3212 }
3213
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003214 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003215 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003216
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003217 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003218 iter->func = rec;
3219
3220 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003221}
3222
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003223static void *
3224t_next(struct seq_file *m, void *v, loff_t *pos)
3225{
3226 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)fcdc7122017-04-17 10:22:29 -04003227 loff_t l = *pos; /* t_hash_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003228 void *ret;
3229
3230 if (unlikely(ftrace_disabled))
3231 return NULL;
3232
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003233 if (iter->flags & FTRACE_ITER_PROBE)
3234 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003235
3236 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003237 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003238 (*pos)++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003239 return t_probe_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003240 }
3241
3242 ret = t_func_next(m, pos);
3243
3244 if (!ret)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003245 return t_probe_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003246
3247 return ret;
3248}
3249
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003250static void reset_iter_read(struct ftrace_iterator *iter)
3251{
3252 iter->pos = 0;
3253 iter->func_pos = 0;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003254 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE);
Steven Rostedt5072c592008-05-12 21:20:43 +02003255}
3256
3257static void *t_start(struct seq_file *m, loff_t *pos)
3258{
3259 struct ftrace_iterator *iter = m->private;
3260 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003261 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003262
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003263 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003264
3265 if (unlikely(ftrace_disabled))
3266 return NULL;
3267
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003268 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003269 * If an lseek was done, then reset and start from beginning.
3270 */
3271 if (*pos < iter->pos)
3272 reset_iter_read(iter);
3273
3274 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003275 * For set_ftrace_filter reading, if we have the filter
3276 * off, we can short cut and just print out that all
3277 * functions are enabled.
3278 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003279 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3280 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003281 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003282 if (*pos > 0)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003283 return t_probe_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003284 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003285 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003286 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003287 return iter;
3288 }
3289
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003290 if (iter->flags & FTRACE_ITER_PROBE)
3291 return t_probe_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003292
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003293 /*
3294 * Unfortunately, we need to restart at ftrace_pages_start
3295 * every time we let go of the ftrace_mutex. This is because
3296 * those pointers can change without the lock.
3297 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003298 iter->pg = ftrace_pages_start;
3299 iter->idx = 0;
3300 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003301 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003302 if (!p)
3303 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003304 }
walimis5821e1b2008-11-15 15:19:06 +08003305
Steven Rostedt69a30832011-12-19 15:21:16 -05003306 if (!p)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003307 return t_probe_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003308
3309 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003310}
3311
3312static void t_stop(struct seq_file *m, void *p)
3313{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003314 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003315}
3316
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003317void * __weak
3318arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3319{
3320 return NULL;
3321}
3322
3323static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3324 struct dyn_ftrace *rec)
3325{
3326 void *ptr;
3327
3328 ptr = arch_ftrace_trampoline_func(ops, rec);
3329 if (ptr)
3330 seq_printf(m, " ->%pS", ptr);
3331}
3332
Steven Rostedt5072c592008-05-12 21:20:43 +02003333static int t_show(struct seq_file *m, void *v)
3334{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003335 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003336 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003337
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003338 if (iter->flags & FTRACE_ITER_PROBE)
3339 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003340
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003341 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003342 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003343 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003344 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003345 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003346 return 0;
3347 }
3348
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003349 rec = iter->func;
3350
Steven Rostedt5072c592008-05-12 21:20:43 +02003351 if (!rec)
3352 return 0;
3353
Steven Rostedt647bcd02011-05-03 14:39:21 -04003354 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003355 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003356 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003357
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003358 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003359 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003360 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3361 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003362 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003363 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003364 if (ops) {
3365 do {
3366 seq_printf(m, "\ttramp: %pS (%pS)",
3367 (void *)ops->trampoline,
3368 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003369 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003370 ops = ftrace_find_tramp_ops_next(rec, ops);
3371 } while (ops);
3372 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003373 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003374 } else {
3375 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003376 }
3377 }
3378
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003379 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003380
3381 return 0;
3382}
3383
James Morris88e9d342009-09-22 16:43:43 -07003384static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003385 .start = t_start,
3386 .next = t_next,
3387 .stop = t_stop,
3388 .show = t_show,
3389};
3390
Ingo Molnare309b412008-05-12 21:20:51 +02003391static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003392ftrace_avail_open(struct inode *inode, struct file *file)
3393{
3394 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003395
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003396 if (unlikely(ftrace_disabled))
3397 return -ENODEV;
3398
Jiri Olsa50e18b92012-04-25 10:23:39 +02003399 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003400 if (!iter)
3401 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003402
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003403 iter->pg = ftrace_pages_start;
3404 iter->ops = &global_ops;
3405
3406 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003407}
3408
Steven Rostedt647bcd02011-05-03 14:39:21 -04003409static int
3410ftrace_enabled_open(struct inode *inode, struct file *file)
3411{
3412 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003413
Jiri Olsa50e18b92012-04-25 10:23:39 +02003414 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003415 if (!iter)
3416 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003417
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003418 iter->pg = ftrace_pages_start;
3419 iter->flags = FTRACE_ITER_ENABLED;
3420 iter->ops = &global_ops;
3421
3422 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003423}
3424
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003425/**
3426 * ftrace_regex_open - initialize function tracer filter files
3427 * @ops: The ftrace_ops that hold the hash filters
3428 * @flag: The type of filter to process
3429 * @inode: The inode, usually passed in to your open routine
3430 * @file: The file, usually passed in to your open routine
3431 *
3432 * ftrace_regex_open() initializes the filter files for the
3433 * @ops. Depending on @flag it may process the filter hash or
3434 * the notrace hash of @ops. With this called from the open
3435 * routine, you can use ftrace_filter_write() for the write
3436 * routine if @flag has FTRACE_ITER_FILTER set, or
3437 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003438 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003439 * release must call ftrace_regex_release().
3440 */
3441int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003442ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003443 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003444{
3445 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003446 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003447 int ret = 0;
3448
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003449 ftrace_ops_init(ops);
3450
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003451 if (unlikely(ftrace_disabled))
3452 return -ENODEV;
3453
Steven Rostedt5072c592008-05-12 21:20:43 +02003454 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3455 if (!iter)
3456 return -ENOMEM;
3457
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003458 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3459 kfree(iter);
3460 return -ENOMEM;
3461 }
3462
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003463 iter->ops = ops;
3464 iter->flags = flag;
3465
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003466 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003467
Steven Rostedtf45948e2011-05-02 12:29:25 -04003468 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003469 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003470 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003471 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003472
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003473 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003474 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3475
3476 if (file->f_flags & O_TRUNC)
3477 iter->hash = alloc_ftrace_hash(size_bits);
3478 else
3479 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3480
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003481 if (!iter->hash) {
3482 trace_parser_put(&iter->parser);
3483 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003484 ret = -ENOMEM;
3485 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003486 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003487 } else
3488 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003489
Steven Rostedt5072c592008-05-12 21:20:43 +02003490 if (file->f_mode & FMODE_READ) {
3491 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003492
3493 ret = seq_open(file, &show_ftrace_seq_ops);
3494 if (!ret) {
3495 struct seq_file *m = file->private_data;
3496 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003497 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003498 /* Failed */
3499 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003500 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003501 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003502 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003503 } else
3504 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003505
3506 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003507 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003508
3509 return ret;
3510}
3511
Steven Rostedt41c52c02008-05-22 11:46:33 -04003512static int
3513ftrace_filter_open(struct inode *inode, struct file *file)
3514{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003515 struct ftrace_ops *ops = inode->i_private;
3516
3517 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003518 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003519 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003520}
3521
3522static int
3523ftrace_notrace_open(struct inode *inode, struct file *file)
3524{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003525 struct ftrace_ops *ops = inode->i_private;
3526
3527 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003528 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003529}
3530
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003531/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3532struct ftrace_glob {
3533 char *search;
3534 unsigned len;
3535 int type;
3536};
3537
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003538/*
3539 * If symbols in an architecture don't correspond exactly to the user-visible
3540 * name of what they represent, it is possible to define this function to
3541 * perform the necessary adjustments.
3542*/
3543char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3544{
3545 return str;
3546}
3547
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003548static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003549{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003550 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003551 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003552
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003553 str = arch_ftrace_match_adjust(str, g->search);
3554
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003555 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003556 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003557 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003558 matched = 1;
3559 break;
3560 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003561 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003562 matched = 1;
3563 break;
3564 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003565 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003566 matched = 1;
3567 break;
3568 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003569 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003570 if (slen >= g->len &&
3571 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003572 matched = 1;
3573 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003574 case MATCH_GLOB:
3575 if (glob_match(g->search, str))
3576 matched = 1;
3577 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003578 }
3579
3580 return matched;
3581}
3582
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003583static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003584enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003585{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003586 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003587 int ret = 0;
3588
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003589 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003590 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003591 /* Do nothing if it doesn't exist */
3592 if (!entry)
3593 return 0;
3594
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003595 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003596 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003597 /* Do nothing if it exists */
3598 if (entry)
3599 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003600
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003601 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003602 }
3603 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003604}
3605
Steven Rostedt64e7c442009-02-13 17:08:48 -05003606static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003607ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3608 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003609{
3610 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003611 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003612
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003613 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3614
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003615 if (mod_g) {
3616 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3617
3618 /* blank module name to match all modules */
3619 if (!mod_g->len) {
3620 /* blank module globbing: modname xor exclude_mod */
3621 if ((!exclude_mod) != (!modname))
3622 goto func_match;
3623 return 0;
3624 }
3625
3626 /* not matching the module */
3627 if (!modname || !mod_matches) {
3628 if (exclude_mod)
3629 goto func_match;
3630 else
3631 return 0;
3632 }
3633
3634 if (mod_matches && exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003635 return 0;
3636
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003637func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003638 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003639 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003640 return 1;
3641 }
3642
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003643 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003644}
3645
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003646static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003647match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003648{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003649 struct ftrace_page *pg;
3650 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003651 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003652 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3653 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3654 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003655 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003656 int ret;
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003657 int clear_filter;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003658
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003659 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003660 func_g.type = filter_parse_regex(func, len, &func_g.search,
3661 &clear_filter);
3662 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003663 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003664
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003665 if (mod) {
3666 mod_g.type = filter_parse_regex(mod, strlen(mod),
3667 &mod_g.search, &exclude_mod);
3668 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003669 }
3670
Steven Rostedt52baf112009-02-14 01:15:39 -05003671 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003672
3673 if (unlikely(ftrace_disabled))
3674 goto out_unlock;
3675
Steven Rostedt265c8312009-02-13 12:43:56 -05003676 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003677
3678 if (rec->flags & FTRACE_FL_DISABLED)
3679 continue;
3680
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003681 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003682 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003683 if (ret < 0) {
3684 found = ret;
3685 goto out_unlock;
3686 }
Li Zefan311d16d2009-12-08 11:15:11 +08003687 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003688 }
3689 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003690 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003691 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003692
3693 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003694}
3695
Steven Rostedt64e7c442009-02-13 17:08:48 -05003696static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003697ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003698{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003699 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003700}
3701
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003702static void ftrace_ops_update_code(struct ftrace_ops *ops,
3703 struct ftrace_ops_hash *old_hash)
3704{
3705 struct ftrace_ops *op;
3706
3707 if (!ftrace_enabled)
3708 return;
3709
3710 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3711 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3712 return;
3713 }
3714
3715 /*
3716 * If this is the shared global_ops filter, then we need to
3717 * check if there is another ops that shares it, is enabled.
3718 * If so, we still need to run the modify code.
3719 */
3720 if (ops->func_hash != &global_ops.local_hash)
3721 return;
3722
3723 do_for_each_ftrace_op(op, ftrace_ops_list) {
3724 if (op->func_hash == &global_ops.local_hash &&
3725 op->flags & FTRACE_OPS_FL_ENABLED) {
3726 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3727 /* Only need to do this once */
3728 return;
3729 }
3730 } while_for_each_ftrace_op(op);
3731}
3732
3733static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3734 struct ftrace_hash **orig_hash,
3735 struct ftrace_hash *hash,
3736 int enable)
3737{
3738 struct ftrace_ops_hash old_hash_ops;
3739 struct ftrace_hash *old_hash;
3740 int ret;
3741
3742 old_hash = *orig_hash;
3743 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3744 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3745 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3746 if (!ret) {
3747 ftrace_ops_update_code(ops, &old_hash_ops);
3748 free_ftrace_hash_rcu(old_hash);
3749 }
3750 return ret;
3751}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003752
Steven Rostedtf6180772009-02-14 00:40:25 -05003753/*
3754 * We register the module command as a template to show others how
3755 * to register the a command as well.
3756 */
3757
3758static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003759ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003760 char *func, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003761{
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003762 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003763
3764 /*
3765 * cmd == 'mod' because we only registered this func
3766 * for the 'mod' ftrace_func_command.
3767 * But if you register one func with multiple commands,
3768 * you can tell which command was used by the cmd
3769 * parameter.
3770 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003771 ret = match_records(hash, func, strlen(func), module);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003772 if (!ret)
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003773 return -EINVAL;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003774 if (ret < 0)
3775 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003776 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003777}
3778
3779static struct ftrace_func_command ftrace_mod_cmd = {
3780 .name = "mod",
3781 .func = ftrace_mod_callback,
3782};
3783
3784static int __init ftrace_mod_cmd_init(void)
3785{
3786 return register_ftrace_command(&ftrace_mod_cmd);
3787}
Steven Rostedt6f415672012-10-05 12:13:07 -04003788core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003789
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003790static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003791 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003792{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003793 struct ftrace_probe_ops *probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003794
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003795 probe_ops = container_of(op, struct ftrace_probe_ops, ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003796
3797 /*
3798 * Disable preemption for these calls to prevent a RCU grace
3799 * period. This syncs the hash iteration and freeing of items
3800 * on the hash. rcu_read_lock is too dangerous here.
3801 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003802 preempt_disable_notrace();
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003803 probe_ops->func(ip, parent_ip, probe_ops, NULL);
Steven Rostedt5168ae52010-06-03 09:36:50 -04003804 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003805}
3806
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04003807struct ftrace_func_map {
3808 struct ftrace_func_entry entry;
3809 void *data;
3810};
3811
3812struct ftrace_func_mapper {
3813 struct ftrace_hash hash;
3814};
3815
3816/**
3817 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
3818 *
3819 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
3820 */
3821struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
3822{
3823 struct ftrace_hash *hash;
3824
3825 /*
3826 * The mapper is simply a ftrace_hash, but since the entries
3827 * in the hash are not ftrace_func_entry type, we define it
3828 * as a separate structure.
3829 */
3830 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3831 return (struct ftrace_func_mapper *)hash;
3832}
3833
3834/**
3835 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
3836 * @mapper: The mapper that has the ip maps
3837 * @ip: the instruction pointer to find the data for
3838 *
3839 * Returns the data mapped to @ip if found otherwise NULL. The return
3840 * is actually the address of the mapper data pointer. The address is
3841 * returned for use cases where the data is no bigger than a long, and
3842 * the user can use the data pointer as its data instead of having to
3843 * allocate more memory for the reference.
3844 */
3845void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
3846 unsigned long ip)
3847{
3848 struct ftrace_func_entry *entry;
3849 struct ftrace_func_map *map;
3850
3851 entry = ftrace_lookup_ip(&mapper->hash, ip);
3852 if (!entry)
3853 return NULL;
3854
3855 map = (struct ftrace_func_map *)entry;
3856 return &map->data;
3857}
3858
3859/**
3860 * ftrace_func_mapper_add_ip - Map some data to an ip
3861 * @mapper: The mapper that has the ip maps
3862 * @ip: The instruction pointer address to map @data to
3863 * @data: The data to map to @ip
3864 *
3865 * Returns 0 on succes otherwise an error.
3866 */
3867int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
3868 unsigned long ip, void *data)
3869{
3870 struct ftrace_func_entry *entry;
3871 struct ftrace_func_map *map;
3872
3873 entry = ftrace_lookup_ip(&mapper->hash, ip);
3874 if (entry)
3875 return -EBUSY;
3876
3877 map = kmalloc(sizeof(*map), GFP_KERNEL);
3878 if (!map)
3879 return -ENOMEM;
3880
3881 map->entry.ip = ip;
3882 map->data = data;
3883
3884 __add_hash_entry(&mapper->hash, &map->entry);
3885
3886 return 0;
3887}
3888
3889/**
3890 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
3891 * @mapper: The mapper that has the ip maps
3892 * @ip: The instruction pointer address to remove the data from
3893 *
3894 * Returns the data if it is found, otherwise NULL.
3895 * Note, if the data pointer is used as the data itself, (see
3896 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
3897 * if the data pointer was set to zero.
3898 */
3899void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
3900 unsigned long ip)
3901{
3902 struct ftrace_func_entry *entry;
3903 struct ftrace_func_map *map;
3904 void *data;
3905
3906 entry = ftrace_lookup_ip(&mapper->hash, ip);
3907 if (!entry)
3908 return NULL;
3909
3910 map = (struct ftrace_func_map *)entry;
3911 data = map->data;
3912
3913 remove_hash_entry(&mapper->hash, entry);
3914 kfree(entry);
3915
3916 return data;
3917}
3918
3919/**
3920 * free_ftrace_func_mapper - free a mapping of ips and data
3921 * @mapper: The mapper that has the ip maps
3922 * @free_func: A function to be called on each data item.
3923 *
3924 * This is used to free the function mapper. The @free_func is optional
3925 * and can be used if the data needs to be freed as well.
3926 */
3927void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
3928 ftrace_mapper_func free_func)
3929{
3930 struct ftrace_func_entry *entry;
3931 struct ftrace_func_map *map;
3932 struct hlist_head *hhd;
3933 int size = 1 << mapper->hash.size_bits;
3934 int i;
3935
3936 if (free_func && mapper->hash.count) {
3937 for (i = 0; i < size; i++) {
3938 hhd = &mapper->hash.buckets[i];
3939 hlist_for_each_entry(entry, hhd, hlist) {
3940 map = (struct ftrace_func_map *)entry;
3941 free_func(map);
3942 }
3943 }
3944 }
3945 free_ftrace_hash(&mapper->hash);
3946}
3947
Steven Rostedt59df055f2009-02-14 15:29:06 -05003948int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003949register_ftrace_function_probe(char *glob, struct trace_array *tr,
3950 struct ftrace_probe_ops *ops, void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003951{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003952 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003953 struct ftrace_hash **orig_hash;
3954 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003955 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003956 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003957 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003958 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003959 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003960
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04003961 if (WARN_ON(!tr))
3962 return -EINVAL;
3963
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003964 /* We do not support '!' for function probes */
3965 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003966 return -EINVAL;
3967
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003968 if (!(ops->ops.flags & FTRACE_OPS_FL_INITIALIZED)) {
3969 ops->ops.func = function_trace_probe_call;
3970 ftrace_ops_init(&ops->ops);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003971 INIT_LIST_HEAD(&ops->list);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003972 }
3973
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003974 mutex_lock(&ops->ops.func_hash->regex_lock);
3975
3976 orig_hash = &ops->ops.func_hash->filter_hash;
3977 old_hash = *orig_hash;
3978 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3979
3980 ret = ftrace_match_records(hash, glob, strlen(glob));
3981
3982 /* Nothing found? */
3983 if (!ret)
3984 ret = -EINVAL;
3985
3986 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003987 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003988
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003989 size = 1 << hash->size_bits;
3990 for (i = 0; i < size; i++) {
3991 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
3992 if (ftrace_lookup_ip(old_hash, entry->ip))
3993 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003994 /*
3995 * The caller might want to do something special
3996 * for each function we find. We call the callback
3997 * to give the caller an opportunity to do so.
3998 */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003999 if (ops->init) {
4000 ret = ops->init(ops, entry->ip, data);
4001 if (ret < 0)
4002 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004003 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004004 count++;
4005 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004006 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004007
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004008 mutex_lock(&ftrace_lock);
4009
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004010 ret = ftrace_hash_move_and_update_ops(&ops->ops, orig_hash,
4011 hash, 1);
4012 if (ret < 0)
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004013 goto err_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004014
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004015 if (list_empty(&ops->list))
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004016 list_add(&ops->list, &tr->func_probes);
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004017
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004018 if (!(ops->ops.flags & FTRACE_OPS_FL_ENABLED))
4019 ret = ftrace_startup(&ops->ops, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004020
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004021 out_unlock:
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004022 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004023
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004024 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004025 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004026 out:
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004027 mutex_unlock(&ops->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004028 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004029
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004030 return ret;
Steven Rostedt (VMware)8d707252017-04-05 13:36:18 -04004031
4032 err_unlock:
4033 if (!ops->free)
4034 goto out_unlock;
4035
4036 /* Failed to do the move, need to call the free functions */
4037 for (i = 0; i < size; i++) {
4038 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4039 if (ftrace_lookup_ip(old_hash, entry->ip))
4040 continue;
4041 ops->free(ops, entry->ip, NULL);
4042 }
4043 }
4044 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004045}
4046
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004047int
Steven Rostedt (VMware)78f78e02017-04-04 13:39:59 -04004048unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004049{
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004050 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004051 struct ftrace_func_entry *entry;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004052 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004053 struct ftrace_hash **orig_hash;
4054 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004055 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004056 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004057 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004058 char str[KSYM_SYMBOL_LEN];
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004059 int i, ret;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004060 int size;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004061
4062 if (!(ops->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4063 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004064
Atsushi Tsujib36461d2009-09-15 19:06:30 +09004065 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004066 func_g.search = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09004067 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004068 int not;
4069
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004070 func_g.type = filter_parse_regex(glob, strlen(glob),
4071 &func_g.search, &not);
4072 func_g.len = strlen(func_g.search);
4073 func_g.search = glob;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004074
Steven Rostedtb6887d72009-02-17 12:32:04 -05004075 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004076 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004077 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004078 }
4079
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004080 mutex_lock(&ops->ops.func_hash->regex_lock);
4081
4082 orig_hash = &ops->ops.func_hash->filter_hash;
4083 old_hash = *orig_hash;
4084
4085 ret = -EINVAL;
4086 if (ftrace_hash_empty(old_hash))
4087 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004088
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004089 old_hash_ops.filter_hash = old_hash;
4090 /* Probes only have filters */
4091 old_hash_ops.notrace_hash = NULL;
4092
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004093 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004094 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004095 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004096 goto out_unlock;
4097
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004098 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004099
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004100 size = 1 << hash->size_bits;
4101 for (i = 0; i < size; i++) {
4102 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004103
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004104 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004105 kallsyms_lookup(entry->ip, NULL, NULL,
4106 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004107 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004108 continue;
4109 }
4110
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004111 remove_hash_entry(hash, entry);
4112 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004113 }
4114 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004115
4116 /* Nothing found? */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004117 if (hlist_empty(&hhd)) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004118 ret = -EINVAL;
4119 goto out_unlock;
4120 }
4121
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004122 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004123
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004124 if (ftrace_hash_empty(hash)) {
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004125 ftrace_shutdown(&ops->ops, 0);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004126 list_del_init(&ops->list);
4127 }
4128
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004129
4130 ret = ftrace_hash_move_and_update_ops(&ops->ops, orig_hash,
4131 hash, 1);
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004132
4133 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004134 if (ftrace_enabled && !ftrace_hash_empty(hash))
4135 ftrace_run_modify_code(&ops->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004136 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004137 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004138
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004139 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4140 hlist_del(&entry->hlist);
4141 if (ops->free)
4142 ops->free(ops, entry->ip, NULL);
4143 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004144 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004145 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004146
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004147 out_unlock:
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004148 mutex_unlock(&ops->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004149 free_ftrace_hash(hash);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004150 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004151}
4152
Steven Rostedtf6180772009-02-14 00:40:25 -05004153static LIST_HEAD(ftrace_commands);
4154static DEFINE_MUTEX(ftrace_cmd_mutex);
4155
Tom Zanussi38de93a2013-10-24 08:34:18 -05004156/*
4157 * Currently we only register ftrace commands from __init, so mark this
4158 * __init too.
4159 */
4160__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004161{
4162 struct ftrace_func_command *p;
4163 int ret = 0;
4164
4165 mutex_lock(&ftrace_cmd_mutex);
4166 list_for_each_entry(p, &ftrace_commands, list) {
4167 if (strcmp(cmd->name, p->name) == 0) {
4168 ret = -EBUSY;
4169 goto out_unlock;
4170 }
4171 }
4172 list_add(&cmd->list, &ftrace_commands);
4173 out_unlock:
4174 mutex_unlock(&ftrace_cmd_mutex);
4175
4176 return ret;
4177}
4178
Tom Zanussi38de93a2013-10-24 08:34:18 -05004179/*
4180 * Currently we only unregister ftrace commands from __init, so mark
4181 * this __init too.
4182 */
4183__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004184{
4185 struct ftrace_func_command *p, *n;
4186 int ret = -ENODEV;
4187
4188 mutex_lock(&ftrace_cmd_mutex);
4189 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4190 if (strcmp(cmd->name, p->name) == 0) {
4191 ret = 0;
4192 list_del_init(&p->list);
4193 goto out_unlock;
4194 }
4195 }
4196 out_unlock:
4197 mutex_unlock(&ftrace_cmd_mutex);
4198
4199 return ret;
4200}
4201
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004202static int ftrace_process_regex(struct ftrace_iterator *iter,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004203 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004204{
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004205 struct ftrace_hash *hash = iter->hash;
4206 struct trace_array *tr = global_ops.private;
Steven Rostedtf6180772009-02-14 00:40:25 -05004207 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004208 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004209 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004210
4211 func = strsep(&next, ":");
4212
4213 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004214 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004215 if (!ret)
4216 ret = -EINVAL;
4217 if (ret < 0)
4218 return ret;
4219 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004220 }
4221
Steven Rostedtf6180772009-02-14 00:40:25 -05004222 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004223
4224 command = strsep(&next, ":");
4225
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004226 if (WARN_ON_ONCE(!tr))
4227 return -EINVAL;
4228
Steven Rostedtf6180772009-02-14 00:40:25 -05004229 mutex_lock(&ftrace_cmd_mutex);
4230 list_for_each_entry(p, &ftrace_commands, list) {
4231 if (strcmp(p->name, command) == 0) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004232 ret = p->func(tr, hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004233 goto out_unlock;
4234 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004235 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004236 out_unlock:
4237 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004238
Steven Rostedtf6180772009-02-14 00:40:25 -05004239 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004240}
4241
Ingo Molnare309b412008-05-12 21:20:51 +02004242static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004243ftrace_regex_write(struct file *file, const char __user *ubuf,
4244 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004245{
4246 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004247 struct trace_parser *parser;
4248 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004249
Li Zefan4ba79782009-09-22 13:52:20 +08004250 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004251 return 0;
4252
Steven Rostedt5072c592008-05-12 21:20:43 +02004253 if (file->f_mode & FMODE_READ) {
4254 struct seq_file *m = file->private_data;
4255 iter = m->private;
4256 } else
4257 iter = file->private_data;
4258
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004259 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004260 return -ENODEV;
4261
4262 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004263
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004264 parser = &iter->parser;
4265 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004266
Li Zefan4ba79782009-09-22 13:52:20 +08004267 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004268 !trace_parser_cont(parser)) {
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04004269 ret = ftrace_process_regex(iter, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004270 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004271 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004272 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004273 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004274 }
4275
Steven Rostedt5072c592008-05-12 21:20:43 +02004276 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004277 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004278 return ret;
4279}
4280
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004281ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004282ftrace_filter_write(struct file *file, const char __user *ubuf,
4283 size_t cnt, loff_t *ppos)
4284{
4285 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4286}
4287
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004288ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004289ftrace_notrace_write(struct file *file, const char __user *ubuf,
4290 size_t cnt, loff_t *ppos)
4291{
4292 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4293}
4294
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004295static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004296ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4297{
4298 struct ftrace_func_entry *entry;
4299
4300 if (!ftrace_location(ip))
4301 return -EINVAL;
4302
4303 if (remove) {
4304 entry = ftrace_lookup_ip(hash, ip);
4305 if (!entry)
4306 return -ENOENT;
4307 free_hash_entry(hash, entry);
4308 return 0;
4309 }
4310
4311 return add_hash_entry(hash, ip);
4312}
4313
4314static int
4315ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4316 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004317{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004318 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004319 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004320 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004321
Steven Rostedt41c52c02008-05-22 11:46:33 -04004322 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004323 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004324
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004325 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004326
Steven Rostedtf45948e2011-05-02 12:29:25 -04004327 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004328 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004329 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004330 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004331
Wang Nanb972cc52014-07-15 08:40:20 +08004332 if (reset)
4333 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4334 else
4335 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4336
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004337 if (!hash) {
4338 ret = -ENOMEM;
4339 goto out_regex_unlock;
4340 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004341
Jiri Olsaac483c42012-01-02 10:04:14 +01004342 if (buf && !ftrace_match_records(hash, buf, len)) {
4343 ret = -EINVAL;
4344 goto out_regex_unlock;
4345 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004346 if (ip) {
4347 ret = ftrace_match_addr(hash, ip, remove);
4348 if (ret < 0)
4349 goto out_regex_unlock;
4350 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004351
4352 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004353 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004354 mutex_unlock(&ftrace_lock);
4355
Jiri Olsaac483c42012-01-02 10:04:14 +01004356 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004357 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004358
4359 free_ftrace_hash(hash);
4360 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004361}
4362
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004363static int
4364ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4365 int reset, int enable)
4366{
4367 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4368}
4369
4370/**
4371 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4372 * @ops - the ops to set the filter with
4373 * @ip - the address to add to or remove from the filter.
4374 * @remove - non zero to remove the ip from the filter
4375 * @reset - non zero to reset all filters before applying this filter.
4376 *
4377 * Filters denote which functions should be enabled when tracing is enabled
4378 * If @ip is NULL, it failes to update filter.
4379 */
4380int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4381 int remove, int reset)
4382{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004383 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004384 return ftrace_set_addr(ops, ip, remove, reset, 1);
4385}
4386EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4387
Joel Fernandesd032ae82016-11-15 12:31:20 -08004388/**
4389 * ftrace_ops_set_global_filter - setup ops to use global filters
4390 * @ops - the ops which will use the global filters
4391 *
4392 * ftrace users who need global function trace filtering should call this.
4393 * It can set the global filter only if ops were not initialized before.
4394 */
4395void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4396{
4397 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4398 return;
4399
4400 ftrace_ops_init(ops);
4401 ops->func_hash = &global_ops.local_hash;
4402}
4403EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4404
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004405static int
4406ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4407 int reset, int enable)
4408{
4409 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4410}
4411
Steven Rostedt77a2b372008-05-12 21:20:45 +02004412/**
4413 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004414 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004415 * @buf - the string that holds the function filter text.
4416 * @len - the length of the string.
4417 * @reset - non zero to reset all filters before applying this filter.
4418 *
4419 * Filters denote which functions should be enabled when tracing is enabled.
4420 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4421 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004422int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004423 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004424{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004425 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004426 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004427}
Steven Rostedt936e0742011-05-05 22:54:01 -04004428EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004429
Steven Rostedt41c52c02008-05-22 11:46:33 -04004430/**
4431 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004432 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004433 * @buf - the string that holds the function notrace text.
4434 * @len - the length of the string.
4435 * @reset - non zero to reset all filters before applying this filter.
4436 *
4437 * Notrace Filters denote which functions should not be enabled when tracing
4438 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4439 * for tracing.
4440 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004441int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004442 int len, int reset)
4443{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004444 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004445 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004446}
4447EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4448/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004449 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004450 * @buf - the string that holds the function filter text.
4451 * @len - the length of the string.
4452 * @reset - non zero to reset all filters before applying this filter.
4453 *
4454 * Filters denote which functions should be enabled when tracing is enabled.
4455 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4456 */
4457void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4458{
4459 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4460}
4461EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4462
4463/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004464 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004465 * @buf - the string that holds the function notrace text.
4466 * @len - the length of the string.
4467 * @reset - non zero to reset all filters before applying this filter.
4468 *
4469 * Notrace Filters denote which functions should not be enabled when tracing
4470 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4471 * for tracing.
4472 */
4473void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004474{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004475 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004476}
Steven Rostedt936e0742011-05-05 22:54:01 -04004477EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004478
Steven Rostedt2af15d62009-05-28 13:37:24 -04004479/*
4480 * command line interface to allow users to set filters on boot up.
4481 */
4482#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4483static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4484static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4485
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004486/* Used by function selftest to not test if filter is set */
4487bool ftrace_filter_param __initdata;
4488
Steven Rostedt2af15d62009-05-28 13:37:24 -04004489static int __init set_ftrace_notrace(char *str)
4490{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004491 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004492 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004493 return 1;
4494}
4495__setup("ftrace_notrace=", set_ftrace_notrace);
4496
4497static int __init set_ftrace_filter(char *str)
4498{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004499 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004500 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004501 return 1;
4502}
4503__setup("ftrace_filter=", set_ftrace_filter);
4504
Stefan Assmann369bc182009-10-12 22:17:21 +02004505#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004506static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004507static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004508static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004509
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004510static unsigned long save_global_trampoline;
4511static unsigned long save_global_flags;
4512
Stefan Assmann369bc182009-10-12 22:17:21 +02004513static int __init set_graph_function(char *str)
4514{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004515 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004516 return 1;
4517}
4518__setup("ftrace_graph_filter=", set_graph_function);
4519
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004520static int __init set_graph_notrace_function(char *str)
4521{
4522 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4523 return 1;
4524}
4525__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4526
Todd Brandt65a50c652017-03-02 16:12:15 -08004527static int __init set_graph_max_depth_function(char *str)
4528{
4529 if (!str)
4530 return 0;
4531 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4532 return 1;
4533}
4534__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4535
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004536static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004537{
4538 int ret;
4539 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004540 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004541
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004542 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4543 if (WARN_ON(!hash))
4544 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004545
4546 while (buf) {
4547 func = strsep(&buf, ",");
4548 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004549 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004550 if (ret)
4551 printk(KERN_DEBUG "ftrace: function %s not "
4552 "traceable\n", func);
4553 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004554
4555 if (enable)
4556 ftrace_graph_hash = hash;
4557 else
4558 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02004559}
4560#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4561
Steven Rostedt2a85a372011-12-19 21:57:44 -05004562void __init
4563ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004564{
4565 char *func;
4566
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004567 ftrace_ops_init(ops);
4568
Steven Rostedt2af15d62009-05-28 13:37:24 -04004569 while (buf) {
4570 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004571 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004572 }
4573}
4574
4575static void __init set_ftrace_early_filters(void)
4576{
4577 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004578 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004579 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004580 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004581#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4582 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004583 set_ftrace_early_graph(ftrace_graph_buf, 1);
4584 if (ftrace_graph_notrace_buf[0])
4585 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004586#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004587}
4588
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004589int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004590{
4591 struct seq_file *m = (struct seq_file *)file->private_data;
4592 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004593 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004594 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004595 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004596 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004597
Steven Rostedt5072c592008-05-12 21:20:43 +02004598 if (file->f_mode & FMODE_READ) {
4599 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004600 seq_release(inode, file);
4601 } else
4602 iter = file->private_data;
4603
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004604 parser = &iter->parser;
4605 if (trace_parser_loaded(parser)) {
4606 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004607 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004608 }
4609
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004610 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004611
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004612 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004613
Steven Rostedt058e2972011-04-29 22:35:33 -04004614 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004615 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4616
4617 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004618 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004619 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004620 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004621
Steven Rostedt058e2972011-04-29 22:35:33 -04004622 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004623 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
4624 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04004625 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04004626 } else {
4627 /* For read only, the hash is the ops hash */
4628 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04004629 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004630
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004631 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004632 free_ftrace_hash(iter->hash);
4633 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004634
Steven Rostedt5072c592008-05-12 21:20:43 +02004635 return 0;
4636}
4637
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004638static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004639 .open = ftrace_avail_open,
4640 .read = seq_read,
4641 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004642 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004643};
4644
Steven Rostedt647bcd02011-05-03 14:39:21 -04004645static const struct file_operations ftrace_enabled_fops = {
4646 .open = ftrace_enabled_open,
4647 .read = seq_read,
4648 .llseek = seq_lseek,
4649 .release = seq_release_private,
4650};
4651
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004652static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004653 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004654 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004655 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004656 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004657 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004658};
4659
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004660static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004661 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004662 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004663 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004664 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004665 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004666};
4667
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004668#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4669
4670static DEFINE_MUTEX(graph_lock);
4671
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004672struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
4673struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
4674
4675enum graph_filter_type {
4676 GRAPH_FILTER_NOTRACE = 0,
4677 GRAPH_FILTER_FUNCTION,
4678};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004679
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004680#define FTRACE_GRAPH_EMPTY ((void *)1)
4681
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004682struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004683 struct ftrace_hash *hash;
4684 struct ftrace_func_entry *entry;
4685 int idx; /* for hash table iteration */
4686 enum graph_filter_type type;
4687 struct ftrace_hash *new_hash;
4688 const struct seq_operations *seq_ops;
4689 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004690};
4691
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004692static void *
Li Zefan85951842009-06-24 09:54:00 +08004693__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004694{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004695 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004696 struct ftrace_func_entry *entry = fgd->entry;
4697 struct hlist_head *head;
4698 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004699
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004700 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004701 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004702
4703 if (entry) {
4704 hlist_for_each_entry_continue(entry, hlist) {
4705 fgd->entry = entry;
4706 return entry;
4707 }
4708
4709 idx++;
4710 }
4711
4712 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
4713 head = &fgd->hash->buckets[i];
4714 hlist_for_each_entry(entry, head, hlist) {
4715 fgd->entry = entry;
4716 fgd->idx = i;
4717 return entry;
4718 }
4719 }
4720 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08004721}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004722
Li Zefan85951842009-06-24 09:54:00 +08004723static void *
4724g_next(struct seq_file *m, void *v, loff_t *pos)
4725{
4726 (*pos)++;
4727 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004728}
4729
4730static void *g_start(struct seq_file *m, loff_t *pos)
4731{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004732 struct ftrace_graph_data *fgd = m->private;
4733
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004734 mutex_lock(&graph_lock);
4735
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004736 if (fgd->type == GRAPH_FILTER_FUNCTION)
4737 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4738 lockdep_is_held(&graph_lock));
4739 else
4740 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4741 lockdep_is_held(&graph_lock));
4742
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004743 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004744 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004745 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004746
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004747 fgd->idx = 0;
4748 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08004749 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004750}
4751
4752static void g_stop(struct seq_file *m, void *p)
4753{
4754 mutex_unlock(&graph_lock);
4755}
4756
4757static int g_show(struct seq_file *m, void *v)
4758{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004759 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004760
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004761 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004762 return 0;
4763
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004764 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004765 struct ftrace_graph_data *fgd = m->private;
4766
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004767 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004768 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004769 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004770 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004771 return 0;
4772 }
4773
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004774 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004775
4776 return 0;
4777}
4778
James Morris88e9d342009-09-22 16:43:43 -07004779static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004780 .start = g_start,
4781 .next = g_next,
4782 .stop = g_stop,
4783 .show = g_show,
4784};
4785
4786static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004787__ftrace_graph_open(struct inode *inode, struct file *file,
4788 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004789{
4790 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004791 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004792
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004793 if (file->f_mode & FMODE_WRITE) {
4794 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
4795
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004796 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
4797 return -ENOMEM;
4798
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004799 if (file->f_flags & O_TRUNC)
4800 new_hash = alloc_ftrace_hash(size_bits);
4801 else
4802 new_hash = alloc_and_copy_ftrace_hash(size_bits,
4803 fgd->hash);
4804 if (!new_hash) {
4805 ret = -ENOMEM;
4806 goto out;
4807 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004808 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004809
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004810 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004811 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004812 if (!ret) {
4813 struct seq_file *m = file->private_data;
4814 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004815 } else {
4816 /* Failed */
4817 free_ftrace_hash(new_hash);
4818 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004819 }
4820 } else
4821 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004822
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004823out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004824 if (ret < 0 && file->f_mode & FMODE_WRITE)
4825 trace_parser_put(&fgd->parser);
4826
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004827 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004828
4829 /*
4830 * All uses of fgd->hash must be taken with the graph_lock
4831 * held. The graph_lock is going to be released, so force
4832 * fgd->hash to be reinitialized when it is taken again.
4833 */
4834 fgd->hash = NULL;
4835
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004836 return ret;
4837}
4838
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004839static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004840ftrace_graph_open(struct inode *inode, struct file *file)
4841{
4842 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004843 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004844
4845 if (unlikely(ftrace_disabled))
4846 return -ENODEV;
4847
4848 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4849 if (fgd == NULL)
4850 return -ENOMEM;
4851
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004852 mutex_lock(&graph_lock);
4853
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004854 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4855 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004856 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004857 fgd->seq_ops = &ftrace_graph_seq_ops;
4858
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004859 ret = __ftrace_graph_open(inode, file, fgd);
4860 if (ret < 0)
4861 kfree(fgd);
4862
4863 mutex_unlock(&graph_lock);
4864 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004865}
4866
4867static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004868ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4869{
4870 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004871 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004872
4873 if (unlikely(ftrace_disabled))
4874 return -ENODEV;
4875
4876 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4877 if (fgd == NULL)
4878 return -ENOMEM;
4879
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004880 mutex_lock(&graph_lock);
4881
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004882 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4883 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004884 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004885 fgd->seq_ops = &ftrace_graph_seq_ops;
4886
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004887 ret = __ftrace_graph_open(inode, file, fgd);
4888 if (ret < 0)
4889 kfree(fgd);
4890
4891 mutex_unlock(&graph_lock);
4892 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004893}
4894
4895static int
Li Zefan87827112009-07-23 11:29:11 +08004896ftrace_graph_release(struct inode *inode, struct file *file)
4897{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004898 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004899 struct ftrace_hash *old_hash, *new_hash;
4900 struct trace_parser *parser;
4901 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004902
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004903 if (file->f_mode & FMODE_READ) {
4904 struct seq_file *m = file->private_data;
4905
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004906 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08004907 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004908 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004909 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004910 }
4911
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004912
4913 if (file->f_mode & FMODE_WRITE) {
4914
4915 parser = &fgd->parser;
4916
4917 if (trace_parser_loaded((parser))) {
4918 parser->buffer[parser->idx] = 0;
4919 ret = ftrace_graph_set_hash(fgd->new_hash,
4920 parser->buffer);
4921 }
4922
4923 trace_parser_put(parser);
4924
4925 new_hash = __ftrace_hash_move(fgd->new_hash);
4926 if (!new_hash) {
4927 ret = -ENOMEM;
4928 goto out;
4929 }
4930
4931 mutex_lock(&graph_lock);
4932
4933 if (fgd->type == GRAPH_FILTER_FUNCTION) {
4934 old_hash = rcu_dereference_protected(ftrace_graph_hash,
4935 lockdep_is_held(&graph_lock));
4936 rcu_assign_pointer(ftrace_graph_hash, new_hash);
4937 } else {
4938 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4939 lockdep_is_held(&graph_lock));
4940 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
4941 }
4942
4943 mutex_unlock(&graph_lock);
4944
4945 /* Wait till all users are no longer using the old hash */
4946 synchronize_sched();
4947
4948 free_ftrace_hash(old_hash);
4949 }
4950
4951 out:
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004952 kfree(fgd->new_hash);
4953 kfree(fgd);
4954
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004955 return ret;
Li Zefan87827112009-07-23 11:29:11 +08004956}
4957
4958static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004959ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004960{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004961 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004962 struct dyn_ftrace *rec;
4963 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004964 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004965 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004966 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004967
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004968 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004969 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4970 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004971
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004972 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004973
Steven Rostedt52baf112009-02-14 01:15:39 -05004974 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004975
4976 if (unlikely(ftrace_disabled)) {
4977 mutex_unlock(&ftrace_lock);
4978 return -ENODEV;
4979 }
4980
Steven Rostedt265c8312009-02-13 12:43:56 -05004981 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004982
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004983 if (rec->flags & FTRACE_FL_DISABLED)
4984 continue;
4985
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004986 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004987 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004988
4989 if (!not) {
4990 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004991
4992 if (entry)
4993 continue;
4994 if (add_hash_entry(hash, rec->ip) < 0)
4995 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004996 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004997 if (entry) {
4998 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004999 fail = 0;
5000 }
5001 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005002 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005003 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005004out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005005 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005006
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005007 if (fail)
5008 return -EINVAL;
5009
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005010 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005011}
5012
5013static ssize_t
5014ftrace_graph_write(struct file *file, const char __user *ubuf,
5015 size_t cnt, loff_t *ppos)
5016{
Namhyung Kim6a101082013-10-14 17:24:25 +09005017 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005018 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005019 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005020
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005021 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005022 return 0;
5023
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005024 /* Read mode uses seq functions */
5025 if (file->f_mode & FMODE_READ) {
5026 struct seq_file *m = file->private_data;
5027 fgd = m->private;
5028 }
5029
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005030 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005031
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005032 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005033
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005034 if (read >= 0 && trace_parser_loaded(parser) &&
5035 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005036
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005037 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005038 parser->buffer);
5039 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005040 }
5041
Namhyung Kim6a101082013-10-14 17:24:25 +09005042 if (!ret)
5043 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005044
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005045 return ret;
5046}
5047
5048static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005049 .open = ftrace_graph_open,
5050 .read = seq_read,
5051 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005052 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005053 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005054};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005055
5056static const struct file_operations ftrace_graph_notrace_fops = {
5057 .open = ftrace_graph_notrace_open,
5058 .read = seq_read,
5059 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005060 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005061 .release = ftrace_graph_release,
5062};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005063#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5064
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005065void ftrace_create_filter_files(struct ftrace_ops *ops,
5066 struct dentry *parent)
5067{
5068
5069 trace_create_file("set_ftrace_filter", 0644, parent,
5070 ops, &ftrace_filter_fops);
5071
5072 trace_create_file("set_ftrace_notrace", 0644, parent,
5073 ops, &ftrace_notrace_fops);
5074}
5075
5076/*
5077 * The name "destroy_filter_files" is really a misnomer. Although
5078 * in the future, it may actualy delete the files, but this is
5079 * really intended to make sure the ops passed in are disabled
5080 * and that when this function returns, the caller is free to
5081 * free the ops.
5082 *
5083 * The "destroy" name is only to match the "create" name that this
5084 * should be paired with.
5085 */
5086void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5087{
5088 mutex_lock(&ftrace_lock);
5089 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5090 ftrace_shutdown(ops, 0);
5091 ops->flags |= FTRACE_OPS_FL_DELETED;
5092 mutex_unlock(&ftrace_lock);
5093}
5094
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005095static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005096{
Steven Rostedt5072c592008-05-12 21:20:43 +02005097
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005098 trace_create_file("available_filter_functions", 0444,
5099 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005100
Steven Rostedt647bcd02011-05-03 14:39:21 -04005101 trace_create_file("enabled_functions", 0444,
5102 d_tracer, NULL, &ftrace_enabled_fops);
5103
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005104 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005105
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005106#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005107 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005108 NULL,
5109 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005110 trace_create_file("set_graph_notrace", 0444, d_tracer,
5111 NULL,
5112 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005113#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5114
Steven Rostedt5072c592008-05-12 21:20:43 +02005115 return 0;
5116}
5117
Steven Rostedt9fd49322012-04-24 22:32:06 -04005118static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005119{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005120 const unsigned long *ipa = a;
5121 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005122
Steven Rostedt9fd49322012-04-24 22:32:06 -04005123 if (*ipa > *ipb)
5124 return 1;
5125 if (*ipa < *ipb)
5126 return -1;
5127 return 0;
5128}
5129
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005130static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005131 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005132 unsigned long *end)
5133{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005134 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005135 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005136 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005137 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005138 unsigned long *p;
5139 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005140 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005141 int ret = -ENOMEM;
5142
5143 count = end - start;
5144
5145 if (!count)
5146 return 0;
5147
Steven Rostedt9fd49322012-04-24 22:32:06 -04005148 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005149 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005150
Steven Rostedt706c81f2012-04-24 23:45:26 -04005151 start_pg = ftrace_allocate_pages(count);
5152 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005153 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005154
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005155 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005156
Steven Rostedt320823092011-12-16 14:42:37 -05005157 /*
5158 * Core and each module needs their own pages, as
5159 * modules will free them when they are removed.
5160 * Force a new page to be allocated for modules.
5161 */
Steven Rostedta7900872011-12-16 16:23:44 -05005162 if (!mod) {
5163 WARN_ON(ftrace_pages || ftrace_pages_start);
5164 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005165 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005166 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005167 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005168 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005169
Steven Rostedta7900872011-12-16 16:23:44 -05005170 if (WARN_ON(ftrace_pages->next)) {
5171 /* Hmm, we have free pages? */
5172 while (ftrace_pages->next)
5173 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005174 }
Steven Rostedta7900872011-12-16 16:23:44 -05005175
Steven Rostedt706c81f2012-04-24 23:45:26 -04005176 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005177 }
5178
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005179 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005180 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005181 while (p < end) {
5182 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005183 /*
5184 * Some architecture linkers will pad between
5185 * the different mcount_loc sections of different
5186 * object files to satisfy alignments.
5187 * Skip any NULL pointers.
5188 */
5189 if (!addr)
5190 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005191
5192 if (pg->index == pg->size) {
5193 /* We should have allocated enough */
5194 if (WARN_ON(!pg->next))
5195 break;
5196 pg = pg->next;
5197 }
5198
5199 rec = &pg->records[pg->index++];
5200 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005201 }
5202
Steven Rostedt706c81f2012-04-24 23:45:26 -04005203 /* We should have used all pages */
5204 WARN_ON(pg->next);
5205
5206 /* Assign the last page to ftrace_pages */
5207 ftrace_pages = pg;
5208
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005209 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005210 * We only need to disable interrupts on start up
5211 * because we are modifying code that an interrupt
5212 * may execute, and the modification is not atomic.
5213 * But for modules, nothing runs the code we modify
5214 * until we are finished with it, and there's no
5215 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005216 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005217 if (!mod)
5218 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005219 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005220 if (!mod)
5221 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005222 ret = 0;
5223 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005224 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005225
Steven Rostedta7900872011-12-16 16:23:44 -05005226 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005227}
5228
Steven Rostedt93eb6772009-04-15 13:24:06 -04005229#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005230
5231#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5232
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005233static int referenced_filters(struct dyn_ftrace *rec)
5234{
5235 struct ftrace_ops *ops;
5236 int cnt = 0;
5237
5238 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5239 if (ops_references_rec(ops, rec))
5240 cnt++;
5241 }
5242
5243 return cnt;
5244}
5245
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005246void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005247{
5248 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005249 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005250 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005251 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005252
Steven Rostedt93eb6772009-04-15 13:24:06 -04005253 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005254
5255 if (ftrace_disabled)
5256 goto out_unlock;
5257
Steven Rostedt320823092011-12-16 14:42:37 -05005258 /*
5259 * Each module has its own ftrace_pages, remove
5260 * them from the list.
5261 */
5262 last_pg = &ftrace_pages_start;
5263 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5264 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005265 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005266 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005267 * As core pages are first, the first
5268 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005269 */
Steven Rostedt320823092011-12-16 14:42:37 -05005270 if (WARN_ON(pg == ftrace_pages_start))
5271 goto out_unlock;
5272
5273 /* Check if we are deleting the last page */
5274 if (pg == ftrace_pages)
5275 ftrace_pages = next_to_ftrace_page(last_pg);
5276
5277 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05005278 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5279 free_pages((unsigned long)pg->records, order);
5280 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05005281 } else
5282 last_pg = &pg->next;
5283 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005284 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005285 mutex_unlock(&ftrace_lock);
5286}
5287
Jessica Yu7dcd1822016-02-16 17:32:33 -05005288void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005289{
5290 struct dyn_ftrace *rec;
5291 struct ftrace_page *pg;
5292
5293 mutex_lock(&ftrace_lock);
5294
5295 if (ftrace_disabled)
5296 goto out_unlock;
5297
5298 /*
5299 * If the tracing is enabled, go ahead and enable the record.
5300 *
5301 * The reason not to enable the record immediatelly is the
5302 * inherent check of ftrace_make_nop/ftrace_make_call for
5303 * correct previous instructions. Making first the NOP
5304 * conversion puts the module to the correct state, thus
5305 * passing the ftrace_make_call check.
5306 *
5307 * We also delay this to after the module code already set the
5308 * text to read-only, as we now need to set it back to read-write
5309 * so that we can modify the text.
5310 */
5311 if (ftrace_start_up)
5312 ftrace_arch_code_modify_prepare();
5313
5314 do_for_each_ftrace_rec(pg, rec) {
5315 int cnt;
5316 /*
5317 * do_for_each_ftrace_rec() is a double loop.
5318 * module text shares the pg. If a record is
5319 * not part of this module, then skip this pg,
5320 * which the "break" will do.
5321 */
5322 if (!within_module_core(rec->ip, mod))
5323 break;
5324
5325 cnt = 0;
5326
5327 /*
5328 * When adding a module, we need to check if tracers are
5329 * currently enabled and if they are, and can trace this record,
5330 * we need to enable the module functions as well as update the
5331 * reference counts for those function records.
5332 */
5333 if (ftrace_start_up)
5334 cnt += referenced_filters(rec);
5335
5336 /* This clears FTRACE_FL_DISABLED */
5337 rec->flags = cnt;
5338
5339 if (ftrace_start_up && cnt) {
5340 int failed = __ftrace_replace_code(rec, 1);
5341 if (failed) {
5342 ftrace_bug(failed, rec);
5343 goto out_loop;
5344 }
5345 }
5346
5347 } while_for_each_ftrace_rec();
5348
5349 out_loop:
5350 if (ftrace_start_up)
5351 ftrace_arch_code_modify_post_process();
5352
5353 out_unlock:
5354 mutex_unlock(&ftrace_lock);
5355}
5356
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005357void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005358{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005359 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005360 return;
5361
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005362 ftrace_process_locs(mod, mod->ftrace_callsites,
5363 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005364}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005365#endif /* CONFIG_MODULES */
5366
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -04005367void __init ftrace_free_init_mem(void)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005368{
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -04005369 unsigned long start = (unsigned long)(&__init_begin);
5370 unsigned long end = (unsigned long)(&__init_end);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005371 struct ftrace_page **last_pg = &ftrace_pages_start;
5372 struct ftrace_page *pg;
5373 struct dyn_ftrace *rec;
5374 struct dyn_ftrace key;
5375 int order;
5376
5377 key.ip = start;
5378 key.flags = end; /* overload flags, as it is unsigned long */
5379
5380 mutex_lock(&ftrace_lock);
5381
5382 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
5383 if (end < pg->records[0].ip ||
5384 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
5385 continue;
5386 again:
5387 rec = bsearch(&key, pg->records, pg->index,
5388 sizeof(struct dyn_ftrace),
5389 ftrace_cmp_recs);
5390 if (!rec)
5391 continue;
5392 pg->index--;
5393 if (!pg->index) {
5394 *last_pg = pg->next;
5395 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5396 free_pages((unsigned long)pg->records, order);
5397 kfree(pg);
5398 pg = container_of(last_pg, struct ftrace_page, next);
5399 if (!(*last_pg))
5400 ftrace_pages = pg;
5401 continue;
5402 }
5403 memmove(rec, rec + 1,
5404 (pg->index - (rec - pg->records)) * sizeof(*rec));
5405 /* More than one function may be in this block */
5406 goto again;
5407 }
5408 mutex_unlock(&ftrace_lock);
5409}
5410
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005411void __init ftrace_init(void)
5412{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005413 extern unsigned long __start_mcount_loc[];
5414 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005415 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005416 int ret;
5417
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005418 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005419 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005420 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01005421 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005422 goto failed;
5423
5424 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005425 if (!count) {
5426 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005427 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005428 }
5429
5430 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5431 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005432
5433 last_ftrace_enabled = ftrace_enabled = 1;
5434
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005435 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005436 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005437 __stop_mcount_loc);
5438
Steven Rostedt2af15d62009-05-28 13:37:24 -04005439 set_ftrace_early_filters();
5440
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005441 return;
5442 failed:
5443 ftrace_disabled = 1;
5444}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005445
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005446/* Do nothing if arch does not support this */
5447void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5448{
5449}
5450
5451static void ftrace_update_trampoline(struct ftrace_ops *ops)
5452{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005453 arch_ftrace_update_trampoline(ops);
5454}
5455
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04005456void ftrace_init_trace_array(struct trace_array *tr)
5457{
5458 INIT_LIST_HEAD(&tr->func_probes);
5459}
Steven Rostedt3d083392008-05-12 21:20:42 +02005460#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005461
Steven Rostedt2b499382011-05-03 22:49:52 -04005462static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005463 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005464 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5465 FTRACE_OPS_FL_INITIALIZED |
5466 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005467};
5468
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005469static int __init ftrace_nodyn_init(void)
5470{
5471 ftrace_enabled = 1;
5472 return 0;
5473}
Steven Rostedt6f415672012-10-05 12:13:07 -04005474core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005475
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005476static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005477static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005478static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005479/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005480# define ftrace_startup(ops, command) \
5481 ({ \
5482 int ___ret = __register_ftrace_function(ops); \
5483 if (!___ret) \
5484 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5485 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005486 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005487# define ftrace_shutdown(ops, command) \
5488 ({ \
5489 int ___ret = __unregister_ftrace_function(ops); \
5490 if (!___ret) \
5491 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5492 ___ret; \
5493 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005494
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005495# define ftrace_startup_sysctl() do { } while (0)
5496# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005497
5498static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005499ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005500{
5501 return 1;
5502}
5503
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005504static void ftrace_update_trampoline(struct ftrace_ops *ops)
5505{
5506}
5507
Steven Rostedt3d083392008-05-12 21:20:42 +02005508#endif /* CONFIG_DYNAMIC_FTRACE */
5509
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005510__init void ftrace_init_global_array_ops(struct trace_array *tr)
5511{
5512 tr->ops = &global_ops;
5513 tr->ops->private = tr;
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04005514 ftrace_init_trace_array(tr);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005515}
5516
5517void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5518{
5519 /* If we filter on pids, update to use the pid function */
5520 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5521 if (WARN_ON(tr->ops->func != ftrace_stub))
5522 printk("ftrace ops had %pS for function\n",
5523 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005524 }
5525 tr->ops->func = func;
5526 tr->ops->private = tr;
5527}
5528
5529void ftrace_reset_array_ops(struct trace_array *tr)
5530{
5531 tr->ops->func = ftrace_stub;
5532}
5533
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005534static inline void
5535__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005536 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005537{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005538 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005539 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005540
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005541 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5542 if (bit < 0)
5543 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005544
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005545 /*
5546 * Some of the ops may be dynamically allocated,
5547 * they must be freed after a synchronize_sched().
5548 */
5549 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005550
Steven Rostedt0a016402012-11-02 17:03:03 -04005551 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005552 /*
5553 * Check the following for each ops before calling their func:
5554 * if RCU flag is set, then rcu_is_watching() must be true
5555 * if PER_CPU is set, then ftrace_function_local_disable()
5556 * must be false
5557 * Otherwise test if the ip matches the ops filter
5558 *
5559 * If any of the above fails then the op->func() is not executed.
5560 */
5561 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5562 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5563 !ftrace_function_local_disabled(op)) &&
5564 ftrace_ops_test(op, ip, regs)) {
5565
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005566 if (FTRACE_WARN_ON(!op->func)) {
5567 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005568 goto out;
5569 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005570 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005571 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005572 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005573out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005574 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005575 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005576}
5577
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005578/*
5579 * Some archs only support passing ip and parent_ip. Even though
5580 * the list function ignores the op parameter, we do not want any
5581 * C side effects, where a function is called without the caller
5582 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005583 * Archs are to support both the regs and ftrace_ops at the same time.
5584 * If they support ftrace_ops, it is assumed they support regs.
5585 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005586 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5587 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005588 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08005589 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005590 */
5591#if ARCH_SUPPORTS_FTRACE_OPS
5592static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005593 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005594{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005595 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005596}
5597#else
5598static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5599{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005600 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005601}
5602#endif
5603
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005604/*
5605 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005606 * recursion, needs RCU protection and/or requires per cpu handling, then
5607 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005608 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005609static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005610 struct ftrace_ops *op, struct pt_regs *regs)
5611{
5612 int bit;
5613
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005614 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5615 return;
5616
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005617 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5618 if (bit < 0)
5619 return;
5620
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005621 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005622
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005623 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5624 !ftrace_function_local_disabled(op)) {
5625 op->func(ip, parent_ip, op, regs);
5626 }
5627
5628 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005629 trace_clear_recursion(bit);
5630}
5631
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005632/**
5633 * ftrace_ops_get_func - get the function a trampoline should call
5634 * @ops: the ops to get the function for
5635 *
5636 * Normally the mcount trampoline will call the ops->func, but there
5637 * are times that it should not. For example, if the ops does not
5638 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08005639 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005640 *
5641 * Returns the function that the trampoline should call for @ops.
5642 */
5643ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5644{
5645 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005646 * If the function does not handle recursion, needs to be RCU safe,
5647 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005648 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005649 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5650 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5651 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005652
5653 return ops->func;
5654}
5655
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005656static void
5657ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5658 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05005659{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005660 struct trace_array *tr = data;
5661 struct trace_pid_list *pid_list;
5662
5663 pid_list = rcu_dereference_sched(tr->function_pids);
5664
5665 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5666 trace_ignore_this_task(pid_list, next));
5667}
5668
Namhyung Kim1e104862017-04-17 11:44:28 +09005669static void
5670ftrace_pid_follow_sched_process_fork(void *data,
5671 struct task_struct *self,
5672 struct task_struct *task)
5673{
5674 struct trace_pid_list *pid_list;
5675 struct trace_array *tr = data;
5676
5677 pid_list = rcu_dereference_sched(tr->function_pids);
5678 trace_filter_add_remove_task(pid_list, self, task);
5679}
5680
5681static void
5682ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
5683{
5684 struct trace_pid_list *pid_list;
5685 struct trace_array *tr = data;
5686
5687 pid_list = rcu_dereference_sched(tr->function_pids);
5688 trace_filter_add_remove_task(pid_list, NULL, task);
5689}
5690
5691void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
5692{
5693 if (enable) {
5694 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
5695 tr);
5696 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
5697 tr);
5698 } else {
5699 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
5700 tr);
5701 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
5702 tr);
5703 }
5704}
5705
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005706static void clear_ftrace_pids(struct trace_array *tr)
5707{
5708 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05005709 int cpu;
5710
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005711 pid_list = rcu_dereference_protected(tr->function_pids,
5712 lockdep_is_held(&ftrace_lock));
5713 if (!pid_list)
5714 return;
5715
5716 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5717
5718 for_each_possible_cpu(cpu)
5719 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5720
5721 rcu_assign_pointer(tr->function_pids, NULL);
5722
5723 /* Wait till all users are no longer using pid filtering */
5724 synchronize_sched();
5725
5726 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05005727}
5728
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005729static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05005730{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005731 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005732 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005733
5734 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005735 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005736
5737 mutex_unlock(&ftrace_lock);
5738}
5739
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005740/* Greater than any max PID */
5741#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5742
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005743static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005744 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005745{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005746 struct trace_pid_list *pid_list;
5747 struct trace_array *tr = m->private;
5748
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005749 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005750 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005751
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005752 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005753
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005754 if (!pid_list)
5755 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5756
5757 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005758}
5759
5760static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5761{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005762 struct trace_array *tr = m->private;
5763 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5764
5765 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005766 return NULL;
5767
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005768 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005769}
5770
5771static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005772 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005773{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005774 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005775 mutex_unlock(&ftrace_lock);
5776}
5777
5778static int fpid_show(struct seq_file *m, void *v)
5779{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005780 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005781 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005782 return 0;
5783 }
5784
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005785 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005786}
5787
5788static const struct seq_operations ftrace_pid_sops = {
5789 .start = fpid_start,
5790 .next = fpid_next,
5791 .stop = fpid_stop,
5792 .show = fpid_show,
5793};
5794
5795static int
5796ftrace_pid_open(struct inode *inode, struct file *file)
5797{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005798 struct trace_array *tr = inode->i_private;
5799 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005800 int ret = 0;
5801
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005802 if (trace_array_get(tr) < 0)
5803 return -ENODEV;
5804
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005805 if ((file->f_mode & FMODE_WRITE) &&
5806 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005807 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005808
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005809 ret = seq_open(file, &ftrace_pid_sops);
5810 if (ret < 0) {
5811 trace_array_put(tr);
5812 } else {
5813 m = file->private_data;
5814 /* copy tr over to seq ops */
5815 m->private = tr;
5816 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005817
5818 return ret;
5819}
5820
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005821static void ignore_task_cpu(void *data)
5822{
5823 struct trace_array *tr = data;
5824 struct trace_pid_list *pid_list;
5825
5826 /*
5827 * This function is called by on_each_cpu() while the
5828 * event_mutex is held.
5829 */
5830 pid_list = rcu_dereference_protected(tr->function_pids,
5831 mutex_is_locked(&ftrace_lock));
5832
5833 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5834 trace_ignore_this_task(pid_list, current));
5835}
5836
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005837static ssize_t
5838ftrace_pid_write(struct file *filp, const char __user *ubuf,
5839 size_t cnt, loff_t *ppos)
5840{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005841 struct seq_file *m = filp->private_data;
5842 struct trace_array *tr = m->private;
5843 struct trace_pid_list *filtered_pids = NULL;
5844 struct trace_pid_list *pid_list;
5845 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005846
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005847 if (!cnt)
5848 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005849
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005850 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005851
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005852 filtered_pids = rcu_dereference_protected(tr->function_pids,
5853 lockdep_is_held(&ftrace_lock));
5854
5855 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5856 if (ret < 0)
5857 goto out;
5858
5859 rcu_assign_pointer(tr->function_pids, pid_list);
5860
5861 if (filtered_pids) {
5862 synchronize_sched();
5863 trace_free_pid_list(filtered_pids);
5864 } else if (pid_list) {
5865 /* Register a probe to set whether to ignore the tracing of a task */
5866 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5867 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005868
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005869 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005870 * Ignoring of pids is done at task switch. But we have to
5871 * check for those tasks that are currently running.
5872 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005873 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005874 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005875
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005876 ftrace_update_pid_func();
5877 ftrace_startup_all(0);
5878 out:
5879 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005880
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005881 if (ret > 0)
5882 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05005883
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005884 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005885}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005886
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005887static int
5888ftrace_pid_release(struct inode *inode, struct file *file)
5889{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005890 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005891
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005892 trace_array_put(tr);
5893
5894 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005895}
5896
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005897static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005898 .open = ftrace_pid_open,
5899 .write = ftrace_pid_write,
5900 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005901 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005902 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005903};
5904
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005905void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005906{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005907 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005908 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005909}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005910
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04005911void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5912 struct dentry *d_tracer)
5913{
5914 /* Only the top level directory has the dyn_tracefs and profile */
5915 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5916
5917 ftrace_init_dyn_tracefs(d_tracer);
5918 ftrace_profile_tracefs(d_tracer);
5919}
5920
Steven Rostedt3d083392008-05-12 21:20:42 +02005921/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005922 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005923 *
5924 * This function should be used by panic code. It stops ftrace
5925 * but in a not so nice way. If you need to simply kill ftrace
5926 * from a non-atomic section, use ftrace_kill.
5927 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005928void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005929{
5930 ftrace_disabled = 1;
5931 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005932 clear_ftrace_function();
5933}
5934
5935/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005936 * Test if ftrace is dead or not.
5937 */
5938int ftrace_is_dead(void)
5939{
5940 return ftrace_disabled;
5941}
5942
5943/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005944 * register_ftrace_function - register a function for profiling
5945 * @ops - ops structure that holds the function for profiling.
5946 *
5947 * Register a function to be called by all functions in the
5948 * kernel.
5949 *
5950 * Note: @ops->func and all the functions it calls must be labeled
5951 * with "notrace", otherwise it will go into a
5952 * recursive loop.
5953 */
5954int register_ftrace_function(struct ftrace_ops *ops)
5955{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005956 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005957
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005958 ftrace_ops_init(ops);
5959
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005960 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005961
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005962 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005963
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005964 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005965
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005966 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005967}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005968EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005969
5970/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005971 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005972 * @ops - ops structure that holds the function to unregister
5973 *
5974 * Unregister a function that was added to be called by ftrace profiling.
5975 */
5976int unregister_ftrace_function(struct ftrace_ops *ops)
5977{
5978 int ret;
5979
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005980 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005981 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005982 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005983
5984 return ret;
5985}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005986EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005987
Ingo Molnare309b412008-05-12 21:20:51 +02005988int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005989ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005990 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005991 loff_t *ppos)
5992{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005993 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005994
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005995 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005996
Steven Rostedt45a4a232011-04-21 23:16:46 -04005997 if (unlikely(ftrace_disabled))
5998 goto out;
5999
6000 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006001
Li Zefana32c7762009-06-26 16:55:51 +08006002 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006003 goto out;
6004
Li Zefana32c7762009-06-26 16:55:51 +08006005 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006006
6007 if (ftrace_enabled) {
6008
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006009 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01006010 if (ftrace_ops_list != &ftrace_list_end)
6011 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006012
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006013 ftrace_startup_sysctl();
6014
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006015 } else {
6016 /* stopping ftrace calls (just send to ftrace_stub) */
6017 ftrace_trace_function = ftrace_stub;
6018
6019 ftrace_shutdown_sysctl();
6020 }
6021
6022 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006023 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006024 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006025}
Ingo Molnarf17845e2008-10-24 12:47:10 +02006026
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006027#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006028
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006029static struct ftrace_ops graph_ops = {
6030 .func = ftrace_stub,
6031 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6032 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006033 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006034 FTRACE_OPS_FL_STUB,
6035#ifdef FTRACE_GRAPH_TRAMP_ADDR
6036 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05006037 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006038#endif
6039 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6040};
6041
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006042void ftrace_graph_sleep_time_control(bool enable)
6043{
6044 fgraph_sleep_time = enable;
6045}
6046
6047void ftrace_graph_graph_time_control(bool enable)
6048{
6049 fgraph_graph_time = enable;
6050}
6051
Steven Rostedte49dc192008-12-02 23:50:05 -05006052int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6053{
6054 return 0;
6055}
6056
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006057/* The callbacks that hook a function */
6058trace_func_graph_ret_t ftrace_graph_return =
6059 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006060trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006061static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006062
6063/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6064static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6065{
6066 int i;
6067 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006068 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6069 struct task_struct *g, *t;
6070
6071 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6072 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
6073 * sizeof(struct ftrace_ret_stack),
6074 GFP_KERNEL);
6075 if (!ret_stack_list[i]) {
6076 start = 0;
6077 end = i;
6078 ret = -ENOMEM;
6079 goto free;
6080 }
6081 }
6082
Soumya PN6112a302016-05-17 21:31:14 +05306083 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006084 do_each_thread(g, t) {
6085 if (start == end) {
6086 ret = -EAGAIN;
6087 goto unlock;
6088 }
6089
6090 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01006091 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006092 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04006093 t->curr_ret_stack = -1;
6094 /* Make sure the tasks see the -1 first: */
6095 smp_wmb();
6096 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006097 }
6098 } while_each_thread(g, t);
6099
6100unlock:
Soumya PN6112a302016-05-17 21:31:14 +05306101 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006102free:
6103 for (i = start; i < end; i++)
6104 kfree(ret_stack_list[i]);
6105 return ret;
6106}
6107
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006108static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02006109ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04006110 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006111{
6112 unsigned long long timestamp;
6113 int index;
6114
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006115 /*
6116 * Does the user want to count the time a function was asleep.
6117 * If so, do not update the time stamps.
6118 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006119 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006120 return;
6121
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006122 timestamp = trace_clock_local();
6123
6124 prev->ftrace_timestamp = timestamp;
6125
6126 /* only process tasks that we timestamped */
6127 if (!next->ftrace_timestamp)
6128 return;
6129
6130 /*
6131 * Update all the counters in next to make up for the
6132 * time next was sleeping.
6133 */
6134 timestamp -= next->ftrace_timestamp;
6135
6136 for (index = next->curr_ret_stack; index >= 0; index--)
6137 next->ret_stack[index].calltime += timestamp;
6138}
6139
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006140/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006141static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006142{
6143 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006144 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006145
6146 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
6147 sizeof(struct ftrace_ret_stack *),
6148 GFP_KERNEL);
6149
6150 if (!ret_stack_list)
6151 return -ENOMEM;
6152
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006153 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04006154 for_each_online_cpu(cpu) {
6155 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05006156 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04006157 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006158
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006159 do {
6160 ret = alloc_retstack_tasklist(ret_stack_list);
6161 } while (ret == -EAGAIN);
6162
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006163 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04006164 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006165 if (ret)
6166 pr_info("ftrace_graph: Couldn't activate tracepoint"
6167 " probe to kernel_sched_switch\n");
6168 }
6169
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006170 kfree(ret_stack_list);
6171 return ret;
6172}
6173
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006174/*
6175 * Hibernation protection.
6176 * The state of the current task is too much unstable during
6177 * suspend/restore to disk. We want to protect against that.
6178 */
6179static int
6180ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6181 void *unused)
6182{
6183 switch (state) {
6184 case PM_HIBERNATION_PREPARE:
6185 pause_graph_tracing();
6186 break;
6187
6188 case PM_POST_HIBERNATION:
6189 unpause_graph_tracing();
6190 break;
6191 }
6192 return NOTIFY_DONE;
6193}
6194
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006195static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6196{
6197 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6198 return 0;
6199 return __ftrace_graph_entry(trace);
6200}
6201
6202/*
6203 * The function graph tracer should only trace the functions defined
6204 * by set_ftrace_filter and set_ftrace_notrace. If another function
6205 * tracer ops is registered, the graph tracer requires testing the
6206 * function against the global ops, and not just trace any function
6207 * that any ftrace_ops registered.
6208 */
6209static void update_function_graph_func(void)
6210{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006211 struct ftrace_ops *op;
6212 bool do_test = false;
6213
6214 /*
6215 * The graph and global ops share the same set of functions
6216 * to test. If any other ops is on the list, then
6217 * the graph tracing needs to test if its the function
6218 * it should call.
6219 */
6220 do_for_each_ftrace_op(op, ftrace_ops_list) {
6221 if (op != &global_ops && op != &graph_ops &&
6222 op != &ftrace_list_end) {
6223 do_test = true;
6224 /* in double loop, break out with goto */
6225 goto out;
6226 }
6227 } while_for_each_ftrace_op(op);
6228 out:
6229 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006230 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006231 else
6232 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006233}
6234
Mathias Krause8275f692014-03-30 15:31:50 +02006235static struct notifier_block ftrace_suspend_notifier = {
6236 .notifier_call = ftrace_suspend_notifier_call,
6237};
6238
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006239int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6240 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006241{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006242 int ret = 0;
6243
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006244 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006245
Steven Rostedt05ce5812009-03-24 00:18:31 -04006246 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04006247 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04006248 ret = -EBUSY;
6249 goto out;
6250 }
6251
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006252 register_pm_notifier(&ftrace_suspend_notifier);
6253
Steven Rostedt597af812009-04-03 15:24:12 -04006254 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006255 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006256 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04006257 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006258 goto out;
6259 }
Steven Rostedte53a6312008-11-26 00:16:25 -05006260
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006261 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006262
6263 /*
6264 * Update the indirect function to the entryfunc, and the
6265 * function that gets called to the entry_test first. Then
6266 * call the update fgraph entry function to determine if
6267 * the entryfunc should be called directly or not.
6268 */
6269 __ftrace_graph_entry = entryfunc;
6270 ftrace_graph_entry = ftrace_graph_entry_test;
6271 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05006272
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006273 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006274out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006275 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006276 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006277}
6278
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006279void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006280{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006281 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006282
Steven Rostedt597af812009-04-03 15:24:12 -04006283 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006284 goto out;
6285
Steven Rostedt597af812009-04-03 15:24:12 -04006286 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006287 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006288 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006289 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006290 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006291 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04006292 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006293
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006294#ifdef CONFIG_DYNAMIC_FTRACE
6295 /*
6296 * Function graph does not allocate the trampoline, but
6297 * other global_ops do. We need to reset the ALLOC_TRAMP flag
6298 * if one was used.
6299 */
6300 global_ops.trampoline = save_global_trampoline;
6301 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
6302 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
6303#endif
6304
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006305 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006306 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006307}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006308
Steven Rostedt868baf02011-02-10 21:26:13 -05006309static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6310
6311static void
6312graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6313{
6314 atomic_set(&t->tracing_graph_pause, 0);
6315 atomic_set(&t->trace_overrun, 0);
6316 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006317 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05006318 smp_wmb();
6319 t->ret_stack = ret_stack;
6320}
6321
6322/*
6323 * Allocate a return stack for the idle task. May be the first
6324 * time through, or it may be done by CPU hotplug online.
6325 */
6326void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6327{
6328 t->curr_ret_stack = -1;
6329 /*
6330 * The idle task has no parent, it either has its own
6331 * stack or no stack at all.
6332 */
6333 if (t->ret_stack)
6334 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6335
6336 if (ftrace_graph_active) {
6337 struct ftrace_ret_stack *ret_stack;
6338
6339 ret_stack = per_cpu(idle_ret_stack, cpu);
6340 if (!ret_stack) {
6341 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6342 * sizeof(struct ftrace_ret_stack),
6343 GFP_KERNEL);
6344 if (!ret_stack)
6345 return;
6346 per_cpu(idle_ret_stack, cpu) = ret_stack;
6347 }
6348 graph_init_task(t, ret_stack);
6349 }
6350}
6351
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006352/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006353void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006354{
Steven Rostedt84047e32009-06-02 16:51:55 -04006355 /* Make sure we do not use the parent ret_stack */
6356 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05006357 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04006358
Steven Rostedt597af812009-04-03 15:24:12 -04006359 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04006360 struct ftrace_ret_stack *ret_stack;
6361
6362 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006363 * sizeof(struct ftrace_ret_stack),
6364 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04006365 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006366 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05006367 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04006368 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006369}
6370
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006371void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006372{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006373 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6374
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006375 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006376 /* NULL must become visible to IRQs before we free it: */
6377 barrier();
6378
6379 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006380}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006381#endif