blob: f39616f91392c81024d65752caeb34636f340d74 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050021#include <linux/tracefs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Masami Hiramatsu25f467d2019-02-24 01:50:20 +090035#include <linux/kprobes.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 Rostedt2af15d62009-05-28 13:37:24 -040039#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053040
Steven Rostedt0706f1c2009-03-23 23:12:58 -040041#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040042#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020043
Steven Rostedt6912896e2008-10-23 09:33:03 -040044#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040045 ({ \
46 int ___r = cond; \
47 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040048 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040049 ___r; \
50 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040051
52#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040053 ({ \
54 int ___r = cond; \
55 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040056 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040057 ___r; \
58 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040059
Steven Rostedt8fc0c702009-02-16 15:28:00 -050060/* hash bits for specific function selection */
61#define FTRACE_HASH_BITS 7
62#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040063#define FTRACE_HASH_DEFAULT_BITS 10
64#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050065
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090066#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040067#define INIT_OPS_HASH(opsname) \
68 .func_hash = &opsname.local_hash, \
69 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040070#define ASSIGN_OPS_HASH(opsname, val) \
71 .func_hash = val, \
72 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090073#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040074#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040075#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090076#endif
77
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040078static struct ftrace_ops ftrace_list_end __read_mostly = {
79 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040080 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040081 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040082};
83
Steven Rostedt4eebcc82008-05-12 21:20:48 +020084/* ftrace_enabled is a method to turn ftrace on or off */
85int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020086static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020087
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040088/* Current function tracing op */
89struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050090/* What to set function_trace_op to */
91static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050092
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040093static bool ftrace_pids_enabled(struct ftrace_ops *ops)
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -040094{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -040095 struct trace_array *tr;
96
97 if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
98 return false;
99
100 tr = ops->private;
101
102 return tr->function_pids != NULL;
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -0400103}
104
105static void ftrace_update_trampoline(struct ftrace_ops *ops);
106
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200107/*
108 * ftrace_disabled is set when an anomaly is discovered.
109 * ftrace_disabled is much stronger than ftrace_enabled.
110 */
111static int ftrace_disabled __read_mostly;
112
Steven Rostedt52baf112009-02-14 01:15:39 -0500113static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200114
Steven Rostedtb8489142011-05-04 09:27:52 -0400115static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200116ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400117static struct ftrace_ops global_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200118
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400119#if ARCH_SUPPORTS_FTRACE_OPS
120static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400121 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122#else
123/* See comment below, where ftrace_ops_list_func is defined */
Sami Tolvanenc2f9bce2018-05-10 14:56:41 -0700124static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
125 struct ftrace_ops *op, struct pt_regs *regs);
126#define ftrace_ops_list_func ftrace_ops_no_ops
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400127#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
Wen Yang1a2985a2020-01-03 11:02:48 +0800613 avg = div64_ul(rec->time, rec->counter);
Umesh Tiwari8e436ca2015-06-22 16:58:08 +0530614 if (tracing_thresh && (avg < tracing_thresh))
615 goto out;
616#endif
617
Steven Rostedt493762f2009-03-23 17:12:36 -0400618 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400619 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400620
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400621#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100622 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400623
Chase Douglase330b3b2010-04-26 14:02:05 -0400624 /* Sample standard deviation (s^2) */
625 if (rec->counter <= 1)
626 stddev = 0;
627 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200628 /*
629 * Apply Welford's method:
630 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
631 */
632 stddev = rec->counter * rec->time_squared -
633 rec->time * rec->time;
634
Chase Douglase330b3b2010-04-26 14:02:05 -0400635 /*
636 * Divide only 1000 for ns^2 -> us^2 conversion.
637 * trace_print_graph_duration will divide 1000 again.
638 */
Wen Yang1a2985a2020-01-03 11:02:48 +0800639 stddev = div64_ul(stddev,
640 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
Steven Rostedt (VMware)741397d2017-08-17 16:37:25 -0400881 /* If function graph is shutting down, ret_stack can be NULL */
882 if (!current->ret_stack)
883 return 0;
884
Namhyung Kim8861dd32016-08-31 11:55:29 +0900885 if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
886 current->ret_stack[index].subtime = 0;
887
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400888 return 1;
889}
890
891static void profile_graph_return(struct ftrace_graph_ret *trace)
892{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400893 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400894 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400895 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400896 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400897
898 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500899 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400900 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400901 goto out;
902
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400903 /* If the calltime was zero'd ignore it */
904 if (!trace->calltime)
905 goto out;
906
Steven Rostedta2a16d62009-03-24 23:17:58 -0400907 calltime = trace->rettime - trace->calltime;
908
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -0400909 if (!fgraph_graph_time) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400910 int index;
911
912 index = trace->depth;
913
914 /* Append this call time to the parent time to subtract */
915 if (index)
916 current->ret_stack[index - 1].subtime += calltime;
917
918 if (current->ret_stack[index].subtime < calltime)
919 calltime -= current->ret_stack[index].subtime;
920 else
921 calltime = 0;
922 }
923
Steven Rostedtcafb1682009-03-24 20:50:39 -0400924 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400925 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400926 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400927 rec->time_squared += calltime * calltime;
928 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400929
Steven Rostedtcafb1682009-03-24 20:50:39 -0400930 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400931 local_irq_restore(flags);
932}
933
934static int register_ftrace_profiler(void)
935{
936 return register_ftrace_graph(&profile_graph_return,
937 &profile_graph_entry);
938}
939
940static void unregister_ftrace_profiler(void)
941{
942 unregister_ftrace_graph();
943}
944#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100945static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400946 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900947 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400948 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400949};
950
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400951static int register_ftrace_profiler(void)
952{
953 return register_ftrace_function(&ftrace_profile_ops);
954}
955
956static void unregister_ftrace_profiler(void)
957{
958 unregister_ftrace_function(&ftrace_profile_ops);
959}
960#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
961
Steven Rostedt493762f2009-03-23 17:12:36 -0400962static ssize_t
963ftrace_profile_write(struct file *filp, const char __user *ubuf,
964 size_t cnt, loff_t *ppos)
965{
966 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400967 int ret;
968
Peter Huewe22fe9b52011-06-07 21:58:27 +0200969 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
970 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400971 return ret;
972
973 val = !!val;
974
975 mutex_lock(&ftrace_profile_lock);
976 if (ftrace_profile_enabled ^ val) {
977 if (val) {
978 ret = ftrace_profile_init();
979 if (ret < 0) {
980 cnt = ret;
981 goto out;
982 }
983
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400984 ret = register_ftrace_profiler();
985 if (ret < 0) {
986 cnt = ret;
987 goto out;
988 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400989 ftrace_profile_enabled = 1;
990 } else {
991 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400992 /*
993 * unregister_ftrace_profiler calls stop_machine
994 * so this acts like an synchronize_sched.
995 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400996 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400997 }
998 }
999 out:
1000 mutex_unlock(&ftrace_profile_lock);
1001
Jiri Olsacf8517c2009-10-23 19:36:16 -04001002 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001003
1004 return cnt;
1005}
1006
1007static ssize_t
1008ftrace_profile_read(struct file *filp, char __user *ubuf,
1009 size_t cnt, loff_t *ppos)
1010{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001011 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001012 int r;
1013
1014 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1015 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1016}
1017
1018static const struct file_operations ftrace_profile_fops = {
1019 .open = tracing_open_generic,
1020 .read = ftrace_profile_read,
1021 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001022 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001023};
1024
Steven Rostedtcafb1682009-03-24 20:50:39 -04001025/* used to initialize the real stat files */
1026static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001027 .name = "functions",
1028 .stat_start = function_stat_start,
1029 .stat_next = function_stat_next,
1030 .stat_cmp = function_stat_cmp,
1031 .stat_headers = function_stat_headers,
1032 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001033};
1034
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001035static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001036{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001037 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001038 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001039 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001040 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001041 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001042
Steven Rostedtcafb1682009-03-24 20:50:39 -04001043 for_each_possible_cpu(cpu) {
1044 stat = &per_cpu(ftrace_profile_stats, cpu);
1045
Geliang Tang6363c6b2016-03-15 22:12:34 +08001046 name = kasprintf(GFP_KERNEL, "function%d", cpu);
Steven Rostedtcafb1682009-03-24 20:50:39 -04001047 if (!name) {
1048 /*
1049 * The files created are permanent, if something happens
1050 * we still do not free memory.
1051 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001052 WARN(1,
1053 "Could not allocate stat file for cpu %d\n",
1054 cpu);
1055 return;
1056 }
1057 stat->stat = function_stats;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001058 stat->stat.name = name;
1059 ret = register_stat_tracer(&stat->stat);
1060 if (ret) {
1061 WARN(1,
1062 "Could not register function stat for cpu %d\n",
1063 cpu);
1064 kfree(name);
1065 return;
1066 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001067 }
1068
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001069 entry = tracefs_create_file("function_profile_enabled", 0644,
Steven Rostedt493762f2009-03-23 17:12:36 -04001070 d_tracer, NULL, &ftrace_profile_fops);
1071 if (!entry)
Joe Perchesa395d6a2016-03-22 14:28:09 -07001072 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
Steven Rostedt493762f2009-03-23 17:12:36 -04001073}
1074
1075#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001076static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001077{
1078}
1079#endif /* CONFIG_FUNCTION_PROFILER */
1080
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001081static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1082
Pratyush Anand1619dc32015-03-06 23:58:06 +05301083#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1084static int ftrace_graph_active;
1085#else
1086# define ftrace_graph_active 0
1087#endif
1088
Steven Rostedt3d083392008-05-12 21:20:42 +02001089#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001090
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001091static struct ftrace_ops *removed_ops;
1092
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001093/*
1094 * Set when doing a global update, like enabling all recs or disabling them.
1095 * It is not set when just updating a single ftrace_ops.
1096 */
1097static bool update_all_ops;
1098
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001099#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001100# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001101#endif
1102
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001103static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1104
Steven Rostedtb6887d72009-02-17 12:32:04 -05001105struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001106 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001107 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001108 unsigned long flags;
1109 unsigned long ip;
1110 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001111 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001112};
1113
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001114struct ftrace_func_entry {
1115 struct hlist_node hlist;
1116 unsigned long ip;
1117};
1118
1119struct ftrace_hash {
1120 unsigned long size_bits;
1121 struct hlist_head *buckets;
1122 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001123 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001124};
1125
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001126/*
1127 * We make these constant because no one should touch them,
1128 * but they are used as the default "empty hash", to avoid allocating
1129 * it all the time. These are in a read only section such that if
1130 * anyone does try to modify it, it will cause an exception.
1131 */
1132static const struct hlist_head empty_buckets[1];
1133static const struct ftrace_hash empty_hash = {
1134 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001135};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001136#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001137
Steven Rostedt2b499382011-05-03 22:49:52 -04001138static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001139 .func = ftrace_stub,
1140 .local_hash.notrace_hash = EMPTY_HASH,
1141 .local_hash.filter_hash = EMPTY_HASH,
1142 INIT_OPS_HASH(global_ops)
1143 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04001144 FTRACE_OPS_FL_INITIALIZED |
1145 FTRACE_OPS_FL_PID,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001146};
1147
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001148/*
1149 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001150 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001151 * not return true for either core_kernel_text() or
1152 * is_module_text_address().
1153 */
1154bool is_ftrace_trampoline(unsigned long addr)
1155{
1156 struct ftrace_ops *op;
1157 bool ret = false;
1158
1159 /*
1160 * Some of the ops may be dynamically allocated,
1161 * they are freed after a synchronize_sched().
1162 */
1163 preempt_disable_notrace();
1164
1165 do_for_each_ftrace_op(op, ftrace_ops_list) {
1166 /*
1167 * This is to check for dynamically allocated trampolines.
1168 * Trampolines that are in kernel text will have
1169 * core_kernel_text() return true.
1170 */
1171 if (op->trampoline && op->trampoline_size)
1172 if (addr >= op->trampoline &&
1173 addr < op->trampoline + op->trampoline_size) {
1174 ret = true;
1175 goto out;
1176 }
1177 } while_for_each_ftrace_op(op);
1178
1179 out:
1180 preempt_enable_notrace();
1181
1182 return ret;
1183}
1184
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001185struct ftrace_page {
1186 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001187 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001188 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001189 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001190};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001191
Steven Rostedta7900872011-12-16 16:23:44 -05001192#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1193#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001194
1195/* estimate from running different kernels */
1196#define NR_TO_INIT 10000
1197
1198static struct ftrace_page *ftrace_pages_start;
1199static struct ftrace_page *ftrace_pages;
1200
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001201static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001202{
1203 return !hash || !hash->count;
1204}
1205
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001206static struct ftrace_func_entry *
1207ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1208{
1209 unsigned long key;
1210 struct ftrace_func_entry *entry;
1211 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001212
Steven Rostedt06a51d92011-12-19 19:07:36 -05001213 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001214 return NULL;
1215
1216 if (hash->size_bits > 0)
1217 key = hash_long(ip, hash->size_bits);
1218 else
1219 key = 0;
1220
1221 hhd = &hash->buckets[key];
1222
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001223 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001224 if (entry->ip == ip)
1225 return entry;
1226 }
1227 return NULL;
1228}
1229
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001230static void __add_hash_entry(struct ftrace_hash *hash,
1231 struct ftrace_func_entry *entry)
1232{
1233 struct hlist_head *hhd;
1234 unsigned long key;
1235
1236 if (hash->size_bits)
1237 key = hash_long(entry->ip, hash->size_bits);
1238 else
1239 key = 0;
1240
1241 hhd = &hash->buckets[key];
1242 hlist_add_head(&entry->hlist, hhd);
1243 hash->count++;
1244}
1245
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001246static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1247{
1248 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001249
1250 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1251 if (!entry)
1252 return -ENOMEM;
1253
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001254 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001255 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001256
1257 return 0;
1258}
1259
1260static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001261free_hash_entry(struct ftrace_hash *hash,
1262 struct ftrace_func_entry *entry)
1263{
1264 hlist_del(&entry->hlist);
1265 kfree(entry);
1266 hash->count--;
1267}
1268
1269static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001270remove_hash_entry(struct ftrace_hash *hash,
1271 struct ftrace_func_entry *entry)
1272{
1273 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001274 hash->count--;
1275}
1276
1277static void ftrace_hash_clear(struct ftrace_hash *hash)
1278{
1279 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001280 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001281 struct ftrace_func_entry *entry;
1282 int size = 1 << hash->size_bits;
1283 int i;
1284
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001285 if (!hash->count)
1286 return;
1287
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001288 for (i = 0; i < size; i++) {
1289 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001290 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001291 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001292 }
1293 FTRACE_WARN_ON(hash->count);
1294}
1295
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001296static void free_ftrace_hash(struct ftrace_hash *hash)
1297{
1298 if (!hash || hash == EMPTY_HASH)
1299 return;
1300 ftrace_hash_clear(hash);
1301 kfree(hash->buckets);
1302 kfree(hash);
1303}
1304
Steven Rostedt07fd5512011-05-05 18:03:47 -04001305static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1306{
1307 struct ftrace_hash *hash;
1308
1309 hash = container_of(rcu, struct ftrace_hash, rcu);
1310 free_ftrace_hash(hash);
1311}
1312
1313static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1314{
1315 if (!hash || hash == EMPTY_HASH)
1316 return;
1317 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1318}
1319
Jiri Olsa5500fa52012-02-15 15:51:54 +01001320void ftrace_free_filter(struct ftrace_ops *ops)
1321{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001322 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001323 free_ftrace_hash(ops->func_hash->filter_hash);
1324 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001325}
1326
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001327static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1328{
1329 struct ftrace_hash *hash;
1330 int size;
1331
1332 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1333 if (!hash)
1334 return NULL;
1335
1336 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001337 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001338
1339 if (!hash->buckets) {
1340 kfree(hash);
1341 return NULL;
1342 }
1343
1344 hash->size_bits = size_bits;
1345
1346 return hash;
1347}
1348
1349static struct ftrace_hash *
1350alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1351{
1352 struct ftrace_func_entry *entry;
1353 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001354 int size;
1355 int ret;
1356 int i;
1357
1358 new_hash = alloc_ftrace_hash(size_bits);
1359 if (!new_hash)
1360 return NULL;
1361
1362 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001363 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001364 return new_hash;
1365
1366 size = 1 << hash->size_bits;
1367 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001368 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001369 ret = add_hash_entry(new_hash, entry->ip);
1370 if (ret < 0)
1371 goto free_hash;
1372 }
1373 }
1374
1375 FTRACE_WARN_ON(new_hash->count != hash->count);
1376
1377 return new_hash;
1378
1379 free_hash:
1380 free_ftrace_hash(new_hash);
1381 return NULL;
1382}
1383
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001384static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001385ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001386static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001387ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001388
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001389static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1390 struct ftrace_hash *new_hash);
1391
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001392static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001393ftrace_hash_move(struct ftrace_ops *ops, int enable,
1394 struct ftrace_hash **dst, 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;
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001402 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001403 int i;
1404
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001405 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1406 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1407 return -EINVAL;
1408
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001409 /*
1410 * If the new source is empty, just free dst and assign it
1411 * the empty_hash.
1412 */
1413 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001414 new_hash = EMPTY_HASH;
1415 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001416 }
1417
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001418 /*
1419 * Make the hash size about 1/2 the # found
1420 */
1421 for (size /= 2; size; size >>= 1)
1422 bits++;
1423
1424 /* Don't allocate too much */
1425 if (bits > FTRACE_HASH_MAX_BITS)
1426 bits = FTRACE_HASH_MAX_BITS;
1427
Steven Rostedt07fd5512011-05-05 18:03:47 -04001428 new_hash = alloc_ftrace_hash(bits);
1429 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001430 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001431
1432 size = 1 << src->size_bits;
1433 for (i = 0; i < size; i++) {
1434 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001435 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001436 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001437 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001438 }
1439 }
1440
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001441update:
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001442 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1443 if (enable) {
1444 /* IPMODIFY should be updated only when filter_hash updating */
1445 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1446 if (ret < 0) {
1447 free_ftrace_hash(new_hash);
1448 return ret;
1449 }
1450 }
1451
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001452 /*
1453 * Remove the current set, update the hash and add
1454 * them back.
1455 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001456 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001457
Steven Rostedt07fd5512011-05-05 18:03:47 -04001458 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001459
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001460 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001461
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001462 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001463}
1464
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001465static bool hash_contains_ip(unsigned long ip,
1466 struct ftrace_ops_hash *hash)
1467{
1468 /*
1469 * The function record is a match if it exists in the filter
1470 * hash and not in the notrace hash. Note, an emty hash is
1471 * considered a match for the filter hash, but an empty
1472 * notrace hash is considered not in the notrace hash.
1473 */
1474 return (ftrace_hash_empty(hash->filter_hash) ||
1475 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1476 (ftrace_hash_empty(hash->notrace_hash) ||
1477 !ftrace_lookup_ip(hash->notrace_hash, ip));
1478}
1479
Steven Rostedt265c8312009-02-13 12:43:56 -05001480/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001481 * Test the hashes for this ops to see if we want to call
1482 * the ops->func or not.
1483 *
1484 * It's a match if the ip is in the ops->filter_hash or
1485 * the filter_hash does not exist or is empty,
1486 * AND
1487 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001488 *
1489 * This needs to be called with preemption disabled as
1490 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001491 */
1492static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001493ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001494{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001495 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001496 int ret;
1497
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001498#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1499 /*
1500 * There's a small race when adding ops that the ftrace handler
1501 * that wants regs, may be called without them. We can not
1502 * allow that handler to be called if regs is NULL.
1503 */
1504 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1505 return 0;
1506#endif
1507
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001508 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1509 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001510
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001511 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001512 ret = 1;
1513 else
1514 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001515
1516 return ret;
1517}
1518
1519/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001520 * This is a double for. Do not use 'break' to break out of the loop,
1521 * you must use a goto.
1522 */
1523#define do_for_each_ftrace_rec(pg, rec) \
1524 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1525 int _____i; \
1526 for (_____i = 0; _____i < pg->index; _____i++) { \
1527 rec = &pg->records[_____i];
1528
1529#define while_for_each_ftrace_rec() \
1530 } \
1531 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301532
Steven Rostedt5855fea2011-12-16 19:27:42 -05001533
1534static int ftrace_cmp_recs(const void *a, const void *b)
1535{
Steven Rostedta650e022012-04-25 13:48:13 -04001536 const struct dyn_ftrace *key = a;
1537 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001538
Steven Rostedta650e022012-04-25 13:48:13 -04001539 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001540 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001541 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1542 return 1;
1543 return 0;
1544}
1545
Michael Ellerman04cf31a2016-03-24 22:04:01 +11001546/**
1547 * ftrace_location_range - return the first address of a traced location
1548 * if it touches the given ip range
1549 * @start: start of range to search.
1550 * @end: end of range to search (inclusive). @end points to the last byte
1551 * to check.
1552 *
1553 * Returns rec->ip if the related ftrace location is a least partly within
1554 * the given address range. That is, the first address of the instruction
1555 * that is either a NOP or call to the function tracer. It checks the ftrace
1556 * internal tables to determine if the address belongs or not.
1557 */
1558unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001559{
1560 struct ftrace_page *pg;
1561 struct dyn_ftrace *rec;
1562 struct dyn_ftrace key;
1563
1564 key.ip = start;
1565 key.flags = end; /* overload flags, as it is unsigned long */
1566
1567 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1568 if (end < pg->records[0].ip ||
1569 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1570 continue;
1571 rec = bsearch(&key, pg->records, pg->index,
1572 sizeof(struct dyn_ftrace),
1573 ftrace_cmp_recs);
1574 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001575 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001576 }
1577
Steven Rostedt5855fea2011-12-16 19:27:42 -05001578 return 0;
1579}
1580
Steven Rostedtc88fd862011-08-16 09:53:39 -04001581/**
1582 * ftrace_location - return true if the ip giving is a traced location
1583 * @ip: the instruction pointer to check
1584 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001585 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001586 * That is, the instruction that is either a NOP or call to
1587 * the function tracer. It checks the ftrace internal tables to
1588 * determine if the address belongs or not.
1589 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001590unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001591{
Steven Rostedta650e022012-04-25 13:48:13 -04001592 return ftrace_location_range(ip, ip);
1593}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001594
Steven Rostedta650e022012-04-25 13:48:13 -04001595/**
1596 * ftrace_text_reserved - return true if range contains an ftrace location
1597 * @start: start of range to search
1598 * @end: end of range to search (inclusive). @end points to the last byte to check.
1599 *
1600 * Returns 1 if @start and @end contains a ftrace location.
1601 * That is, the instruction that is either a NOP or call to
1602 * the function tracer. It checks the ftrace internal tables to
1603 * determine if the address belongs or not.
1604 */
Sasha Levind88471c2013-01-09 18:09:20 -05001605int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001606{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001607 unsigned long ret;
1608
1609 ret = ftrace_location_range((unsigned long)start,
1610 (unsigned long)end);
1611
1612 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001613}
1614
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001615/* Test if ops registered to this rec needs regs */
1616static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1617{
1618 struct ftrace_ops *ops;
1619 bool keep_regs = false;
1620
1621 for (ops = ftrace_ops_list;
1622 ops != &ftrace_list_end; ops = ops->next) {
1623 /* pass rec in as regs to have non-NULL val */
1624 if (ftrace_ops_test(ops, rec->ip, rec)) {
1625 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1626 keep_regs = true;
1627 break;
1628 }
1629 }
1630 }
1631
1632 return keep_regs;
1633}
1634
Cheng Jian81c09ba2019-05-04 19:39:39 +08001635static struct ftrace_ops *
1636ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1637static struct ftrace_ops *
1638ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1639
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001640static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001641 int filter_hash,
1642 bool inc)
1643{
1644 struct ftrace_hash *hash;
1645 struct ftrace_hash *other_hash;
1646 struct ftrace_page *pg;
1647 struct dyn_ftrace *rec;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001648 bool update = false;
Steven Rostedted926f92011-05-03 13:25:24 -04001649 int count = 0;
1650 int all = 0;
1651
1652 /* Only update if the ops has been registered */
1653 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001654 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001655
1656 /*
1657 * In the filter_hash case:
1658 * If the count is zero, we update all records.
1659 * Otherwise we just update the items in the hash.
1660 *
1661 * In the notrace_hash case:
1662 * We enable the update in the hash.
1663 * As disabling notrace means enabling the tracing,
1664 * and enabling notrace means disabling, the inc variable
1665 * gets inversed.
1666 */
1667 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001668 hash = ops->func_hash->filter_hash;
1669 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001670 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001671 all = 1;
1672 } else {
1673 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001674 hash = ops->func_hash->notrace_hash;
1675 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001676 /*
1677 * If the notrace hash has no items,
1678 * then there's nothing to do.
1679 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001680 if (ftrace_hash_empty(hash))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001681 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001682 }
1683
1684 do_for_each_ftrace_rec(pg, rec) {
1685 int in_other_hash = 0;
1686 int in_hash = 0;
1687 int match = 0;
1688
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05001689 if (rec->flags & FTRACE_FL_DISABLED)
1690 continue;
1691
Steven Rostedted926f92011-05-03 13:25:24 -04001692 if (all) {
1693 /*
1694 * Only the filter_hash affects all records.
1695 * Update if the record is not in the notrace hash.
1696 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001697 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001698 match = 1;
1699 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001700 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1701 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001702
1703 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001704 * If filter_hash is set, we want to match all functions
1705 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001706 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001707 * If filter_hash is not set, then we are decrementing.
1708 * That means we match anything that is in the hash
1709 * and also in the other_hash. That is, we need to turn
1710 * off functions in the other hash because they are disabled
1711 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001712 */
1713 if (filter_hash && in_hash && !in_other_hash)
1714 match = 1;
1715 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001716 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001717 match = 1;
1718 }
1719 if (!match)
1720 continue;
1721
1722 if (inc) {
1723 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001724 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001725 return false;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001726
1727 /*
1728 * If there's only a single callback registered to a
1729 * function, and the ops has a trampoline registered
1730 * for it, then we can call it directly.
1731 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001732 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001733 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001734 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001735 /*
1736 * If we are adding another function callback
1737 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001738 * custom trampoline in use, then we need to go
1739 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001740 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001741 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001742
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001743 /*
1744 * If any ops wants regs saved for this function
1745 * then all ops will get saved regs.
1746 */
1747 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1748 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001749 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001750 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001751 return false;
Steven Rostedted926f92011-05-03 13:25:24 -04001752 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001753
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001754 /*
1755 * If the rec had REGS enabled and the ops that is
1756 * being removed had REGS set, then see if there is
1757 * still any ops for this record that wants regs.
1758 * If not, we can stop recording them.
1759 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001760 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001761 rec->flags & FTRACE_FL_REGS &&
1762 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1763 if (!test_rec_ops_needs_regs(rec))
1764 rec->flags &= ~FTRACE_FL_REGS;
1765 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001766
1767 /*
Cheng Jian81c09ba2019-05-04 19:39:39 +08001768 * The TRAMP needs to be set only if rec count
1769 * is decremented to one, and the ops that is
1770 * left has a trampoline. As TRAMP can only be
1771 * enabled if there is only a single ops attached
1772 * to it.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001773 */
Cheng Jian81c09ba2019-05-04 19:39:39 +08001774 if (ftrace_rec_count(rec) == 1 &&
1775 ftrace_find_tramp_ops_any(rec))
1776 rec->flags |= FTRACE_FL_TRAMP;
1777 else
1778 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001779
1780 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001781 * flags will be cleared in ftrace_check_record()
1782 * if rec count is zero.
1783 */
Steven Rostedted926f92011-05-03 13:25:24 -04001784 }
1785 count++;
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001786
1787 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1788 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1789
Steven Rostedted926f92011-05-03 13:25:24 -04001790 /* Shortcut, if we handled all records, we are done. */
1791 if (!all && count == hash->count)
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001792 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001793 } while_for_each_ftrace_rec();
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001794
1795 return update;
Steven Rostedted926f92011-05-03 13:25:24 -04001796}
1797
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001798static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001799 int filter_hash)
1800{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001801 return __ftrace_hash_rec_update(ops, filter_hash, 0);
Steven Rostedted926f92011-05-03 13:25:24 -04001802}
1803
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001804static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
Steven Rostedted926f92011-05-03 13:25:24 -04001805 int filter_hash)
1806{
Jiri Olsa84b6d3e2016-03-16 15:34:32 +01001807 return __ftrace_hash_rec_update(ops, filter_hash, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04001808}
1809
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001810static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1811 int filter_hash, int inc)
1812{
1813 struct ftrace_ops *op;
1814
1815 __ftrace_hash_rec_update(ops, filter_hash, inc);
1816
1817 if (ops->func_hash != &global_ops.local_hash)
1818 return;
1819
1820 /*
1821 * If the ops shares the global_ops hash, then we need to update
1822 * all ops that are enabled and use this hash.
1823 */
1824 do_for_each_ftrace_op(op, ftrace_ops_list) {
1825 /* Already done */
1826 if (op == ops)
1827 continue;
1828 if (op->func_hash == &global_ops.local_hash)
1829 __ftrace_hash_rec_update(op, filter_hash, inc);
1830 } while_for_each_ftrace_op(op);
1831}
1832
1833static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1834 int filter_hash)
1835{
1836 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1837}
1838
1839static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1840 int filter_hash)
1841{
1842 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1843}
1844
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001845/*
1846 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1847 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1848 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1849 * Note that old_hash and new_hash has below meanings
1850 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1851 * - If the hash is EMPTY_HASH, it hits nothing
1852 * - Anything else hits the recs which match the hash entries.
1853 */
1854static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1855 struct ftrace_hash *old_hash,
1856 struct ftrace_hash *new_hash)
1857{
1858 struct ftrace_page *pg;
1859 struct dyn_ftrace *rec, *end = NULL;
1860 int in_old, in_new;
1861
1862 /* Only update if the ops has been registered */
1863 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1864 return 0;
1865
1866 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1867 return 0;
1868
1869 /*
1870 * Since the IPMODIFY is a very address sensitive action, we do not
1871 * allow ftrace_ops to set all functions to new hash.
1872 */
1873 if (!new_hash || !old_hash)
1874 return -EINVAL;
1875
1876 /* Update rec->flags */
1877 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001878
1879 if (rec->flags & FTRACE_FL_DISABLED)
1880 continue;
1881
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001882 /* We need to update only differences of filter_hash */
1883 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1884 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1885 if (in_old == in_new)
1886 continue;
1887
1888 if (in_new) {
1889 /* New entries must ensure no others are using it */
1890 if (rec->flags & FTRACE_FL_IPMODIFY)
1891 goto rollback;
1892 rec->flags |= FTRACE_FL_IPMODIFY;
1893 } else /* Removed entry */
1894 rec->flags &= ~FTRACE_FL_IPMODIFY;
1895 } while_for_each_ftrace_rec();
1896
1897 return 0;
1898
1899rollback:
1900 end = rec;
1901
1902 /* Roll back what we did above */
1903 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05001904
1905 if (rec->flags & FTRACE_FL_DISABLED)
1906 continue;
1907
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001908 if (rec == end)
1909 goto err_out;
1910
1911 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1912 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1913 if (in_old == in_new)
1914 continue;
1915
1916 if (in_new)
1917 rec->flags &= ~FTRACE_FL_IPMODIFY;
1918 else
1919 rec->flags |= FTRACE_FL_IPMODIFY;
1920 } while_for_each_ftrace_rec();
1921
1922err_out:
1923 return -EBUSY;
1924}
1925
1926static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1927{
1928 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1929
1930 if (ftrace_hash_empty(hash))
1931 hash = NULL;
1932
1933 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1934}
1935
1936/* Disabling always succeeds */
1937static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1938{
1939 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1940
1941 if (ftrace_hash_empty(hash))
1942 hash = NULL;
1943
1944 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1945}
1946
1947static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1948 struct ftrace_hash *new_hash)
1949{
1950 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1951
1952 if (ftrace_hash_empty(old_hash))
1953 old_hash = NULL;
1954
1955 if (ftrace_hash_empty(new_hash))
1956 new_hash = NULL;
1957
1958 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1959}
1960
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001961static void print_ip_ins(const char *fmt, const unsigned char *p)
Steven Rostedt05736a42008-09-22 14:55:47 -07001962{
1963 int i;
1964
1965 printk(KERN_CONT "%s", fmt);
1966
1967 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1968 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1969}
1970
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001971enum ftrace_bug_type ftrace_bug_type;
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05001972const void *ftrace_expected;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05001973
1974static void print_bug_type(void)
1975{
1976 switch (ftrace_bug_type) {
1977 case FTRACE_BUG_UNKNOWN:
1978 break;
1979 case FTRACE_BUG_INIT:
1980 pr_info("Initializing ftrace call sites\n");
1981 break;
1982 case FTRACE_BUG_NOP:
1983 pr_info("Setting ftrace call site to NOP\n");
1984 break;
1985 case FTRACE_BUG_CALL:
1986 pr_info("Setting ftrace call site to call ftrace function\n");
1987 break;
1988 case FTRACE_BUG_UPDATE:
1989 pr_info("Updating ftrace call site to call a different ftrace function\n");
1990 break;
1991 }
1992}
1993
Steven Rostedtc88fd862011-08-16 09:53:39 -04001994/**
1995 * ftrace_bug - report and shutdown function tracer
1996 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001997 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001998 *
1999 * The arch code that enables or disables the function tracing
2000 * can call ftrace_bug() when it has detected a problem in
2001 * modifying the code. @failed should be one of either:
2002 * EFAULT - if the problem happens on reading the @ip address
2003 * EINVAL - if what is read at @ip is not what was expected
2004 * EPERM - if the problem happens on writting to the @ip address
2005 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002006void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002007{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002008 unsigned long ip = rec ? rec->ip : 0;
2009
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002010 switch (failed) {
2011 case -EFAULT:
2012 FTRACE_WARN_ON_ONCE(1);
2013 pr_info("ftrace faulted on modifying ");
2014 print_ip_sym(ip);
2015 break;
2016 case -EINVAL:
2017 FTRACE_WARN_ON_ONCE(1);
2018 pr_info("ftrace failed to modify ");
2019 print_ip_sym(ip);
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002020 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002021 pr_cont("\n");
Steven Rostedt (Red Hat)b05086c2015-11-25 14:13:11 -05002022 if (ftrace_expected) {
2023 print_ip_ins(" expected: ", ftrace_expected);
2024 pr_cont("\n");
2025 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002026 break;
2027 case -EPERM:
2028 FTRACE_WARN_ON_ONCE(1);
2029 pr_info("ftrace faulted on writing ");
2030 print_ip_sym(ip);
2031 break;
2032 default:
2033 FTRACE_WARN_ON_ONCE(1);
2034 pr_info("ftrace faulted on unknown error ");
2035 print_ip_sym(ip);
2036 }
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002037 print_bug_type();
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002038 if (rec) {
2039 struct ftrace_ops *ops = NULL;
2040
2041 pr_info("ftrace record flags: %lx\n", rec->flags);
2042 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2043 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2044 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2045 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002046 if (ops) {
2047 do {
2048 pr_cont("\ttramp: %pS (%pS)",
2049 (void *)ops->trampoline,
2050 (void *)ops->func);
2051 ops = ftrace_find_tramp_ops_next(rec, ops);
2052 } while (ops);
2053 } else
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002054 pr_cont("\ttramp: ERROR!");
2055
2056 }
2057 ip = ftrace_get_addr_curr(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002058 pr_cont("\n expected tramp: %lx\n", ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002059 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08002060}
2061
Steven Rostedtc88fd862011-08-16 09:53:39 -04002062static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02002063{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002064 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01002065
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002066 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2067
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002068 if (rec->flags & FTRACE_FL_DISABLED)
2069 return FTRACE_UPDATE_IGNORE;
2070
Steven Rostedt982c3502008-11-15 16:31:41 -05002071 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002072 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05002073 *
Steven Rostedted926f92011-05-03 13:25:24 -04002074 * If the record has a ref count, then we need to enable it
2075 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05002076 *
Steven Rostedted926f92011-05-03 13:25:24 -04002077 * Otherwise we make sure its disabled.
2078 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002079 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04002080 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05002081 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002082 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04002083 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02002084
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002085 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002086 * If enabling and the REGS flag does not match the REGS_EN, or
2087 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2088 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002089 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002090 if (flag) {
2091 if (!(rec->flags & FTRACE_FL_REGS) !=
2092 !(rec->flags & FTRACE_FL_REGS_EN))
2093 flag |= FTRACE_FL_REGS;
2094
2095 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2096 !(rec->flags & FTRACE_FL_TRAMP_EN))
2097 flag |= FTRACE_FL_TRAMP;
2098 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002099
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002100 /* If the state of this record hasn't changed, then do nothing */
2101 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002102 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002103
2104 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002105 /* Save off if rec is being enabled (for return value) */
2106 flag ^= rec->flags & FTRACE_FL_ENABLED;
2107
2108 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002109 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002110 if (flag & FTRACE_FL_REGS) {
2111 if (rec->flags & FTRACE_FL_REGS)
2112 rec->flags |= FTRACE_FL_REGS_EN;
2113 else
2114 rec->flags &= ~FTRACE_FL_REGS_EN;
2115 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002116 if (flag & FTRACE_FL_TRAMP) {
2117 if (rec->flags & FTRACE_FL_TRAMP)
2118 rec->flags |= FTRACE_FL_TRAMP_EN;
2119 else
2120 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2121 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002122 }
2123
2124 /*
2125 * If this record is being updated from a nop, then
2126 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002127 * Otherwise,
2128 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002129 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002130 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002131 */
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002132 if (flag & FTRACE_FL_ENABLED) {
2133 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002134 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002135 }
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002136
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002137 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002138 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002139 }
2140
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002141 if (update) {
2142 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002143 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002144 rec->flags = 0;
2145 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002146 /*
2147 * Just disable the record, but keep the ops TRAMP
2148 * and REGS states. The _EN flags must be disabled though.
2149 */
2150 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2151 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002152 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002153
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002154 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002155 return FTRACE_UPDATE_MAKE_NOP;
2156}
2157
2158/**
2159 * ftrace_update_record, set a record that now is tracing or not
2160 * @rec: the record to update
2161 * @enable: set to 1 if the record is tracing, zero to force disable
2162 *
2163 * The records that represent all functions that can be traced need
2164 * to be updated when tracing has been enabled.
2165 */
2166int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2167{
2168 return ftrace_check_record(rec, enable, 1);
2169}
2170
2171/**
2172 * ftrace_test_record, check if the record has been enabled or not
2173 * @rec: the record to test
2174 * @enable: set to 1 to check if enabled, 0 if it is disabled
2175 *
2176 * The arch code may need to test if a record is already set to
2177 * tracing to determine how to modify the function code that it
2178 * represents.
2179 */
2180int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2181{
2182 return ftrace_check_record(rec, enable, 0);
2183}
2184
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002185static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002186ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2187{
2188 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002189 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002190
2191 do_for_each_ftrace_op(op, ftrace_ops_list) {
2192
2193 if (!op->trampoline)
2194 continue;
2195
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002196 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002197 return op;
2198 } while_for_each_ftrace_op(op);
2199
2200 return NULL;
2201}
2202
2203static struct ftrace_ops *
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05002204ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2205 struct ftrace_ops *op)
2206{
2207 unsigned long ip = rec->ip;
2208
2209 while_for_each_ftrace_op(op) {
2210
2211 if (!op->trampoline)
2212 continue;
2213
2214 if (hash_contains_ip(ip, op->func_hash))
2215 return op;
2216 }
2217
2218 return NULL;
2219}
2220
2221static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002222ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2223{
2224 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002225 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002226
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002227 /*
2228 * Need to check removed ops first.
2229 * If they are being removed, and this rec has a tramp,
2230 * and this rec is in the ops list, then it would be the
2231 * one with the tramp.
2232 */
2233 if (removed_ops) {
2234 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002235 return removed_ops;
2236 }
2237
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002238 /*
2239 * Need to find the current trampoline for a rec.
2240 * Now, a trampoline is only attached to a rec if there
2241 * was a single 'ops' attached to it. But this can be called
2242 * when we are adding another op to the rec or removing the
2243 * current one. Thus, if the op is being added, we can
2244 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002245 * yet.
2246 *
2247 * If an ops is being modified (hooking to different functions)
2248 * then we don't care about the new functions that are being
2249 * added, just the old ones (that are probably being removed).
2250 *
2251 * If we are adding an ops to a function that already is using
2252 * a trampoline, it needs to be removed (trampolines are only
2253 * for single ops connected), then an ops that is not being
2254 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002255 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002256 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002257
2258 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002259 continue;
2260
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002261 /*
2262 * If the ops is being added, it hasn't gotten to
2263 * the point to be removed from this tree yet.
2264 */
2265 if (op->flags & FTRACE_OPS_FL_ADDING)
2266 continue;
2267
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002268
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002269 /*
2270 * If the ops is being modified and is in the old
2271 * hash, then it is probably being removed from this
2272 * function.
2273 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002274 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2275 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002276 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002277 /*
2278 * If the ops is not being added or modified, and it's
2279 * in its normal filter hash, then this must be the one
2280 * we want!
2281 */
2282 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2283 hash_contains_ip(ip, op->func_hash))
2284 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002285
2286 } while_for_each_ftrace_op(op);
2287
2288 return NULL;
2289}
2290
2291static struct ftrace_ops *
2292ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2293{
2294 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002295 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002296
2297 do_for_each_ftrace_op(op, ftrace_ops_list) {
2298 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002299 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002300 return op;
2301 } while_for_each_ftrace_op(op);
2302
2303 return NULL;
2304}
2305
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002306/**
2307 * ftrace_get_addr_new - Get the call address to set to
2308 * @rec: The ftrace record descriptor
2309 *
2310 * If the record has the FTRACE_FL_REGS set, that means that it
2311 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2312 * is not not set, then it wants to convert to the normal callback.
2313 *
2314 * Returns the address of the trampoline to set to
2315 */
2316unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2317{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002318 struct ftrace_ops *ops;
2319
2320 /* Trampolines take precedence over regs */
2321 if (rec->flags & FTRACE_FL_TRAMP) {
2322 ops = ftrace_find_tramp_ops_new(rec);
2323 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002324 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2325 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002326 /* Ftrace is shutting down, return anything */
2327 return (unsigned long)FTRACE_ADDR;
2328 }
2329 return ops->trampoline;
2330 }
2331
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002332 if (rec->flags & FTRACE_FL_REGS)
2333 return (unsigned long)FTRACE_REGS_ADDR;
2334 else
2335 return (unsigned long)FTRACE_ADDR;
2336}
2337
2338/**
2339 * ftrace_get_addr_curr - Get the call address that is already there
2340 * @rec: The ftrace record descriptor
2341 *
2342 * The FTRACE_FL_REGS_EN is set when the record already points to
2343 * a function that saves all the regs. Basically the '_EN' version
2344 * represents the current state of the function.
2345 *
2346 * Returns the address of the trampoline that is currently being called
2347 */
2348unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2349{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002350 struct ftrace_ops *ops;
2351
2352 /* Trampolines take precedence over regs */
2353 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2354 ops = ftrace_find_tramp_ops_curr(rec);
2355 if (FTRACE_WARN_ON(!ops)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07002356 pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2357 (void *)rec->ip, (void *)rec->ip);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002358 /* Ftrace is shutting down, return anything */
2359 return (unsigned long)FTRACE_ADDR;
2360 }
2361 return ops->trampoline;
2362 }
2363
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002364 if (rec->flags & FTRACE_FL_REGS_EN)
2365 return (unsigned long)FTRACE_REGS_ADDR;
2366 else
2367 return (unsigned long)FTRACE_ADDR;
2368}
2369
Steven Rostedtc88fd862011-08-16 09:53:39 -04002370static int
2371__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2372{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002373 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002374 unsigned long ftrace_addr;
2375 int ret;
2376
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002377 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002378
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002379 /* This needs to be done before we call ftrace_update_record */
2380 ftrace_old_addr = ftrace_get_addr_curr(rec);
2381
2382 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002383
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002384 ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2385
Steven Rostedtc88fd862011-08-16 09:53:39 -04002386 switch (ret) {
2387 case FTRACE_UPDATE_IGNORE:
2388 return 0;
2389
2390 case FTRACE_UPDATE_MAKE_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002391 ftrace_bug_type = FTRACE_BUG_CALL;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002392 return ftrace_make_call(rec, ftrace_addr);
2393
2394 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002395 ftrace_bug_type = FTRACE_BUG_NOP;
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002396 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002397
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002398 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002399 ftrace_bug_type = FTRACE_BUG_UPDATE;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002400 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002401 }
2402
2403 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002404}
2405
Steven Rostedte4f5d542012-04-27 09:13:18 -04002406void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002407{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002408 struct dyn_ftrace *rec;
2409 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002410 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002411
Steven Rostedt45a4a232011-04-21 23:16:46 -04002412 if (unlikely(ftrace_disabled))
2413 return;
2414
Steven Rostedt265c8312009-02-13 12:43:56 -05002415 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05002416
2417 if (rec->flags & FTRACE_FL_DISABLED)
2418 continue;
2419
Steven Rostedte4f5d542012-04-27 09:13:18 -04002420 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002421 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002422 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002423 /* Stop processing */
2424 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002425 }
2426 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002427}
2428
Steven Rostedtc88fd862011-08-16 09:53:39 -04002429struct ftrace_rec_iter {
2430 struct ftrace_page *pg;
2431 int index;
2432};
2433
2434/**
2435 * ftrace_rec_iter_start, start up iterating over traced functions
2436 *
2437 * Returns an iterator handle that is used to iterate over all
2438 * the records that represent address locations where functions
2439 * are traced.
2440 *
2441 * May return NULL if no records are available.
2442 */
2443struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2444{
2445 /*
2446 * We only use a single iterator.
2447 * Protected by the ftrace_lock mutex.
2448 */
2449 static struct ftrace_rec_iter ftrace_rec_iter;
2450 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2451
2452 iter->pg = ftrace_pages_start;
2453 iter->index = 0;
2454
2455 /* Could have empty pages */
2456 while (iter->pg && !iter->pg->index)
2457 iter->pg = iter->pg->next;
2458
2459 if (!iter->pg)
2460 return NULL;
2461
2462 return iter;
2463}
2464
2465/**
2466 * ftrace_rec_iter_next, get the next record to process.
2467 * @iter: The handle to the iterator.
2468 *
2469 * Returns the next iterator after the given iterator @iter.
2470 */
2471struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2472{
2473 iter->index++;
2474
2475 if (iter->index >= iter->pg->index) {
2476 iter->pg = iter->pg->next;
2477 iter->index = 0;
2478
2479 /* Could have empty pages */
2480 while (iter->pg && !iter->pg->index)
2481 iter->pg = iter->pg->next;
2482 }
2483
2484 if (!iter->pg)
2485 return NULL;
2486
2487 return iter;
2488}
2489
2490/**
2491 * ftrace_rec_iter_record, get the record at the iterator location
2492 * @iter: The current iterator location
2493 *
2494 * Returns the record that the current @iter is at.
2495 */
2496struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2497{
2498 return &iter->pg->records[iter->index];
2499}
2500
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302501static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002502ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002503{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002504 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002505
Steven Rostedt45a4a232011-04-21 23:16:46 -04002506 if (unlikely(ftrace_disabled))
2507 return 0;
2508
Shaohua Li25aac9d2009-01-09 11:29:40 +08002509 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002510 if (ret) {
Steven Rostedt (Red Hat)02a392a2015-11-25 12:50:47 -05002511 ftrace_bug_type = FTRACE_BUG_INIT;
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002512 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302513 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002514 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302515 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002516}
2517
Steven Rostedt000ab692009-02-17 13:35:06 -05002518/*
2519 * archs can override this function if they must do something
2520 * before the modifying code is performed.
2521 */
2522int __weak ftrace_arch_code_modify_prepare(void)
2523{
2524 return 0;
2525}
2526
2527/*
2528 * archs can override this function if they must do something
2529 * after the modifying code is performed.
2530 */
2531int __weak ftrace_arch_code_modify_post_process(void)
2532{
2533 return 0;
2534}
2535
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002536void ftrace_modify_all_code(int command)
2537{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002538 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002539 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002540
2541 /*
2542 * If the ftrace_caller calls a ftrace_ops func directly,
2543 * we need to make sure that it only traces functions it
2544 * expects to trace. When doing the switch of functions,
2545 * we need to update to the ftrace_ops_list_func first
2546 * before the transition between old and new calls are set,
2547 * as the ftrace_ops_list_func will check the ops hashes
2548 * to make sure the ops are having the right functions
2549 * traced.
2550 */
Petr Mladekcd210672014-02-24 17:12:21 +01002551 if (update) {
2552 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2553 if (FTRACE_WARN_ON(err))
2554 return;
2555 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002556
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002557 if (command & FTRACE_UPDATE_CALLS)
2558 ftrace_replace_code(1);
2559 else if (command & FTRACE_DISABLE_CALLS)
2560 ftrace_replace_code(0);
2561
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002562 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2563 function_trace_op = set_function_trace_op;
2564 smp_wmb();
2565 /* If irqs are disabled, we are in stop machine */
2566 if (!irqs_disabled())
2567 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002568 err = ftrace_update_ftrace_func(ftrace_trace_function);
2569 if (FTRACE_WARN_ON(err))
2570 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002571 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002572
2573 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002574 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002575 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002576 err = ftrace_disable_ftrace_graph_caller();
2577 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002578}
2579
Ingo Molnare309b412008-05-12 21:20:51 +02002580static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002581{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002582 int *command = data;
2583
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002584 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002585
Steven Rostedtc88fd862011-08-16 09:53:39 -04002586 return 0;
2587}
2588
2589/**
2590 * ftrace_run_stop_machine, go back to the stop machine method
2591 * @command: The command to tell ftrace what to do
2592 *
2593 * If an arch needs to fall back to the stop machine method, the
2594 * it can call this function.
2595 */
2596void ftrace_run_stop_machine(int command)
2597{
2598 stop_machine(__ftrace_modify_code, &command, NULL);
2599}
2600
2601/**
2602 * arch_ftrace_update_code, modify the code to trace or not trace
2603 * @command: The command that needs to be done
2604 *
2605 * Archs can override this function if it does not need to
2606 * run stop_machine() to modify code.
2607 */
2608void __weak arch_ftrace_update_code(int command)
2609{
2610 ftrace_run_stop_machine(command);
2611}
2612
2613static void ftrace_run_update_code(int command)
2614{
2615 int ret;
2616
2617 ret = ftrace_arch_code_modify_prepare();
2618 FTRACE_WARN_ON(ret);
2619 if (ret)
2620 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002621
2622 /*
2623 * By default we use stop_machine() to modify the code.
2624 * But archs can do what ever they want as long as it
2625 * is safe. The stop_machine() is the safest, but also
2626 * produces the most overhead.
2627 */
2628 arch_ftrace_update_code(command);
2629
Steven Rostedt000ab692009-02-17 13:35:06 -05002630 ret = ftrace_arch_code_modify_post_process();
2631 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002632}
2633
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002634static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002635 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002636{
2637 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002638 ops->old_hash.filter_hash = old_hash->filter_hash;
2639 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002640 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002641 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002642 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002643 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2644}
2645
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002646static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002647static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002648
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002649void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2650{
2651}
2652
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002653static void per_cpu_ops_free(struct ftrace_ops *ops)
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002654{
2655 free_percpu(ops->disabled);
2656}
2657
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002658static void ftrace_startup_enable(int command)
2659{
2660 if (saved_ftrace_func != ftrace_trace_function) {
2661 saved_ftrace_func = ftrace_trace_function;
2662 command |= FTRACE_UPDATE_TRACE_FUNC;
2663 }
2664
2665 if (!command || !ftrace_enabled)
2666 return;
2667
2668 ftrace_run_update_code(command);
2669}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002670
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002671static void ftrace_startup_all(int command)
2672{
2673 update_all_ops = true;
2674 ftrace_startup_enable(command);
2675 update_all_ops = false;
2676}
2677
Steven Rostedta1cd6172011-05-23 15:24:25 -04002678static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002679{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002680 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002681
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002682 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002683 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002684
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002685 ret = __register_ftrace_function(ops);
2686 if (ret)
2687 return ret;
2688
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002689 ftrace_start_up++;
Steven Rostedt3d083392008-05-12 21:20:42 +02002690
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002691 /*
2692 * Note that ftrace probes uses this to start up
2693 * and modify functions it will probe. But we still
2694 * set the ADDING flag for modification, as probes
2695 * do not have trampolines. If they add them in the
2696 * future, then the probes will need to distinguish
2697 * between adding and updating probes.
2698 */
2699 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002700
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002701 ret = ftrace_hash_ipmodify_enable(ops);
2702 if (ret < 0) {
2703 /* Rollback registration process */
2704 __unregister_ftrace_function(ops);
2705 ftrace_start_up--;
2706 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2707 return ret;
2708 }
2709
Jiri Olsa7f50d062016-03-16 15:34:33 +01002710 if (ftrace_hash_rec_enable(ops, 1))
2711 command |= FTRACE_UPDATE_CALLS;
Steven Rostedted926f92011-05-03 13:25:24 -04002712
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002713 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002714
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002715 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2716
Steven Rostedta1cd6172011-05-23 15:24:25 -04002717 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002718}
2719
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002720static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002721{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002722 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002723
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002724 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002725 return -ENODEV;
2726
2727 ret = __unregister_ftrace_function(ops);
2728 if (ret)
2729 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002730
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002731 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002732 /*
2733 * Just warn in case of unbalance, no need to kill ftrace, it's not
2734 * critical but the ftrace_call callers may be never nopped again after
2735 * further ftrace uses.
2736 */
2737 WARN_ON_ONCE(ftrace_start_up < 0);
2738
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002739 /* Disabling ipmodify never fails */
2740 ftrace_hash_ipmodify_disable(ops);
Jiri Olsa7f50d062016-03-16 15:34:33 +01002741
2742 if (ftrace_hash_rec_disable(ops, 1))
2743 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtb8489142011-05-04 09:27:52 -04002744
Namhyung Kima737e6d2014-06-12 23:56:12 +09002745 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002746
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002747 if (saved_ftrace_func != ftrace_trace_function) {
2748 saved_ftrace_func = ftrace_trace_function;
2749 command |= FTRACE_UPDATE_TRACE_FUNC;
2750 }
2751
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002752 if (!command || !ftrace_enabled) {
2753 /*
Steven Rostedt (VMware)100553e2017-09-01 12:18:28 -04002754 * If these are dynamic or per_cpu ops, they still
2755 * need their data freed. Since, function tracing is
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002756 * not currently active, we can just free them
2757 * without synchronizing all CPUs.
2758 */
Steven Rostedt (VMware)100553e2017-09-01 12:18:28 -04002759 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2760 goto free_ops;
2761
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002762 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002763 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002764
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002765 /*
2766 * If the ops uses a trampoline, then it needs to be
2767 * tested first on update.
2768 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002769 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002770 removed_ops = ops;
2771
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002772 /* The trampoline logic checks the old hashes */
2773 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2774 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2775
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002776 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002777
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002778 /*
2779 * If there's no more ops registered with ftrace, run a
2780 * sanity check to make sure all rec flags are cleared.
2781 */
2782 if (ftrace_ops_list == &ftrace_list_end) {
2783 struct ftrace_page *pg;
2784 struct dyn_ftrace *rec;
2785
2786 do_for_each_ftrace_rec(pg, rec) {
Alexei Starovoitov977c1f92016-11-07 15:14:20 -08002787 if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002788 pr_warn(" %pS flags:%lx\n",
2789 (void *)rec->ip, rec->flags);
2790 } while_for_each_ftrace_rec();
2791 }
2792
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002793 ops->old_hash.filter_hash = NULL;
2794 ops->old_hash.notrace_hash = NULL;
2795
2796 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002797 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002798
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002799 /*
2800 * Dynamic ops may be freed, we must make sure that all
2801 * callers are done before leaving this function.
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002802 * The same goes for freeing the per_cpu data of the per_cpu
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002803 * ops.
2804 *
2805 * Again, normal synchronize_sched() is not good enough.
2806 * We need to do a hard force of sched synchronization.
2807 * This is because we use preempt_disable() to do RCU, but
2808 * the function tracers can be called where RCU is not watching
2809 * (like before user_exit()). We can not rely on the RCU
2810 * infrastructure to do the synchronization, thus we must do it
2811 * ourselves.
2812 */
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002813 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002814 schedule_on_each_cpu(ftrace_sync);
2815
Steven Rostedt (VMware)100553e2017-09-01 12:18:28 -04002816 free_ops:
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002817 arch_ftrace_trampoline_free(ops);
2818
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05002819 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2820 per_cpu_ops_free(ops);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002821 }
2822
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002823 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002824}
2825
Ingo Molnare309b412008-05-12 21:20:51 +02002826static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002827{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302828 int command;
2829
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002830 if (unlikely(ftrace_disabled))
2831 return;
2832
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002833 /* Force update next time */
2834 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002835 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302836 if (ftrace_start_up) {
2837 command = FTRACE_UPDATE_CALLS;
2838 if (ftrace_graph_active)
2839 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002840 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302841 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002842}
2843
Ingo Molnare309b412008-05-12 21:20:51 +02002844static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002845{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302846 int command;
2847
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002848 if (unlikely(ftrace_disabled))
2849 return;
2850
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002851 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302852 if (ftrace_start_up) {
2853 command = FTRACE_DISABLE_CALLS;
2854 if (ftrace_graph_active)
2855 command |= FTRACE_STOP_FUNC_RET;
2856 ftrace_run_update_code(command);
2857 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002858}
2859
Steven Rostedt3d083392008-05-12 21:20:42 +02002860static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002861unsigned long ftrace_update_tot_cnt;
2862
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002863static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002864{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002865 /*
2866 * Filter_hash being empty will default to trace module.
2867 * But notrace hash requires a test of individual module functions.
2868 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002869 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2870 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002871}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002872
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002873/*
2874 * Check if the current ops references the record.
2875 *
2876 * If the ops traces all functions, then it was already accounted for.
2877 * If the ops does not trace the current record function, skip it.
2878 * If the ops ignores the function via notrace filter, skip it.
2879 */
2880static inline bool
2881ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2882{
2883 /* If ops isn't enabled, ignore it */
2884 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2885 return 0;
2886
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002887 /* If ops traces all then it includes this function */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002888 if (ops_traces_mod(ops))
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002889 return 1;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002890
2891 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002892 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2893 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002894 return 0;
2895
2896 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002897 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002898 return 0;
2899
2900 return 1;
2901}
2902
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002903static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002904{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002905 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002906 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302907 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002908 unsigned long update_cnt = 0;
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002909 unsigned long rec_flags = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002910 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002911
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002912 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002913
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002914 /*
2915 * When a module is loaded, this function is called to convert
2916 * the calls to mcount in its text to nops, and also to create
2917 * an entry in the ftrace data. Now, if ftrace is activated
2918 * after this call, but before the module sets its text to
2919 * read-only, the modification of enabling ftrace can fail if
2920 * the read-only is done while ftrace is converting the calls.
2921 * To prevent this, the module's records are set as disabled
2922 * and will be enabled after the call to set the module's text
2923 * to read-only.
2924 */
2925 if (mod)
2926 rec_flags |= FTRACE_FL_DISABLED;
2927
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002928 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302929
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002930 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002931
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002932 /* If something went wrong, bail without enabling anything */
2933 if (unlikely(ftrace_disabled))
2934 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002935
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002936 p = &pg->records[i];
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05002937 p->flags = rec_flags;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302938
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002939 /*
2940 * Do the initial record conversion from mcount jump
2941 * to the NOP instructions.
2942 */
2943 if (!ftrace_code_disable(mod, p))
2944 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002945
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002946 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002947 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002948 }
2949
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002950 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002951 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002952 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002953
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002954 return 0;
2955}
2956
Steven Rostedta7900872011-12-16 16:23:44 -05002957static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002958{
Steven Rostedta7900872011-12-16 16:23:44 -05002959 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002960 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002961
Steven Rostedta7900872011-12-16 16:23:44 -05002962 if (WARN_ON(!count))
2963 return -EINVAL;
2964
2965 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002966
2967 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002968 * We want to fill as much as possible. No more than a page
2969 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002970 */
Steven Rostedta7900872011-12-16 16:23:44 -05002971 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2972 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002973
Steven Rostedta7900872011-12-16 16:23:44 -05002974 again:
2975 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2976
2977 if (!pg->records) {
2978 /* if we can't allocate this size, try something smaller */
2979 if (!order)
2980 return -ENOMEM;
2981 order >>= 1;
2982 goto again;
2983 }
2984
2985 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2986 pg->size = cnt;
2987
2988 if (cnt > count)
2989 cnt = count;
2990
2991 return cnt;
2992}
2993
2994static struct ftrace_page *
2995ftrace_allocate_pages(unsigned long num_to_init)
2996{
2997 struct ftrace_page *start_pg;
2998 struct ftrace_page *pg;
2999 int order;
3000 int cnt;
3001
3002 if (!num_to_init)
3003 return 0;
3004
3005 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3006 if (!pg)
3007 return NULL;
3008
3009 /*
3010 * Try to allocate as much as possible in one continues
3011 * location that fills in all of the space. We want to
3012 * waste as little space as possible.
3013 */
3014 for (;;) {
3015 cnt = ftrace_allocate_records(pg, num_to_init);
3016 if (cnt < 0)
3017 goto free_pages;
3018
3019 num_to_init -= cnt;
3020 if (!num_to_init)
3021 break;
3022
3023 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3024 if (!pg->next)
3025 goto free_pages;
3026
3027 pg = pg->next;
3028 }
3029
3030 return start_pg;
3031
3032 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09003033 pg = start_pg;
3034 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05003035 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3036 free_pages((unsigned long)pg->records, order);
3037 start_pg = pg->next;
3038 kfree(pg);
3039 pg = start_pg;
3040 }
3041 pr_info("ftrace: FAILED to allocate memory for functions\n");
3042 return NULL;
3043}
3044
Steven Rostedt5072c592008-05-12 21:20:43 +02003045#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3046
3047struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003048 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003049 loff_t func_pos;
3050 struct ftrace_page *pg;
3051 struct dyn_ftrace *func;
3052 struct ftrace_func_probe *probe;
3053 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003054 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003055 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003056 int hidx;
3057 int idx;
3058 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02003059};
3060
Ingo Molnare309b412008-05-12 21:20:51 +02003061static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003062t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003063{
3064 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003065 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003066 struct hlist_head *hhd;
3067
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003068 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003069 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003070
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003071 if (iter->probe)
3072 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003073 retry:
3074 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3075 return NULL;
3076
3077 hhd = &ftrace_func_hash[iter->hidx];
3078
3079 if (hlist_empty(hhd)) {
3080 iter->hidx++;
3081 hnd = NULL;
3082 goto retry;
3083 }
3084
3085 if (!hnd)
3086 hnd = hhd->first;
3087 else {
3088 hnd = hnd->next;
3089 if (!hnd) {
3090 iter->hidx++;
3091 goto retry;
3092 }
3093 }
3094
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003095 if (WARN_ON_ONCE(!hnd))
3096 return NULL;
3097
3098 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3099
3100 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003101}
3102
3103static void *t_hash_start(struct seq_file *m, loff_t *pos)
3104{
3105 struct ftrace_iterator *iter = m->private;
3106 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003107 loff_t l;
3108
Steven Rostedt69a30832011-12-19 15:21:16 -05003109 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3110 return NULL;
3111
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003112 if (iter->func_pos > *pos)
3113 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003114
Li Zefand82d6242009-06-24 09:54:54 +08003115 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003116 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003117 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003118 if (!p)
3119 break;
3120 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003121 if (!p)
3122 return NULL;
3123
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003124 /* Only set this if we have an item */
3125 iter->flags |= FTRACE_ITER_HASH;
3126
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003127 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003128}
3129
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003130static int
3131t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003132{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003133 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003134
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003135 rec = iter->probe;
3136 if (WARN_ON_ONCE(!rec))
3137 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003138
Steven Rostedt809dcf22009-02-16 23:06:01 -05003139 if (rec->ops->print)
3140 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3141
Steven Rostedtb375a112009-09-17 00:05:58 -04003142 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003143
3144 if (rec->data)
3145 seq_printf(m, ":%p", rec->data);
3146 seq_putc(m, '\n');
3147
3148 return 0;
3149}
3150
3151static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02003152t_next(struct seq_file *m, void *v, loff_t *pos)
3153{
3154 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003155 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003156 struct dyn_ftrace *rec = NULL;
3157
Steven Rostedt45a4a232011-04-21 23:16:46 -04003158 if (unlikely(ftrace_disabled))
3159 return NULL;
3160
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003161 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003162 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003163
Steven Rostedt5072c592008-05-12 21:20:43 +02003164 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01003165 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02003166
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003167 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003168 return t_hash_start(m, 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 Rostedt32082302011-12-16 14:42:37 -05003179 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003180 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05003181
Steven Rostedt41c52c02008-05-22 11:46:33 -04003182 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003183 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003184
3185 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003186 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003187
Steven Rostedt5072c592008-05-12 21:20:43 +02003188 rec = NULL;
3189 goto retry;
3190 }
3191 }
3192
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003193 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003194 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003195
3196 iter->func = rec;
3197
3198 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003199}
3200
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003201static void reset_iter_read(struct ftrace_iterator *iter)
3202{
3203 iter->pos = 0;
3204 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03003205 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003206}
3207
3208static void *t_start(struct seq_file *m, loff_t *pos)
3209{
3210 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003211 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003212 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003213 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003214
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003215 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003216
3217 if (unlikely(ftrace_disabled))
3218 return NULL;
3219
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003220 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003221 * If an lseek was done, then reset and start from beginning.
3222 */
3223 if (*pos < iter->pos)
3224 reset_iter_read(iter);
3225
3226 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003227 * For set_ftrace_filter reading, if we have the filter
3228 * off, we can short cut and just print out that all
3229 * functions are enabled.
3230 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003231 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003232 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003233 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003234 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003235 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003236 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003237 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003238 /* reset in case of seek/pread */
3239 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003240 return iter;
3241 }
3242
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003243 if (iter->flags & FTRACE_ITER_HASH)
3244 return t_hash_start(m, pos);
3245
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003246 /*
3247 * Unfortunately, we need to restart at ftrace_pages_start
3248 * every time we let go of the ftrace_mutex. This is because
3249 * those pointers can change without the lock.
3250 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003251 iter->pg = ftrace_pages_start;
3252 iter->idx = 0;
3253 for (l = 0; l <= *pos; ) {
3254 p = t_next(m, p, &l);
3255 if (!p)
3256 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003257 }
walimis5821e1b2008-11-15 15:19:06 +08003258
Steven Rostedt69a30832011-12-19 15:21:16 -05003259 if (!p)
3260 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003261
3262 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003263}
3264
3265static void t_stop(struct seq_file *m, void *p)
3266{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003267 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003268}
3269
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003270void * __weak
3271arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3272{
3273 return NULL;
3274}
3275
3276static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3277 struct dyn_ftrace *rec)
3278{
3279 void *ptr;
3280
3281 ptr = arch_ftrace_trampoline_func(ops, rec);
3282 if (ptr)
3283 seq_printf(m, " ->%pS", ptr);
3284}
3285
Steven Rostedt5072c592008-05-12 21:20:43 +02003286static int t_show(struct seq_file *m, void *v)
3287{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003288 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003289 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003290
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003291 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003292 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003293
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003294 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003295 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003296 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003297 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003298 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003299 return 0;
3300 }
3301
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003302 rec = iter->func;
3303
Steven Rostedt5072c592008-05-12 21:20:43 +02003304 if (!rec)
3305 return 0;
3306
Steven Rostedt647bcd02011-05-03 14:39:21 -04003307 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003308 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003309 struct ftrace_ops *ops;
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003310
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003311 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003312 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003313 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3314 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003315 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003316 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003317 if (ops) {
3318 do {
3319 seq_printf(m, "\ttramp: %pS (%pS)",
3320 (void *)ops->trampoline,
3321 (void *)ops->func);
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003322 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)39daa7b2015-11-25 15:12:38 -05003323 ops = ftrace_find_tramp_ops_next(rec, ops);
3324 } while (ops);
3325 } else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003326 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)030f4e12015-12-01 12:24:45 -05003327 } else {
3328 add_trampoline_func(m, NULL, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003329 }
3330 }
3331
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003332 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003333
3334 return 0;
3335}
3336
James Morris88e9d342009-09-22 16:43:43 -07003337static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003338 .start = t_start,
3339 .next = t_next,
3340 .stop = t_stop,
3341 .show = t_show,
3342};
3343
Ingo Molnare309b412008-05-12 21:20:51 +02003344static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003345ftrace_avail_open(struct inode *inode, struct file *file)
3346{
3347 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003348
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003349 if (unlikely(ftrace_disabled))
3350 return -ENODEV;
3351
Jiri Olsa50e18b92012-04-25 10:23:39 +02003352 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3353 if (iter) {
3354 iter->pg = ftrace_pages_start;
3355 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003356 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003357
Jiri Olsa50e18b92012-04-25 10:23:39 +02003358 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003359}
3360
Steven Rostedt647bcd02011-05-03 14:39:21 -04003361static int
3362ftrace_enabled_open(struct inode *inode, struct file *file)
3363{
3364 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003365
Jiri Olsa50e18b92012-04-25 10:23:39 +02003366 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3367 if (iter) {
3368 iter->pg = ftrace_pages_start;
3369 iter->flags = FTRACE_ITER_ENABLED;
3370 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003371 }
3372
Jiri Olsa50e18b92012-04-25 10:23:39 +02003373 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003374}
3375
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003376/**
3377 * ftrace_regex_open - initialize function tracer filter files
3378 * @ops: The ftrace_ops that hold the hash filters
3379 * @flag: The type of filter to process
3380 * @inode: The inode, usually passed in to your open routine
3381 * @file: The file, usually passed in to your open routine
3382 *
3383 * ftrace_regex_open() initializes the filter files for the
3384 * @ops. Depending on @flag it may process the filter hash or
3385 * the notrace hash of @ops. With this called from the open
3386 * routine, you can use ftrace_filter_write() for the write
3387 * routine if @flag has FTRACE_ITER_FILTER set, or
3388 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003389 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003390 * release must call ftrace_regex_release().
3391 */
3392int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003393ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003394 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003395{
3396 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003397 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003398 int ret = 0;
3399
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003400 ftrace_ops_init(ops);
3401
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003402 if (unlikely(ftrace_disabled))
3403 return -ENODEV;
3404
Steven Rostedt5072c592008-05-12 21:20:43 +02003405 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3406 if (!iter)
3407 return -ENOMEM;
3408
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003409 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3410 kfree(iter);
3411 return -ENOMEM;
3412 }
3413
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003414 iter->ops = ops;
3415 iter->flags = flag;
3416
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003417 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003418
Steven Rostedtf45948e2011-05-02 12:29:25 -04003419 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003420 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003421 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003422 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003423
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003424 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003425 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3426
3427 if (file->f_flags & O_TRUNC)
3428 iter->hash = alloc_ftrace_hash(size_bits);
3429 else
3430 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3431
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003432 if (!iter->hash) {
3433 trace_parser_put(&iter->parser);
3434 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003435 ret = -ENOMEM;
3436 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003437 }
3438 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003439
Steven Rostedt5072c592008-05-12 21:20:43 +02003440 if (file->f_mode & FMODE_READ) {
3441 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003442
3443 ret = seq_open(file, &show_ftrace_seq_ops);
3444 if (!ret) {
3445 struct seq_file *m = file->private_data;
3446 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003447 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003448 /* Failed */
3449 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003450 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003451 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003452 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003453 } else
3454 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003455
3456 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003457 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003458
3459 return ret;
3460}
3461
Steven Rostedt41c52c02008-05-22 11:46:33 -04003462static int
3463ftrace_filter_open(struct inode *inode, struct file *file)
3464{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003465 struct ftrace_ops *ops = inode->i_private;
3466
3467 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003468 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3469 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003470}
3471
3472static int
3473ftrace_notrace_open(struct inode *inode, struct file *file)
3474{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003475 struct ftrace_ops *ops = inode->i_private;
3476
3477 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003478 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003479}
3480
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003481/* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3482struct ftrace_glob {
3483 char *search;
3484 unsigned len;
3485 int type;
3486};
3487
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003488/*
3489 * If symbols in an architecture don't correspond exactly to the user-visible
3490 * name of what they represent, it is possible to define this function to
3491 * perform the necessary adjustments.
3492*/
3493char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3494{
3495 return str;
3496}
3497
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003498static int ftrace_match(char *str, struct ftrace_glob *g)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003499{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003500 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003501 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003502
Thiago Jung Bauermann7132e2d2016-04-25 18:56:14 -03003503 str = arch_ftrace_match_adjust(str, g->search);
3504
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003505 switch (g->type) {
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003506 case MATCH_FULL:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003507 if (strcmp(str, g->search) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003508 matched = 1;
3509 break;
3510 case MATCH_FRONT_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003511 if (strncmp(str, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003512 matched = 1;
3513 break;
3514 case MATCH_MIDDLE_ONLY:
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003515 if (strstr(str, g->search))
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003516 matched = 1;
3517 break;
3518 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003519 slen = strlen(str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003520 if (slen >= g->len &&
3521 memcmp(str + slen - g->len, g->search, g->len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003522 matched = 1;
3523 break;
3524 }
3525
3526 return matched;
3527}
3528
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003529static int
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003530enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003531{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003532 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003533 int ret = 0;
3534
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003535 entry = ftrace_lookup_ip(hash, rec->ip);
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003536 if (clear_filter) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003537 /* Do nothing if it doesn't exist */
3538 if (!entry)
3539 return 0;
3540
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003541 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003542 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003543 /* Do nothing if it exists */
3544 if (entry)
3545 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003546
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003547 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003548 }
3549 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003550}
3551
Steven Rostedt64e7c442009-02-13 17:08:48 -05003552static int
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003553ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3554 struct ftrace_glob *mod_g, int exclude_mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003555{
3556 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003557 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003558
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003559 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3560
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003561 if (mod_g) {
3562 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3563
3564 /* blank module name to match all modules */
3565 if (!mod_g->len) {
3566 /* blank module globbing: modname xor exclude_mod */
3567 if ((!exclude_mod) != (!modname))
3568 goto func_match;
3569 return 0;
3570 }
3571
3572 /* not matching the module */
3573 if (!modname || !mod_matches) {
3574 if (exclude_mod)
3575 goto func_match;
3576 else
3577 return 0;
3578 }
3579
3580 if (mod_matches && exclude_mod)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003581 return 0;
3582
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003583func_match:
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003584 /* blank search means to match all funcs in the mod */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003585 if (!func_g->len)
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003586 return 1;
3587 }
3588
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003589 return ftrace_match(str, func_g);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003590}
3591
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003592static int
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003593match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003594{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003595 struct ftrace_page *pg;
3596 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003597 struct ftrace_glob func_g = { .type = MATCH_FULL };
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003598 struct ftrace_glob mod_g = { .type = MATCH_FULL };
3599 struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3600 int exclude_mod = 0;
Li Zefan311d16d2009-12-08 11:15:11 +08003601 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003602 int ret;
Dan Carpenter198bd492017-07-12 10:35:57 +03003603 int clear_filter = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003604
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003605 if (func) {
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003606 func_g.type = filter_parse_regex(func, len, &func_g.search,
3607 &clear_filter);
3608 func_g.len = strlen(func_g.search);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003609 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003610
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003611 if (mod) {
3612 mod_g.type = filter_parse_regex(mod, strlen(mod),
3613 &mod_g.search, &exclude_mod);
3614 mod_g.len = strlen(mod_g.search);
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003615 }
3616
Steven Rostedt52baf112009-02-14 01:15:39 -05003617 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003618
3619 if (unlikely(ftrace_disabled))
3620 goto out_unlock;
3621
Steven Rostedt265c8312009-02-13 12:43:56 -05003622 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003623
3624 if (rec->flags & FTRACE_FL_DISABLED)
3625 continue;
3626
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003627 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003628 ret = enter_record(hash, rec, clear_filter);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003629 if (ret < 0) {
3630 found = ret;
3631 goto out_unlock;
3632 }
Li Zefan311d16d2009-12-08 11:15:11 +08003633 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003634 }
3635 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003636 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003637 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003638
3639 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003640}
3641
Steven Rostedt64e7c442009-02-13 17:08:48 -05003642static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003643ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003644{
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003645 return match_records(hash, buff, len, NULL);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003646}
3647
Steven Rostedt64e7c442009-02-13 17:08:48 -05003648
Steven Rostedtf6180772009-02-14 00:40:25 -05003649/*
3650 * We register the module command as a template to show others how
3651 * to register the a command as well.
3652 */
3653
3654static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003655ftrace_mod_callback(struct ftrace_hash *hash,
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003656 char *func, char *cmd, char *module, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003657{
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003658 int ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003659
3660 /*
3661 * cmd == 'mod' because we only registered this func
3662 * for the 'mod' ftrace_func_command.
3663 * But if you register one func with multiple commands,
3664 * you can tell which command was used by the cmd
3665 * parameter.
3666 */
Dmitry Safonovf0a3b152015-09-29 19:46:13 +03003667 ret = match_records(hash, func, strlen(func), module);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003668 if (!ret)
Dmitry Safonov5e3949f2015-09-29 19:46:12 +03003669 return -EINVAL;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003670 if (ret < 0)
3671 return ret;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003672 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003673}
3674
3675static struct ftrace_func_command ftrace_mod_cmd = {
3676 .name = "mod",
3677 .func = ftrace_mod_callback,
3678};
3679
3680static int __init ftrace_mod_cmd_init(void)
3681{
3682 return register_ftrace_command(&ftrace_mod_cmd);
3683}
Steven Rostedt6f415672012-10-05 12:13:07 -04003684core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003685
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003686static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003687 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003688{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003689 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003690 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003691 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003692
3693 key = hash_long(ip, FTRACE_HASH_BITS);
3694
3695 hhd = &ftrace_func_hash[key];
3696
3697 if (hlist_empty(hhd))
3698 return;
3699
3700 /*
3701 * Disable preemption for these calls to prevent a RCU grace
3702 * period. This syncs the hash iteration and freeing of items
3703 * on the hash. rcu_read_lock is too dangerous here.
3704 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003705 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003706 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003707 if (entry->ip == ip)
3708 entry->ops->func(ip, parent_ip, &entry->data);
3709 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003710 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003711}
3712
Steven Rostedtb6887d72009-02-17 12:32:04 -05003713static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003714{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003715 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003716 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003717 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003718};
3719
Steven Rostedtb6887d72009-02-17 12:32:04 -05003720static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003721
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003722static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003723{
Steven Rostedtb8489142011-05-04 09:27:52 -04003724 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003725 int i;
3726
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003727 if (ftrace_probe_registered) {
3728 /* still need to update the function call sites */
3729 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003730 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3731 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003732 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003733 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003734
3735 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3736 struct hlist_head *hhd = &ftrace_func_hash[i];
3737 if (hhd->first)
3738 break;
3739 }
3740 /* Nothing registered? */
3741 if (i == FTRACE_FUNC_HASHSIZE)
3742 return;
3743
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003744 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003745
Steven Rostedtb6887d72009-02-17 12:32:04 -05003746 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003747}
3748
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003749static bool __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003750{
3751 int i;
3752
Steven Rostedtb6887d72009-02-17 12:32:04 -05003753 if (!ftrace_probe_registered)
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003754 return false;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003755
3756 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3757 struct hlist_head *hhd = &ftrace_func_hash[i];
3758 if (hhd->first)
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003759 return false;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003760 }
3761
3762 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003763 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003764
Steven Rostedtb6887d72009-02-17 12:32:04 -05003765 ftrace_probe_registered = 0;
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003766 return true;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003767}
3768
3769
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003770static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003771{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003772 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003773 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003774 kfree(entry);
3775}
3776
Steven Rostedt59df055f2009-02-14 15:29:06 -05003777int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003778register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003779 void *data)
3780{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003781 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003782 struct ftrace_func_probe *entry;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003783 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003784 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003785 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003786 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003787 struct ftrace_page *pg;
3788 struct dyn_ftrace *rec;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003789 int not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003790 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003791 int count = 0;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003792 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003793
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003794 func_g.type = filter_parse_regex(glob, strlen(glob),
3795 &func_g.search, &not);
3796 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003797
Steven Rostedtb6887d72009-02-17 12:32:04 -05003798 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003799 if (WARN_ON(not))
3800 return -EINVAL;
3801
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003802 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003803
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003804 old_hash_ops.filter_hash = old_hash;
3805 /* Probes only have filters */
3806 old_hash_ops.notrace_hash = NULL;
3807
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003808 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003809 if (!hash) {
3810 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003811 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003812 }
3813
3814 if (unlikely(ftrace_disabled)) {
3815 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003816 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003817 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003818
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003819 mutex_lock(&ftrace_lock);
3820
Steven Rostedt59df055f2009-02-14 15:29:06 -05003821 do_for_each_ftrace_rec(pg, rec) {
3822
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05003823 if (rec->flags & FTRACE_FL_DISABLED)
3824 continue;
3825
Dmitry Safonov0b507e12015-09-29 19:46:15 +03003826 if (!ftrace_match_record(rec, &func_g, NULL, 0))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003827 continue;
3828
3829 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3830 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003831 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003832 if (!count)
3833 count = -ENOMEM;
3834 goto out_unlock;
3835 }
3836
3837 count++;
3838
3839 entry->data = data;
3840
3841 /*
3842 * The caller might want to do something special
3843 * for each function we find. We call the callback
3844 * to give the caller an opportunity to do so.
3845 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003846 if (ops->init) {
3847 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003848 /* caller does not like this func */
3849 kfree(entry);
3850 continue;
3851 }
3852 }
3853
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003854 ret = enter_record(hash, rec, 0);
3855 if (ret < 0) {
3856 kfree(entry);
3857 count = ret;
3858 goto out_unlock;
3859 }
3860
Steven Rostedt59df055f2009-02-14 15:29:06 -05003861 entry->ops = ops;
3862 entry->ip = rec->ip;
3863
3864 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3865 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3866
3867 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003868
3869 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003870
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003871 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003872
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003873 if (!ret)
3874 free_ftrace_hash_rcu(old_hash);
3875 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003876 count = ret;
3877
Steven Rostedt59df055f2009-02-14 15:29:06 -05003878 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003879 mutex_unlock(&ftrace_lock);
3880 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003881 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003882 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003883
3884 return count;
3885}
3886
3887enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003888 PROBE_TEST_FUNC = 1,
3889 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003890};
3891
3892static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003893__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003894 void *data, int flags)
3895{
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003896 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003897 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003898 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003899 struct ftrace_func_probe *p;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003900 struct ftrace_glob func_g;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003901 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003902 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003903 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003904 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003905 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003906 char str[KSYM_SYMBOL_LEN];
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003907 int i, ret;
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003908 bool disabled;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003909
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003910 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003911 func_g.search = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003912 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003913 int not;
3914
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003915 func_g.type = filter_parse_regex(glob, strlen(glob),
3916 &func_g.search, &not);
3917 func_g.len = strlen(func_g.search);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003918
Steven Rostedtb6887d72009-02-17 12:32:04 -05003919 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003920 if (WARN_ON(not))
3921 return;
3922 }
3923
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003924 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003925
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003926 old_hash_ops.filter_hash = old_hash;
3927 /* Probes only have filters */
3928 old_hash_ops.notrace_hash = NULL;
3929
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003930 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3931 if (!hash)
3932 /* Hmm, should report this somehow */
3933 goto out_unlock;
3934
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003935 INIT_LIST_HEAD(&free_list);
3936
Steven Rostedt59df055f2009-02-14 15:29:06 -05003937 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3938 struct hlist_head *hhd = &ftrace_func_hash[i];
3939
Sasha Levinb67bfe02013-02-27 17:06:00 -08003940 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003941
3942 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003943 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003944 continue;
3945
Steven Rostedtb6887d72009-02-17 12:32:04 -05003946 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003947 continue;
3948
3949 /* do this last, since it is the most expensive */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003950 if (func_g.search) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003951 kallsyms_lookup(entry->ip, NULL, NULL,
3952 NULL, str);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003953 if (!ftrace_match(str, &func_g))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003954 continue;
3955 }
3956
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003957 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3958 /* It is possible more than one entry had this ip */
3959 if (rec_entry)
3960 free_hash_entry(hash, rec_entry);
3961
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003962 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003963 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003964 }
3965 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003966 mutex_lock(&ftrace_lock);
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003967 disabled = __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003968 /*
3969 * Remove after the disable is called. Otherwise, if the last
3970 * probe is removed, a null hash means *all enabled*.
3971 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003972 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (VMware)666452f2017-04-14 17:45:45 -04003973
3974 /* still need to update the function call sites */
3975 if (ftrace_enabled && !disabled)
3976 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3977 &old_hash_ops);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003978 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003979 if (!ret)
3980 free_ftrace_hash_rcu(old_hash);
3981
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003982 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3983 list_del(&entry->free_list);
3984 ftrace_free_entry(entry);
3985 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003986 mutex_unlock(&ftrace_lock);
Dmitry Safonov3ba00922015-09-29 19:46:14 +03003987
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003988 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003989 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003990 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003991}
3992
3993void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003994unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003995 void *data)
3996{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003997 __unregister_ftrace_function_probe(glob, ops, data,
3998 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003999}
4000
4001void
Steven Rostedtb6887d72009-02-17 12:32:04 -05004002unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004003{
Steven Rostedtb6887d72009-02-17 12:32:04 -05004004 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004005}
4006
Steven Rostedtb6887d72009-02-17 12:32:04 -05004007void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05004008{
Steven Rostedtb6887d72009-02-17 12:32:04 -05004009 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05004010}
4011
Steven Rostedtf6180772009-02-14 00:40:25 -05004012static LIST_HEAD(ftrace_commands);
4013static DEFINE_MUTEX(ftrace_cmd_mutex);
4014
Tom Zanussi38de93a2013-10-24 08:34:18 -05004015/*
4016 * Currently we only register ftrace commands from __init, so mark this
4017 * __init too.
4018 */
4019__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004020{
4021 struct ftrace_func_command *p;
4022 int ret = 0;
4023
4024 mutex_lock(&ftrace_cmd_mutex);
4025 list_for_each_entry(p, &ftrace_commands, list) {
4026 if (strcmp(cmd->name, p->name) == 0) {
4027 ret = -EBUSY;
4028 goto out_unlock;
4029 }
4030 }
4031 list_add(&cmd->list, &ftrace_commands);
4032 out_unlock:
4033 mutex_unlock(&ftrace_cmd_mutex);
4034
4035 return ret;
4036}
4037
Tom Zanussi38de93a2013-10-24 08:34:18 -05004038/*
4039 * Currently we only unregister ftrace commands from __init, so mark
4040 * this __init too.
4041 */
4042__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05004043{
4044 struct ftrace_func_command *p, *n;
4045 int ret = -ENODEV;
4046
4047 mutex_lock(&ftrace_cmd_mutex);
4048 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4049 if (strcmp(cmd->name, p->name) == 0) {
4050 ret = 0;
4051 list_del_init(&p->list);
4052 goto out_unlock;
4053 }
4054 }
4055 out_unlock:
4056 mutex_unlock(&ftrace_cmd_mutex);
4057
4058 return ret;
4059}
4060
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004061static int ftrace_process_regex(struct ftrace_hash *hash,
4062 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05004063{
Steven Rostedtf6180772009-02-14 00:40:25 -05004064 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05004065 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08004066 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004067
4068 func = strsep(&next, ":");
4069
4070 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004071 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04004072 if (!ret)
4073 ret = -EINVAL;
4074 if (ret < 0)
4075 return ret;
4076 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004077 }
4078
Steven Rostedtf6180772009-02-14 00:40:25 -05004079 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05004080
4081 command = strsep(&next, ":");
4082
Steven Rostedtf6180772009-02-14 00:40:25 -05004083 mutex_lock(&ftrace_cmd_mutex);
4084 list_for_each_entry(p, &ftrace_commands, list) {
4085 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04004086 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05004087 goto out_unlock;
4088 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05004089 }
Steven Rostedtf6180772009-02-14 00:40:25 -05004090 out_unlock:
4091 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05004092
Steven Rostedtf6180772009-02-14 00:40:25 -05004093 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05004094}
4095
Ingo Molnare309b412008-05-12 21:20:51 +02004096static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004097ftrace_regex_write(struct file *file, const char __user *ubuf,
4098 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02004099{
4100 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004101 struct trace_parser *parser;
4102 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02004103
Li Zefan4ba79782009-09-22 13:52:20 +08004104 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02004105 return 0;
4106
Steven Rostedt5072c592008-05-12 21:20:43 +02004107 if (file->f_mode & FMODE_READ) {
4108 struct seq_file *m = file->private_data;
4109 iter = m->private;
4110 } else
4111 iter = file->private_data;
4112
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004113 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004114 return -ENODEV;
4115
4116 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004117
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004118 parser = &iter->parser;
4119 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02004120
Li Zefan4ba79782009-09-22 13:52:20 +08004121 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004122 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004123 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004124 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08004125 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04004126 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004127 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02004128 }
4129
Steven Rostedt5072c592008-05-12 21:20:43 +02004130 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004131 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004132 return ret;
4133}
4134
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004135ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004136ftrace_filter_write(struct file *file, const char __user *ubuf,
4137 size_t cnt, loff_t *ppos)
4138{
4139 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4140}
4141
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004142ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004143ftrace_notrace_write(struct file *file, const char __user *ubuf,
4144 size_t cnt, loff_t *ppos)
4145{
4146 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4147}
4148
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004149static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004150ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4151{
4152 struct ftrace_func_entry *entry;
4153
4154 if (!ftrace_location(ip))
4155 return -EINVAL;
4156
4157 if (remove) {
4158 entry = ftrace_lookup_ip(hash, ip);
4159 if (!entry)
4160 return -ENOENT;
4161 free_hash_entry(hash, entry);
4162 return 0;
4163 }
4164
4165 return add_hash_entry(hash, ip);
4166}
4167
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004168static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004169 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004170{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004171 struct ftrace_ops *op;
4172
4173 if (!ftrace_enabled)
4174 return;
4175
4176 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004177 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004178 return;
4179 }
4180
4181 /*
4182 * If this is the shared global_ops filter, then we need to
4183 * check if there is another ops that shares it, is enabled.
4184 * If so, we still need to run the modify code.
4185 */
4186 if (ops->func_hash != &global_ops.local_hash)
4187 return;
4188
4189 do_for_each_ftrace_op(op, ftrace_ops_list) {
4190 if (op->func_hash == &global_ops.local_hash &&
4191 op->flags & FTRACE_OPS_FL_ENABLED) {
4192 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4193 /* Only need to do this once */
4194 return;
4195 }
4196 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004197}
4198
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004199static int
4200ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4201 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004202{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004203 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004204 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004205 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004206 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004207 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004208
Steven Rostedt41c52c02008-05-22 11:46:33 -04004209 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004210 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004211
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004212 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004213
Steven Rostedtf45948e2011-05-02 12:29:25 -04004214 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004215 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004216 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004217 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004218
Wang Nanb972cc52014-07-15 08:40:20 +08004219 if (reset)
4220 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4221 else
4222 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4223
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004224 if (!hash) {
4225 ret = -ENOMEM;
4226 goto out_regex_unlock;
4227 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004228
Jiri Olsaac483c42012-01-02 10:04:14 +01004229 if (buf && !ftrace_match_records(hash, buf, len)) {
4230 ret = -EINVAL;
4231 goto out_regex_unlock;
4232 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004233 if (ip) {
4234 ret = ftrace_match_addr(hash, ip, remove);
4235 if (ret < 0)
4236 goto out_regex_unlock;
4237 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004238
4239 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004240 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004241 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4242 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004243 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004244 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004245 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004246 free_ftrace_hash_rcu(old_hash);
4247 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004248 mutex_unlock(&ftrace_lock);
4249
Jiri Olsaac483c42012-01-02 10:04:14 +01004250 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004251 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004252
4253 free_ftrace_hash(hash);
4254 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004255}
4256
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004257static int
4258ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4259 int reset, int enable)
4260{
4261 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4262}
4263
4264/**
4265 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4266 * @ops - the ops to set the filter with
4267 * @ip - the address to add to or remove from the filter.
4268 * @remove - non zero to remove the ip from the filter
4269 * @reset - non zero to reset all filters before applying this filter.
4270 *
4271 * Filters denote which functions should be enabled when tracing is enabled
4272 * If @ip is NULL, it failes to update filter.
4273 */
4274int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4275 int remove, int reset)
4276{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004277 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004278 return ftrace_set_addr(ops, ip, remove, reset, 1);
4279}
4280EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4281
4282static int
4283ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4284 int reset, int enable)
4285{
4286 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4287}
4288
Steven Rostedt77a2b372008-05-12 21:20:45 +02004289/**
4290 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004291 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004292 * @buf - the string that holds the function filter text.
4293 * @len - the length of the string.
4294 * @reset - non zero to reset all filters before applying this filter.
4295 *
4296 * Filters denote which functions should be enabled when tracing is enabled.
4297 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4298 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004299int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004300 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004301{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004302 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004303 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004304}
Steven Rostedt936e0742011-05-05 22:54:01 -04004305EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004306
Steven Rostedt41c52c02008-05-22 11:46:33 -04004307/**
4308 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004309 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004310 * @buf - the string that holds the function notrace text.
4311 * @len - the length of the string.
4312 * @reset - non zero to reset all filters before applying this filter.
4313 *
4314 * Notrace Filters denote which functions should not be enabled when tracing
4315 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4316 * for tracing.
4317 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004318int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004319 int len, int reset)
4320{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004321 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004322 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004323}
4324EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4325/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004326 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004327 * @buf - the string that holds the function filter text.
4328 * @len - the length of the string.
4329 * @reset - non zero to reset all filters before applying this filter.
4330 *
4331 * Filters denote which functions should be enabled when tracing is enabled.
4332 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4333 */
4334void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4335{
4336 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4337}
4338EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4339
4340/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004341 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004342 * @buf - the string that holds the function notrace text.
4343 * @len - the length of the string.
4344 * @reset - non zero to reset all filters before applying this filter.
4345 *
4346 * Notrace Filters denote which functions should not be enabled when tracing
4347 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4348 * for tracing.
4349 */
4350void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004351{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004352 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004353}
Steven Rostedt936e0742011-05-05 22:54:01 -04004354EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004355
Steven Rostedt2af15d62009-05-28 13:37:24 -04004356/*
4357 * command line interface to allow users to set filters on boot up.
4358 */
4359#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4360static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4361static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4362
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004363/* Used by function selftest to not test if filter is set */
4364bool ftrace_filter_param __initdata;
4365
Steven Rostedt2af15d62009-05-28 13:37:24 -04004366static int __init set_ftrace_notrace(char *str)
4367{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004368 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004369 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004370 return 1;
4371}
4372__setup("ftrace_notrace=", set_ftrace_notrace);
4373
4374static int __init set_ftrace_filter(char *str)
4375{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004376 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004377 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004378 return 1;
4379}
4380__setup("ftrace_filter=", set_ftrace_filter);
4381
Stefan Assmann369bc182009-10-12 22:17:21 +02004382#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004383static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004384static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004385static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004386
Stefan Assmann369bc182009-10-12 22:17:21 +02004387static int __init set_graph_function(char *str)
4388{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004389 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004390 return 1;
4391}
4392__setup("ftrace_graph_filter=", set_graph_function);
4393
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004394static int __init set_graph_notrace_function(char *str)
4395{
4396 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4397 return 1;
4398}
4399__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4400
4401static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004402{
4403 int ret;
4404 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004405 unsigned long *table = ftrace_graph_funcs;
4406 int *count = &ftrace_graph_count;
4407
4408 if (!enable) {
4409 table = ftrace_graph_notrace_funcs;
4410 count = &ftrace_graph_notrace_count;
4411 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004412
4413 while (buf) {
4414 func = strsep(&buf, ",");
4415 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004416 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004417 if (ret)
4418 printk(KERN_DEBUG "ftrace: function %s not "
4419 "traceable\n", func);
4420 }
4421}
4422#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4423
Steven Rostedt2a85a372011-12-19 21:57:44 -05004424void __init
4425ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004426{
4427 char *func;
4428
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004429 ftrace_ops_init(ops);
4430
Steven Rostedt2af15d62009-05-28 13:37:24 -04004431 while (buf) {
4432 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004433 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004434 }
4435}
4436
4437static void __init set_ftrace_early_filters(void)
4438{
4439 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004440 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004441 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004442 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004443#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4444 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004445 set_ftrace_early_graph(ftrace_graph_buf, 1);
4446 if (ftrace_graph_notrace_buf[0])
4447 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004448#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004449}
4450
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004451int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004452{
4453 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004454 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004455 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004456 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004457 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004458 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004459 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004460 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004461
Steven Rostedt5072c592008-05-12 21:20:43 +02004462 if (file->f_mode & FMODE_READ) {
4463 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004464 seq_release(inode, file);
4465 } else
4466 iter = file->private_data;
4467
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004468 parser = &iter->parser;
4469 if (trace_parser_loaded(parser)) {
4470 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004471 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004472 }
4473
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004474 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004475
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004476 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004477
Steven Rostedt058e2972011-04-29 22:35:33 -04004478 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004479 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4480
4481 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004482 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004483 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004484 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004485
Steven Rostedt058e2972011-04-29 22:35:33 -04004486 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004487 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004488 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4489 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004490 ret = ftrace_hash_move(iter->ops, filter_hash,
4491 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004492 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004493 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004494 free_ftrace_hash_rcu(old_hash);
4495 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004496 mutex_unlock(&ftrace_lock);
4497 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004498
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004499 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004500 free_ftrace_hash(iter->hash);
4501 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004502
Steven Rostedt5072c592008-05-12 21:20:43 +02004503 return 0;
4504}
4505
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004506static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004507 .open = ftrace_avail_open,
4508 .read = seq_read,
4509 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004510 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004511};
4512
Steven Rostedt647bcd02011-05-03 14:39:21 -04004513static const struct file_operations ftrace_enabled_fops = {
4514 .open = ftrace_enabled_open,
4515 .read = seq_read,
4516 .llseek = seq_lseek,
4517 .release = seq_release_private,
4518};
4519
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004520static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004521 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004522 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004523 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004524 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004525 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004526};
4527
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004528static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004529 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004530 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004531 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004532 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004533 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004534};
4535
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004536#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4537
4538static DEFINE_MUTEX(graph_lock);
4539
4540int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004541int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004542unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004543unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004544
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004545struct ftrace_graph_data {
4546 unsigned long *table;
4547 size_t size;
4548 int *count;
4549 const struct seq_operations *seq_ops;
4550};
4551
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004552static void *
Li Zefan85951842009-06-24 09:54:00 +08004553__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004554{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004555 struct ftrace_graph_data *fgd = m->private;
4556
4557 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004558 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004559 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004560}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004561
Li Zefan85951842009-06-24 09:54:00 +08004562static void *
4563g_next(struct seq_file *m, void *v, loff_t *pos)
4564{
4565 (*pos)++;
4566 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004567}
4568
4569static void *g_start(struct seq_file *m, loff_t *pos)
4570{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004571 struct ftrace_graph_data *fgd = m->private;
4572
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004573 mutex_lock(&graph_lock);
4574
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004575 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004576 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004577 return (void *)1;
4578
Li Zefan85951842009-06-24 09:54:00 +08004579 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004580}
4581
4582static void g_stop(struct seq_file *m, void *p)
4583{
4584 mutex_unlock(&graph_lock);
4585}
4586
4587static int g_show(struct seq_file *m, void *v)
4588{
4589 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004590
4591 if (!ptr)
4592 return 0;
4593
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004594 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004595 struct ftrace_graph_data *fgd = m->private;
4596
4597 if (fgd->table == ftrace_graph_funcs)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004598 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004599 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004600 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004601 return 0;
4602 }
4603
Steven Rostedtb375a112009-09-17 00:05:58 -04004604 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004605
4606 return 0;
4607}
4608
James Morris88e9d342009-09-22 16:43:43 -07004609static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004610 .start = g_start,
4611 .next = g_next,
4612 .stop = g_stop,
4613 .show = g_show,
4614};
4615
4616static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004617__ftrace_graph_open(struct inode *inode, struct file *file,
4618 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004619{
4620 int ret = 0;
4621
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004622 mutex_lock(&graph_lock);
4623 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004624 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004625 *fgd->count = 0;
4626 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004627 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004628 mutex_unlock(&graph_lock);
4629
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004630 if (file->f_mode & FMODE_READ) {
4631 ret = seq_open(file, fgd->seq_ops);
4632 if (!ret) {
4633 struct seq_file *m = file->private_data;
4634 m->private = fgd;
4635 }
4636 } else
4637 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004638
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004639 return ret;
4640}
4641
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004642static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004643ftrace_graph_open(struct inode *inode, struct file *file)
4644{
4645 struct ftrace_graph_data *fgd;
4646
4647 if (unlikely(ftrace_disabled))
4648 return -ENODEV;
4649
4650 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4651 if (fgd == NULL)
4652 return -ENOMEM;
4653
4654 fgd->table = ftrace_graph_funcs;
4655 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4656 fgd->count = &ftrace_graph_count;
4657 fgd->seq_ops = &ftrace_graph_seq_ops;
4658
4659 return __ftrace_graph_open(inode, file, fgd);
4660}
4661
4662static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004663ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4664{
4665 struct ftrace_graph_data *fgd;
4666
4667 if (unlikely(ftrace_disabled))
4668 return -ENODEV;
4669
4670 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4671 if (fgd == NULL)
4672 return -ENOMEM;
4673
4674 fgd->table = ftrace_graph_notrace_funcs;
4675 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4676 fgd->count = &ftrace_graph_notrace_count;
4677 fgd->seq_ops = &ftrace_graph_seq_ops;
4678
4679 return __ftrace_graph_open(inode, file, fgd);
4680}
4681
4682static int
Li Zefan87827112009-07-23 11:29:11 +08004683ftrace_graph_release(struct inode *inode, struct file *file)
4684{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004685 if (file->f_mode & FMODE_READ) {
4686 struct seq_file *m = file->private_data;
4687
4688 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004689 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004690 } else {
4691 kfree(file->private_data);
4692 }
4693
Li Zefan87827112009-07-23 11:29:11 +08004694 return 0;
4695}
4696
4697static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004698ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004699{
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004700 struct ftrace_glob func_g;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004701 struct dyn_ftrace *rec;
4702 struct ftrace_page *pg;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004703 int fail = 1;
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004704 int not;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004705 bool exists;
4706 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004707
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004708 /* decode regex */
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004709 func_g.type = filter_parse_regex(buffer, strlen(buffer),
4710 &func_g.search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004711 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004712 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004713
Dmitry Safonov3ba00922015-09-29 19:46:14 +03004714 func_g.len = strlen(func_g.search);
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004715
Steven Rostedt52baf112009-02-14 01:15:39 -05004716 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004717
4718 if (unlikely(ftrace_disabled)) {
4719 mutex_unlock(&ftrace_lock);
4720 return -ENODEV;
4721 }
4722
Steven Rostedt265c8312009-02-13 12:43:56 -05004723 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004724
Steven Rostedt (Red Hat)546fece2016-11-14 16:31:49 -05004725 if (rec->flags & FTRACE_FL_DISABLED)
4726 continue;
4727
Dmitry Safonov0b507e12015-09-29 19:46:15 +03004728 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004729 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004730 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004731 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004732 if (array[i] == rec->ip) {
4733 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004734 break;
4735 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004736 }
4737
4738 if (!not) {
4739 fail = 0;
4740 if (!exists) {
4741 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004742 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004743 goto out;
4744 }
4745 } else {
4746 if (exists) {
4747 array[i] = array[--(*idx)];
4748 array[*idx] = 0;
4749 fail = 0;
4750 }
4751 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004752 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004753 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004754out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004755 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004756
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004757 if (fail)
4758 return -EINVAL;
4759
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004760 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004761}
4762
4763static ssize_t
4764ftrace_graph_write(struct file *file, const char __user *ubuf,
4765 size_t cnt, loff_t *ppos)
4766{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004767 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004768 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004769 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004770
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004771 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004772 return 0;
4773
Namhyung Kim6a101082013-10-14 17:24:25 +09004774 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4775 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004776
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004777 read = trace_get_user(&parser, ubuf, cnt, ppos);
4778
Li Zefan4ba79782009-09-22 13:52:20 +08004779 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004780 parser.buffer[parser.idx] = 0;
4781
Namhyung Kim6a101082013-10-14 17:24:25 +09004782 mutex_lock(&graph_lock);
4783
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004784 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004785 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4786 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004787
4788 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004789 }
4790
Namhyung Kim6a101082013-10-14 17:24:25 +09004791 if (!ret)
4792 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004793
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004794 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004795
4796 return ret;
4797}
4798
4799static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004800 .open = ftrace_graph_open,
4801 .read = seq_read,
4802 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004803 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004804 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004805};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004806
4807static const struct file_operations ftrace_graph_notrace_fops = {
4808 .open = ftrace_graph_notrace_open,
4809 .read = seq_read,
4810 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004811 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004812 .release = ftrace_graph_release,
4813};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004814#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4815
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004816void ftrace_create_filter_files(struct ftrace_ops *ops,
4817 struct dentry *parent)
4818{
4819
4820 trace_create_file("set_ftrace_filter", 0644, parent,
4821 ops, &ftrace_filter_fops);
4822
4823 trace_create_file("set_ftrace_notrace", 0644, parent,
4824 ops, &ftrace_notrace_fops);
4825}
4826
4827/*
4828 * The name "destroy_filter_files" is really a misnomer. Although
4829 * in the future, it may actualy delete the files, but this is
4830 * really intended to make sure the ops passed in are disabled
4831 * and that when this function returns, the caller is free to
4832 * free the ops.
4833 *
4834 * The "destroy" name is only to match the "create" name that this
4835 * should be paired with.
4836 */
4837void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4838{
4839 mutex_lock(&ftrace_lock);
4840 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4841 ftrace_shutdown(ops, 0);
4842 ops->flags |= FTRACE_OPS_FL_DELETED;
Steven Rostedt (VMware)326c9e12018-12-10 23:58:01 -05004843 ftrace_free_filter(ops);
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004844 mutex_unlock(&ftrace_lock);
4845}
4846
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05004847static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004848{
Steven Rostedt5072c592008-05-12 21:20:43 +02004849
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004850 trace_create_file("available_filter_functions", 0444,
4851 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004852
Steven Rostedt647bcd02011-05-03 14:39:21 -04004853 trace_create_file("enabled_functions", 0444,
4854 d_tracer, NULL, &ftrace_enabled_fops);
4855
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004856 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004857
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004858#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004859 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004860 NULL,
4861 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004862 trace_create_file("set_graph_notrace", 0444, d_tracer,
4863 NULL,
4864 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004865#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4866
Steven Rostedt5072c592008-05-12 21:20:43 +02004867 return 0;
4868}
4869
Steven Rostedt9fd49322012-04-24 22:32:06 -04004870static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004871{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004872 const unsigned long *ipa = a;
4873 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004874
Steven Rostedt9fd49322012-04-24 22:32:06 -04004875 if (*ipa > *ipb)
4876 return 1;
4877 if (*ipa < *ipb)
4878 return -1;
4879 return 0;
4880}
4881
Sami Tolvanen7bd125e2017-06-16 12:52:57 -07004882static int __norecordmcount ftrace_process_locs(struct module *mod,
4883 unsigned long *start,
4884 unsigned long *end)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004885{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004886 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004887 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004888 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004889 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004890 unsigned long *p;
4891 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004892 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004893 int ret = -ENOMEM;
4894
4895 count = end - start;
4896
4897 if (!count)
4898 return 0;
4899
Steven Rostedt9fd49322012-04-24 22:32:06 -04004900 sort(start, count, sizeof(*start),
Rasmus Villemoes6db02902015-09-09 23:27:02 +02004901 ftrace_cmp_ips, NULL);
Steven Rostedt9fd49322012-04-24 22:32:06 -04004902
Steven Rostedt706c81f2012-04-24 23:45:26 -04004903 start_pg = ftrace_allocate_pages(count);
4904 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004905 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004906
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004907 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004908
Steven Rostedt32082302011-12-16 14:42:37 -05004909 /*
4910 * Core and each module needs their own pages, as
4911 * modules will free them when they are removed.
4912 * Force a new page to be allocated for modules.
4913 */
Steven Rostedta7900872011-12-16 16:23:44 -05004914 if (!mod) {
4915 WARN_ON(ftrace_pages || ftrace_pages_start);
4916 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004917 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004918 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004919 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004920 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004921
Steven Rostedta7900872011-12-16 16:23:44 -05004922 if (WARN_ON(ftrace_pages->next)) {
4923 /* Hmm, we have free pages? */
4924 while (ftrace_pages->next)
4925 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004926 }
Steven Rostedta7900872011-12-16 16:23:44 -05004927
Steven Rostedt706c81f2012-04-24 23:45:26 -04004928 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004929 }
4930
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004931 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004932 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004933 while (p < end) {
4934 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004935 /*
4936 * Some architecture linkers will pad between
4937 * the different mcount_loc sections of different
4938 * object files to satisfy alignments.
4939 * Skip any NULL pointers.
4940 */
4941 if (!addr)
4942 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004943
4944 if (pg->index == pg->size) {
4945 /* We should have allocated enough */
4946 if (WARN_ON(!pg->next))
4947 break;
4948 pg = pg->next;
4949 }
4950
4951 rec = &pg->records[pg->index++];
4952 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004953 }
4954
Steven Rostedt706c81f2012-04-24 23:45:26 -04004955 /* We should have used all pages */
4956 WARN_ON(pg->next);
4957
4958 /* Assign the last page to ftrace_pages */
4959 ftrace_pages = pg;
4960
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004961 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004962 * We only need to disable interrupts on start up
4963 * because we are modifying code that an interrupt
4964 * may execute, and the modification is not atomic.
4965 * But for modules, nothing runs the code we modify
4966 * until we are finished with it, and there's no
4967 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004968 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004969 if (!mod)
4970 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004971 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004972 if (!mod)
4973 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004974 ret = 0;
4975 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004976 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004977
Steven Rostedta7900872011-12-16 16:23:44 -05004978 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004979}
4980
Steven Rostedt93eb6772009-04-15 13:24:06 -04004981#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004982
4983#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4984
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05004985static int referenced_filters(struct dyn_ftrace *rec)
4986{
4987 struct ftrace_ops *ops;
4988 int cnt = 0;
4989
4990 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
4991 if (ops_references_rec(ops, rec))
4992 cnt++;
4993 }
4994
4995 return cnt;
4996}
4997
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004998void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004999{
5000 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05005001 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005002 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05005003 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04005004
Steven Rostedt93eb6772009-04-15 13:24:06 -04005005 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04005006
5007 if (ftrace_disabled)
5008 goto out_unlock;
5009
Steven Rostedt32082302011-12-16 14:42:37 -05005010 /*
5011 * Each module has its own ftrace_pages, remove
5012 * them from the list.
5013 */
5014 last_pg = &ftrace_pages_start;
5015 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5016 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02005017 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04005018 /*
Steven Rostedt32082302011-12-16 14:42:37 -05005019 * As core pages are first, the first
5020 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04005021 */
Steven Rostedt32082302011-12-16 14:42:37 -05005022 if (WARN_ON(pg == ftrace_pages_start))
5023 goto out_unlock;
5024
5025 /* Check if we are deleting the last page */
5026 if (pg == ftrace_pages)
5027 ftrace_pages = next_to_ftrace_page(last_pg);
5028
5029 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05005030 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5031 free_pages((unsigned long)pg->records, order);
5032 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05005033 } else
5034 last_pg = &pg->next;
5035 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04005036 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04005037 mutex_unlock(&ftrace_lock);
5038}
5039
Jessica Yu7dcd1822016-02-16 17:32:33 -05005040void ftrace_module_enable(struct module *mod)
Steven Rostedt (Red Hat)b7ffffb2016-01-07 15:40:01 -05005041{
5042 struct dyn_ftrace *rec;
5043 struct ftrace_page *pg;
5044
5045 mutex_lock(&ftrace_lock);
5046
5047 if (ftrace_disabled)
5048 goto out_unlock;
5049
5050 /*
5051 * If the tracing is enabled, go ahead and enable the record.
5052 *
5053 * The reason not to enable the record immediatelly is the
5054 * inherent check of ftrace_make_nop/ftrace_make_call for
5055 * correct previous instructions. Making first the NOP
5056 * conversion puts the module to the correct state, thus
5057 * passing the ftrace_make_call check.
5058 *
5059 * We also delay this to after the module code already set the
5060 * text to read-only, as we now need to set it back to read-write
5061 * so that we can modify the text.
5062 */
5063 if (ftrace_start_up)
5064 ftrace_arch_code_modify_prepare();
5065
5066 do_for_each_ftrace_rec(pg, rec) {
5067 int cnt;
5068 /*
5069 * do_for_each_ftrace_rec() is a double loop.
5070 * module text shares the pg. If a record is
5071 * not part of this module, then skip this pg,
5072 * which the "break" will do.
5073 */
5074 if (!within_module_core(rec->ip, mod))
5075 break;
5076
5077 cnt = 0;
5078
5079 /*
5080 * When adding a module, we need to check if tracers are
5081 * currently enabled and if they are, and can trace this record,
5082 * we need to enable the module functions as well as update the
5083 * reference counts for those function records.
5084 */
5085 if (ftrace_start_up)
5086 cnt += referenced_filters(rec);
5087
5088 /* This clears FTRACE_FL_DISABLED */
5089 rec->flags = cnt;
5090
5091 if (ftrace_start_up && cnt) {
5092 int failed = __ftrace_replace_code(rec, 1);
5093 if (failed) {
5094 ftrace_bug(failed, rec);
5095 goto out_loop;
5096 }
5097 }
5098
5099 } while_for_each_ftrace_rec();
5100
5101 out_loop:
5102 if (ftrace_start_up)
5103 ftrace_arch_code_modify_post_process();
5104
5105 out_unlock:
5106 mutex_unlock(&ftrace_lock);
5107}
5108
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04005109void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04005110{
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005111 if (ftrace_disabled || !mod->num_ftrace_callsites)
Abel Vesab6b71f62015-12-02 15:39:57 +01005112 return;
5113
Steven Rostedt (Red Hat)97e9b4f2015-12-23 12:12:22 -05005114 ftrace_process_locs(mod, mod->ftrace_callsites,
5115 mod->ftrace_callsites + mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05005116}
Steven Rostedt93eb6772009-04-15 13:24:06 -04005117#endif /* CONFIG_MODULES */
5118
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005119void __init ftrace_init(void)
5120{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01005121 extern unsigned long __start_mcount_loc[];
5122 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005123 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005124 int ret;
5125
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005126 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01005127 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005128 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01005129 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005130 goto failed;
5131
5132 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005133 if (!count) {
5134 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005135 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01005136 }
5137
5138 pr_info("ftrace: allocating %ld entries in %ld pages\n",
5139 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005140
5141 last_ftrace_enabled = ftrace_enabled = 1;
5142
Jiri Olsa5cb084b2009-10-13 16:33:53 -04005143 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08005144 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005145 __stop_mcount_loc);
5146
Steven Rostedt2af15d62009-05-28 13:37:24 -04005147 set_ftrace_early_filters();
5148
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005149 return;
5150 failed:
5151 ftrace_disabled = 1;
5152}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04005153
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005154/* Do nothing if arch does not support this */
5155void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5156{
5157}
5158
5159static void ftrace_update_trampoline(struct ftrace_ops *ops)
5160{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005161
5162/*
5163 * Currently there's no safe way to free a trampoline when the kernel
5164 * is configured with PREEMPT. That is because a task could be preempted
5165 * when it jumped to the trampoline, it may be preempted for a long time
5166 * depending on the system load, and currently there's no way to know
5167 * when it will be off the trampoline. If the trampoline is freed
5168 * too early, when the task runs again, it will be executing on freed
5169 * memory and crash.
5170 */
5171#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005172 /* Currently, only non dynamic ops can have a trampoline */
5173 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5174 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005175#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005176
5177 arch_ftrace_update_trampoline(ops);
5178}
5179
Steven Rostedt3d083392008-05-12 21:20:42 +02005180#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005181
Steven Rostedt2b499382011-05-03 22:49:52 -04005182static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005183 .func = ftrace_stub,
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005184 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5185 FTRACE_OPS_FL_INITIALIZED |
5186 FTRACE_OPS_FL_PID,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005187};
5188
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005189static int __init ftrace_nodyn_init(void)
5190{
5191 ftrace_enabled = 1;
5192 return 0;
5193}
Steven Rostedt6f415672012-10-05 12:13:07 -04005194core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005195
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05005196static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005197static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005198static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005199/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005200# define ftrace_startup(ops, command) \
5201 ({ \
5202 int ___ret = __register_ftrace_function(ops); \
5203 if (!___ret) \
5204 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5205 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005206 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005207# define ftrace_shutdown(ops, command) \
5208 ({ \
5209 int ___ret = __unregister_ftrace_function(ops); \
5210 if (!___ret) \
5211 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5212 ___ret; \
5213 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005214
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005215# define ftrace_startup_sysctl() do { } while (0)
5216# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005217
5218static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005219ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005220{
5221 return 1;
5222}
5223
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005224static void ftrace_update_trampoline(struct ftrace_ops *ops)
5225{
5226}
5227
Steven Rostedt3d083392008-05-12 21:20:42 +02005228#endif /* CONFIG_DYNAMIC_FTRACE */
5229
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005230__init void ftrace_init_global_array_ops(struct trace_array *tr)
5231{
5232 tr->ops = &global_ops;
5233 tr->ops->private = tr;
5234}
5235
5236void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5237{
5238 /* If we filter on pids, update to use the pid function */
5239 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5240 if (WARN_ON(tr->ops->func != ftrace_stub))
5241 printk("ftrace ops had %pS for function\n",
5242 tr->ops->func);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005243 }
5244 tr->ops->func = func;
5245 tr->ops->private = tr;
5246}
5247
5248void ftrace_reset_array_ops(struct trace_array *tr)
5249{
5250 tr->ops->func = ftrace_stub;
5251}
5252
Masami Hiramatsu25f467d2019-02-24 01:50:20 +09005253static nokprobe_inline void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005254__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005255 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005256{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005257 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005258 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005259
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005260 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5261 if (bit < 0)
5262 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005263
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005264 /*
5265 * Some of the ops may be dynamically allocated,
5266 * they must be freed after a synchronize_sched().
5267 */
5268 preempt_disable_notrace();
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005269
Steven Rostedt0a016402012-11-02 17:03:03 -04005270 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)ba27f2b2015-11-30 17:23:39 -05005271 /*
5272 * Check the following for each ops before calling their func:
5273 * if RCU flag is set, then rcu_is_watching() must be true
5274 * if PER_CPU is set, then ftrace_function_local_disable()
5275 * must be false
5276 * Otherwise test if the ip matches the ops filter
5277 *
5278 * If any of the above fails then the op->func() is not executed.
5279 */
5280 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5281 (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5282 !ftrace_function_local_disabled(op)) &&
5283 ftrace_ops_test(op, ip, regs)) {
5284
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005285 if (FTRACE_WARN_ON(!op->func)) {
5286 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005287 goto out;
5288 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005289 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005290 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005291 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005292out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005293 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005294 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005295}
5296
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005297/*
5298 * Some archs only support passing ip and parent_ip. Even though
5299 * the list function ignores the op parameter, we do not want any
5300 * C side effects, where a function is called without the caller
5301 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005302 * Archs are to support both the regs and ftrace_ops at the same time.
5303 * If they support ftrace_ops, it is assumed they support regs.
5304 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005305 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5306 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005307 * An architecture can pass partial regs with ftrace_ops and still
Li Binb8ec3302015-11-30 18:23:36 +08005308 * set the ARCH_SUPPORTS_FTRACE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005309 */
5310#if ARCH_SUPPORTS_FTRACE_OPS
5311static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005312 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005313{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005314 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005315}
Masami Hiramatsu25f467d2019-02-24 01:50:20 +09005316NOKPROBE_SYMBOL(ftrace_ops_list_func);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005317#else
Sami Tolvanenc2f9bce2018-05-10 14:56:41 -07005318static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip,
5319 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005320{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005321 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005322}
Masami Hiramatsu25f467d2019-02-24 01:50:20 +09005323NOKPROBE_SYMBOL(ftrace_ops_no_ops);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005324#endif
5325
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005326/*
5327 * If there's only one function registered but it does not support
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005328 * recursion, needs RCU protection and/or requires per cpu handling, then
5329 * this function will be called by the mcount trampoline.
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005330 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005331static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005332 struct ftrace_ops *op, struct pt_regs *regs)
5333{
5334 int bit;
5335
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005336 if ((op->flags & FTRACE_OPS_FL_RCU) && !rcu_is_watching())
5337 return;
5338
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005339 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5340 if (bit < 0)
5341 return;
5342
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005343 preempt_disable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005344
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005345 if (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5346 !ftrace_function_local_disabled(op)) {
5347 op->func(ip, parent_ip, op, regs);
5348 }
5349
5350 preempt_enable_notrace();
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005351 trace_clear_recursion(bit);
5352}
Masami Hiramatsu25f467d2019-02-24 01:50:20 +09005353NOKPROBE_SYMBOL(ftrace_ops_assist_func);
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005354
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005355/**
5356 * ftrace_ops_get_func - get the function a trampoline should call
5357 * @ops: the ops to get the function for
5358 *
5359 * Normally the mcount trampoline will call the ops->func, but there
5360 * are times that it should not. For example, if the ops does not
5361 * have its own recursion protection, then it should call the
5362 * ftrace_ops_recurs_func() instead.
5363 *
5364 * Returns the function that the trampoline should call for @ops.
5365 */
5366ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5367{
5368 /*
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005369 * If the function does not handle recursion, needs to be RCU safe,
5370 * or does per cpu logic, then we need to call the assist handler.
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005371 */
Steven Rostedt (Red Hat)c68c0fa2015-12-01 13:28:16 -05005372 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5373 ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5374 return ftrace_ops_assist_func;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005375
5376 return ops->func;
5377}
5378
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005379static void
5380ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5381 struct task_struct *prev, struct task_struct *next)
Steven Rostedte32d8952008-12-04 00:26:41 -05005382{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005383 struct trace_array *tr = data;
5384 struct trace_pid_list *pid_list;
5385
5386 pid_list = rcu_dereference_sched(tr->function_pids);
5387
5388 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5389 trace_ignore_this_task(pid_list, next));
5390}
5391
5392static void clear_ftrace_pids(struct trace_array *tr)
5393{
5394 struct trace_pid_list *pid_list;
Steven Rostedte32d8952008-12-04 00:26:41 -05005395 int cpu;
5396
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005397 pid_list = rcu_dereference_protected(tr->function_pids,
5398 lockdep_is_held(&ftrace_lock));
5399 if (!pid_list)
5400 return;
5401
5402 unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5403
5404 for_each_possible_cpu(cpu)
5405 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5406
5407 rcu_assign_pointer(tr->function_pids, NULL);
5408
5409 /* Wait till all users are no longer using pid filtering */
5410 synchronize_sched();
5411
5412 trace_free_pid_list(pid_list);
Steven Rostedte32d8952008-12-04 00:26:41 -05005413}
5414
Namhyung Kim7da0f8e2017-04-17 11:44:27 +09005415void ftrace_clear_pids(struct trace_array *tr)
5416{
5417 mutex_lock(&ftrace_lock);
5418
5419 clear_ftrace_pids(tr);
5420
5421 mutex_unlock(&ftrace_lock);
5422}
5423
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005424static void ftrace_pid_reset(struct trace_array *tr)
Steven Rostedte32d8952008-12-04 00:26:41 -05005425{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005426 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005427 clear_ftrace_pids(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005428
5429 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005430 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005431
5432 mutex_unlock(&ftrace_lock);
5433}
5434
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005435/* Greater than any max PID */
5436#define FTRACE_NO_PIDS (void *)(PID_MAX_LIMIT + 1)
5437
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005438static void *fpid_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005439 __acquires(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005440{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005441 struct trace_pid_list *pid_list;
5442 struct trace_array *tr = m->private;
5443
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005444 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005445 rcu_read_lock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005446
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005447 pid_list = rcu_dereference_sched(tr->function_pids);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005448
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005449 if (!pid_list)
5450 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5451
5452 return trace_pid_start(pid_list, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005453}
5454
5455static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5456{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005457 struct trace_array *tr = m->private;
5458 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5459
5460 if (v == FTRACE_NO_PIDS)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005461 return NULL;
5462
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005463 return trace_pid_next(pid_list, v, pos);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005464}
5465
5466static void fpid_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005467 __releases(RCU)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005468{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005469 rcu_read_unlock_sched();
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005470 mutex_unlock(&ftrace_lock);
5471}
5472
5473static int fpid_show(struct seq_file *m, void *v)
5474{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005475 if (v == FTRACE_NO_PIDS) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005476 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005477 return 0;
5478 }
5479
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005480 return trace_pid_show(m, v);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005481}
5482
5483static const struct seq_operations ftrace_pid_sops = {
5484 .start = fpid_start,
5485 .next = fpid_next,
5486 .stop = fpid_stop,
5487 .show = fpid_show,
5488};
5489
5490static int
5491ftrace_pid_open(struct inode *inode, struct file *file)
5492{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005493 struct trace_array *tr = inode->i_private;
5494 struct seq_file *m;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005495 int ret = 0;
5496
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005497 if (trace_array_get(tr) < 0)
5498 return -ENODEV;
5499
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005500 if ((file->f_mode & FMODE_WRITE) &&
5501 (file->f_flags & O_TRUNC))
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005502 ftrace_pid_reset(tr);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005503
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005504 ret = seq_open(file, &ftrace_pid_sops);
5505 if (ret < 0) {
5506 trace_array_put(tr);
5507 } else {
5508 m = file->private_data;
5509 /* copy tr over to seq ops */
5510 m->private = tr;
5511 }
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005512
5513 return ret;
5514}
5515
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005516static void ignore_task_cpu(void *data)
5517{
5518 struct trace_array *tr = data;
5519 struct trace_pid_list *pid_list;
5520
5521 /*
5522 * This function is called by on_each_cpu() while the
5523 * event_mutex is held.
5524 */
5525 pid_list = rcu_dereference_protected(tr->function_pids,
5526 mutex_is_locked(&ftrace_lock));
5527
5528 this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5529 trace_ignore_this_task(pid_list, current));
5530}
5531
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005532static ssize_t
5533ftrace_pid_write(struct file *filp, const char __user *ubuf,
5534 size_t cnt, loff_t *ppos)
5535{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005536 struct seq_file *m = filp->private_data;
5537 struct trace_array *tr = m->private;
5538 struct trace_pid_list *filtered_pids = NULL;
5539 struct trace_pid_list *pid_list;
5540 ssize_t ret;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005541
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005542 if (!cnt)
5543 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005544
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005545 mutex_lock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005546
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005547 filtered_pids = rcu_dereference_protected(tr->function_pids,
5548 lockdep_is_held(&ftrace_lock));
5549
5550 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5551 if (ret < 0)
5552 goto out;
5553
5554 rcu_assign_pointer(tr->function_pids, pid_list);
5555
5556 if (filtered_pids) {
5557 synchronize_sched();
5558 trace_free_pid_list(filtered_pids);
5559 } else if (pid_list) {
5560 /* Register a probe to set whether to ignore the tracing of a task */
5561 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5562 }
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005563
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005564 /*
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005565 * Ignoring of pids is done at task switch. But we have to
5566 * check for those tasks that are currently running.
5567 * Always do this in case a pid was appended or removed.
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005568 */
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005569 on_each_cpu(ignore_task_cpu, tr, 1);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005570
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005571 ftrace_update_pid_func();
5572 ftrace_startup_all(0);
5573 out:
5574 mutex_unlock(&ftrace_lock);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005575
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005576 if (ret > 0)
5577 *ppos += ret;
Steven Rostedt978f3a42008-12-04 00:26:40 -05005578
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005579 return ret;
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005580}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005581
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005582static int
5583ftrace_pid_release(struct inode *inode, struct file *file)
5584{
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005585 struct trace_array *tr = inode->i_private;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005586
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005587 trace_array_put(tr);
5588
5589 return seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005590}
5591
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005592static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005593 .open = ftrace_pid_open,
5594 .write = ftrace_pid_write,
5595 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005596 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005597 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005598};
5599
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005600void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005601{
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005602 trace_create_file("set_ftrace_pid", 0644, d_tracer,
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -04005603 tr, &ftrace_pid_fops);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005604}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005605
Steven Rostedt (Red Hat)501c2372016-07-05 10:04:34 -04005606void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5607 struct dentry *d_tracer)
5608{
5609 /* Only the top level directory has the dyn_tracefs and profile */
5610 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5611
5612 ftrace_init_dyn_tracefs(d_tracer);
5613 ftrace_profile_tracefs(d_tracer);
5614}
5615
Steven Rostedt3d083392008-05-12 21:20:42 +02005616/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005617 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005618 *
5619 * This function should be used by panic code. It stops ftrace
5620 * but in a not so nice way. If you need to simply kill ftrace
5621 * from a non-atomic section, use ftrace_kill.
5622 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005623void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005624{
5625 ftrace_disabled = 1;
5626 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005627 clear_ftrace_function();
5628}
5629
5630/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005631 * Test if ftrace is dead or not.
5632 */
5633int ftrace_is_dead(void)
5634{
5635 return ftrace_disabled;
5636}
5637
5638/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005639 * register_ftrace_function - register a function for profiling
5640 * @ops - ops structure that holds the function for profiling.
5641 *
5642 * Register a function to be called by all functions in the
5643 * kernel.
5644 *
5645 * Note: @ops->func and all the functions it calls must be labeled
5646 * with "notrace", otherwise it will go into a
5647 * recursive loop.
5648 */
5649int register_ftrace_function(struct ftrace_ops *ops)
5650{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005651 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005652
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005653 ftrace_ops_init(ops);
5654
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005655 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005656
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005657 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005658
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005659 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005660
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005661 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005662}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005663EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005664
5665/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005666 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005667 * @ops - ops structure that holds the function to unregister
5668 *
5669 * Unregister a function that was added to be called by ftrace profiling.
5670 */
5671int unregister_ftrace_function(struct ftrace_ops *ops)
5672{
5673 int ret;
5674
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005675 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005676 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005677 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005678
5679 return ret;
5680}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005681EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005682
Ingo Molnare309b412008-05-12 21:20:51 +02005683int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005684ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005685 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005686 loff_t *ppos)
5687{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005688 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005689
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005690 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005691
Steven Rostedt45a4a232011-04-21 23:16:46 -04005692 if (unlikely(ftrace_disabled))
5693 goto out;
5694
5695 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005696
Li Zefana32c7762009-06-26 16:55:51 +08005697 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005698 goto out;
5699
Li Zefana32c7762009-06-26 16:55:51 +08005700 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005701
5702 if (ftrace_enabled) {
5703
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005704 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005705 if (ftrace_ops_list != &ftrace_list_end)
5706 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005707
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05005708 ftrace_startup_sysctl();
5709
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005710 } else {
5711 /* stopping ftrace calls (just send to ftrace_stub) */
5712 ftrace_trace_function = ftrace_stub;
5713
5714 ftrace_shutdown_sysctl();
5715 }
5716
5717 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005718 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005719 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005720}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005721
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005722#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005723
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005724static struct ftrace_ops graph_ops = {
5725 .func = ftrace_stub,
5726 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5727 FTRACE_OPS_FL_INITIALIZED |
Steven Rostedt (Red Hat)e3eea142015-07-24 10:38:12 -04005728 FTRACE_OPS_FL_PID |
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005729 FTRACE_OPS_FL_STUB,
5730#ifdef FTRACE_GRAPH_TRAMP_ADDR
5731 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05005732 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005733#endif
5734 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5735};
5736
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005737void ftrace_graph_sleep_time_control(bool enable)
5738{
5739 fgraph_sleep_time = enable;
5740}
5741
5742void ftrace_graph_graph_time_control(bool enable)
5743{
5744 fgraph_graph_time = enable;
5745}
5746
Sami Tolvanenc2f9bce2018-05-10 14:56:41 -07005747void ftrace_graph_return_stub(struct ftrace_graph_ret *trace)
5748{
5749}
5750
Steven Rostedte49dc192008-12-02 23:50:05 -05005751int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5752{
5753 return 0;
5754}
5755
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005756/* The callbacks that hook a function */
Sami Tolvanenc2f9bce2018-05-10 14:56:41 -07005757trace_func_graph_ret_t ftrace_graph_return = ftrace_graph_return_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005758trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005759static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005760
5761/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5762static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5763{
5764 int i;
5765 int ret = 0;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005766 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5767 struct task_struct *g, *t;
5768
5769 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5770 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5771 * sizeof(struct ftrace_ret_stack),
5772 GFP_KERNEL);
5773 if (!ret_stack_list[i]) {
5774 start = 0;
5775 end = i;
5776 ret = -ENOMEM;
5777 goto free;
5778 }
5779 }
5780
Soumya PN6112a302016-05-17 21:31:14 +05305781 read_lock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005782 do_each_thread(g, t) {
5783 if (start == end) {
5784 ret = -EAGAIN;
5785 goto unlock;
5786 }
5787
5788 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005789 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005790 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005791 t->curr_ret_stack = -1;
5792 /* Make sure the tasks see the -1 first: */
5793 smp_wmb();
5794 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005795 }
5796 } while_each_thread(g, t);
5797
5798unlock:
Soumya PN6112a302016-05-17 21:31:14 +05305799 read_unlock(&tasklist_lock);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005800free:
5801 for (i = start; i < end; i++)
5802 kfree(ret_stack_list[i]);
5803 return ret;
5804}
5805
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005806static void
Peter Zijlstrac73464b2015-09-28 18:06:56 +02005807ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
Steven Rostedt38516ab2010-04-20 17:04:50 -04005808 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005809{
5810 unsigned long long timestamp;
5811 int index;
5812
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005813 /*
5814 * Does the user want to count the time a function was asleep.
5815 * If so, do not update the time stamps.
5816 */
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04005817 if (fgraph_sleep_time)
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005818 return;
5819
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005820 timestamp = trace_clock_local();
5821
5822 prev->ftrace_timestamp = timestamp;
5823
5824 /* only process tasks that we timestamped */
5825 if (!next->ftrace_timestamp)
5826 return;
5827
5828 /*
5829 * Update all the counters in next to make up for the
5830 * time next was sleeping.
5831 */
5832 timestamp -= next->ftrace_timestamp;
5833
5834 for (index = next->curr_ret_stack; index >= 0; index--)
5835 next->ret_stack[index].calltime += timestamp;
5836}
5837
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005838/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005839static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005840{
5841 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005842 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005843
5844 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5845 sizeof(struct ftrace_ret_stack *),
5846 GFP_KERNEL);
5847
5848 if (!ret_stack_list)
5849 return -ENOMEM;
5850
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005851 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005852 for_each_online_cpu(cpu) {
5853 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005854 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005855 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005856
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005857 do {
5858 ret = alloc_retstack_tasklist(ret_stack_list);
5859 } while (ret == -EAGAIN);
5860
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005861 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005862 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005863 if (ret)
5864 pr_info("ftrace_graph: Couldn't activate tracepoint"
5865 " probe to kernel_sched_switch\n");
5866 }
5867
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005868 kfree(ret_stack_list);
5869 return ret;
5870}
5871
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005872/*
5873 * Hibernation protection.
5874 * The state of the current task is too much unstable during
5875 * suspend/restore to disk. We want to protect against that.
5876 */
5877static int
5878ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5879 void *unused)
5880{
5881 switch (state) {
5882 case PM_HIBERNATION_PREPARE:
5883 pause_graph_tracing();
5884 break;
5885
5886 case PM_POST_HIBERNATION:
5887 unpause_graph_tracing();
5888 break;
5889 }
5890 return NOTIFY_DONE;
5891}
5892
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005893static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5894{
5895 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5896 return 0;
5897 return __ftrace_graph_entry(trace);
5898}
5899
5900/*
5901 * The function graph tracer should only trace the functions defined
5902 * by set_ftrace_filter and set_ftrace_notrace. If another function
5903 * tracer ops is registered, the graph tracer requires testing the
5904 * function against the global ops, and not just trace any function
5905 * that any ftrace_ops registered.
5906 */
5907static void update_function_graph_func(void)
5908{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005909 struct ftrace_ops *op;
5910 bool do_test = false;
5911
5912 /*
5913 * The graph and global ops share the same set of functions
5914 * to test. If any other ops is on the list, then
5915 * the graph tracing needs to test if its the function
5916 * it should call.
5917 */
5918 do_for_each_ftrace_op(op, ftrace_ops_list) {
5919 if (op != &global_ops && op != &graph_ops &&
5920 op != &ftrace_list_end) {
5921 do_test = true;
5922 /* in double loop, break out with goto */
5923 goto out;
5924 }
5925 } while_for_each_ftrace_op(op);
5926 out:
5927 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005928 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005929 else
5930 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005931}
5932
Mathias Krause8275f692014-03-30 15:31:50 +02005933static struct notifier_block ftrace_suspend_notifier = {
5934 .notifier_call = ftrace_suspend_notifier_call,
5935};
5936
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005937int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5938 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005939{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005940 int ret = 0;
5941
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005942 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005943
Steven Rostedt05ce5812009-03-24 00:18:31 -04005944 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005945 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005946 ret = -EBUSY;
5947 goto out;
5948 }
5949
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005950 register_pm_notifier(&ftrace_suspend_notifier);
5951
Steven Rostedt597af812009-04-03 15:24:12 -04005952 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005953 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005954 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005955 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005956 goto out;
5957 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005958
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005959 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005960
5961 /*
5962 * Update the indirect function to the entryfunc, and the
5963 * function that gets called to the entry_test first. Then
5964 * call the update fgraph entry function to determine if
5965 * the entryfunc should be called directly or not.
5966 */
5967 __ftrace_graph_entry = entryfunc;
5968 ftrace_graph_entry = ftrace_graph_entry_test;
5969 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005970
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005971 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005972out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005973 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005974 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005975}
5976
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005977void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005978{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005979 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005980
Steven Rostedt597af812009-04-03 15:24:12 -04005981 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005982 goto out;
5983
Steven Rostedt597af812009-04-03 15:24:12 -04005984 ftrace_graph_active--;
Sami Tolvanenc2f9bce2018-05-10 14:56:41 -07005985 ftrace_graph_return = ftrace_graph_return_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005986 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005987 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005988 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005989 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005990 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005991
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005992 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005993 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005994}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005995
Steven Rostedt868baf02011-02-10 21:26:13 -05005996static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5997
5998static void
5999graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6000{
6001 atomic_set(&t->tracing_graph_pause, 0);
6002 atomic_set(&t->trace_overrun, 0);
6003 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006004 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05006005 smp_wmb();
6006 t->ret_stack = ret_stack;
6007}
6008
6009/*
6010 * Allocate a return stack for the idle task. May be the first
6011 * time through, or it may be done by CPU hotplug online.
6012 */
6013void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6014{
6015 t->curr_ret_stack = -1;
6016 /*
6017 * The idle task has no parent, it either has its own
6018 * stack or no stack at all.
6019 */
6020 if (t->ret_stack)
6021 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6022
6023 if (ftrace_graph_active) {
6024 struct ftrace_ret_stack *ret_stack;
6025
6026 ret_stack = per_cpu(idle_ret_stack, cpu);
6027 if (!ret_stack) {
6028 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6029 * sizeof(struct ftrace_ret_stack),
6030 GFP_KERNEL);
6031 if (!ret_stack)
6032 return;
6033 per_cpu(idle_ret_stack, cpu) = ret_stack;
6034 }
6035 graph_init_task(t, ret_stack);
6036 }
6037}
6038
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006039/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006040void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006041{
Steven Rostedt84047e32009-06-02 16:51:55 -04006042 /* Make sure we do not use the parent ret_stack */
6043 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05006044 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04006045
Steven Rostedt597af812009-04-03 15:24:12 -04006046 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04006047 struct ftrace_ret_stack *ret_stack;
6048
6049 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006050 * sizeof(struct ftrace_ret_stack),
6051 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04006052 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006053 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05006054 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04006055 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006056}
6057
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006058void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006059{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006060 struct ftrace_ret_stack *ret_stack = t->ret_stack;
6061
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006062 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01006063 /* NULL must become visible to IRQs before we free it: */
6064 barrier();
6065
6066 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01006067}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01006068#endif