blob: 15f910a0382276654b08ae139e63f8ca03c8e380 [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 Rostedt8fc0c702009-02-16 15:28:00 -05001099static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1100
Steven Rostedtb6887d72009-02-17 12:32:04 -05001101struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001102 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001103 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001104 unsigned long ip;
1105 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001106 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001107};
1108
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001109struct ftrace_func_entry {
1110 struct hlist_node hlist;
1111 unsigned long ip;
1112};
1113
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001114/*
1115 * We make these constant because no one should touch them,
1116 * but they are used as the default "empty hash", to avoid allocating
1117 * it all the time. These are in a read only section such that if
1118 * anyone does try to modify it, it will cause an exception.
1119 */
1120static const struct hlist_head empty_buckets[1];
1121static const struct ftrace_hash empty_hash = {
1122 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001124#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001125
Steven Rostedt2b499382011-05-03 22:49:52 -04001126static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001127 .func = ftrace_stub,
1128 .local_hash.notrace_hash = EMPTY_HASH,
1129 .local_hash.filter_hash = EMPTY_HASH,
1130 INIT_OPS_HASH(global_ops)
1131 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001132 FTRACE_OPS_FL_INITIALIZED |
1133 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001134};
1135
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001136/*
1137 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001138 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001139 * not return true for either core_kernel_text() or
1140 * is_module_text_address().
1141 */
1142bool is_ftrace_trampoline(unsigned long addr)
1143{
1144 struct ftrace_ops *op;
1145 bool ret = false;
1146
1147 /*
1148 * Some of the ops may be dynamically allocated,
1149 * they are freed after a synchronize_sched().
1150 */
1151 preempt_disable_notrace();
1152
1153 do_for_each_ftrace_op(op, ftrace_ops_list) {
1154 /*
1155 * This is to check for dynamically allocated trampolines.
1156 * Trampolines that are in kernel text will have
1157 * core_kernel_text() return true.
1158 */
1159 if (op->trampoline && op->trampoline_size)
1160 if (addr >= op->trampoline &&
1161 addr < op->trampoline + op->trampoline_size) {
1162 ret = true;
1163 goto out;
1164 }
1165 } while_for_each_ftrace_op(op);
1166
1167 out:
1168 preempt_enable_notrace();
1169
1170 return ret;
1171}
1172
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001173struct ftrace_page {
1174 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001175 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001176 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001177 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001178};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001179
Steven Rostedta7900872011-12-16 16:23:44 -05001180#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1181#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001182
1183/* estimate from running different kernels */
1184#define NR_TO_INIT 10000
1185
1186static struct ftrace_page *ftrace_pages_start;
1187static struct ftrace_page *ftrace_pages;
1188
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001189static __always_inline unsigned long
1190ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1191{
1192 if (hash->size_bits > 0)
1193 return hash_long(ip, hash->size_bits);
1194
1195 return 0;
1196}
1197
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001198/* Only use this function if ftrace_hash_empty() has already been tested */
1199static __always_inline struct ftrace_func_entry *
1200__ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001201{
1202 unsigned long key;
1203 struct ftrace_func_entry *entry;
1204 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001205
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001206 key = ftrace_hash_key(hash, ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001207 hhd = &hash->buckets[key];
1208
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001209 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210 if (entry->ip == ip)
1211 return entry;
1212 }
1213 return NULL;
1214}
1215
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001216/**
1217 * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1218 * @hash: The hash to look at
1219 * @ip: The instruction pointer to test
1220 *
1221 * Search a given @hash to see if a given instruction pointer (@ip)
1222 * exists in it.
1223 *
1224 * Returns the entry that holds the @ip if found. NULL otherwise.
1225 */
1226struct ftrace_func_entry *
1227ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1228{
1229 if (ftrace_hash_empty(hash))
1230 return NULL;
1231
1232 return __ftrace_lookup_ip(hash, ip);
1233}
1234
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001235static void __add_hash_entry(struct ftrace_hash *hash,
1236 struct ftrace_func_entry *entry)
1237{
1238 struct hlist_head *hhd;
1239 unsigned long key;
1240
Steven Rostedt (VMware)2b0cce02017-02-01 12:19:33 -05001241 key = ftrace_hash_key(hash, entry->ip);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001242 hhd = &hash->buckets[key];
1243 hlist_add_head(&entry->hlist, hhd);
1244 hash->count++;
1245}
1246
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001247static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1248{
1249 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001250
1251 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1252 if (!entry)
1253 return -ENOMEM;
1254
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001255 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001256 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001257
1258 return 0;
1259}
1260
1261static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001262free_hash_entry(struct ftrace_hash *hash,
1263 struct ftrace_func_entry *entry)
1264{
1265 hlist_del(&entry->hlist);
1266 kfree(entry);
1267 hash->count--;
1268}
1269
1270static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001271remove_hash_entry(struct ftrace_hash *hash,
1272 struct ftrace_func_entry *entry)
1273{
1274 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001275 hash->count--;
1276}
1277
1278static void ftrace_hash_clear(struct ftrace_hash *hash)
1279{
1280 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001281 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001282 struct ftrace_func_entry *entry;
1283 int size = 1 << hash->size_bits;
1284 int i;
1285
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001286 if (!hash->count)
1287 return;
1288
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001289 for (i = 0; i < size; i++) {
1290 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001291 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001292 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001293 }
1294 FTRACE_WARN_ON(hash->count);
1295}
1296
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001297static void free_ftrace_hash(struct ftrace_hash *hash)
1298{
1299 if (!hash || hash == EMPTY_HASH)
1300 return;
1301 ftrace_hash_clear(hash);
1302 kfree(hash->buckets);
1303 kfree(hash);
1304}
1305
Steven Rostedt07fd5512011-05-05 18:03:47 -04001306static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1307{
1308 struct ftrace_hash *hash;
1309
1310 hash = container_of(rcu, struct ftrace_hash, rcu);
1311 free_ftrace_hash(hash);
1312}
1313
1314static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1315{
1316 if (!hash || hash == EMPTY_HASH)
1317 return;
1318 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1319}
1320
Jiri Olsa5500fa52012-02-15 15:51:54 +01001321void ftrace_free_filter(struct ftrace_ops *ops)
1322{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001323 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001324 free_ftrace_hash(ops->func_hash->filter_hash);
1325 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001326}
1327
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1329{
1330 struct ftrace_hash *hash;
1331 int size;
1332
1333 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1334 if (!hash)
1335 return NULL;
1336
1337 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001338 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001339
1340 if (!hash->buckets) {
1341 kfree(hash);
1342 return NULL;
1343 }
1344
1345 hash->size_bits = size_bits;
1346
1347 return hash;
1348}
1349
1350static struct ftrace_hash *
1351alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1352{
1353 struct ftrace_func_entry *entry;
1354 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001355 int size;
1356 int ret;
1357 int i;
1358
1359 new_hash = alloc_ftrace_hash(size_bits);
1360 if (!new_hash)
1361 return NULL;
1362
1363 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001364 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001365 return new_hash;
1366
1367 size = 1 << hash->size_bits;
1368 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001369 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001370 ret = add_hash_entry(new_hash, entry->ip);
1371 if (ret < 0)
1372 goto free_hash;
1373 }
1374 }
1375
1376 FTRACE_WARN_ON(new_hash->count != hash->count);
1377
1378 return new_hash;
1379
1380 free_hash:
1381 free_ftrace_hash(new_hash);
1382 return NULL;
1383}
1384
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001385static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001386ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001387static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001388ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001389
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001390static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1391 struct ftrace_hash *new_hash);
1392
Namhyung Kim3e278c02017-01-20 11:44:45 +09001393static struct ftrace_hash *
1394__ftrace_hash_move(struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001395{
1396 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001397 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001398 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001399 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001400 int size = src->count;
1401 int bits = 0;
1402 int i;
1403
1404 /*
Namhyung Kim3e278c02017-01-20 11:44:45 +09001405 * If the new source is empty, just return the empty_hash.
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001406 */
Namhyung Kim3e278c02017-01-20 11:44:45 +09001407 if (!src->count)
1408 return EMPTY_HASH;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001409
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001410 /*
1411 * Make the hash size about 1/2 the # found
1412 */
1413 for (size /= 2; size; size >>= 1)
1414 bits++;
1415
1416 /* Don't allocate too much */
1417 if (bits > FTRACE_HASH_MAX_BITS)
1418 bits = FTRACE_HASH_MAX_BITS;
1419
Steven Rostedt07fd5512011-05-05 18:03:47 -04001420 new_hash = alloc_ftrace_hash(bits);
1421 if (!new_hash)
Namhyung Kim3e278c02017-01-20 11:44:45 +09001422 return NULL;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001423
1424 size = 1 << src->size_bits;
1425 for (i = 0; i < size; i++) {
1426 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001427 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001428 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001429 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001430 }
1431 }
1432
Namhyung Kim3e278c02017-01-20 11:44:45 +09001433 return new_hash;
1434}
1435
1436static int
1437ftrace_hash_move(struct ftrace_ops *ops, int enable,
1438 struct ftrace_hash **dst, struct ftrace_hash *src)
1439{
1440 struct ftrace_hash *new_hash;
1441 int ret;
1442
1443 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1444 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1445 return -EINVAL;
1446
1447 new_hash = __ftrace_hash_move(src);
1448 if (!new_hash)
1449 return -ENOMEM;
1450
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001451 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1452 if (enable) {
1453 /* IPMODIFY should be updated only when filter_hash updating */
1454 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1455 if (ret < 0) {
1456 free_ftrace_hash(new_hash);
1457 return ret;
1458 }
1459 }
1460
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001461 /*
1462 * Remove the current set, update the hash and add
1463 * them back.
1464 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001465 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001466
Steven Rostedt07fd5512011-05-05 18:03:47 -04001467 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001468
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001469 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001470
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001471 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001472}
1473
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001474static bool hash_contains_ip(unsigned long ip,
1475 struct ftrace_ops_hash *hash)
1476{
1477 /*
1478 * The function record is a match if it exists in the filter
1479 * hash and not in the notrace hash. Note, an emty hash is
1480 * considered a match for the filter hash, but an empty
1481 * notrace hash is considered not in the notrace hash.
1482 */
1483 return (ftrace_hash_empty(hash->filter_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001484 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001485 (ftrace_hash_empty(hash->notrace_hash) ||
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05001486 !__ftrace_lookup_ip(hash->notrace_hash, ip));
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001487}
1488
Steven Rostedt265c8312009-02-13 12:43:56 -05001489/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001490 * Test the hashes for this ops to see if we want to call
1491 * the ops->func or not.
1492 *
1493 * It's a match if the ip is in the ops->filter_hash or
1494 * the filter_hash does not exist or is empty,
1495 * AND
1496 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001497 *
1498 * This needs to be called with preemption disabled as
1499 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001500 */
1501static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001502ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001503{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001504 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001505 int ret;
1506
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001507#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1508 /*
1509 * There's a small race when adding ops that the ftrace handler
1510 * that wants regs, may be called without them. We can not
1511 * allow that handler to be called if regs is NULL.
1512 */
1513 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1514 return 0;
1515#endif
1516
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001517 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1518 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001519
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001520 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001521 ret = 1;
1522 else
1523 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001524
1525 return ret;
1526}
1527
1528/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001529 * This is a double for. Do not use 'break' to break out of the loop,
1530 * you must use a goto.
1531 */
1532#define do_for_each_ftrace_rec(pg, rec) \
1533 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1534 int _____i; \
1535 for (_____i = 0; _____i < pg->index; _____i++) { \
1536 rec = &pg->records[_____i];
1537
1538#define while_for_each_ftrace_rec() \
1539 } \
1540 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301541
Steven Rostedt5855fea2011-12-16 19:27:42 -05001542
1543static int ftrace_cmp_recs(const void *a, const void *b)
1544{
Steven Rostedta650e022012-04-25 13:48:13 -04001545 const struct dyn_ftrace *key = a;
1546 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001547
Steven Rostedta650e022012-04-25 13:48:13 -04001548 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001549 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001550 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1551 return 1;
1552 return 0;
1553}
1554
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001555/**
1556 * ftrace_location_range - return the first address of a traced location
1557 * if it touches the given ip range
1558 * @start: start of range to search.
1559 * @end: end of range to search (inclusive). @end points to the last byte
1560 * to check.
1561 *
1562 * Returns rec->ip if the related ftrace location is a least partly within
1563 * the given address range. That is, the first address of the instruction
1564 * that is either a NOP or call to the function tracer. It checks the ftrace
1565 * internal tables to determine if the address belongs or not.
1566 */
1567unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001568{
1569 struct ftrace_page *pg;
1570 struct dyn_ftrace *rec;
1571 struct dyn_ftrace key;
1572
1573 key.ip = start;
1574 key.flags = end; /* overload flags, as it is unsigned long */
1575
1576 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1577 if (end < pg->records[0].ip ||
1578 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1579 continue;
1580 rec = bsearch(&key, pg->records, pg->index,
1581 sizeof(struct dyn_ftrace),
1582 ftrace_cmp_recs);
1583 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001584 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001585 }
1586
Steven Rostedt5855fea2011-12-16 19:27:42 -05001587 return 0;
1588}
1589
Steven Rostedtc88fd862011-08-16 09:53:39 -04001590/**
1591 * ftrace_location - return true if the ip giving is a traced location
1592 * @ip: the instruction pointer to check
1593 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001594 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001595 * That is, the instruction that is either a NOP or call to
1596 * the function tracer. It checks the ftrace internal tables to
1597 * determine if the address belongs or not.
1598 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001599unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001600{
Steven Rostedta650e022012-04-25 13:48:13 -04001601 return ftrace_location_range(ip, ip);
1602}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001603
Steven Rostedta650e022012-04-25 13:48:13 -04001604/**
1605 * ftrace_text_reserved - return true if range contains an ftrace location
1606 * @start: start of range to search
1607 * @end: end of range to search (inclusive). @end points to the last byte to check.
1608 *
1609 * Returns 1 if @start and @end contains a ftrace location.
1610 * That is, the instruction that is either a NOP or call to
1611 * the function tracer. It checks the ftrace internal tables to
1612 * determine if the address belongs or not.
1613 */
Sasha Levind88471c2013-01-09 18:09:20 -05001614int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001615{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001616 unsigned long ret;
1617
1618 ret = ftrace_location_range((unsigned long)start,
1619 (unsigned long)end);
1620
1621 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001622}
1623
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001624/* Test if ops registered to this rec needs regs */
1625static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1626{
1627 struct ftrace_ops *ops;
1628 bool keep_regs = false;
1629
1630 for (ops = ftrace_ops_list;
1631 ops != &ftrace_list_end; ops = ops->next) {
1632 /* pass rec in as regs to have non-NULL val */
1633 if (ftrace_ops_test(ops, rec->ip, rec)) {
1634 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1635 keep_regs = true;
1636 break;
1637 }
1638 }
1639 }
1640
1641 return keep_regs;
1642}
1643
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001644static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001645 int filter_hash,
1646 bool inc)
1647{
1648 struct ftrace_hash *hash;
1649 struct ftrace_hash *other_hash;
1650 struct ftrace_page *pg;
1651 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001652 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001653 int count = 0;
1654 int all = 0;
1655
1656 /* Only update if the ops has been registered */
1657 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001658 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001659
1660 /*
1661 * In the filter_hash case:
1662 * If the count is zero, we update all records.
1663 * Otherwise we just update the items in the hash.
1664 *
1665 * In the notrace_hash case:
1666 * We enable the update in the hash.
1667 * As disabling notrace means enabling the tracing,
1668 * and enabling notrace means disabling, the inc variable
1669 * gets inversed.
1670 */
1671 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001672 hash = ops->func_hash->filter_hash;
1673 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001674 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001675 all = 1;
1676 } else {
1677 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001678 hash = ops->func_hash->notrace_hash;
1679 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001680 /*
1681 * If the notrace hash has no items,
1682 * then there's nothing to do.
1683 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001684 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001685 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001686 }
1687
1688 do_for_each_ftrace_rec(pg, rec) {
1689 int in_other_hash = 0;
1690 int in_hash = 0;
1691 int match = 0;
1692
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001693 if (rec->flags & FTRACE_FL_DISABLED)
1694 continue;
1695
Steven Rostedted926f92011-05-03 13:25:24 -04001696 if (all) {
1697 /*
1698 * Only the filter_hash affects all records.
1699 * Update if the record is not in the notrace hash.
1700 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001701 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001702 match = 1;
1703 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001704 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1705 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001706
1707 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001708 * If filter_hash is set, we want to match all functions
1709 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001710 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001711 * If filter_hash is not set, then we are decrementing.
1712 * That means we match anything that is in the hash
1713 * and also in the other_hash. That is, we need to turn
1714 * off functions in the other hash because they are disabled
1715 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001716 */
1717 if (filter_hash && in_hash && !in_other_hash)
1718 match = 1;
1719 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001720 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001721 match = 1;
1722 }
1723 if (!match)
1724 continue;
1725
1726 if (inc) {
1727 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001728 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001729 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001730
1731 /*
1732 * If there's only a single callback registered to a
1733 * function, and the ops has a trampoline registered
1734 * for it, then we can call it directly.
1735 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001736 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001737 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001738 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001739 /*
1740 * If we are adding another function callback
1741 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001742 * custom trampoline in use, then we need to go
1743 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001744 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001745 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001746
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001747 /*
1748 * If any ops wants regs saved for this function
1749 * then all ops will get saved regs.
1750 */
1751 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1752 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001753 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001754 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001755 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001756 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001757
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001758 /*
1759 * If the rec had REGS enabled and the ops that is
1760 * being removed had REGS set, then see if there is
1761 * still any ops for this record that wants regs.
1762 * If not, we can stop recording them.
1763 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001764 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001765 rec->flags & FTRACE_FL_REGS &&
1766 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1767 if (!test_rec_ops_needs_regs(rec))
1768 rec->flags &= ~FTRACE_FL_REGS;
1769 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001770
1771 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001772 * If the rec had TRAMP enabled, then it needs to
1773 * be cleared. As TRAMP can only be enabled iff
1774 * there is only a single ops attached to it.
1775 * In otherwords, always disable it on decrementing.
1776 * In the future, we may set it if rec count is
1777 * decremented to one, and the ops that is left
1778 * has a trampoline.
1779 */
1780 rec->flags &= ~FTRACE_FL_TRAMP;
1781
1782 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001783 * flags will be cleared in ftrace_check_record()
1784 * if rec count is zero.
1785 */
Steven Rostedted926f92011-05-03 13:25:24 -04001786 }
1787 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001788
1789 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1790 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1791
Steven Rostedted926f92011-05-03 13:25:24 -04001792 /* Shortcut, if we handled all records, we are done. */
1793 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001794 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001795 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001796
1797 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001798}
1799
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001800static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001801 int filter_hash)
1802{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001803 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001804}
1805
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001806static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001807 int filter_hash)
1808{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001809 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001810}
1811
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001812static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1813 int filter_hash, int inc)
1814{
1815 struct ftrace_ops *op;
1816
1817 __ftrace_hash_rec_update(ops, filter_hash, inc);
1818
1819 if (ops->func_hash != &global_ops.local_hash)
1820 return;
1821
1822 /*
1823 * If the ops shares the global_ops hash, then we need to update
1824 * all ops that are enabled and use this hash.
1825 */
1826 do_for_each_ftrace_op(op, ftrace_ops_list) {
1827 /* Already done */
1828 if (op == ops)
1829 continue;
1830 if (op->func_hash == &global_ops.local_hash)
1831 __ftrace_hash_rec_update(op, filter_hash, inc);
1832 } while_for_each_ftrace_op(op);
1833}
1834
1835static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1836 int filter_hash)
1837{
1838 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1839}
1840
1841static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1842 int filter_hash)
1843{
1844 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1845}
1846
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001847/*
1848 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1849 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1850 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1851 * Note that old_hash and new_hash has below meanings
1852 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1853 * - If the hash is EMPTY_HASH, it hits nothing
1854 * - Anything else hits the recs which match the hash entries.
1855 */
1856static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1857 struct ftrace_hash *old_hash,
1858 struct ftrace_hash *new_hash)
1859{
1860 struct ftrace_page *pg;
1861 struct dyn_ftrace *rec, *end = NULL;
1862 int in_old, in_new;
1863
1864 /* Only update if the ops has been registered */
1865 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1866 return 0;
1867
1868 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1869 return 0;
1870
1871 /*
1872 * Since the IPMODIFY is a very address sensitive action, we do not
1873 * allow ftrace_ops to set all functions to new hash.
1874 */
1875 if (!new_hash || !old_hash)
1876 return -EINVAL;
1877
1878 /* Update rec->flags */
1879 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001880
1881 if (rec->flags & FTRACE_FL_DISABLED)
1882 continue;
1883
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001884 /* We need to update only differences of filter_hash */
1885 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1886 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1887 if (in_old == in_new)
1888 continue;
1889
1890 if (in_new) {
1891 /* New entries must ensure no others are using it */
1892 if (rec->flags & FTRACE_FL_IPMODIFY)
1893 goto rollback;
1894 rec->flags |= FTRACE_FL_IPMODIFY;
1895 } else /* Removed entry */
1896 rec->flags &= ~FTRACE_FL_IPMODIFY;
1897 } while_for_each_ftrace_rec();
1898
1899 return 0;
1900
1901rollback:
1902 end = rec;
1903
1904 /* Roll back what we did above */
1905 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001906
1907 if (rec->flags & FTRACE_FL_DISABLED)
1908 continue;
1909
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001910 if (rec == end)
1911 goto err_out;
1912
1913 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1914 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1915 if (in_old == in_new)
1916 continue;
1917
1918 if (in_new)
1919 rec->flags &= ~FTRACE_FL_IPMODIFY;
1920 else
1921 rec->flags |= FTRACE_FL_IPMODIFY;
1922 } while_for_each_ftrace_rec();
1923
1924err_out:
1925 return -EBUSY;
1926}
1927
1928static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1929{
1930 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1931
1932 if (ftrace_hash_empty(hash))
1933 hash = NULL;
1934
1935 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1936}
1937
1938/* Disabling always succeeds */
1939static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1940{
1941 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1942
1943 if (ftrace_hash_empty(hash))
1944 hash = NULL;
1945
1946 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1947}
1948
1949static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1950 struct ftrace_hash *new_hash)
1951{
1952 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1953
1954 if (ftrace_hash_empty(old_hash))
1955 old_hash = NULL;
1956
1957 if (ftrace_hash_empty(new_hash))
1958 new_hash = NULL;
1959
1960 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1961}
1962
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001963static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001964{
1965 int i;
1966
1967 printk(KERN_CONT "%s", fmt);
1968
1969 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1970 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1971}
1972
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001973static struct ftrace_ops *
1974ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05001975static struct ftrace_ops *
1976ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001977
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001978enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001979const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001980
1981static void print_bug_type(void)
1982{
1983 switch (ftrace_bug_type) {
1984 case FTRACE_BUG_UNKNOWN:
1985 break;
1986 case FTRACE_BUG_INIT:
1987 pr_info("Initializing ftrace call sites\n");
1988 break;
1989 case FTRACE_BUG_NOP:
1990 pr_info("Setting ftrace call site to NOP\n");
1991 break;
1992 case FTRACE_BUG_CALL:
1993 pr_info("Setting ftrace call site to call ftrace function\n");
1994 break;
1995 case FTRACE_BUG_UPDATE:
1996 pr_info("Updating ftrace call site to call a different ftrace function\n");
1997 break;
1998 }
1999}
2000
Steven Rostedtc88fd862011-08-16 09:53:39 -04002001/**
2002 * ftrace_bug - report and shutdown function tracer
2003 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002004 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04002005 *
2006 * The arch code that enables or disables the function tracing
2007 * can call ftrace_bug() when it has detected a problem in
2008 * modifying the code. @failed should be one of either:
2009 * EFAULT - if the problem happens on reading the @ip address
2010 * EINVAL - if what is read at @ip is not what was expected
2011 * EPERM - if the problem happens on writting to the @ip address
2012 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002013void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002014{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002015 unsigned long ip = rec ? rec->ip : 0;
2016
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002017 switch (failed) {
2018 case -EFAULT:
2019 FTRACE_WARN_ON_ONCE(1);
2020 pr_info("ftrace faulted on modifying ");
2021 print_ip_sym(ip);
2022 break;
2023 case -EINVAL:
2024 FTRACE_WARN_ON_ONCE(1);
2025 pr_info("ftrace failed to modify ");
2026 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002027 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002028 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002029 if (ftrace_expected) {
2030 print_ip_ins(" expected: ", ftrace_expected);
2031 pr_cont("\n");
2032 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002033 break;
2034 case -EPERM:
2035 FTRACE_WARN_ON_ONCE(1);
2036 pr_info("ftrace faulted on writing ");
2037 print_ip_sym(ip);
2038 break;
2039 default:
2040 FTRACE_WARN_ON_ONCE(1);
2041 pr_info("ftrace faulted on unknown error ");
2042 print_ip_sym(ip);
2043 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002044 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002045 if (rec) {
2046 struct ftrace_ops *ops = NULL;
2047
2048 pr_info("ftrace record flags: %lx\n", rec->flags);
2049 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2050 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2051 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2052 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002053 if (ops) {
2054 do {
2055 pr_cont("\ttramp: %pS (%pS)",
2056 (void *)ops->trampoline,
2057 (void *)ops->func);
2058 ops = ftrace_find_tramp_ops_next(rec, ops);
2059 } while (ops);
2060 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002061 pr_cont("\ttramp: ERROR!");
2062
2063 }
2064 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002065 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002066 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002067}
2068
Steven Rostedtc88fd862011-08-16 09:53:39 -04002069static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002070{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002071 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002072
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002073 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2074
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002075 if (rec->flags & FTRACE_FL_DISABLED)
2076 return FTRACE_UPDATE_IGNORE;
2077
Steven Rostedt982c3502008-11-15 16:31:41 -05002078 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002079 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002080 *
Steven Rostedted926f92011-05-03 13:25:24 -04002081 * If the record has a ref count, then we need to enable it
2082 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002083 *
Steven Rostedted926f92011-05-03 13:25:24 -04002084 * Otherwise we make sure its disabled.
2085 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002086 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002087 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002088 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002089 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002090 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002091
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002092 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002093 * If enabling and the REGS flag does not match the REGS_EN, or
2094 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2095 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002096 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002097 if (flag) {
2098 if (!(rec->flags & FTRACE_FL_REGS) !=
2099 !(rec->flags & FTRACE_FL_REGS_EN))
2100 flag |= FTRACE_FL_REGS;
2101
2102 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2103 !(rec->flags & FTRACE_FL_TRAMP_EN))
2104 flag |= FTRACE_FL_TRAMP;
2105 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002106
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002107 /* If the state of this record hasn't changed, then do nothing */
2108 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002109 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002110
2111 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002112 /* Save off if rec is being enabled (for return value) */
2113 flag ^= rec->flags & FTRACE_FL_ENABLED;
2114
2115 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002116 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002117 if (flag & FTRACE_FL_REGS) {
2118 if (rec->flags & FTRACE_FL_REGS)
2119 rec->flags |= FTRACE_FL_REGS_EN;
2120 else
2121 rec->flags &= ~FTRACE_FL_REGS_EN;
2122 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002123 if (flag & FTRACE_FL_TRAMP) {
2124 if (rec->flags & FTRACE_FL_TRAMP)
2125 rec->flags |= FTRACE_FL_TRAMP_EN;
2126 else
2127 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2128 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002129 }
2130
2131 /*
2132 * If this record is being updated from a nop, then
2133 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002134 * Otherwise,
2135 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002136 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002137 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002138 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002139 if (flag & FTRACE_FL_ENABLED) {
2140 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002141 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002142 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002143
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002144 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002145 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002146 }
2147
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002148 if (update) {
2149 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002150 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002151 rec->flags = 0;
2152 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002153 /*
2154 * Just disable the record, but keep the ops TRAMP
2155 * and REGS states. The _EN flags must be disabled though.
2156 */
2157 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2158 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002159 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002160
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002161 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002162 return FTRACE_UPDATE_MAKE_NOP;
2163}
2164
2165/**
2166 * ftrace_update_record, set a record that now is tracing or not
2167 * @rec: the record to update
2168 * @enable: set to 1 if the record is tracing, zero to force disable
2169 *
2170 * The records that represent all functions that can be traced need
2171 * to be updated when tracing has been enabled.
2172 */
2173int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2174{
2175 return ftrace_check_record(rec, enable, 1);
2176}
2177
2178/**
2179 * ftrace_test_record, check if the record has been enabled or not
2180 * @rec: the record to test
2181 * @enable: set to 1 to check if enabled, 0 if it is disabled
2182 *
2183 * The arch code may need to test if a record is already set to
2184 * tracing to determine how to modify the function code that it
2185 * represents.
2186 */
2187int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2188{
2189 return ftrace_check_record(rec, enable, 0);
2190}
2191
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002192static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002193ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2194{
2195 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002196 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002197
2198 do_for_each_ftrace_op(op, ftrace_ops_list) {
2199
2200 if (!op->trampoline)
2201 continue;
2202
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002203 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002204 return op;
2205 } while_for_each_ftrace_op(op);
2206
2207 return NULL;
2208}
2209
2210static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002211ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2212 struct ftrace_ops *op)
2213{
2214 unsigned long ip = rec->ip;
2215
2216 while_for_each_ftrace_op(op) {
2217
2218 if (!op->trampoline)
2219 continue;
2220
2221 if (hash_contains_ip(ip, op->func_hash))
2222 return op;
2223 }
2224
2225 return NULL;
2226}
2227
2228static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002229ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2230{
2231 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002232 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002233
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002234 /*
2235 * Need to check removed ops first.
2236 * If they are being removed, and this rec has a tramp,
2237 * and this rec is in the ops list, then it would be the
2238 * one with the tramp.
2239 */
2240 if (removed_ops) {
2241 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002242 return removed_ops;
2243 }
2244
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002245 /*
2246 * Need to find the current trampoline for a rec.
2247 * Now, a trampoline is only attached to a rec if there
2248 * was a single 'ops' attached to it. But this can be called
2249 * when we are adding another op to the rec or removing the
2250 * current one. Thus, if the op is being added, we can
2251 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002252 * yet.
2253 *
2254 * If an ops is being modified (hooking to different functions)
2255 * then we don't care about the new functions that are being
2256 * added, just the old ones (that are probably being removed).
2257 *
2258 * If we are adding an ops to a function that already is using
2259 * a trampoline, it needs to be removed (trampolines are only
2260 * for single ops connected), then an ops that is not being
2261 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002262 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002263 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002264
2265 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002266 continue;
2267
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002268 /*
2269 * If the ops is being added, it hasn't gotten to
2270 * the point to be removed from this tree yet.
2271 */
2272 if (op->flags & FTRACE_OPS_FL_ADDING)
2273 continue;
2274
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002275
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002276 /*
2277 * If the ops is being modified and is in the old
2278 * hash, then it is probably being removed from this
2279 * function.
2280 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002281 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2282 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002283 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002284 /*
2285 * If the ops is not being added or modified, and it's
2286 * in its normal filter hash, then this must be the one
2287 * we want!
2288 */
2289 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2290 hash_contains_ip(ip, op->func_hash))
2291 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002292
2293 } while_for_each_ftrace_op(op);
2294
2295 return NULL;
2296}
2297
2298static struct ftrace_ops *
2299ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2300{
2301 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002302 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002303
2304 do_for_each_ftrace_op(op, ftrace_ops_list) {
2305 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002306 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002307 return op;
2308 } while_for_each_ftrace_op(op);
2309
2310 return NULL;
2311}
2312
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002313/**
2314 * ftrace_get_addr_new - Get the call address to set to
2315 * @rec: The ftrace record descriptor
2316 *
2317 * If the record has the FTRACE_FL_REGS set, that means that it
2318 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2319 * is not not set, then it wants to convert to the normal callback.
2320 *
2321 * Returns the address of the trampoline to set to
2322 */
2323unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2324{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002325 struct ftrace_ops *ops;
2326
2327 /* Trampolines take precedence over regs */
2328 if (rec->flags & FTRACE_FL_TRAMP) {
2329 ops = ftrace_find_tramp_ops_new(rec);
2330 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002331 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2332 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002333 /* Ftrace is shutting down, return anything */
2334 return (unsigned long)FTRACE_ADDR;
2335 }
2336 return ops->trampoline;
2337 }
2338
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002339 if (rec->flags & FTRACE_FL_REGS)
2340 return (unsigned long)FTRACE_REGS_ADDR;
2341 else
2342 return (unsigned long)FTRACE_ADDR;
2343}
2344
2345/**
2346 * ftrace_get_addr_curr - Get the call address that is already there
2347 * @rec: The ftrace record descriptor
2348 *
2349 * The FTRACE_FL_REGS_EN is set when the record already points to
2350 * a function that saves all the regs. Basically the '_EN' version
2351 * represents the current state of the function.
2352 *
2353 * Returns the address of the trampoline that is currently being called
2354 */
2355unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2356{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002357 struct ftrace_ops *ops;
2358
2359 /* Trampolines take precedence over regs */
2360 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2361 ops = ftrace_find_tramp_ops_curr(rec);
2362 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002363 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2364 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002365 /* Ftrace is shutting down, return anything */
2366 return (unsigned long)FTRACE_ADDR;
2367 }
2368 return ops->trampoline;
2369 }
2370
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002371 if (rec->flags & FTRACE_FL_REGS_EN)
2372 return (unsigned long)FTRACE_REGS_ADDR;
2373 else
2374 return (unsigned long)FTRACE_ADDR;
2375}
2376
Steven Rostedtc88fd862011-08-16 09:53:39 -04002377static int
2378__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2379{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002380 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002381 unsigned long ftrace_addr;
2382 int ret;
2383
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002384 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002385
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002386 /* This needs to be done before we call ftrace_update_record */
2387 ftrace_old_addr = ftrace_get_addr_curr(rec);
2388
2389 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002390
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002391 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2392
Steven Rostedtc88fd862011-08-16 09:53:39 -04002393 switch (ret) {
2394 case FTRACE_UPDATE_IGNORE:
2395 return 0;
2396
2397 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002398 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002399 return ftrace_make_call(rec, ftrace_addr);
2400
2401 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002402 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002403 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002404
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002405 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002406 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002407 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002408 }
2409
2410 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002411}
2412
Steven Rostedte4f5d542012-04-27 09:13:18 -04002413void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002414{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002415 struct dyn_ftrace *rec;
2416 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002417 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002418
Steven Rostedt45a4a232011-04-21 23:16:46 -04002419 if (unlikely(ftrace_disabled))
2420 return;
2421
Steven Rostedt265c8312009-02-13 12:43:56 -05002422 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002423
2424 if (rec->flags & FTRACE_FL_DISABLED)
2425 continue;
2426
Steven Rostedte4f5d542012-04-27 09:13:18 -04002427 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002428 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002429 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002430 /* Stop processing */
2431 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002432 }
2433 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002434}
2435
Steven Rostedtc88fd862011-08-16 09:53:39 -04002436struct ftrace_rec_iter {
2437 struct ftrace_page *pg;
2438 int index;
2439};
2440
2441/**
2442 * ftrace_rec_iter_start, start up iterating over traced functions
2443 *
2444 * Returns an iterator handle that is used to iterate over all
2445 * the records that represent address locations where functions
2446 * are traced.
2447 *
2448 * May return NULL if no records are available.
2449 */
2450struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2451{
2452 /*
2453 * We only use a single iterator.
2454 * Protected by the ftrace_lock mutex.
2455 */
2456 static struct ftrace_rec_iter ftrace_rec_iter;
2457 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2458
2459 iter->pg = ftrace_pages_start;
2460 iter->index = 0;
2461
2462 /* Could have empty pages */
2463 while (iter->pg && !iter->pg->index)
2464 iter->pg = iter->pg->next;
2465
2466 if (!iter->pg)
2467 return NULL;
2468
2469 return iter;
2470}
2471
2472/**
2473 * ftrace_rec_iter_next, get the next record to process.
2474 * @iter: The handle to the iterator.
2475 *
2476 * Returns the next iterator after the given iterator @iter.
2477 */
2478struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2479{
2480 iter->index++;
2481
2482 if (iter->index >= iter->pg->index) {
2483 iter->pg = iter->pg->next;
2484 iter->index = 0;
2485
2486 /* Could have empty pages */
2487 while (iter->pg && !iter->pg->index)
2488 iter->pg = iter->pg->next;
2489 }
2490
2491 if (!iter->pg)
2492 return NULL;
2493
2494 return iter;
2495}
2496
2497/**
2498 * ftrace_rec_iter_record, get the record at the iterator location
2499 * @iter: The current iterator location
2500 *
2501 * Returns the record that the current @iter is at.
2502 */
2503struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2504{
2505 return &iter->pg->records[iter->index];
2506}
2507
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302508static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002509ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002510{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002511 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002512
Steven Rostedt45a4a232011-04-21 23:16:46 -04002513 if (unlikely(ftrace_disabled))
2514 return 0;
2515
Shaohua Li25aac9d2009-01-09 11:29:40 +08002516 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002517 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002518 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002519 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302520 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002521 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302522 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002523}
2524
Steven Rostedt000ab692009-02-17 13:35:06 -05002525/*
2526 * archs can override this function if they must do something
2527 * before the modifying code is performed.
2528 */
2529int __weak ftrace_arch_code_modify_prepare(void)
2530{
2531 return 0;
2532}
2533
2534/*
2535 * archs can override this function if they must do something
2536 * after the modifying code is performed.
2537 */
2538int __weak ftrace_arch_code_modify_post_process(void)
2539{
2540 return 0;
2541}
2542
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002543void ftrace_modify_all_code(int command)
2544{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002545 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002546 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002547
2548 /*
2549 * If the ftrace_caller calls a ftrace_ops func directly,
2550 * we need to make sure that it only traces functions it
2551 * expects to trace. When doing the switch of functions,
2552 * we need to update to the ftrace_ops_list_func first
2553 * before the transition between old and new calls are set,
2554 * as the ftrace_ops_list_func will check the ops hashes
2555 * to make sure the ops are having the right functions
2556 * traced.
2557 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002558 if (update) {
2559 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2560 if (FTRACE_WARN_ON(err))
2561 return;
2562 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002563
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002564 if (command & FTRACE_UPDATE_CALLS)
2565 ftrace_replace_code(1);
2566 else if (command & FTRACE_DISABLE_CALLS)
2567 ftrace_replace_code(0);
2568
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002569 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2570 function_trace_op = set_function_trace_op;
2571 smp_wmb();
2572 /* If irqs are disabled, we are in stop machine */
2573 if (!irqs_disabled())
2574 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002575 err = ftrace_update_ftrace_func(ftrace_trace_function);
2576 if (FTRACE_WARN_ON(err))
2577 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002578 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002579
2580 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002581 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002582 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002583 err = ftrace_disable_ftrace_graph_caller();
2584 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002585}
2586
Ingo Molnare309b412008-05-12 21:20:51 +02002587static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002588{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002589 int *command = data;
2590
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002591 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002592
Steven Rostedtc88fd862011-08-16 09:53:39 -04002593 return 0;
2594}
2595
2596/**
2597 * ftrace_run_stop_machine, go back to the stop machine method
2598 * @command: The command to tell ftrace what to do
2599 *
2600 * If an arch needs to fall back to the stop machine method, the
2601 * it can call this function.
2602 */
2603void ftrace_run_stop_machine(int command)
2604{
2605 stop_machine(__ftrace_modify_code, &command, NULL);
2606}
2607
2608/**
2609 * arch_ftrace_update_code, modify the code to trace or not trace
2610 * @command: The command that needs to be done
2611 *
2612 * Archs can override this function if it does not need to
2613 * run stop_machine() to modify code.
2614 */
2615void __weak arch_ftrace_update_code(int command)
2616{
2617 ftrace_run_stop_machine(command);
2618}
2619
2620static void ftrace_run_update_code(int command)
2621{
2622 int ret;
2623
2624 ret = ftrace_arch_code_modify_prepare();
2625 FTRACE_WARN_ON(ret);
2626 if (ret)
2627 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002628
2629 /*
2630 * By default we use stop_machine() to modify the code.
2631 * But archs can do what ever they want as long as it
2632 * is safe. The stop_machine() is the safest, but also
2633 * produces the most overhead.
2634 */
2635 arch_ftrace_update_code(command);
2636
Steven Rostedt000ab692009-02-17 13:35:06 -05002637 ret = ftrace_arch_code_modify_post_process();
2638 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002639}
2640
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002641static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002642 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002643{
2644 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002645 ops->old_hash.filter_hash = old_hash->filter_hash;
2646 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002647 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002648 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002649 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002650 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2651}
2652
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002653static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002654static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002655
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002656void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2657{
2658}
2659
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002660static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002661{
2662 free_percpu(ops->disabled);
2663}
2664
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002665static void ftrace_startup_enable(int command)
2666{
2667 if (saved_ftrace_func != ftrace_trace_function) {
2668 saved_ftrace_func = ftrace_trace_function;
2669 command |= FTRACE_UPDATE_TRACE_FUNC;
2670 }
2671
2672 if (!command || !ftrace_enabled)
2673 return;
2674
2675 ftrace_run_update_code(command);
2676}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002677
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002678static void ftrace_startup_all(int command)
2679{
2680 update_all_ops = true;
2681 ftrace_startup_enable(command);
2682 update_all_ops = false;
2683}
2684
Steven Rostedta1cd6172011-05-23 15:24:25 -04002685static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002686{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002687 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002688
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002689 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002690 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002691
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002692 ret = __register_ftrace_function(ops);
2693 if (ret)
2694 return ret;
2695
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002696 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002697
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002698 /*
2699 * Note that ftrace probes uses this to start up
2700 * and modify functions it will probe. But we still
2701 * set the ADDING flag for modification, as probes
2702 * do not have trampolines. If they add them in the
2703 * future, then the probes will need to distinguish
2704 * between adding and updating probes.
2705 */
2706 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002707
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002708 ret = ftrace_hash_ipmodify_enable(ops);
2709 if (ret < 0) {
2710 /* Rollback registration process */
2711 __unregister_ftrace_function(ops);
2712 ftrace_start_up--;
2713 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2714 return ret;
2715 }
2716
Jiri Olsa7f50d062016-03-16 15:34:33 +01002717 if (ftrace_hash_rec_enable(ops, 1))
2718 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002719
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002720 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002721
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002722 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2723
Steven Rostedta1cd6172011-05-23 15:24:25 -04002724 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002725}
2726
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002727static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002728{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002729 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002730
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002731 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002732 return -ENODEV;
2733
2734 ret = __unregister_ftrace_function(ops);
2735 if (ret)
2736 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002737
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002738 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002739 /*
2740 * Just warn in case of unbalance, no need to kill ftrace, it's not
2741 * critical but the ftrace_call callers may be never nopped again after
2742 * further ftrace uses.
2743 */
2744 WARN_ON_ONCE(ftrace_start_up < 0);
2745
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002746 /* Disabling ipmodify never fails */
2747 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002748
2749 if (ftrace_hash_rec_disable(ops, 1))
2750 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002751
Namhyung Kima737e6d2014-06-12 23:56:12 +09002752 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002753
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002754 if (saved_ftrace_func != ftrace_trace_function) {
2755 saved_ftrace_func = ftrace_trace_function;
2756 command |= FTRACE_UPDATE_TRACE_FUNC;
2757 }
2758
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002759 if (!command || !ftrace_enabled) {
2760 /*
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002761 * If these are per_cpu ops, they still need their
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002762 * per_cpu field freed. Since, function tracing is
2763 * not currently active, we can just free them
2764 * without synchronizing all CPUs.
2765 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002766 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2767 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002768 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002769 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002770
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002771 /*
2772 * If the ops uses a trampoline, then it needs to be
2773 * tested first on update.
2774 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002775 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002776 removed_ops = ops;
2777
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002778 /* The trampoline logic checks the old hashes */
2779 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2780 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2781
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002782 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002783
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002784 /*
2785 * If there's no more ops registered with ftrace, run a
2786 * sanity check to make sure all rec flags are cleared.
2787 */
2788 if (ftrace_ops_list == &ftrace_list_end) {
2789 struct ftrace_page *pg;
2790 struct dyn_ftrace *rec;
2791
2792 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002793 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002794 pr_warn(" %pS flags:%lx\n",
2795 (void *)rec->ip, rec->flags);
2796 } while_for_each_ftrace_rec();
2797 }
2798
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002799 ops->old_hash.filter_hash = NULL;
2800 ops->old_hash.notrace_hash = NULL;
2801
2802 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002803 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002804
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002805 /*
2806 * Dynamic ops may be freed, we must make sure that all
2807 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002808 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002809 * ops.
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002810 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002811 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002812 /*
2813 * We need to do a hard force of sched synchronization.
2814 * This is because we use preempt_disable() to do RCU, but
2815 * the function tracers can be called where RCU is not watching
2816 * (like before user_exit()). We can not rely on the RCU
2817 * infrastructure to do the synchronization, thus we must do it
2818 * ourselves.
2819 */
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002820 schedule_on_each_cpu(ftrace_sync);
2821
Steven Rostedt (VMware)0598e4f2017-04-06 10:28:12 -04002822 /*
2823 * When the kernel is preeptive, tasks can be preempted
2824 * while on a ftrace trampoline. Just scheduling a task on
2825 * a CPU is not good enough to flush them. Calling
2826 * synchornize_rcu_tasks() will wait for those tasks to
2827 * execute and either schedule voluntarily or enter user space.
2828 */
2829 if (IS_ENABLED(CONFIG_PREEMPT))
2830 synchronize_rcu_tasks();
2831
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002832 arch_ftrace_trampoline_free(ops);
2833
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002834 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2835 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002836 }
2837
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002838 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002839}
2840
Ingo Molnare309b412008-05-12 21:20:51 +02002841static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002842{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302843 int command;
2844
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002845 if (unlikely(ftrace_disabled))
2846 return;
2847
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002848 /* Force update next time */
2849 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002850 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302851 if (ftrace_start_up) {
2852 command = FTRACE_UPDATE_CALLS;
2853 if (ftrace_graph_active)
2854 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002855 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302856 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002857}
2858
Ingo Molnare309b412008-05-12 21:20:51 +02002859static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002860{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302861 int command;
2862
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002863 if (unlikely(ftrace_disabled))
2864 return;
2865
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002866 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302867 if (ftrace_start_up) {
2868 command = FTRACE_DISABLE_CALLS;
2869 if (ftrace_graph_active)
2870 command |= FTRACE_STOP_FUNC_RET;
2871 ftrace_run_update_code(command);
2872 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002873}
2874
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002875static u64 ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002876unsigned long ftrace_update_tot_cnt;
2877
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002878static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002879{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002880 /*
2881 * Filter_hash being empty will default to trace module.
2882 * But notrace hash requires a test of individual module functions.
2883 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002884 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2885 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002886}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002887
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002888/*
2889 * Check if the current ops references the record.
2890 *
2891 * If the ops traces all functions, then it was already accounted for.
2892 * If the ops does not trace the current record function, skip it.
2893 * If the ops ignores the function via notrace filter, skip it.
2894 */
2895static inline bool
2896ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2897{
2898 /* If ops isn't enabled, ignore it */
2899 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2900 return 0;
2901
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002902 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002903 if (ops_traces_mod(ops))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002904 return 1;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002905
2906 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002907 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
Steven Rostedt (VMware)2b2c2792017-02-01 15:37:07 -05002908 !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002909 return 0;
2910
2911 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002912 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002913 return 0;
2914
2915 return 1;
2916}
2917
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002918static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002919{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002920 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002921 struct dyn_ftrace *p;
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +01002922 u64 start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002923 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002924 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002925 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002926
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002927 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002928
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002929 /*
2930 * When a module is loaded, this function is called to convert
2931 * the calls to mcount in its text to nops, and also to create
2932 * an entry in the ftrace data. Now, if ftrace is activated
2933 * after this call, but before the module sets its text to
2934 * read-only, the modification of enabling ftrace can fail if
2935 * the read-only is done while ftrace is converting the calls.
2936 * To prevent this, the module's records are set as disabled
2937 * and will be enabled after the call to set the module's text
2938 * to read-only.
2939 */
2940 if (mod)
2941 rec_flags |= FTRACE_FL_DISABLED;
2942
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002943 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302944
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002945 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002946
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002947 /* If something went wrong, bail without enabling anything */
2948 if (unlikely(ftrace_disabled))
2949 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002950
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002951 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002952 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302953
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002954 /*
2955 * Do the initial record conversion from mcount jump
2956 * to the NOP instructions.
2957 */
2958 if (!ftrace_code_disable(mod, p))
2959 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002960
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002961 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002962 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002963 }
2964
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002965 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002966 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002967 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002968
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002969 return 0;
2970}
2971
Steven Rostedta7900872011-12-16 16:23:44 -05002972static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002973{
Steven Rostedta7900872011-12-16 16:23:44 -05002974 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002975 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002976
Steven Rostedta7900872011-12-16 16:23:44 -05002977 if (WARN_ON(!count))
2978 return -EINVAL;
2979
2980 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002981
2982 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002983 * We want to fill as much as possible. No more than a page
2984 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002985 */
Steven Rostedta7900872011-12-16 16:23:44 -05002986 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2987 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002988
Steven Rostedta7900872011-12-16 16:23:44 -05002989 again:
2990 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2991
2992 if (!pg->records) {
2993 /* if we can't allocate this size, try something smaller */
2994 if (!order)
2995 return -ENOMEM;
2996 order >>= 1;
2997 goto again;
2998 }
2999
3000 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3001 pg->size = cnt;
3002
3003 if (cnt > count)
3004 cnt = count;
3005
3006 return cnt;
3007}
3008
3009static struct ftrace_page *
3010ftrace_allocate_pages(unsigned long num_to_init)
3011{
3012 struct ftrace_page *start_pg;
3013 struct ftrace_page *pg;
3014 int order;
3015 int cnt;
3016
3017 if (!num_to_init)
3018 return 0;
3019
3020 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3021 if (!pg)
3022 return NULL;
3023
3024 /*
3025 * Try to allocate as much as possible in one continues
3026 * location that fills in all of the space. We want to
3027 * waste as little space as possible.
3028 */
3029 for (;;) {
3030 cnt = ftrace_allocate_records(pg, num_to_init);
3031 if (cnt < 0)
3032 goto free_pages;
3033
3034 num_to_init -= cnt;
3035 if (!num_to_init)
3036 break;
3037
3038 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3039 if (!pg->next)
3040 goto free_pages;
3041
3042 pg = pg->next;
3043 }
3044
3045 return start_pg;
3046
3047 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003048 pg = start_pg;
3049 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003050 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3051 free_pages((unsigned long)pg->records, order);
3052 start_pg = pg->next;
3053 kfree(pg);
3054 pg = start_pg;
3055 }
3056 pr_info("ftrace: FAILED to allocate memory for functions\n");
3057 return NULL;
3058}
3059
Steven Rostedt5072c592008-05-12 21:20:43 +02003060#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3061
3062struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003063 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003064 loff_t func_pos;
3065 struct ftrace_page *pg;
3066 struct dyn_ftrace *func;
3067 struct ftrace_func_probe *probe;
3068 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003069 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003070 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003071 int hidx;
3072 int idx;
3073 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003074};
3075
Ingo Molnare309b412008-05-12 21:20:51 +02003076static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003077t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003078{
3079 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003080 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003081 struct hlist_head *hhd;
3082
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003083 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003084 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003085
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003086 if (iter->probe)
3087 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003088 retry:
3089 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3090 return NULL;
3091
3092 hhd = &ftrace_func_hash[iter->hidx];
3093
3094 if (hlist_empty(hhd)) {
3095 iter->hidx++;
3096 hnd = NULL;
3097 goto retry;
3098 }
3099
3100 if (!hnd)
3101 hnd = hhd->first;
3102 else {
3103 hnd = hnd->next;
3104 if (!hnd) {
3105 iter->hidx++;
3106 goto retry;
3107 }
3108 }
3109
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003110 if (WARN_ON_ONCE(!hnd))
3111 return NULL;
3112
3113 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3114
3115 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003116}
3117
3118static void *t_hash_start(struct seq_file *m, loff_t *pos)
3119{
3120 struct ftrace_iterator *iter = m->private;
3121 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003122 loff_t l;
3123
Steven Rostedt69a30832011-12-19 15:21:16 -05003124 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3125 return NULL;
3126
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003127 if (iter->func_pos > *pos)
3128 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003129
Li Zefand82d6242009-06-24 09:54:54 +08003130 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003131 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003132 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003133 if (!p)
3134 break;
3135 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003136 if (!p)
3137 return NULL;
3138
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003139 /* Only set this if we have an item */
3140 iter->flags |= FTRACE_ITER_HASH;
3141
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003142 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003143}
3144
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003145static int
3146t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003147{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003148 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003149
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003150 rec = iter->probe;
3151 if (WARN_ON_ONCE(!rec))
3152 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003153
Steven Rostedt809dcf22009-02-16 23:06:01 -05003154 if (rec->ops->print)
3155 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3156
Steven Rostedt (VMware)02b77e22017-04-04 10:04:26 -04003157 seq_printf(m, "%ps:%ps\n", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003158
3159 return 0;
3160}
3161
3162static void *
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003163t_func_next(struct seq_file *m, loff_t *pos)
Steven Rostedt5072c592008-05-12 21:20:43 +02003164{
3165 struct ftrace_iterator *iter = m->private;
3166 struct dyn_ftrace *rec = NULL;
3167
3168 (*pos)++;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003169
Steven Rostedt5072c592008-05-12 21:20:43 +02003170 retry:
3171 if (iter->idx >= iter->pg->index) {
3172 if (iter->pg->next) {
3173 iter->pg = iter->pg->next;
3174 iter->idx = 0;
3175 goto retry;
3176 }
3177 } else {
3178 rec = &iter->pg->records[iter->idx++];
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003179 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3180 !ftrace_lookup_ip(iter->hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003181
3182 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003183 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003184
Steven Rostedt5072c592008-05-12 21:20:43 +02003185 rec = NULL;
3186 goto retry;
3187 }
3188 }
3189
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003190 if (!rec)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003191 return NULL;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003192
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003193 iter->pos = iter->func_pos = *pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003194 iter->func = rec;
3195
3196 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003197}
3198
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003199static void *
3200t_next(struct seq_file *m, void *v, loff_t *pos)
3201{
3202 struct ftrace_iterator *iter = m->private;
Steven Rostedt (VMware)fcdc7122017-04-17 10:22:29 -04003203 loff_t l = *pos; /* t_hash_start() must use original pos */
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003204 void *ret;
3205
3206 if (unlikely(ftrace_disabled))
3207 return NULL;
3208
3209 if (iter->flags & FTRACE_ITER_HASH)
3210 return t_hash_next(m, pos);
3211
3212 if (iter->flags & FTRACE_ITER_PRINTALL) {
3213 /* next must increment pos, and t_hash_start does not */
3214 (*pos)++;
Steven Rostedt (VMware)fcdc7122017-04-17 10:22:29 -04003215 return t_hash_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003216 }
3217
3218 ret = t_func_next(m, pos);
3219
3220 if (!ret)
Steven Rostedt (VMware)fcdc7122017-04-17 10:22:29 -04003221 return t_hash_start(m, &l);
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003222
3223 return ret;
3224}
3225
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003226static void reset_iter_read(struct ftrace_iterator *iter)
3227{
3228 iter->pos = 0;
3229 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03003230 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003231}
3232
3233static void *t_start(struct seq_file *m, loff_t *pos)
3234{
3235 struct ftrace_iterator *iter = m->private;
3236 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003237 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003238
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003239 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003240
3241 if (unlikely(ftrace_disabled))
3242 return NULL;
3243
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003244 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003245 * If an lseek was done, then reset and start from beginning.
3246 */
3247 if (*pos < iter->pos)
3248 reset_iter_read(iter);
3249
3250 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003251 * For set_ftrace_filter reading, if we have the filter
3252 * off, we can short cut and just print out that all
3253 * functions are enabled.
3254 */
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003255 if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3256 ftrace_hash_empty(iter->hash)) {
Steven Rostedt (VMware)43ff9262017-03-30 16:51:43 -04003257 iter->func_pos = 1; /* Account for the message */
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003258 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003259 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003260 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003261 /* reset in case of seek/pread */
3262 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003263 return iter;
3264 }
3265
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003266 if (iter->flags & FTRACE_ITER_HASH)
3267 return t_hash_start(m, pos);
3268
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003269 /*
3270 * Unfortunately, we need to restart at ftrace_pages_start
3271 * every time we let go of the ftrace_mutex. This is because
3272 * those pointers can change without the lock.
3273 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003274 iter->pg = ftrace_pages_start;
3275 iter->idx = 0;
3276 for (l = 0; l <= *pos; ) {
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003277 p = t_func_next(m, &l);
Li Zefan694ce0a2009-06-24 09:54:19 +08003278 if (!p)
3279 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003280 }
walimis5821e1b2008-11-15 15:19:06 +08003281
Steven Rostedt69a30832011-12-19 15:21:16 -05003282 if (!p)
Steven Rostedt (VMware)5bd84622017-03-29 22:45:18 -04003283 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003284
3285 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003286}
3287
3288static void t_stop(struct seq_file *m, void *p)
3289{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003290 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003291}
3292
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003293void * __weak
3294arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3295{
3296 return NULL;
3297}
3298
3299static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3300 struct dyn_ftrace *rec)
3301{
3302 void *ptr;
3303
3304 ptr = arch_ftrace_trampoline_func(ops, rec);
3305 if (ptr)
3306 seq_printf(m, " ->%pS", ptr);
3307}
3308
Steven Rostedt5072c592008-05-12 21:20:43 +02003309static int t_show(struct seq_file *m, void *v)
3310{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003311 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003312 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003313
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003314 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003315 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003316
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003317 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003318 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003319 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003320 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003321 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003322 return 0;
3323 }
3324
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003325 rec = iter->func;
3326
Steven Rostedt5072c592008-05-12 21:20:43 +02003327 if (!rec)
3328 return 0;
3329
Steven Rostedt647bcd02011-05-03 14:39:21 -04003330 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003331 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003332 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003333
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003334 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003335 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003336 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3337 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003338 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003339 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003340 if (ops) {
3341 do {
3342 seq_printf(m, "\ttramp: %pS (%pS)",
3343 (void *)ops->trampoline,
3344 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003345 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003346 ops = ftrace_find_tramp_ops_next(rec, ops);
3347 } while (ops);
3348 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003349 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003350 } else {
3351 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003352 }
3353 }
3354
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003355 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003356
3357 return 0;
3358}
3359
James Morris88e9d342009-09-22 16:43:43 -07003360static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003361 .start = t_start,
3362 .next = t_next,
3363 .stop = t_stop,
3364 .show = t_show,
3365};
3366
Ingo Molnare309b412008-05-12 21:20:51 +02003367static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003368ftrace_avail_open(struct inode *inode, struct file *file)
3369{
3370 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003371
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003372 if (unlikely(ftrace_disabled))
3373 return -ENODEV;
3374
Jiri Olsa50e18b92012-04-25 10:23:39 +02003375 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003376 if (!iter)
3377 return -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003378
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003379 iter->pg = ftrace_pages_start;
3380 iter->ops = &global_ops;
3381
3382 return 0;
Steven Rostedt5072c592008-05-12 21:20:43 +02003383}
3384
Steven Rostedt647bcd02011-05-03 14:39:21 -04003385static int
3386ftrace_enabled_open(struct inode *inode, struct file *file)
3387{
3388 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003389
Jiri Olsa50e18b92012-04-25 10:23:39 +02003390 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003391 if (!iter)
3392 return -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003393
Steven Rostedt (VMware)c1bc5912017-03-29 11:38:13 -04003394 iter->pg = ftrace_pages_start;
3395 iter->flags = FTRACE_ITER_ENABLED;
3396 iter->ops = &global_ops;
3397
3398 return 0;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003399}
3400
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003401/**
3402 * ftrace_regex_open - initialize function tracer filter files
3403 * @ops: The ftrace_ops that hold the hash filters
3404 * @flag: The type of filter to process
3405 * @inode: The inode, usually passed in to your open routine
3406 * @file: The file, usually passed in to your open routine
3407 *
3408 * ftrace_regex_open() initializes the filter files for the
3409 * @ops. Depending on @flag it may process the filter hash or
3410 * the notrace hash of @ops. With this called from the open
3411 * routine, you can use ftrace_filter_write() for the write
3412 * routine if @flag has FTRACE_ITER_FILTER set, or
3413 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003414 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003415 * release must call ftrace_regex_release().
3416 */
3417int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003418ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003419 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003420{
3421 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003422 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003423 int ret = 0;
3424
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003425 ftrace_ops_init(ops);
3426
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003427 if (unlikely(ftrace_disabled))
3428 return -ENODEV;
3429
Steven Rostedt5072c592008-05-12 21:20:43 +02003430 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3431 if (!iter)
3432 return -ENOMEM;
3433
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003434 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3435 kfree(iter);
3436 return -ENOMEM;
3437 }
3438
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003439 iter->ops = ops;
3440 iter->flags = flag;
3441
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003442 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003443
Steven Rostedtf45948e2011-05-02 12:29:25 -04003444 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003445 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003446 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003447 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003448
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003449 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003450 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3451
3452 if (file->f_flags & O_TRUNC)
3453 iter->hash = alloc_ftrace_hash(size_bits);
3454 else
3455 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3456
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003457 if (!iter->hash) {
3458 trace_parser_put(&iter->parser);
3459 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003460 ret = -ENOMEM;
3461 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003462 }
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04003463 } else
3464 iter->hash = hash;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003465
Steven Rostedt5072c592008-05-12 21:20:43 +02003466 if (file->f_mode & FMODE_READ) {
3467 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003468
3469 ret = seq_open(file, &show_ftrace_seq_ops);
3470 if (!ret) {
3471 struct seq_file *m = file->private_data;
3472 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003473 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003474 /* Failed */
3475 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003476 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003477 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003478 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003479 } else
3480 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003481
3482 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003483 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003484
3485 return ret;
3486}
3487
Steven Rostedt41c52c02008-05-22 11:46:33 -04003488static int
3489ftrace_filter_open(struct inode *inode, struct file *file)
3490{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003491 struct ftrace_ops *ops = inode->i_private;
3492
3493 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003494 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3495 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003496}
3497
3498static int
3499ftrace_notrace_open(struct inode *inode, struct file *file)
3500{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003501 struct ftrace_ops *ops = inode->i_private;
3502
3503 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003504 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003505}
3506
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003507/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3508struct ftrace_glob {
3509 char *search;
3510 unsigned len;
3511 int type;
3512};
3513
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003514/*
3515 * If symbols in an architecture don't correspond exactly to the user-visible
3516 * name of what they represent, it is possible to define this function to
3517 * perform the necessary adjustments.
3518*/
3519char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3520{
3521 return str;
3522}
3523
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003524static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003525{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003526 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003527 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003528
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003529 str = arch_ftrace_match_adjust(str, g->search);
3530
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003531 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003532 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003533 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003534 matched = 1;
3535 break;
3536 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003537 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003538 matched = 1;
3539 break;
3540 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003541 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003542 matched = 1;
3543 break;
3544 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003545 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003546 if (slen >= g->len &&
3547 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003548 matched = 1;
3549 break;
Masami Hiramatsu60f1d5e2016-10-05 20:58:15 +09003550 case MATCH_GLOB:
3551 if (glob_match(g->search, str))
3552 matched = 1;
3553 break;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003554 }
3555
3556 return matched;
3557}
3558
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003559static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003560enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003561{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003562 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003563 int ret = 0;
3564
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003565 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003566 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003567 /* Do nothing if it doesn't exist */
3568 if (!entry)
3569 return 0;
3570
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003571 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003572 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003573 /* Do nothing if it exists */
3574 if (entry)
3575 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003576
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003577 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003578 }
3579 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003580}
3581
Steven Rostedt64e7c442009-02-13 17:08:48 -05003582static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003583ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3584 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003585{
3586 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003587 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003588
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003589 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3590
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003591 if (mod_g) {
3592 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3593
3594 /* blank module name to match all modules */
3595 if (!mod_g->len) {
3596 /* blank module globbing: modname xor exclude_mod */
3597 if ((!exclude_mod) != (!modname))
3598 goto func_match;
3599 return 0;
3600 }
3601
3602 /* not matching the module */
3603 if (!modname || !mod_matches) {
3604 if (exclude_mod)
3605 goto func_match;
3606 else
3607 return 0;
3608 }
3609
3610 if (mod_matches && exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003611 return 0;
3612
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003613func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003614 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003615 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003616 return 1;
3617 }
3618
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003619 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003620}
3621
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003622static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003623match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003624{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003625 struct ftrace_page *pg;
3626 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003627 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003628 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3629 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3630 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003631 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003632 int ret;
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003633 int clear_filter;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003634
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003635 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003636 func_g.type = filter_parse_regex(func, len, &func_g.search,
3637 &clear_filter);
3638 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003639 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003640
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003641 if (mod) {
3642 mod_g.type = filter_parse_regex(mod, strlen(mod),
3643 &mod_g.search, &exclude_mod);
3644 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003645 }
3646
Steven Rostedt52baf112009-02-14 01:15:39 -05003647 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003648
3649 if (unlikely(ftrace_disabled))
3650 goto out_unlock;
3651
Steven Rostedt265c8312009-02-13 12:43:56 -05003652 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003653
3654 if (rec->flags & FTRACE_FL_DISABLED)
3655 continue;
3656
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003657 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003658 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003659 if (ret < 0) {
3660 found = ret;
3661 goto out_unlock;
3662 }
Li Zefan311d16d2009-12-08 11:15:11 +08003663 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003664 }
3665 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003666 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003667 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003668
3669 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003670}
3671
Steven Rostedt64e7c442009-02-13 17:08:48 -05003672static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003673ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003674{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003675 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003676}
3677
Steven Rostedt64e7c442009-02-13 17:08:48 -05003678
Steven Rostedtf6180772009-02-14 00:40:25 -05003679/*
3680 * We register the module command as a template to show others how
3681 * to register the a command as well.
3682 */
3683
3684static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003685ftrace_mod_callback(struct ftrace_hash *hash,
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003686 char *func, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003687{
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003688 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003689
3690 /*
3691 * cmd == 'mod' because we only registered this func
3692 * for the 'mod' ftrace_func_command.
3693 * But if you register one func with multiple commands,
3694 * you can tell which command was used by the cmd
3695 * parameter.
3696 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003697 ret = match_records(hash, func, strlen(func), module);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003698 if (!ret)
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003699 return -EINVAL;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003700 if (ret < 0)
3701 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003702 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003703}
3704
3705static struct ftrace_func_command ftrace_mod_cmd = {
3706 .name = "mod",
3707 .func = ftrace_mod_callback,
3708};
3709
3710static int __init ftrace_mod_cmd_init(void)
3711{
3712 return register_ftrace_command(&ftrace_mod_cmd);
3713}
Steven Rostedt6f415672012-10-05 12:13:07 -04003714core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003715
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003716static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003717 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003718{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003719 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003720 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003721 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003722
3723 key = hash_long(ip, FTRACE_HASH_BITS);
3724
3725 hhd = &ftrace_func_hash[key];
3726
3727 if (hlist_empty(hhd))
3728 return;
3729
3730 /*
3731 * Disable preemption for these calls to prevent a RCU grace
3732 * period. This syncs the hash iteration and freeing of items
3733 * on the hash. rcu_read_lock is too dangerous here.
3734 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003735 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003736 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003737 if (entry->ip == ip)
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04003738 entry->ops->func(ip, parent_ip, entry->ops, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003739 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003740 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003741}
3742
Steven Rostedtb6887d72009-02-17 12:32:04 -05003743static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003744{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003745 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003746 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003747 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003748};
3749
Steven Rostedtb6887d72009-02-17 12:32:04 -05003750static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003751
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003752static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003753{
Steven Rostedtb8489142011-05-04 09:27:52 -04003754 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003755 int i;
3756
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003757 if (ftrace_probe_registered) {
3758 /* still need to update the function call sites */
3759 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003760 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3761 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003762 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003763 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003764
3765 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3766 struct hlist_head *hhd = &ftrace_func_hash[i];
3767 if (hhd->first)
3768 break;
3769 }
3770 /* Nothing registered? */
3771 if (i == FTRACE_FUNC_HASHSIZE)
3772 return;
3773
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003774 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003775
Steven Rostedtb6887d72009-02-17 12:32:04 -05003776 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003777}
3778
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04003779static bool __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003780{
3781 int i;
3782
Steven Rostedtb6887d72009-02-17 12:32:04 -05003783 if (!ftrace_probe_registered)
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04003784 return false;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003785
3786 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3787 struct hlist_head *hhd = &ftrace_func_hash[i];
3788 if (hhd->first)
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04003789 return false;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003790 }
3791
3792 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003793 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003794
Steven Rostedtb6887d72009-02-17 12:32:04 -05003795 ftrace_probe_registered = 0;
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04003796 return true;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003797}
3798
3799
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003800static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003801{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003802 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003803 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003804 kfree(entry);
3805}
3806
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04003807struct ftrace_func_map {
3808 struct ftrace_func_entry entry;
3809 void *data;
3810};
3811
3812struct ftrace_func_mapper {
3813 struct ftrace_hash hash;
3814};
3815
3816/**
3817 * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
3818 *
3819 * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
3820 */
3821struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
3822{
3823 struct ftrace_hash *hash;
3824
3825 /*
3826 * The mapper is simply a ftrace_hash, but since the entries
3827 * in the hash are not ftrace_func_entry type, we define it
3828 * as a separate structure.
3829 */
3830 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3831 return (struct ftrace_func_mapper *)hash;
3832}
3833
3834/**
3835 * ftrace_func_mapper_find_ip - Find some data mapped to an ip
3836 * @mapper: The mapper that has the ip maps
3837 * @ip: the instruction pointer to find the data for
3838 *
3839 * Returns the data mapped to @ip if found otherwise NULL. The return
3840 * is actually the address of the mapper data pointer. The address is
3841 * returned for use cases where the data is no bigger than a long, and
3842 * the user can use the data pointer as its data instead of having to
3843 * allocate more memory for the reference.
3844 */
3845void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
3846 unsigned long ip)
3847{
3848 struct ftrace_func_entry *entry;
3849 struct ftrace_func_map *map;
3850
3851 entry = ftrace_lookup_ip(&mapper->hash, ip);
3852 if (!entry)
3853 return NULL;
3854
3855 map = (struct ftrace_func_map *)entry;
3856 return &map->data;
3857}
3858
3859/**
3860 * ftrace_func_mapper_add_ip - Map some data to an ip
3861 * @mapper: The mapper that has the ip maps
3862 * @ip: The instruction pointer address to map @data to
3863 * @data: The data to map to @ip
3864 *
3865 * Returns 0 on succes otherwise an error.
3866 */
3867int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
3868 unsigned long ip, void *data)
3869{
3870 struct ftrace_func_entry *entry;
3871 struct ftrace_func_map *map;
3872
3873 entry = ftrace_lookup_ip(&mapper->hash, ip);
3874 if (entry)
3875 return -EBUSY;
3876
3877 map = kmalloc(sizeof(*map), GFP_KERNEL);
3878 if (!map)
3879 return -ENOMEM;
3880
3881 map->entry.ip = ip;
3882 map->data = data;
3883
3884 __add_hash_entry(&mapper->hash, &map->entry);
3885
3886 return 0;
3887}
3888
3889/**
3890 * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
3891 * @mapper: The mapper that has the ip maps
3892 * @ip: The instruction pointer address to remove the data from
3893 *
3894 * Returns the data if it is found, otherwise NULL.
3895 * Note, if the data pointer is used as the data itself, (see
3896 * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
3897 * if the data pointer was set to zero.
3898 */
3899void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
3900 unsigned long ip)
3901{
3902 struct ftrace_func_entry *entry;
3903 struct ftrace_func_map *map;
3904 void *data;
3905
3906 entry = ftrace_lookup_ip(&mapper->hash, ip);
3907 if (!entry)
3908 return NULL;
3909
3910 map = (struct ftrace_func_map *)entry;
3911 data = map->data;
3912
3913 remove_hash_entry(&mapper->hash, entry);
3914 kfree(entry);
3915
3916 return data;
3917}
3918
3919/**
3920 * free_ftrace_func_mapper - free a mapping of ips and data
3921 * @mapper: The mapper that has the ip maps
3922 * @free_func: A function to be called on each data item.
3923 *
3924 * This is used to free the function mapper. The @free_func is optional
3925 * and can be used if the data needs to be freed as well.
3926 */
3927void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
3928 ftrace_mapper_func free_func)
3929{
3930 struct ftrace_func_entry *entry;
3931 struct ftrace_func_map *map;
3932 struct hlist_head *hhd;
3933 int size = 1 << mapper->hash.size_bits;
3934 int i;
3935
3936 if (free_func && mapper->hash.count) {
3937 for (i = 0; i < size; i++) {
3938 hhd = &mapper->hash.buckets[i];
3939 hlist_for_each_entry(entry, hhd, hlist) {
3940 map = (struct ftrace_func_map *)entry;
3941 free_func(map);
3942 }
3943 }
3944 }
3945 free_ftrace_hash(&mapper->hash);
3946}
3947
Steven Rostedt59df055f2009-02-14 15:29:06 -05003948int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003949register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003950 void *data)
3951{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003952 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003953 struct ftrace_func_probe *entry;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003954 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003955 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003956 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003957 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003958 struct ftrace_page *pg;
3959 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003960 int not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003961 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003962 int count = 0;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003963 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003964
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003965 func_g.type = filter_parse_regex(glob, strlen(glob),
3966 &func_g.search, &not);
3967 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003968
Steven Rostedtb6887d72009-02-17 12:32:04 -05003969 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003970 if (WARN_ON(not))
3971 return -EINVAL;
3972
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003973 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003974
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003975 old_hash_ops.filter_hash = old_hash;
3976 /* Probes only have filters */
3977 old_hash_ops.notrace_hash = NULL;
3978
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003979 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003980 if (!hash) {
3981 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003982 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003983 }
3984
3985 if (unlikely(ftrace_disabled)) {
3986 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003987 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003988 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003989
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003990 mutex_lock(&ftrace_lock);
3991
Steven Rostedt59df055f2009-02-14 15:29:06 -05003992 do_for_each_ftrace_rec(pg, rec) {
3993
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003994 if (rec->flags & FTRACE_FL_DISABLED)
3995 continue;
3996
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003997 if (!ftrace_match_record(rec, &func_g, NULL, 0))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003998 continue;
3999
4000 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
4001 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05004002 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004003 if (!count)
4004 count = -ENOMEM;
4005 goto out_unlock;
4006 }
4007
4008 count++;
4009
4010 entry->data = data;
4011
4012 /*
4013 * The caller might want to do something special
4014 * for each function we find. We call the callback
4015 * to give the caller an opportunity to do so.
4016 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04004017 if (ops->init) {
4018 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004019 /* caller does not like this func */
4020 kfree(entry);
4021 continue;
4022 }
4023 }
4024
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004025 ret = enter_record(hash, rec, 0);
4026 if (ret < 0) {
4027 kfree(entry);
4028 count = ret;
4029 goto out_unlock;
4030 }
4031
Steven Rostedt59df055f2009-02-14 15:29:06 -05004032 entry->ops = ops;
4033 entry->ip = rec->ip;
4034
4035 key = hash_long(entry->ip, FTRACE_HASH_BITS);
4036 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
4037
4038 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004039
4040 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004041
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004042 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004043
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004044 if (!ret)
4045 free_ftrace_hash_rcu(old_hash);
4046 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004047 count = ret;
4048
Steven Rostedt59df055f2009-02-14 15:29:06 -05004049 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04004050 mutex_unlock(&ftrace_lock);
4051 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004052 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004053 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004054
4055 return count;
4056}
4057
Steven Rostedt (VMware)78f78e02017-04-04 13:39:59 -04004058void
4059unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004060{
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004061 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004062 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05004063 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004064 struct ftrace_func_probe *p;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004065 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004066 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004067 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004068 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004069 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004070 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004071 char str[KSYM_SYMBOL_LEN];
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004072 int i, ret;
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004073 bool disabled;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004074
Atsushi Tsujib36461d2009-09-15 19:06:30 +09004075 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004076 func_g.search = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09004077 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004078 int not;
4079
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004080 func_g.type = filter_parse_regex(glob, strlen(glob),
4081 &func_g.search, &not);
4082 func_g.len = strlen(func_g.search);
4083 func_g.search = glob;
Steven Rostedt59df055f2009-02-14 15:29:06 -05004084
Steven Rostedtb6887d72009-02-17 12:32:04 -05004085 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05004086 if (WARN_ON(not))
4087 return;
4088 }
4089
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004090 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004091
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004092 old_hash_ops.filter_hash = old_hash;
4093 /* Probes only have filters */
4094 old_hash_ops.notrace_hash = NULL;
4095
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004096 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4097 if (!hash)
4098 /* Hmm, should report this somehow */
4099 goto out_unlock;
4100
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004101 INIT_LIST_HEAD(&free_list);
4102
Steven Rostedt59df055f2009-02-14 15:29:06 -05004103 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
4104 struct hlist_head *hhd = &ftrace_func_hash[i];
4105
Sasha Levinb67bfe02013-02-27 17:06:00 -08004106 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004107
4108 /* break up if statements for readability */
Steven Rostedt (VMware)78f78e02017-04-04 13:39:59 -04004109 if (entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004110 continue;
4111
Steven Rostedt59df055f2009-02-14 15:29:06 -05004112 /* do this last, since it is the most expensive */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004113 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05004114 kallsyms_lookup(entry->ip, NULL, NULL,
4115 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004116 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05004117 continue;
4118 }
4119
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004120 rec_entry = ftrace_lookup_ip(hash, entry->ip);
4121 /* It is possible more than one entry had this ip */
4122 if (rec_entry)
4123 free_hash_entry(hash, rec_entry);
4124
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04004125 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004126 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004127 }
4128 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004129 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004130 disabled = __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004131 /*
4132 * Remove after the disable is called. Otherwise, if the last
4133 * probe is removed, a null hash means *all enabled*.
4134 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004135 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (VMware)acceb722017-04-14 17:45:45 -04004136
4137 /* still need to update the function call sites */
4138 if (ftrace_enabled && !disabled)
4139 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
4140 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004141 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004142 if (!ret)
4143 free_ftrace_hash_rcu(old_hash);
4144
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04004145 list_for_each_entry_safe(entry, p, &free_list, free_list) {
4146 list_del(&entry->free_list);
4147 ftrace_free_entry(entry);
4148 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004149 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004150
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004151 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004152 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04004153 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004154}
4155
Steven Rostedtf6180772009-02-14 00:40:25 -05004156static LIST_HEAD(ftrace_commands);
4157static DEFINE_MUTEX(ftrace_cmd_mutex);
4158
Tom Zanussi38de93a2013-10-24 08:34:18 -05004159/*
4160 * Currently we only register ftrace commands from __init, so mark this
4161 * __init too.
4162 */
4163__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004164{
4165 struct ftrace_func_command *p;
4166 int ret = 0;
4167
4168 mutex_lock(&ftrace_cmd_mutex);
4169 list_for_each_entry(p, &ftrace_commands, list) {
4170 if (strcmp(cmd->name, p->name) == 0) {
4171 ret = -EBUSY;
4172 goto out_unlock;
4173 }
4174 }
4175 list_add(&cmd->list, &ftrace_commands);
4176 out_unlock:
4177 mutex_unlock(&ftrace_cmd_mutex);
4178
4179 return ret;
4180}
4181
Tom Zanussi38de93a2013-10-24 08:34:18 -05004182/*
4183 * Currently we only unregister ftrace commands from __init, so mark
4184 * this __init too.
4185 */
4186__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004187{
4188 struct ftrace_func_command *p, *n;
4189 int ret = -ENODEV;
4190
4191 mutex_lock(&ftrace_cmd_mutex);
4192 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4193 if (strcmp(cmd->name, p->name) == 0) {
4194 ret = 0;
4195 list_del_init(&p->list);
4196 goto out_unlock;
4197 }
4198 }
4199 out_unlock:
4200 mutex_unlock(&ftrace_cmd_mutex);
4201
4202 return ret;
4203}
4204
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004205static int ftrace_process_regex(struct ftrace_hash *hash,
4206 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004207{
Steven Rostedtf6180772009-02-14 00:40:25 -05004208 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004209 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004210 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004211
4212 func = strsep(&next, ":");
4213
4214 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004215 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004216 if (!ret)
4217 ret = -EINVAL;
4218 if (ret < 0)
4219 return ret;
4220 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004221 }
4222
Steven Rostedtf6180772009-02-14 00:40:25 -05004223 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004224
4225 command = strsep(&next, ":");
4226
Steven Rostedtf6180772009-02-14 00:40:25 -05004227 mutex_lock(&ftrace_cmd_mutex);
4228 list_for_each_entry(p, &ftrace_commands, list) {
4229 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04004230 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004231 goto out_unlock;
4232 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004233 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004234 out_unlock:
4235 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004236
Steven Rostedtf6180772009-02-14 00:40:25 -05004237 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004238}
4239
Ingo Molnare309b412008-05-12 21:20:51 +02004240static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004241ftrace_regex_write(struct file *file, const char __user *ubuf,
4242 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004243{
4244 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004245 struct trace_parser *parser;
4246 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004247
Li Zefan4ba79782009-09-22 13:52:20 +08004248 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004249 return 0;
4250
Steven Rostedt5072c592008-05-12 21:20:43 +02004251 if (file->f_mode & FMODE_READ) {
4252 struct seq_file *m = file->private_data;
4253 iter = m->private;
4254 } else
4255 iter = file->private_data;
4256
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004257 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004258 return -ENODEV;
4259
4260 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004261
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004262 parser = &iter->parser;
4263 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004264
Li Zefan4ba79782009-09-22 13:52:20 +08004265 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004266 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004267 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004268 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004269 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004270 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004271 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004272 }
4273
Steven Rostedt5072c592008-05-12 21:20:43 +02004274 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004275 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004276 return ret;
4277}
4278
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004279ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004280ftrace_filter_write(struct file *file, const char __user *ubuf,
4281 size_t cnt, loff_t *ppos)
4282{
4283 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4284}
4285
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004286ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004287ftrace_notrace_write(struct file *file, const char __user *ubuf,
4288 size_t cnt, loff_t *ppos)
4289{
4290 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4291}
4292
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004293static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004294ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4295{
4296 struct ftrace_func_entry *entry;
4297
4298 if (!ftrace_location(ip))
4299 return -EINVAL;
4300
4301 if (remove) {
4302 entry = ftrace_lookup_ip(hash, ip);
4303 if (!entry)
4304 return -ENOENT;
4305 free_hash_entry(hash, entry);
4306 return 0;
4307 }
4308
4309 return add_hash_entry(hash, ip);
4310}
4311
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004312static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004313 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004314{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004315 struct ftrace_ops *op;
4316
4317 if (!ftrace_enabled)
4318 return;
4319
4320 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004321 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004322 return;
4323 }
4324
4325 /*
4326 * If this is the shared global_ops filter, then we need to
4327 * check if there is another ops that shares it, is enabled.
4328 * If so, we still need to run the modify code.
4329 */
4330 if (ops->func_hash != &global_ops.local_hash)
4331 return;
4332
4333 do_for_each_ftrace_op(op, ftrace_ops_list) {
4334 if (op->func_hash == &global_ops.local_hash &&
4335 op->flags & FTRACE_OPS_FL_ENABLED) {
4336 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4337 /* Only need to do this once */
4338 return;
4339 }
4340 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004341}
4342
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004343static int
4344ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4345 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004346{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004347 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004348 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004349 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004350 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004351 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004352
Steven Rostedt41c52c02008-05-22 11:46:33 -04004353 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004354 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004355
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004356 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004357
Steven Rostedtf45948e2011-05-02 12:29:25 -04004358 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004359 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004360 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004361 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004362
Wang Nanb972cc52014-07-15 08:40:20 +08004363 if (reset)
4364 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4365 else
4366 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4367
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004368 if (!hash) {
4369 ret = -ENOMEM;
4370 goto out_regex_unlock;
4371 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004372
Jiri Olsaac483c42012-01-02 10:04:14 +01004373 if (buf && !ftrace_match_records(hash, buf, len)) {
4374 ret = -EINVAL;
4375 goto out_regex_unlock;
4376 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004377 if (ip) {
4378 ret = ftrace_match_addr(hash, ip, remove);
4379 if (ret < 0)
4380 goto out_regex_unlock;
4381 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004382
4383 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004384 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004385 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4386 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004387 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004388 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004389 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004390 free_ftrace_hash_rcu(old_hash);
4391 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004392 mutex_unlock(&ftrace_lock);
4393
Jiri Olsaac483c42012-01-02 10:04:14 +01004394 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004395 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004396
4397 free_ftrace_hash(hash);
4398 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004399}
4400
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004401static int
4402ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4403 int reset, int enable)
4404{
4405 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4406}
4407
4408/**
4409 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4410 * @ops - the ops to set the filter with
4411 * @ip - the address to add to or remove from the filter.
4412 * @remove - non zero to remove the ip from the filter
4413 * @reset - non zero to reset all filters before applying this filter.
4414 *
4415 * Filters denote which functions should be enabled when tracing is enabled
4416 * If @ip is NULL, it failes to update filter.
4417 */
4418int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4419 int remove, int reset)
4420{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004421 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004422 return ftrace_set_addr(ops, ip, remove, reset, 1);
4423}
4424EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4425
Joel Fernandesd032ae82016-11-15 12:31:20 -08004426/**
4427 * ftrace_ops_set_global_filter - setup ops to use global filters
4428 * @ops - the ops which will use the global filters
4429 *
4430 * ftrace users who need global function trace filtering should call this.
4431 * It can set the global filter only if ops were not initialized before.
4432 */
4433void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4434{
4435 if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4436 return;
4437
4438 ftrace_ops_init(ops);
4439 ops->func_hash = &global_ops.local_hash;
4440}
4441EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4442
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004443static int
4444ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4445 int reset, int enable)
4446{
4447 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4448}
4449
Steven Rostedt77a2b372008-05-12 21:20:45 +02004450/**
4451 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004452 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004453 * @buf - the string that holds the function filter text.
4454 * @len - the length of the string.
4455 * @reset - non zero to reset all filters before applying this filter.
4456 *
4457 * Filters denote which functions should be enabled when tracing is enabled.
4458 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4459 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004460int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004461 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004462{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004463 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004464 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004465}
Steven Rostedt936e0742011-05-05 22:54:01 -04004466EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004467
Steven Rostedt41c52c02008-05-22 11:46:33 -04004468/**
4469 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004470 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004471 * @buf - the string that holds the function notrace text.
4472 * @len - the length of the string.
4473 * @reset - non zero to reset all filters before applying this filter.
4474 *
4475 * Notrace Filters denote which functions should not be enabled when tracing
4476 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4477 * for tracing.
4478 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004479int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004480 int len, int reset)
4481{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004482 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004483 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004484}
4485EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4486/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004487 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004488 * @buf - the string that holds the function filter text.
4489 * @len - the length of the string.
4490 * @reset - non zero to reset all filters before applying this filter.
4491 *
4492 * Filters denote which functions should be enabled when tracing is enabled.
4493 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4494 */
4495void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4496{
4497 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4498}
4499EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4500
4501/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004502 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004503 * @buf - the string that holds the function notrace text.
4504 * @len - the length of the string.
4505 * @reset - non zero to reset all filters before applying this filter.
4506 *
4507 * Notrace Filters denote which functions should not be enabled when tracing
4508 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4509 * for tracing.
4510 */
4511void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004512{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004513 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004514}
Steven Rostedt936e0742011-05-05 22:54:01 -04004515EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004516
Steven Rostedt2af15d62009-05-28 13:37:24 -04004517/*
4518 * command line interface to allow users to set filters on boot up.
4519 */
4520#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4521static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4522static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4523
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004524/* Used by function selftest to not test if filter is set */
4525bool ftrace_filter_param __initdata;
4526
Steven Rostedt2af15d62009-05-28 13:37:24 -04004527static int __init set_ftrace_notrace(char *str)
4528{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004529 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004530 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004531 return 1;
4532}
4533__setup("ftrace_notrace=", set_ftrace_notrace);
4534
4535static int __init set_ftrace_filter(char *str)
4536{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004537 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004538 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004539 return 1;
4540}
4541__setup("ftrace_filter=", set_ftrace_filter);
4542
Stefan Assmann369bc182009-10-12 22:17:21 +02004543#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004544static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004545static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004546static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004547
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004548static unsigned long save_global_trampoline;
4549static unsigned long save_global_flags;
4550
Stefan Assmann369bc182009-10-12 22:17:21 +02004551static int __init set_graph_function(char *str)
4552{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004553 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004554 return 1;
4555}
4556__setup("ftrace_graph_filter=", set_graph_function);
4557
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004558static int __init set_graph_notrace_function(char *str)
4559{
4560 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4561 return 1;
4562}
4563__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4564
Todd Brandt65a50c652017-03-02 16:12:15 -08004565static int __init set_graph_max_depth_function(char *str)
4566{
4567 if (!str)
4568 return 0;
4569 fgraph_max_depth = simple_strtoul(str, NULL, 0);
4570 return 1;
4571}
4572__setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4573
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004574static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004575{
4576 int ret;
4577 char *func;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004578 struct ftrace_hash *hash;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004579
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004580 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4581 if (WARN_ON(!hash))
4582 return;
Stefan Assmann369bc182009-10-12 22:17:21 +02004583
4584 while (buf) {
4585 func = strsep(&buf, ",");
4586 /* we allow only one expression at a time */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004587 ret = ftrace_graph_set_hash(hash, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004588 if (ret)
4589 printk(KERN_DEBUG "ftrace: function %s not "
4590 "traceable\n", func);
4591 }
Steven Rostedt (VMware)92ad18e2017-03-02 12:53:26 -05004592
4593 if (enable)
4594 ftrace_graph_hash = hash;
4595 else
4596 ftrace_graph_notrace_hash = hash;
Stefan Assmann369bc182009-10-12 22:17:21 +02004597}
4598#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4599
Steven Rostedt2a85a372011-12-19 21:57:44 -05004600void __init
4601ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004602{
4603 char *func;
4604
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004605 ftrace_ops_init(ops);
4606
Steven Rostedt2af15d62009-05-28 13:37:24 -04004607 while (buf) {
4608 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004609 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004610 }
4611}
4612
4613static void __init set_ftrace_early_filters(void)
4614{
4615 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004616 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004617 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004618 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004619#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4620 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004621 set_ftrace_early_graph(ftrace_graph_buf, 1);
4622 if (ftrace_graph_notrace_buf[0])
4623 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004624#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004625}
4626
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004627int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004628{
4629 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004630 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004631 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004632 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004633 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004634 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004635 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004636 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004637
Steven Rostedt5072c592008-05-12 21:20:43 +02004638 if (file->f_mode & FMODE_READ) {
4639 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004640 seq_release(inode, file);
4641 } else
4642 iter = file->private_data;
4643
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004644 parser = &iter->parser;
4645 if (trace_parser_loaded(parser)) {
4646 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004647 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004648 }
4649
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004650 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004651
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004652 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004653
Steven Rostedt058e2972011-04-29 22:35:33 -04004654 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004655 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4656
4657 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004658 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004659 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004660 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004661
Steven Rostedt058e2972011-04-29 22:35:33 -04004662 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004663 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004664 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4665 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004666 ret = ftrace_hash_move(iter->ops, filter_hash,
4667 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004668 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004669 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004670 free_ftrace_hash_rcu(old_hash);
4671 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004672 mutex_unlock(&ftrace_lock);
Steven Rostedt (VMware)c20489d2017-03-29 14:55:49 -04004673 } else {
4674 /* For read only, the hash is the ops hash */
4675 iter->hash = NULL;
Steven Rostedt058e2972011-04-29 22:35:33 -04004676 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004677
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004678 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004679 free_ftrace_hash(iter->hash);
4680 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004681
Steven Rostedt5072c592008-05-12 21:20:43 +02004682 return 0;
4683}
4684
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004685static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004686 .open = ftrace_avail_open,
4687 .read = seq_read,
4688 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004689 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004690};
4691
Steven Rostedt647bcd02011-05-03 14:39:21 -04004692static const struct file_operations ftrace_enabled_fops = {
4693 .open = ftrace_enabled_open,
4694 .read = seq_read,
4695 .llseek = seq_lseek,
4696 .release = seq_release_private,
4697};
4698
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004699static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004700 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004701 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004702 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004703 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004704 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004705};
4706
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004707static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004708 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004709 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004710 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004711 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004712 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004713};
4714
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004715#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4716
4717static DEFINE_MUTEX(graph_lock);
4718
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004719struct ftrace_hash *ftrace_graph_hash = EMPTY_HASH;
4720struct ftrace_hash *ftrace_graph_notrace_hash = EMPTY_HASH;
4721
4722enum graph_filter_type {
4723 GRAPH_FILTER_NOTRACE = 0,
4724 GRAPH_FILTER_FUNCTION,
4725};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004726
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004727#define FTRACE_GRAPH_EMPTY ((void *)1)
4728
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004729struct ftrace_graph_data {
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004730 struct ftrace_hash *hash;
4731 struct ftrace_func_entry *entry;
4732 int idx; /* for hash table iteration */
4733 enum graph_filter_type type;
4734 struct ftrace_hash *new_hash;
4735 const struct seq_operations *seq_ops;
4736 struct trace_parser parser;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004737};
4738
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004739static void *
Li Zefan85951842009-06-24 09:54:00 +08004740__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004741{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004742 struct ftrace_graph_data *fgd = m->private;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004743 struct ftrace_func_entry *entry = fgd->entry;
4744 struct hlist_head *head;
4745 int i, idx = fgd->idx;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004746
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004747 if (*pos >= fgd->hash->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004748 return NULL;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004749
4750 if (entry) {
4751 hlist_for_each_entry_continue(entry, hlist) {
4752 fgd->entry = entry;
4753 return entry;
4754 }
4755
4756 idx++;
4757 }
4758
4759 for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
4760 head = &fgd->hash->buckets[i];
4761 hlist_for_each_entry(entry, head, hlist) {
4762 fgd->entry = entry;
4763 fgd->idx = i;
4764 return entry;
4765 }
4766 }
4767 return NULL;
Li Zefan85951842009-06-24 09:54:00 +08004768}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004769
Li Zefan85951842009-06-24 09:54:00 +08004770static void *
4771g_next(struct seq_file *m, void *v, loff_t *pos)
4772{
4773 (*pos)++;
4774 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004775}
4776
4777static void *g_start(struct seq_file *m, loff_t *pos)
4778{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004779 struct ftrace_graph_data *fgd = m->private;
4780
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004781 mutex_lock(&graph_lock);
4782
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004783 if (fgd->type == GRAPH_FILTER_FUNCTION)
4784 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4785 lockdep_is_held(&graph_lock));
4786 else
4787 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4788 lockdep_is_held(&graph_lock));
4789
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004790 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004791 if (ftrace_hash_empty(fgd->hash) && !*pos)
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004792 return FTRACE_GRAPH_EMPTY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004793
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004794 fgd->idx = 0;
4795 fgd->entry = NULL;
Li Zefan85951842009-06-24 09:54:00 +08004796 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004797}
4798
4799static void g_stop(struct seq_file *m, void *p)
4800{
4801 mutex_unlock(&graph_lock);
4802}
4803
4804static int g_show(struct seq_file *m, void *v)
4805{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004806 struct ftrace_func_entry *entry = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004807
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004808 if (!entry)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004809 return 0;
4810
Steven Rostedt (VMware)555fc782017-02-02 10:15:22 -05004811 if (entry == FTRACE_GRAPH_EMPTY) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004812 struct ftrace_graph_data *fgd = m->private;
4813
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004814 if (fgd->type == GRAPH_FILTER_FUNCTION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004815 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004816 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004817 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004818 return 0;
4819 }
4820
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004821 seq_printf(m, "%ps\n", (void *)entry->ip);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004822
4823 return 0;
4824}
4825
James Morris88e9d342009-09-22 16:43:43 -07004826static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004827 .start = g_start,
4828 .next = g_next,
4829 .stop = g_stop,
4830 .show = g_show,
4831};
4832
4833static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004834__ftrace_graph_open(struct inode *inode, struct file *file,
4835 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004836{
4837 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004838 struct ftrace_hash *new_hash = NULL;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004839
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004840 if (file->f_mode & FMODE_WRITE) {
4841 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
4842
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004843 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
4844 return -ENOMEM;
4845
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004846 if (file->f_flags & O_TRUNC)
4847 new_hash = alloc_ftrace_hash(size_bits);
4848 else
4849 new_hash = alloc_and_copy_ftrace_hash(size_bits,
4850 fgd->hash);
4851 if (!new_hash) {
4852 ret = -ENOMEM;
4853 goto out;
4854 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004855 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004856
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004857 if (file->f_mode & FMODE_READ) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004858 ret = seq_open(file, &ftrace_graph_seq_ops);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004859 if (!ret) {
4860 struct seq_file *m = file->private_data;
4861 m->private = fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004862 } else {
4863 /* Failed */
4864 free_ftrace_hash(new_hash);
4865 new_hash = NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004866 }
4867 } else
4868 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004869
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004870out:
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004871 if (ret < 0 && file->f_mode & FMODE_WRITE)
4872 trace_parser_put(&fgd->parser);
4873
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004874 fgd->new_hash = new_hash;
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004875
4876 /*
4877 * All uses of fgd->hash must be taken with the graph_lock
4878 * held. The graph_lock is going to be released, so force
4879 * fgd->hash to be reinitialized when it is taken again.
4880 */
4881 fgd->hash = NULL;
4882
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004883 return ret;
4884}
4885
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004886static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004887ftrace_graph_open(struct inode *inode, struct file *file)
4888{
4889 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004890 int ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004891
4892 if (unlikely(ftrace_disabled))
4893 return -ENODEV;
4894
4895 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4896 if (fgd == NULL)
4897 return -ENOMEM;
4898
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004899 mutex_lock(&graph_lock);
4900
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004901 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
4902 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004903 fgd->type = GRAPH_FILTER_FUNCTION;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004904 fgd->seq_ops = &ftrace_graph_seq_ops;
4905
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004906 ret = __ftrace_graph_open(inode, file, fgd);
4907 if (ret < 0)
4908 kfree(fgd);
4909
4910 mutex_unlock(&graph_lock);
4911 return ret;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004912}
4913
4914static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004915ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4916{
4917 struct ftrace_graph_data *fgd;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004918 int ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004919
4920 if (unlikely(ftrace_disabled))
4921 return -ENODEV;
4922
4923 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4924 if (fgd == NULL)
4925 return -ENOMEM;
4926
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004927 mutex_lock(&graph_lock);
4928
Steven Rostedt (VMware)649b9882017-02-02 20:16:29 -05004929 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4930 lockdep_is_held(&graph_lock));
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004931 fgd->type = GRAPH_FILTER_NOTRACE;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004932 fgd->seq_ops = &ftrace_graph_seq_ops;
4933
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004934 ret = __ftrace_graph_open(inode, file, fgd);
4935 if (ret < 0)
4936 kfree(fgd);
4937
4938 mutex_unlock(&graph_lock);
4939 return ret;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004940}
4941
4942static int
Li Zefan87827112009-07-23 11:29:11 +08004943ftrace_graph_release(struct inode *inode, struct file *file)
4944{
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004945 struct ftrace_graph_data *fgd;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004946 struct ftrace_hash *old_hash, *new_hash;
4947 struct trace_parser *parser;
4948 int ret = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004949
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004950 if (file->f_mode & FMODE_READ) {
4951 struct seq_file *m = file->private_data;
4952
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004953 fgd = m->private;
Li Zefan87827112009-07-23 11:29:11 +08004954 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004955 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004956 fgd = file->private_data;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004957 }
4958
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05004959
4960 if (file->f_mode & FMODE_WRITE) {
4961
4962 parser = &fgd->parser;
4963
4964 if (trace_parser_loaded((parser))) {
4965 parser->buffer[parser->idx] = 0;
4966 ret = ftrace_graph_set_hash(fgd->new_hash,
4967 parser->buffer);
4968 }
4969
4970 trace_parser_put(parser);
4971
4972 new_hash = __ftrace_hash_move(fgd->new_hash);
4973 if (!new_hash) {
4974 ret = -ENOMEM;
4975 goto out;
4976 }
4977
4978 mutex_lock(&graph_lock);
4979
4980 if (fgd->type == GRAPH_FILTER_FUNCTION) {
4981 old_hash = rcu_dereference_protected(ftrace_graph_hash,
4982 lockdep_is_held(&graph_lock));
4983 rcu_assign_pointer(ftrace_graph_hash, new_hash);
4984 } else {
4985 old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
4986 lockdep_is_held(&graph_lock));
4987 rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
4988 }
4989
4990 mutex_unlock(&graph_lock);
4991
4992 /* Wait till all users are no longer using the old hash */
4993 synchronize_sched();
4994
4995 free_ftrace_hash(old_hash);
4996 }
4997
4998 out:
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09004999 kfree(fgd->new_hash);
5000 kfree(fgd);
5001
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005002 return ret;
Li Zefan87827112009-07-23 11:29:11 +08005003}
5004
5005static int
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005006ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005007{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005008 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005009 struct dyn_ftrace *rec;
5010 struct ftrace_page *pg;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005011 struct ftrace_func_entry *entry;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005012 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005013 int not;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005014
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005015 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005016 func_g.type = filter_parse_regex(buffer, strlen(buffer),
5017 &func_g.search, &not);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005018
Dmitry Safonov3ba00922015-09-29 19:46:14 +03005019 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01005020
Steven Rostedt52baf112009-02-14 01:15:39 -05005021 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005022
5023 if (unlikely(ftrace_disabled)) {
5024 mutex_unlock(&ftrace_lock);
5025 return -ENODEV;
5026 }
5027
Steven Rostedt265c8312009-02-13 12:43:56 -05005028 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005029
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05005030 if (rec->flags & FTRACE_FL_DISABLED)
5031 continue;
5032
Dmitry Safonov0b507e12015-09-29 19:46:15 +03005033 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005034 entry = ftrace_lookup_ip(hash, rec->ip);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005035
5036 if (!not) {
5037 fail = 0;
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005038
5039 if (entry)
5040 continue;
5041 if (add_hash_entry(hash, rec->ip) < 0)
5042 goto out;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005043 } else {
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005044 if (entry) {
5045 free_hash_entry(hash, entry);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005046 fail = 0;
5047 }
5048 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005049 }
Steven Rostedt265c8312009-02-13 12:43:56 -05005050 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005051out:
Steven Rostedt52baf112009-02-14 01:15:39 -05005052 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005053
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005054 if (fail)
5055 return -EINVAL;
5056
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005057 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005058}
5059
5060static ssize_t
5061ftrace_graph_write(struct file *file, const char __user *ubuf,
5062 size_t cnt, loff_t *ppos)
5063{
Namhyung Kim6a101082013-10-14 17:24:25 +09005064 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09005065 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005066 struct trace_parser *parser;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005067
Li Zefanc7c6b1f2010-02-10 15:43:04 +08005068 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005069 return 0;
5070
Steven Rostedt (VMware)ae98d272017-02-02 16:59:06 -05005071 /* Read mode uses seq functions */
5072 if (file->f_mode & FMODE_READ) {
5073 struct seq_file *m = file->private_data;
5074 fgd = m->private;
5075 }
5076
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005077 parser = &fgd->parser;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005078
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005079 read = trace_get_user(parser, ubuf, cnt, ppos);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02005080
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005081 if (read >= 0 && trace_parser_loaded(parser) &&
5082 !trace_parser_cont(parser)) {
Namhyung Kim6a101082013-10-14 17:24:25 +09005083
Namhyung Kimb9b0c8312017-01-20 11:44:47 +09005084 ret = ftrace_graph_set_hash(fgd->new_hash,
Steven Rostedt (VMware)e704eff2017-02-02 20:34:37 -05005085 parser->buffer);
5086 trace_parser_clear(parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005087 }
5088
Namhyung Kim6a101082013-10-14 17:24:25 +09005089 if (!ret)
5090 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08005091
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005092 return ret;
5093}
5094
5095static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08005096 .open = ftrace_graph_open,
5097 .read = seq_read,
5098 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005099 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08005100 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005101};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005102
5103static const struct file_operations ftrace_graph_notrace_fops = {
5104 .open = ftrace_graph_notrace_open,
5105 .read = seq_read,
5106 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005107 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005108 .release = ftrace_graph_release,
5109};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005110#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5111
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005112void ftrace_create_filter_files(struct ftrace_ops *ops,
5113 struct dentry *parent)
5114{
5115
5116 trace_create_file("set_ftrace_filter", 0644, parent,
5117 ops, &ftrace_filter_fops);
5118
5119 trace_create_file("set_ftrace_notrace", 0644, parent,
5120 ops, &ftrace_notrace_fops);
5121}
5122
5123/*
5124 * The name "destroy_filter_files" is really a misnomer. Although
5125 * in the future, it may actualy delete the files, but this is
5126 * really intended to make sure the ops passed in are disabled
5127 * and that when this function returns, the caller is free to
5128 * free the ops.
5129 *
5130 * The "destroy" name is only to match the "create" name that this
5131 * should be paired with.
5132 */
5133void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5134{
5135 mutex_lock(&ftrace_lock);
5136 if (ops->flags & FTRACE_OPS_FL_ENABLED)
5137 ftrace_shutdown(ops, 0);
5138 ops->flags |= FTRACE_OPS_FL_DELETED;
5139 mutex_unlock(&ftrace_lock);
5140}
5141
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005142static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02005143{
Steven Rostedt5072c592008-05-12 21:20:43 +02005144
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005145 trace_create_file("available_filter_functions", 0444,
5146 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02005147
Steven Rostedt647bcd02011-05-03 14:39:21 -04005148 trace_create_file("enabled_functions", 0444,
5149 d_tracer, NULL, &ftrace_enabled_fops);
5150
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05005151 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04005152
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005153#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005154 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005155 NULL,
5156 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09005157 trace_create_file("set_graph_notrace", 0444, d_tracer,
5158 NULL,
5159 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05005160#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5161
Steven Rostedt5072c592008-05-12 21:20:43 +02005162 return 0;
5163}
5164
Steven Rostedt9fd49322012-04-24 22:32:06 -04005165static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05005166{
Steven Rostedt9fd49322012-04-24 22:32:06 -04005167 const unsigned long *ipa = a;
5168 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05005169
Steven Rostedt9fd49322012-04-24 22:32:06 -04005170 if (*ipa > *ipb)
5171 return 1;
5172 if (*ipa < *ipb)
5173 return -1;
5174 return 0;
5175}
5176
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005177static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08005178 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005179 unsigned long *end)
5180{
Steven Rostedt706c81f2012-04-24 23:45:26 -04005181 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005182 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005183 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05005184 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005185 unsigned long *p;
5186 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04005187 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05005188 int ret = -ENOMEM;
5189
5190 count = end - start;
5191
5192 if (!count)
5193 return 0;
5194
Steven Rostedt9fd49322012-04-24 22:32:06 -04005195 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02005196 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04005197
Steven Rostedt706c81f2012-04-24 23:45:26 -04005198 start_pg = ftrace_allocate_pages(count);
5199 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05005200 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005201
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005202 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05005203
Steven Rostedt320823092011-12-16 14:42:37 -05005204 /*
5205 * Core and each module needs their own pages, as
5206 * modules will free them when they are removed.
5207 * Force a new page to be allocated for modules.
5208 */
Steven Rostedta7900872011-12-16 16:23:44 -05005209 if (!mod) {
5210 WARN_ON(ftrace_pages || ftrace_pages_start);
5211 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04005212 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005213 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05005214 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05005215 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05005216
Steven Rostedta7900872011-12-16 16:23:44 -05005217 if (WARN_ON(ftrace_pages->next)) {
5218 /* Hmm, we have free pages? */
5219 while (ftrace_pages->next)
5220 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05005221 }
Steven Rostedta7900872011-12-16 16:23:44 -05005222
Steven Rostedt706c81f2012-04-24 23:45:26 -04005223 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05005224 }
5225
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005226 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005227 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005228 while (p < end) {
5229 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08005230 /*
5231 * Some architecture linkers will pad between
5232 * the different mcount_loc sections of different
5233 * object files to satisfy alignments.
5234 * Skip any NULL pointers.
5235 */
5236 if (!addr)
5237 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04005238
5239 if (pg->index == pg->size) {
5240 /* We should have allocated enough */
5241 if (WARN_ON(!pg->next))
5242 break;
5243 pg = pg->next;
5244 }
5245
5246 rec = &pg->records[pg->index++];
5247 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005248 }
5249
Steven Rostedt706c81f2012-04-24 23:45:26 -04005250 /* We should have used all pages */
5251 WARN_ON(pg->next);
5252
5253 /* Assign the last page to ftrace_pages */
5254 ftrace_pages = pg;
5255
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005256 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04005257 * We only need to disable interrupts on start up
5258 * because we are modifying code that an interrupt
5259 * may execute, and the modification is not atomic.
5260 * But for modules, nothing runs the code we modify
5261 * until we are finished with it, and there's no
5262 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04005263 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04005264 if (!mod)
5265 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005266 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04005267 if (!mod)
5268 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05005269 ret = 0;
5270 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005271 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005272
Steven Rostedta7900872011-12-16 16:23:44 -05005273 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005274}
5275
Steven Rostedt93eb6772009-04-15 13:24:06 -04005276#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05005277
5278#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5279
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005280static int referenced_filters(struct dyn_ftrace *rec)
5281{
5282 struct ftrace_ops *ops;
5283 int cnt = 0;
5284
5285 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5286 if (ops_references_rec(ops, rec))
5287 cnt++;
5288 }
5289
5290 return cnt;
5291}
5292
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005293void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005294{
5295 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05005296 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005297 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005298 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005299
Steven Rostedt93eb6772009-04-15 13:24:06 -04005300 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005301
5302 if (ftrace_disabled)
5303 goto out_unlock;
5304
Steven Rostedt320823092011-12-16 14:42:37 -05005305 /*
5306 * Each module has its own ftrace_pages, remove
5307 * them from the list.
5308 */
5309 last_pg = &ftrace_pages_start;
5310 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5311 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005312 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005313 /*
Steven Rostedt320823092011-12-16 14:42:37 -05005314 * As core pages are first, the first
5315 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005316 */
Steven Rostedt320823092011-12-16 14:42:37 -05005317 if (WARN_ON(pg == ftrace_pages_start))
5318 goto out_unlock;
5319
5320 /* Check if we are deleting the last page */
5321 if (pg == ftrace_pages)
5322 ftrace_pages = next_to_ftrace_page(last_pg);
5323
5324 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05005325 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5326 free_pages((unsigned long)pg->records, order);
5327 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05005328 } else
5329 last_pg = &pg->next;
5330 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005331 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005332 mutex_unlock(&ftrace_lock);
5333}
5334
Jessica Yu7dcd1822016-02-16 17:32:33 -05005335void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005336{
5337 struct dyn_ftrace *rec;
5338 struct ftrace_page *pg;
5339
5340 mutex_lock(&ftrace_lock);
5341
5342 if (ftrace_disabled)
5343 goto out_unlock;
5344
5345 /*
5346 * If the tracing is enabled, go ahead and enable the record.
5347 *
5348 * The reason not to enable the record immediatelly is the
5349 * inherent check of ftrace_make_nop/ftrace_make_call for
5350 * correct previous instructions. Making first the NOP
5351 * conversion puts the module to the correct state, thus
5352 * passing the ftrace_make_call check.
5353 *
5354 * We also delay this to after the module code already set the
5355 * text to read-only, as we now need to set it back to read-write
5356 * so that we can modify the text.
5357 */
5358 if (ftrace_start_up)
5359 ftrace_arch_code_modify_prepare();
5360
5361 do_for_each_ftrace_rec(pg, rec) {
5362 int cnt;
5363 /*
5364 * do_for_each_ftrace_rec() is a double loop.
5365 * module text shares the pg. If a record is
5366 * not part of this module, then skip this pg,
5367 * which the "break" will do.
5368 */
5369 if (!within_module_core(rec->ip, mod))
5370 break;
5371
5372 cnt = 0;
5373
5374 /*
5375 * When adding a module, we need to check if tracers are
5376 * currently enabled and if they are, and can trace this record,
5377 * we need to enable the module functions as well as update the
5378 * reference counts for those function records.
5379 */
5380 if (ftrace_start_up)
5381 cnt += referenced_filters(rec);
5382
5383 /* This clears FTRACE_FL_DISABLED */
5384 rec->flags = cnt;
5385
5386 if (ftrace_start_up && cnt) {
5387 int failed = __ftrace_replace_code(rec, 1);
5388 if (failed) {
5389 ftrace_bug(failed, rec);
5390 goto out_loop;
5391 }
5392 }
5393
5394 } while_for_each_ftrace_rec();
5395
5396 out_loop:
5397 if (ftrace_start_up)
5398 ftrace_arch_code_modify_post_process();
5399
5400 out_unlock:
5401 mutex_unlock(&ftrace_lock);
5402}
5403
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005404void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005405{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005406 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005407 return;
5408
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005409 ftrace_process_locs(mod, mod->ftrace_callsites,
5410 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005411}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005412#endif /* CONFIG_MODULES */
5413
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -04005414void __init ftrace_free_init_mem(void)
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005415{
Steven Rostedt (VMware)b80f0f62017-04-03 12:57:35 -04005416 unsigned long start = (unsigned long)(&__init_begin);
5417 unsigned long end = (unsigned long)(&__init_end);
Steven Rostedt (VMware)42c269c2017-03-03 16:15:39 -05005418 struct ftrace_page **last_pg = &ftrace_pages_start;
5419 struct ftrace_page *pg;
5420 struct dyn_ftrace *rec;
5421 struct dyn_ftrace key;
5422 int order;
5423
5424 key.ip = start;
5425 key.flags = end; /* overload flags, as it is unsigned long */
5426
5427 mutex_lock(&ftrace_lock);
5428
5429 for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
5430 if (end < pg->records[0].ip ||
5431 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
5432 continue;
5433 again:
5434 rec = bsearch(&key, pg->records, pg->index,
5435 sizeof(struct dyn_ftrace),
5436 ftrace_cmp_recs);
5437 if (!rec)
5438 continue;
5439 pg->index--;
5440 if (!pg->index) {
5441 *last_pg = pg->next;
5442 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5443 free_pages((unsigned long)pg->records, order);
5444 kfree(pg);
5445 pg = container_of(last_pg, struct ftrace_page, next);
5446 if (!(*last_pg))
5447 ftrace_pages = pg;
5448 continue;
5449 }
5450 memmove(rec, rec + 1,
5451 (pg->index - (rec - pg->records)) * sizeof(*rec));
5452 /* More than one function may be in this block */
5453 goto again;
5454 }
5455 mutex_unlock(&ftrace_lock);
5456}
5457
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005458void __init ftrace_init(void)
5459{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005460 extern unsigned long __start_mcount_loc[];
5461 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005462 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005463 int ret;
5464
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005465 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005466 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005467 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01005468 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005469 goto failed;
5470
5471 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005472 if (!count) {
5473 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005474 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005475 }
5476
5477 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5478 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005479
5480 last_ftrace_enabled = ftrace_enabled = 1;
5481
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005482 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005483 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005484 __stop_mcount_loc);
5485
Steven Rostedt2af15d62009-05-28 13:37:24 -04005486 set_ftrace_early_filters();
5487
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005488 return;
5489 failed:
5490 ftrace_disabled = 1;
5491}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005492
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005493/* Do nothing if arch does not support this */
5494void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5495{
5496}
5497
5498static void ftrace_update_trampoline(struct ftrace_ops *ops)
5499{
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005500 arch_ftrace_update_trampoline(ops);
5501}
5502
Steven Rostedt3d083392008-05-12 21:20:42 +02005503#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005504
Steven Rostedt2b499382011-05-03 22:49:52 -04005505static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005506 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005507 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5508 FTRACE_OPS_FL_INITIALIZED |
5509 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005510};
5511
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005512static int __init ftrace_nodyn_init(void)
5513{
5514 ftrace_enabled = 1;
5515 return 0;
5516}
Steven Rostedt6f415672012-10-05 12:13:07 -04005517core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005518
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005519static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005520static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005521static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005522/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005523# define ftrace_startup(ops, command) \
5524 ({ \
5525 int ___ret = __register_ftrace_function(ops); \
5526 if (!___ret) \
5527 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5528 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005529 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005530# define ftrace_shutdown(ops, command) \
5531 ({ \
5532 int ___ret = __unregister_ftrace_function(ops); \
5533 if (!___ret) \
5534 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5535 ___ret; \
5536 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005537
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005538# define ftrace_startup_sysctl() do { } while (0)
5539# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005540
5541static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005542ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005543{
5544 return 1;
5545}
5546
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005547static void ftrace_update_trampoline(struct ftrace_ops *ops)
5548{
5549}
5550
Steven Rostedt3d083392008-05-12 21:20:42 +02005551#endif /* CONFIG_DYNAMIC_FTRACE */
5552
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005553__init void ftrace_init_global_array_ops(struct trace_array *tr)
5554{
5555 tr->ops = &global_ops;
5556 tr->ops->private = tr;
5557}
5558
5559void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5560{
5561 /* If we filter on pids, update to use the pid function */
5562 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5563 if (WARN_ON(tr->ops->func != ftrace_stub))
5564 printk("ftrace ops had %pS for function\n",
5565 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005566 }
5567 tr->ops->func = func;
5568 tr->ops->private = tr;
5569}
5570
5571void ftrace_reset_array_ops(struct trace_array *tr)
5572{
5573 tr->ops->func = ftrace_stub;
5574}
5575
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005576static inline void
5577__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005578 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005579{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005580 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005581 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005582
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005583 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5584 if (bit < 0)
5585 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005586
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005587 /*
5588 * Some of the ops may be dynamically allocated,
5589 * they must be freed after a synchronize_sched().
5590 */
5591 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005592
Steven Rostedt0a016402012-11-02 17:03:03 -04005593 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005594 /*
5595 * Check the following for each ops before calling their func:
5596 * if RCU flag is set, then rcu_is_watching() must be true
5597 * if PER_CPU is set, then ftrace_function_local_disable()
5598 * must be false
5599 * Otherwise test if the ip matches the ops filter
5600 *
5601 * If any of the above fails then the op->func() is not executed.
5602 */
5603 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5604 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5605 !ftrace_function_local_disabled(op)) &&
5606 ftrace_ops_test(op, ip, regs)) {
5607
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005608 if (FTRACE_WARN_ON(!op->func)) {
5609 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005610 goto out;
5611 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005612 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005613 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005614 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005615out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005616 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005617 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005618}
5619
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005620/*
5621 * Some archs only support passing ip and parent_ip. Even though
5622 * the list function ignores the op parameter, we do not want any
5623 * C side effects, where a function is called without the caller
5624 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005625 * Archs are to support both the regs and ftrace_ops at the same time.
5626 * If they support ftrace_ops, it is assumed they support regs.
5627 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005628 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5629 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005630 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08005631 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005632 */
5633#if ARCH_SUPPORTS_FTRACE_OPS
5634static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005635 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005636{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005637 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005638}
5639#else
5640static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5641{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005642 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005643}
5644#endif
5645
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005646/*
5647 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005648 * recursion, needs RCU protection and/or requires per cpu handling, then
5649 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005650 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005651static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005652 struct ftrace_ops *op, struct pt_regs *regs)
5653{
5654 int bit;
5655
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005656 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5657 return;
5658
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005659 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5660 if (bit < 0)
5661 return;
5662
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005663 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005664
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005665 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5666 !ftrace_function_local_disabled(op)) {
5667 op->func(ip, parent_ip, op, regs);
5668 }
5669
5670 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005671 trace_clear_recursion(bit);
5672}
5673
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005674/**
5675 * ftrace_ops_get_func - get the function a trampoline should call
5676 * @ops: the ops to get the function for
5677 *
5678 * Normally the mcount trampoline will call the ops->func, but there
5679 * are times that it should not. For example, if the ops does not
5680 * have its own recursion protection, then it should call the
Chunyu Hu3a150df2017-02-22 08:29:26 +08005681 * ftrace_ops_assist_func() instead.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005682 *
5683 * Returns the function that the trampoline should call for @ops.
5684 */
5685ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5686{
5687 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005688 * If the function does not handle recursion, needs to be RCU safe,
5689 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005690 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005691 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5692 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5693 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005694
5695 return ops->func;
5696}
5697
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005698static void
5699ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5700 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05005701{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005702 struct trace_array *tr = data;
5703 struct trace_pid_list *pid_list;
5704
5705 pid_list = rcu_dereference_sched(tr->function_pids);
5706
5707 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5708 trace_ignore_this_task(pid_list, next));
5709}
5710
Namhyung Kim1e104862017-04-17 11:44:28 +09005711static void
5712ftrace_pid_follow_sched_process_fork(void *data,
5713 struct task_struct *self,
5714 struct task_struct *task)
5715{
5716 struct trace_pid_list *pid_list;
5717 struct trace_array *tr = data;
5718
5719 pid_list = rcu_dereference_sched(tr->function_pids);
5720 trace_filter_add_remove_task(pid_list, self, task);
5721}
5722
5723static void
5724ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
5725{
5726 struct trace_pid_list *pid_list;
5727 struct trace_array *tr = data;
5728
5729 pid_list = rcu_dereference_sched(tr->function_pids);
5730 trace_filter_add_remove_task(pid_list, NULL, task);
5731}
5732
5733void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
5734{
5735 if (enable) {
5736 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
5737 tr);
5738 register_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
5739 tr);
5740 } else {
5741 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
5742 tr);
5743 unregister_trace_sched_process_exit(ftrace_pid_follow_sched_process_exit,
5744 tr);
5745 }
5746}
5747
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005748static void clear_ftrace_pids(struct trace_array *tr)
5749{
5750 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05005751 int cpu;
5752
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005753 pid_list = rcu_dereference_protected(tr->function_pids,
5754 lockdep_is_held(&ftrace_lock));
5755 if (!pid_list)
5756 return;
5757
5758 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5759
5760 for_each_possible_cpu(cpu)
5761 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5762
5763 rcu_assign_pointer(tr->function_pids, NULL);
5764
5765 /* Wait till all users are no longer using pid filtering */
5766 synchronize_sched();
5767
5768 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05005769}
5770
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005771static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05005772{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005773 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005774 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005775
5776 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005777 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005778
5779 mutex_unlock(&ftrace_lock);
5780}
5781
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005782/* Greater than any max PID */
5783#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5784
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005785static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005786 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005787{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005788 struct trace_pid_list *pid_list;
5789 struct trace_array *tr = m->private;
5790
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005791 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005792 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005793
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005794 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005795
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005796 if (!pid_list)
5797 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5798
5799 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005800}
5801
5802static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5803{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005804 struct trace_array *tr = m->private;
5805 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5806
5807 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005808 return NULL;
5809
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005810 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005811}
5812
5813static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005814 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005815{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005816 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005817 mutex_unlock(&ftrace_lock);
5818}
5819
5820static int fpid_show(struct seq_file *m, void *v)
5821{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005822 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005823 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005824 return 0;
5825 }
5826
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005827 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005828}
5829
5830static const struct seq_operations ftrace_pid_sops = {
5831 .start = fpid_start,
5832 .next = fpid_next,
5833 .stop = fpid_stop,
5834 .show = fpid_show,
5835};
5836
5837static int
5838ftrace_pid_open(struct inode *inode, struct file *file)
5839{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005840 struct trace_array *tr = inode->i_private;
5841 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005842 int ret = 0;
5843
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005844 if (trace_array_get(tr) < 0)
5845 return -ENODEV;
5846
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005847 if ((file->f_mode & FMODE_WRITE) &&
5848 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005849 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005850
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005851 ret = seq_open(file, &ftrace_pid_sops);
5852 if (ret < 0) {
5853 trace_array_put(tr);
5854 } else {
5855 m = file->private_data;
5856 /* copy tr over to seq ops */
5857 m->private = tr;
5858 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005859
5860 return ret;
5861}
5862
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005863static void ignore_task_cpu(void *data)
5864{
5865 struct trace_array *tr = data;
5866 struct trace_pid_list *pid_list;
5867
5868 /*
5869 * This function is called by on_each_cpu() while the
5870 * event_mutex is held.
5871 */
5872 pid_list = rcu_dereference_protected(tr->function_pids,
5873 mutex_is_locked(&ftrace_lock));
5874
5875 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5876 trace_ignore_this_task(pid_list, current));
5877}
5878
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005879static ssize_t
5880ftrace_pid_write(struct file *filp, const char __user *ubuf,
5881 size_t cnt, loff_t *ppos)
5882{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005883 struct seq_file *m = filp->private_data;
5884 struct trace_array *tr = m->private;
5885 struct trace_pid_list *filtered_pids = NULL;
5886 struct trace_pid_list *pid_list;
5887 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005888
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005889 if (!cnt)
5890 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005891
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005892 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005893
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005894 filtered_pids = rcu_dereference_protected(tr->function_pids,
5895 lockdep_is_held(&ftrace_lock));
5896
5897 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5898 if (ret < 0)
5899 goto out;
5900
5901 rcu_assign_pointer(tr->function_pids, pid_list);
5902
5903 if (filtered_pids) {
5904 synchronize_sched();
5905 trace_free_pid_list(filtered_pids);
5906 } else if (pid_list) {
5907 /* Register a probe to set whether to ignore the tracing of a task */
5908 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5909 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005910
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005911 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005912 * Ignoring of pids is done at task switch. But we have to
5913 * check for those tasks that are currently running.
5914 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005915 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005916 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005917
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005918 ftrace_update_pid_func();
5919 ftrace_startup_all(0);
5920 out:
5921 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005922
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005923 if (ret > 0)
5924 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05005925
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005926 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005927}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005928
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005929static int
5930ftrace_pid_release(struct inode *inode, struct file *file)
5931{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005932 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005933
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005934 trace_array_put(tr);
5935
5936 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005937}
5938
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005939static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005940 .open = ftrace_pid_open,
5941 .write = ftrace_pid_write,
5942 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005943 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005944 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005945};
5946
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005947void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005948{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005949 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005950 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005951}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005952
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04005953void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5954 struct dentry *d_tracer)
5955{
5956 /* Only the top level directory has the dyn_tracefs and profile */
5957 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5958
5959 ftrace_init_dyn_tracefs(d_tracer);
5960 ftrace_profile_tracefs(d_tracer);
5961}
5962
Steven Rostedt3d083392008-05-12 21:20:42 +02005963/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005964 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005965 *
5966 * This function should be used by panic code. It stops ftrace
5967 * but in a not so nice way. If you need to simply kill ftrace
5968 * from a non-atomic section, use ftrace_kill.
5969 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005970void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005971{
5972 ftrace_disabled = 1;
5973 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005974 clear_ftrace_function();
5975}
5976
5977/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005978 * Test if ftrace is dead or not.
5979 */
5980int ftrace_is_dead(void)
5981{
5982 return ftrace_disabled;
5983}
5984
5985/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005986 * register_ftrace_function - register a function for profiling
5987 * @ops - ops structure that holds the function for profiling.
5988 *
5989 * Register a function to be called by all functions in the
5990 * kernel.
5991 *
5992 * Note: @ops->func and all the functions it calls must be labeled
5993 * with "notrace", otherwise it will go into a
5994 * recursive loop.
5995 */
5996int register_ftrace_function(struct ftrace_ops *ops)
5997{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005998 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005999
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09006000 ftrace_ops_init(ops);
6001
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006002 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006003
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006004 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04006005
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006006 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02006007
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006008 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02006009}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006010EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02006011
6012/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01006013 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02006014 * @ops - ops structure that holds the function to unregister
6015 *
6016 * Unregister a function that was added to be called by ftrace profiling.
6017 */
6018int unregister_ftrace_function(struct ftrace_ops *ops)
6019{
6020 int ret;
6021
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006022 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05006023 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006024 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006025
6026 return ret;
6027}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04006028EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006029
Ingo Molnare309b412008-05-12 21:20:51 +02006030int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006031ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07006032 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006033 loff_t *ppos)
6034{
Steven Rostedt45a4a232011-04-21 23:16:46 -04006035 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02006036
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006037 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006038
Steven Rostedt45a4a232011-04-21 23:16:46 -04006039 if (unlikely(ftrace_disabled))
6040 goto out;
6041
6042 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006043
Li Zefana32c7762009-06-26 16:55:51 +08006044 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006045 goto out;
6046
Li Zefana32c7762009-06-26 16:55:51 +08006047 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006048
6049 if (ftrace_enabled) {
6050
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006051 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01006052 if (ftrace_ops_list != &ftrace_list_end)
6053 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006054
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05006055 ftrace_startup_sysctl();
6056
Steven Rostedtb0fc4942008-05-12 21:20:43 +02006057 } else {
6058 /* stopping ftrace calls (just send to ftrace_stub) */
6059 ftrace_trace_function = ftrace_stub;
6060
6061 ftrace_shutdown_sysctl();
6062 }
6063
6064 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006065 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02006066 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02006067}
Ingo Molnarf17845e2008-10-24 12:47:10 +02006068
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006069#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006070
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006071static struct ftrace_ops graph_ops = {
6072 .func = ftrace_stub,
6073 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
6074 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04006075 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006076 FTRACE_OPS_FL_STUB,
6077#ifdef FTRACE_GRAPH_TRAMP_ADDR
6078 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05006079 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006080#endif
6081 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6082};
6083
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006084void ftrace_graph_sleep_time_control(bool enable)
6085{
6086 fgraph_sleep_time = enable;
6087}
6088
6089void ftrace_graph_graph_time_control(bool enable)
6090{
6091 fgraph_graph_time = enable;
6092}
6093
Steven Rostedte49dc192008-12-02 23:50:05 -05006094int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6095{
6096 return 0;
6097}
6098
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006099/* The callbacks that hook a function */
6100trace_func_graph_ret_t ftrace_graph_return =
6101 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006102trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006103static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006104
6105/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6106static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6107{
6108 int i;
6109 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006110 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6111 struct task_struct *g, *t;
6112
6113 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6114 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
6115 * sizeof(struct ftrace_ret_stack),
6116 GFP_KERNEL);
6117 if (!ret_stack_list[i]) {
6118 start = 0;
6119 end = i;
6120 ret = -ENOMEM;
6121 goto free;
6122 }
6123 }
6124
Soumya PN6112a302016-05-17 21:31:14 +05306125 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006126 do_each_thread(g, t) {
6127 if (start == end) {
6128 ret = -EAGAIN;
6129 goto unlock;
6130 }
6131
6132 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01006133 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006134 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04006135 t->curr_ret_stack = -1;
6136 /* Make sure the tasks see the -1 first: */
6137 smp_wmb();
6138 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006139 }
6140 } while_each_thread(g, t);
6141
6142unlock:
Soumya PN6112a302016-05-17 21:31:14 +05306143 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006144free:
6145 for (i = start; i < end; i++)
6146 kfree(ret_stack_list[i]);
6147 return ret;
6148}
6149
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006150static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02006151ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04006152 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006153{
6154 unsigned long long timestamp;
6155 int index;
6156
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006157 /*
6158 * Does the user want to count the time a function was asleep.
6159 * If so, do not update the time stamps.
6160 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04006161 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04006162 return;
6163
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006164 timestamp = trace_clock_local();
6165
6166 prev->ftrace_timestamp = timestamp;
6167
6168 /* only process tasks that we timestamped */
6169 if (!next->ftrace_timestamp)
6170 return;
6171
6172 /*
6173 * Update all the counters in next to make up for the
6174 * time next was sleeping.
6175 */
6176 timestamp -= next->ftrace_timestamp;
6177
6178 for (index = next->curr_ret_stack; index >= 0; index--)
6179 next->ret_stack[index].calltime += timestamp;
6180}
6181
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006182/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006183static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006184{
6185 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006186 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006187
6188 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
6189 sizeof(struct ftrace_ret_stack *),
6190 GFP_KERNEL);
6191
6192 if (!ret_stack_list)
6193 return -ENOMEM;
6194
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006195 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04006196 for_each_online_cpu(cpu) {
6197 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05006198 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04006199 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01006200
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006201 do {
6202 ret = alloc_retstack_tasklist(ret_stack_list);
6203 } while (ret == -EAGAIN);
6204
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006205 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04006206 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04006207 if (ret)
6208 pr_info("ftrace_graph: Couldn't activate tracepoint"
6209 " probe to kernel_sched_switch\n");
6210 }
6211
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006212 kfree(ret_stack_list);
6213 return ret;
6214}
6215
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006216/*
6217 * Hibernation protection.
6218 * The state of the current task is too much unstable during
6219 * suspend/restore to disk. We want to protect against that.
6220 */
6221static int
6222ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6223 void *unused)
6224{
6225 switch (state) {
6226 case PM_HIBERNATION_PREPARE:
6227 pause_graph_tracing();
6228 break;
6229
6230 case PM_POST_HIBERNATION:
6231 unpause_graph_tracing();
6232 break;
6233 }
6234 return NOTIFY_DONE;
6235}
6236
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006237static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
6238{
6239 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
6240 return 0;
6241 return __ftrace_graph_entry(trace);
6242}
6243
6244/*
6245 * The function graph tracer should only trace the functions defined
6246 * by set_ftrace_filter and set_ftrace_notrace. If another function
6247 * tracer ops is registered, the graph tracer requires testing the
6248 * function against the global ops, and not just trace any function
6249 * that any ftrace_ops registered.
6250 */
6251static void update_function_graph_func(void)
6252{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006253 struct ftrace_ops *op;
6254 bool do_test = false;
6255
6256 /*
6257 * The graph and global ops share the same set of functions
6258 * to test. If any other ops is on the list, then
6259 * the graph tracing needs to test if its the function
6260 * it should call.
6261 */
6262 do_for_each_ftrace_op(op, ftrace_ops_list) {
6263 if (op != &global_ops && op != &graph_ops &&
6264 op != &ftrace_list_end) {
6265 do_test = true;
6266 /* in double loop, break out with goto */
6267 goto out;
6268 }
6269 } while_for_each_ftrace_op(op);
6270 out:
6271 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006272 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006273 else
6274 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006275}
6276
Mathias Krause8275f692014-03-30 15:31:50 +02006277static struct notifier_block ftrace_suspend_notifier = {
6278 .notifier_call = ftrace_suspend_notifier_call,
6279};
6280
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006281int register_ftrace_graph(trace_func_graph_ret_t retfunc,
6282 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006283{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006284 int ret = 0;
6285
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006286 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006287
Steven Rostedt05ce5812009-03-24 00:18:31 -04006288 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04006289 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04006290 ret = -EBUSY;
6291 goto out;
6292 }
6293
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006294 register_pm_notifier(&ftrace_suspend_notifier);
6295
Steven Rostedt597af812009-04-03 15:24:12 -04006296 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006297 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006298 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04006299 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006300 goto out;
6301 }
Steven Rostedte53a6312008-11-26 00:16:25 -05006302
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006303 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006304
6305 /*
6306 * Update the indirect function to the entryfunc, and the
6307 * function that gets called to the entry_test first. Then
6308 * call the update fgraph entry function to determine if
6309 * the entryfunc should be called directly or not.
6310 */
6311 __ftrace_graph_entry = entryfunc;
6312 ftrace_graph_entry = ftrace_graph_entry_test;
6313 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05006314
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006315 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006316out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006317 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006318 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006319}
6320
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006321void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006322{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006323 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006324
Steven Rostedt597af812009-04-03 15:24:12 -04006325 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006326 goto out;
6327
Steven Rostedt597af812009-04-03 15:24:12 -04006328 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01006329 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05006330 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05006331 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04006332 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08006333 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04006334 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01006335
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04006336#ifdef CONFIG_DYNAMIC_FTRACE
6337 /*
6338 * Function graph does not allocate the trampoline, but
6339 * other global_ops do. We need to reset the ALLOC_TRAMP flag
6340 * if one was used.
6341 */
6342 global_ops.trampoline = save_global_trampoline;
6343 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
6344 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
6345#endif
6346
Steven Rostedt2aad1b72009-03-30 11:11:28 -04006347 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05006348 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006349}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006350
Steven Rostedt868baf02011-02-10 21:26:13 -05006351static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6352
6353static void
6354graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6355{
6356 atomic_set(&t->tracing_graph_pause, 0);
6357 atomic_set(&t->trace_overrun, 0);
6358 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006359 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05006360 smp_wmb();
6361 t->ret_stack = ret_stack;
6362}
6363
6364/*
6365 * Allocate a return stack for the idle task. May be the first
6366 * time through, or it may be done by CPU hotplug online.
6367 */
6368void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6369{
6370 t->curr_ret_stack = -1;
6371 /*
6372 * The idle task has no parent, it either has its own
6373 * stack or no stack at all.
6374 */
6375 if (t->ret_stack)
6376 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6377
6378 if (ftrace_graph_active) {
6379 struct ftrace_ret_stack *ret_stack;
6380
6381 ret_stack = per_cpu(idle_ret_stack, cpu);
6382 if (!ret_stack) {
6383 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6384 * sizeof(struct ftrace_ret_stack),
6385 GFP_KERNEL);
6386 if (!ret_stack)
6387 return;
6388 per_cpu(idle_ret_stack, cpu) = ret_stack;
6389 }
6390 graph_init_task(t, ret_stack);
6391 }
6392}
6393
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006394/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006395void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006396{
Steven Rostedt84047e32009-06-02 16:51:55 -04006397 /* Make sure we do not use the parent ret_stack */
6398 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05006399 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04006400
Steven Rostedt597af812009-04-03 15:24:12 -04006401 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04006402 struct ftrace_ret_stack *ret_stack;
6403
6404 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006405 * sizeof(struct ftrace_ret_stack),
6406 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04006407 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006408 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05006409 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04006410 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006411}
6412
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006413void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006414{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006415 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6416
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006417 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006418 /* NULL must become visible to IRQs before we free it: */
6419 barrier();
6420
6421 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006422}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006423#endif