blob: 4043332f672065da6f1916b069bb71e5d154dc42 [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 Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -050065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
Jiri Olsae2484912012-02-15 15:51:48 +010066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040068#define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040071#define ASSIGN_OPS_HASH(opsname, val) \
72 .func_hash = val, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090074#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040075#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040076#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090077#endif
78
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040079static struct ftrace_ops ftrace_list_end __read_mostly = {
80 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040081 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040082 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040083};
84
Steven Rostedt4eebcc82008-05-12 21:20:48 +020085/* ftrace_enabled is a method to turn ftrace on or off */
86int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020087static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020088
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040089/* Current function tracing op */
90struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050091/* What to set function_trace_op to */
92static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050093
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040094/* List for set_ftrace_pid's pids. */
95LIST_HEAD(ftrace_pids);
96struct ftrace_pid {
97 struct list_head list;
98 struct pid *pid;
99};
100
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200101/*
102 * ftrace_disabled is set when an anomaly is discovered.
103 * ftrace_disabled is much stronger than ftrace_enabled.
104 */
105static int ftrace_disabled __read_mostly;
106
Steven Rostedt52baf112009-02-14 01:15:39 -0500107static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200108
Jiri Olsae2484912012-02-15 15:51:48 +0100109static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400110static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500112ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400113static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100114static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -0400116static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
117 struct ftrace_ops *op, struct pt_regs *regs);
118
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 */
124static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
125#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
126#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400127
Steven Rostedt0a016402012-11-02 17:03:03 -0400128/*
129 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400130 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400131 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400132 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400133 * concurrent insertions into the ftrace_global_list.
134 *
135 * Silly Alpha and silly pointer-speculation compiler optimizations!
136 */
137#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 do
140
141/*
142 * Optimized for just a single item in the list (as that is the normal case).
143 */
144#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400145 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400146 unlikely((op) != &ftrace_list_end))
147
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900148static inline void ftrace_ops_init(struct ftrace_ops *ops)
149{
150#ifdef CONFIG_DYNAMIC_FTRACE
151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
155 }
156#endif
157}
158
Steven Rostedtea701f12012-07-20 13:08:05 -0400159/**
160 * ftrace_nr_registered_ops - return number of ops registered
161 *
162 * Returns the number of ftrace_ops registered and tracing functions
163 */
164int ftrace_nr_registered_ops(void)
165{
166 struct ftrace_ops *ops;
167 int cnt = 0;
168
169 mutex_lock(&ftrace_lock);
170
171 for (ops = ftrace_ops_list;
172 ops != &ftrace_list_end; ops = ops->next)
173 cnt++;
174
175 mutex_unlock(&ftrace_lock);
176
177 return cnt;
178}
179
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400180static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400181 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500182{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500183 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500184 return;
185
Steven Rostedta1e2e312011-08-09 12:50:46 -0400186 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500187}
188
189static void set_ftrace_pid_function(ftrace_func_t func)
190{
191 /* do not set ftrace_pid_function to itself! */
192 if (func != ftrace_pid_func)
193 ftrace_pid_function = func;
194}
195
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200197 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200202void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200203{
Steven Rostedt3d083392008-05-12 21:20:42 +0200204 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500205 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200206}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207
Jiri Olsae2484912012-02-15 15:51:48 +0100208static void control_ops_disable_all(struct ftrace_ops *ops)
209{
210 int cpu;
211
212 for_each_possible_cpu(cpu)
213 *per_cpu_ptr(ops->disabled, cpu) = 1;
214}
215
216static int control_ops_alloc(struct ftrace_ops *ops)
217{
218 int __percpu *disabled;
219
220 disabled = alloc_percpu(int);
221 if (!disabled)
222 return -ENOMEM;
223
224 ops->disabled = disabled;
225 control_ops_disable_all(ops);
226 return 0;
227}
228
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500229static void ftrace_sync(struct work_struct *work)
230{
231 /*
232 * This function is just a stub to implement a hard force
233 * of synchronize_sched(). This requires synchronizing
234 * tasks even in userspace and idle.
235 *
236 * Yes, function tracing is rude.
237 */
238}
239
240static void ftrace_sync_ipi(void *data)
241{
242 /* Probably not needed, but do it anyway */
243 smp_rmb();
244}
245
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500246#ifdef CONFIG_FUNCTION_GRAPH_TRACER
247static void update_function_graph_func(void);
248#else
249static inline void update_function_graph_func(void) { }
250#endif
251
Steven Rostedt2b499382011-05-03 22:49:52 -0400252static void update_ftrace_function(void)
253{
254 ftrace_func_t func;
255
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400256 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400257 * Prepare the ftrace_ops that the arch callback will use.
258 * If there's only one ftrace_ops registered, the ftrace_ops_list
259 * will point to the ops we want.
260 */
261 set_function_trace_op = ftrace_ops_list;
262
263 /* If there's no ftrace_ops registered, just call the stub function */
264 if (ftrace_ops_list == &ftrace_list_end) {
265 func = ftrace_stub;
266
267 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400268 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400269 * recursion safe and not dynamic and the arch supports passing ops,
270 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400271 */
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400272 } else if (ftrace_ops_list->next == &ftrace_list_end) {
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -0400273 func = ftrace_ops_get_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400274
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400275 } else {
276 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500277 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400278 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400279 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400280
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400281 update_function_graph_func();
282
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500283 /* If there's no change, then do nothing more here */
284 if (ftrace_trace_function == func)
285 return;
286
287 /*
288 * If we are using the list function, it doesn't care
289 * about the function_trace_ops.
290 */
291 if (func == ftrace_ops_list_func) {
292 ftrace_trace_function = func;
293 /*
294 * Don't even bother setting function_trace_ops,
295 * it would be racy to do so anyway.
296 */
297 return;
298 }
299
300#ifndef CONFIG_DYNAMIC_FTRACE
301 /*
302 * For static tracing, we need to be a bit more careful.
303 * The function change takes affect immediately. Thus,
304 * we need to coorditate the setting of the function_trace_ops
305 * with the setting of the ftrace_trace_function.
306 *
307 * Set the function to the list ops, which will call the
308 * function we want, albeit indirectly, but it handles the
309 * ftrace_ops and doesn't depend on function_trace_op.
310 */
311 ftrace_trace_function = ftrace_ops_list_func;
312 /*
313 * Make sure all CPUs see this. Yes this is slow, but static
314 * tracing is slow and nasty to have enabled.
315 */
316 schedule_on_each_cpu(ftrace_sync);
317 /* Now all cpus are using the list ops. */
318 function_trace_op = set_function_trace_op;
319 /* Make sure the function_trace_op is visible on all CPUs */
320 smp_wmb();
321 /* Nasty way to force a rmb on all cpus */
322 smp_call_function(ftrace_sync_ipi, NULL, 1);
323 /* OK, we are all set to update the ftrace_trace_function now! */
324#endif /* !CONFIG_DYNAMIC_FTRACE */
325
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400326 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400327}
328
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800329int using_ftrace_ops_list_func(void)
330{
331 return ftrace_trace_function == ftrace_ops_list_func;
332}
333
Steven Rostedt2b499382011-05-03 22:49:52 -0400334static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200335{
Steven Rostedt2b499382011-05-03 22:49:52 -0400336 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200337 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400338 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200339 * CPU might be walking that list. We need to make sure
340 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400341 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200342 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400343 rcu_assign_pointer(*list, ops);
344}
Steven Rostedt3d083392008-05-12 21:20:42 +0200345
Steven Rostedt2b499382011-05-03 22:49:52 -0400346static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
347{
348 struct ftrace_ops **p;
349
350 /*
351 * If we are removing the last function, then simply point
352 * to the ftrace_stub.
353 */
354 if (*list == ops && ops->next == &ftrace_list_end) {
355 *list = &ftrace_list_end;
356 return 0;
357 }
358
359 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
360 if (*p == ops)
361 break;
362
363 if (*p != ops)
364 return -1;
365
366 *p = (*p)->next;
367 return 0;
368}
369
Jiri Olsae2484912012-02-15 15:51:48 +0100370static void add_ftrace_list_ops(struct ftrace_ops **list,
371 struct ftrace_ops *main_ops,
372 struct ftrace_ops *ops)
373{
374 int first = *list == &ftrace_list_end;
375 add_ftrace_ops(list, ops);
376 if (first)
377 add_ftrace_ops(&ftrace_ops_list, main_ops);
378}
379
380static int remove_ftrace_list_ops(struct ftrace_ops **list,
381 struct ftrace_ops *main_ops,
382 struct ftrace_ops *ops)
383{
384 int ret = remove_ftrace_ops(list, ops);
385 if (!ret && *list == &ftrace_list_end)
386 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
387 return ret;
388}
389
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400390static void ftrace_update_trampoline(struct ftrace_ops *ops);
391
Steven Rostedt2b499382011-05-03 22:49:52 -0400392static int __register_ftrace_function(struct ftrace_ops *ops)
393{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500394 if (ops->flags & FTRACE_OPS_FL_DELETED)
395 return -EINVAL;
396
Steven Rostedtb8489142011-05-04 09:27:52 -0400397 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
398 return -EBUSY;
399
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900400#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400401 /*
402 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
403 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
404 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
405 */
406 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
407 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
408 return -EINVAL;
409
410 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
411 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
412#endif
413
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400414 if (!core_kernel_data((unsigned long)ops))
415 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
416
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500417 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100418 if (control_ops_alloc(ops))
419 return -ENOMEM;
420 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400421 } else
422 add_ftrace_ops(&ftrace_ops_list, ops);
423
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400424 ftrace_update_trampoline(ops);
425
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400426 if (ftrace_enabled)
427 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200428
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200429 return 0;
430}
431
Ingo Molnare309b412008-05-12 21:20:51 +0200432static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200433{
Steven Rostedt2b499382011-05-03 22:49:52 -0400434 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200435
Steven Rostedtb8489142011-05-04 09:27:52 -0400436 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
437 return -EBUSY;
438
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500439 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100440 ret = remove_ftrace_list_ops(&ftrace_control_list,
441 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400442 } else
443 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
444
Steven Rostedt2b499382011-05-03 22:49:52 -0400445 if (ret < 0)
446 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400447
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400448 if (ftrace_enabled)
449 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200450
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500451 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200452}
453
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500454static void ftrace_update_pid_func(void)
455{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400456 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500457 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900458 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500459
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400460 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500461}
462
Steven Rostedt493762f2009-03-23 17:12:36 -0400463#ifdef CONFIG_FUNCTION_PROFILER
464struct ftrace_profile {
465 struct hlist_node node;
466 unsigned long ip;
467 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400468#ifdef CONFIG_FUNCTION_GRAPH_TRACER
469 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400470 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400471#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400472};
473
474struct ftrace_profile_page {
475 struct ftrace_profile_page *next;
476 unsigned long index;
477 struct ftrace_profile records[];
478};
479
Steven Rostedtcafb1682009-03-24 20:50:39 -0400480struct ftrace_profile_stat {
481 atomic_t disabled;
482 struct hlist_head *hash;
483 struct ftrace_profile_page *pages;
484 struct ftrace_profile_page *start;
485 struct tracer_stat stat;
486};
487
Steven Rostedt493762f2009-03-23 17:12:36 -0400488#define PROFILE_RECORDS_SIZE \
489 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
490
491#define PROFILES_PER_PAGE \
492 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
493
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400494static int ftrace_profile_enabled __read_mostly;
495
496/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400497static DEFINE_MUTEX(ftrace_profile_lock);
498
Steven Rostedtcafb1682009-03-24 20:50:39 -0400499static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400500
Namhyung Kim20079eb2013-04-10 08:55:50 +0900501#define FTRACE_PROFILE_HASH_BITS 10
502#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400503
Steven Rostedt493762f2009-03-23 17:12:36 -0400504static void *
505function_stat_next(void *v, int idx)
506{
507 struct ftrace_profile *rec = v;
508 struct ftrace_profile_page *pg;
509
510 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
511
512 again:
Li Zefan0296e422009-06-26 11:15:37 +0800513 if (idx != 0)
514 rec++;
515
Steven Rostedt493762f2009-03-23 17:12:36 -0400516 if ((void *)rec >= (void *)&pg->records[pg->index]) {
517 pg = pg->next;
518 if (!pg)
519 return NULL;
520 rec = &pg->records[0];
521 if (!rec->counter)
522 goto again;
523 }
524
525 return rec;
526}
527
528static void *function_stat_start(struct tracer_stat *trace)
529{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400530 struct ftrace_profile_stat *stat =
531 container_of(trace, struct ftrace_profile_stat, stat);
532
533 if (!stat || !stat->start)
534 return NULL;
535
536 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400537}
538
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400539#ifdef CONFIG_FUNCTION_GRAPH_TRACER
540/* function graph compares on total time */
541static int function_stat_cmp(void *p1, void *p2)
542{
543 struct ftrace_profile *a = p1;
544 struct ftrace_profile *b = p2;
545
546 if (a->time < b->time)
547 return -1;
548 if (a->time > b->time)
549 return 1;
550 else
551 return 0;
552}
553#else
554/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400555static int function_stat_cmp(void *p1, void *p2)
556{
557 struct ftrace_profile *a = p1;
558 struct ftrace_profile *b = p2;
559
560 if (a->counter < b->counter)
561 return -1;
562 if (a->counter > b->counter)
563 return 1;
564 else
565 return 0;
566}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400567#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400568
569static int function_stat_headers(struct seq_file *m)
570{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400572 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400573 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400574 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400575 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400576#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400577 seq_printf(m, " Function Hit\n"
578 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400579#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400580 return 0;
581}
582
583static int function_stat_show(struct seq_file *m, void *v)
584{
585 struct ftrace_profile *rec = v;
586 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800587 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400588#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400589 static struct trace_seq s;
590 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400591 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400592#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800593 mutex_lock(&ftrace_profile_lock);
594
595 /* we raced with function_profile_reset() */
596 if (unlikely(rec->counter == 0)) {
597 ret = -EBUSY;
598 goto out;
599 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400600
601 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400602 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400603
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400604#ifdef CONFIG_FUNCTION_GRAPH_TRACER
605 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400606 avg = rec->time;
607 do_div(avg, rec->counter);
608
Chase Douglase330b3b2010-04-26 14:02:05 -0400609 /* Sample standard deviation (s^2) */
610 if (rec->counter <= 1)
611 stddev = 0;
612 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200613 /*
614 * Apply Welford's method:
615 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
616 */
617 stddev = rec->counter * rec->time_squared -
618 rec->time * rec->time;
619
Chase Douglase330b3b2010-04-26 14:02:05 -0400620 /*
621 * Divide only 1000 for ns^2 -> us^2 conversion.
622 * trace_print_graph_duration will divide 1000 again.
623 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200624 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400625 }
626
Steven Rostedt34886c82009-03-25 21:00:47 -0400627 trace_seq_init(&s);
628 trace_print_graph_duration(rec->time, &s);
629 trace_seq_puts(&s, " ");
630 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400631 trace_seq_puts(&s, " ");
632 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400633 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400634#endif
635 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800636out:
637 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400638
Li Zefan3aaba202010-08-23 16:50:12 +0800639 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400640}
641
Steven Rostedtcafb1682009-03-24 20:50:39 -0400642static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400643{
644 struct ftrace_profile_page *pg;
645
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400647
648 while (pg) {
649 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
650 pg->index = 0;
651 pg = pg->next;
652 }
653
Steven Rostedtcafb1682009-03-24 20:50:39 -0400654 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400655 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
656}
657
Steven Rostedtcafb1682009-03-24 20:50:39 -0400658int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400659{
660 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400661 int functions;
662 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400663 int i;
664
665 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400667 return 0;
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
670 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400671 return -ENOMEM;
672
Steven Rostedt318e0a72009-03-25 20:06:34 -0400673#ifdef CONFIG_DYNAMIC_FTRACE
674 functions = ftrace_update_tot_cnt;
675#else
676 /*
677 * We do not know the number of functions that exist because
678 * dynamic tracing is what counts them. With past experience
679 * we have around 20K functions. That should be more than enough.
680 * It is highly unlikely we will execute every function in
681 * the kernel.
682 */
683 functions = 20000;
684#endif
685
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400687
Steven Rostedt318e0a72009-03-25 20:06:34 -0400688 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
689
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900690 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400691 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400692 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400693 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400694 pg = pg->next;
695 }
696
697 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400698
699 out_free:
700 pg = stat->start;
701 while (pg) {
702 unsigned long tmp = (unsigned long)pg;
703
704 pg = pg->next;
705 free_page(tmp);
706 }
707
Steven Rostedt318e0a72009-03-25 20:06:34 -0400708 stat->pages = NULL;
709 stat->start = NULL;
710
711 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400712}
713
Steven Rostedtcafb1682009-03-24 20:50:39 -0400714static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400715{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 int size;
718
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 stat = &per_cpu(ftrace_profile_stats, cpu);
720
721 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400722 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400723 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 return 0;
725 }
726
727 /*
728 * We are profiling all functions, but usually only a few thousand
729 * functions are hit. We'll make a hash of 1024 items.
730 */
731 size = FTRACE_PROFILE_HASH_SIZE;
732
Steven Rostedtcafb1682009-03-24 20:50:39 -0400733 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400734
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400736 return -ENOMEM;
737
Steven Rostedt318e0a72009-03-25 20:06:34 -0400738 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739 if (ftrace_profile_pages_init(stat) < 0) {
740 kfree(stat->hash);
741 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 return -ENOMEM;
743 }
744
745 return 0;
746}
747
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748static int ftrace_profile_init(void)
749{
750 int cpu;
751 int ret = 0;
752
Miao Xiec4602c12013-12-16 15:20:01 +0800753 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 ret = ftrace_profile_init_cpu(cpu);
755 if (ret)
756 break;
757 }
758
759 return ret;
760}
761
Steven Rostedt493762f2009-03-23 17:12:36 -0400762/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400763static struct ftrace_profile *
764ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400765{
766 struct ftrace_profile *rec;
767 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400768 unsigned long key;
769
Namhyung Kim20079eb2013-04-10 08:55:50 +0900770 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400772
773 if (hlist_empty(hhd))
774 return NULL;
775
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400776 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400777 if (rec->ip == ip)
778 return rec;
779 }
780
781 return NULL;
782}
783
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784static void ftrace_add_profile(struct ftrace_profile_stat *stat,
785 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400786{
787 unsigned long key;
788
Namhyung Kim20079eb2013-04-10 08:55:50 +0900789 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400791}
792
Steven Rostedt318e0a72009-03-25 20:06:34 -0400793/*
794 * The memory is already allocated, this simply finds a new record to use.
795 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400796static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400797ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400798{
799 struct ftrace_profile *rec = NULL;
800
Steven Rostedt318e0a72009-03-25 20:06:34 -0400801 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 goto out;
804
Steven Rostedt493762f2009-03-23 17:12:36 -0400805 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400806 * Try to find the function again since an NMI
807 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400808 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400810 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400812
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 if (stat->pages->index == PROFILES_PER_PAGE) {
814 if (!stat->pages->next)
815 goto out;
816 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400817 }
818
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400822
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400825
826 return rec;
827}
828
Steven Rostedt493762f2009-03-23 17:12:36 -0400829static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400830function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400831 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400832{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400833 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400834 struct ftrace_profile *rec;
835 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400836
837 if (!ftrace_profile_enabled)
838 return;
839
Steven Rostedt493762f2009-03-23 17:12:36 -0400840 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400841
Christoph Lameterbdffd892014-04-29 14:17:40 -0500842 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400843 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400844 goto out;
845
846 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400847 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400848 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400849 if (!rec)
850 goto out;
851 }
852
853 rec->counter++;
854 out:
855 local_irq_restore(flags);
856}
857
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400858#ifdef CONFIG_FUNCTION_GRAPH_TRACER
859static int profile_graph_entry(struct ftrace_graph_ent *trace)
860{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400861 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400862 return 1;
863}
864
865static void profile_graph_return(struct ftrace_graph_ret *trace)
866{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400867 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400868 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400869 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400870 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400871
872 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500873 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400874 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400875 goto out;
876
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400877 /* If the calltime was zero'd ignore it */
878 if (!trace->calltime)
879 goto out;
880
Steven Rostedta2a16d62009-03-24 23:17:58 -0400881 calltime = trace->rettime - trace->calltime;
882
883 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
884 int index;
885
886 index = trace->depth;
887
888 /* Append this call time to the parent time to subtract */
889 if (index)
890 current->ret_stack[index - 1].subtime += calltime;
891
892 if (current->ret_stack[index].subtime < calltime)
893 calltime -= current->ret_stack[index].subtime;
894 else
895 calltime = 0;
896 }
897
Steven Rostedtcafb1682009-03-24 20:50:39 -0400898 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400899 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400900 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400901 rec->time_squared += calltime * calltime;
902 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400903
Steven Rostedtcafb1682009-03-24 20:50:39 -0400904 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400905 local_irq_restore(flags);
906}
907
908static int register_ftrace_profiler(void)
909{
910 return register_ftrace_graph(&profile_graph_return,
911 &profile_graph_entry);
912}
913
914static void unregister_ftrace_profiler(void)
915{
916 unregister_ftrace_graph();
917}
918#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100919static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400920 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900921 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400922 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400923};
924
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400925static int register_ftrace_profiler(void)
926{
927 return register_ftrace_function(&ftrace_profile_ops);
928}
929
930static void unregister_ftrace_profiler(void)
931{
932 unregister_ftrace_function(&ftrace_profile_ops);
933}
934#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
935
Steven Rostedt493762f2009-03-23 17:12:36 -0400936static ssize_t
937ftrace_profile_write(struct file *filp, const char __user *ubuf,
938 size_t cnt, loff_t *ppos)
939{
940 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400941 int ret;
942
Peter Huewe22fe9b52011-06-07 21:58:27 +0200943 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
944 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400945 return ret;
946
947 val = !!val;
948
949 mutex_lock(&ftrace_profile_lock);
950 if (ftrace_profile_enabled ^ val) {
951 if (val) {
952 ret = ftrace_profile_init();
953 if (ret < 0) {
954 cnt = ret;
955 goto out;
956 }
957
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400958 ret = register_ftrace_profiler();
959 if (ret < 0) {
960 cnt = ret;
961 goto out;
962 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400963 ftrace_profile_enabled = 1;
964 } else {
965 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400966 /*
967 * unregister_ftrace_profiler calls stop_machine
968 * so this acts like an synchronize_sched.
969 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400970 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400971 }
972 }
973 out:
974 mutex_unlock(&ftrace_profile_lock);
975
Jiri Olsacf8517c2009-10-23 19:36:16 -0400976 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400977
978 return cnt;
979}
980
981static ssize_t
982ftrace_profile_read(struct file *filp, char __user *ubuf,
983 size_t cnt, loff_t *ppos)
984{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400985 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400986 int r;
987
988 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
989 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
990}
991
992static const struct file_operations ftrace_profile_fops = {
993 .open = tracing_open_generic,
994 .read = ftrace_profile_read,
995 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200996 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400997};
998
Steven Rostedtcafb1682009-03-24 20:50:39 -0400999/* used to initialize the real stat files */
1000static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001001 .name = "functions",
1002 .stat_start = function_stat_start,
1003 .stat_next = function_stat_next,
1004 .stat_cmp = function_stat_cmp,
1005 .stat_headers = function_stat_headers,
1006 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001007};
1008
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001009static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001010{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001011 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001012 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001013 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001014 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001015 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001016
Steven Rostedtcafb1682009-03-24 20:50:39 -04001017 for_each_possible_cpu(cpu) {
1018 stat = &per_cpu(ftrace_profile_stats, cpu);
1019
1020 /* allocate enough for function name + cpu number */
1021 name = kmalloc(32, GFP_KERNEL);
1022 if (!name) {
1023 /*
1024 * The files created are permanent, if something happens
1025 * we still do not free memory.
1026 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001027 WARN(1,
1028 "Could not allocate stat file for cpu %d\n",
1029 cpu);
1030 return;
1031 }
1032 stat->stat = function_stats;
1033 snprintf(name, 32, "function%d", cpu);
1034 stat->stat.name = name;
1035 ret = register_stat_tracer(&stat->stat);
1036 if (ret) {
1037 WARN(1,
1038 "Could not register function stat for cpu %d\n",
1039 cpu);
1040 kfree(name);
1041 return;
1042 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001043 }
1044
1045 entry = debugfs_create_file("function_profile_enabled", 0644,
1046 d_tracer, NULL, &ftrace_profile_fops);
1047 if (!entry)
1048 pr_warning("Could not create debugfs "
1049 "'function_profile_enabled' entry\n");
1050}
1051
1052#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001053static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001054{
1055}
1056#endif /* CONFIG_FUNCTION_PROFILER */
1057
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001058static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1059
Steven Rostedt3d083392008-05-12 21:20:42 +02001060#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001061
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001062static struct ftrace_ops *removed_ops;
1063
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001064/*
1065 * Set when doing a global update, like enabling all recs or disabling them.
1066 * It is not set when just updating a single ftrace_ops.
1067 */
1068static bool update_all_ops;
1069
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001070#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001071# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001072#endif
1073
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001074static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1075
Steven Rostedtb6887d72009-02-17 12:32:04 -05001076struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001077 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001078 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001079 unsigned long flags;
1080 unsigned long ip;
1081 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001082 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001083};
1084
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001085struct ftrace_func_entry {
1086 struct hlist_node hlist;
1087 unsigned long ip;
1088};
1089
1090struct ftrace_hash {
1091 unsigned long size_bits;
1092 struct hlist_head *buckets;
1093 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001094 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001095};
1096
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001097/*
1098 * We make these constant because no one should touch them,
1099 * but they are used as the default "empty hash", to avoid allocating
1100 * it all the time. These are in a read only section such that if
1101 * anyone does try to modify it, it will cause an exception.
1102 */
1103static const struct hlist_head empty_buckets[1];
1104static const struct ftrace_hash empty_hash = {
1105 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001106};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001107#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001108
Steven Rostedt2b499382011-05-03 22:49:52 -04001109static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001110 .func = ftrace_stub,
1111 .local_hash.notrace_hash = EMPTY_HASH,
1112 .local_hash.filter_hash = EMPTY_HASH,
1113 INIT_OPS_HASH(global_ops)
1114 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1115 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001116};
1117
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001118struct ftrace_page {
1119 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001120 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001121 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001122 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001123};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001124
Steven Rostedta7900872011-12-16 16:23:44 -05001125#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1126#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001127
1128/* estimate from running different kernels */
1129#define NR_TO_INIT 10000
1130
1131static struct ftrace_page *ftrace_pages_start;
1132static struct ftrace_page *ftrace_pages;
1133
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001134static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001135{
1136 return !hash || !hash->count;
1137}
1138
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001139static struct ftrace_func_entry *
1140ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1141{
1142 unsigned long key;
1143 struct ftrace_func_entry *entry;
1144 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001145
Steven Rostedt06a51d92011-12-19 19:07:36 -05001146 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001147 return NULL;
1148
1149 if (hash->size_bits > 0)
1150 key = hash_long(ip, hash->size_bits);
1151 else
1152 key = 0;
1153
1154 hhd = &hash->buckets[key];
1155
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001156 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001157 if (entry->ip == ip)
1158 return entry;
1159 }
1160 return NULL;
1161}
1162
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001163static void __add_hash_entry(struct ftrace_hash *hash,
1164 struct ftrace_func_entry *entry)
1165{
1166 struct hlist_head *hhd;
1167 unsigned long key;
1168
1169 if (hash->size_bits)
1170 key = hash_long(entry->ip, hash->size_bits);
1171 else
1172 key = 0;
1173
1174 hhd = &hash->buckets[key];
1175 hlist_add_head(&entry->hlist, hhd);
1176 hash->count++;
1177}
1178
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001179static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1180{
1181 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001182
1183 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1184 if (!entry)
1185 return -ENOMEM;
1186
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001187 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001188 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001189
1190 return 0;
1191}
1192
1193static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001194free_hash_entry(struct ftrace_hash *hash,
1195 struct ftrace_func_entry *entry)
1196{
1197 hlist_del(&entry->hlist);
1198 kfree(entry);
1199 hash->count--;
1200}
1201
1202static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001203remove_hash_entry(struct ftrace_hash *hash,
1204 struct ftrace_func_entry *entry)
1205{
1206 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001207 hash->count--;
1208}
1209
1210static void ftrace_hash_clear(struct ftrace_hash *hash)
1211{
1212 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001213 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001214 struct ftrace_func_entry *entry;
1215 int size = 1 << hash->size_bits;
1216 int i;
1217
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001218 if (!hash->count)
1219 return;
1220
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001221 for (i = 0; i < size; i++) {
1222 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001223 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001224 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001225 }
1226 FTRACE_WARN_ON(hash->count);
1227}
1228
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001229static void free_ftrace_hash(struct ftrace_hash *hash)
1230{
1231 if (!hash || hash == EMPTY_HASH)
1232 return;
1233 ftrace_hash_clear(hash);
1234 kfree(hash->buckets);
1235 kfree(hash);
1236}
1237
Steven Rostedt07fd5512011-05-05 18:03:47 -04001238static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1239{
1240 struct ftrace_hash *hash;
1241
1242 hash = container_of(rcu, struct ftrace_hash, rcu);
1243 free_ftrace_hash(hash);
1244}
1245
1246static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1247{
1248 if (!hash || hash == EMPTY_HASH)
1249 return;
1250 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1251}
1252
Jiri Olsa5500fa52012-02-15 15:51:54 +01001253void ftrace_free_filter(struct ftrace_ops *ops)
1254{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001255 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001256 free_ftrace_hash(ops->func_hash->filter_hash);
1257 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001258}
1259
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001260static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1261{
1262 struct ftrace_hash *hash;
1263 int size;
1264
1265 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1266 if (!hash)
1267 return NULL;
1268
1269 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001270 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001271
1272 if (!hash->buckets) {
1273 kfree(hash);
1274 return NULL;
1275 }
1276
1277 hash->size_bits = size_bits;
1278
1279 return hash;
1280}
1281
1282static struct ftrace_hash *
1283alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1284{
1285 struct ftrace_func_entry *entry;
1286 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001287 int size;
1288 int ret;
1289 int i;
1290
1291 new_hash = alloc_ftrace_hash(size_bits);
1292 if (!new_hash)
1293 return NULL;
1294
1295 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001296 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001297 return new_hash;
1298
1299 size = 1 << hash->size_bits;
1300 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001301 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302 ret = add_hash_entry(new_hash, entry->ip);
1303 if (ret < 0)
1304 goto free_hash;
1305 }
1306 }
1307
1308 FTRACE_WARN_ON(new_hash->count != hash->count);
1309
1310 return new_hash;
1311
1312 free_hash:
1313 free_ftrace_hash(new_hash);
1314 return NULL;
1315}
1316
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001317static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001318ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001319static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001320ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001321
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001322static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001323ftrace_hash_move(struct ftrace_ops *ops, int enable,
1324 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001325{
1326 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001327 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001329 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001330 int size = src->count;
1331 int bits = 0;
1332 int i;
1333
1334 /*
1335 * If the new source is empty, just free dst and assign it
1336 * the empty_hash.
1337 */
1338 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001339 new_hash = EMPTY_HASH;
1340 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001341 }
1342
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001343 /*
1344 * Make the hash size about 1/2 the # found
1345 */
1346 for (size /= 2; size; size >>= 1)
1347 bits++;
1348
1349 /* Don't allocate too much */
1350 if (bits > FTRACE_HASH_MAX_BITS)
1351 bits = FTRACE_HASH_MAX_BITS;
1352
Steven Rostedt07fd5512011-05-05 18:03:47 -04001353 new_hash = alloc_ftrace_hash(bits);
1354 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001355 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001356
1357 size = 1 << src->size_bits;
1358 for (i = 0; i < size; i++) {
1359 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001360 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001361 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001362 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001363 }
1364 }
1365
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001366update:
1367 /*
1368 * Remove the current set, update the hash and add
1369 * them back.
1370 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001371 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001372
Steven Rostedt07fd5512011-05-05 18:03:47 -04001373 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001374
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001375 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001376
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001377 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001378}
1379
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001380static bool hash_contains_ip(unsigned long ip,
1381 struct ftrace_ops_hash *hash)
1382{
1383 /*
1384 * The function record is a match if it exists in the filter
1385 * hash and not in the notrace hash. Note, an emty hash is
1386 * considered a match for the filter hash, but an empty
1387 * notrace hash is considered not in the notrace hash.
1388 */
1389 return (ftrace_hash_empty(hash->filter_hash) ||
1390 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1391 (ftrace_hash_empty(hash->notrace_hash) ||
1392 !ftrace_lookup_ip(hash->notrace_hash, ip));
1393}
1394
Steven Rostedt265c8312009-02-13 12:43:56 -05001395/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001396 * Test the hashes for this ops to see if we want to call
1397 * the ops->func or not.
1398 *
1399 * It's a match if the ip is in the ops->filter_hash or
1400 * the filter_hash does not exist or is empty,
1401 * AND
1402 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001403 *
1404 * This needs to be called with preemption disabled as
1405 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001406 */
1407static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001408ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001409{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001410 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001411 int ret;
1412
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001413#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1414 /*
1415 * There's a small race when adding ops that the ftrace handler
1416 * that wants regs, may be called without them. We can not
1417 * allow that handler to be called if regs is NULL.
1418 */
1419 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1420 return 0;
1421#endif
1422
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001423 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1424 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001425
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001426 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001427 ret = 1;
1428 else
1429 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001430
1431 return ret;
1432}
1433
1434/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001435 * This is a double for. Do not use 'break' to break out of the loop,
1436 * you must use a goto.
1437 */
1438#define do_for_each_ftrace_rec(pg, rec) \
1439 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1440 int _____i; \
1441 for (_____i = 0; _____i < pg->index; _____i++) { \
1442 rec = &pg->records[_____i];
1443
1444#define while_for_each_ftrace_rec() \
1445 } \
1446 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301447
Steven Rostedt5855fea2011-12-16 19:27:42 -05001448
1449static int ftrace_cmp_recs(const void *a, const void *b)
1450{
Steven Rostedta650e022012-04-25 13:48:13 -04001451 const struct dyn_ftrace *key = a;
1452 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001453
Steven Rostedta650e022012-04-25 13:48:13 -04001454 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001455 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001456 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1457 return 1;
1458 return 0;
1459}
1460
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001461static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001462{
1463 struct ftrace_page *pg;
1464 struct dyn_ftrace *rec;
1465 struct dyn_ftrace key;
1466
1467 key.ip = start;
1468 key.flags = end; /* overload flags, as it is unsigned long */
1469
1470 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1471 if (end < pg->records[0].ip ||
1472 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1473 continue;
1474 rec = bsearch(&key, pg->records, pg->index,
1475 sizeof(struct dyn_ftrace),
1476 ftrace_cmp_recs);
1477 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001478 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001479 }
1480
Steven Rostedt5855fea2011-12-16 19:27:42 -05001481 return 0;
1482}
1483
Steven Rostedtc88fd862011-08-16 09:53:39 -04001484/**
1485 * ftrace_location - return true if the ip giving is a traced location
1486 * @ip: the instruction pointer to check
1487 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001488 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001489 * That is, the instruction that is either a NOP or call to
1490 * the function tracer. It checks the ftrace internal tables to
1491 * determine if the address belongs or not.
1492 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001493unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001494{
Steven Rostedta650e022012-04-25 13:48:13 -04001495 return ftrace_location_range(ip, ip);
1496}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001497
Steven Rostedta650e022012-04-25 13:48:13 -04001498/**
1499 * ftrace_text_reserved - return true if range contains an ftrace location
1500 * @start: start of range to search
1501 * @end: end of range to search (inclusive). @end points to the last byte to check.
1502 *
1503 * Returns 1 if @start and @end contains a ftrace location.
1504 * That is, the instruction that is either a NOP or call to
1505 * the function tracer. It checks the ftrace internal tables to
1506 * determine if the address belongs or not.
1507 */
Sasha Levind88471c2013-01-09 18:09:20 -05001508int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001509{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001510 unsigned long ret;
1511
1512 ret = ftrace_location_range((unsigned long)start,
1513 (unsigned long)end);
1514
1515 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001516}
1517
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001518/* Test if ops registered to this rec needs regs */
1519static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1520{
1521 struct ftrace_ops *ops;
1522 bool keep_regs = false;
1523
1524 for (ops = ftrace_ops_list;
1525 ops != &ftrace_list_end; ops = ops->next) {
1526 /* pass rec in as regs to have non-NULL val */
1527 if (ftrace_ops_test(ops, rec->ip, rec)) {
1528 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1529 keep_regs = true;
1530 break;
1531 }
1532 }
1533 }
1534
1535 return keep_regs;
1536}
1537
Steven Rostedted926f92011-05-03 13:25:24 -04001538static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1539 int filter_hash,
1540 bool inc)
1541{
1542 struct ftrace_hash *hash;
1543 struct ftrace_hash *other_hash;
1544 struct ftrace_page *pg;
1545 struct dyn_ftrace *rec;
1546 int count = 0;
1547 int all = 0;
1548
1549 /* Only update if the ops has been registered */
1550 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1551 return;
1552
1553 /*
1554 * In the filter_hash case:
1555 * If the count is zero, we update all records.
1556 * Otherwise we just update the items in the hash.
1557 *
1558 * In the notrace_hash case:
1559 * We enable the update in the hash.
1560 * As disabling notrace means enabling the tracing,
1561 * and enabling notrace means disabling, the inc variable
1562 * gets inversed.
1563 */
1564 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001565 hash = ops->func_hash->filter_hash;
1566 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001567 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001568 all = 1;
1569 } else {
1570 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001571 hash = ops->func_hash->notrace_hash;
1572 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001573 /*
1574 * If the notrace hash has no items,
1575 * then there's nothing to do.
1576 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001577 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001578 return;
1579 }
1580
1581 do_for_each_ftrace_rec(pg, rec) {
1582 int in_other_hash = 0;
1583 int in_hash = 0;
1584 int match = 0;
1585
1586 if (all) {
1587 /*
1588 * Only the filter_hash affects all records.
1589 * Update if the record is not in the notrace hash.
1590 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001591 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001592 match = 1;
1593 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001594 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1595 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001596
1597 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001598 * If filter_hash is set, we want to match all functions
1599 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001600 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001601 * If filter_hash is not set, then we are decrementing.
1602 * That means we match anything that is in the hash
1603 * and also in the other_hash. That is, we need to turn
1604 * off functions in the other hash because they are disabled
1605 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001606 */
1607 if (filter_hash && in_hash && !in_other_hash)
1608 match = 1;
1609 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001610 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001611 match = 1;
1612 }
1613 if (!match)
1614 continue;
1615
1616 if (inc) {
1617 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001618 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001619 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001620
1621 /*
1622 * If there's only a single callback registered to a
1623 * function, and the ops has a trampoline registered
1624 * for it, then we can call it directly.
1625 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001626 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001627 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001628 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001629 /*
1630 * If we are adding another function callback
1631 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001632 * custom trampoline in use, then we need to go
1633 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001634 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001635 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001636
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001637 /*
1638 * If any ops wants regs saved for this function
1639 * then all ops will get saved regs.
1640 */
1641 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1642 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001643 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001644 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001645 return;
1646 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001647
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001648 /*
1649 * If the rec had REGS enabled and the ops that is
1650 * being removed had REGS set, then see if there is
1651 * still any ops for this record that wants regs.
1652 * If not, we can stop recording them.
1653 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001654 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001655 rec->flags & FTRACE_FL_REGS &&
1656 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1657 if (!test_rec_ops_needs_regs(rec))
1658 rec->flags &= ~FTRACE_FL_REGS;
1659 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001660
1661 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001662 * If the rec had TRAMP enabled, then it needs to
1663 * be cleared. As TRAMP can only be enabled iff
1664 * there is only a single ops attached to it.
1665 * In otherwords, always disable it on decrementing.
1666 * In the future, we may set it if rec count is
1667 * decremented to one, and the ops that is left
1668 * has a trampoline.
1669 */
1670 rec->flags &= ~FTRACE_FL_TRAMP;
1671
1672 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001673 * flags will be cleared in ftrace_check_record()
1674 * if rec count is zero.
1675 */
Steven Rostedted926f92011-05-03 13:25:24 -04001676 }
1677 count++;
1678 /* Shortcut, if we handled all records, we are done. */
1679 if (!all && count == hash->count)
1680 return;
1681 } while_for_each_ftrace_rec();
1682}
1683
1684static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1685 int filter_hash)
1686{
1687 __ftrace_hash_rec_update(ops, filter_hash, 0);
1688}
1689
1690static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1691 int filter_hash)
1692{
1693 __ftrace_hash_rec_update(ops, filter_hash, 1);
1694}
1695
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001696static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1697 int filter_hash, int inc)
1698{
1699 struct ftrace_ops *op;
1700
1701 __ftrace_hash_rec_update(ops, filter_hash, inc);
1702
1703 if (ops->func_hash != &global_ops.local_hash)
1704 return;
1705
1706 /*
1707 * If the ops shares the global_ops hash, then we need to update
1708 * all ops that are enabled and use this hash.
1709 */
1710 do_for_each_ftrace_op(op, ftrace_ops_list) {
1711 /* Already done */
1712 if (op == ops)
1713 continue;
1714 if (op->func_hash == &global_ops.local_hash)
1715 __ftrace_hash_rec_update(op, filter_hash, inc);
1716 } while_for_each_ftrace_op(op);
1717}
1718
1719static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1720 int filter_hash)
1721{
1722 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1723}
1724
1725static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1726 int filter_hash)
1727{
1728 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1729}
1730
Steven Rostedt05736a42008-09-22 14:55:47 -07001731static void print_ip_ins(const char *fmt, unsigned char *p)
1732{
1733 int i;
1734
1735 printk(KERN_CONT "%s", fmt);
1736
1737 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1738 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1739}
1740
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001741static struct ftrace_ops *
1742ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1743
Steven Rostedtc88fd862011-08-16 09:53:39 -04001744/**
1745 * ftrace_bug - report and shutdown function tracer
1746 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001747 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001748 *
1749 * The arch code that enables or disables the function tracing
1750 * can call ftrace_bug() when it has detected a problem in
1751 * modifying the code. @failed should be one of either:
1752 * EFAULT - if the problem happens on reading the @ip address
1753 * EINVAL - if what is read at @ip is not what was expected
1754 * EPERM - if the problem happens on writting to the @ip address
1755 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001756void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001757{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001758 unsigned long ip = rec ? rec->ip : 0;
1759
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001760 switch (failed) {
1761 case -EFAULT:
1762 FTRACE_WARN_ON_ONCE(1);
1763 pr_info("ftrace faulted on modifying ");
1764 print_ip_sym(ip);
1765 break;
1766 case -EINVAL:
1767 FTRACE_WARN_ON_ONCE(1);
1768 pr_info("ftrace failed to modify ");
1769 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001770 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001771 pr_cont("\n");
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001772 break;
1773 case -EPERM:
1774 FTRACE_WARN_ON_ONCE(1);
1775 pr_info("ftrace faulted on writing ");
1776 print_ip_sym(ip);
1777 break;
1778 default:
1779 FTRACE_WARN_ON_ONCE(1);
1780 pr_info("ftrace faulted on unknown error ");
1781 print_ip_sym(ip);
1782 }
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001783 if (rec) {
1784 struct ftrace_ops *ops = NULL;
1785
1786 pr_info("ftrace record flags: %lx\n", rec->flags);
1787 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
1788 rec->flags & FTRACE_FL_REGS ? " R" : " ");
1789 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1790 ops = ftrace_find_tramp_ops_any(rec);
1791 if (ops)
1792 pr_cont("\ttramp: %pS",
1793 (void *)ops->trampoline);
1794 else
1795 pr_cont("\ttramp: ERROR!");
1796
1797 }
1798 ip = ftrace_get_addr_curr(rec);
1799 pr_cont(" expected tramp: %lx\n", ip);
1800 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001801}
1802
Steven Rostedtc88fd862011-08-16 09:53:39 -04001803static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001804{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001805 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001806
Steven Rostedt982c3502008-11-15 16:31:41 -05001807 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001808 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001809 *
Steven Rostedted926f92011-05-03 13:25:24 -04001810 * If the record has a ref count, then we need to enable it
1811 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001812 *
Steven Rostedted926f92011-05-03 13:25:24 -04001813 * Otherwise we make sure its disabled.
1814 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001815 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001816 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001817 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001818 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001819 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001820
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001821 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001822 * If enabling and the REGS flag does not match the REGS_EN, or
1823 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1824 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001825 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001826 if (flag) {
1827 if (!(rec->flags & FTRACE_FL_REGS) !=
1828 !(rec->flags & FTRACE_FL_REGS_EN))
1829 flag |= FTRACE_FL_REGS;
1830
1831 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1832 !(rec->flags & FTRACE_FL_TRAMP_EN))
1833 flag |= FTRACE_FL_TRAMP;
1834 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001835
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001836 /* If the state of this record hasn't changed, then do nothing */
1837 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001838 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001839
1840 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001841 /* Save off if rec is being enabled (for return value) */
1842 flag ^= rec->flags & FTRACE_FL_ENABLED;
1843
1844 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001845 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001846 if (flag & FTRACE_FL_REGS) {
1847 if (rec->flags & FTRACE_FL_REGS)
1848 rec->flags |= FTRACE_FL_REGS_EN;
1849 else
1850 rec->flags &= ~FTRACE_FL_REGS_EN;
1851 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001852 if (flag & FTRACE_FL_TRAMP) {
1853 if (rec->flags & FTRACE_FL_TRAMP)
1854 rec->flags |= FTRACE_FL_TRAMP_EN;
1855 else
1856 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1857 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001858 }
1859
1860 /*
1861 * If this record is being updated from a nop, then
1862 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001863 * Otherwise,
1864 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001865 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001866 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001867 */
1868 if (flag & FTRACE_FL_ENABLED)
1869 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001870
1871 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001872 }
1873
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001874 if (update) {
1875 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001876 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001877 rec->flags = 0;
1878 else
1879 /* Just disable the record (keep REGS state) */
1880 rec->flags &= ~FTRACE_FL_ENABLED;
1881 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001882
1883 return FTRACE_UPDATE_MAKE_NOP;
1884}
1885
1886/**
1887 * ftrace_update_record, set a record that now is tracing or not
1888 * @rec: the record to update
1889 * @enable: set to 1 if the record is tracing, zero to force disable
1890 *
1891 * The records that represent all functions that can be traced need
1892 * to be updated when tracing has been enabled.
1893 */
1894int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1895{
1896 return ftrace_check_record(rec, enable, 1);
1897}
1898
1899/**
1900 * ftrace_test_record, check if the record has been enabled or not
1901 * @rec: the record to test
1902 * @enable: set to 1 to check if enabled, 0 if it is disabled
1903 *
1904 * The arch code may need to test if a record is already set to
1905 * tracing to determine how to modify the function code that it
1906 * represents.
1907 */
1908int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1909{
1910 return ftrace_check_record(rec, enable, 0);
1911}
1912
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001913static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001914ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
1915{
1916 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001917 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001918
1919 do_for_each_ftrace_op(op, ftrace_ops_list) {
1920
1921 if (!op->trampoline)
1922 continue;
1923
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001924 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001925 return op;
1926 } while_for_each_ftrace_op(op);
1927
1928 return NULL;
1929}
1930
1931static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001932ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1933{
1934 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001935 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001936
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001937 /*
1938 * Need to check removed ops first.
1939 * If they are being removed, and this rec has a tramp,
1940 * and this rec is in the ops list, then it would be the
1941 * one with the tramp.
1942 */
1943 if (removed_ops) {
1944 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001945 return removed_ops;
1946 }
1947
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001948 /*
1949 * Need to find the current trampoline for a rec.
1950 * Now, a trampoline is only attached to a rec if there
1951 * was a single 'ops' attached to it. But this can be called
1952 * when we are adding another op to the rec or removing the
1953 * current one. Thus, if the op is being added, we can
1954 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04001955 * yet.
1956 *
1957 * If an ops is being modified (hooking to different functions)
1958 * then we don't care about the new functions that are being
1959 * added, just the old ones (that are probably being removed).
1960 *
1961 * If we are adding an ops to a function that already is using
1962 * a trampoline, it needs to be removed (trampolines are only
1963 * for single ops connected), then an ops that is not being
1964 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001965 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001966 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001967
1968 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001969 continue;
1970
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001971 /*
1972 * If the ops is being added, it hasn't gotten to
1973 * the point to be removed from this tree yet.
1974 */
1975 if (op->flags & FTRACE_OPS_FL_ADDING)
1976 continue;
1977
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001978
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04001979 /*
1980 * If the ops is being modified and is in the old
1981 * hash, then it is probably being removed from this
1982 * function.
1983 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001984 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
1985 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001986 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04001987 /*
1988 * If the ops is not being added or modified, and it's
1989 * in its normal filter hash, then this must be the one
1990 * we want!
1991 */
1992 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
1993 hash_contains_ip(ip, op->func_hash))
1994 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001995
1996 } while_for_each_ftrace_op(op);
1997
1998 return NULL;
1999}
2000
2001static struct ftrace_ops *
2002ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2003{
2004 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002005 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002006
2007 do_for_each_ftrace_op(op, ftrace_ops_list) {
2008 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002009 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002010 return op;
2011 } while_for_each_ftrace_op(op);
2012
2013 return NULL;
2014}
2015
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002016/**
2017 * ftrace_get_addr_new - Get the call address to set to
2018 * @rec: The ftrace record descriptor
2019 *
2020 * If the record has the FTRACE_FL_REGS set, that means that it
2021 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2022 * is not not set, then it wants to convert to the normal callback.
2023 *
2024 * Returns the address of the trampoline to set to
2025 */
2026unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2027{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002028 struct ftrace_ops *ops;
2029
2030 /* Trampolines take precedence over regs */
2031 if (rec->flags & FTRACE_FL_TRAMP) {
2032 ops = ftrace_find_tramp_ops_new(rec);
2033 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002034 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2035 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002036 /* Ftrace is shutting down, return anything */
2037 return (unsigned long)FTRACE_ADDR;
2038 }
2039 return ops->trampoline;
2040 }
2041
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002042 if (rec->flags & FTRACE_FL_REGS)
2043 return (unsigned long)FTRACE_REGS_ADDR;
2044 else
2045 return (unsigned long)FTRACE_ADDR;
2046}
2047
2048/**
2049 * ftrace_get_addr_curr - Get the call address that is already there
2050 * @rec: The ftrace record descriptor
2051 *
2052 * The FTRACE_FL_REGS_EN is set when the record already points to
2053 * a function that saves all the regs. Basically the '_EN' version
2054 * represents the current state of the function.
2055 *
2056 * Returns the address of the trampoline that is currently being called
2057 */
2058unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2059{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002060 struct ftrace_ops *ops;
2061
2062 /* Trampolines take precedence over regs */
2063 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2064 ops = ftrace_find_tramp_ops_curr(rec);
2065 if (FTRACE_WARN_ON(!ops)) {
2066 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2067 (void *)rec->ip, (void *)rec->ip);
2068 /* Ftrace is shutting down, return anything */
2069 return (unsigned long)FTRACE_ADDR;
2070 }
2071 return ops->trampoline;
2072 }
2073
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002074 if (rec->flags & FTRACE_FL_REGS_EN)
2075 return (unsigned long)FTRACE_REGS_ADDR;
2076 else
2077 return (unsigned long)FTRACE_ADDR;
2078}
2079
Steven Rostedtc88fd862011-08-16 09:53:39 -04002080static int
2081__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2082{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002083 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002084 unsigned long ftrace_addr;
2085 int ret;
2086
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002087 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002088
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002089 /* This needs to be done before we call ftrace_update_record */
2090 ftrace_old_addr = ftrace_get_addr_curr(rec);
2091
2092 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002093
Steven Rostedtc88fd862011-08-16 09:53:39 -04002094 switch (ret) {
2095 case FTRACE_UPDATE_IGNORE:
2096 return 0;
2097
2098 case FTRACE_UPDATE_MAKE_CALL:
2099 return ftrace_make_call(rec, ftrace_addr);
2100
2101 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002102 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002103
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002104 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002105 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002106 }
2107
2108 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002109}
2110
Steven Rostedte4f5d542012-04-27 09:13:18 -04002111void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002112{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002113 struct dyn_ftrace *rec;
2114 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002115 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002116
Steven Rostedt45a4a232011-04-21 23:16:46 -04002117 if (unlikely(ftrace_disabled))
2118 return;
2119
Steven Rostedt265c8312009-02-13 12:43:56 -05002120 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002121 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002122 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002123 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002124 /* Stop processing */
2125 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002126 }
2127 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002128}
2129
Steven Rostedtc88fd862011-08-16 09:53:39 -04002130struct ftrace_rec_iter {
2131 struct ftrace_page *pg;
2132 int index;
2133};
2134
2135/**
2136 * ftrace_rec_iter_start, start up iterating over traced functions
2137 *
2138 * Returns an iterator handle that is used to iterate over all
2139 * the records that represent address locations where functions
2140 * are traced.
2141 *
2142 * May return NULL if no records are available.
2143 */
2144struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2145{
2146 /*
2147 * We only use a single iterator.
2148 * Protected by the ftrace_lock mutex.
2149 */
2150 static struct ftrace_rec_iter ftrace_rec_iter;
2151 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2152
2153 iter->pg = ftrace_pages_start;
2154 iter->index = 0;
2155
2156 /* Could have empty pages */
2157 while (iter->pg && !iter->pg->index)
2158 iter->pg = iter->pg->next;
2159
2160 if (!iter->pg)
2161 return NULL;
2162
2163 return iter;
2164}
2165
2166/**
2167 * ftrace_rec_iter_next, get the next record to process.
2168 * @iter: The handle to the iterator.
2169 *
2170 * Returns the next iterator after the given iterator @iter.
2171 */
2172struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2173{
2174 iter->index++;
2175
2176 if (iter->index >= iter->pg->index) {
2177 iter->pg = iter->pg->next;
2178 iter->index = 0;
2179
2180 /* Could have empty pages */
2181 while (iter->pg && !iter->pg->index)
2182 iter->pg = iter->pg->next;
2183 }
2184
2185 if (!iter->pg)
2186 return NULL;
2187
2188 return iter;
2189}
2190
2191/**
2192 * ftrace_rec_iter_record, get the record at the iterator location
2193 * @iter: The current iterator location
2194 *
2195 * Returns the record that the current @iter is at.
2196 */
2197struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2198{
2199 return &iter->pg->records[iter->index];
2200}
2201
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302202static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002203ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002204{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002205 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002206
Steven Rostedt45a4a232011-04-21 23:16:46 -04002207 if (unlikely(ftrace_disabled))
2208 return 0;
2209
Shaohua Li25aac9d2009-01-09 11:29:40 +08002210 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002211 if (ret) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002212 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302213 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02002214 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302215 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002216}
2217
Steven Rostedt000ab692009-02-17 13:35:06 -05002218/*
2219 * archs can override this function if they must do something
2220 * before the modifying code is performed.
2221 */
2222int __weak ftrace_arch_code_modify_prepare(void)
2223{
2224 return 0;
2225}
2226
2227/*
2228 * archs can override this function if they must do something
2229 * after the modifying code is performed.
2230 */
2231int __weak ftrace_arch_code_modify_post_process(void)
2232{
2233 return 0;
2234}
2235
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002236void ftrace_modify_all_code(int command)
2237{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002238 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd21067f2014-02-24 17:12:21 +01002239 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002240
2241 /*
2242 * If the ftrace_caller calls a ftrace_ops func directly,
2243 * we need to make sure that it only traces functions it
2244 * expects to trace. When doing the switch of functions,
2245 * we need to update to the ftrace_ops_list_func first
2246 * before the transition between old and new calls are set,
2247 * as the ftrace_ops_list_func will check the ops hashes
2248 * to make sure the ops are having the right functions
2249 * traced.
2250 */
Petr Mladekcd21067f2014-02-24 17:12:21 +01002251 if (update) {
2252 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2253 if (FTRACE_WARN_ON(err))
2254 return;
2255 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002256
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002257 if (command & FTRACE_UPDATE_CALLS)
2258 ftrace_replace_code(1);
2259 else if (command & FTRACE_DISABLE_CALLS)
2260 ftrace_replace_code(0);
2261
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002262 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2263 function_trace_op = set_function_trace_op;
2264 smp_wmb();
2265 /* If irqs are disabled, we are in stop machine */
2266 if (!irqs_disabled())
2267 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd21067f2014-02-24 17:12:21 +01002268 err = ftrace_update_ftrace_func(ftrace_trace_function);
2269 if (FTRACE_WARN_ON(err))
2270 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002271 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002272
2273 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002274 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002275 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd21067f2014-02-24 17:12:21 +01002276 err = ftrace_disable_ftrace_graph_caller();
2277 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002278}
2279
Ingo Molnare309b412008-05-12 21:20:51 +02002280static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002281{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002282 int *command = data;
2283
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002284 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002285
Steven Rostedtc88fd862011-08-16 09:53:39 -04002286 return 0;
2287}
2288
2289/**
2290 * ftrace_run_stop_machine, go back to the stop machine method
2291 * @command: The command to tell ftrace what to do
2292 *
2293 * If an arch needs to fall back to the stop machine method, the
2294 * it can call this function.
2295 */
2296void ftrace_run_stop_machine(int command)
2297{
2298 stop_machine(__ftrace_modify_code, &command, NULL);
2299}
2300
2301/**
2302 * arch_ftrace_update_code, modify the code to trace or not trace
2303 * @command: The command that needs to be done
2304 *
2305 * Archs can override this function if it does not need to
2306 * run stop_machine() to modify code.
2307 */
2308void __weak arch_ftrace_update_code(int command)
2309{
2310 ftrace_run_stop_machine(command);
2311}
2312
2313static void ftrace_run_update_code(int command)
2314{
2315 int ret;
2316
2317 ret = ftrace_arch_code_modify_prepare();
2318 FTRACE_WARN_ON(ret);
2319 if (ret)
2320 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002321
2322 /*
2323 * By default we use stop_machine() to modify the code.
2324 * But archs can do what ever they want as long as it
2325 * is safe. The stop_machine() is the safest, but also
2326 * produces the most overhead.
2327 */
2328 arch_ftrace_update_code(command);
2329
Steven Rostedt000ab692009-02-17 13:35:06 -05002330 ret = ftrace_arch_code_modify_post_process();
2331 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002332}
2333
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002334static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2335 struct ftrace_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002336{
2337 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002338 ops->old_hash.filter_hash = old_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002339 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002340 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002341 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2342}
2343
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002344static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002345static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002346
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002347void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2348{
2349}
2350
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002351static void control_ops_free(struct ftrace_ops *ops)
2352{
2353 free_percpu(ops->disabled);
2354}
2355
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002356static void ftrace_startup_enable(int command)
2357{
2358 if (saved_ftrace_func != ftrace_trace_function) {
2359 saved_ftrace_func = ftrace_trace_function;
2360 command |= FTRACE_UPDATE_TRACE_FUNC;
2361 }
2362
2363 if (!command || !ftrace_enabled)
2364 return;
2365
2366 ftrace_run_update_code(command);
2367}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002368
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002369static void ftrace_startup_all(int command)
2370{
2371 update_all_ops = true;
2372 ftrace_startup_enable(command);
2373 update_all_ops = false;
2374}
2375
Steven Rostedta1cd6172011-05-23 15:24:25 -04002376static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002377{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002378 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002379
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002380 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002381 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002382
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002383 ret = __register_ftrace_function(ops);
2384 if (ret)
2385 return ret;
2386
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002387 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002388 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002389
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002390 /*
2391 * Note that ftrace probes uses this to start up
2392 * and modify functions it will probe. But we still
2393 * set the ADDING flag for modification, as probes
2394 * do not have trampolines. If they add them in the
2395 * future, then the probes will need to distinguish
2396 * between adding and updating probes.
2397 */
2398 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002399
2400 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002401
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002402 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002403
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002404 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2405
Steven Rostedta1cd6172011-05-23 15:24:25 -04002406 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002407}
2408
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002409static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002410{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002411 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002412
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002413 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002414 return -ENODEV;
2415
2416 ret = __unregister_ftrace_function(ops);
2417 if (ret)
2418 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002419
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002420 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002421 /*
2422 * Just warn in case of unbalance, no need to kill ftrace, it's not
2423 * critical but the ftrace_call callers may be never nopped again after
2424 * further ftrace uses.
2425 */
2426 WARN_ON_ONCE(ftrace_start_up < 0);
2427
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002428 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002429
Namhyung Kima737e6d2014-06-12 23:56:12 +09002430 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002431
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002432 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002433
2434 if (saved_ftrace_func != ftrace_trace_function) {
2435 saved_ftrace_func = ftrace_trace_function;
2436 command |= FTRACE_UPDATE_TRACE_FUNC;
2437 }
2438
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002439 if (!command || !ftrace_enabled) {
2440 /*
2441 * If these are control ops, they still need their
2442 * per_cpu field freed. Since, function tracing is
2443 * not currently active, we can just free them
2444 * without synchronizing all CPUs.
2445 */
2446 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2447 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002448 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002449 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002450
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002451 /*
2452 * If the ops uses a trampoline, then it needs to be
2453 * tested first on update.
2454 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002455 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002456 removed_ops = ops;
2457
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002458 /* The trampoline logic checks the old hashes */
2459 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2460 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2461
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002462 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002463
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002464 /*
2465 * If there's no more ops registered with ftrace, run a
2466 * sanity check to make sure all rec flags are cleared.
2467 */
2468 if (ftrace_ops_list == &ftrace_list_end) {
2469 struct ftrace_page *pg;
2470 struct dyn_ftrace *rec;
2471
2472 do_for_each_ftrace_rec(pg, rec) {
2473 if (FTRACE_WARN_ON_ONCE(rec->flags))
2474 pr_warn(" %pS flags:%lx\n",
2475 (void *)rec->ip, rec->flags);
2476 } while_for_each_ftrace_rec();
2477 }
2478
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002479 ops->old_hash.filter_hash = NULL;
2480 ops->old_hash.notrace_hash = NULL;
2481
2482 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002483 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002484
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002485 /*
2486 * Dynamic ops may be freed, we must make sure that all
2487 * callers are done before leaving this function.
2488 * The same goes for freeing the per_cpu data of the control
2489 * ops.
2490 *
2491 * Again, normal synchronize_sched() is not good enough.
2492 * We need to do a hard force of sched synchronization.
2493 * This is because we use preempt_disable() to do RCU, but
2494 * the function tracers can be called where RCU is not watching
2495 * (like before user_exit()). We can not rely on the RCU
2496 * infrastructure to do the synchronization, thus we must do it
2497 * ourselves.
2498 */
2499 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2500 schedule_on_each_cpu(ftrace_sync);
2501
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002502 arch_ftrace_trampoline_free(ops);
2503
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002504 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2505 control_ops_free(ops);
2506 }
2507
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002508 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002509}
2510
Ingo Molnare309b412008-05-12 21:20:51 +02002511static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002512{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002513 if (unlikely(ftrace_disabled))
2514 return;
2515
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002516 /* Force update next time */
2517 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002518 /* ftrace_start_up is true if we want ftrace running */
2519 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002520 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002521}
2522
Ingo Molnare309b412008-05-12 21:20:51 +02002523static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002524{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002525 if (unlikely(ftrace_disabled))
2526 return;
2527
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002528 /* ftrace_start_up is true if ftrace is running */
2529 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002530 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002531}
2532
Steven Rostedt3d083392008-05-12 21:20:42 +02002533static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002534unsigned long ftrace_update_tot_cnt;
2535
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002536static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002537{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002538 /*
2539 * Filter_hash being empty will default to trace module.
2540 * But notrace hash requires a test of individual module functions.
2541 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002542 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2543 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002544}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002545
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002546/*
2547 * Check if the current ops references the record.
2548 *
2549 * If the ops traces all functions, then it was already accounted for.
2550 * If the ops does not trace the current record function, skip it.
2551 * If the ops ignores the function via notrace filter, skip it.
2552 */
2553static inline bool
2554ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2555{
2556 /* If ops isn't enabled, ignore it */
2557 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2558 return 0;
2559
2560 /* If ops traces all mods, we already accounted for it */
2561 if (ops_traces_mod(ops))
2562 return 0;
2563
2564 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002565 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2566 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002567 return 0;
2568
2569 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002570 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002571 return 0;
2572
2573 return 1;
2574}
2575
2576static int referenced_filters(struct dyn_ftrace *rec)
2577{
2578 struct ftrace_ops *ops;
2579 int cnt = 0;
2580
2581 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2582 if (ops_references_rec(ops, rec))
2583 cnt++;
2584 }
2585
2586 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002587}
2588
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002589static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002590{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002591 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002592 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302593 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002594 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002595 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002596 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002597 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002598
2599 /*
2600 * When adding a module, we need to check if tracers are
2601 * currently enabled and if they are set to trace all functions.
2602 * If they are, we need to enable the module functions as well
2603 * as update the reference counts for those function records.
2604 */
2605 if (mod) {
2606 struct ftrace_ops *ops;
2607
2608 for (ops = ftrace_ops_list;
2609 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002610 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2611 if (ops_traces_mod(ops))
2612 ref++;
2613 else
2614 test = true;
2615 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002616 }
2617 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002618
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002619 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002620
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002621 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302622
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002623 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002624 int cnt = ref;
2625
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002626 /* If something went wrong, bail without enabling anything */
2627 if (unlikely(ftrace_disabled))
2628 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002629
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002630 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002631 if (test)
2632 cnt += referenced_filters(p);
2633 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302634
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002635 /*
2636 * Do the initial record conversion from mcount jump
2637 * to the NOP instructions.
2638 */
2639 if (!ftrace_code_disable(mod, p))
2640 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002641
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002642 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002643
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002644 /*
2645 * If the tracing is enabled, go ahead and enable the record.
2646 *
2647 * The reason not to enable the record immediatelly is the
2648 * inherent check of ftrace_make_nop/ftrace_make_call for
2649 * correct previous instructions. Making first the NOP
2650 * conversion puts the module to the correct state, thus
2651 * passing the ftrace_make_call check.
2652 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002653 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002654 int failed = __ftrace_replace_code(p, 1);
2655 if (failed)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002656 ftrace_bug(failed, p);
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002657 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002658 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002659 }
2660
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002661 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002662 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002663 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002664
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002665 return 0;
2666}
2667
Steven Rostedta7900872011-12-16 16:23:44 -05002668static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002669{
Steven Rostedta7900872011-12-16 16:23:44 -05002670 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002671 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002672
Steven Rostedta7900872011-12-16 16:23:44 -05002673 if (WARN_ON(!count))
2674 return -EINVAL;
2675
2676 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002677
2678 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002679 * We want to fill as much as possible. No more than a page
2680 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002681 */
Steven Rostedta7900872011-12-16 16:23:44 -05002682 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2683 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002684
Steven Rostedta7900872011-12-16 16:23:44 -05002685 again:
2686 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2687
2688 if (!pg->records) {
2689 /* if we can't allocate this size, try something smaller */
2690 if (!order)
2691 return -ENOMEM;
2692 order >>= 1;
2693 goto again;
2694 }
2695
2696 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2697 pg->size = cnt;
2698
2699 if (cnt > count)
2700 cnt = count;
2701
2702 return cnt;
2703}
2704
2705static struct ftrace_page *
2706ftrace_allocate_pages(unsigned long num_to_init)
2707{
2708 struct ftrace_page *start_pg;
2709 struct ftrace_page *pg;
2710 int order;
2711 int cnt;
2712
2713 if (!num_to_init)
2714 return 0;
2715
2716 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2717 if (!pg)
2718 return NULL;
2719
2720 /*
2721 * Try to allocate as much as possible in one continues
2722 * location that fills in all of the space. We want to
2723 * waste as little space as possible.
2724 */
2725 for (;;) {
2726 cnt = ftrace_allocate_records(pg, num_to_init);
2727 if (cnt < 0)
2728 goto free_pages;
2729
2730 num_to_init -= cnt;
2731 if (!num_to_init)
2732 break;
2733
2734 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2735 if (!pg->next)
2736 goto free_pages;
2737
2738 pg = pg->next;
2739 }
2740
2741 return start_pg;
2742
2743 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002744 pg = start_pg;
2745 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002746 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2747 free_pages((unsigned long)pg->records, order);
2748 start_pg = pg->next;
2749 kfree(pg);
2750 pg = start_pg;
2751 }
2752 pr_info("ftrace: FAILED to allocate memory for functions\n");
2753 return NULL;
2754}
2755
Steven Rostedt5072c592008-05-12 21:20:43 +02002756#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2757
2758struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002759 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002760 loff_t func_pos;
2761 struct ftrace_page *pg;
2762 struct dyn_ftrace *func;
2763 struct ftrace_func_probe *probe;
2764 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002765 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002766 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002767 int hidx;
2768 int idx;
2769 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002770};
2771
Ingo Molnare309b412008-05-12 21:20:51 +02002772static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002773t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002774{
2775 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002776 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002777 struct hlist_head *hhd;
2778
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002779 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002780 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002781
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002782 if (iter->probe)
2783 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002784 retry:
2785 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2786 return NULL;
2787
2788 hhd = &ftrace_func_hash[iter->hidx];
2789
2790 if (hlist_empty(hhd)) {
2791 iter->hidx++;
2792 hnd = NULL;
2793 goto retry;
2794 }
2795
2796 if (!hnd)
2797 hnd = hhd->first;
2798 else {
2799 hnd = hnd->next;
2800 if (!hnd) {
2801 iter->hidx++;
2802 goto retry;
2803 }
2804 }
2805
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002806 if (WARN_ON_ONCE(!hnd))
2807 return NULL;
2808
2809 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2810
2811 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002812}
2813
2814static void *t_hash_start(struct seq_file *m, loff_t *pos)
2815{
2816 struct ftrace_iterator *iter = m->private;
2817 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002818 loff_t l;
2819
Steven Rostedt69a30832011-12-19 15:21:16 -05002820 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2821 return NULL;
2822
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002823 if (iter->func_pos > *pos)
2824 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002825
Li Zefand82d6242009-06-24 09:54:54 +08002826 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002827 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002828 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002829 if (!p)
2830 break;
2831 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002832 if (!p)
2833 return NULL;
2834
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002835 /* Only set this if we have an item */
2836 iter->flags |= FTRACE_ITER_HASH;
2837
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002838 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002839}
2840
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002841static int
2842t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002843{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002844 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002845
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002846 rec = iter->probe;
2847 if (WARN_ON_ONCE(!rec))
2848 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002849
Steven Rostedt809dcf22009-02-16 23:06:01 -05002850 if (rec->ops->print)
2851 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2852
Steven Rostedtb375a112009-09-17 00:05:58 -04002853 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002854
2855 if (rec->data)
2856 seq_printf(m, ":%p", rec->data);
2857 seq_putc(m, '\n');
2858
2859 return 0;
2860}
2861
2862static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002863t_next(struct seq_file *m, void *v, loff_t *pos)
2864{
2865 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002866 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002867 struct dyn_ftrace *rec = NULL;
2868
Steven Rostedt45a4a232011-04-21 23:16:46 -04002869 if (unlikely(ftrace_disabled))
2870 return NULL;
2871
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002872 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002873 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002874
Steven Rostedt5072c592008-05-12 21:20:43 +02002875 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002876 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002877
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002878 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002879 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002880
Steven Rostedt5072c592008-05-12 21:20:43 +02002881 retry:
2882 if (iter->idx >= iter->pg->index) {
2883 if (iter->pg->next) {
2884 iter->pg = iter->pg->next;
2885 iter->idx = 0;
2886 goto retry;
2887 }
2888 } else {
2889 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002890 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002891 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002892
Steven Rostedt41c52c02008-05-22 11:46:33 -04002893 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002894 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04002895
2896 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002897 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002898
Steven Rostedt5072c592008-05-12 21:20:43 +02002899 rec = NULL;
2900 goto retry;
2901 }
2902 }
2903
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002904 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002905 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002906
2907 iter->func = rec;
2908
2909 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002910}
2911
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002912static void reset_iter_read(struct ftrace_iterator *iter)
2913{
2914 iter->pos = 0;
2915 iter->func_pos = 0;
Dan Carpenter70f77b3f2012-06-09 19:10:27 +03002916 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002917}
2918
2919static void *t_start(struct seq_file *m, loff_t *pos)
2920{
2921 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002922 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002923 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002924 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002925
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002926 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002927
2928 if (unlikely(ftrace_disabled))
2929 return NULL;
2930
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002931 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002932 * If an lseek was done, then reset and start from beginning.
2933 */
2934 if (*pos < iter->pos)
2935 reset_iter_read(iter);
2936
2937 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002938 * For set_ftrace_filter reading, if we have the filter
2939 * off, we can short cut and just print out that all
2940 * functions are enabled.
2941 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002942 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002943 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002944 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002945 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002946 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002947 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002948 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002949 /* reset in case of seek/pread */
2950 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002951 return iter;
2952 }
2953
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002954 if (iter->flags & FTRACE_ITER_HASH)
2955 return t_hash_start(m, pos);
2956
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002957 /*
2958 * Unfortunately, we need to restart at ftrace_pages_start
2959 * every time we let go of the ftrace_mutex. This is because
2960 * those pointers can change without the lock.
2961 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002962 iter->pg = ftrace_pages_start;
2963 iter->idx = 0;
2964 for (l = 0; l <= *pos; ) {
2965 p = t_next(m, p, &l);
2966 if (!p)
2967 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002968 }
walimis5821e1b2008-11-15 15:19:06 +08002969
Steven Rostedt69a30832011-12-19 15:21:16 -05002970 if (!p)
2971 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002972
2973 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002974}
2975
2976static void t_stop(struct seq_file *m, void *p)
2977{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002978 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002979}
2980
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04002981void * __weak
2982arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2983{
2984 return NULL;
2985}
2986
2987static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
2988 struct dyn_ftrace *rec)
2989{
2990 void *ptr;
2991
2992 ptr = arch_ftrace_trampoline_func(ops, rec);
2993 if (ptr)
2994 seq_printf(m, " ->%pS", ptr);
2995}
2996
Steven Rostedt5072c592008-05-12 21:20:43 +02002997static int t_show(struct seq_file *m, void *v)
2998{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002999 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003000 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003001
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003002 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003003 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003004
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003005 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003006 if (iter->flags & FTRACE_ITER_NOTRACE)
3007 seq_printf(m, "#### no functions disabled ####\n");
3008 else
3009 seq_printf(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003010 return 0;
3011 }
3012
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003013 rec = iter->func;
3014
Steven Rostedt5072c592008-05-12 21:20:43 +02003015 if (!rec)
3016 return 0;
3017
Steven Rostedt647bcd02011-05-03 14:39:21 -04003018 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003019 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003020 struct ftrace_ops *ops = NULL;
3021
Steven Rostedt08f6fba2012-04-30 16:20:23 -04003022 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003023 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003024 rec->flags & FTRACE_FL_REGS ? " R" : " ");
3025 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003026 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04003027 if (ops)
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003028 seq_printf(m, "\ttramp: %pS",
3029 (void *)ops->trampoline);
3030 else
3031 seq_printf(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003032
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003033 }
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003034 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003035 }
3036
Steven Rostedt647bcd02011-05-03 14:39:21 -04003037 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02003038
3039 return 0;
3040}
3041
James Morris88e9d342009-09-22 16:43:43 -07003042static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003043 .start = t_start,
3044 .next = t_next,
3045 .stop = t_stop,
3046 .show = t_show,
3047};
3048
Ingo Molnare309b412008-05-12 21:20:51 +02003049static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003050ftrace_avail_open(struct inode *inode, struct file *file)
3051{
3052 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003053
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003054 if (unlikely(ftrace_disabled))
3055 return -ENODEV;
3056
Jiri Olsa50e18b92012-04-25 10:23:39 +02003057 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3058 if (iter) {
3059 iter->pg = ftrace_pages_start;
3060 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003061 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003062
Jiri Olsa50e18b92012-04-25 10:23:39 +02003063 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003064}
3065
Steven Rostedt647bcd02011-05-03 14:39:21 -04003066static int
3067ftrace_enabled_open(struct inode *inode, struct file *file)
3068{
3069 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003070
Jiri Olsa50e18b92012-04-25 10:23:39 +02003071 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3072 if (iter) {
3073 iter->pg = ftrace_pages_start;
3074 iter->flags = FTRACE_ITER_ENABLED;
3075 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003076 }
3077
Jiri Olsa50e18b92012-04-25 10:23:39 +02003078 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003079}
3080
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003081/**
3082 * ftrace_regex_open - initialize function tracer filter files
3083 * @ops: The ftrace_ops that hold the hash filters
3084 * @flag: The type of filter to process
3085 * @inode: The inode, usually passed in to your open routine
3086 * @file: The file, usually passed in to your open routine
3087 *
3088 * ftrace_regex_open() initializes the filter files for the
3089 * @ops. Depending on @flag it may process the filter hash or
3090 * the notrace hash of @ops. With this called from the open
3091 * routine, you can use ftrace_filter_write() for the write
3092 * routine if @flag has FTRACE_ITER_FILTER set, or
3093 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003094 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003095 * release must call ftrace_regex_release().
3096 */
3097int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003098ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003099 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003100{
3101 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003102 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003103 int ret = 0;
3104
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003105 ftrace_ops_init(ops);
3106
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003107 if (unlikely(ftrace_disabled))
3108 return -ENODEV;
3109
Steven Rostedt5072c592008-05-12 21:20:43 +02003110 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3111 if (!iter)
3112 return -ENOMEM;
3113
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003114 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3115 kfree(iter);
3116 return -ENOMEM;
3117 }
3118
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003119 iter->ops = ops;
3120 iter->flags = flag;
3121
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003122 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003123
Steven Rostedtf45948e2011-05-02 12:29:25 -04003124 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003125 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003126 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003127 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003128
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003129 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003130 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3131
3132 if (file->f_flags & O_TRUNC)
3133 iter->hash = alloc_ftrace_hash(size_bits);
3134 else
3135 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3136
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003137 if (!iter->hash) {
3138 trace_parser_put(&iter->parser);
3139 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003140 ret = -ENOMEM;
3141 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003142 }
3143 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003144
Steven Rostedt5072c592008-05-12 21:20:43 +02003145 if (file->f_mode & FMODE_READ) {
3146 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003147
3148 ret = seq_open(file, &show_ftrace_seq_ops);
3149 if (!ret) {
3150 struct seq_file *m = file->private_data;
3151 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003152 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003153 /* Failed */
3154 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003155 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003156 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003157 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003158 } else
3159 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003160
3161 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003162 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003163
3164 return ret;
3165}
3166
Steven Rostedt41c52c02008-05-22 11:46:33 -04003167static int
3168ftrace_filter_open(struct inode *inode, struct file *file)
3169{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003170 struct ftrace_ops *ops = inode->i_private;
3171
3172 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003173 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3174 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003175}
3176
3177static int
3178ftrace_notrace_open(struct inode *inode, struct file *file)
3179{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003180 struct ftrace_ops *ops = inode->i_private;
3181
3182 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003183 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003184}
3185
Steven Rostedt64e7c442009-02-13 17:08:48 -05003186static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003187{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003188 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003189 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003190
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003191 switch (type) {
3192 case MATCH_FULL:
3193 if (strcmp(str, regex) == 0)
3194 matched = 1;
3195 break;
3196 case MATCH_FRONT_ONLY:
3197 if (strncmp(str, regex, len) == 0)
3198 matched = 1;
3199 break;
3200 case MATCH_MIDDLE_ONLY:
3201 if (strstr(str, regex))
3202 matched = 1;
3203 break;
3204 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003205 slen = strlen(str);
3206 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003207 matched = 1;
3208 break;
3209 }
3210
3211 return matched;
3212}
3213
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003214static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003215enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003216{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003217 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003218 int ret = 0;
3219
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003220 entry = ftrace_lookup_ip(hash, rec->ip);
3221 if (not) {
3222 /* Do nothing if it doesn't exist */
3223 if (!entry)
3224 return 0;
3225
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003226 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003227 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003228 /* Do nothing if it exists */
3229 if (entry)
3230 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003231
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003232 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003233 }
3234 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003235}
3236
Steven Rostedt64e7c442009-02-13 17:08:48 -05003237static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003238ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3239 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003240{
3241 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003242 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003243
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003244 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3245
3246 if (mod) {
3247 /* module lookup requires matching the module */
3248 if (!modname || strcmp(modname, mod))
3249 return 0;
3250
3251 /* blank search means to match all funcs in the mod */
3252 if (!len)
3253 return 1;
3254 }
3255
Steven Rostedt64e7c442009-02-13 17:08:48 -05003256 return ftrace_match(str, regex, len, type);
3257}
3258
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003259static int
3260match_records(struct ftrace_hash *hash, char *buff,
3261 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003262{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003263 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003264 struct ftrace_page *pg;
3265 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003266 int type = MATCH_FULL;
3267 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003268 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003269 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003270
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003271 if (len) {
3272 type = filter_parse_regex(buff, len, &search, &not);
3273 search_len = strlen(search);
3274 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003275
Steven Rostedt52baf112009-02-14 01:15:39 -05003276 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003277
3278 if (unlikely(ftrace_disabled))
3279 goto out_unlock;
3280
Steven Rostedt265c8312009-02-13 12:43:56 -05003281 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003282 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003283 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003284 if (ret < 0) {
3285 found = ret;
3286 goto out_unlock;
3287 }
Li Zefan311d16d2009-12-08 11:15:11 +08003288 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003289 }
3290 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003291 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003292 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003293
3294 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003295}
3296
Steven Rostedt64e7c442009-02-13 17:08:48 -05003297static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003298ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003299{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003300 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003301}
3302
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003303static int
3304ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003305{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003306 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003307
Steven Rostedt64e7c442009-02-13 17:08:48 -05003308 /* blank or '*' mean the same */
3309 if (strcmp(buff, "*") == 0)
3310 buff[0] = 0;
3311
3312 /* handle the case of 'dont filter this module' */
3313 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3314 buff[0] = 0;
3315 not = 1;
3316 }
3317
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003318 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003319}
3320
Steven Rostedtf6180772009-02-14 00:40:25 -05003321/*
3322 * We register the module command as a template to show others how
3323 * to register the a command as well.
3324 */
3325
3326static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003327ftrace_mod_callback(struct ftrace_hash *hash,
3328 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003329{
3330 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003331 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003332
3333 /*
3334 * cmd == 'mod' because we only registered this func
3335 * for the 'mod' ftrace_func_command.
3336 * But if you register one func with multiple commands,
3337 * you can tell which command was used by the cmd
3338 * parameter.
3339 */
3340
3341 /* we must have a module name */
3342 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003343 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003344
3345 mod = strsep(&param, ":");
3346 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003347 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003348
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003349 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003350 if (!ret)
3351 ret = -EINVAL;
3352 if (ret < 0)
3353 return ret;
3354
3355 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003356}
3357
3358static struct ftrace_func_command ftrace_mod_cmd = {
3359 .name = "mod",
3360 .func = ftrace_mod_callback,
3361};
3362
3363static int __init ftrace_mod_cmd_init(void)
3364{
3365 return register_ftrace_command(&ftrace_mod_cmd);
3366}
Steven Rostedt6f415672012-10-05 12:13:07 -04003367core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003368
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003369static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003370 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003371{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003372 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003373 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003374 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003375
3376 key = hash_long(ip, FTRACE_HASH_BITS);
3377
3378 hhd = &ftrace_func_hash[key];
3379
3380 if (hlist_empty(hhd))
3381 return;
3382
3383 /*
3384 * Disable preemption for these calls to prevent a RCU grace
3385 * period. This syncs the hash iteration and freeing of items
3386 * on the hash. rcu_read_lock is too dangerous here.
3387 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003388 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003389 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003390 if (entry->ip == ip)
3391 entry->ops->func(ip, parent_ip, &entry->data);
3392 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003393 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003394}
3395
Steven Rostedtb6887d72009-02-17 12:32:04 -05003396static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003397{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003398 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003399 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003400 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003401};
3402
Steven Rostedtb6887d72009-02-17 12:32:04 -05003403static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003404
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003405static void __enable_ftrace_function_probe(struct ftrace_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003406{
Steven Rostedtb8489142011-05-04 09:27:52 -04003407 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003408 int i;
3409
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003410 if (ftrace_probe_registered) {
3411 /* still need to update the function call sites */
3412 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003413 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3414 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003415 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003416 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003417
3418 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3419 struct hlist_head *hhd = &ftrace_func_hash[i];
3420 if (hhd->first)
3421 break;
3422 }
3423 /* Nothing registered? */
3424 if (i == FTRACE_FUNC_HASHSIZE)
3425 return;
3426
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003427 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003428
Steven Rostedtb6887d72009-02-17 12:32:04 -05003429 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003430}
3431
Steven Rostedtb6887d72009-02-17 12:32:04 -05003432static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003433{
3434 int i;
3435
Steven Rostedtb6887d72009-02-17 12:32:04 -05003436 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003437 return;
3438
3439 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3440 struct hlist_head *hhd = &ftrace_func_hash[i];
3441 if (hhd->first)
3442 return;
3443 }
3444
3445 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003446 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003447
Steven Rostedtb6887d72009-02-17 12:32:04 -05003448 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003449}
3450
3451
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003452static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003453{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003454 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003455 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003456 kfree(entry);
3457}
3458
Steven Rostedt59df055f2009-02-14 15:29:06 -05003459int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003460register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003461 void *data)
3462{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003463 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003464 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003465 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003466 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003467 struct ftrace_page *pg;
3468 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003469 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003470 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003471 int count = 0;
3472 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003473 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003474
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003475 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003476 len = strlen(search);
3477
Steven Rostedtb6887d72009-02-17 12:32:04 -05003478 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003479 if (WARN_ON(not))
3480 return -EINVAL;
3481
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003482 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003483
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003484 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003485 if (!hash) {
3486 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003487 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003488 }
3489
3490 if (unlikely(ftrace_disabled)) {
3491 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003492 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003493 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003494
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003495 mutex_lock(&ftrace_lock);
3496
Steven Rostedt59df055f2009-02-14 15:29:06 -05003497 do_for_each_ftrace_rec(pg, rec) {
3498
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003499 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003500 continue;
3501
3502 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3503 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003504 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003505 if (!count)
3506 count = -ENOMEM;
3507 goto out_unlock;
3508 }
3509
3510 count++;
3511
3512 entry->data = data;
3513
3514 /*
3515 * The caller might want to do something special
3516 * for each function we find. We call the callback
3517 * to give the caller an opportunity to do so.
3518 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003519 if (ops->init) {
3520 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003521 /* caller does not like this func */
3522 kfree(entry);
3523 continue;
3524 }
3525 }
3526
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003527 ret = enter_record(hash, rec, 0);
3528 if (ret < 0) {
3529 kfree(entry);
3530 count = ret;
3531 goto out_unlock;
3532 }
3533
Steven Rostedt59df055f2009-02-14 15:29:06 -05003534 entry->ops = ops;
3535 entry->ip = rec->ip;
3536
3537 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3538 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3539
3540 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003541
3542 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003543
3544 __enable_ftrace_function_probe(old_hash);
3545
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003546 if (!ret)
3547 free_ftrace_hash_rcu(old_hash);
3548 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003549 count = ret;
3550
Steven Rostedt59df055f2009-02-14 15:29:06 -05003551 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003552 mutex_unlock(&ftrace_lock);
3553 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003554 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003555 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003556
3557 return count;
3558}
3559
3560enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003561 PROBE_TEST_FUNC = 1,
3562 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003563};
3564
3565static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003566__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003567 void *data, int flags)
3568{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003569 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003570 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003571 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003572 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003573 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003574 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003575 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003576 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003577 char str[KSYM_SYMBOL_LEN];
3578 int type = MATCH_FULL;
3579 int i, len = 0;
3580 char *search;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003581 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003582
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003583 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003584 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003585 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003586 int not;
3587
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003588 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003589 len = strlen(search);
3590
Steven Rostedtb6887d72009-02-17 12:32:04 -05003591 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003592 if (WARN_ON(not))
3593 return;
3594 }
3595
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003596 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003597
3598 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3599 if (!hash)
3600 /* Hmm, should report this somehow */
3601 goto out_unlock;
3602
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003603 INIT_LIST_HEAD(&free_list);
3604
Steven Rostedt59df055f2009-02-14 15:29:06 -05003605 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3606 struct hlist_head *hhd = &ftrace_func_hash[i];
3607
Sasha Levinb67bfe02013-02-27 17:06:00 -08003608 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003609
3610 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003611 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003612 continue;
3613
Steven Rostedtb6887d72009-02-17 12:32:04 -05003614 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003615 continue;
3616
3617 /* do this last, since it is the most expensive */
3618 if (glob) {
3619 kallsyms_lookup(entry->ip, NULL, NULL,
3620 NULL, str);
3621 if (!ftrace_match(str, glob, len, type))
3622 continue;
3623 }
3624
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003625 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3626 /* It is possible more than one entry had this ip */
3627 if (rec_entry)
3628 free_hash_entry(hash, rec_entry);
3629
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003630 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003631 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003632 }
3633 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003634 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003635 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003636 /*
3637 * Remove after the disable is called. Otherwise, if the last
3638 * probe is removed, a null hash means *all enabled*.
3639 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003640 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003641 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003642 if (!ret)
3643 free_ftrace_hash_rcu(old_hash);
3644
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003645 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3646 list_del(&entry->free_list);
3647 ftrace_free_entry(entry);
3648 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003649 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003650
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003651 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003652 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003653 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003654}
3655
3656void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003657unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003658 void *data)
3659{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003660 __unregister_ftrace_function_probe(glob, ops, data,
3661 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003662}
3663
3664void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003665unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003666{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003667 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003668}
3669
Steven Rostedtb6887d72009-02-17 12:32:04 -05003670void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003671{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003672 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003673}
3674
Steven Rostedtf6180772009-02-14 00:40:25 -05003675static LIST_HEAD(ftrace_commands);
3676static DEFINE_MUTEX(ftrace_cmd_mutex);
3677
Tom Zanussi38de93a2013-10-24 08:34:18 -05003678/*
3679 * Currently we only register ftrace commands from __init, so mark this
3680 * __init too.
3681 */
3682__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003683{
3684 struct ftrace_func_command *p;
3685 int ret = 0;
3686
3687 mutex_lock(&ftrace_cmd_mutex);
3688 list_for_each_entry(p, &ftrace_commands, list) {
3689 if (strcmp(cmd->name, p->name) == 0) {
3690 ret = -EBUSY;
3691 goto out_unlock;
3692 }
3693 }
3694 list_add(&cmd->list, &ftrace_commands);
3695 out_unlock:
3696 mutex_unlock(&ftrace_cmd_mutex);
3697
3698 return ret;
3699}
3700
Tom Zanussi38de93a2013-10-24 08:34:18 -05003701/*
3702 * Currently we only unregister ftrace commands from __init, so mark
3703 * this __init too.
3704 */
3705__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003706{
3707 struct ftrace_func_command *p, *n;
3708 int ret = -ENODEV;
3709
3710 mutex_lock(&ftrace_cmd_mutex);
3711 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3712 if (strcmp(cmd->name, p->name) == 0) {
3713 ret = 0;
3714 list_del_init(&p->list);
3715 goto out_unlock;
3716 }
3717 }
3718 out_unlock:
3719 mutex_unlock(&ftrace_cmd_mutex);
3720
3721 return ret;
3722}
3723
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003724static int ftrace_process_regex(struct ftrace_hash *hash,
3725 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003726{
Steven Rostedtf6180772009-02-14 00:40:25 -05003727 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003728 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003729 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003730
3731 func = strsep(&next, ":");
3732
3733 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003734 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003735 if (!ret)
3736 ret = -EINVAL;
3737 if (ret < 0)
3738 return ret;
3739 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003740 }
3741
Steven Rostedtf6180772009-02-14 00:40:25 -05003742 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003743
3744 command = strsep(&next, ":");
3745
Steven Rostedtf6180772009-02-14 00:40:25 -05003746 mutex_lock(&ftrace_cmd_mutex);
3747 list_for_each_entry(p, &ftrace_commands, list) {
3748 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003749 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003750 goto out_unlock;
3751 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003752 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003753 out_unlock:
3754 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003755
Steven Rostedtf6180772009-02-14 00:40:25 -05003756 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003757}
3758
Ingo Molnare309b412008-05-12 21:20:51 +02003759static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003760ftrace_regex_write(struct file *file, const char __user *ubuf,
3761 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003762{
3763 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003764 struct trace_parser *parser;
3765 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003766
Li Zefan4ba79782009-09-22 13:52:20 +08003767 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003768 return 0;
3769
Steven Rostedt5072c592008-05-12 21:20:43 +02003770 if (file->f_mode & FMODE_READ) {
3771 struct seq_file *m = file->private_data;
3772 iter = m->private;
3773 } else
3774 iter = file->private_data;
3775
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003776 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003777 return -ENODEV;
3778
3779 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003780
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003781 parser = &iter->parser;
3782 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003783
Li Zefan4ba79782009-09-22 13:52:20 +08003784 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003785 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003786 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003787 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003788 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003789 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003790 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003791 }
3792
Steven Rostedt5072c592008-05-12 21:20:43 +02003793 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003794 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003795 return ret;
3796}
3797
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003798ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003799ftrace_filter_write(struct file *file, const char __user *ubuf,
3800 size_t cnt, loff_t *ppos)
3801{
3802 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3803}
3804
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003805ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003806ftrace_notrace_write(struct file *file, const char __user *ubuf,
3807 size_t cnt, loff_t *ppos)
3808{
3809 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3810}
3811
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003812static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003813ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3814{
3815 struct ftrace_func_entry *entry;
3816
3817 if (!ftrace_location(ip))
3818 return -EINVAL;
3819
3820 if (remove) {
3821 entry = ftrace_lookup_ip(hash, ip);
3822 if (!entry)
3823 return -ENOENT;
3824 free_hash_entry(hash, entry);
3825 return 0;
3826 }
3827
3828 return add_hash_entry(hash, ip);
3829}
3830
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003831static void ftrace_ops_update_code(struct ftrace_ops *ops,
3832 struct ftrace_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003833{
3834 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003835 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003836}
3837
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003838static int
3839ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3840 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003841{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003842 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003843 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003844 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003845 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003846
Steven Rostedt41c52c02008-05-22 11:46:33 -04003847 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003848 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003849
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003850 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003851
Steven Rostedtf45948e2011-05-02 12:29:25 -04003852 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003853 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003854 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003855 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003856
Wang Nanb972cc52014-07-15 08:40:20 +08003857 if (reset)
3858 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3859 else
3860 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3861
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003862 if (!hash) {
3863 ret = -ENOMEM;
3864 goto out_regex_unlock;
3865 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003866
Jiri Olsaac483c42012-01-02 10:04:14 +01003867 if (buf && !ftrace_match_records(hash, buf, len)) {
3868 ret = -EINVAL;
3869 goto out_regex_unlock;
3870 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003871 if (ip) {
3872 ret = ftrace_match_addr(hash, ip, remove);
3873 if (ret < 0)
3874 goto out_regex_unlock;
3875 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003876
3877 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003878 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003879 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003880 if (!ret) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003881 ftrace_ops_update_code(ops, old_hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003882 free_ftrace_hash_rcu(old_hash);
3883 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003884 mutex_unlock(&ftrace_lock);
3885
Jiri Olsaac483c42012-01-02 10:04:14 +01003886 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003887 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003888
3889 free_ftrace_hash(hash);
3890 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003891}
3892
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003893static int
3894ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3895 int reset, int enable)
3896{
3897 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3898}
3899
3900/**
3901 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3902 * @ops - the ops to set the filter with
3903 * @ip - the address to add to or remove from the filter.
3904 * @remove - non zero to remove the ip from the filter
3905 * @reset - non zero to reset all filters before applying this filter.
3906 *
3907 * Filters denote which functions should be enabled when tracing is enabled
3908 * If @ip is NULL, it failes to update filter.
3909 */
3910int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3911 int remove, int reset)
3912{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003913 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003914 return ftrace_set_addr(ops, ip, remove, reset, 1);
3915}
3916EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3917
3918static int
3919ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3920 int reset, int enable)
3921{
3922 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3923}
3924
Steven Rostedt77a2b372008-05-12 21:20:45 +02003925/**
3926 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003927 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003928 * @buf - the string that holds the function filter text.
3929 * @len - the length of the string.
3930 * @reset - non zero to reset all filters before applying this filter.
3931 *
3932 * Filters denote which functions should be enabled when tracing is enabled.
3933 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3934 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003935int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003936 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003937{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003938 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003939 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003940}
Steven Rostedt936e0742011-05-05 22:54:01 -04003941EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003942
Steven Rostedt41c52c02008-05-22 11:46:33 -04003943/**
3944 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003945 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003946 * @buf - the string that holds the function notrace text.
3947 * @len - the length of the string.
3948 * @reset - non zero to reset all filters before applying this filter.
3949 *
3950 * Notrace Filters denote which functions should not be enabled when tracing
3951 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3952 * for tracing.
3953 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003954int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003955 int len, int reset)
3956{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003957 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003958 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003959}
3960EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3961/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003962 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003963 * @buf - the string that holds the function filter text.
3964 * @len - the length of the string.
3965 * @reset - non zero to reset all filters before applying this filter.
3966 *
3967 * Filters denote which functions should be enabled when tracing is enabled.
3968 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3969 */
3970void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3971{
3972 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3973}
3974EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3975
3976/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003977 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003978 * @buf - the string that holds the function notrace text.
3979 * @len - the length of the string.
3980 * @reset - non zero to reset all filters before applying this filter.
3981 *
3982 * Notrace Filters denote which functions should not be enabled when tracing
3983 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3984 * for tracing.
3985 */
3986void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003987{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003988 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003989}
Steven Rostedt936e0742011-05-05 22:54:01 -04003990EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003991
Steven Rostedt2af15d62009-05-28 13:37:24 -04003992/*
3993 * command line interface to allow users to set filters on boot up.
3994 */
3995#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3996static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3997static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3998
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003999/* Used by function selftest to not test if filter is set */
4000bool ftrace_filter_param __initdata;
4001
Steven Rostedt2af15d62009-05-28 13:37:24 -04004002static int __init set_ftrace_notrace(char *str)
4003{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004004 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004005 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004006 return 1;
4007}
4008__setup("ftrace_notrace=", set_ftrace_notrace);
4009
4010static int __init set_ftrace_filter(char *str)
4011{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004012 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004013 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004014 return 1;
4015}
4016__setup("ftrace_filter=", set_ftrace_filter);
4017
Stefan Assmann369bc182009-10-12 22:17:21 +02004018#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004019static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004020static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004021static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004022
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004023static unsigned long save_global_trampoline;
4024static unsigned long save_global_flags;
4025
Stefan Assmann369bc182009-10-12 22:17:21 +02004026static int __init set_graph_function(char *str)
4027{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004028 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004029 return 1;
4030}
4031__setup("ftrace_graph_filter=", set_graph_function);
4032
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004033static int __init set_graph_notrace_function(char *str)
4034{
4035 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4036 return 1;
4037}
4038__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4039
4040static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004041{
4042 int ret;
4043 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004044 unsigned long *table = ftrace_graph_funcs;
4045 int *count = &ftrace_graph_count;
4046
4047 if (!enable) {
4048 table = ftrace_graph_notrace_funcs;
4049 count = &ftrace_graph_notrace_count;
4050 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004051
4052 while (buf) {
4053 func = strsep(&buf, ",");
4054 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004055 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004056 if (ret)
4057 printk(KERN_DEBUG "ftrace: function %s not "
4058 "traceable\n", func);
4059 }
4060}
4061#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4062
Steven Rostedt2a85a372011-12-19 21:57:44 -05004063void __init
4064ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004065{
4066 char *func;
4067
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004068 ftrace_ops_init(ops);
4069
Steven Rostedt2af15d62009-05-28 13:37:24 -04004070 while (buf) {
4071 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004072 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004073 }
4074}
4075
4076static void __init set_ftrace_early_filters(void)
4077{
4078 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004079 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004080 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004081 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004082#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4083 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004084 set_ftrace_early_graph(ftrace_graph_buf, 1);
4085 if (ftrace_graph_notrace_buf[0])
4086 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004087#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004088}
4089
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004090int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004091{
4092 struct seq_file *m = (struct seq_file *)file->private_data;
4093 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004094 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004095 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004096 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004097 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004098 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004099
Steven Rostedt5072c592008-05-12 21:20:43 +02004100 if (file->f_mode & FMODE_READ) {
4101 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004102 seq_release(inode, file);
4103 } else
4104 iter = file->private_data;
4105
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004106 parser = &iter->parser;
4107 if (trace_parser_loaded(parser)) {
4108 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004109 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004110 }
4111
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004112 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004113
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004114 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004115
Steven Rostedt058e2972011-04-29 22:35:33 -04004116 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004117 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4118
4119 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004120 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004121 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004122 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004123
Steven Rostedt058e2972011-04-29 22:35:33 -04004124 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004125 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004126 ret = ftrace_hash_move(iter->ops, filter_hash,
4127 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004128 if (!ret) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004129 ftrace_ops_update_code(iter->ops, old_hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004130 free_ftrace_hash_rcu(old_hash);
4131 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004132 mutex_unlock(&ftrace_lock);
4133 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004134
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004135 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004136 free_ftrace_hash(iter->hash);
4137 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004138
Steven Rostedt5072c592008-05-12 21:20:43 +02004139 return 0;
4140}
4141
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004142static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004143 .open = ftrace_avail_open,
4144 .read = seq_read,
4145 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004146 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004147};
4148
Steven Rostedt647bcd02011-05-03 14:39:21 -04004149static const struct file_operations ftrace_enabled_fops = {
4150 .open = ftrace_enabled_open,
4151 .read = seq_read,
4152 .llseek = seq_lseek,
4153 .release = seq_release_private,
4154};
4155
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004156static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004157 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004158 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004159 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004160 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004161 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004162};
4163
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004164static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004165 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004166 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004167 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004168 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004169 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004170};
4171
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004172#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4173
4174static DEFINE_MUTEX(graph_lock);
4175
4176int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004177int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004178unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004179unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004180
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004181struct ftrace_graph_data {
4182 unsigned long *table;
4183 size_t size;
4184 int *count;
4185 const struct seq_operations *seq_ops;
4186};
4187
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004188static void *
Li Zefan85951842009-06-24 09:54:00 +08004189__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004190{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004191 struct ftrace_graph_data *fgd = m->private;
4192
4193 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004194 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004195 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004196}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004197
Li Zefan85951842009-06-24 09:54:00 +08004198static void *
4199g_next(struct seq_file *m, void *v, loff_t *pos)
4200{
4201 (*pos)++;
4202 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004203}
4204
4205static void *g_start(struct seq_file *m, loff_t *pos)
4206{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004207 struct ftrace_graph_data *fgd = m->private;
4208
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004209 mutex_lock(&graph_lock);
4210
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004211 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004212 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004213 return (void *)1;
4214
Li Zefan85951842009-06-24 09:54:00 +08004215 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004216}
4217
4218static void g_stop(struct seq_file *m, void *p)
4219{
4220 mutex_unlock(&graph_lock);
4221}
4222
4223static int g_show(struct seq_file *m, void *v)
4224{
4225 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004226
4227 if (!ptr)
4228 return 0;
4229
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004230 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004231 struct ftrace_graph_data *fgd = m->private;
4232
4233 if (fgd->table == ftrace_graph_funcs)
4234 seq_printf(m, "#### all functions enabled ####\n");
4235 else
4236 seq_printf(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004237 return 0;
4238 }
4239
Steven Rostedtb375a112009-09-17 00:05:58 -04004240 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004241
4242 return 0;
4243}
4244
James Morris88e9d342009-09-22 16:43:43 -07004245static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004246 .start = g_start,
4247 .next = g_next,
4248 .stop = g_stop,
4249 .show = g_show,
4250};
4251
4252static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004253__ftrace_graph_open(struct inode *inode, struct file *file,
4254 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004255{
4256 int ret = 0;
4257
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004258 mutex_lock(&graph_lock);
4259 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004260 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004261 *fgd->count = 0;
4262 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004263 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004264 mutex_unlock(&graph_lock);
4265
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004266 if (file->f_mode & FMODE_READ) {
4267 ret = seq_open(file, fgd->seq_ops);
4268 if (!ret) {
4269 struct seq_file *m = file->private_data;
4270 m->private = fgd;
4271 }
4272 } else
4273 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004274
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004275 return ret;
4276}
4277
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004278static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004279ftrace_graph_open(struct inode *inode, struct file *file)
4280{
4281 struct ftrace_graph_data *fgd;
4282
4283 if (unlikely(ftrace_disabled))
4284 return -ENODEV;
4285
4286 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4287 if (fgd == NULL)
4288 return -ENOMEM;
4289
4290 fgd->table = ftrace_graph_funcs;
4291 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4292 fgd->count = &ftrace_graph_count;
4293 fgd->seq_ops = &ftrace_graph_seq_ops;
4294
4295 return __ftrace_graph_open(inode, file, fgd);
4296}
4297
4298static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004299ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4300{
4301 struct ftrace_graph_data *fgd;
4302
4303 if (unlikely(ftrace_disabled))
4304 return -ENODEV;
4305
4306 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4307 if (fgd == NULL)
4308 return -ENOMEM;
4309
4310 fgd->table = ftrace_graph_notrace_funcs;
4311 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4312 fgd->count = &ftrace_graph_notrace_count;
4313 fgd->seq_ops = &ftrace_graph_seq_ops;
4314
4315 return __ftrace_graph_open(inode, file, fgd);
4316}
4317
4318static int
Li Zefan87827112009-07-23 11:29:11 +08004319ftrace_graph_release(struct inode *inode, struct file *file)
4320{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004321 if (file->f_mode & FMODE_READ) {
4322 struct seq_file *m = file->private_data;
4323
4324 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004325 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004326 } else {
4327 kfree(file->private_data);
4328 }
4329
Li Zefan87827112009-07-23 11:29:11 +08004330 return 0;
4331}
4332
4333static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004334ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004335{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004336 struct dyn_ftrace *rec;
4337 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004338 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004339 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004340 int type, not;
4341 char *search;
4342 bool exists;
4343 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004344
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004345 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004346 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004347 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004348 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004349
4350 search_len = strlen(search);
4351
Steven Rostedt52baf112009-02-14 01:15:39 -05004352 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004353
4354 if (unlikely(ftrace_disabled)) {
4355 mutex_unlock(&ftrace_lock);
4356 return -ENODEV;
4357 }
4358
Steven Rostedt265c8312009-02-13 12:43:56 -05004359 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004360
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004361 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004362 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004363 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004364 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004365 if (array[i] == rec->ip) {
4366 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004367 break;
4368 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004369 }
4370
4371 if (!not) {
4372 fail = 0;
4373 if (!exists) {
4374 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004375 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004376 goto out;
4377 }
4378 } else {
4379 if (exists) {
4380 array[i] = array[--(*idx)];
4381 array[*idx] = 0;
4382 fail = 0;
4383 }
4384 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004385 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004386 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004387out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004388 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004389
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004390 if (fail)
4391 return -EINVAL;
4392
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004393 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004394}
4395
4396static ssize_t
4397ftrace_graph_write(struct file *file, const char __user *ubuf,
4398 size_t cnt, loff_t *ppos)
4399{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004400 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004401 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004402 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004403
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004404 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004405 return 0;
4406
Namhyung Kim6a101082013-10-14 17:24:25 +09004407 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4408 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004409
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004410 read = trace_get_user(&parser, ubuf, cnt, ppos);
4411
Li Zefan4ba79782009-09-22 13:52:20 +08004412 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004413 parser.buffer[parser.idx] = 0;
4414
Namhyung Kim6a101082013-10-14 17:24:25 +09004415 mutex_lock(&graph_lock);
4416
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004417 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004418 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4419 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004420
4421 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004422 }
4423
Namhyung Kim6a101082013-10-14 17:24:25 +09004424 if (!ret)
4425 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004426
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004427 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004428
4429 return ret;
4430}
4431
4432static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004433 .open = ftrace_graph_open,
4434 .read = seq_read,
4435 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004436 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004437 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004438};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004439
4440static const struct file_operations ftrace_graph_notrace_fops = {
4441 .open = ftrace_graph_notrace_open,
4442 .read = seq_read,
4443 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004444 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004445 .release = ftrace_graph_release,
4446};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004447#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4448
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004449void ftrace_create_filter_files(struct ftrace_ops *ops,
4450 struct dentry *parent)
4451{
4452
4453 trace_create_file("set_ftrace_filter", 0644, parent,
4454 ops, &ftrace_filter_fops);
4455
4456 trace_create_file("set_ftrace_notrace", 0644, parent,
4457 ops, &ftrace_notrace_fops);
4458}
4459
4460/*
4461 * The name "destroy_filter_files" is really a misnomer. Although
4462 * in the future, it may actualy delete the files, but this is
4463 * really intended to make sure the ops passed in are disabled
4464 * and that when this function returns, the caller is free to
4465 * free the ops.
4466 *
4467 * The "destroy" name is only to match the "create" name that this
4468 * should be paired with.
4469 */
4470void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4471{
4472 mutex_lock(&ftrace_lock);
4473 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4474 ftrace_shutdown(ops, 0);
4475 ops->flags |= FTRACE_OPS_FL_DELETED;
4476 mutex_unlock(&ftrace_lock);
4477}
4478
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004479static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004480{
Steven Rostedt5072c592008-05-12 21:20:43 +02004481
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004482 trace_create_file("available_filter_functions", 0444,
4483 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004484
Steven Rostedt647bcd02011-05-03 14:39:21 -04004485 trace_create_file("enabled_functions", 0444,
4486 d_tracer, NULL, &ftrace_enabled_fops);
4487
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004488 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004489
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004490#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004491 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004492 NULL,
4493 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004494 trace_create_file("set_graph_notrace", 0444, d_tracer,
4495 NULL,
4496 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004497#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4498
Steven Rostedt5072c592008-05-12 21:20:43 +02004499 return 0;
4500}
4501
Steven Rostedt9fd49322012-04-24 22:32:06 -04004502static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004503{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004504 const unsigned long *ipa = a;
4505 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004506
Steven Rostedt9fd49322012-04-24 22:32:06 -04004507 if (*ipa > *ipb)
4508 return 1;
4509 if (*ipa < *ipb)
4510 return -1;
4511 return 0;
4512}
4513
4514static void ftrace_swap_ips(void *a, void *b, int size)
4515{
4516 unsigned long *ipa = a;
4517 unsigned long *ipb = b;
4518 unsigned long t;
4519
4520 t = *ipa;
4521 *ipa = *ipb;
4522 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004523}
4524
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004525static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004526 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004527 unsigned long *end)
4528{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004529 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004530 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004531 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004532 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004533 unsigned long *p;
4534 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004535 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004536 int ret = -ENOMEM;
4537
4538 count = end - start;
4539
4540 if (!count)
4541 return 0;
4542
Steven Rostedt9fd49322012-04-24 22:32:06 -04004543 sort(start, count, sizeof(*start),
4544 ftrace_cmp_ips, ftrace_swap_ips);
4545
Steven Rostedt706c81f2012-04-24 23:45:26 -04004546 start_pg = ftrace_allocate_pages(count);
4547 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004548 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004549
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004550 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004551
Steven Rostedt320823092011-12-16 14:42:37 -05004552 /*
4553 * Core and each module needs their own pages, as
4554 * modules will free them when they are removed.
4555 * Force a new page to be allocated for modules.
4556 */
Steven Rostedta7900872011-12-16 16:23:44 -05004557 if (!mod) {
4558 WARN_ON(ftrace_pages || ftrace_pages_start);
4559 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004560 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004561 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05004562 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004563 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05004564
Steven Rostedta7900872011-12-16 16:23:44 -05004565 if (WARN_ON(ftrace_pages->next)) {
4566 /* Hmm, we have free pages? */
4567 while (ftrace_pages->next)
4568 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05004569 }
Steven Rostedta7900872011-12-16 16:23:44 -05004570
Steven Rostedt706c81f2012-04-24 23:45:26 -04004571 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05004572 }
4573
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004574 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004575 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004576 while (p < end) {
4577 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004578 /*
4579 * Some architecture linkers will pad between
4580 * the different mcount_loc sections of different
4581 * object files to satisfy alignments.
4582 * Skip any NULL pointers.
4583 */
4584 if (!addr)
4585 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004586
4587 if (pg->index == pg->size) {
4588 /* We should have allocated enough */
4589 if (WARN_ON(!pg->next))
4590 break;
4591 pg = pg->next;
4592 }
4593
4594 rec = &pg->records[pg->index++];
4595 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004596 }
4597
Steven Rostedt706c81f2012-04-24 23:45:26 -04004598 /* We should have used all pages */
4599 WARN_ON(pg->next);
4600
4601 /* Assign the last page to ftrace_pages */
4602 ftrace_pages = pg;
4603
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004604 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004605 * We only need to disable interrupts on start up
4606 * because we are modifying code that an interrupt
4607 * may execute, and the modification is not atomic.
4608 * But for modules, nothing runs the code we modify
4609 * until we are finished with it, and there's no
4610 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004611 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004612 if (!mod)
4613 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004614 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004615 if (!mod)
4616 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004617 ret = 0;
4618 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004619 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004620
Steven Rostedta7900872011-12-16 16:23:44 -05004621 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004622}
4623
Steven Rostedt93eb6772009-04-15 13:24:06 -04004624#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05004625
4626#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4627
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004628void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004629{
4630 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05004631 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004632 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004633 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004634
Steven Rostedt93eb6772009-04-15 13:24:06 -04004635 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004636
4637 if (ftrace_disabled)
4638 goto out_unlock;
4639
Steven Rostedt320823092011-12-16 14:42:37 -05004640 /*
4641 * Each module has its own ftrace_pages, remove
4642 * them from the list.
4643 */
4644 last_pg = &ftrace_pages_start;
4645 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4646 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004647 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004648 /*
Steven Rostedt320823092011-12-16 14:42:37 -05004649 * As core pages are first, the first
4650 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004651 */
Steven Rostedt320823092011-12-16 14:42:37 -05004652 if (WARN_ON(pg == ftrace_pages_start))
4653 goto out_unlock;
4654
4655 /* Check if we are deleting the last page */
4656 if (pg == ftrace_pages)
4657 ftrace_pages = next_to_ftrace_page(last_pg);
4658
4659 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004660 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4661 free_pages((unsigned long)pg->records, order);
4662 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05004663 } else
4664 last_pg = &pg->next;
4665 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004666 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004667 mutex_unlock(&ftrace_lock);
4668}
4669
4670static void ftrace_init_module(struct module *mod,
4671 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004672{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004673 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004674 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004675 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004676}
4677
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004678void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004679{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004680 ftrace_init_module(mod, mod->ftrace_callsites,
4681 mod->ftrace_callsites +
4682 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004683}
4684
4685static int ftrace_module_notify_exit(struct notifier_block *self,
4686 unsigned long val, void *data)
4687{
4688 struct module *mod = data;
4689
4690 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004691 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004692
4693 return 0;
4694}
4695#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004696static int ftrace_module_notify_exit(struct notifier_block *self,
4697 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004698{
4699 return 0;
4700}
4701#endif /* CONFIG_MODULES */
4702
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004703struct notifier_block ftrace_module_exit_nb = {
4704 .notifier_call = ftrace_module_notify_exit,
4705 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4706};
4707
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004708void __init ftrace_init(void)
4709{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004710 extern unsigned long __start_mcount_loc[];
4711 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004712 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004713 int ret;
4714
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004715 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004716 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004717 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004718 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004719 goto failed;
4720
4721 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004722 if (!count) {
4723 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004724 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004725 }
4726
4727 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4728 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004729
4730 last_ftrace_enabled = ftrace_enabled = 1;
4731
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004732 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004733 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004734 __stop_mcount_loc);
4735
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004736 ret = register_module_notifier(&ftrace_module_exit_nb);
4737 if (ret)
4738 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004739
Steven Rostedt2af15d62009-05-28 13:37:24 -04004740 set_ftrace_early_filters();
4741
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004742 return;
4743 failed:
4744 ftrace_disabled = 1;
4745}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004746
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004747/* Do nothing if arch does not support this */
4748void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
4749{
4750}
4751
4752static void ftrace_update_trampoline(struct ftrace_ops *ops)
4753{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04004754
4755/*
4756 * Currently there's no safe way to free a trampoline when the kernel
4757 * is configured with PREEMPT. That is because a task could be preempted
4758 * when it jumped to the trampoline, it may be preempted for a long time
4759 * depending on the system load, and currently there's no way to know
4760 * when it will be off the trampoline. If the trampoline is freed
4761 * too early, when the task runs again, it will be executing on freed
4762 * memory and crash.
4763 */
4764#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004765 /* Currently, only non dynamic ops can have a trampoline */
4766 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
4767 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04004768#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004769
4770 arch_ftrace_update_trampoline(ops);
4771}
4772
Steven Rostedt3d083392008-05-12 21:20:42 +02004773#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004774
Steven Rostedt2b499382011-05-03 22:49:52 -04004775static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004776 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004777 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004778};
4779
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004780static int __init ftrace_nodyn_init(void)
4781{
4782 ftrace_enabled = 1;
4783 return 0;
4784}
Steven Rostedt6f415672012-10-05 12:13:07 -04004785core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004786
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004787static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4788static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04004789static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004790/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004791# define ftrace_startup(ops, command) \
4792 ({ \
4793 int ___ret = __register_ftrace_function(ops); \
4794 if (!___ret) \
4795 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4796 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004797 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004798# define ftrace_shutdown(ops, command) \
4799 ({ \
4800 int ___ret = __unregister_ftrace_function(ops); \
4801 if (!___ret) \
4802 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4803 ___ret; \
4804 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004805
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004806# define ftrace_startup_sysctl() do { } while (0)
4807# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004808
4809static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004810ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004811{
4812 return 1;
4813}
4814
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004815static void ftrace_update_trampoline(struct ftrace_ops *ops)
4816{
4817}
4818
Steven Rostedt3d083392008-05-12 21:20:42 +02004819#endif /* CONFIG_DYNAMIC_FTRACE */
4820
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004821__init void ftrace_init_global_array_ops(struct trace_array *tr)
4822{
4823 tr->ops = &global_ops;
4824 tr->ops->private = tr;
4825}
4826
4827void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4828{
4829 /* If we filter on pids, update to use the pid function */
4830 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4831 if (WARN_ON(tr->ops->func != ftrace_stub))
4832 printk("ftrace ops had %pS for function\n",
4833 tr->ops->func);
4834 /* Only the top level instance does pid tracing */
4835 if (!list_empty(&ftrace_pids)) {
4836 set_ftrace_pid_function(func);
4837 func = ftrace_pid_func;
4838 }
4839 }
4840 tr->ops->func = func;
4841 tr->ops->private = tr;
4842}
4843
4844void ftrace_reset_array_ops(struct trace_array *tr)
4845{
4846 tr->ops->func = ftrace_stub;
4847}
4848
Steven Rostedtb8489142011-05-04 09:27:52 -04004849static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004850ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004851 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004852{
Jiri Olsae2484912012-02-15 15:51:48 +01004853 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4854 return;
4855
4856 /*
4857 * Some of the ops may be dynamically allocated,
4858 * they must be freed after a synchronize_sched().
4859 */
4860 preempt_disable_notrace();
4861 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004862
4863 /*
4864 * Control funcs (perf) uses RCU. Only trace if
4865 * RCU is currently active.
4866 */
4867 if (!rcu_is_watching())
4868 goto out;
4869
Steven Rostedt0a016402012-11-02 17:03:03 -04004870 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004871 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4872 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004873 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004874 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004875 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004876 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004877 trace_recursion_clear(TRACE_CONTROL_BIT);
4878 preempt_enable_notrace();
4879}
4880
4881static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004882 .func = ftrace_ops_control_func,
4883 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004884 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004885};
4886
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004887static inline void
4888__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004889 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004890{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004891 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004892 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004893
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004894 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4895 if (bit < 0)
4896 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004897
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004898 /*
4899 * Some of the ops may be dynamically allocated,
4900 * they must be freed after a synchronize_sched().
4901 */
4902 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004903 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004904 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004905 if (FTRACE_WARN_ON(!op->func)) {
4906 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004907 goto out;
4908 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004909 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004910 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004911 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004912out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004913 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004914 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004915}
4916
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004917/*
4918 * Some archs only support passing ip and parent_ip. Even though
4919 * the list function ignores the op parameter, we do not want any
4920 * C side effects, where a function is called without the caller
4921 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004922 * Archs are to support both the regs and ftrace_ops at the same time.
4923 * If they support ftrace_ops, it is assumed they support regs.
4924 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004925 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4926 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004927 * An architecture can pass partial regs with ftrace_ops and still
4928 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004929 */
4930#if ARCH_SUPPORTS_FTRACE_OPS
4931static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004932 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004933{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004934 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004935}
4936#else
4937static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4938{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004939 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004940}
4941#endif
4942
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04004943/*
4944 * If there's only one function registered but it does not support
4945 * recursion, this function will be called by the mcount trampoline.
4946 * This function will handle recursion protection.
4947 */
4948static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
4949 struct ftrace_ops *op, struct pt_regs *regs)
4950{
4951 int bit;
4952
4953 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4954 if (bit < 0)
4955 return;
4956
4957 op->func(ip, parent_ip, op, regs);
4958
4959 trace_clear_recursion(bit);
4960}
4961
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04004962/**
4963 * ftrace_ops_get_func - get the function a trampoline should call
4964 * @ops: the ops to get the function for
4965 *
4966 * Normally the mcount trampoline will call the ops->func, but there
4967 * are times that it should not. For example, if the ops does not
4968 * have its own recursion protection, then it should call the
4969 * ftrace_ops_recurs_func() instead.
4970 *
4971 * Returns the function that the trampoline should call for @ops.
4972 */
4973ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
4974{
4975 /*
4976 * If this is a dynamic ops or we force list func,
4977 * then it needs to call the list anyway.
4978 */
4979 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
4980 return ftrace_ops_list_func;
4981
4982 /*
4983 * If the func handles its own recursion, call it directly.
4984 * Otherwise call the recursion protected function that
4985 * will call the ftrace ops function.
4986 */
4987 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
4988 return ftrace_ops_recurs_func;
4989
4990 return ops->func;
4991}
4992
Steven Rostedte32d8952008-12-04 00:26:41 -05004993static void clear_ftrace_swapper(void)
4994{
4995 struct task_struct *p;
4996 int cpu;
4997
4998 get_online_cpus();
4999 for_each_online_cpu(cpu) {
5000 p = idle_task(cpu);
5001 clear_tsk_trace_trace(p);
5002 }
5003 put_online_cpus();
5004}
5005
5006static void set_ftrace_swapper(void)
5007{
5008 struct task_struct *p;
5009 int cpu;
5010
5011 get_online_cpus();
5012 for_each_online_cpu(cpu) {
5013 p = idle_task(cpu);
5014 set_tsk_trace_trace(p);
5015 }
5016 put_online_cpus();
5017}
5018
5019static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005020{
5021 struct task_struct *p;
5022
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005023 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05005024 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05005025 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05005026 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005027 rcu_read_unlock();
5028
Steven Rostedte32d8952008-12-04 00:26:41 -05005029 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005030}
5031
Steven Rostedte32d8952008-12-04 00:26:41 -05005032static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005033{
5034 struct task_struct *p;
5035
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005036 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005037 do_each_pid_task(pid, PIDTYPE_PID, p) {
5038 set_tsk_trace_trace(p);
5039 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005040 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005041}
5042
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005043static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005044{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005045 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005046 clear_ftrace_swapper();
5047 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005048 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05005049}
5050
5051static void set_ftrace_pid_task(struct pid *pid)
5052{
5053 if (pid == ftrace_swapper_pid)
5054 set_ftrace_swapper();
5055 else
5056 set_ftrace_pid(pid);
5057}
5058
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005059static int ftrace_pid_add(int p)
5060{
5061 struct pid *pid;
5062 struct ftrace_pid *fpid;
5063 int ret = -EINVAL;
5064
5065 mutex_lock(&ftrace_lock);
5066
5067 if (!p)
5068 pid = ftrace_swapper_pid;
5069 else
5070 pid = find_get_pid(p);
5071
5072 if (!pid)
5073 goto out;
5074
5075 ret = 0;
5076
5077 list_for_each_entry(fpid, &ftrace_pids, list)
5078 if (fpid->pid == pid)
5079 goto out_put;
5080
5081 ret = -ENOMEM;
5082
5083 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5084 if (!fpid)
5085 goto out_put;
5086
5087 list_add(&fpid->list, &ftrace_pids);
5088 fpid->pid = pid;
5089
5090 set_ftrace_pid_task(pid);
5091
5092 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005093
5094 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005095
5096 mutex_unlock(&ftrace_lock);
5097 return 0;
5098
5099out_put:
5100 if (pid != ftrace_swapper_pid)
5101 put_pid(pid);
5102
5103out:
5104 mutex_unlock(&ftrace_lock);
5105 return ret;
5106}
5107
5108static void ftrace_pid_reset(void)
5109{
5110 struct ftrace_pid *fpid, *safe;
5111
5112 mutex_lock(&ftrace_lock);
5113 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5114 struct pid *pid = fpid->pid;
5115
5116 clear_ftrace_pid_task(pid);
5117
5118 list_del(&fpid->list);
5119 kfree(fpid);
5120 }
5121
5122 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005123 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005124
5125 mutex_unlock(&ftrace_lock);
5126}
5127
5128static void *fpid_start(struct seq_file *m, loff_t *pos)
5129{
5130 mutex_lock(&ftrace_lock);
5131
5132 if (list_empty(&ftrace_pids) && (!*pos))
5133 return (void *) 1;
5134
5135 return seq_list_start(&ftrace_pids, *pos);
5136}
5137
5138static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5139{
5140 if (v == (void *)1)
5141 return NULL;
5142
5143 return seq_list_next(v, &ftrace_pids, pos);
5144}
5145
5146static void fpid_stop(struct seq_file *m, void *p)
5147{
5148 mutex_unlock(&ftrace_lock);
5149}
5150
5151static int fpid_show(struct seq_file *m, void *v)
5152{
5153 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5154
5155 if (v == (void *)1) {
5156 seq_printf(m, "no pid\n");
5157 return 0;
5158 }
5159
5160 if (fpid->pid == ftrace_swapper_pid)
5161 seq_printf(m, "swapper tasks\n");
5162 else
5163 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5164
5165 return 0;
5166}
5167
5168static const struct seq_operations ftrace_pid_sops = {
5169 .start = fpid_start,
5170 .next = fpid_next,
5171 .stop = fpid_stop,
5172 .show = fpid_show,
5173};
5174
5175static int
5176ftrace_pid_open(struct inode *inode, struct file *file)
5177{
5178 int ret = 0;
5179
5180 if ((file->f_mode & FMODE_WRITE) &&
5181 (file->f_flags & O_TRUNC))
5182 ftrace_pid_reset();
5183
5184 if (file->f_mode & FMODE_READ)
5185 ret = seq_open(file, &ftrace_pid_sops);
5186
5187 return ret;
5188}
5189
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005190static ssize_t
5191ftrace_pid_write(struct file *filp, const char __user *ubuf,
5192 size_t cnt, loff_t *ppos)
5193{
Ingo Molnar457dc922009-11-23 11:03:28 +01005194 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005195 long val;
5196 int ret;
5197
5198 if (cnt >= sizeof(buf))
5199 return -EINVAL;
5200
5201 if (copy_from_user(&buf, ubuf, cnt))
5202 return -EFAULT;
5203
5204 buf[cnt] = 0;
5205
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005206 /*
5207 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5208 * to clean the filter quietly.
5209 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005210 tmp = strstrip(buf);
5211 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005212 return 1;
5213
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005214 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005215 if (ret < 0)
5216 return ret;
5217
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005218 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005219
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005220 return ret ? ret : cnt;
5221}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005222
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005223static int
5224ftrace_pid_release(struct inode *inode, struct file *file)
5225{
5226 if (file->f_mode & FMODE_READ)
5227 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005228
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005229 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005230}
5231
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005232static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005233 .open = ftrace_pid_open,
5234 .write = ftrace_pid_write,
5235 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005236 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005237 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005238};
5239
5240static __init int ftrace_init_debugfs(void)
5241{
5242 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005243
5244 d_tracer = tracing_init_dentry();
5245 if (!d_tracer)
5246 return 0;
5247
5248 ftrace_init_dyn_debugfs(d_tracer);
5249
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005250 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5251 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005252
5253 ftrace_profile_debugfs(d_tracer);
5254
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005255 return 0;
5256}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005257fs_initcall(ftrace_init_debugfs);
5258
Steven Rostedt3d083392008-05-12 21:20:42 +02005259/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005260 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005261 *
5262 * This function should be used by panic code. It stops ftrace
5263 * but in a not so nice way. If you need to simply kill ftrace
5264 * from a non-atomic section, use ftrace_kill.
5265 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005266void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005267{
5268 ftrace_disabled = 1;
5269 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005270 clear_ftrace_function();
5271}
5272
5273/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005274 * Test if ftrace is dead or not.
5275 */
5276int ftrace_is_dead(void)
5277{
5278 return ftrace_disabled;
5279}
5280
5281/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005282 * register_ftrace_function - register a function for profiling
5283 * @ops - ops structure that holds the function for profiling.
5284 *
5285 * Register a function to be called by all functions in the
5286 * kernel.
5287 *
5288 * Note: @ops->func and all the functions it calls must be labeled
5289 * with "notrace", otherwise it will go into a
5290 * recursive loop.
5291 */
5292int register_ftrace_function(struct ftrace_ops *ops)
5293{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005294 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005295
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005296 ftrace_ops_init(ops);
5297
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005298 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005299
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005300 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005301
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005302 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005303
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005304 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005305}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005306EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005307
5308/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005309 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005310 * @ops - ops structure that holds the function to unregister
5311 *
5312 * Unregister a function that was added to be called by ftrace profiling.
5313 */
5314int unregister_ftrace_function(struct ftrace_ops *ops)
5315{
5316 int ret;
5317
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005318 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005319 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005320 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005321
5322 return ret;
5323}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005324EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005325
Ingo Molnare309b412008-05-12 21:20:51 +02005326int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005327ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005328 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005329 loff_t *ppos)
5330{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005331 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005332
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005333 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005334
Steven Rostedt45a4a232011-04-21 23:16:46 -04005335 if (unlikely(ftrace_disabled))
5336 goto out;
5337
5338 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005339
Li Zefana32c7762009-06-26 16:55:51 +08005340 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005341 goto out;
5342
Li Zefana32c7762009-06-26 16:55:51 +08005343 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005344
5345 if (ftrace_enabled) {
5346
5347 ftrace_startup_sysctl();
5348
5349 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005350 if (ftrace_ops_list != &ftrace_list_end)
5351 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005352
5353 } else {
5354 /* stopping ftrace calls (just send to ftrace_stub) */
5355 ftrace_trace_function = ftrace_stub;
5356
5357 ftrace_shutdown_sysctl();
5358 }
5359
5360 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005361 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005362 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005363}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005364
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005365#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005366
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005367static struct ftrace_ops graph_ops = {
5368 .func = ftrace_stub,
5369 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5370 FTRACE_OPS_FL_INITIALIZED |
5371 FTRACE_OPS_FL_STUB,
5372#ifdef FTRACE_GRAPH_TRAMP_ADDR
5373 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5374#endif
5375 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5376};
5377
Steven Rostedt597af812009-04-03 15:24:12 -04005378static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005379
Steven Rostedte49dc192008-12-02 23:50:05 -05005380int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5381{
5382 return 0;
5383}
5384
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005385/* The callbacks that hook a function */
5386trace_func_graph_ret_t ftrace_graph_return =
5387 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005388trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005389static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005390
5391/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5392static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5393{
5394 int i;
5395 int ret = 0;
5396 unsigned long flags;
5397 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5398 struct task_struct *g, *t;
5399
5400 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5401 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5402 * sizeof(struct ftrace_ret_stack),
5403 GFP_KERNEL);
5404 if (!ret_stack_list[i]) {
5405 start = 0;
5406 end = i;
5407 ret = -ENOMEM;
5408 goto free;
5409 }
5410 }
5411
5412 read_lock_irqsave(&tasklist_lock, flags);
5413 do_each_thread(g, t) {
5414 if (start == end) {
5415 ret = -EAGAIN;
5416 goto unlock;
5417 }
5418
5419 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005420 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005421 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005422 t->curr_ret_stack = -1;
5423 /* Make sure the tasks see the -1 first: */
5424 smp_wmb();
5425 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005426 }
5427 } while_each_thread(g, t);
5428
5429unlock:
5430 read_unlock_irqrestore(&tasklist_lock, flags);
5431free:
5432 for (i = start; i < end; i++)
5433 kfree(ret_stack_list[i]);
5434 return ret;
5435}
5436
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005437static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005438ftrace_graph_probe_sched_switch(void *ignore,
5439 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005440{
5441 unsigned long long timestamp;
5442 int index;
5443
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005444 /*
5445 * Does the user want to count the time a function was asleep.
5446 * If so, do not update the time stamps.
5447 */
5448 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5449 return;
5450
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005451 timestamp = trace_clock_local();
5452
5453 prev->ftrace_timestamp = timestamp;
5454
5455 /* only process tasks that we timestamped */
5456 if (!next->ftrace_timestamp)
5457 return;
5458
5459 /*
5460 * Update all the counters in next to make up for the
5461 * time next was sleeping.
5462 */
5463 timestamp -= next->ftrace_timestamp;
5464
5465 for (index = next->curr_ret_stack; index >= 0; index--)
5466 next->ret_stack[index].calltime += timestamp;
5467}
5468
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005469/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005470static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005471{
5472 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005473 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005474
5475 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5476 sizeof(struct ftrace_ret_stack *),
5477 GFP_KERNEL);
5478
5479 if (!ret_stack_list)
5480 return -ENOMEM;
5481
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005482 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005483 for_each_online_cpu(cpu) {
5484 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005485 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005486 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005487
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005488 do {
5489 ret = alloc_retstack_tasklist(ret_stack_list);
5490 } while (ret == -EAGAIN);
5491
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005492 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005493 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005494 if (ret)
5495 pr_info("ftrace_graph: Couldn't activate tracepoint"
5496 " probe to kernel_sched_switch\n");
5497 }
5498
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005499 kfree(ret_stack_list);
5500 return ret;
5501}
5502
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005503/*
5504 * Hibernation protection.
5505 * The state of the current task is too much unstable during
5506 * suspend/restore to disk. We want to protect against that.
5507 */
5508static int
5509ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5510 void *unused)
5511{
5512 switch (state) {
5513 case PM_HIBERNATION_PREPARE:
5514 pause_graph_tracing();
5515 break;
5516
5517 case PM_POST_HIBERNATION:
5518 unpause_graph_tracing();
5519 break;
5520 }
5521 return NOTIFY_DONE;
5522}
5523
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005524static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5525{
5526 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5527 return 0;
5528 return __ftrace_graph_entry(trace);
5529}
5530
5531/*
5532 * The function graph tracer should only trace the functions defined
5533 * by set_ftrace_filter and set_ftrace_notrace. If another function
5534 * tracer ops is registered, the graph tracer requires testing the
5535 * function against the global ops, and not just trace any function
5536 * that any ftrace_ops registered.
5537 */
5538static void update_function_graph_func(void)
5539{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005540 struct ftrace_ops *op;
5541 bool do_test = false;
5542
5543 /*
5544 * The graph and global ops share the same set of functions
5545 * to test. If any other ops is on the list, then
5546 * the graph tracing needs to test if its the function
5547 * it should call.
5548 */
5549 do_for_each_ftrace_op(op, ftrace_ops_list) {
5550 if (op != &global_ops && op != &graph_ops &&
5551 op != &ftrace_list_end) {
5552 do_test = true;
5553 /* in double loop, break out with goto */
5554 goto out;
5555 }
5556 } while_for_each_ftrace_op(op);
5557 out:
5558 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005559 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005560 else
5561 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005562}
5563
Mathias Krause8275f692014-03-30 15:31:50 +02005564static struct notifier_block ftrace_suspend_notifier = {
5565 .notifier_call = ftrace_suspend_notifier_call,
5566};
5567
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005568int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5569 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005570{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005571 int ret = 0;
5572
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005573 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005574
Steven Rostedt05ce5812009-03-24 00:18:31 -04005575 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005576 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005577 ret = -EBUSY;
5578 goto out;
5579 }
5580
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005581 register_pm_notifier(&ftrace_suspend_notifier);
5582
Steven Rostedt597af812009-04-03 15:24:12 -04005583 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005584 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005585 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005586 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005587 goto out;
5588 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005589
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005590 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005591
5592 /*
5593 * Update the indirect function to the entryfunc, and the
5594 * function that gets called to the entry_test first. Then
5595 * call the update fgraph entry function to determine if
5596 * the entryfunc should be called directly or not.
5597 */
5598 __ftrace_graph_entry = entryfunc;
5599 ftrace_graph_entry = ftrace_graph_entry_test;
5600 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005601
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005602 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005603out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005604 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005605 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005606}
5607
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005608void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005609{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005610 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005611
Steven Rostedt597af812009-04-03 15:24:12 -04005612 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005613 goto out;
5614
Steven Rostedt597af812009-04-03 15:24:12 -04005615 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005616 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005617 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005618 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005619 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005620 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005621 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005622
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005623#ifdef CONFIG_DYNAMIC_FTRACE
5624 /*
5625 * Function graph does not allocate the trampoline, but
5626 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5627 * if one was used.
5628 */
5629 global_ops.trampoline = save_global_trampoline;
5630 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5631 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5632#endif
5633
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005634 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005635 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005636}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005637
Steven Rostedt868baf02011-02-10 21:26:13 -05005638static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5639
5640static void
5641graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5642{
5643 atomic_set(&t->tracing_graph_pause, 0);
5644 atomic_set(&t->trace_overrun, 0);
5645 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005646 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005647 smp_wmb();
5648 t->ret_stack = ret_stack;
5649}
5650
5651/*
5652 * Allocate a return stack for the idle task. May be the first
5653 * time through, or it may be done by CPU hotplug online.
5654 */
5655void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5656{
5657 t->curr_ret_stack = -1;
5658 /*
5659 * The idle task has no parent, it either has its own
5660 * stack or no stack at all.
5661 */
5662 if (t->ret_stack)
5663 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5664
5665 if (ftrace_graph_active) {
5666 struct ftrace_ret_stack *ret_stack;
5667
5668 ret_stack = per_cpu(idle_ret_stack, cpu);
5669 if (!ret_stack) {
5670 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5671 * sizeof(struct ftrace_ret_stack),
5672 GFP_KERNEL);
5673 if (!ret_stack)
5674 return;
5675 per_cpu(idle_ret_stack, cpu) = ret_stack;
5676 }
5677 graph_init_task(t, ret_stack);
5678 }
5679}
5680
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005681/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005682void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005683{
Steven Rostedt84047e32009-06-02 16:51:55 -04005684 /* Make sure we do not use the parent ret_stack */
5685 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005686 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005687
Steven Rostedt597af812009-04-03 15:24:12 -04005688 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005689 struct ftrace_ret_stack *ret_stack;
5690
5691 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005692 * sizeof(struct ftrace_ret_stack),
5693 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005694 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005695 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005696 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005697 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005698}
5699
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005700void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005701{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005702 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5703
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005704 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005705 /* NULL must become visible to IRQs before we free it: */
5706 barrier();
5707
5708 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005709}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005710#endif