blob: 493c7ff7e8606d83e6b4389575375a00fd41af71 [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 Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001099static LIST_HEAD(ftrace_func_probes);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001100
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001101struct ftrace_func_entry {
1102 struct hlist_node hlist;
1103 unsigned long ip;
1104};
1105
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001106/*
1107 * We make these constant because no one should touch them,
1108 * but they are used as the default "empty hash", to avoid allocating
1109 * it all the time. These are in a read only section such that if
1110 * anyone does try to modify it, it will cause an exception.
1111 */
1112static const struct hlist_head empty_buckets[1];
1113static const struct ftrace_hash empty_hash = {
1114 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001115};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001116#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001117
Steven Rostedt2b499382011-05-03 22:49:52 -04001118static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001119 .func = ftrace_stub,
1120 .local_hash.notrace_hash = EMPTY_HASH,
1121 .local_hash.filter_hash = EMPTY_HASH,
1122 INIT_OPS_HASH(global_ops)
1123 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001124 FTRACE_OPS_FL_INITIALIZED |
1125 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001126};
1127
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001128/*
1129 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001130 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001131 * not return true for either core_kernel_text() or
1132 * is_module_text_address().
1133 */
1134bool is_ftrace_trampoline(unsigned long addr)
1135{
1136 struct ftrace_ops *op;
1137 bool ret = false;
1138
1139 /*
1140 * Some of the ops may be dynamically allocated,
1141 * they are freed after a synchronize_sched().
1142 */
1143 preempt_disable_notrace();
1144
1145 do_for_each_ftrace_op(op, ftrace_ops_list) {
1146 /*
1147 * This is to check for dynamically allocated trampolines.
1148 * Trampolines that are in kernel text will have
1149 * core_kernel_text() return true.
1150 */
1151 if (op->trampoline && op->trampoline_size)
1152 if (addr >= op->trampoline &&
1153 addr < op->trampoline + op->trampoline_size) {
1154 ret = true;
1155 goto out;
1156 }
1157 } while_for_each_ftrace_op(op);
1158
1159 out:
1160 preempt_enable_notrace();
1161
1162 return ret;
1163}
1164
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001165struct ftrace_page {
1166 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001167 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001168 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001169 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001170};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001171
Steven Rostedta7900872011-12-16 16:23:44 -05001172#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1173#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001174
1175/* estimate from running different kernels */
1176#define NR_TO_INIT 10000
1177
1178static struct ftrace_page *ftrace_pages_start;
1179static struct ftrace_page *ftrace_pages;
1180
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001181static __always_inline unsigned long
1182ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1183{
1184 if (hash->size_bits > 0)
1185 return hash_long(ip, hash->size_bits);
1186
1187 return 0;
1188}
1189
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001190/* Only use this function if ftrace_hash_empty() has already been tested */
1191static __always_inline struct ftrace_func_entry *
1192__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001193{
1194 unsigned long key;
1195 struct ftrace_func_entry *entry;
1196 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001197
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001198 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199 hhd = &hash->buckets[key];
1200
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001201 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001202 if (entry->ip == ip)
1203 return entry;
1204 }
1205 return NULL;
1206}
1207
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001208/**
1209 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1210 * @hash: The hash to look at
1211 * @ip: The instruction pointer to test
1212 *
1213 * Search a given @hash to see if a given instruction pointer (@ip)
1214 * exists in it.
1215 *
1216 * Returns the entry that holds the @ip if found. NULL otherwise.
1217 */
1218struct ftrace_func_entry *
1219ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1220{
1221 if (ftrace_hash_empty(hash))
1222 return NULL;
1223
1224 return __ftrace_lookup_ip(hash, ip);
1225}
1226
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001227static void __add_hash_entry(struct ftrace_hash *hash,
1228 struct ftrace_func_entry *entry)
1229{
1230 struct hlist_head *hhd;
1231 unsigned long key;
1232
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001233 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001234 hhd = &hash->buckets[key];
1235 hlist_add_head(&entry->hlist, hhd);
1236 hash->count++;
1237}
1238
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001239static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1240{
1241 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001242
1243 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1244 if (!entry)
1245 return -ENOMEM;
1246
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001247 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001248 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001249
1250 return 0;
1251}
1252
1253static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001254free_hash_entry(struct ftrace_hash *hash,
1255 struct ftrace_func_entry *entry)
1256{
1257 hlist_del(&entry->hlist);
1258 kfree(entry);
1259 hash->count--;
1260}
1261
1262static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001263remove_hash_entry(struct ftrace_hash *hash,
1264 struct ftrace_func_entry *entry)
1265{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04001266 hlist_del_rcu(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001267 hash->count--;
1268}
1269
1270static void ftrace_hash_clear(struct ftrace_hash *hash)
1271{
1272 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001273 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001274 struct ftrace_func_entry *entry;
1275 int size = 1 << hash->size_bits;
1276 int i;
1277
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001278 if (!hash->count)
1279 return;
1280
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001281 for (i = 0; i < size; i++) {
1282 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001283 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001284 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001285 }
1286 FTRACE_WARN_ON(hash->count);
1287}
1288
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001289static void free_ftrace_hash(struct ftrace_hash *hash)
1290{
1291 if (!hash || hash == EMPTY_HASH)
1292 return;
1293 ftrace_hash_clear(hash);
1294 kfree(hash->buckets);
1295 kfree(hash);
1296}
1297
Steven Rostedt07fd5512011-05-05 18:03:47 -04001298static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1299{
1300 struct ftrace_hash *hash;
1301
1302 hash = container_of(rcu, struct ftrace_hash, rcu);
1303 free_ftrace_hash(hash);
1304}
1305
1306static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1307{
1308 if (!hash || hash == EMPTY_HASH)
1309 return;
1310 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1311}
1312
Jiri Olsa5500fa52012-02-15 15:51:54 +01001313void ftrace_free_filter(struct ftrace_ops *ops)
1314{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001315 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001316 free_ftrace_hash(ops->func_hash->filter_hash);
1317 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001318}
1319
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001320static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1321{
1322 struct ftrace_hash *hash;
1323 int size;
1324
1325 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1326 if (!hash)
1327 return NULL;
1328
1329 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001330 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001331
1332 if (!hash->buckets) {
1333 kfree(hash);
1334 return NULL;
1335 }
1336
1337 hash->size_bits = size_bits;
1338
1339 return hash;
1340}
1341
1342static struct ftrace_hash *
1343alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1344{
1345 struct ftrace_func_entry *entry;
1346 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001347 int size;
1348 int ret;
1349 int i;
1350
1351 new_hash = alloc_ftrace_hash(size_bits);
1352 if (!new_hash)
1353 return NULL;
1354
1355 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001356 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001357 return new_hash;
1358
1359 size = 1 << hash->size_bits;
1360 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001361 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001362 ret = add_hash_entry(new_hash, entry->ip);
1363 if (ret < 0)
1364 goto free_hash;
1365 }
1366 }
1367
1368 FTRACE_WARN_ON(new_hash->count != hash->count);
1369
1370 return new_hash;
1371
1372 free_hash:
1373 free_ftrace_hash(new_hash);
1374 return NULL;
1375}
1376
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001377static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001378ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001379static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001380ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001381
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001382static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1383 struct ftrace_hash *new_hash);
1384
Namhyung Kim3e278c02017-01-20 11:44:45 +09001385static struct ftrace_hash *
1386__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001387{
1388 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001389 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001390 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001391 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001392 int size = src->count;
1393 int bits = 0;
1394 int i;
1395
1396 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001397 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001398 */
Namhyung Kim3e278c02017-01-20 11:44:45 +09001399 if (!src->count)
1400 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001401
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001402 /*
1403 * Make the hash size about 1/2 the # found
1404 */
1405 for (size /= 2; size; size >>= 1)
1406 bits++;
1407
1408 /* Don't allocate too much */
1409 if (bits > FTRACE_HASH_MAX_BITS)
1410 bits = FTRACE_HASH_MAX_BITS;
1411
Steven Rostedt07fd5512011-05-05 18:03:47 -04001412 new_hash = alloc_ftrace_hash(bits);
1413 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001414 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001415
1416 size = 1 << src->size_bits;
1417 for (i = 0; i < size; i++) {
1418 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001419 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001420 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001421 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001422 }
1423 }
1424
Namhyung Kim3e278c02017-01-20 11:44:45 +09001425 return new_hash;
1426}
1427
1428static int
1429ftrace_hash_move(struct ftrace_ops *ops, int enable,
1430 struct ftrace_hash **dst, struct ftrace_hash *src)
1431{
1432 struct ftrace_hash *new_hash;
1433 int ret;
1434
1435 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1436 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1437 return -EINVAL;
1438
1439 new_hash = __ftrace_hash_move(src);
1440 if (!new_hash)
1441 return -ENOMEM;
1442
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001443 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1444 if (enable) {
1445 /* IPMODIFY should be updated only when filter_hash updating */
1446 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1447 if (ret < 0) {
1448 free_ftrace_hash(new_hash);
1449 return ret;
1450 }
1451 }
1452
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001453 /*
1454 * Remove the current set, update the hash and add
1455 * them back.
1456 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001457 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001458
Steven Rostedt07fd5512011-05-05 18:03:47 -04001459 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001460
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001461 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001462
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001463 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001464}
1465
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001466static bool hash_contains_ip(unsigned long ip,
1467 struct ftrace_ops_hash *hash)
1468{
1469 /*
1470 * The function record is a match if it exists in the filter
1471 * hash and not in the notrace hash. Note, an emty hash is
1472 * considered a match for the filter hash, but an empty
1473 * notrace hash is considered not in the notrace hash.
1474 */
1475 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001476 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001477 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001478 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001479}
1480
Steven Rostedt265c8312009-02-13 12:43:56 -05001481/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001482 * Test the hashes for this ops to see if we want to call
1483 * the ops->func or not.
1484 *
1485 * It's a match if the ip is in the ops->filter_hash or
1486 * the filter_hash does not exist or is empty,
1487 * AND
1488 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001489 *
1490 * This needs to be called with preemption disabled as
1491 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001492 */
1493static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001494ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001495{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001496 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001497 int ret;
1498
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001499#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1500 /*
1501 * There's a small race when adding ops that the ftrace handler
1502 * that wants regs, may be called without them. We can not
1503 * allow that handler to be called if regs is NULL.
1504 */
1505 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1506 return 0;
1507#endif
1508
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001509 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1510 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001511
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001512 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001513 ret = 1;
1514 else
1515 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001516
1517 return ret;
1518}
1519
1520/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001521 * This is a double for. Do not use 'break' to break out of the loop,
1522 * you must use a goto.
1523 */
1524#define do_for_each_ftrace_rec(pg, rec) \
1525 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1526 int _____i; \
1527 for (_____i = 0; _____i < pg->index; _____i++) { \
1528 rec = &pg->records[_____i];
1529
1530#define while_for_each_ftrace_rec() \
1531 } \
1532 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301533
Steven Rostedt5855fea2011-12-16 19:27:42 -05001534
1535static int ftrace_cmp_recs(const void *a, const void *b)
1536{
Steven Rostedta650e022012-04-25 13:48:13 -04001537 const struct dyn_ftrace *key = a;
1538 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001539
Steven Rostedta650e022012-04-25 13:48:13 -04001540 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001541 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001542 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1543 return 1;
1544 return 0;
1545}
1546
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001547/**
1548 * ftrace_location_range - return the first address of a traced location
1549 * if it touches the given ip range
1550 * @start: start of range to search.
1551 * @end: end of range to search (inclusive). @end points to the last byte
1552 * to check.
1553 *
1554 * Returns rec->ip if the related ftrace location is a least partly within
1555 * the given address range. That is, the first address of the instruction
1556 * that is either a NOP or call to the function tracer. It checks the ftrace
1557 * internal tables to determine if the address belongs or not.
1558 */
1559unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001560{
1561 struct ftrace_page *pg;
1562 struct dyn_ftrace *rec;
1563 struct dyn_ftrace key;
1564
1565 key.ip = start;
1566 key.flags = end; /* overload flags, as it is unsigned long */
1567
1568 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1569 if (end < pg->records[0].ip ||
1570 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1571 continue;
1572 rec = bsearch(&key, pg->records, pg->index,
1573 sizeof(struct dyn_ftrace),
1574 ftrace_cmp_recs);
1575 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001576 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001577 }
1578
Steven Rostedt5855fea2011-12-16 19:27:42 -05001579 return 0;
1580}
1581
Steven Rostedtc88fd862011-08-16 09:53:39 -04001582/**
1583 * ftrace_location - return true if the ip giving is a traced location
1584 * @ip: the instruction pointer to check
1585 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001586 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001587 * That is, the instruction that is either a NOP or call to
1588 * the function tracer. It checks the ftrace internal tables to
1589 * determine if the address belongs or not.
1590 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001591unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001592{
Steven Rostedta650e022012-04-25 13:48:13 -04001593 return ftrace_location_range(ip, ip);
1594}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001595
Steven Rostedta650e022012-04-25 13:48:13 -04001596/**
1597 * ftrace_text_reserved - return true if range contains an ftrace location
1598 * @start: start of range to search
1599 * @end: end of range to search (inclusive). @end points to the last byte to check.
1600 *
1601 * Returns 1 if @start and @end contains a ftrace location.
1602 * That is, the instruction that is either a NOP or call to
1603 * the function tracer. It checks the ftrace internal tables to
1604 * determine if the address belongs or not.
1605 */
Sasha Levind88471c2013-01-09 18:09:20 -05001606int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001607{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001608 unsigned long ret;
1609
1610 ret = ftrace_location_range((unsigned long)start,
1611 (unsigned long)end);
1612
1613 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001614}
1615
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001616/* Test if ops registered to this rec needs regs */
1617static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1618{
1619 struct ftrace_ops *ops;
1620 bool keep_regs = false;
1621
1622 for (ops = ftrace_ops_list;
1623 ops != &ftrace_list_end; ops = ops->next) {
1624 /* pass rec in as regs to have non-NULL val */
1625 if (ftrace_ops_test(ops, rec->ip, rec)) {
1626 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1627 keep_regs = true;
1628 break;
1629 }
1630 }
1631 }
1632
1633 return keep_regs;
1634}
1635
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001636static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001637 int filter_hash,
1638 bool inc)
1639{
1640 struct ftrace_hash *hash;
1641 struct ftrace_hash *other_hash;
1642 struct ftrace_page *pg;
1643 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001644 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001645 int count = 0;
1646 int all = 0;
1647
1648 /* Only update if the ops has been registered */
1649 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001650 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001651
1652 /*
1653 * In the filter_hash case:
1654 * If the count is zero, we update all records.
1655 * Otherwise we just update the items in the hash.
1656 *
1657 * In the notrace_hash case:
1658 * We enable the update in the hash.
1659 * As disabling notrace means enabling the tracing,
1660 * and enabling notrace means disabling, the inc variable
1661 * gets inversed.
1662 */
1663 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001664 hash = ops->func_hash->filter_hash;
1665 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001666 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001667 all = 1;
1668 } else {
1669 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001670 hash = ops->func_hash->notrace_hash;
1671 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001672 /*
1673 * If the notrace hash has no items,
1674 * then there's nothing to do.
1675 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001676 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001677 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001678 }
1679
1680 do_for_each_ftrace_rec(pg, rec) {
1681 int in_other_hash = 0;
1682 int in_hash = 0;
1683 int match = 0;
1684
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001685 if (rec->flags & FTRACE_FL_DISABLED)
1686 continue;
1687
Steven Rostedted926f92011-05-03 13:25:24 -04001688 if (all) {
1689 /*
1690 * Only the filter_hash affects all records.
1691 * Update if the record is not in the notrace hash.
1692 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001693 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001694 match = 1;
1695 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001696 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1697 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001698
1699 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001700 * If filter_hash is set, we want to match all functions
1701 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001702 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001703 * If filter_hash is not set, then we are decrementing.
1704 * That means we match anything that is in the hash
1705 * and also in the other_hash. That is, we need to turn
1706 * off functions in the other hash because they are disabled
1707 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001708 */
1709 if (filter_hash && in_hash && !in_other_hash)
1710 match = 1;
1711 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001712 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001713 match = 1;
1714 }
1715 if (!match)
1716 continue;
1717
1718 if (inc) {
1719 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001720 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001721 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001722
1723 /*
1724 * If there's only a single callback registered to a
1725 * function, and the ops has a trampoline registered
1726 * for it, then we can call it directly.
1727 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001728 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001729 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001730 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001731 /*
1732 * If we are adding another function callback
1733 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001734 * custom trampoline in use, then we need to go
1735 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001736 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001737 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001738
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001739 /*
1740 * If any ops wants regs saved for this function
1741 * then all ops will get saved regs.
1742 */
1743 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1744 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001745 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001746 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001747 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001748 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001749
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001750 /*
1751 * If the rec had REGS enabled and the ops that is
1752 * being removed had REGS set, then see if there is
1753 * still any ops for this record that wants regs.
1754 * If not, we can stop recording them.
1755 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001756 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001757 rec->flags & FTRACE_FL_REGS &&
1758 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1759 if (!test_rec_ops_needs_regs(rec))
1760 rec->flags &= ~FTRACE_FL_REGS;
1761 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001762
1763 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001764 * If the rec had TRAMP enabled, then it needs to
1765 * be cleared. As TRAMP can only be enabled iff
1766 * there is only a single ops attached to it.
1767 * In otherwords, always disable it on decrementing.
1768 * In the future, we may set it if rec count is
1769 * decremented to one, and the ops that is left
1770 * has a trampoline.
1771 */
1772 rec->flags &= ~FTRACE_FL_TRAMP;
1773
1774 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001775 * flags will be cleared in ftrace_check_record()
1776 * if rec count is zero.
1777 */
Steven Rostedted926f92011-05-03 13:25:24 -04001778 }
1779 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001780
1781 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1782 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1783
Steven Rostedted926f92011-05-03 13:25:24 -04001784 /* Shortcut, if we handled all records, we are done. */
1785 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001786 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001787 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001788
1789 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001790}
1791
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001792static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001793 int filter_hash)
1794{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001795 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001796}
1797
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001798static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001799 int filter_hash)
1800{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001801 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001802}
1803
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001804static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1805 int filter_hash, int inc)
1806{
1807 struct ftrace_ops *op;
1808
1809 __ftrace_hash_rec_update(ops, filter_hash, inc);
1810
1811 if (ops->func_hash != &global_ops.local_hash)
1812 return;
1813
1814 /*
1815 * If the ops shares the global_ops hash, then we need to update
1816 * all ops that are enabled and use this hash.
1817 */
1818 do_for_each_ftrace_op(op, ftrace_ops_list) {
1819 /* Already done */
1820 if (op == ops)
1821 continue;
1822 if (op->func_hash == &global_ops.local_hash)
1823 __ftrace_hash_rec_update(op, filter_hash, inc);
1824 } while_for_each_ftrace_op(op);
1825}
1826
1827static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1828 int filter_hash)
1829{
1830 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1831}
1832
1833static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1834 int filter_hash)
1835{
1836 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1837}
1838
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001839/*
1840 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1841 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1842 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1843 * Note that old_hash and new_hash has below meanings
1844 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1845 * - If the hash is EMPTY_HASH, it hits nothing
1846 * - Anything else hits the recs which match the hash entries.
1847 */
1848static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1849 struct ftrace_hash *old_hash,
1850 struct ftrace_hash *new_hash)
1851{
1852 struct ftrace_page *pg;
1853 struct dyn_ftrace *rec, *end = NULL;
1854 int in_old, in_new;
1855
1856 /* Only update if the ops has been registered */
1857 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1858 return 0;
1859
1860 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1861 return 0;
1862
1863 /*
1864 * Since the IPMODIFY is a very address sensitive action, we do not
1865 * allow ftrace_ops to set all functions to new hash.
1866 */
1867 if (!new_hash || !old_hash)
1868 return -EINVAL;
1869
1870 /* Update rec->flags */
1871 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001872
1873 if (rec->flags & FTRACE_FL_DISABLED)
1874 continue;
1875
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001876 /* We need to update only differences of filter_hash */
1877 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1878 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1879 if (in_old == in_new)
1880 continue;
1881
1882 if (in_new) {
1883 /* New entries must ensure no others are using it */
1884 if (rec->flags & FTRACE_FL_IPMODIFY)
1885 goto rollback;
1886 rec->flags |= FTRACE_FL_IPMODIFY;
1887 } else /* Removed entry */
1888 rec->flags &= ~FTRACE_FL_IPMODIFY;
1889 } while_for_each_ftrace_rec();
1890
1891 return 0;
1892
1893rollback:
1894 end = rec;
1895
1896 /* Roll back what we did above */
1897 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001898
1899 if (rec->flags & FTRACE_FL_DISABLED)
1900 continue;
1901
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001902 if (rec == end)
1903 goto err_out;
1904
1905 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1906 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1907 if (in_old == in_new)
1908 continue;
1909
1910 if (in_new)
1911 rec->flags &= ~FTRACE_FL_IPMODIFY;
1912 else
1913 rec->flags |= FTRACE_FL_IPMODIFY;
1914 } while_for_each_ftrace_rec();
1915
1916err_out:
1917 return -EBUSY;
1918}
1919
1920static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1921{
1922 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1923
1924 if (ftrace_hash_empty(hash))
1925 hash = NULL;
1926
1927 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1928}
1929
1930/* Disabling always succeeds */
1931static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1932{
1933 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1934
1935 if (ftrace_hash_empty(hash))
1936 hash = NULL;
1937
1938 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1939}
1940
1941static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1942 struct ftrace_hash *new_hash)
1943{
1944 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1945
1946 if (ftrace_hash_empty(old_hash))
1947 old_hash = NULL;
1948
1949 if (ftrace_hash_empty(new_hash))
1950 new_hash = NULL;
1951
1952 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1953}
1954
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001955static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001956{
1957 int i;
1958
1959 printk(KERN_CONT "%s", fmt);
1960
1961 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1962 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1963}
1964
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001965static struct ftrace_ops *
1966ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001967static struct ftrace_ops *
1968ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001969
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001970enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001971const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001972
1973static void print_bug_type(void)
1974{
1975 switch (ftrace_bug_type) {
1976 case FTRACE_BUG_UNKNOWN:
1977 break;
1978 case FTRACE_BUG_INIT:
1979 pr_info("Initializing ftrace call sites\n");
1980 break;
1981 case FTRACE_BUG_NOP:
1982 pr_info("Setting ftrace call site to NOP\n");
1983 break;
1984 case FTRACE_BUG_CALL:
1985 pr_info("Setting ftrace call site to call ftrace function\n");
1986 break;
1987 case FTRACE_BUG_UPDATE:
1988 pr_info("Updating ftrace call site to call a different ftrace function\n");
1989 break;
1990 }
1991}
1992
Steven Rostedtc88fd862011-08-16 09:53:39 -04001993/**
1994 * ftrace_bug - report and shutdown function tracer
1995 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001996 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001997 *
1998 * The arch code that enables or disables the function tracing
1999 * can call ftrace_bug() when it has detected a problem in
2000 * modifying the code. @failed should be one of either:
2001 * EFAULT - if the problem happens on reading the @ip address
2002 * EINVAL - if what is read at @ip is not what was expected
2003 * EPERM - if the problem happens on writting to the @ip address
2004 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002005void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002006{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002007 unsigned long ip = rec ? rec->ip : 0;
2008
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002009 switch (failed) {
2010 case -EFAULT:
2011 FTRACE_WARN_ON_ONCE(1);
2012 pr_info("ftrace faulted on modifying ");
2013 print_ip_sym(ip);
2014 break;
2015 case -EINVAL:
2016 FTRACE_WARN_ON_ONCE(1);
2017 pr_info("ftrace failed to modify ");
2018 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002019 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002020 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002021 if (ftrace_expected) {
2022 print_ip_ins(" expected: ", ftrace_expected);
2023 pr_cont("\n");
2024 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002025 break;
2026 case -EPERM:
2027 FTRACE_WARN_ON_ONCE(1);
2028 pr_info("ftrace faulted on writing ");
2029 print_ip_sym(ip);
2030 break;
2031 default:
2032 FTRACE_WARN_ON_ONCE(1);
2033 pr_info("ftrace faulted on unknown error ");
2034 print_ip_sym(ip);
2035 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002036 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002037 if (rec) {
2038 struct ftrace_ops *ops = NULL;
2039
2040 pr_info("ftrace record flags: %lx\n", rec->flags);
2041 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2042 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2043 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2044 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002045 if (ops) {
2046 do {
2047 pr_cont("\ttramp: %pS (%pS)",
2048 (void *)ops->trampoline,
2049 (void *)ops->func);
2050 ops = ftrace_find_tramp_ops_next(rec, ops);
2051 } while (ops);
2052 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002053 pr_cont("\ttramp: ERROR!");
2054
2055 }
2056 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002057 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002058 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002059}
2060
Steven Rostedtc88fd862011-08-16 09:53:39 -04002061static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002062{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002063 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002064
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002065 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2066
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002067 if (rec->flags & FTRACE_FL_DISABLED)
2068 return FTRACE_UPDATE_IGNORE;
2069
Steven Rostedt982c3502008-11-15 16:31:41 -05002070 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002071 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002072 *
Steven Rostedted926f92011-05-03 13:25:24 -04002073 * If the record has a ref count, then we need to enable it
2074 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002075 *
Steven Rostedted926f92011-05-03 13:25:24 -04002076 * Otherwise we make sure its disabled.
2077 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002078 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002079 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002080 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002081 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002082 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002083
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002084 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002085 * If enabling and the REGS flag does not match the REGS_EN, or
2086 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2087 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002088 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002089 if (flag) {
2090 if (!(rec->flags & FTRACE_FL_REGS) !=
2091 !(rec->flags & FTRACE_FL_REGS_EN))
2092 flag |= FTRACE_FL_REGS;
2093
2094 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2095 !(rec->flags & FTRACE_FL_TRAMP_EN))
2096 flag |= FTRACE_FL_TRAMP;
2097 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002098
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002099 /* If the state of this record hasn't changed, then do nothing */
2100 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002101 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002102
2103 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002104 /* Save off if rec is being enabled (for return value) */
2105 flag ^= rec->flags & FTRACE_FL_ENABLED;
2106
2107 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002108 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002109 if (flag & FTRACE_FL_REGS) {
2110 if (rec->flags & FTRACE_FL_REGS)
2111 rec->flags |= FTRACE_FL_REGS_EN;
2112 else
2113 rec->flags &= ~FTRACE_FL_REGS_EN;
2114 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002115 if (flag & FTRACE_FL_TRAMP) {
2116 if (rec->flags & FTRACE_FL_TRAMP)
2117 rec->flags |= FTRACE_FL_TRAMP_EN;
2118 else
2119 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2120 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002121 }
2122
2123 /*
2124 * If this record is being updated from a nop, then
2125 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002126 * Otherwise,
2127 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002128 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002129 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002130 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002131 if (flag & FTRACE_FL_ENABLED) {
2132 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002133 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002134 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002135
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002136 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002137 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002138 }
2139
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002140 if (update) {
2141 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002142 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002143 rec->flags = 0;
2144 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002145 /*
2146 * Just disable the record, but keep the ops TRAMP
2147 * and REGS states. The _EN flags must be disabled though.
2148 */
2149 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2150 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002151 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002152
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002153 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002154 return FTRACE_UPDATE_MAKE_NOP;
2155}
2156
2157/**
2158 * ftrace_update_record, set a record that now is tracing or not
2159 * @rec: the record to update
2160 * @enable: set to 1 if the record is tracing, zero to force disable
2161 *
2162 * The records that represent all functions that can be traced need
2163 * to be updated when tracing has been enabled.
2164 */
2165int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2166{
2167 return ftrace_check_record(rec, enable, 1);
2168}
2169
2170/**
2171 * ftrace_test_record, check if the record has been enabled or not
2172 * @rec: the record to test
2173 * @enable: set to 1 to check if enabled, 0 if it is disabled
2174 *
2175 * The arch code may need to test if a record is already set to
2176 * tracing to determine how to modify the function code that it
2177 * represents.
2178 */
2179int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2180{
2181 return ftrace_check_record(rec, enable, 0);
2182}
2183
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002184static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002185ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2186{
2187 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002188 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002189
2190 do_for_each_ftrace_op(op, ftrace_ops_list) {
2191
2192 if (!op->trampoline)
2193 continue;
2194
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002195 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002196 return op;
2197 } while_for_each_ftrace_op(op);
2198
2199 return NULL;
2200}
2201
2202static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002203ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2204 struct ftrace_ops *op)
2205{
2206 unsigned long ip = rec->ip;
2207
2208 while_for_each_ftrace_op(op) {
2209
2210 if (!op->trampoline)
2211 continue;
2212
2213 if (hash_contains_ip(ip, op->func_hash))
2214 return op;
2215 }
2216
2217 return NULL;
2218}
2219
2220static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002221ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2222{
2223 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002224 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002225
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002226 /*
2227 * Need to check removed ops first.
2228 * If they are being removed, and this rec has a tramp,
2229 * and this rec is in the ops list, then it would be the
2230 * one with the tramp.
2231 */
2232 if (removed_ops) {
2233 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002234 return removed_ops;
2235 }
2236
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002237 /*
2238 * Need to find the current trampoline for a rec.
2239 * Now, a trampoline is only attached to a rec if there
2240 * was a single 'ops' attached to it. But this can be called
2241 * when we are adding another op to the rec or removing the
2242 * current one. Thus, if the op is being added, we can
2243 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002244 * yet.
2245 *
2246 * If an ops is being modified (hooking to different functions)
2247 * then we don't care about the new functions that are being
2248 * added, just the old ones (that are probably being removed).
2249 *
2250 * If we are adding an ops to a function that already is using
2251 * a trampoline, it needs to be removed (trampolines are only
2252 * for single ops connected), then an ops that is not being
2253 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002254 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002255 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002256
2257 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002258 continue;
2259
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002260 /*
2261 * If the ops is being added, it hasn't gotten to
2262 * the point to be removed from this tree yet.
2263 */
2264 if (op->flags & FTRACE_OPS_FL_ADDING)
2265 continue;
2266
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002267
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002268 /*
2269 * If the ops is being modified and is in the old
2270 * hash, then it is probably being removed from this
2271 * function.
2272 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002273 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2274 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002275 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002276 /*
2277 * If the ops is not being added or modified, and it's
2278 * in its normal filter hash, then this must be the one
2279 * we want!
2280 */
2281 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2282 hash_contains_ip(ip, op->func_hash))
2283 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002284
2285 } while_for_each_ftrace_op(op);
2286
2287 return NULL;
2288}
2289
2290static struct ftrace_ops *
2291ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2292{
2293 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002294 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002295
2296 do_for_each_ftrace_op(op, ftrace_ops_list) {
2297 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002298 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002299 return op;
2300 } while_for_each_ftrace_op(op);
2301
2302 return NULL;
2303}
2304
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002305/**
2306 * ftrace_get_addr_new - Get the call address to set to
2307 * @rec: The ftrace record descriptor
2308 *
2309 * If the record has the FTRACE_FL_REGS set, that means that it
2310 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2311 * is not not set, then it wants to convert to the normal callback.
2312 *
2313 * Returns the address of the trampoline to set to
2314 */
2315unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2316{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002317 struct ftrace_ops *ops;
2318
2319 /* Trampolines take precedence over regs */
2320 if (rec->flags & FTRACE_FL_TRAMP) {
2321 ops = ftrace_find_tramp_ops_new(rec);
2322 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002323 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2324 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002325 /* Ftrace is shutting down, return anything */
2326 return (unsigned long)FTRACE_ADDR;
2327 }
2328 return ops->trampoline;
2329 }
2330
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002331 if (rec->flags & FTRACE_FL_REGS)
2332 return (unsigned long)FTRACE_REGS_ADDR;
2333 else
2334 return (unsigned long)FTRACE_ADDR;
2335}
2336
2337/**
2338 * ftrace_get_addr_curr - Get the call address that is already there
2339 * @rec: The ftrace record descriptor
2340 *
2341 * The FTRACE_FL_REGS_EN is set when the record already points to
2342 * a function that saves all the regs. Basically the '_EN' version
2343 * represents the current state of the function.
2344 *
2345 * Returns the address of the trampoline that is currently being called
2346 */
2347unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2348{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002349 struct ftrace_ops *ops;
2350
2351 /* Trampolines take precedence over regs */
2352 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2353 ops = ftrace_find_tramp_ops_curr(rec);
2354 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002355 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2356 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002357 /* Ftrace is shutting down, return anything */
2358 return (unsigned long)FTRACE_ADDR;
2359 }
2360 return ops->trampoline;
2361 }
2362
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002363 if (rec->flags & FTRACE_FL_REGS_EN)
2364 return (unsigned long)FTRACE_REGS_ADDR;
2365 else
2366 return (unsigned long)FTRACE_ADDR;
2367}
2368
Steven Rostedtc88fd862011-08-16 09:53:39 -04002369static int
2370__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2371{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002372 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002373 unsigned long ftrace_addr;
2374 int ret;
2375
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002376 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002377
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002378 /* This needs to be done before we call ftrace_update_record */
2379 ftrace_old_addr = ftrace_get_addr_curr(rec);
2380
2381 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002382
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002383 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2384
Steven Rostedtc88fd862011-08-16 09:53:39 -04002385 switch (ret) {
2386 case FTRACE_UPDATE_IGNORE:
2387 return 0;
2388
2389 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002390 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002391 return ftrace_make_call(rec, ftrace_addr);
2392
2393 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002394 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002395 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002396
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002397 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002398 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002399 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002400 }
2401
2402 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002403}
2404
Steven Rostedte4f5d542012-04-27 09:13:18 -04002405void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002406{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002407 struct dyn_ftrace *rec;
2408 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002409 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002410
Steven Rostedt45a4a232011-04-21 23:16:46 -04002411 if (unlikely(ftrace_disabled))
2412 return;
2413
Steven Rostedt265c8312009-02-13 12:43:56 -05002414 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002415
2416 if (rec->flags & FTRACE_FL_DISABLED)
2417 continue;
2418
Steven Rostedte4f5d542012-04-27 09:13:18 -04002419 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002420 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002421 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002422 /* Stop processing */
2423 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002424 }
2425 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002426}
2427
Steven Rostedtc88fd862011-08-16 09:53:39 -04002428struct ftrace_rec_iter {
2429 struct ftrace_page *pg;
2430 int index;
2431};
2432
2433/**
2434 * ftrace_rec_iter_start, start up iterating over traced functions
2435 *
2436 * Returns an iterator handle that is used to iterate over all
2437 * the records that represent address locations where functions
2438 * are traced.
2439 *
2440 * May return NULL if no records are available.
2441 */
2442struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2443{
2444 /*
2445 * We only use a single iterator.
2446 * Protected by the ftrace_lock mutex.
2447 */
2448 static struct ftrace_rec_iter ftrace_rec_iter;
2449 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2450
2451 iter->pg = ftrace_pages_start;
2452 iter->index = 0;
2453
2454 /* Could have empty pages */
2455 while (iter->pg && !iter->pg->index)
2456 iter->pg = iter->pg->next;
2457
2458 if (!iter->pg)
2459 return NULL;
2460
2461 return iter;
2462}
2463
2464/**
2465 * ftrace_rec_iter_next, get the next record to process.
2466 * @iter: The handle to the iterator.
2467 *
2468 * Returns the next iterator after the given iterator @iter.
2469 */
2470struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2471{
2472 iter->index++;
2473
2474 if (iter->index >= iter->pg->index) {
2475 iter->pg = iter->pg->next;
2476 iter->index = 0;
2477
2478 /* Could have empty pages */
2479 while (iter->pg && !iter->pg->index)
2480 iter->pg = iter->pg->next;
2481 }
2482
2483 if (!iter->pg)
2484 return NULL;
2485
2486 return iter;
2487}
2488
2489/**
2490 * ftrace_rec_iter_record, get the record at the iterator location
2491 * @iter: The current iterator location
2492 *
2493 * Returns the record that the current @iter is at.
2494 */
2495struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2496{
2497 return &iter->pg->records[iter->index];
2498}
2499
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302500static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002501ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002502{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002503 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002504
Steven Rostedt45a4a232011-04-21 23:16:46 -04002505 if (unlikely(ftrace_disabled))
2506 return 0;
2507
Shaohua Li25aac9d2009-01-09 11:29:40 +08002508 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002509 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002510 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002511 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302512 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002513 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302514 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002515}
2516
Steven Rostedt000ab692009-02-17 13:35:06 -05002517/*
2518 * archs can override this function if they must do something
2519 * before the modifying code is performed.
2520 */
2521int __weak ftrace_arch_code_modify_prepare(void)
2522{
2523 return 0;
2524}
2525
2526/*
2527 * archs can override this function if they must do something
2528 * after the modifying code is performed.
2529 */
2530int __weak ftrace_arch_code_modify_post_process(void)
2531{
2532 return 0;
2533}
2534
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002535void ftrace_modify_all_code(int command)
2536{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002537 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002538 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002539
2540 /*
2541 * If the ftrace_caller calls a ftrace_ops func directly,
2542 * we need to make sure that it only traces functions it
2543 * expects to trace. When doing the switch of functions,
2544 * we need to update to the ftrace_ops_list_func first
2545 * before the transition between old and new calls are set,
2546 * as the ftrace_ops_list_func will check the ops hashes
2547 * to make sure the ops are having the right functions
2548 * traced.
2549 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002550 if (update) {
2551 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2552 if (FTRACE_WARN_ON(err))
2553 return;
2554 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002555
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002556 if (command & FTRACE_UPDATE_CALLS)
2557 ftrace_replace_code(1);
2558 else if (command & FTRACE_DISABLE_CALLS)
2559 ftrace_replace_code(0);
2560
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002561 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2562 function_trace_op = set_function_trace_op;
2563 smp_wmb();
2564 /* If irqs are disabled, we are in stop machine */
2565 if (!irqs_disabled())
2566 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002567 err = ftrace_update_ftrace_func(ftrace_trace_function);
2568 if (FTRACE_WARN_ON(err))
2569 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002570 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002571
2572 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002573 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002574 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002575 err = ftrace_disable_ftrace_graph_caller();
2576 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002577}
2578
Ingo Molnare309b412008-05-12 21:20:51 +02002579static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002580{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002581 int *command = data;
2582
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002583 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002584
Steven Rostedtc88fd862011-08-16 09:53:39 -04002585 return 0;
2586}
2587
2588/**
2589 * ftrace_run_stop_machine, go back to the stop machine method
2590 * @command: The command to tell ftrace what to do
2591 *
2592 * If an arch needs to fall back to the stop machine method, the
2593 * it can call this function.
2594 */
2595void ftrace_run_stop_machine(int command)
2596{
2597 stop_machine(__ftrace_modify_code, &command, NULL);
2598}
2599
2600/**
2601 * arch_ftrace_update_code, modify the code to trace or not trace
2602 * @command: The command that needs to be done
2603 *
2604 * Archs can override this function if it does not need to
2605 * run stop_machine() to modify code.
2606 */
2607void __weak arch_ftrace_update_code(int command)
2608{
2609 ftrace_run_stop_machine(command);
2610}
2611
2612static void ftrace_run_update_code(int command)
2613{
2614 int ret;
2615
2616 ret = ftrace_arch_code_modify_prepare();
2617 FTRACE_WARN_ON(ret);
2618 if (ret)
2619 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002620
2621 /*
2622 * By default we use stop_machine() to modify the code.
2623 * But archs can do what ever they want as long as it
2624 * is safe. The stop_machine() is the safest, but also
2625 * produces the most overhead.
2626 */
2627 arch_ftrace_update_code(command);
2628
Steven Rostedt000ab692009-02-17 13:35:06 -05002629 ret = ftrace_arch_code_modify_post_process();
2630 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002631}
2632
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002633static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002634 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002635{
2636 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002637 ops->old_hash.filter_hash = old_hash->filter_hash;
2638 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002639 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002640 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002641 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002642 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2643}
2644
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002645static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002646static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002647
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002648void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2649{
2650}
2651
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002652static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002653{
2654 free_percpu(ops->disabled);
2655}
2656
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002657static void ftrace_startup_enable(int command)
2658{
2659 if (saved_ftrace_func != ftrace_trace_function) {
2660 saved_ftrace_func = ftrace_trace_function;
2661 command |= FTRACE_UPDATE_TRACE_FUNC;
2662 }
2663
2664 if (!command || !ftrace_enabled)
2665 return;
2666
2667 ftrace_run_update_code(command);
2668}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002669
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002670static void ftrace_startup_all(int command)
2671{
2672 update_all_ops = true;
2673 ftrace_startup_enable(command);
2674 update_all_ops = false;
2675}
2676
Steven Rostedta1cd6172011-05-23 15:24:25 -04002677static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002678{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002679 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002680
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002681 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002682 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002683
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002684 ret = __register_ftrace_function(ops);
2685 if (ret)
2686 return ret;
2687
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002688 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002689
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002690 /*
2691 * Note that ftrace probes uses this to start up
2692 * and modify functions it will probe. But we still
2693 * set the ADDING flag for modification, as probes
2694 * do not have trampolines. If they add them in the
2695 * future, then the probes will need to distinguish
2696 * between adding and updating probes.
2697 */
2698 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002699
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002700 ret = ftrace_hash_ipmodify_enable(ops);
2701 if (ret < 0) {
2702 /* Rollback registration process */
2703 __unregister_ftrace_function(ops);
2704 ftrace_start_up--;
2705 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2706 return ret;
2707 }
2708
Jiri Olsa7f50d062016-03-16 15:34:33 +01002709 if (ftrace_hash_rec_enable(ops, 1))
2710 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002711
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002712 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002713
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002714 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2715
Steven Rostedta1cd6172011-05-23 15:24:25 -04002716 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002717}
2718
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002719static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002720{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002721 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002722
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002723 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002724 return -ENODEV;
2725
2726 ret = __unregister_ftrace_function(ops);
2727 if (ret)
2728 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002729
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002730 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002731 /*
2732 * Just warn in case of unbalance, no need to kill ftrace, it's not
2733 * critical but the ftrace_call callers may be never nopped again after
2734 * further ftrace uses.
2735 */
2736 WARN_ON_ONCE(ftrace_start_up < 0);
2737
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002738 /* Disabling ipmodify never fails */
2739 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002740
2741 if (ftrace_hash_rec_disable(ops, 1))
2742 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002743
Namhyung Kima737e6d2014-06-12 23:56:12 +09002744 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002745
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002746 if (saved_ftrace_func != ftrace_trace_function) {
2747 saved_ftrace_func = ftrace_trace_function;
2748 command |= FTRACE_UPDATE_TRACE_FUNC;
2749 }
2750
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002751 if (!command || !ftrace_enabled) {
2752 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002753 * If these are per_cpu ops, they still need their
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002754 * per_cpu field freed. Since, function tracing is
2755 * not currently active, we can just free them
2756 * without synchronizing all CPUs.
2757 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002758 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2759 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002760 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002761 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002762
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002763 /*
2764 * If the ops uses a trampoline, then it needs to be
2765 * tested first on update.
2766 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002767 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002768 removed_ops = ops;
2769
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002770 /* The trampoline logic checks the old hashes */
2771 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2772 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2773
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002774 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002775
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002776 /*
2777 * If there's no more ops registered with ftrace, run a
2778 * sanity check to make sure all rec flags are cleared.
2779 */
2780 if (ftrace_ops_list == &ftrace_list_end) {
2781 struct ftrace_page *pg;
2782 struct dyn_ftrace *rec;
2783
2784 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002785 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002786 pr_warn(" %pS flags:%lx\n",
2787 (void *)rec->ip, rec->flags);
2788 } while_for_each_ftrace_rec();
2789 }
2790
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002791 ops->old_hash.filter_hash = NULL;
2792 ops->old_hash.notrace_hash = NULL;
2793
2794 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002795 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002796
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002797 /*
2798 * Dynamic ops may be freed, we must make sure that all
2799 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002800 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002801 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002802 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002803 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002804 /*
2805 * We need to do a hard force of sched synchronization.
2806 * This is because we use preempt_disable() to do RCU, but
2807 * the function tracers can be called where RCU is not watching
2808 * (like before user_exit()). We can not rely on the RCU
2809 * infrastructure to do the synchronization, thus we must do it
2810 * ourselves.
2811 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002812 schedule_on_each_cpu(ftrace_sync);
2813
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002814 /*
2815 * When the kernel is preeptive, tasks can be preempted
2816 * while on a ftrace trampoline. Just scheduling a task on
2817 * a CPU is not good enough to flush them. Calling
2818 * synchornize_rcu_tasks() will wait for those tasks to
2819 * execute and either schedule voluntarily or enter user space.
2820 */
2821 if (IS_ENABLED(CONFIG_PREEMPT))
2822 synchronize_rcu_tasks();
2823
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002824 arch_ftrace_trampoline_free(ops);
2825
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002826 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2827 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002828 }
2829
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002830 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002831}
2832
Ingo Molnare309b412008-05-12 21:20:51 +02002833static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002834{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302835 int command;
2836
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002837 if (unlikely(ftrace_disabled))
2838 return;
2839
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002840 /* Force update next time */
2841 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002842 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302843 if (ftrace_start_up) {
2844 command = FTRACE_UPDATE_CALLS;
2845 if (ftrace_graph_active)
2846 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002847 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302848 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002849}
2850
Ingo Molnare309b412008-05-12 21:20:51 +02002851static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002852{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302853 int command;
2854
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002855 if (unlikely(ftrace_disabled))
2856 return;
2857
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002858 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302859 if (ftrace_start_up) {
2860 command = FTRACE_DISABLE_CALLS;
2861 if (ftrace_graph_active)
2862 command |= FTRACE_STOP_FUNC_RET;
2863 ftrace_run_update_code(command);
2864 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002865}
2866
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002867static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002868unsigned long ftrace_update_tot_cnt;
2869
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002870static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002871{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002872 /*
2873 * Filter_hash being empty will default to trace module.
2874 * But notrace hash requires a test of individual module functions.
2875 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002876 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2877 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002878}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002879
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002880/*
2881 * Check if the current ops references the record.
2882 *
2883 * If the ops traces all functions, then it was already accounted for.
2884 * If the ops does not trace the current record function, skip it.
2885 * If the ops ignores the function via notrace filter, skip it.
2886 */
2887static inline bool
2888ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2889{
2890 /* If ops isn't enabled, ignore it */
2891 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2892 return 0;
2893
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002894 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002895 if (ops_traces_mod(ops))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002896 return 1;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002897
2898 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002899 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002900 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002901 return 0;
2902
2903 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002904 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002905 return 0;
2906
2907 return 1;
2908}
2909
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002910static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002911{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002912 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002913 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002914 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002915 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002916 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002917 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002918
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002919 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002920
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002921 /*
2922 * When a module is loaded, this function is called to convert
2923 * the calls to mcount in its text to nops, and also to create
2924 * an entry in the ftrace data. Now, if ftrace is activated
2925 * after this call, but before the module sets its text to
2926 * read-only, the modification of enabling ftrace can fail if
2927 * the read-only is done while ftrace is converting the calls.
2928 * To prevent this, the module's records are set as disabled
2929 * and will be enabled after the call to set the module's text
2930 * to read-only.
2931 */
2932 if (mod)
2933 rec_flags |= FTRACE_FL_DISABLED;
2934
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002935 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302936
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002937 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002938
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002939 /* If something went wrong, bail without enabling anything */
2940 if (unlikely(ftrace_disabled))
2941 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002942
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002943 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002944 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302945
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002946 /*
2947 * Do the initial record conversion from mcount jump
2948 * to the NOP instructions.
2949 */
2950 if (!ftrace_code_disable(mod, p))
2951 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002952
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002953 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002954 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002955 }
2956
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002957 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002958 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002959 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002960
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002961 return 0;
2962}
2963
Steven Rostedta7900872011-12-16 16:23:44 -05002964static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002965{
Steven Rostedta7900872011-12-16 16:23:44 -05002966 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002967 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002968
Steven Rostedta7900872011-12-16 16:23:44 -05002969 if (WARN_ON(!count))
2970 return -EINVAL;
2971
2972 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002973
2974 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002975 * We want to fill as much as possible. No more than a page
2976 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002977 */
Steven Rostedta7900872011-12-16 16:23:44 -05002978 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2979 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002980
Steven Rostedta7900872011-12-16 16:23:44 -05002981 again:
2982 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2983
2984 if (!pg->records) {
2985 /* if we can't allocate this size, try something smaller */
2986 if (!order)
2987 return -ENOMEM;
2988 order >>= 1;
2989 goto again;
2990 }
2991
2992 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2993 pg->size = cnt;
2994
2995 if (cnt > count)
2996 cnt = count;
2997
2998 return cnt;
2999}
3000
3001static struct ftrace_page *
3002ftrace_allocate_pages(unsigned long num_to_init)
3003{
3004 struct ftrace_page *start_pg;
3005 struct ftrace_page *pg;
3006 int order;
3007 int cnt;
3008
3009 if (!num_to_init)
3010 return 0;
3011
3012 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3013 if (!pg)
3014 return NULL;
3015
3016 /*
3017 * Try to allocate as much as possible in one continues
3018 * location that fills in all of the space. We want to
3019 * waste as little space as possible.
3020 */
3021 for (;;) {
3022 cnt = ftrace_allocate_records(pg, num_to_init);
3023 if (cnt < 0)
3024 goto free_pages;
3025
3026 num_to_init -= cnt;
3027 if (!num_to_init)
3028 break;
3029
3030 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3031 if (!pg->next)
3032 goto free_pages;
3033
3034 pg = pg->next;
3035 }
3036
3037 return start_pg;
3038
3039 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003040 pg = start_pg;
3041 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003042 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3043 free_pages((unsigned long)pg->records, order);
3044 start_pg = pg->next;
3045 kfree(pg);
3046 pg = start_pg;
3047 }
3048 pr_info("ftrace: FAILED to allocate memory for functions\n");
3049 return NULL;
3050}
3051
Steven Rostedt5072c592008-05-12 21:20:43 +02003052#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3053
3054struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003055 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003056 loff_t func_pos;
3057 struct ftrace_page *pg;
3058 struct dyn_ftrace *func;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003059 struct ftrace_probe_ops *probe;
3060 struct ftrace_func_entry *probe_entry;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003061 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003062 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003063 struct ftrace_ops *ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003064 int pidx;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003065 int idx;
3066 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003067};
3068
Ingo Molnare309b412008-05-12 21:20:51 +02003069static void *
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003070t_probe_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003071{
3072 struct ftrace_iterator *iter = m->private;
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)eee8ded2017-04-04 21:31:28 -04003082 if (list_empty(&ftrace_func_probes))
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003083 return NULL;
3084
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003085 if (!iter->probe) {
3086 next = ftrace_func_probes.next;
3087 iter->probe = list_entry(next, struct ftrace_probe_ops, list);
3088 }
3089
3090 if (iter->probe_entry)
3091 hnd = &iter->probe_entry->hlist;
3092
3093 hash = iter->probe->ops.func_hash->filter_hash;
3094 size = 1 << hash->size_bits;
3095
3096 retry:
3097 if (iter->pidx >= size) {
3098 if (iter->probe->list.next == &ftrace_func_probes)
3099 return NULL;
3100 next = iter->probe->list.next;
3101 iter->probe = list_entry(next, struct ftrace_probe_ops, list);
3102 hash = iter->probe->ops.func_hash->filter_hash;
3103 size = 1 << hash->size_bits;
3104 iter->pidx = 0;
3105 }
3106
3107 hhd = &hash->buckets[iter->pidx];
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003108
3109 if (hlist_empty(hhd)) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003110 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003111 hnd = NULL;
3112 goto retry;
3113 }
3114
3115 if (!hnd)
3116 hnd = hhd->first;
3117 else {
3118 hnd = hnd->next;
3119 if (!hnd) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003120 iter->pidx++;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003121 goto retry;
3122 }
3123 }
3124
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003125 if (WARN_ON_ONCE(!hnd))
3126 return NULL;
3127
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003128 iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003129
3130 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003131}
3132
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003133static void *t_probe_start(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003134{
3135 struct ftrace_iterator *iter = m->private;
3136 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003137 loff_t l;
3138
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003139 if (!(iter->flags & FTRACE_ITER_DO_PROBES))
Steven Rostedt69a30832011-12-19 15:21:16 -05003140 return NULL;
3141
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003142 if (iter->func_pos > *pos)
3143 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003144
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003145 iter->probe = NULL;
3146 iter->probe_entry = NULL;
3147 iter->pidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003148 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003149 p = t_probe_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003150 if (!p)
3151 break;
3152 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003153 if (!p)
3154 return NULL;
3155
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003156 /* Only set this if we have an item */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003157 iter->flags |= FTRACE_ITER_PROBE;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003158
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003159 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003160}
3161
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003162static int
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003163t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003164{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003165 struct ftrace_probe_ops *probe;
3166 struct ftrace_func_entry *probe_entry;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003167
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003168 probe = iter->probe;
3169 probe_entry = iter->probe_entry;
3170
3171 if (WARN_ON_ONCE(!probe || !probe_entry))
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003172 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003173
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003174 if (probe->print)
3175 return probe->print(m, probe_entry->ip, probe, NULL);
Steven Rostedt809dcf22009-02-16 23:06:01 -05003176
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003177 seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip, (void *)probe->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003178
3179 return 0;
3180}
3181
3182static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003183t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003184{
3185 struct ftrace_iterator *iter = m->private;
3186 struct dyn_ftrace *rec = NULL;
3187
3188 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003189
Steven Rostedt5072c592008-05-12 21:20:43 +02003190 retry:
3191 if (iter->idx >= iter->pg->index) {
3192 if (iter->pg->next) {
3193 iter->pg = iter->pg->next;
3194 iter->idx = 0;
3195 goto retry;
3196 }
3197 } else {
3198 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003199 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3200 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003201
3202 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003203 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003204
Steven Rostedt5072c592008-05-12 21:20:43 +02003205 rec = NULL;
3206 goto retry;
3207 }
3208 }
3209
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003210 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003211 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003212
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003213 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003214 iter->func = rec;
3215
3216 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003217}
3218
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003219static void *
3220t_next(struct seq_file *m, void *v, loff_t *pos)
3221{
3222 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)fcdc7122017-04-17 10:22:29 -04003223 loff_t l = *pos; /* t_hash_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003224 void *ret;
3225
3226 if (unlikely(ftrace_disabled))
3227 return NULL;
3228
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003229 if (iter->flags & FTRACE_ITER_PROBE)
3230 return t_probe_next(m, pos);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003231
3232 if (iter->flags & FTRACE_ITER_PRINTALL) {
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003233 /* next must increment pos, and t_probe_start does not */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003234 (*pos)++;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003235 return t_probe_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003236 }
3237
3238 ret = t_func_next(m, pos);
3239
3240 if (!ret)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003241 return t_probe_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003242
3243 return ret;
3244}
3245
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003246static void reset_iter_read(struct ftrace_iterator *iter)
3247{
3248 iter->pos = 0;
3249 iter->func_pos = 0;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003250 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE);
Steven Rostedt5072c592008-05-12 21:20:43 +02003251}
3252
3253static void *t_start(struct seq_file *m, loff_t *pos)
3254{
3255 struct ftrace_iterator *iter = m->private;
3256 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003257 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003258
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003259 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003260
3261 if (unlikely(ftrace_disabled))
3262 return NULL;
3263
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003264 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003265 * If an lseek was done, then reset and start from beginning.
3266 */
3267 if (*pos < iter->pos)
3268 reset_iter_read(iter);
3269
3270 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003271 * For set_ftrace_filter reading, if we have the filter
3272 * off, we can short cut and just print out that all
3273 * functions are enabled.
3274 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003275 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3276 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003277 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003278 if (*pos > 0)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003279 return t_probe_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003280 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003281 /* reset in case of seek/pread */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003282 iter->flags &= ~FTRACE_ITER_PROBE;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003283 return iter;
3284 }
3285
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003286 if (iter->flags & FTRACE_ITER_PROBE)
3287 return t_probe_start(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003288
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003289 /*
3290 * Unfortunately, we need to restart at ftrace_pages_start
3291 * every time we let go of the ftrace_mutex. This is because
3292 * those pointers can change without the lock.
3293 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003294 iter->pg = ftrace_pages_start;
3295 iter->idx = 0;
3296 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003297 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003298 if (!p)
3299 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003300 }
walimis5821e1b2008-11-15 15:19:06 +08003301
Steven Rostedt69a30832011-12-19 15:21:16 -05003302 if (!p)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003303 return t_probe_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003304
3305 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003306}
3307
3308static void t_stop(struct seq_file *m, void *p)
3309{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003310 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003311}
3312
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003313void * __weak
3314arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3315{
3316 return NULL;
3317}
3318
3319static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3320 struct dyn_ftrace *rec)
3321{
3322 void *ptr;
3323
3324 ptr = arch_ftrace_trampoline_func(ops, rec);
3325 if (ptr)
3326 seq_printf(m, " ->%pS", ptr);
3327}
3328
Steven Rostedt5072c592008-05-12 21:20:43 +02003329static int t_show(struct seq_file *m, void *v)
3330{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003331 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003332 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003333
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003334 if (iter->flags & FTRACE_ITER_PROBE)
3335 return t_probe_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003336
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003337 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003338 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003339 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003340 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003341 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003342 return 0;
3343 }
3344
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003345 rec = iter->func;
3346
Steven Rostedt5072c592008-05-12 21:20:43 +02003347 if (!rec)
3348 return 0;
3349
Steven Rostedt647bcd02011-05-03 14:39:21 -04003350 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003351 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003352 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003353
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003354 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003355 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003356 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3357 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003358 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003359 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003360 if (ops) {
3361 do {
3362 seq_printf(m, "\ttramp: %pS (%pS)",
3363 (void *)ops->trampoline,
3364 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003365 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003366 ops = ftrace_find_tramp_ops_next(rec, ops);
3367 } while (ops);
3368 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003369 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003370 } else {
3371 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003372 }
3373 }
3374
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003375 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003376
3377 return 0;
3378}
3379
James Morris88e9d342009-09-22 16:43:43 -07003380static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003381 .start = t_start,
3382 .next = t_next,
3383 .stop = t_stop,
3384 .show = t_show,
3385};
3386
Ingo Molnare309b412008-05-12 21:20:51 +02003387static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003388ftrace_avail_open(struct inode *inode, struct file *file)
3389{
3390 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003391
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003392 if (unlikely(ftrace_disabled))
3393 return -ENODEV;
3394
Jiri Olsa50e18b92012-04-25 10:23:39 +02003395 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003396 if (!iter)
3397 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003398
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003399 iter->pg = ftrace_pages_start;
3400 iter->ops = &global_ops;
3401
3402 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003403}
3404
Steven Rostedt647bcd02011-05-03 14:39:21 -04003405static int
3406ftrace_enabled_open(struct inode *inode, struct file *file)
3407{
3408 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003409
Jiri Olsa50e18b92012-04-25 10:23:39 +02003410 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003411 if (!iter)
3412 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003413
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003414 iter->pg = ftrace_pages_start;
3415 iter->flags = FTRACE_ITER_ENABLED;
3416 iter->ops = &global_ops;
3417
3418 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003419}
3420
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003421/**
3422 * ftrace_regex_open - initialize function tracer filter files
3423 * @ops: The ftrace_ops that hold the hash filters
3424 * @flag: The type of filter to process
3425 * @inode: The inode, usually passed in to your open routine
3426 * @file: The file, usually passed in to your open routine
3427 *
3428 * ftrace_regex_open() initializes the filter files for the
3429 * @ops. Depending on @flag it may process the filter hash or
3430 * the notrace hash of @ops. With this called from the open
3431 * routine, you can use ftrace_filter_write() for the write
3432 * routine if @flag has FTRACE_ITER_FILTER set, or
3433 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003434 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003435 * release must call ftrace_regex_release().
3436 */
3437int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003438ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003439 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003440{
3441 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003442 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003443 int ret = 0;
3444
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003445 ftrace_ops_init(ops);
3446
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003447 if (unlikely(ftrace_disabled))
3448 return -ENODEV;
3449
Steven Rostedt5072c592008-05-12 21:20:43 +02003450 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3451 if (!iter)
3452 return -ENOMEM;
3453
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003454 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3455 kfree(iter);
3456 return -ENOMEM;
3457 }
3458
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003459 iter->ops = ops;
3460 iter->flags = flag;
3461
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003462 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003463
Steven Rostedtf45948e2011-05-02 12:29:25 -04003464 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003465 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003466 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003467 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003468
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003469 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003470 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3471
3472 if (file->f_flags & O_TRUNC)
3473 iter->hash = alloc_ftrace_hash(size_bits);
3474 else
3475 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3476
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003477 if (!iter->hash) {
3478 trace_parser_put(&iter->parser);
3479 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003480 ret = -ENOMEM;
3481 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003482 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003483 } else
3484 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003485
Steven Rostedt5072c592008-05-12 21:20:43 +02003486 if (file->f_mode & FMODE_READ) {
3487 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003488
3489 ret = seq_open(file, &show_ftrace_seq_ops);
3490 if (!ret) {
3491 struct seq_file *m = file->private_data;
3492 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003493 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003494 /* Failed */
3495 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003496 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003497 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003498 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003499 } else
3500 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003501
3502 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003503 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003504
3505 return ret;
3506}
3507
Steven Rostedt41c52c02008-05-22 11:46:33 -04003508static int
3509ftrace_filter_open(struct inode *inode, struct file *file)
3510{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003511 struct ftrace_ops *ops = inode->i_private;
3512
3513 return ftrace_regex_open(ops,
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003514 FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
Steven Rostedt69a30832011-12-19 15:21:16 -05003515 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003516}
3517
3518static int
3519ftrace_notrace_open(struct inode *inode, struct file *file)
3520{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003521 struct ftrace_ops *ops = inode->i_private;
3522
3523 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003524 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003525}
3526
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003527/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3528struct ftrace_glob {
3529 char *search;
3530 unsigned len;
3531 int type;
3532};
3533
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003534/*
3535 * If symbols in an architecture don't correspond exactly to the user-visible
3536 * name of what they represent, it is possible to define this function to
3537 * perform the necessary adjustments.
3538*/
3539char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3540{
3541 return str;
3542}
3543
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003544static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003545{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003546 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003547 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003548
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003549 str = arch_ftrace_match_adjust(str, g->search);
3550
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003551 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003552 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003553 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003554 matched = 1;
3555 break;
3556 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003557 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003558 matched = 1;
3559 break;
3560 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003561 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003562 matched = 1;
3563 break;
3564 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003565 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003566 if (slen >= g->len &&
3567 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003568 matched = 1;
3569 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003570 case MATCH_GLOB:
3571 if (glob_match(g->search, str))
3572 matched = 1;
3573 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003574 }
3575
3576 return matched;
3577}
3578
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003579static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003580enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003581{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003582 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003583 int ret = 0;
3584
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003585 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003586 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003587 /* Do nothing if it doesn't exist */
3588 if (!entry)
3589 return 0;
3590
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003591 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003592 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003593 /* Do nothing if it exists */
3594 if (entry)
3595 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003596
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003597 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003598 }
3599 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003600}
3601
Steven Rostedt64e7c442009-02-13 17:08:48 -05003602static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003603ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3604 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003605{
3606 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003607 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003608
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003609 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3610
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003611 if (mod_g) {
3612 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3613
3614 /* blank module name to match all modules */
3615 if (!mod_g->len) {
3616 /* blank module globbing: modname xor exclude_mod */
3617 if ((!exclude_mod) != (!modname))
3618 goto func_match;
3619 return 0;
3620 }
3621
3622 /* not matching the module */
3623 if (!modname || !mod_matches) {
3624 if (exclude_mod)
3625 goto func_match;
3626 else
3627 return 0;
3628 }
3629
3630 if (mod_matches && exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003631 return 0;
3632
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003633func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003634 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003635 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003636 return 1;
3637 }
3638
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003639 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003640}
3641
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003642static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003643match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003644{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003645 struct ftrace_page *pg;
3646 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003647 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003648 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3649 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3650 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003651 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003652 int ret;
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003653 int clear_filter;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003654
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003655 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003656 func_g.type = filter_parse_regex(func, len, &func_g.search,
3657 &clear_filter);
3658 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003659 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003660
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003661 if (mod) {
3662 mod_g.type = filter_parse_regex(mod, strlen(mod),
3663 &mod_g.search, &exclude_mod);
3664 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003665 }
3666
Steven Rostedt52baf112009-02-14 01:15:39 -05003667 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003668
3669 if (unlikely(ftrace_disabled))
3670 goto out_unlock;
3671
Steven Rostedt265c8312009-02-13 12:43:56 -05003672 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003673
3674 if (rec->flags & FTRACE_FL_DISABLED)
3675 continue;
3676
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003677 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003678 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003679 if (ret < 0) {
3680 found = ret;
3681 goto out_unlock;
3682 }
Li Zefan311d16d2009-12-08 11:15:11 +08003683 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003684 }
3685 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003686 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003687 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003688
3689 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003690}
3691
Steven Rostedt64e7c442009-02-13 17:08:48 -05003692static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003693ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003694{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003695 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003696}
3697
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04003698static void ftrace_ops_update_code(struct ftrace_ops *ops,
3699 struct ftrace_ops_hash *old_hash)
3700{
3701 struct ftrace_ops *op;
3702
3703 if (!ftrace_enabled)
3704 return;
3705
3706 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3707 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3708 return;
3709 }
3710
3711 /*
3712 * If this is the shared global_ops filter, then we need to
3713 * check if there is another ops that shares it, is enabled.
3714 * If so, we still need to run the modify code.
3715 */
3716 if (ops->func_hash != &global_ops.local_hash)
3717 return;
3718
3719 do_for_each_ftrace_op(op, ftrace_ops_list) {
3720 if (op->func_hash == &global_ops.local_hash &&
3721 op->flags & FTRACE_OPS_FL_ENABLED) {
3722 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3723 /* Only need to do this once */
3724 return;
3725 }
3726 } while_for_each_ftrace_op(op);
3727}
3728
3729static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3730 struct ftrace_hash **orig_hash,
3731 struct ftrace_hash *hash,
3732 int enable)
3733{
3734 struct ftrace_ops_hash old_hash_ops;
3735 struct ftrace_hash *old_hash;
3736 int ret;
3737
3738 old_hash = *orig_hash;
3739 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3740 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3741 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3742 if (!ret) {
3743 ftrace_ops_update_code(ops, &old_hash_ops);
3744 free_ftrace_hash_rcu(old_hash);
3745 }
3746 return ret;
3747}
Steven Rostedt64e7c442009-02-13 17:08:48 -05003748
Steven Rostedtf6180772009-02-14 00:40:25 -05003749/*
3750 * We register the module command as a template to show others how
3751 * to register the a command as well.
3752 */
3753
3754static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003755ftrace_mod_callback(struct ftrace_hash *hash,
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003756 char *func, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003757{
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003758 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003759
3760 /*
3761 * cmd == 'mod' because we only registered this func
3762 * for the 'mod' ftrace_func_command.
3763 * But if you register one func with multiple commands,
3764 * you can tell which command was used by the cmd
3765 * parameter.
3766 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003767 ret = match_records(hash, func, strlen(func), module);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003768 if (!ret)
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003769 return -EINVAL;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003770 if (ret < 0)
3771 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003772 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003773}
3774
3775static struct ftrace_func_command ftrace_mod_cmd = {
3776 .name = "mod",
3777 .func = ftrace_mod_callback,
3778};
3779
3780static int __init ftrace_mod_cmd_init(void)
3781{
3782 return register_ftrace_command(&ftrace_mod_cmd);
3783}
Steven Rostedt6f415672012-10-05 12:13:07 -04003784core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003785
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003786static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003787 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003788{
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003789 struct ftrace_probe_ops *probe_ops;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003790
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003791 probe_ops = container_of(op, struct ftrace_probe_ops, ops);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003792
3793 /*
3794 * Disable preemption for these calls to prevent a RCU grace
3795 * period. This syncs the hash iteration and freeing of items
3796 * on the hash. rcu_read_lock is too dangerous here.
3797 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003798 preempt_disable_notrace();
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003799 probe_ops->func(ip, parent_ip, probe_ops, NULL);
Steven Rostedt5168ae52010-06-03 09:36:50 -04003800 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003801}
3802
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04003803struct ftrace_func_map {
3804 struct ftrace_func_entry entry;
3805 void *data;
3806};
3807
3808struct ftrace_func_mapper {
3809 struct ftrace_hash hash;
3810};
3811
3812/**
3813 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
3814 *
3815 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
3816 */
3817struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
3818{
3819 struct ftrace_hash *hash;
3820
3821 /*
3822 * The mapper is simply a ftrace_hash, but since the entries
3823 * in the hash are not ftrace_func_entry type, we define it
3824 * as a separate structure.
3825 */
3826 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3827 return (struct ftrace_func_mapper *)hash;
3828}
3829
3830/**
3831 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
3832 * @mapper: The mapper that has the ip maps
3833 * @ip: the instruction pointer to find the data for
3834 *
3835 * Returns the data mapped to @ip if found otherwise NULL. The return
3836 * is actually the address of the mapper data pointer. The address is
3837 * returned for use cases where the data is no bigger than a long, and
3838 * the user can use the data pointer as its data instead of having to
3839 * allocate more memory for the reference.
3840 */
3841void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
3842 unsigned long ip)
3843{
3844 struct ftrace_func_entry *entry;
3845 struct ftrace_func_map *map;
3846
3847 entry = ftrace_lookup_ip(&mapper->hash, ip);
3848 if (!entry)
3849 return NULL;
3850
3851 map = (struct ftrace_func_map *)entry;
3852 return &map->data;
3853}
3854
3855/**
3856 * ftrace_func_mapper_add_ip - Map some data to an ip
3857 * @mapper: The mapper that has the ip maps
3858 * @ip: The instruction pointer address to map @data to
3859 * @data: The data to map to @ip
3860 *
3861 * Returns 0 on succes otherwise an error.
3862 */
3863int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
3864 unsigned long ip, void *data)
3865{
3866 struct ftrace_func_entry *entry;
3867 struct ftrace_func_map *map;
3868
3869 entry = ftrace_lookup_ip(&mapper->hash, ip);
3870 if (entry)
3871 return -EBUSY;
3872
3873 map = kmalloc(sizeof(*map), GFP_KERNEL);
3874 if (!map)
3875 return -ENOMEM;
3876
3877 map->entry.ip = ip;
3878 map->data = data;
3879
3880 __add_hash_entry(&mapper->hash, &map->entry);
3881
3882 return 0;
3883}
3884
3885/**
3886 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
3887 * @mapper: The mapper that has the ip maps
3888 * @ip: The instruction pointer address to remove the data from
3889 *
3890 * Returns the data if it is found, otherwise NULL.
3891 * Note, if the data pointer is used as the data itself, (see
3892 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
3893 * if the data pointer was set to zero.
3894 */
3895void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
3896 unsigned long ip)
3897{
3898 struct ftrace_func_entry *entry;
3899 struct ftrace_func_map *map;
3900 void *data;
3901
3902 entry = ftrace_lookup_ip(&mapper->hash, ip);
3903 if (!entry)
3904 return NULL;
3905
3906 map = (struct ftrace_func_map *)entry;
3907 data = map->data;
3908
3909 remove_hash_entry(&mapper->hash, entry);
3910 kfree(entry);
3911
3912 return data;
3913}
3914
3915/**
3916 * free_ftrace_func_mapper - free a mapping of ips and data
3917 * @mapper: The mapper that has the ip maps
3918 * @free_func: A function to be called on each data item.
3919 *
3920 * This is used to free the function mapper. The @free_func is optional
3921 * and can be used if the data needs to be freed as well.
3922 */
3923void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
3924 ftrace_mapper_func free_func)
3925{
3926 struct ftrace_func_entry *entry;
3927 struct ftrace_func_map *map;
3928 struct hlist_head *hhd;
3929 int size = 1 << mapper->hash.size_bits;
3930 int i;
3931
3932 if (free_func && mapper->hash.count) {
3933 for (i = 0; i < size; i++) {
3934 hhd = &mapper->hash.buckets[i];
3935 hlist_for_each_entry(entry, hhd, hlist) {
3936 map = (struct ftrace_func_map *)entry;
3937 free_func(map);
3938 }
3939 }
3940 }
3941 free_ftrace_hash(&mapper->hash);
3942}
3943
Steven Rostedt59df055f2009-02-14 15:29:06 -05003944int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003945register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003946 void *data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003947{
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003948 struct ftrace_func_entry *entry;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003949 struct ftrace_hash **orig_hash;
3950 struct ftrace_hash *old_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003951 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003952 int count = 0;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003953 int size;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003954 int ret;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003955 int i;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003956
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003957 /* We do not support '!' for function probes */
3958 if (WARN_ON(glob[0] == '!'))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003959 return -EINVAL;
3960
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003961 if (!(ops->ops.flags & FTRACE_OPS_FL_INITIALIZED)) {
3962 ops->ops.func = function_trace_probe_call;
3963 ftrace_ops_init(&ops->ops);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003964 INIT_LIST_HEAD(&ops->list);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003965 }
3966
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003967 mutex_lock(&ops->ops.func_hash->regex_lock);
3968
3969 orig_hash = &ops->ops.func_hash->filter_hash;
3970 old_hash = *orig_hash;
3971 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3972
3973 ret = ftrace_match_records(hash, glob, strlen(glob));
3974
3975 /* Nothing found? */
3976 if (!ret)
3977 ret = -EINVAL;
3978
3979 if (ret < 0)
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003980 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003981
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003982 size = 1 << hash->size_bits;
3983 for (i = 0; i < size; i++) {
3984 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
3985 if (ftrace_lookup_ip(old_hash, entry->ip))
3986 continue;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003987 /*
3988 * The caller might want to do something special
3989 * for each function we find. We call the callback
3990 * to give the caller an opportunity to do so.
3991 */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04003992 if (ops->init) {
3993 ret = ops->init(ops, entry->ip, data);
3994 if (ret < 0)
3995 goto out;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003996 }
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04003997 count++;
3998 }
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003999 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004000
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004001 mutex_lock(&ftrace_lock);
4002
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004003 ret = ftrace_hash_move_and_update_ops(&ops->ops, orig_hash,
4004 hash, 1);
4005 if (ret < 0)
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004006 goto out_unlock;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004007
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004008 if (list_empty(&ops->list))
4009 list_add(&ops->list, &ftrace_func_probes);
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004010
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004011 if (!(ops->ops.flags & FTRACE_OPS_FL_ENABLED))
4012 ret = ftrace_startup(&ops->ops, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004013
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004014 out_unlock:
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004015 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004016
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004017 if (!ret)
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004018 ret = count;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004019 out:
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004020 mutex_unlock(&ops->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004021 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004022
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004023 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004024}
4025
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004026int
Steven Rostedt (VMware)78f78e02017-04-04 13:39:59 -04004027unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004028{
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004029 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004030 struct ftrace_func_entry *entry;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004031 struct ftrace_glob func_g;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004032 struct ftrace_hash **orig_hash;
4033 struct ftrace_hash *old_hash;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004034 struct ftrace_hash *hash = NULL;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004035 struct hlist_node *tmp;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004036 struct hlist_head hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004037 char str[KSYM_SYMBOL_LEN];
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004038 int i, ret;
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004039 int size;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004040
4041 if (!(ops->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4042 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004043
Atsushi Tsujib36461d2009-09-15 19:06:30 +09004044 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004045 func_g.search = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09004046 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004047 int not;
4048
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004049 func_g.type = filter_parse_regex(glob, strlen(glob),
4050 &func_g.search, &not);
4051 func_g.len = strlen(func_g.search);
4052 func_g.search = glob;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004053
Steven Rostedtb6887d72009-02-17 12:32:04 -05004054 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004055 if (WARN_ON(not))
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004056 return -EINVAL;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004057 }
4058
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004059 mutex_lock(&ops->ops.func_hash->regex_lock);
4060
4061 orig_hash = &ops->ops.func_hash->filter_hash;
4062 old_hash = *orig_hash;
4063
4064 ret = -EINVAL;
4065 if (ftrace_hash_empty(old_hash))
4066 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004067
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004068 old_hash_ops.filter_hash = old_hash;
4069 /* Probes only have filters */
4070 old_hash_ops.notrace_hash = NULL;
4071
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004072 ret = -ENOMEM;
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004073 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004074 if (!hash)
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004075 goto out_unlock;
4076
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004077 INIT_HLIST_HEAD(&hhd);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004078
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004079 size = 1 << hash->size_bits;
4080 for (i = 0; i < size; i++) {
4081 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004082
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004083 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004084 kallsyms_lookup(entry->ip, NULL, NULL,
4085 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004086 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004087 continue;
4088 }
4089
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004090 remove_hash_entry(hash, entry);
4091 hlist_add_head(&entry->hlist, &hhd);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004092 }
4093 }
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004094
4095 /* Nothing found? */
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004096 if (hlist_empty(&hhd)) {
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004097 ret = -EINVAL;
4098 goto out_unlock;
4099 }
4100
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004101 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004102
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004103 if (ftrace_hash_empty(hash)) {
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004104 ftrace_shutdown(&ops->ops, 0);
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004105 list_del_init(&ops->list);
4106 }
4107
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004108
4109 ret = ftrace_hash_move_and_update_ops(&ops->ops, orig_hash,
4110 hash, 1);
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004111
4112 /* still need to update the function call sites */
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004113 if (ftrace_enabled && !ftrace_hash_empty(hash))
4114 ftrace_run_modify_code(&ops->ops, FTRACE_UPDATE_CALLS,
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004115 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004116 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004117
Steven Rostedt (VMware)eee8ded2017-04-04 21:31:28 -04004118 hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4119 hlist_del(&entry->hlist);
4120 if (ops->free)
4121 ops->free(ops, entry->ip, NULL);
4122 kfree(entry);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004123 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004124 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004125
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004126 out_unlock:
Steven Rostedt (VMware)1ec3a812017-04-04 18:16:29 -04004127 mutex_unlock(&ops->ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004128 free_ftrace_hash(hash);
Steven Rostedt (VMware)d3d532d2017-04-04 16:44:43 -04004129 return ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004130}
4131
Steven Rostedtf6180772009-02-14 00:40:25 -05004132static LIST_HEAD(ftrace_commands);
4133static DEFINE_MUTEX(ftrace_cmd_mutex);
4134
Tom Zanussi38de93a2013-10-24 08:34:18 -05004135/*
4136 * Currently we only register ftrace commands from __init, so mark this
4137 * __init too.
4138 */
4139__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004140{
4141 struct ftrace_func_command *p;
4142 int ret = 0;
4143
4144 mutex_lock(&ftrace_cmd_mutex);
4145 list_for_each_entry(p, &ftrace_commands, list) {
4146 if (strcmp(cmd->name, p->name) == 0) {
4147 ret = -EBUSY;
4148 goto out_unlock;
4149 }
4150 }
4151 list_add(&cmd->list, &ftrace_commands);
4152 out_unlock:
4153 mutex_unlock(&ftrace_cmd_mutex);
4154
4155 return ret;
4156}
4157
Tom Zanussi38de93a2013-10-24 08:34:18 -05004158/*
4159 * Currently we only unregister ftrace commands from __init, so mark
4160 * this __init too.
4161 */
4162__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004163{
4164 struct ftrace_func_command *p, *n;
4165 int ret = -ENODEV;
4166
4167 mutex_lock(&ftrace_cmd_mutex);
4168 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4169 if (strcmp(cmd->name, p->name) == 0) {
4170 ret = 0;
4171 list_del_init(&p->list);
4172 goto out_unlock;
4173 }
4174 }
4175 out_unlock:
4176 mutex_unlock(&ftrace_cmd_mutex);
4177
4178 return ret;
4179}
4180
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004181static int ftrace_process_regex(struct ftrace_hash *hash,
4182 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004183{
Steven Rostedtf6180772009-02-14 00:40:25 -05004184 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004185 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004186 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004187
4188 func = strsep(&next, ":");
4189
4190 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004191 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004192 if (!ret)
4193 ret = -EINVAL;
4194 if (ret < 0)
4195 return ret;
4196 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004197 }
4198
Steven Rostedtf6180772009-02-14 00:40:25 -05004199 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004200
4201 command = strsep(&next, ":");
4202
Steven Rostedtf6180772009-02-14 00:40:25 -05004203 mutex_lock(&ftrace_cmd_mutex);
4204 list_for_each_entry(p, &ftrace_commands, list) {
4205 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04004206 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004207 goto out_unlock;
4208 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004209 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004210 out_unlock:
4211 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004212
Steven Rostedtf6180772009-02-14 00:40:25 -05004213 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004214}
4215
Ingo Molnare309b412008-05-12 21:20:51 +02004216static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004217ftrace_regex_write(struct file *file, const char __user *ubuf,
4218 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004219{
4220 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004221 struct trace_parser *parser;
4222 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004223
Li Zefan4ba79782009-09-22 13:52:20 +08004224 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004225 return 0;
4226
Steven Rostedt5072c592008-05-12 21:20:43 +02004227 if (file->f_mode & FMODE_READ) {
4228 struct seq_file *m = file->private_data;
4229 iter = m->private;
4230 } else
4231 iter = file->private_data;
4232
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004233 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004234 return -ENODEV;
4235
4236 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004237
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004238 parser = &iter->parser;
4239 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004240
Li Zefan4ba79782009-09-22 13:52:20 +08004241 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004242 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004243 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004244 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004245 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004246 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004247 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004248 }
4249
Steven Rostedt5072c592008-05-12 21:20:43 +02004250 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004251 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004252 return ret;
4253}
4254
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004255ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004256ftrace_filter_write(struct file *file, const char __user *ubuf,
4257 size_t cnt, loff_t *ppos)
4258{
4259 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4260}
4261
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004262ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004263ftrace_notrace_write(struct file *file, const char __user *ubuf,
4264 size_t cnt, loff_t *ppos)
4265{
4266 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4267}
4268
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004269static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004270ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4271{
4272 struct ftrace_func_entry *entry;
4273
4274 if (!ftrace_location(ip))
4275 return -EINVAL;
4276
4277 if (remove) {
4278 entry = ftrace_lookup_ip(hash, ip);
4279 if (!entry)
4280 return -ENOENT;
4281 free_hash_entry(hash, entry);
4282 return 0;
4283 }
4284
4285 return add_hash_entry(hash, ip);
4286}
4287
4288static int
4289ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4290 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004291{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004292 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004293 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004294 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004295
Steven Rostedt41c52c02008-05-22 11:46:33 -04004296 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004297 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004298
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004299 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004300
Steven Rostedtf45948e2011-05-02 12:29:25 -04004301 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004302 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004303 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004304 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004305
Wang Nanb972cc52014-07-15 08:40:20 +08004306 if (reset)
4307 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4308 else
4309 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4310
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004311 if (!hash) {
4312 ret = -ENOMEM;
4313 goto out_regex_unlock;
4314 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004315
Jiri Olsaac483c42012-01-02 10:04:14 +01004316 if (buf && !ftrace_match_records(hash, buf, len)) {
4317 ret = -EINVAL;
4318 goto out_regex_unlock;
4319 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004320 if (ip) {
4321 ret = ftrace_match_addr(hash, ip, remove);
4322 if (ret < 0)
4323 goto out_regex_unlock;
4324 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004325
4326 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004327 ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004328 mutex_unlock(&ftrace_lock);
4329
Jiri Olsaac483c42012-01-02 10:04:14 +01004330 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004331 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004332
4333 free_ftrace_hash(hash);
4334 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004335}
4336
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004337static int
4338ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4339 int reset, int enable)
4340{
4341 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4342}
4343
4344/**
4345 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4346 * @ops - the ops to set the filter with
4347 * @ip - the address to add to or remove from the filter.
4348 * @remove - non zero to remove the ip from the filter
4349 * @reset - non zero to reset all filters before applying this filter.
4350 *
4351 * Filters denote which functions should be enabled when tracing is enabled
4352 * If @ip is NULL, it failes to update filter.
4353 */
4354int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4355 int remove, int reset)
4356{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004357 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004358 return ftrace_set_addr(ops, ip, remove, reset, 1);
4359}
4360EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4361
Joel Fernandesd032ae82016-11-15 12:31:20 -08004362/**
4363 * ftrace_ops_set_global_filter - setup ops to use global filters
4364 * @ops - the ops which will use the global filters
4365 *
4366 * ftrace users who need global function trace filtering should call this.
4367 * It can set the global filter only if ops were not initialized before.
4368 */
4369void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4370{
4371 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4372 return;
4373
4374 ftrace_ops_init(ops);
4375 ops->func_hash = &global_ops.local_hash;
4376}
4377EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4378
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004379static int
4380ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4381 int reset, int enable)
4382{
4383 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4384}
4385
Steven Rostedt77a2b372008-05-12 21:20:45 +02004386/**
4387 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004388 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004389 * @buf - the string that holds the function filter text.
4390 * @len - the length of the string.
4391 * @reset - non zero to reset all filters before applying this filter.
4392 *
4393 * Filters denote which functions should be enabled when tracing is enabled.
4394 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4395 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004396int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004397 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004398{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004399 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004400 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004401}
Steven Rostedt936e0742011-05-05 22:54:01 -04004402EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004403
Steven Rostedt41c52c02008-05-22 11:46:33 -04004404/**
4405 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004406 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004407 * @buf - the string that holds the function notrace text.
4408 * @len - the length of the string.
4409 * @reset - non zero to reset all filters before applying this filter.
4410 *
4411 * Notrace Filters denote which functions should not be enabled when tracing
4412 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4413 * for tracing.
4414 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004415int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004416 int len, int reset)
4417{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004418 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004419 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004420}
4421EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4422/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004423 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004424 * @buf - the string that holds the function filter text.
4425 * @len - the length of the string.
4426 * @reset - non zero to reset all filters before applying this filter.
4427 *
4428 * Filters denote which functions should be enabled when tracing is enabled.
4429 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4430 */
4431void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4432{
4433 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4434}
4435EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4436
4437/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004438 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004439 * @buf - the string that holds the function notrace text.
4440 * @len - the length of the string.
4441 * @reset - non zero to reset all filters before applying this filter.
4442 *
4443 * Notrace Filters denote which functions should not be enabled when tracing
4444 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4445 * for tracing.
4446 */
4447void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004448{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004449 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004450}
Steven Rostedt936e0742011-05-05 22:54:01 -04004451EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004452
Steven Rostedt2af15d62009-05-28 13:37:24 -04004453/*
4454 * command line interface to allow users to set filters on boot up.
4455 */
4456#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4457static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4458static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4459
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004460/* Used by function selftest to not test if filter is set */
4461bool ftrace_filter_param __initdata;
4462
Steven Rostedt2af15d62009-05-28 13:37:24 -04004463static int __init set_ftrace_notrace(char *str)
4464{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004465 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004466 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004467 return 1;
4468}
4469__setup("ftrace_notrace=", set_ftrace_notrace);
4470
4471static int __init set_ftrace_filter(char *str)
4472{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004473 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004474 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004475 return 1;
4476}
4477__setup("ftrace_filter=", set_ftrace_filter);
4478
Stefan Assmann369bc182009-10-12 22:17:21 +02004479#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004480static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004481static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004482static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004483
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004484static unsigned long save_global_trampoline;
4485static unsigned long save_global_flags;
4486
Stefan Assmann369bc182009-10-12 22:17:21 +02004487static int __init set_graph_function(char *str)
4488{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004489 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004490 return 1;
4491}
4492__setup("ftrace_graph_filter=", set_graph_function);
4493
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004494static int __init set_graph_notrace_function(char *str)
4495{
4496 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4497 return 1;
4498}
4499__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4500
Todd Brandt65a50c652017-03-02 16:12:15 -08004501static int __init set_graph_max_depth_function(char *str)
4502{
4503 if (!str)
4504 return 0;
4505 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4506 return 1;
4507}
4508__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4509
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004510static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004511{
4512 int ret;
4513 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004514 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004515
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004516 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4517 if (WARN_ON(!hash))
4518 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004519
4520 while (buf) {
4521 func = strsep(&buf, ",");
4522 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004523 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004524 if (ret)
4525 printk(KERN_DEBUG "ftrace: function %s not "
4526 "traceable\n", func);
4527 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004528
4529 if (enable)
4530 ftrace_graph_hash = hash;
4531 else
4532 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02004533}
4534#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4535
Steven Rostedt2a85a372011-12-19 21:57:44 -05004536void __init
4537ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004538{
4539 char *func;
4540
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004541 ftrace_ops_init(ops);
4542
Steven Rostedt2af15d62009-05-28 13:37:24 -04004543 while (buf) {
4544 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004545 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004546 }
4547}
4548
4549static void __init set_ftrace_early_filters(void)
4550{
4551 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004552 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004553 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004554 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004555#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4556 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004557 set_ftrace_early_graph(ftrace_graph_buf, 1);
4558 if (ftrace_graph_notrace_buf[0])
4559 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004560#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004561}
4562
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004563int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004564{
4565 struct seq_file *m = (struct seq_file *)file->private_data;
4566 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004567 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004568 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004569 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004570 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004571
Steven Rostedt5072c592008-05-12 21:20:43 +02004572 if (file->f_mode & FMODE_READ) {
4573 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004574 seq_release(inode, file);
4575 } else
4576 iter = file->private_data;
4577
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004578 parser = &iter->parser;
4579 if (trace_parser_loaded(parser)) {
4580 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004581 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004582 }
4583
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004584 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004585
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004586 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004587
Steven Rostedt058e2972011-04-29 22:35:33 -04004588 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004589 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4590
4591 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004592 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004593 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004594 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004595
Steven Rostedt058e2972011-04-29 22:35:33 -04004596 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)e16b35d2017-04-04 14:46:56 -04004597 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
4598 iter->hash, filter_hash);
Steven Rostedt058e2972011-04-29 22:35:33 -04004599 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04004600 } else {
4601 /* For read only, the hash is the ops hash */
4602 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04004603 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004604
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004605 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004606 free_ftrace_hash(iter->hash);
4607 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004608
Steven Rostedt5072c592008-05-12 21:20:43 +02004609 return 0;
4610}
4611
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004612static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004613 .open = ftrace_avail_open,
4614 .read = seq_read,
4615 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004616 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004617};
4618
Steven Rostedt647bcd02011-05-03 14:39:21 -04004619static const struct file_operations ftrace_enabled_fops = {
4620 .open = ftrace_enabled_open,
4621 .read = seq_read,
4622 .llseek = seq_lseek,
4623 .release = seq_release_private,
4624};
4625
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004626static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004627 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004628 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004629 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004630 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004631 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004632};
4633
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004634static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004635 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004636 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004637 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004638 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004639 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004640};
4641
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004642#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4643
4644static DEFINE_MUTEX(graph_lock);
4645
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004646struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
4647struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
4648
4649enum graph_filter_type {
4650 GRAPH_FILTER_NOTRACE = 0,
4651 GRAPH_FILTER_FUNCTION,
4652};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004653
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004654#define FTRACE_GRAPH_EMPTY ((void *)1)
4655
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004656struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004657 struct ftrace_hash *hash;
4658 struct ftrace_func_entry *entry;
4659 int idx; /* for hash table iteration */
4660 enum graph_filter_type type;
4661 struct ftrace_hash *new_hash;
4662 const struct seq_operations *seq_ops;
4663 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004664};
4665
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004666static void *
Li Zefan85951842009-06-24 09:54:00 +08004667__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004668{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004669 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004670 struct ftrace_func_entry *entry = fgd->entry;
4671 struct hlist_head *head;
4672 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004673
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004674 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004675 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004676
4677 if (entry) {
4678 hlist_for_each_entry_continue(entry, hlist) {
4679 fgd->entry = entry;
4680 return entry;
4681 }
4682
4683 idx++;
4684 }
4685
4686 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
4687 head = &fgd->hash->buckets[i];
4688 hlist_for_each_entry(entry, head, hlist) {
4689 fgd->entry = entry;
4690 fgd->idx = i;
4691 return entry;
4692 }
4693 }
4694 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08004695}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004696
Li Zefan85951842009-06-24 09:54:00 +08004697static void *
4698g_next(struct seq_file *m, void *v, loff_t *pos)
4699{
4700 (*pos)++;
4701 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004702}
4703
4704static void *g_start(struct seq_file *m, loff_t *pos)
4705{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004706 struct ftrace_graph_data *fgd = m->private;
4707
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004708 mutex_lock(&graph_lock);
4709
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004710 if (fgd->type == GRAPH_FILTER_FUNCTION)
4711 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4712 lockdep_is_held(&graph_lock));
4713 else
4714 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4715 lockdep_is_held(&graph_lock));
4716
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004717 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004718 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004719 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004720
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004721 fgd->idx = 0;
4722 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08004723 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004724}
4725
4726static void g_stop(struct seq_file *m, void *p)
4727{
4728 mutex_unlock(&graph_lock);
4729}
4730
4731static int g_show(struct seq_file *m, void *v)
4732{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004733 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004734
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004735 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004736 return 0;
4737
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004738 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004739 struct ftrace_graph_data *fgd = m->private;
4740
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004741 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004742 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004743 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004744 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004745 return 0;
4746 }
4747
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004748 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004749
4750 return 0;
4751}
4752
James Morris88e9d342009-09-22 16:43:43 -07004753static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004754 .start = g_start,
4755 .next = g_next,
4756 .stop = g_stop,
4757 .show = g_show,
4758};
4759
4760static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004761__ftrace_graph_open(struct inode *inode, struct file *file,
4762 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004763{
4764 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004765 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004766
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004767 if (file->f_mode & FMODE_WRITE) {
4768 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
4769
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004770 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
4771 return -ENOMEM;
4772
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004773 if (file->f_flags & O_TRUNC)
4774 new_hash = alloc_ftrace_hash(size_bits);
4775 else
4776 new_hash = alloc_and_copy_ftrace_hash(size_bits,
4777 fgd->hash);
4778 if (!new_hash) {
4779 ret = -ENOMEM;
4780 goto out;
4781 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004782 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004783
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004784 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004785 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004786 if (!ret) {
4787 struct seq_file *m = file->private_data;
4788 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004789 } else {
4790 /* Failed */
4791 free_ftrace_hash(new_hash);
4792 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004793 }
4794 } else
4795 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004796
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004797out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004798 if (ret < 0 && file->f_mode & FMODE_WRITE)
4799 trace_parser_put(&fgd->parser);
4800
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004801 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004802
4803 /*
4804 * All uses of fgd->hash must be taken with the graph_lock
4805 * held. The graph_lock is going to be released, so force
4806 * fgd->hash to be reinitialized when it is taken again.
4807 */
4808 fgd->hash = NULL;
4809
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004810 return ret;
4811}
4812
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004813static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004814ftrace_graph_open(struct inode *inode, struct file *file)
4815{
4816 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004817 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004818
4819 if (unlikely(ftrace_disabled))
4820 return -ENODEV;
4821
4822 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4823 if (fgd == NULL)
4824 return -ENOMEM;
4825
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004826 mutex_lock(&graph_lock);
4827
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004828 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4829 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004830 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004831 fgd->seq_ops = &ftrace_graph_seq_ops;
4832
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004833 ret = __ftrace_graph_open(inode, file, fgd);
4834 if (ret < 0)
4835 kfree(fgd);
4836
4837 mutex_unlock(&graph_lock);
4838 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004839}
4840
4841static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004842ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4843{
4844 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004845 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004846
4847 if (unlikely(ftrace_disabled))
4848 return -ENODEV;
4849
4850 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4851 if (fgd == NULL)
4852 return -ENOMEM;
4853
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004854 mutex_lock(&graph_lock);
4855
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004856 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4857 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004858 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004859 fgd->seq_ops = &ftrace_graph_seq_ops;
4860
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004861 ret = __ftrace_graph_open(inode, file, fgd);
4862 if (ret < 0)
4863 kfree(fgd);
4864
4865 mutex_unlock(&graph_lock);
4866 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004867}
4868
4869static int
Li Zefan87827112009-07-23 11:29:11 +08004870ftrace_graph_release(struct inode *inode, struct file *file)
4871{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004872 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004873 struct ftrace_hash *old_hash, *new_hash;
4874 struct trace_parser *parser;
4875 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004876
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004877 if (file->f_mode & FMODE_READ) {
4878 struct seq_file *m = file->private_data;
4879
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004880 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08004881 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004882 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004883 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004884 }
4885
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004886
4887 if (file->f_mode & FMODE_WRITE) {
4888
4889 parser = &fgd->parser;
4890
4891 if (trace_parser_loaded((parser))) {
4892 parser->buffer[parser->idx] = 0;
4893 ret = ftrace_graph_set_hash(fgd->new_hash,
4894 parser->buffer);
4895 }
4896
4897 trace_parser_put(parser);
4898
4899 new_hash = __ftrace_hash_move(fgd->new_hash);
4900 if (!new_hash) {
4901 ret = -ENOMEM;
4902 goto out;
4903 }
4904
4905 mutex_lock(&graph_lock);
4906
4907 if (fgd->type == GRAPH_FILTER_FUNCTION) {
4908 old_hash = rcu_dereference_protected(ftrace_graph_hash,
4909 lockdep_is_held(&graph_lock));
4910 rcu_assign_pointer(ftrace_graph_hash, new_hash);
4911 } else {
4912 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4913 lockdep_is_held(&graph_lock));
4914 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
4915 }
4916
4917 mutex_unlock(&graph_lock);
4918
4919 /* Wait till all users are no longer using the old hash */
4920 synchronize_sched();
4921
4922 free_ftrace_hash(old_hash);
4923 }
4924
4925 out:
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004926 kfree(fgd->new_hash);
4927 kfree(fgd);
4928
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004929 return ret;
Li Zefan87827112009-07-23 11:29:11 +08004930}
4931
4932static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004933ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004934{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004935 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004936 struct dyn_ftrace *rec;
4937 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004938 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004939 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004940 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004941
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004942 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004943 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4944 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004945
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004946 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004947
Steven Rostedt52baf112009-02-14 01:15:39 -05004948 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004949
4950 if (unlikely(ftrace_disabled)) {
4951 mutex_unlock(&ftrace_lock);
4952 return -ENODEV;
4953 }
4954
Steven Rostedt265c8312009-02-13 12:43:56 -05004955 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004956
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004957 if (rec->flags & FTRACE_FL_DISABLED)
4958 continue;
4959
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004960 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004961 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004962
4963 if (!not) {
4964 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004965
4966 if (entry)
4967 continue;
4968 if (add_hash_entry(hash, rec->ip) < 0)
4969 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004970 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004971 if (entry) {
4972 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004973 fail = 0;
4974 }
4975 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004976 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004977 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004978out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004979 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004980
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004981 if (fail)
4982 return -EINVAL;
4983
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004984 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004985}
4986
4987static ssize_t
4988ftrace_graph_write(struct file *file, const char __user *ubuf,
4989 size_t cnt, loff_t *ppos)
4990{
Namhyung Kim6a101082013-10-14 17:24:25 +09004991 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004992 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004993 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004994
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004995 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004996 return 0;
4997
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05004998 /* Read mode uses seq functions */
4999 if (file->f_mode & FMODE_READ) {
5000 struct seq_file *m = file->private_data;
5001 fgd = m->private;
5002 }
5003
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005004 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005005
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005006 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005007
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005008 if (read >= 0 && trace_parser_loaded(parser) &&
5009 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005010
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005011 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005012 parser->buffer);
5013 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005014 }
5015
Namhyung Kim6a101082013-10-14 17:24:25 +09005016 if (!ret)
5017 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005018
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005019 return ret;
5020}
5021
5022static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005023 .open = ftrace_graph_open,
5024 .read = seq_read,
5025 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005026 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005027 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005028};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005029
5030static const struct file_operations ftrace_graph_notrace_fops = {
5031 .open = ftrace_graph_notrace_open,
5032 .read = seq_read,
5033 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005034 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005035 .release = ftrace_graph_release,
5036};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005037#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5038
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005039void ftrace_create_filter_files(struct ftrace_ops *ops,
5040 struct dentry *parent)
5041{
5042
5043 trace_create_file("set_ftrace_filter", 0644, parent,
5044 ops, &ftrace_filter_fops);
5045
5046 trace_create_file("set_ftrace_notrace", 0644, parent,
5047 ops, &ftrace_notrace_fops);
5048}
5049
5050/*
5051 * The name "destroy_filter_files" is really a misnomer. Although
5052 * in the future, it may actualy delete the files, but this is
5053 * really intended to make sure the ops passed in are disabled
5054 * and that when this function returns, the caller is free to
5055 * free the ops.
5056 *
5057 * The "destroy" name is only to match the "create" name that this
5058 * should be paired with.
5059 */
5060void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5061{
5062 mutex_lock(&ftrace_lock);
5063 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5064 ftrace_shutdown(ops, 0);
5065 ops->flags |= FTRACE_OPS_FL_DELETED;
5066 mutex_unlock(&ftrace_lock);
5067}
5068
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005069static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005070{
Steven Rostedt5072c592008-05-12 21:20:43 +02005071
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005072 trace_create_file("available_filter_functions", 0444,
5073 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005074
Steven Rostedt647bcd02011-05-03 14:39:21 -04005075 trace_create_file("enabled_functions", 0444,
5076 d_tracer, NULL, &ftrace_enabled_fops);
5077
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005078 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005079
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005080#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005081 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005082 NULL,
5083 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005084 trace_create_file("set_graph_notrace", 0444, d_tracer,
5085 NULL,
5086 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005087#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5088
Steven Rostedt5072c592008-05-12 21:20:43 +02005089 return 0;
5090}
5091
Steven Rostedt9fd49322012-04-24 22:32:06 -04005092static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005093{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005094 const unsigned long *ipa = a;
5095 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005096
Steven Rostedt9fd49322012-04-24 22:32:06 -04005097 if (*ipa > *ipb)
5098 return 1;
5099 if (*ipa < *ipb)
5100 return -1;
5101 return 0;
5102}
5103
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005104static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005105 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005106 unsigned long *end)
5107{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005108 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005109 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005110 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005111 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005112 unsigned long *p;
5113 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005114 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005115 int ret = -ENOMEM;
5116
5117 count = end - start;
5118
5119 if (!count)
5120 return 0;
5121
Steven Rostedt9fd49322012-04-24 22:32:06 -04005122 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005123 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005124
Steven Rostedt706c81f2012-04-24 23:45:26 -04005125 start_pg = ftrace_allocate_pages(count);
5126 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005127 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005128
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005129 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005130
Steven Rostedt320823092011-12-16 14:42:37 -05005131 /*
5132 * Core and each module needs their own pages, as
5133 * modules will free them when they are removed.
5134 * Force a new page to be allocated for modules.
5135 */
Steven Rostedta7900872011-12-16 16:23:44 -05005136 if (!mod) {
5137 WARN_ON(ftrace_pages || ftrace_pages_start);
5138 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005139 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005140 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005141 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005142 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005143
Steven Rostedta7900872011-12-16 16:23:44 -05005144 if (WARN_ON(ftrace_pages->next)) {
5145 /* Hmm, we have free pages? */
5146 while (ftrace_pages->next)
5147 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005148 }
Steven Rostedta7900872011-12-16 16:23:44 -05005149
Steven Rostedt706c81f2012-04-24 23:45:26 -04005150 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005151 }
5152
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005153 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005154 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005155 while (p < end) {
5156 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005157 /*
5158 * Some architecture linkers will pad between
5159 * the different mcount_loc sections of different
5160 * object files to satisfy alignments.
5161 * Skip any NULL pointers.
5162 */
5163 if (!addr)
5164 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005165
5166 if (pg->index == pg->size) {
5167 /* We should have allocated enough */
5168 if (WARN_ON(!pg->next))
5169 break;
5170 pg = pg->next;
5171 }
5172
5173 rec = &pg->records[pg->index++];
5174 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005175 }
5176
Steven Rostedt706c81f2012-04-24 23:45:26 -04005177 /* We should have used all pages */
5178 WARN_ON(pg->next);
5179
5180 /* Assign the last page to ftrace_pages */
5181 ftrace_pages = pg;
5182
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005183 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005184 * We only need to disable interrupts on start up
5185 * because we are modifying code that an interrupt
5186 * may execute, and the modification is not atomic.
5187 * But for modules, nothing runs the code we modify
5188 * until we are finished with it, and there's no
5189 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005190 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005191 if (!mod)
5192 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005193 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005194 if (!mod)
5195 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005196 ret = 0;
5197 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005198 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005199
Steven Rostedta7900872011-12-16 16:23:44 -05005200 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005201}
5202
Steven Rostedt93eb6772009-04-15 13:24:06 -04005203#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005204
5205#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5206
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005207static int referenced_filters(struct dyn_ftrace *rec)
5208{
5209 struct ftrace_ops *ops;
5210 int cnt = 0;
5211
5212 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5213 if (ops_references_rec(ops, rec))
5214 cnt++;
5215 }
5216
5217 return cnt;
5218}
5219
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005220void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005221{
5222 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005223 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005224 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005225 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005226
Steven Rostedt93eb6772009-04-15 13:24:06 -04005227 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005228
5229 if (ftrace_disabled)
5230 goto out_unlock;
5231
Steven Rostedt320823092011-12-16 14:42:37 -05005232 /*
5233 * Each module has its own ftrace_pages, remove
5234 * them from the list.
5235 */
5236 last_pg = &ftrace_pages_start;
5237 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5238 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005239 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005240 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005241 * As core pages are first, the first
5242 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005243 */
Steven Rostedt320823092011-12-16 14:42:37 -05005244 if (WARN_ON(pg == ftrace_pages_start))
5245 goto out_unlock;
5246
5247 /* Check if we are deleting the last page */
5248 if (pg == ftrace_pages)
5249 ftrace_pages = next_to_ftrace_page(last_pg);
5250
5251 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05005252 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5253 free_pages((unsigned long)pg->records, order);
5254 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05005255 } else
5256 last_pg = &pg->next;
5257 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005258 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005259 mutex_unlock(&ftrace_lock);
5260}
5261
Jessica Yu7dcd1822016-02-16 17:32:33 -05005262void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005263{
5264 struct dyn_ftrace *rec;
5265 struct ftrace_page *pg;
5266
5267 mutex_lock(&ftrace_lock);
5268
5269 if (ftrace_disabled)
5270 goto out_unlock;
5271
5272 /*
5273 * If the tracing is enabled, go ahead and enable the record.
5274 *
5275 * The reason not to enable the record immediatelly is the
5276 * inherent check of ftrace_make_nop/ftrace_make_call for
5277 * correct previous instructions. Making first the NOP
5278 * conversion puts the module to the correct state, thus
5279 * passing the ftrace_make_call check.
5280 *
5281 * We also delay this to after the module code already set the
5282 * text to read-only, as we now need to set it back to read-write
5283 * so that we can modify the text.
5284 */
5285 if (ftrace_start_up)
5286 ftrace_arch_code_modify_prepare();
5287
5288 do_for_each_ftrace_rec(pg, rec) {
5289 int cnt;
5290 /*
5291 * do_for_each_ftrace_rec() is a double loop.
5292 * module text shares the pg. If a record is
5293 * not part of this module, then skip this pg,
5294 * which the "break" will do.
5295 */
5296 if (!within_module_core(rec->ip, mod))
5297 break;
5298
5299 cnt = 0;
5300
5301 /*
5302 * When adding a module, we need to check if tracers are
5303 * currently enabled and if they are, and can trace this record,
5304 * we need to enable the module functions as well as update the
5305 * reference counts for those function records.
5306 */
5307 if (ftrace_start_up)
5308 cnt += referenced_filters(rec);
5309
5310 /* This clears FTRACE_FL_DISABLED */
5311 rec->flags = cnt;
5312
5313 if (ftrace_start_up && cnt) {
5314 int failed = __ftrace_replace_code(rec, 1);
5315 if (failed) {
5316 ftrace_bug(failed, rec);
5317 goto out_loop;
5318 }
5319 }
5320
5321 } while_for_each_ftrace_rec();
5322
5323 out_loop:
5324 if (ftrace_start_up)
5325 ftrace_arch_code_modify_post_process();
5326
5327 out_unlock:
5328 mutex_unlock(&ftrace_lock);
5329}
5330
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005331void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005332{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005333 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005334 return;
5335
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005336 ftrace_process_locs(mod, mod->ftrace_callsites,
5337 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005338}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005339#endif /* CONFIG_MODULES */
5340
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -04005341void __init ftrace_free_init_mem(void)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005342{
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -04005343 unsigned long start = (unsigned long)(&__init_begin);
5344 unsigned long end = (unsigned long)(&__init_end);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005345 struct ftrace_page **last_pg = &ftrace_pages_start;
5346 struct ftrace_page *pg;
5347 struct dyn_ftrace *rec;
5348 struct dyn_ftrace key;
5349 int order;
5350
5351 key.ip = start;
5352 key.flags = end; /* overload flags, as it is unsigned long */
5353
5354 mutex_lock(&ftrace_lock);
5355
5356 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
5357 if (end < pg->records[0].ip ||
5358 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
5359 continue;
5360 again:
5361 rec = bsearch(&key, pg->records, pg->index,
5362 sizeof(struct dyn_ftrace),
5363 ftrace_cmp_recs);
5364 if (!rec)
5365 continue;
5366 pg->index--;
5367 if (!pg->index) {
5368 *last_pg = pg->next;
5369 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5370 free_pages((unsigned long)pg->records, order);
5371 kfree(pg);
5372 pg = container_of(last_pg, struct ftrace_page, next);
5373 if (!(*last_pg))
5374 ftrace_pages = pg;
5375 continue;
5376 }
5377 memmove(rec, rec + 1,
5378 (pg->index - (rec - pg->records)) * sizeof(*rec));
5379 /* More than one function may be in this block */
5380 goto again;
5381 }
5382 mutex_unlock(&ftrace_lock);
5383}
5384
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005385void __init ftrace_init(void)
5386{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005387 extern unsigned long __start_mcount_loc[];
5388 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005389 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005390 int ret;
5391
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005392 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005393 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005394 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01005395 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005396 goto failed;
5397
5398 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005399 if (!count) {
5400 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005401 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005402 }
5403
5404 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5405 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005406
5407 last_ftrace_enabled = ftrace_enabled = 1;
5408
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005409 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005410 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005411 __stop_mcount_loc);
5412
Steven Rostedt2af15d62009-05-28 13:37:24 -04005413 set_ftrace_early_filters();
5414
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005415 return;
5416 failed:
5417 ftrace_disabled = 1;
5418}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005419
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005420/* Do nothing if arch does not support this */
5421void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5422{
5423}
5424
5425static void ftrace_update_trampoline(struct ftrace_ops *ops)
5426{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005427 arch_ftrace_update_trampoline(ops);
5428}
5429
Steven Rostedt3d083392008-05-12 21:20:42 +02005430#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005431
Steven Rostedt2b499382011-05-03 22:49:52 -04005432static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005433 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005434 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5435 FTRACE_OPS_FL_INITIALIZED |
5436 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005437};
5438
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005439static int __init ftrace_nodyn_init(void)
5440{
5441 ftrace_enabled = 1;
5442 return 0;
5443}
Steven Rostedt6f415672012-10-05 12:13:07 -04005444core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005445
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005446static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005447static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005448static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005449/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005450# define ftrace_startup(ops, command) \
5451 ({ \
5452 int ___ret = __register_ftrace_function(ops); \
5453 if (!___ret) \
5454 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5455 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005456 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005457# define ftrace_shutdown(ops, command) \
5458 ({ \
5459 int ___ret = __unregister_ftrace_function(ops); \
5460 if (!___ret) \
5461 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5462 ___ret; \
5463 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005464
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005465# define ftrace_startup_sysctl() do { } while (0)
5466# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005467
5468static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005469ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005470{
5471 return 1;
5472}
5473
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005474static void ftrace_update_trampoline(struct ftrace_ops *ops)
5475{
5476}
5477
Steven Rostedt3d083392008-05-12 21:20:42 +02005478#endif /* CONFIG_DYNAMIC_FTRACE */
5479
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005480__init void ftrace_init_global_array_ops(struct trace_array *tr)
5481{
5482 tr->ops = &global_ops;
5483 tr->ops->private = tr;
5484}
5485
5486void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5487{
5488 /* If we filter on pids, update to use the pid function */
5489 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5490 if (WARN_ON(tr->ops->func != ftrace_stub))
5491 printk("ftrace ops had %pS for function\n",
5492 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005493 }
5494 tr->ops->func = func;
5495 tr->ops->private = tr;
5496}
5497
5498void ftrace_reset_array_ops(struct trace_array *tr)
5499{
5500 tr->ops->func = ftrace_stub;
5501}
5502
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005503static inline void
5504__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005505 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005506{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005507 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005508 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005509
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005510 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5511 if (bit < 0)
5512 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005513
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005514 /*
5515 * Some of the ops may be dynamically allocated,
5516 * they must be freed after a synchronize_sched().
5517 */
5518 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005519
Steven Rostedt0a016402012-11-02 17:03:03 -04005520 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005521 /*
5522 * Check the following for each ops before calling their func:
5523 * if RCU flag is set, then rcu_is_watching() must be true
5524 * if PER_CPU is set, then ftrace_function_local_disable()
5525 * must be false
5526 * Otherwise test if the ip matches the ops filter
5527 *
5528 * If any of the above fails then the op->func() is not executed.
5529 */
5530 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5531 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5532 !ftrace_function_local_disabled(op)) &&
5533 ftrace_ops_test(op, ip, regs)) {
5534
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005535 if (FTRACE_WARN_ON(!op->func)) {
5536 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005537 goto out;
5538 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005539 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005540 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005541 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005542out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005543 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005544 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005545}
5546
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005547/*
5548 * Some archs only support passing ip and parent_ip. Even though
5549 * the list function ignores the op parameter, we do not want any
5550 * C side effects, where a function is called without the caller
5551 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005552 * Archs are to support both the regs and ftrace_ops at the same time.
5553 * If they support ftrace_ops, it is assumed they support regs.
5554 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005555 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5556 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005557 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08005558 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005559 */
5560#if ARCH_SUPPORTS_FTRACE_OPS
5561static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005562 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005563{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005564 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005565}
5566#else
5567static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5568{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005569 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005570}
5571#endif
5572
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005573/*
5574 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005575 * recursion, needs RCU protection and/or requires per cpu handling, then
5576 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005577 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005578static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005579 struct ftrace_ops *op, struct pt_regs *regs)
5580{
5581 int bit;
5582
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005583 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5584 return;
5585
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005586 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5587 if (bit < 0)
5588 return;
5589
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005590 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005591
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005592 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5593 !ftrace_function_local_disabled(op)) {
5594 op->func(ip, parent_ip, op, regs);
5595 }
5596
5597 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005598 trace_clear_recursion(bit);
5599}
5600
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005601/**
5602 * ftrace_ops_get_func - get the function a trampoline should call
5603 * @ops: the ops to get the function for
5604 *
5605 * Normally the mcount trampoline will call the ops->func, but there
5606 * are times that it should not. For example, if the ops does not
5607 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08005608 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005609 *
5610 * Returns the function that the trampoline should call for @ops.
5611 */
5612ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5613{
5614 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005615 * If the function does not handle recursion, needs to be RCU safe,
5616 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005617 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005618 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5619 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5620 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005621
5622 return ops->func;
5623}
5624
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005625static void
5626ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5627 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05005628{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005629 struct trace_array *tr = data;
5630 struct trace_pid_list *pid_list;
5631
5632 pid_list = rcu_dereference_sched(tr->function_pids);
5633
5634 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5635 trace_ignore_this_task(pid_list, next));
5636}
5637
Namhyung Kim1e104862017-04-17 11:44:28 +09005638static void
5639ftrace_pid_follow_sched_process_fork(void *data,
5640 struct task_struct *self,
5641 struct task_struct *task)
5642{
5643 struct trace_pid_list *pid_list;
5644 struct trace_array *tr = data;
5645
5646 pid_list = rcu_dereference_sched(tr->function_pids);
5647 trace_filter_add_remove_task(pid_list, self, task);
5648}
5649
5650static void
5651ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
5652{
5653 struct trace_pid_list *pid_list;
5654 struct trace_array *tr = data;
5655
5656 pid_list = rcu_dereference_sched(tr->function_pids);
5657 trace_filter_add_remove_task(pid_list, NULL, task);
5658}
5659
5660void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
5661{
5662 if (enable) {
5663 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
5664 tr);
5665 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
5666 tr);
5667 } else {
5668 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
5669 tr);
5670 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
5671 tr);
5672 }
5673}
5674
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005675static void clear_ftrace_pids(struct trace_array *tr)
5676{
5677 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05005678 int cpu;
5679
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005680 pid_list = rcu_dereference_protected(tr->function_pids,
5681 lockdep_is_held(&ftrace_lock));
5682 if (!pid_list)
5683 return;
5684
5685 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5686
5687 for_each_possible_cpu(cpu)
5688 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5689
5690 rcu_assign_pointer(tr->function_pids, NULL);
5691
5692 /* Wait till all users are no longer using pid filtering */
5693 synchronize_sched();
5694
5695 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05005696}
5697
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005698static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05005699{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005700 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005701 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005702
5703 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005704 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005705
5706 mutex_unlock(&ftrace_lock);
5707}
5708
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005709/* Greater than any max PID */
5710#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5711
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005712static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005713 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005714{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005715 struct trace_pid_list *pid_list;
5716 struct trace_array *tr = m->private;
5717
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005718 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005719 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005720
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005721 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005722
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005723 if (!pid_list)
5724 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5725
5726 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005727}
5728
5729static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5730{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005731 struct trace_array *tr = m->private;
5732 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5733
5734 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005735 return NULL;
5736
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005737 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005738}
5739
5740static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005741 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005742{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005743 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005744 mutex_unlock(&ftrace_lock);
5745}
5746
5747static int fpid_show(struct seq_file *m, void *v)
5748{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005749 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005750 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005751 return 0;
5752 }
5753
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005754 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005755}
5756
5757static const struct seq_operations ftrace_pid_sops = {
5758 .start = fpid_start,
5759 .next = fpid_next,
5760 .stop = fpid_stop,
5761 .show = fpid_show,
5762};
5763
5764static int
5765ftrace_pid_open(struct inode *inode, struct file *file)
5766{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005767 struct trace_array *tr = inode->i_private;
5768 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005769 int ret = 0;
5770
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005771 if (trace_array_get(tr) < 0)
5772 return -ENODEV;
5773
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005774 if ((file->f_mode & FMODE_WRITE) &&
5775 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005776 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005777
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005778 ret = seq_open(file, &ftrace_pid_sops);
5779 if (ret < 0) {
5780 trace_array_put(tr);
5781 } else {
5782 m = file->private_data;
5783 /* copy tr over to seq ops */
5784 m->private = tr;
5785 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005786
5787 return ret;
5788}
5789
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005790static void ignore_task_cpu(void *data)
5791{
5792 struct trace_array *tr = data;
5793 struct trace_pid_list *pid_list;
5794
5795 /*
5796 * This function is called by on_each_cpu() while the
5797 * event_mutex is held.
5798 */
5799 pid_list = rcu_dereference_protected(tr->function_pids,
5800 mutex_is_locked(&ftrace_lock));
5801
5802 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5803 trace_ignore_this_task(pid_list, current));
5804}
5805
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005806static ssize_t
5807ftrace_pid_write(struct file *filp, const char __user *ubuf,
5808 size_t cnt, loff_t *ppos)
5809{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005810 struct seq_file *m = filp->private_data;
5811 struct trace_array *tr = m->private;
5812 struct trace_pid_list *filtered_pids = NULL;
5813 struct trace_pid_list *pid_list;
5814 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005815
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005816 if (!cnt)
5817 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005818
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005819 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005820
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005821 filtered_pids = rcu_dereference_protected(tr->function_pids,
5822 lockdep_is_held(&ftrace_lock));
5823
5824 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5825 if (ret < 0)
5826 goto out;
5827
5828 rcu_assign_pointer(tr->function_pids, pid_list);
5829
5830 if (filtered_pids) {
5831 synchronize_sched();
5832 trace_free_pid_list(filtered_pids);
5833 } else if (pid_list) {
5834 /* Register a probe to set whether to ignore the tracing of a task */
5835 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5836 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005837
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005838 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005839 * Ignoring of pids is done at task switch. But we have to
5840 * check for those tasks that are currently running.
5841 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005842 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005843 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005844
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005845 ftrace_update_pid_func();
5846 ftrace_startup_all(0);
5847 out:
5848 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005849
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005850 if (ret > 0)
5851 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05005852
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005853 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005854}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005855
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005856static int
5857ftrace_pid_release(struct inode *inode, struct file *file)
5858{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005859 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005860
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005861 trace_array_put(tr);
5862
5863 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005864}
5865
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005866static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005867 .open = ftrace_pid_open,
5868 .write = ftrace_pid_write,
5869 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005870 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005871 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005872};
5873
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005874void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005875{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005876 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005877 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005878}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005879
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04005880void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5881 struct dentry *d_tracer)
5882{
5883 /* Only the top level directory has the dyn_tracefs and profile */
5884 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5885
5886 ftrace_init_dyn_tracefs(d_tracer);
5887 ftrace_profile_tracefs(d_tracer);
5888}
5889
Steven Rostedt3d083392008-05-12 21:20:42 +02005890/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005891 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005892 *
5893 * This function should be used by panic code. It stops ftrace
5894 * but in a not so nice way. If you need to simply kill ftrace
5895 * from a non-atomic section, use ftrace_kill.
5896 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005897void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005898{
5899 ftrace_disabled = 1;
5900 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005901 clear_ftrace_function();
5902}
5903
5904/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005905 * Test if ftrace is dead or not.
5906 */
5907int ftrace_is_dead(void)
5908{
5909 return ftrace_disabled;
5910}
5911
5912/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005913 * register_ftrace_function - register a function for profiling
5914 * @ops - ops structure that holds the function for profiling.
5915 *
5916 * Register a function to be called by all functions in the
5917 * kernel.
5918 *
5919 * Note: @ops->func and all the functions it calls must be labeled
5920 * with "notrace", otherwise it will go into a
5921 * recursive loop.
5922 */
5923int register_ftrace_function(struct ftrace_ops *ops)
5924{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005925 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005926
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005927 ftrace_ops_init(ops);
5928
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005929 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005930
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005931 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005932
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005933 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005934
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005935 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005936}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005937EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005938
5939/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005940 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005941 * @ops - ops structure that holds the function to unregister
5942 *
5943 * Unregister a function that was added to be called by ftrace profiling.
5944 */
5945int unregister_ftrace_function(struct ftrace_ops *ops)
5946{
5947 int ret;
5948
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005949 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005950 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005951 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005952
5953 return ret;
5954}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005955EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005956
Ingo Molnare309b412008-05-12 21:20:51 +02005957int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005958ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005959 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005960 loff_t *ppos)
5961{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005962 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005963
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005964 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005965
Steven Rostedt45a4a232011-04-21 23:16:46 -04005966 if (unlikely(ftrace_disabled))
5967 goto out;
5968
5969 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005970
Li Zefana32c7762009-06-26 16:55:51 +08005971 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005972 goto out;
5973
Li Zefana32c7762009-06-26 16:55:51 +08005974 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005975
5976 if (ftrace_enabled) {
5977
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005978 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005979 if (ftrace_ops_list != &ftrace_list_end)
5980 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005981
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05005982 ftrace_startup_sysctl();
5983
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005984 } else {
5985 /* stopping ftrace calls (just send to ftrace_stub) */
5986 ftrace_trace_function = ftrace_stub;
5987
5988 ftrace_shutdown_sysctl();
5989 }
5990
5991 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005992 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005993 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005994}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005995
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005996#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005997
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005998static struct ftrace_ops graph_ops = {
5999 .func = ftrace_stub,
6000 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6001 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006002 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006003 FTRACE_OPS_FL_STUB,
6004#ifdef FTRACE_GRAPH_TRAMP_ADDR
6005 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05006006 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006007#endif
6008 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6009};
6010
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006011void ftrace_graph_sleep_time_control(bool enable)
6012{
6013 fgraph_sleep_time = enable;
6014}
6015
6016void ftrace_graph_graph_time_control(bool enable)
6017{
6018 fgraph_graph_time = enable;
6019}
6020
Steven Rostedte49dc192008-12-02 23:50:05 -05006021int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6022{
6023 return 0;
6024}
6025
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006026/* The callbacks that hook a function */
6027trace_func_graph_ret_t ftrace_graph_return =
6028 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006029trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006030static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006031
6032/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6033static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6034{
6035 int i;
6036 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006037 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6038 struct task_struct *g, *t;
6039
6040 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6041 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
6042 * sizeof(struct ftrace_ret_stack),
6043 GFP_KERNEL);
6044 if (!ret_stack_list[i]) {
6045 start = 0;
6046 end = i;
6047 ret = -ENOMEM;
6048 goto free;
6049 }
6050 }
6051
Soumya PN6112a302016-05-17 21:31:14 +05306052 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006053 do_each_thread(g, t) {
6054 if (start == end) {
6055 ret = -EAGAIN;
6056 goto unlock;
6057 }
6058
6059 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01006060 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006061 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04006062 t->curr_ret_stack = -1;
6063 /* Make sure the tasks see the -1 first: */
6064 smp_wmb();
6065 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006066 }
6067 } while_each_thread(g, t);
6068
6069unlock:
Soumya PN6112a302016-05-17 21:31:14 +05306070 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006071free:
6072 for (i = start; i < end; i++)
6073 kfree(ret_stack_list[i]);
6074 return ret;
6075}
6076
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006077static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02006078ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04006079 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006080{
6081 unsigned long long timestamp;
6082 int index;
6083
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006084 /*
6085 * Does the user want to count the time a function was asleep.
6086 * If so, do not update the time stamps.
6087 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006088 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006089 return;
6090
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006091 timestamp = trace_clock_local();
6092
6093 prev->ftrace_timestamp = timestamp;
6094
6095 /* only process tasks that we timestamped */
6096 if (!next->ftrace_timestamp)
6097 return;
6098
6099 /*
6100 * Update all the counters in next to make up for the
6101 * time next was sleeping.
6102 */
6103 timestamp -= next->ftrace_timestamp;
6104
6105 for (index = next->curr_ret_stack; index >= 0; index--)
6106 next->ret_stack[index].calltime += timestamp;
6107}
6108
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006109/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006110static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006111{
6112 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006113 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006114
6115 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
6116 sizeof(struct ftrace_ret_stack *),
6117 GFP_KERNEL);
6118
6119 if (!ret_stack_list)
6120 return -ENOMEM;
6121
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006122 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04006123 for_each_online_cpu(cpu) {
6124 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05006125 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04006126 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006127
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006128 do {
6129 ret = alloc_retstack_tasklist(ret_stack_list);
6130 } while (ret == -EAGAIN);
6131
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006132 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04006133 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006134 if (ret)
6135 pr_info("ftrace_graph: Couldn't activate tracepoint"
6136 " probe to kernel_sched_switch\n");
6137 }
6138
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006139 kfree(ret_stack_list);
6140 return ret;
6141}
6142
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006143/*
6144 * Hibernation protection.
6145 * The state of the current task is too much unstable during
6146 * suspend/restore to disk. We want to protect against that.
6147 */
6148static int
6149ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6150 void *unused)
6151{
6152 switch (state) {
6153 case PM_HIBERNATION_PREPARE:
6154 pause_graph_tracing();
6155 break;
6156
6157 case PM_POST_HIBERNATION:
6158 unpause_graph_tracing();
6159 break;
6160 }
6161 return NOTIFY_DONE;
6162}
6163
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006164static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6165{
6166 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6167 return 0;
6168 return __ftrace_graph_entry(trace);
6169}
6170
6171/*
6172 * The function graph tracer should only trace the functions defined
6173 * by set_ftrace_filter and set_ftrace_notrace. If another function
6174 * tracer ops is registered, the graph tracer requires testing the
6175 * function against the global ops, and not just trace any function
6176 * that any ftrace_ops registered.
6177 */
6178static void update_function_graph_func(void)
6179{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006180 struct ftrace_ops *op;
6181 bool do_test = false;
6182
6183 /*
6184 * The graph and global ops share the same set of functions
6185 * to test. If any other ops is on the list, then
6186 * the graph tracing needs to test if its the function
6187 * it should call.
6188 */
6189 do_for_each_ftrace_op(op, ftrace_ops_list) {
6190 if (op != &global_ops && op != &graph_ops &&
6191 op != &ftrace_list_end) {
6192 do_test = true;
6193 /* in double loop, break out with goto */
6194 goto out;
6195 }
6196 } while_for_each_ftrace_op(op);
6197 out:
6198 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006199 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006200 else
6201 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006202}
6203
Mathias Krause8275f692014-03-30 15:31:50 +02006204static struct notifier_block ftrace_suspend_notifier = {
6205 .notifier_call = ftrace_suspend_notifier_call,
6206};
6207
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006208int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6209 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006210{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006211 int ret = 0;
6212
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006213 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006214
Steven Rostedt05ce5812009-03-24 00:18:31 -04006215 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04006216 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04006217 ret = -EBUSY;
6218 goto out;
6219 }
6220
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006221 register_pm_notifier(&ftrace_suspend_notifier);
6222
Steven Rostedt597af812009-04-03 15:24:12 -04006223 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006224 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006225 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04006226 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006227 goto out;
6228 }
Steven Rostedte53a6312008-11-26 00:16:25 -05006229
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006230 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006231
6232 /*
6233 * Update the indirect function to the entryfunc, and the
6234 * function that gets called to the entry_test first. Then
6235 * call the update fgraph entry function to determine if
6236 * the entryfunc should be called directly or not.
6237 */
6238 __ftrace_graph_entry = entryfunc;
6239 ftrace_graph_entry = ftrace_graph_entry_test;
6240 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05006241
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006242 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006243out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006244 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006245 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006246}
6247
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006248void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006249{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006250 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006251
Steven Rostedt597af812009-04-03 15:24:12 -04006252 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006253 goto out;
6254
Steven Rostedt597af812009-04-03 15:24:12 -04006255 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006256 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006257 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006258 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006259 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006260 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04006261 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006262
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006263#ifdef CONFIG_DYNAMIC_FTRACE
6264 /*
6265 * Function graph does not allocate the trampoline, but
6266 * other global_ops do. We need to reset the ALLOC_TRAMP flag
6267 * if one was used.
6268 */
6269 global_ops.trampoline = save_global_trampoline;
6270 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
6271 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
6272#endif
6273
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006274 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006275 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006276}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006277
Steven Rostedt868baf02011-02-10 21:26:13 -05006278static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6279
6280static void
6281graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6282{
6283 atomic_set(&t->tracing_graph_pause, 0);
6284 atomic_set(&t->trace_overrun, 0);
6285 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006286 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05006287 smp_wmb();
6288 t->ret_stack = ret_stack;
6289}
6290
6291/*
6292 * Allocate a return stack for the idle task. May be the first
6293 * time through, or it may be done by CPU hotplug online.
6294 */
6295void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6296{
6297 t->curr_ret_stack = -1;
6298 /*
6299 * The idle task has no parent, it either has its own
6300 * stack or no stack at all.
6301 */
6302 if (t->ret_stack)
6303 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6304
6305 if (ftrace_graph_active) {
6306 struct ftrace_ret_stack *ret_stack;
6307
6308 ret_stack = per_cpu(idle_ret_stack, cpu);
6309 if (!ret_stack) {
6310 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6311 * sizeof(struct ftrace_ret_stack),
6312 GFP_KERNEL);
6313 if (!ret_stack)
6314 return;
6315 per_cpu(idle_ret_stack, cpu) = ret_stack;
6316 }
6317 graph_init_task(t, ret_stack);
6318 }
6319}
6320
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006321/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006322void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006323{
Steven Rostedt84047e32009-06-02 16:51:55 -04006324 /* Make sure we do not use the parent ret_stack */
6325 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05006326 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04006327
Steven Rostedt597af812009-04-03 15:24:12 -04006328 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04006329 struct ftrace_ret_stack *ret_stack;
6330
6331 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006332 * sizeof(struct ftrace_ret_stack),
6333 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04006334 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006335 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05006336 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04006337 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006338}
6339
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006340void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006341{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006342 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6343
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006344 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006345 /* NULL must become visible to IRQs before we free it: */
6346 barrier();
6347
6348 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006349}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006350#endif