blob: 39df3192e725b3c0b271735370ed6fa2997409d0 [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
68#define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70#else
71#define INIT_REGEX_LOCK(opsname)
72#endif
73
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040074static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040076 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/* ftrace_enabled is a method to turn ftrace on or off */
80int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020081static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050083/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084int function_trace_stop __read_mostly;
85
86/* Current function tracing op */
87struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050088/* What to set function_trace_op to */
89static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050090
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040091/* List for set_ftrace_pid's pids. */
92LIST_HEAD(ftrace_pids);
93struct ftrace_pid {
94 struct list_head list;
95 struct pid *pid;
96};
97
Steven Rostedt4eebcc82008-05-12 21:20:48 +020098/*
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
101 */
102static int ftrace_disabled __read_mostly;
103
Steven Rostedt52baf112009-02-14 01:15:39 -0500104static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200105
Jiri Olsae2484912012-02-15 15:51:48 +0100106static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400107static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200108ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500109ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400110static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100111static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200112
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400113#if ARCH_SUPPORTS_FTRACE_OPS
114static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400115 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400116#else
117/* See comment below, where ftrace_ops_list_func is defined */
118static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
119#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
120#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400121
Steven Rostedt0a016402012-11-02 17:03:03 -0400122/*
123 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400124 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400125 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400126 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400127 * concurrent insertions into the ftrace_global_list.
128 *
129 * Silly Alpha and silly pointer-speculation compiler optimizations!
130 */
131#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400132 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400133 do
134
135/*
136 * Optimized for just a single item in the list (as that is the normal case).
137 */
138#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400139 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400140 unlikely((op) != &ftrace_list_end))
141
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900142static inline void ftrace_ops_init(struct ftrace_ops *ops)
143{
144#ifdef CONFIG_DYNAMIC_FTRACE
145 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
146 mutex_init(&ops->regex_lock);
147 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
148 }
149#endif
150}
151
Steven Rostedtea701f12012-07-20 13:08:05 -0400152/**
153 * ftrace_nr_registered_ops - return number of ops registered
154 *
155 * Returns the number of ftrace_ops registered and tracing functions
156 */
157int ftrace_nr_registered_ops(void)
158{
159 struct ftrace_ops *ops;
160 int cnt = 0;
161
162 mutex_lock(&ftrace_lock);
163
164 for (ops = ftrace_ops_list;
165 ops != &ftrace_list_end; ops = ops->next)
166 cnt++;
167
168 mutex_unlock(&ftrace_lock);
169
170 return cnt;
171}
172
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400173static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400174 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500175{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500176 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500177 return;
178
Steven Rostedta1e2e312011-08-09 12:50:46 -0400179 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500180}
181
182static void set_ftrace_pid_function(ftrace_func_t func)
183{
184 /* do not set ftrace_pid_function to itself! */
185 if (func != ftrace_pid_func)
186 ftrace_pid_function = func;
187}
188
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200189/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200190 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200191 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200192 * This NULLs the ftrace function and in essence stops
193 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200194 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200195void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196{
Steven Rostedt3d083392008-05-12 21:20:42 +0200197 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500198 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200199}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200200
Jiri Olsae2484912012-02-15 15:51:48 +0100201static void control_ops_disable_all(struct ftrace_ops *ops)
202{
203 int cpu;
204
205 for_each_possible_cpu(cpu)
206 *per_cpu_ptr(ops->disabled, cpu) = 1;
207}
208
209static int control_ops_alloc(struct ftrace_ops *ops)
210{
211 int __percpu *disabled;
212
213 disabled = alloc_percpu(int);
214 if (!disabled)
215 return -ENOMEM;
216
217 ops->disabled = disabled;
218 control_ops_disable_all(ops);
219 return 0;
220}
221
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500222static void ftrace_sync(struct work_struct *work)
223{
224 /*
225 * This function is just a stub to implement a hard force
226 * of synchronize_sched(). This requires synchronizing
227 * tasks even in userspace and idle.
228 *
229 * Yes, function tracing is rude.
230 */
231}
232
233static void ftrace_sync_ipi(void *data)
234{
235 /* Probably not needed, but do it anyway */
236 smp_rmb();
237}
238
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500239#ifdef CONFIG_FUNCTION_GRAPH_TRACER
240static void update_function_graph_func(void);
241#else
242static inline void update_function_graph_func(void) { }
243#endif
244
Steven Rostedt2b499382011-05-03 22:49:52 -0400245static void update_ftrace_function(void)
246{
247 ftrace_func_t func;
248
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400249 /*
250 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400251 * recursion safe and not dynamic and the arch supports passing ops,
252 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400253 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400254 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400255 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400256 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400257 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400258 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400259 /* Set the ftrace_ops that the arch callback uses */
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500260 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400261 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400262 } else {
263 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500264 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400265 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400266 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400267
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500268 /* If there's no change, then do nothing more here */
269 if (ftrace_trace_function == func)
270 return;
271
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500272 update_function_graph_func();
273
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500274 /*
275 * If we are using the list function, it doesn't care
276 * about the function_trace_ops.
277 */
278 if (func == ftrace_ops_list_func) {
279 ftrace_trace_function = func;
280 /*
281 * Don't even bother setting function_trace_ops,
282 * it would be racy to do so anyway.
283 */
284 return;
285 }
286
287#ifndef CONFIG_DYNAMIC_FTRACE
288 /*
289 * For static tracing, we need to be a bit more careful.
290 * The function change takes affect immediately. Thus,
291 * we need to coorditate the setting of the function_trace_ops
292 * with the setting of the ftrace_trace_function.
293 *
294 * Set the function to the list ops, which will call the
295 * function we want, albeit indirectly, but it handles the
296 * ftrace_ops and doesn't depend on function_trace_op.
297 */
298 ftrace_trace_function = ftrace_ops_list_func;
299 /*
300 * Make sure all CPUs see this. Yes this is slow, but static
301 * tracing is slow and nasty to have enabled.
302 */
303 schedule_on_each_cpu(ftrace_sync);
304 /* Now all cpus are using the list ops. */
305 function_trace_op = set_function_trace_op;
306 /* Make sure the function_trace_op is visible on all CPUs */
307 smp_wmb();
308 /* Nasty way to force a rmb on all cpus */
309 smp_call_function(ftrace_sync_ipi, NULL, 1);
310 /* OK, we are all set to update the ftrace_trace_function now! */
311#endif /* !CONFIG_DYNAMIC_FTRACE */
312
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400313 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400314}
315
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800316int using_ftrace_ops_list_func(void)
317{
318 return ftrace_trace_function == ftrace_ops_list_func;
319}
320
Steven Rostedt2b499382011-05-03 22:49:52 -0400321static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200322{
Steven Rostedt2b499382011-05-03 22:49:52 -0400323 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200324 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400325 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200326 * CPU might be walking that list. We need to make sure
327 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400328 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200329 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400330 rcu_assign_pointer(*list, ops);
331}
Steven Rostedt3d083392008-05-12 21:20:42 +0200332
Steven Rostedt2b499382011-05-03 22:49:52 -0400333static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
334{
335 struct ftrace_ops **p;
336
337 /*
338 * If we are removing the last function, then simply point
339 * to the ftrace_stub.
340 */
341 if (*list == ops && ops->next == &ftrace_list_end) {
342 *list = &ftrace_list_end;
343 return 0;
344 }
345
346 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
347 if (*p == ops)
348 break;
349
350 if (*p != ops)
351 return -1;
352
353 *p = (*p)->next;
354 return 0;
355}
356
Jiri Olsae2484912012-02-15 15:51:48 +0100357static void add_ftrace_list_ops(struct ftrace_ops **list,
358 struct ftrace_ops *main_ops,
359 struct ftrace_ops *ops)
360{
361 int first = *list == &ftrace_list_end;
362 add_ftrace_ops(list, ops);
363 if (first)
364 add_ftrace_ops(&ftrace_ops_list, main_ops);
365}
366
367static int remove_ftrace_list_ops(struct ftrace_ops **list,
368 struct ftrace_ops *main_ops,
369 struct ftrace_ops *ops)
370{
371 int ret = remove_ftrace_ops(list, ops);
372 if (!ret && *list == &ftrace_list_end)
373 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
374 return ret;
375}
376
Steven Rostedt2b499382011-05-03 22:49:52 -0400377static int __register_ftrace_function(struct ftrace_ops *ops)
378{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500379 if (ops->flags & FTRACE_OPS_FL_DELETED)
380 return -EINVAL;
381
Steven Rostedtb8489142011-05-04 09:27:52 -0400382 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
383 return -EBUSY;
384
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900385#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400386 /*
387 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
388 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
389 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
390 */
391 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
392 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
393 return -EINVAL;
394
395 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
396 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
397#endif
398
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400399 if (!core_kernel_data((unsigned long)ops))
400 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
401
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500402 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100403 if (control_ops_alloc(ops))
404 return -ENOMEM;
405 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400406 } else
407 add_ftrace_ops(&ftrace_ops_list, ops);
408
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400409 if (ftrace_enabled)
410 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200411
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200412 return 0;
413}
414
Ingo Molnare309b412008-05-12 21:20:51 +0200415static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200416{
Steven Rostedt2b499382011-05-03 22:49:52 -0400417 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200418
Steven Rostedtb8489142011-05-04 09:27:52 -0400419 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
420 return -EBUSY;
421
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500422 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100423 ret = remove_ftrace_list_ops(&ftrace_control_list,
424 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400425 } else
426 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
427
Steven Rostedt2b499382011-05-03 22:49:52 -0400428 if (ret < 0)
429 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400430
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400431 if (ftrace_enabled)
432 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200433
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500434 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200435}
436
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500437static void ftrace_update_pid_func(void)
438{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400439 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500440 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900441 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500442
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400443 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500444}
445
Steven Rostedt493762f2009-03-23 17:12:36 -0400446#ifdef CONFIG_FUNCTION_PROFILER
447struct ftrace_profile {
448 struct hlist_node node;
449 unsigned long ip;
450 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400451#ifdef CONFIG_FUNCTION_GRAPH_TRACER
452 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400453 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400454#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400455};
456
457struct ftrace_profile_page {
458 struct ftrace_profile_page *next;
459 unsigned long index;
460 struct ftrace_profile records[];
461};
462
Steven Rostedtcafb1682009-03-24 20:50:39 -0400463struct ftrace_profile_stat {
464 atomic_t disabled;
465 struct hlist_head *hash;
466 struct ftrace_profile_page *pages;
467 struct ftrace_profile_page *start;
468 struct tracer_stat stat;
469};
470
Steven Rostedt493762f2009-03-23 17:12:36 -0400471#define PROFILE_RECORDS_SIZE \
472 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
473
474#define PROFILES_PER_PAGE \
475 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
476
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400477static int ftrace_profile_enabled __read_mostly;
478
479/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400480static DEFINE_MUTEX(ftrace_profile_lock);
481
Steven Rostedtcafb1682009-03-24 20:50:39 -0400482static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400483
Namhyung Kim20079eb2013-04-10 08:55:50 +0900484#define FTRACE_PROFILE_HASH_BITS 10
485#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400486
Steven Rostedt493762f2009-03-23 17:12:36 -0400487static void *
488function_stat_next(void *v, int idx)
489{
490 struct ftrace_profile *rec = v;
491 struct ftrace_profile_page *pg;
492
493 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
494
495 again:
Li Zefan0296e422009-06-26 11:15:37 +0800496 if (idx != 0)
497 rec++;
498
Steven Rostedt493762f2009-03-23 17:12:36 -0400499 if ((void *)rec >= (void *)&pg->records[pg->index]) {
500 pg = pg->next;
501 if (!pg)
502 return NULL;
503 rec = &pg->records[0];
504 if (!rec->counter)
505 goto again;
506 }
507
508 return rec;
509}
510
511static void *function_stat_start(struct tracer_stat *trace)
512{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400513 struct ftrace_profile_stat *stat =
514 container_of(trace, struct ftrace_profile_stat, stat);
515
516 if (!stat || !stat->start)
517 return NULL;
518
519 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400520}
521
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400522#ifdef CONFIG_FUNCTION_GRAPH_TRACER
523/* function graph compares on total time */
524static int function_stat_cmp(void *p1, void *p2)
525{
526 struct ftrace_profile *a = p1;
527 struct ftrace_profile *b = p2;
528
529 if (a->time < b->time)
530 return -1;
531 if (a->time > b->time)
532 return 1;
533 else
534 return 0;
535}
536#else
537/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400538static int function_stat_cmp(void *p1, void *p2)
539{
540 struct ftrace_profile *a = p1;
541 struct ftrace_profile *b = p2;
542
543 if (a->counter < b->counter)
544 return -1;
545 if (a->counter > b->counter)
546 return 1;
547 else
548 return 0;
549}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400550#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400551
552static int function_stat_headers(struct seq_file *m)
553{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400554#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400555 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400556 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400557 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400558 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400559#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400560 seq_printf(m, " Function Hit\n"
561 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400562#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400563 return 0;
564}
565
566static int function_stat_show(struct seq_file *m, void *v)
567{
568 struct ftrace_profile *rec = v;
569 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800570 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400572 static struct trace_seq s;
573 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400574 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400575#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800576 mutex_lock(&ftrace_profile_lock);
577
578 /* we raced with function_profile_reset() */
579 if (unlikely(rec->counter == 0)) {
580 ret = -EBUSY;
581 goto out;
582 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400583
584 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400585 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400586
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400587#ifdef CONFIG_FUNCTION_GRAPH_TRACER
588 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400589 avg = rec->time;
590 do_div(avg, rec->counter);
591
Chase Douglase330b3b2010-04-26 14:02:05 -0400592 /* Sample standard deviation (s^2) */
593 if (rec->counter <= 1)
594 stddev = 0;
595 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200596 /*
597 * Apply Welford's method:
598 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
599 */
600 stddev = rec->counter * rec->time_squared -
601 rec->time * rec->time;
602
Chase Douglase330b3b2010-04-26 14:02:05 -0400603 /*
604 * Divide only 1000 for ns^2 -> us^2 conversion.
605 * trace_print_graph_duration will divide 1000 again.
606 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200607 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400608 }
609
Steven Rostedt34886c82009-03-25 21:00:47 -0400610 trace_seq_init(&s);
611 trace_print_graph_duration(rec->time, &s);
612 trace_seq_puts(&s, " ");
613 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400614 trace_seq_puts(&s, " ");
615 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400616 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400617#endif
618 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800619out:
620 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400621
Li Zefan3aaba202010-08-23 16:50:12 +0800622 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400623}
624
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400626{
627 struct ftrace_profile_page *pg;
628
Steven Rostedtcafb1682009-03-24 20:50:39 -0400629 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400630
631 while (pg) {
632 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
633 pg->index = 0;
634 pg = pg->next;
635 }
636
Steven Rostedtcafb1682009-03-24 20:50:39 -0400637 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400638 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
639}
640
Steven Rostedtcafb1682009-03-24 20:50:39 -0400641int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400642{
643 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400644 int functions;
645 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400646 int i;
647
648 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400650 return 0;
651
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
653 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400654 return -ENOMEM;
655
Steven Rostedt318e0a72009-03-25 20:06:34 -0400656#ifdef CONFIG_DYNAMIC_FTRACE
657 functions = ftrace_update_tot_cnt;
658#else
659 /*
660 * We do not know the number of functions that exist because
661 * dynamic tracing is what counts them. With past experience
662 * we have around 20K functions. That should be more than enough.
663 * It is highly unlikely we will execute every function in
664 * the kernel.
665 */
666 functions = 20000;
667#endif
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400670
Steven Rostedt318e0a72009-03-25 20:06:34 -0400671 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
672
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900673 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400674 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400675 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400676 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400677 pg = pg->next;
678 }
679
680 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400681
682 out_free:
683 pg = stat->start;
684 while (pg) {
685 unsigned long tmp = (unsigned long)pg;
686
687 pg = pg->next;
688 free_page(tmp);
689 }
690
Steven Rostedt318e0a72009-03-25 20:06:34 -0400691 stat->pages = NULL;
692 stat->start = NULL;
693
694 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400695}
696
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400698{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400699 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400700 int size;
701
Steven Rostedtcafb1682009-03-24 20:50:39 -0400702 stat = &per_cpu(ftrace_profile_stats, cpu);
703
704 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400705 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400706 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400707 return 0;
708 }
709
710 /*
711 * We are profiling all functions, but usually only a few thousand
712 * functions are hit. We'll make a hash of 1024 items.
713 */
714 size = FTRACE_PROFILE_HASH_SIZE;
715
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400717
Steven Rostedtcafb1682009-03-24 20:50:39 -0400718 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400719 return -ENOMEM;
720
Steven Rostedt318e0a72009-03-25 20:06:34 -0400721 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 if (ftrace_profile_pages_init(stat) < 0) {
723 kfree(stat->hash);
724 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400725 return -ENOMEM;
726 }
727
728 return 0;
729}
730
Steven Rostedtcafb1682009-03-24 20:50:39 -0400731static int ftrace_profile_init(void)
732{
733 int cpu;
734 int ret = 0;
735
Miao Xiec4602c12013-12-16 15:20:01 +0800736 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400737 ret = ftrace_profile_init_cpu(cpu);
738 if (ret)
739 break;
740 }
741
742 return ret;
743}
744
Steven Rostedt493762f2009-03-23 17:12:36 -0400745/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400746static struct ftrace_profile *
747ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400748{
749 struct ftrace_profile *rec;
750 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400751 unsigned long key;
752
Namhyung Kim20079eb2013-04-10 08:55:50 +0900753 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400755
756 if (hlist_empty(hhd))
757 return NULL;
758
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400759 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400760 if (rec->ip == ip)
761 return rec;
762 }
763
764 return NULL;
765}
766
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767static void ftrace_add_profile(struct ftrace_profile_stat *stat,
768 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400769{
770 unsigned long key;
771
Namhyung Kim20079eb2013-04-10 08:55:50 +0900772 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400774}
775
Steven Rostedt318e0a72009-03-25 20:06:34 -0400776/*
777 * The memory is already allocated, this simply finds a new record to use.
778 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400779static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400780ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400781{
782 struct ftrace_profile *rec = NULL;
783
Steven Rostedt318e0a72009-03-25 20:06:34 -0400784 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400785 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400786 goto out;
787
Steven Rostedt493762f2009-03-23 17:12:36 -0400788 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400789 * Try to find the function again since an NMI
790 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400791 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400794 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400795
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 if (stat->pages->index == PROFILES_PER_PAGE) {
797 if (!stat->pages->next)
798 goto out;
799 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400800 }
801
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400805
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400808
809 return rec;
810}
811
Steven Rostedt493762f2009-03-23 17:12:36 -0400812static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400813function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400814 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400815{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400817 struct ftrace_profile *rec;
818 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400819
820 if (!ftrace_profile_enabled)
821 return;
822
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824
Christoph Lameterbdffd892014-04-29 14:17:40 -0500825 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400826 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400827 goto out;
828
829 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400831 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400832 if (!rec)
833 goto out;
834 }
835
836 rec->counter++;
837 out:
838 local_irq_restore(flags);
839}
840
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400841#ifdef CONFIG_FUNCTION_GRAPH_TRACER
842static int profile_graph_entry(struct ftrace_graph_ent *trace)
843{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400844 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400845 return 1;
846}
847
848static void profile_graph_return(struct ftrace_graph_ret *trace)
849{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400850 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400851 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400852 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400853 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400854
855 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500856 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400857 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400858 goto out;
859
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400860 /* If the calltime was zero'd ignore it */
861 if (!trace->calltime)
862 goto out;
863
Steven Rostedta2a16d62009-03-24 23:17:58 -0400864 calltime = trace->rettime - trace->calltime;
865
866 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
867 int index;
868
869 index = trace->depth;
870
871 /* Append this call time to the parent time to subtract */
872 if (index)
873 current->ret_stack[index - 1].subtime += calltime;
874
875 if (current->ret_stack[index].subtime < calltime)
876 calltime -= current->ret_stack[index].subtime;
877 else
878 calltime = 0;
879 }
880
Steven Rostedtcafb1682009-03-24 20:50:39 -0400881 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400882 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400883 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400884 rec->time_squared += calltime * calltime;
885 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400886
Steven Rostedtcafb1682009-03-24 20:50:39 -0400887 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400888 local_irq_restore(flags);
889}
890
891static int register_ftrace_profiler(void)
892{
893 return register_ftrace_graph(&profile_graph_return,
894 &profile_graph_entry);
895}
896
897static void unregister_ftrace_profiler(void)
898{
899 unregister_ftrace_graph();
900}
901#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100902static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400903 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900904 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
905 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400906};
907
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400908static int register_ftrace_profiler(void)
909{
910 return register_ftrace_function(&ftrace_profile_ops);
911}
912
913static void unregister_ftrace_profiler(void)
914{
915 unregister_ftrace_function(&ftrace_profile_ops);
916}
917#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
918
Steven Rostedt493762f2009-03-23 17:12:36 -0400919static ssize_t
920ftrace_profile_write(struct file *filp, const char __user *ubuf,
921 size_t cnt, loff_t *ppos)
922{
923 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400924 int ret;
925
Peter Huewe22fe9b52011-06-07 21:58:27 +0200926 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
927 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400928 return ret;
929
930 val = !!val;
931
932 mutex_lock(&ftrace_profile_lock);
933 if (ftrace_profile_enabled ^ val) {
934 if (val) {
935 ret = ftrace_profile_init();
936 if (ret < 0) {
937 cnt = ret;
938 goto out;
939 }
940
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400941 ret = register_ftrace_profiler();
942 if (ret < 0) {
943 cnt = ret;
944 goto out;
945 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400946 ftrace_profile_enabled = 1;
947 } else {
948 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400949 /*
950 * unregister_ftrace_profiler calls stop_machine
951 * so this acts like an synchronize_sched.
952 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400953 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400954 }
955 }
956 out:
957 mutex_unlock(&ftrace_profile_lock);
958
Jiri Olsacf8517c2009-10-23 19:36:16 -0400959 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400960
961 return cnt;
962}
963
964static ssize_t
965ftrace_profile_read(struct file *filp, char __user *ubuf,
966 size_t cnt, loff_t *ppos)
967{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400968 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400969 int r;
970
971 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
972 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
973}
974
975static const struct file_operations ftrace_profile_fops = {
976 .open = tracing_open_generic,
977 .read = ftrace_profile_read,
978 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200979 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400980};
981
Steven Rostedtcafb1682009-03-24 20:50:39 -0400982/* used to initialize the real stat files */
983static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400984 .name = "functions",
985 .stat_start = function_stat_start,
986 .stat_next = function_stat_next,
987 .stat_cmp = function_stat_cmp,
988 .stat_headers = function_stat_headers,
989 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400990};
991
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400992static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400993{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400994 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400995 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400996 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400997 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400998 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400999
Steven Rostedtcafb1682009-03-24 20:50:39 -04001000 for_each_possible_cpu(cpu) {
1001 stat = &per_cpu(ftrace_profile_stats, cpu);
1002
1003 /* allocate enough for function name + cpu number */
1004 name = kmalloc(32, GFP_KERNEL);
1005 if (!name) {
1006 /*
1007 * The files created are permanent, if something happens
1008 * we still do not free memory.
1009 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001010 WARN(1,
1011 "Could not allocate stat file for cpu %d\n",
1012 cpu);
1013 return;
1014 }
1015 stat->stat = function_stats;
1016 snprintf(name, 32, "function%d", cpu);
1017 stat->stat.name = name;
1018 ret = register_stat_tracer(&stat->stat);
1019 if (ret) {
1020 WARN(1,
1021 "Could not register function stat for cpu %d\n",
1022 cpu);
1023 kfree(name);
1024 return;
1025 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001026 }
1027
1028 entry = debugfs_create_file("function_profile_enabled", 0644,
1029 d_tracer, NULL, &ftrace_profile_fops);
1030 if (!entry)
1031 pr_warning("Could not create debugfs "
1032 "'function_profile_enabled' entry\n");
1033}
1034
1035#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001036static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001037{
1038}
1039#endif /* CONFIG_FUNCTION_PROFILER */
1040
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001041static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1042
Steven Rostedt3d083392008-05-12 21:20:42 +02001043#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001044
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001045static struct ftrace_ops *removed_ops;
1046
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001047#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001048# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001049#endif
1050
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001051static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1052
Steven Rostedtb6887d72009-02-17 12:32:04 -05001053struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001054 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001055 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001056 unsigned long flags;
1057 unsigned long ip;
1058 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001059 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001060};
1061
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001062struct ftrace_func_entry {
1063 struct hlist_node hlist;
1064 unsigned long ip;
1065};
1066
1067struct ftrace_hash {
1068 unsigned long size_bits;
1069 struct hlist_head *buckets;
1070 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001071 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001072};
1073
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001074/*
1075 * We make these constant because no one should touch them,
1076 * but they are used as the default "empty hash", to avoid allocating
1077 * it all the time. These are in a read only section such that if
1078 * anyone does try to modify it, it will cause an exception.
1079 */
1080static const struct hlist_head empty_buckets[1];
1081static const struct ftrace_hash empty_hash = {
1082 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001083};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001084#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001085
Steven Rostedt2b499382011-05-03 22:49:52 -04001086static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001087 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001088 .notrace_hash = EMPTY_HASH,
1089 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001090 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1091 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001092};
1093
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001094struct ftrace_page {
1095 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001096 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001097 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001098 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001099};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001100
Steven Rostedta7900872011-12-16 16:23:44 -05001101#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1102#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001103
1104/* estimate from running different kernels */
1105#define NR_TO_INIT 10000
1106
1107static struct ftrace_page *ftrace_pages_start;
1108static struct ftrace_page *ftrace_pages;
1109
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001110static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001111{
1112 return !hash || !hash->count;
1113}
1114
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001115static struct ftrace_func_entry *
1116ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1117{
1118 unsigned long key;
1119 struct ftrace_func_entry *entry;
1120 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001121
Steven Rostedt06a51d92011-12-19 19:07:36 -05001122 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123 return NULL;
1124
1125 if (hash->size_bits > 0)
1126 key = hash_long(ip, hash->size_bits);
1127 else
1128 key = 0;
1129
1130 hhd = &hash->buckets[key];
1131
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001132 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001133 if (entry->ip == ip)
1134 return entry;
1135 }
1136 return NULL;
1137}
1138
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001139static void __add_hash_entry(struct ftrace_hash *hash,
1140 struct ftrace_func_entry *entry)
1141{
1142 struct hlist_head *hhd;
1143 unsigned long key;
1144
1145 if (hash->size_bits)
1146 key = hash_long(entry->ip, hash->size_bits);
1147 else
1148 key = 0;
1149
1150 hhd = &hash->buckets[key];
1151 hlist_add_head(&entry->hlist, hhd);
1152 hash->count++;
1153}
1154
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001155static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1156{
1157 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001158
1159 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1160 if (!entry)
1161 return -ENOMEM;
1162
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001163 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001164 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001165
1166 return 0;
1167}
1168
1169static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001170free_hash_entry(struct ftrace_hash *hash,
1171 struct ftrace_func_entry *entry)
1172{
1173 hlist_del(&entry->hlist);
1174 kfree(entry);
1175 hash->count--;
1176}
1177
1178static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001179remove_hash_entry(struct ftrace_hash *hash,
1180 struct ftrace_func_entry *entry)
1181{
1182 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001183 hash->count--;
1184}
1185
1186static void ftrace_hash_clear(struct ftrace_hash *hash)
1187{
1188 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001189 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001190 struct ftrace_func_entry *entry;
1191 int size = 1 << hash->size_bits;
1192 int i;
1193
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001194 if (!hash->count)
1195 return;
1196
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001197 for (i = 0; i < size; i++) {
1198 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001199 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001200 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001201 }
1202 FTRACE_WARN_ON(hash->count);
1203}
1204
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001205static void free_ftrace_hash(struct ftrace_hash *hash)
1206{
1207 if (!hash || hash == EMPTY_HASH)
1208 return;
1209 ftrace_hash_clear(hash);
1210 kfree(hash->buckets);
1211 kfree(hash);
1212}
1213
Steven Rostedt07fd5512011-05-05 18:03:47 -04001214static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1215{
1216 struct ftrace_hash *hash;
1217
1218 hash = container_of(rcu, struct ftrace_hash, rcu);
1219 free_ftrace_hash(hash);
1220}
1221
1222static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1223{
1224 if (!hash || hash == EMPTY_HASH)
1225 return;
1226 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1227}
1228
Jiri Olsa5500fa52012-02-15 15:51:54 +01001229void ftrace_free_filter(struct ftrace_ops *ops)
1230{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001231 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001232 free_ftrace_hash(ops->filter_hash);
1233 free_ftrace_hash(ops->notrace_hash);
1234}
1235
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001236static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1237{
1238 struct ftrace_hash *hash;
1239 int size;
1240
1241 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1242 if (!hash)
1243 return NULL;
1244
1245 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001246 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001247
1248 if (!hash->buckets) {
1249 kfree(hash);
1250 return NULL;
1251 }
1252
1253 hash->size_bits = size_bits;
1254
1255 return hash;
1256}
1257
1258static struct ftrace_hash *
1259alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1260{
1261 struct ftrace_func_entry *entry;
1262 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001263 int size;
1264 int ret;
1265 int i;
1266
1267 new_hash = alloc_ftrace_hash(size_bits);
1268 if (!new_hash)
1269 return NULL;
1270
1271 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001272 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001273 return new_hash;
1274
1275 size = 1 << hash->size_bits;
1276 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001277 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001278 ret = add_hash_entry(new_hash, entry->ip);
1279 if (ret < 0)
1280 goto free_hash;
1281 }
1282 }
1283
1284 FTRACE_WARN_ON(new_hash->count != hash->count);
1285
1286 return new_hash;
1287
1288 free_hash:
1289 free_ftrace_hash(new_hash);
1290 return NULL;
1291}
1292
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001293static void
1294ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1295static void
1296ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1297
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001298static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001299ftrace_hash_move(struct ftrace_ops *ops, int enable,
1300 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001301{
1302 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001303 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001304 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001305 struct ftrace_hash *old_hash;
1306 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001307 int size = src->count;
1308 int bits = 0;
1309 int i;
1310
1311 /*
1312 * If the new source is empty, just free dst and assign it
1313 * the empty_hash.
1314 */
1315 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001316 new_hash = EMPTY_HASH;
1317 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001318 }
1319
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001320 /*
1321 * Make the hash size about 1/2 the # found
1322 */
1323 for (size /= 2; size; size >>= 1)
1324 bits++;
1325
1326 /* Don't allocate too much */
1327 if (bits > FTRACE_HASH_MAX_BITS)
1328 bits = FTRACE_HASH_MAX_BITS;
1329
Steven Rostedt07fd5512011-05-05 18:03:47 -04001330 new_hash = alloc_ftrace_hash(bits);
1331 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001332 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333
1334 size = 1 << src->size_bits;
1335 for (i = 0; i < size; i++) {
1336 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001337 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001338 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001339 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001340 }
1341 }
1342
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001343update:
1344 /*
1345 * Remove the current set, update the hash and add
1346 * them back.
1347 */
1348 ftrace_hash_rec_disable(ops, enable);
1349
Steven Rostedt07fd5512011-05-05 18:03:47 -04001350 old_hash = *dst;
1351 rcu_assign_pointer(*dst, new_hash);
1352 free_ftrace_hash_rcu(old_hash);
1353
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001354 ftrace_hash_rec_enable(ops, enable);
1355
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001356 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001357}
1358
Steven Rostedt265c8312009-02-13 12:43:56 -05001359/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001360 * Test the hashes for this ops to see if we want to call
1361 * the ops->func or not.
1362 *
1363 * It's a match if the ip is in the ops->filter_hash or
1364 * the filter_hash does not exist or is empty,
1365 * AND
1366 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001367 *
1368 * This needs to be called with preemption disabled as
1369 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001370 */
1371static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001372ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001373{
1374 struct ftrace_hash *filter_hash;
1375 struct ftrace_hash *notrace_hash;
1376 int ret;
1377
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001378#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1379 /*
1380 * There's a small race when adding ops that the ftrace handler
1381 * that wants regs, may be called without them. We can not
1382 * allow that handler to be called if regs is NULL.
1383 */
1384 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1385 return 0;
1386#endif
1387
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001388 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1389 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001390
Steven Rostedt06a51d92011-12-19 19:07:36 -05001391 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001392 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001393 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001394 !ftrace_lookup_ip(notrace_hash, ip)))
1395 ret = 1;
1396 else
1397 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001398
1399 return ret;
1400}
1401
1402/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001403 * This is a double for. Do not use 'break' to break out of the loop,
1404 * you must use a goto.
1405 */
1406#define do_for_each_ftrace_rec(pg, rec) \
1407 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1408 int _____i; \
1409 for (_____i = 0; _____i < pg->index; _____i++) { \
1410 rec = &pg->records[_____i];
1411
1412#define while_for_each_ftrace_rec() \
1413 } \
1414 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301415
Steven Rostedt5855fea2011-12-16 19:27:42 -05001416
1417static int ftrace_cmp_recs(const void *a, const void *b)
1418{
Steven Rostedta650e022012-04-25 13:48:13 -04001419 const struct dyn_ftrace *key = a;
1420 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001421
Steven Rostedta650e022012-04-25 13:48:13 -04001422 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001423 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001424 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1425 return 1;
1426 return 0;
1427}
1428
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001429static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001430{
1431 struct ftrace_page *pg;
1432 struct dyn_ftrace *rec;
1433 struct dyn_ftrace key;
1434
1435 key.ip = start;
1436 key.flags = end; /* overload flags, as it is unsigned long */
1437
1438 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1439 if (end < pg->records[0].ip ||
1440 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1441 continue;
1442 rec = bsearch(&key, pg->records, pg->index,
1443 sizeof(struct dyn_ftrace),
1444 ftrace_cmp_recs);
1445 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001446 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001447 }
1448
Steven Rostedt5855fea2011-12-16 19:27:42 -05001449 return 0;
1450}
1451
Steven Rostedtc88fd862011-08-16 09:53:39 -04001452/**
1453 * ftrace_location - return true if the ip giving is a traced location
1454 * @ip: the instruction pointer to check
1455 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001456 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001457 * That is, the instruction that is either a NOP or call to
1458 * the function tracer. It checks the ftrace internal tables to
1459 * determine if the address belongs or not.
1460 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001461unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001462{
Steven Rostedta650e022012-04-25 13:48:13 -04001463 return ftrace_location_range(ip, ip);
1464}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001465
Steven Rostedta650e022012-04-25 13:48:13 -04001466/**
1467 * ftrace_text_reserved - return true if range contains an ftrace location
1468 * @start: start of range to search
1469 * @end: end of range to search (inclusive). @end points to the last byte to check.
1470 *
1471 * Returns 1 if @start and @end contains a ftrace location.
1472 * That is, the instruction that is either a NOP or call to
1473 * the function tracer. It checks the ftrace internal tables to
1474 * determine if the address belongs or not.
1475 */
Sasha Levind88471c2013-01-09 18:09:20 -05001476int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001477{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001478 unsigned long ret;
1479
1480 ret = ftrace_location_range((unsigned long)start,
1481 (unsigned long)end);
1482
1483 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001484}
1485
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001486/* Test if ops registered to this rec needs regs */
1487static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1488{
1489 struct ftrace_ops *ops;
1490 bool keep_regs = false;
1491
1492 for (ops = ftrace_ops_list;
1493 ops != &ftrace_list_end; ops = ops->next) {
1494 /* pass rec in as regs to have non-NULL val */
1495 if (ftrace_ops_test(ops, rec->ip, rec)) {
1496 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1497 keep_regs = true;
1498 break;
1499 }
1500 }
1501 }
1502
1503 return keep_regs;
1504}
1505
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001506static void ftrace_remove_tramp(struct ftrace_ops *ops,
1507 struct dyn_ftrace *rec)
1508{
1509 struct ftrace_func_entry *entry;
1510
1511 entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1512 if (!entry)
1513 return;
1514
1515 /*
1516 * The tramp_hash entry will be removed at time
1517 * of update.
1518 */
1519 ops->trampolines--;
1520 rec->flags &= ~FTRACE_FL_TRAMP;
1521}
1522
1523static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1524{
1525 struct ftrace_ops *op;
1526
1527 do_for_each_ftrace_op(op, ftrace_ops_list) {
1528 if (op->trampolines)
1529 ftrace_remove_tramp(op, rec);
1530 } while_for_each_ftrace_op(op);
1531}
1532
Steven Rostedted926f92011-05-03 13:25:24 -04001533static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1534 int filter_hash,
1535 bool inc)
1536{
1537 struct ftrace_hash *hash;
1538 struct ftrace_hash *other_hash;
1539 struct ftrace_page *pg;
1540 struct dyn_ftrace *rec;
1541 int count = 0;
1542 int all = 0;
1543
1544 /* Only update if the ops has been registered */
1545 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1546 return;
1547
1548 /*
1549 * In the filter_hash case:
1550 * If the count is zero, we update all records.
1551 * Otherwise we just update the items in the hash.
1552 *
1553 * In the notrace_hash case:
1554 * We enable the update in the hash.
1555 * As disabling notrace means enabling the tracing,
1556 * and enabling notrace means disabling, the inc variable
1557 * gets inversed.
1558 */
1559 if (filter_hash) {
1560 hash = ops->filter_hash;
1561 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001562 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001563 all = 1;
1564 } else {
1565 inc = !inc;
1566 hash = ops->notrace_hash;
1567 other_hash = ops->filter_hash;
1568 /*
1569 * If the notrace hash has no items,
1570 * then there's nothing to do.
1571 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001572 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001573 return;
1574 }
1575
1576 do_for_each_ftrace_rec(pg, rec) {
1577 int in_other_hash = 0;
1578 int in_hash = 0;
1579 int match = 0;
1580
1581 if (all) {
1582 /*
1583 * Only the filter_hash affects all records.
1584 * Update if the record is not in the notrace hash.
1585 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001586 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001587 match = 1;
1588 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001589 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1590 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001591
1592 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001593 * If filter_hash is set, we want to match all functions
1594 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001595 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001596 * If filter_hash is not set, then we are decrementing.
1597 * That means we match anything that is in the hash
1598 * and also in the other_hash. That is, we need to turn
1599 * off functions in the other hash because they are disabled
1600 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001601 */
1602 if (filter_hash && in_hash && !in_other_hash)
1603 match = 1;
1604 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001605 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001606 match = 1;
1607 }
1608 if (!match)
1609 continue;
1610
1611 if (inc) {
1612 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001613 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001614 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001615
1616 /*
1617 * If there's only a single callback registered to a
1618 * function, and the ops has a trampoline registered
1619 * for it, then we can call it directly.
1620 */
1621 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1622 rec->flags |= FTRACE_FL_TRAMP;
1623 ops->trampolines++;
1624 } else {
1625 /*
1626 * If we are adding another function callback
1627 * to this function, and the previous had a
1628 * trampoline used, then we need to go back to
1629 * the default trampoline.
1630 */
1631 rec->flags &= ~FTRACE_FL_TRAMP;
1632
1633 /* remove trampolines from any ops for this rec */
1634 ftrace_clear_tramps(rec);
1635 }
1636
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
1648 if (ops->trampoline && !ftrace_rec_count(rec))
1649 ftrace_remove_tramp(ops, rec);
1650
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001651 /*
1652 * If the rec had REGS enabled and the ops that is
1653 * being removed had REGS set, then see if there is
1654 * still any ops for this record that wants regs.
1655 * If not, we can stop recording them.
1656 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001657 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001658 rec->flags & FTRACE_FL_REGS &&
1659 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1660 if (!test_rec_ops_needs_regs(rec))
1661 rec->flags &= ~FTRACE_FL_REGS;
1662 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001663
1664 /*
1665 * flags will be cleared in ftrace_check_record()
1666 * if rec count is zero.
1667 */
Steven Rostedted926f92011-05-03 13:25:24 -04001668 }
1669 count++;
1670 /* Shortcut, if we handled all records, we are done. */
1671 if (!all && count == hash->count)
1672 return;
1673 } while_for_each_ftrace_rec();
1674}
1675
1676static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1677 int filter_hash)
1678{
1679 __ftrace_hash_rec_update(ops, filter_hash, 0);
1680}
1681
1682static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1683 int filter_hash)
1684{
1685 __ftrace_hash_rec_update(ops, filter_hash, 1);
1686}
1687
Steven Rostedt05736a42008-09-22 14:55:47 -07001688static void print_ip_ins(const char *fmt, unsigned char *p)
1689{
1690 int i;
1691
1692 printk(KERN_CONT "%s", fmt);
1693
1694 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1695 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1696}
1697
Steven Rostedtc88fd862011-08-16 09:53:39 -04001698/**
1699 * ftrace_bug - report and shutdown function tracer
1700 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1701 * @ip: The address that failed
1702 *
1703 * The arch code that enables or disables the function tracing
1704 * can call ftrace_bug() when it has detected a problem in
1705 * modifying the code. @failed should be one of either:
1706 * EFAULT - if the problem happens on reading the @ip address
1707 * EINVAL - if what is read at @ip is not what was expected
1708 * EPERM - if the problem happens on writting to the @ip address
1709 */
1710void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001711{
1712 switch (failed) {
1713 case -EFAULT:
1714 FTRACE_WARN_ON_ONCE(1);
1715 pr_info("ftrace faulted on modifying ");
1716 print_ip_sym(ip);
1717 break;
1718 case -EINVAL:
1719 FTRACE_WARN_ON_ONCE(1);
1720 pr_info("ftrace failed to modify ");
1721 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001722 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001723 printk(KERN_CONT "\n");
1724 break;
1725 case -EPERM:
1726 FTRACE_WARN_ON_ONCE(1);
1727 pr_info("ftrace faulted on writing ");
1728 print_ip_sym(ip);
1729 break;
1730 default:
1731 FTRACE_WARN_ON_ONCE(1);
1732 pr_info("ftrace faulted on unknown error ");
1733 print_ip_sym(ip);
1734 }
1735}
1736
Steven Rostedtc88fd862011-08-16 09:53:39 -04001737static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001738{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001739 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001740
Steven Rostedt982c3502008-11-15 16:31:41 -05001741 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001742 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001743 *
Steven Rostedted926f92011-05-03 13:25:24 -04001744 * If the record has a ref count, then we need to enable it
1745 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001746 *
Steven Rostedted926f92011-05-03 13:25:24 -04001747 * Otherwise we make sure its disabled.
1748 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001749 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001750 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001751 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001752 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001753 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001754
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001755 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001756 * If enabling and the REGS flag does not match the REGS_EN, or
1757 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1758 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001759 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001760 if (flag) {
1761 if (!(rec->flags & FTRACE_FL_REGS) !=
1762 !(rec->flags & FTRACE_FL_REGS_EN))
1763 flag |= FTRACE_FL_REGS;
1764
1765 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1766 !(rec->flags & FTRACE_FL_TRAMP_EN))
1767 flag |= FTRACE_FL_TRAMP;
1768 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001769
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001770 /* If the state of this record hasn't changed, then do nothing */
1771 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001772 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001773
1774 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001775 /* Save off if rec is being enabled (for return value) */
1776 flag ^= rec->flags & FTRACE_FL_ENABLED;
1777
1778 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001779 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001780 if (flag & FTRACE_FL_REGS) {
1781 if (rec->flags & FTRACE_FL_REGS)
1782 rec->flags |= FTRACE_FL_REGS_EN;
1783 else
1784 rec->flags &= ~FTRACE_FL_REGS_EN;
1785 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001786 if (flag & FTRACE_FL_TRAMP) {
1787 if (rec->flags & FTRACE_FL_TRAMP)
1788 rec->flags |= FTRACE_FL_TRAMP_EN;
1789 else
1790 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1791 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001792 }
1793
1794 /*
1795 * If this record is being updated from a nop, then
1796 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001797 * Otherwise,
1798 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001799 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001800 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001801 */
1802 if (flag & FTRACE_FL_ENABLED)
1803 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001804
1805 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001806 }
1807
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001808 if (update) {
1809 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001810 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001811 rec->flags = 0;
1812 else
1813 /* Just disable the record (keep REGS state) */
1814 rec->flags &= ~FTRACE_FL_ENABLED;
1815 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001816
1817 return FTRACE_UPDATE_MAKE_NOP;
1818}
1819
1820/**
1821 * ftrace_update_record, set a record that now is tracing or not
1822 * @rec: the record to update
1823 * @enable: set to 1 if the record is tracing, zero to force disable
1824 *
1825 * The records that represent all functions that can be traced need
1826 * to be updated when tracing has been enabled.
1827 */
1828int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1829{
1830 return ftrace_check_record(rec, enable, 1);
1831}
1832
1833/**
1834 * ftrace_test_record, check if the record has been enabled or not
1835 * @rec: the record to test
1836 * @enable: set to 1 to check if enabled, 0 if it is disabled
1837 *
1838 * The arch code may need to test if a record is already set to
1839 * tracing to determine how to modify the function code that it
1840 * represents.
1841 */
1842int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1843{
1844 return ftrace_check_record(rec, enable, 0);
1845}
1846
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001847static struct ftrace_ops *
1848ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1849{
1850 struct ftrace_ops *op;
1851
1852 /* Removed ops need to be tested first */
1853 if (removed_ops && removed_ops->tramp_hash) {
1854 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1855 return removed_ops;
1856 }
1857
1858 do_for_each_ftrace_op(op, ftrace_ops_list) {
1859 if (!op->tramp_hash)
1860 continue;
1861
1862 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1863 return op;
1864
1865 } while_for_each_ftrace_op(op);
1866
1867 return NULL;
1868}
1869
1870static struct ftrace_ops *
1871ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1872{
1873 struct ftrace_ops *op;
1874
1875 do_for_each_ftrace_op(op, ftrace_ops_list) {
1876 /* pass rec in as regs to have non-NULL val */
1877 if (ftrace_ops_test(op, rec->ip, rec))
1878 return op;
1879 } while_for_each_ftrace_op(op);
1880
1881 return NULL;
1882}
1883
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001884/**
1885 * ftrace_get_addr_new - Get the call address to set to
1886 * @rec: The ftrace record descriptor
1887 *
1888 * If the record has the FTRACE_FL_REGS set, that means that it
1889 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1890 * is not not set, then it wants to convert to the normal callback.
1891 *
1892 * Returns the address of the trampoline to set to
1893 */
1894unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1895{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001896 struct ftrace_ops *ops;
1897
1898 /* Trampolines take precedence over regs */
1899 if (rec->flags & FTRACE_FL_TRAMP) {
1900 ops = ftrace_find_tramp_ops_new(rec);
1901 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1902 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1903 (void *)rec->ip, (void *)rec->ip);
1904 /* Ftrace is shutting down, return anything */
1905 return (unsigned long)FTRACE_ADDR;
1906 }
1907 return ops->trampoline;
1908 }
1909
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001910 if (rec->flags & FTRACE_FL_REGS)
1911 return (unsigned long)FTRACE_REGS_ADDR;
1912 else
1913 return (unsigned long)FTRACE_ADDR;
1914}
1915
1916/**
1917 * ftrace_get_addr_curr - Get the call address that is already there
1918 * @rec: The ftrace record descriptor
1919 *
1920 * The FTRACE_FL_REGS_EN is set when the record already points to
1921 * a function that saves all the regs. Basically the '_EN' version
1922 * represents the current state of the function.
1923 *
1924 * Returns the address of the trampoline that is currently being called
1925 */
1926unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1927{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001928 struct ftrace_ops *ops;
1929
1930 /* Trampolines take precedence over regs */
1931 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1932 ops = ftrace_find_tramp_ops_curr(rec);
1933 if (FTRACE_WARN_ON(!ops)) {
1934 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1935 (void *)rec->ip, (void *)rec->ip);
1936 /* Ftrace is shutting down, return anything */
1937 return (unsigned long)FTRACE_ADDR;
1938 }
1939 return ops->trampoline;
1940 }
1941
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001942 if (rec->flags & FTRACE_FL_REGS_EN)
1943 return (unsigned long)FTRACE_REGS_ADDR;
1944 else
1945 return (unsigned long)FTRACE_ADDR;
1946}
1947
Steven Rostedtc88fd862011-08-16 09:53:39 -04001948static int
1949__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1950{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001951 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001952 unsigned long ftrace_addr;
1953 int ret;
1954
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001955 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001956
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001957 /* This needs to be done before we call ftrace_update_record */
1958 ftrace_old_addr = ftrace_get_addr_curr(rec);
1959
1960 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001961
Steven Rostedtc88fd862011-08-16 09:53:39 -04001962 switch (ret) {
1963 case FTRACE_UPDATE_IGNORE:
1964 return 0;
1965
1966 case FTRACE_UPDATE_MAKE_CALL:
1967 return ftrace_make_call(rec, ftrace_addr);
1968
1969 case FTRACE_UPDATE_MAKE_NOP:
1970 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001971
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001972 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001973 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001974 }
1975
1976 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001977}
1978
Steven Rostedte4f5d542012-04-27 09:13:18 -04001979void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001980{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001981 struct dyn_ftrace *rec;
1982 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001983 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001984
Steven Rostedt45a4a232011-04-21 23:16:46 -04001985 if (unlikely(ftrace_disabled))
1986 return;
1987
Steven Rostedt265c8312009-02-13 12:43:56 -05001988 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001989 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001990 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001991 ftrace_bug(failed, rec->ip);
1992 /* Stop processing */
1993 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001994 }
1995 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001996}
1997
Steven Rostedtc88fd862011-08-16 09:53:39 -04001998struct ftrace_rec_iter {
1999 struct ftrace_page *pg;
2000 int index;
2001};
2002
2003/**
2004 * ftrace_rec_iter_start, start up iterating over traced functions
2005 *
2006 * Returns an iterator handle that is used to iterate over all
2007 * the records that represent address locations where functions
2008 * are traced.
2009 *
2010 * May return NULL if no records are available.
2011 */
2012struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2013{
2014 /*
2015 * We only use a single iterator.
2016 * Protected by the ftrace_lock mutex.
2017 */
2018 static struct ftrace_rec_iter ftrace_rec_iter;
2019 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2020
2021 iter->pg = ftrace_pages_start;
2022 iter->index = 0;
2023
2024 /* Could have empty pages */
2025 while (iter->pg && !iter->pg->index)
2026 iter->pg = iter->pg->next;
2027
2028 if (!iter->pg)
2029 return NULL;
2030
2031 return iter;
2032}
2033
2034/**
2035 * ftrace_rec_iter_next, get the next record to process.
2036 * @iter: The handle to the iterator.
2037 *
2038 * Returns the next iterator after the given iterator @iter.
2039 */
2040struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2041{
2042 iter->index++;
2043
2044 if (iter->index >= iter->pg->index) {
2045 iter->pg = iter->pg->next;
2046 iter->index = 0;
2047
2048 /* Could have empty pages */
2049 while (iter->pg && !iter->pg->index)
2050 iter->pg = iter->pg->next;
2051 }
2052
2053 if (!iter->pg)
2054 return NULL;
2055
2056 return iter;
2057}
2058
2059/**
2060 * ftrace_rec_iter_record, get the record at the iterator location
2061 * @iter: The current iterator location
2062 *
2063 * Returns the record that the current @iter is at.
2064 */
2065struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2066{
2067 return &iter->pg->records[iter->index];
2068}
2069
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302070static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002071ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002072{
2073 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002074 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002075
2076 ip = rec->ip;
2077
Steven Rostedt45a4a232011-04-21 23:16:46 -04002078 if (unlikely(ftrace_disabled))
2079 return 0;
2080
Shaohua Li25aac9d2009-01-09 11:29:40 +08002081 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002082 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002083 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302084 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002085 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302086 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002087}
2088
Steven Rostedt000ab692009-02-17 13:35:06 -05002089/*
2090 * archs can override this function if they must do something
2091 * before the modifying code is performed.
2092 */
2093int __weak ftrace_arch_code_modify_prepare(void)
2094{
2095 return 0;
2096}
2097
2098/*
2099 * archs can override this function if they must do something
2100 * after the modifying code is performed.
2101 */
2102int __weak ftrace_arch_code_modify_post_process(void)
2103{
2104 return 0;
2105}
2106
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002107void ftrace_modify_all_code(int command)
2108{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002109 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002110 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002111
2112 /*
2113 * If the ftrace_caller calls a ftrace_ops func directly,
2114 * we need to make sure that it only traces functions it
2115 * expects to trace. When doing the switch of functions,
2116 * we need to update to the ftrace_ops_list_func first
2117 * before the transition between old and new calls are set,
2118 * as the ftrace_ops_list_func will check the ops hashes
2119 * to make sure the ops are having the right functions
2120 * traced.
2121 */
Petr Mladekcd210672014-02-24 17:12:21 +01002122 if (update) {
2123 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2124 if (FTRACE_WARN_ON(err))
2125 return;
2126 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002127
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002128 if (command & FTRACE_UPDATE_CALLS)
2129 ftrace_replace_code(1);
2130 else if (command & FTRACE_DISABLE_CALLS)
2131 ftrace_replace_code(0);
2132
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002133 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2134 function_trace_op = set_function_trace_op;
2135 smp_wmb();
2136 /* If irqs are disabled, we are in stop machine */
2137 if (!irqs_disabled())
2138 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002139 err = ftrace_update_ftrace_func(ftrace_trace_function);
2140 if (FTRACE_WARN_ON(err))
2141 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002142 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002143
2144 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002145 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002146 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002147 err = ftrace_disable_ftrace_graph_caller();
2148 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002149}
2150
Ingo Molnare309b412008-05-12 21:20:51 +02002151static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002152{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002153 int *command = data;
2154
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002155 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002156
Steven Rostedtc88fd862011-08-16 09:53:39 -04002157 return 0;
2158}
2159
2160/**
2161 * ftrace_run_stop_machine, go back to the stop machine method
2162 * @command: The command to tell ftrace what to do
2163 *
2164 * If an arch needs to fall back to the stop machine method, the
2165 * it can call this function.
2166 */
2167void ftrace_run_stop_machine(int command)
2168{
2169 stop_machine(__ftrace_modify_code, &command, NULL);
2170}
2171
2172/**
2173 * arch_ftrace_update_code, modify the code to trace or not trace
2174 * @command: The command that needs to be done
2175 *
2176 * Archs can override this function if it does not need to
2177 * run stop_machine() to modify code.
2178 */
2179void __weak arch_ftrace_update_code(int command)
2180{
2181 ftrace_run_stop_machine(command);
2182}
2183
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002184static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2185{
2186 struct ftrace_page *pg;
2187 struct dyn_ftrace *rec;
2188 int size, bits;
2189 int ret;
2190
2191 size = ops->trampolines;
2192 bits = 0;
2193 /*
2194 * Make the hash size about 1/2 the # found
2195 */
2196 for (size /= 2; size; size >>= 1)
2197 bits++;
2198
2199 ops->tramp_hash = alloc_ftrace_hash(bits);
2200 /*
2201 * TODO: a failed allocation is going to screw up
2202 * the accounting of what needs to be modified
2203 * and not. For now, we kill ftrace if we fail
2204 * to allocate here. But there are ways around this,
2205 * but that will take a little more work.
2206 */
2207 if (!ops->tramp_hash)
2208 return -ENOMEM;
2209
2210 do_for_each_ftrace_rec(pg, rec) {
2211 if (ftrace_rec_count(rec) == 1 &&
2212 ftrace_ops_test(ops, rec->ip, rec)) {
2213
2214 /* This record had better have a trampoline */
2215 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2216 return -1;
2217
2218 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2219 if (ret < 0)
2220 return ret;
2221 }
2222 } while_for_each_ftrace_rec();
2223
2224 return 0;
2225}
2226
2227static int ftrace_save_tramp_hashes(void)
2228{
2229 struct ftrace_ops *op;
2230 int ret;
2231
2232 /*
2233 * Now that any trampoline is being used, we need to save the
2234 * hashes for the ops that have them. This allows the mapping
2235 * back from the record to the ops that has the trampoline to
2236 * know what code is being replaced. Modifying code must always
2237 * verify what it is changing.
2238 */
2239 do_for_each_ftrace_op(op, ftrace_ops_list) {
2240
2241 /* The tramp_hash is recreated each time. */
2242 free_ftrace_hash(op->tramp_hash);
2243 op->tramp_hash = NULL;
2244
2245 if (op->trampolines) {
2246 ret = ftrace_save_ops_tramp_hash(op);
2247 if (ret)
2248 return ret;
2249 }
2250
2251 } while_for_each_ftrace_op(op);
2252
2253 return 0;
2254}
2255
Steven Rostedtc88fd862011-08-16 09:53:39 -04002256static void ftrace_run_update_code(int command)
2257{
2258 int ret;
2259
2260 ret = ftrace_arch_code_modify_prepare();
2261 FTRACE_WARN_ON(ret);
2262 if (ret)
2263 return;
2264 /*
2265 * Do not call function tracer while we update the code.
2266 * We are in stop machine.
2267 */
2268 function_trace_stop++;
2269
2270 /*
2271 * By default we use stop_machine() to modify the code.
2272 * But archs can do what ever they want as long as it
2273 * is safe. The stop_machine() is the safest, but also
2274 * produces the most overhead.
2275 */
2276 arch_ftrace_update_code(command);
2277
Steven Rostedt6331c282011-07-13 15:11:02 -04002278 function_trace_stop--;
2279
Steven Rostedt000ab692009-02-17 13:35:06 -05002280 ret = ftrace_arch_code_modify_post_process();
2281 FTRACE_WARN_ON(ret);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002282
2283 ret = ftrace_save_tramp_hashes();
2284 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002285}
2286
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002287static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002288static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002289
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002290static void control_ops_free(struct ftrace_ops *ops)
2291{
2292 free_percpu(ops->disabled);
2293}
2294
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002295static void ftrace_startup_enable(int command)
2296{
2297 if (saved_ftrace_func != ftrace_trace_function) {
2298 saved_ftrace_func = ftrace_trace_function;
2299 command |= FTRACE_UPDATE_TRACE_FUNC;
2300 }
2301
2302 if (!command || !ftrace_enabled)
2303 return;
2304
2305 ftrace_run_update_code(command);
2306}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002307
Steven Rostedta1cd6172011-05-23 15:24:25 -04002308static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002309{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002310 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002311
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002312 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002313 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002314
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002315 ret = __register_ftrace_function(ops);
2316 if (ret)
2317 return ret;
2318
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002319 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002320 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002321
Steven Rostedted926f92011-05-03 13:25:24 -04002322 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002323
2324 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002325
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002326 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002327
2328 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002329}
2330
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002331static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002332{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002333 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002334
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002335 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002336 return -ENODEV;
2337
2338 ret = __unregister_ftrace_function(ops);
2339 if (ret)
2340 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002341
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002342 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002343 /*
2344 * Just warn in case of unbalance, no need to kill ftrace, it's not
2345 * critical but the ftrace_call callers may be never nopped again after
2346 * further ftrace uses.
2347 */
2348 WARN_ON_ONCE(ftrace_start_up < 0);
2349
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002350 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002351
Namhyung Kima737e6d2014-06-12 23:56:12 +09002352 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002353
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002354 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002355
2356 if (saved_ftrace_func != ftrace_trace_function) {
2357 saved_ftrace_func = ftrace_trace_function;
2358 command |= FTRACE_UPDATE_TRACE_FUNC;
2359 }
2360
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002361 if (!command || !ftrace_enabled) {
2362 /*
2363 * If these are control ops, they still need their
2364 * per_cpu field freed. Since, function tracing is
2365 * not currently active, we can just free them
2366 * without synchronizing all CPUs.
2367 */
2368 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2369 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002370 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002371 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002372
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002373 /*
2374 * If the ops uses a trampoline, then it needs to be
2375 * tested first on update.
2376 */
2377 removed_ops = ops;
2378
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002379 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002380
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002381 removed_ops = NULL;
2382
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002383 /*
2384 * Dynamic ops may be freed, we must make sure that all
2385 * callers are done before leaving this function.
2386 * The same goes for freeing the per_cpu data of the control
2387 * ops.
2388 *
2389 * Again, normal synchronize_sched() is not good enough.
2390 * We need to do a hard force of sched synchronization.
2391 * This is because we use preempt_disable() to do RCU, but
2392 * the function tracers can be called where RCU is not watching
2393 * (like before user_exit()). We can not rely on the RCU
2394 * infrastructure to do the synchronization, thus we must do it
2395 * ourselves.
2396 */
2397 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2398 schedule_on_each_cpu(ftrace_sync);
2399
2400 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2401 control_ops_free(ops);
2402 }
2403
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002404 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002405}
2406
Ingo Molnare309b412008-05-12 21:20:51 +02002407static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002408{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002409 if (unlikely(ftrace_disabled))
2410 return;
2411
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002412 /* Force update next time */
2413 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002414 /* ftrace_start_up is true if we want ftrace running */
2415 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002416 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002417}
2418
Ingo Molnare309b412008-05-12 21:20:51 +02002419static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002420{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002421 if (unlikely(ftrace_disabled))
2422 return;
2423
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002424 /* ftrace_start_up is true if ftrace is running */
2425 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002426 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002427}
2428
Steven Rostedt3d083392008-05-12 21:20:42 +02002429static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002430unsigned long ftrace_update_tot_cnt;
2431
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002432static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002433{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002434 /*
2435 * Filter_hash being empty will default to trace module.
2436 * But notrace hash requires a test of individual module functions.
2437 */
2438 return ftrace_hash_empty(ops->filter_hash) &&
2439 ftrace_hash_empty(ops->notrace_hash);
2440}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002441
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002442/*
2443 * Check if the current ops references the record.
2444 *
2445 * If the ops traces all functions, then it was already accounted for.
2446 * If the ops does not trace the current record function, skip it.
2447 * If the ops ignores the function via notrace filter, skip it.
2448 */
2449static inline bool
2450ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2451{
2452 /* If ops isn't enabled, ignore it */
2453 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2454 return 0;
2455
2456 /* If ops traces all mods, we already accounted for it */
2457 if (ops_traces_mod(ops))
2458 return 0;
2459
2460 /* The function must be in the filter */
2461 if (!ftrace_hash_empty(ops->filter_hash) &&
2462 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2463 return 0;
2464
2465 /* If in notrace hash, we ignore it too */
2466 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2467 return 0;
2468
2469 return 1;
2470}
2471
2472static int referenced_filters(struct dyn_ftrace *rec)
2473{
2474 struct ftrace_ops *ops;
2475 int cnt = 0;
2476
2477 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2478 if (ops_references_rec(ops, rec))
2479 cnt++;
2480 }
2481
2482 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002483}
2484
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002485static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002486{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002487 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002488 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302489 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002490 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002491 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002492 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002493 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002494
2495 /*
2496 * When adding a module, we need to check if tracers are
2497 * currently enabled and if they are set to trace all functions.
2498 * If they are, we need to enable the module functions as well
2499 * as update the reference counts for those function records.
2500 */
2501 if (mod) {
2502 struct ftrace_ops *ops;
2503
2504 for (ops = ftrace_ops_list;
2505 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002506 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2507 if (ops_traces_mod(ops))
2508 ref++;
2509 else
2510 test = true;
2511 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002512 }
2513 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002514
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002515 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002516
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002517 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302518
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002519 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002520 int cnt = ref;
2521
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002522 /* If something went wrong, bail without enabling anything */
2523 if (unlikely(ftrace_disabled))
2524 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002525
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002526 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002527 if (test)
2528 cnt += referenced_filters(p);
2529 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302530
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002531 /*
2532 * Do the initial record conversion from mcount jump
2533 * to the NOP instructions.
2534 */
2535 if (!ftrace_code_disable(mod, p))
2536 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002537
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002538 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002539
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002540 /*
2541 * If the tracing is enabled, go ahead and enable the record.
2542 *
2543 * The reason not to enable the record immediatelly is the
2544 * inherent check of ftrace_make_nop/ftrace_make_call for
2545 * correct previous instructions. Making first the NOP
2546 * conversion puts the module to the correct state, thus
2547 * passing the ftrace_make_call check.
2548 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002549 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002550 int failed = __ftrace_replace_code(p, 1);
2551 if (failed)
2552 ftrace_bug(failed, p->ip);
2553 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002554 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002555 }
2556
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002557 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002558 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002559 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002560
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002561 return 0;
2562}
2563
Steven Rostedta7900872011-12-16 16:23:44 -05002564static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002565{
Steven Rostedta7900872011-12-16 16:23:44 -05002566 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002567 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002568
Steven Rostedta7900872011-12-16 16:23:44 -05002569 if (WARN_ON(!count))
2570 return -EINVAL;
2571
2572 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002573
2574 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002575 * We want to fill as much as possible. No more than a page
2576 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002577 */
Steven Rostedta7900872011-12-16 16:23:44 -05002578 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2579 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002580
Steven Rostedta7900872011-12-16 16:23:44 -05002581 again:
2582 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2583
2584 if (!pg->records) {
2585 /* if we can't allocate this size, try something smaller */
2586 if (!order)
2587 return -ENOMEM;
2588 order >>= 1;
2589 goto again;
2590 }
2591
2592 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2593 pg->size = cnt;
2594
2595 if (cnt > count)
2596 cnt = count;
2597
2598 return cnt;
2599}
2600
2601static struct ftrace_page *
2602ftrace_allocate_pages(unsigned long num_to_init)
2603{
2604 struct ftrace_page *start_pg;
2605 struct ftrace_page *pg;
2606 int order;
2607 int cnt;
2608
2609 if (!num_to_init)
2610 return 0;
2611
2612 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2613 if (!pg)
2614 return NULL;
2615
2616 /*
2617 * Try to allocate as much as possible in one continues
2618 * location that fills in all of the space. We want to
2619 * waste as little space as possible.
2620 */
2621 for (;;) {
2622 cnt = ftrace_allocate_records(pg, num_to_init);
2623 if (cnt < 0)
2624 goto free_pages;
2625
2626 num_to_init -= cnt;
2627 if (!num_to_init)
2628 break;
2629
2630 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2631 if (!pg->next)
2632 goto free_pages;
2633
2634 pg = pg->next;
2635 }
2636
2637 return start_pg;
2638
2639 free_pages:
2640 while (start_pg) {
2641 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2642 free_pages((unsigned long)pg->records, order);
2643 start_pg = pg->next;
2644 kfree(pg);
2645 pg = start_pg;
2646 }
2647 pr_info("ftrace: FAILED to allocate memory for functions\n");
2648 return NULL;
2649}
2650
Steven Rostedt5072c592008-05-12 21:20:43 +02002651#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2652
2653struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002654 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002655 loff_t func_pos;
2656 struct ftrace_page *pg;
2657 struct dyn_ftrace *func;
2658 struct ftrace_func_probe *probe;
2659 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002660 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002661 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002662 int hidx;
2663 int idx;
2664 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002665};
2666
Ingo Molnare309b412008-05-12 21:20:51 +02002667static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002668t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002669{
2670 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002671 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002672 struct hlist_head *hhd;
2673
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002674 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002675 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002676
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002677 if (iter->probe)
2678 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002679 retry:
2680 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2681 return NULL;
2682
2683 hhd = &ftrace_func_hash[iter->hidx];
2684
2685 if (hlist_empty(hhd)) {
2686 iter->hidx++;
2687 hnd = NULL;
2688 goto retry;
2689 }
2690
2691 if (!hnd)
2692 hnd = hhd->first;
2693 else {
2694 hnd = hnd->next;
2695 if (!hnd) {
2696 iter->hidx++;
2697 goto retry;
2698 }
2699 }
2700
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002701 if (WARN_ON_ONCE(!hnd))
2702 return NULL;
2703
2704 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2705
2706 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002707}
2708
2709static void *t_hash_start(struct seq_file *m, loff_t *pos)
2710{
2711 struct ftrace_iterator *iter = m->private;
2712 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002713 loff_t l;
2714
Steven Rostedt69a30832011-12-19 15:21:16 -05002715 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2716 return NULL;
2717
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002718 if (iter->func_pos > *pos)
2719 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002720
Li Zefand82d6242009-06-24 09:54:54 +08002721 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002722 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002723 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002724 if (!p)
2725 break;
2726 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002727 if (!p)
2728 return NULL;
2729
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002730 /* Only set this if we have an item */
2731 iter->flags |= FTRACE_ITER_HASH;
2732
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002733 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002734}
2735
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002736static int
2737t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002738{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002739 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002740
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002741 rec = iter->probe;
2742 if (WARN_ON_ONCE(!rec))
2743 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002744
Steven Rostedt809dcf22009-02-16 23:06:01 -05002745 if (rec->ops->print)
2746 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2747
Steven Rostedtb375a112009-09-17 00:05:58 -04002748 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002749
2750 if (rec->data)
2751 seq_printf(m, ":%p", rec->data);
2752 seq_putc(m, '\n');
2753
2754 return 0;
2755}
2756
2757static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002758t_next(struct seq_file *m, void *v, loff_t *pos)
2759{
2760 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002761 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002762 struct dyn_ftrace *rec = NULL;
2763
Steven Rostedt45a4a232011-04-21 23:16:46 -04002764 if (unlikely(ftrace_disabled))
2765 return NULL;
2766
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002767 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002768 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002769
Steven Rostedt5072c592008-05-12 21:20:43 +02002770 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002771 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002772
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002773 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002774 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002775
Steven Rostedt5072c592008-05-12 21:20:43 +02002776 retry:
2777 if (iter->idx >= iter->pg->index) {
2778 if (iter->pg->next) {
2779 iter->pg = iter->pg->next;
2780 iter->idx = 0;
2781 goto retry;
2782 }
2783 } else {
2784 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002785 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002786 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002787
Steven Rostedt41c52c02008-05-22 11:46:33 -04002788 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002789 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2790
2791 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002792 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002793
Steven Rostedt5072c592008-05-12 21:20:43 +02002794 rec = NULL;
2795 goto retry;
2796 }
2797 }
2798
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002799 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002800 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002801
2802 iter->func = rec;
2803
2804 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002805}
2806
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002807static void reset_iter_read(struct ftrace_iterator *iter)
2808{
2809 iter->pos = 0;
2810 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002811 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002812}
2813
2814static void *t_start(struct seq_file *m, loff_t *pos)
2815{
2816 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002817 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002818 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002819 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002820
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002821 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002822
2823 if (unlikely(ftrace_disabled))
2824 return NULL;
2825
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002826 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002827 * If an lseek was done, then reset and start from beginning.
2828 */
2829 if (*pos < iter->pos)
2830 reset_iter_read(iter);
2831
2832 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002833 * For set_ftrace_filter reading, if we have the filter
2834 * off, we can short cut and just print out that all
2835 * functions are enabled.
2836 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002837 if (iter->flags & FTRACE_ITER_FILTER &&
2838 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002839 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002840 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002841 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002842 /* reset in case of seek/pread */
2843 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002844 return iter;
2845 }
2846
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002847 if (iter->flags & FTRACE_ITER_HASH)
2848 return t_hash_start(m, pos);
2849
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002850 /*
2851 * Unfortunately, we need to restart at ftrace_pages_start
2852 * every time we let go of the ftrace_mutex. This is because
2853 * those pointers can change without the lock.
2854 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002855 iter->pg = ftrace_pages_start;
2856 iter->idx = 0;
2857 for (l = 0; l <= *pos; ) {
2858 p = t_next(m, p, &l);
2859 if (!p)
2860 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002861 }
walimis5821e1b2008-11-15 15:19:06 +08002862
Steven Rostedt69a30832011-12-19 15:21:16 -05002863 if (!p)
2864 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002865
2866 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002867}
2868
2869static void t_stop(struct seq_file *m, void *p)
2870{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002871 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002872}
2873
2874static int t_show(struct seq_file *m, void *v)
2875{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002876 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002877 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002878
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002879 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002880 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002881
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002882 if (iter->flags & FTRACE_ITER_PRINTALL) {
2883 seq_printf(m, "#### all functions enabled ####\n");
2884 return 0;
2885 }
2886
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002887 rec = iter->func;
2888
Steven Rostedt5072c592008-05-12 21:20:43 +02002889 if (!rec)
2890 return 0;
2891
Steven Rostedt647bcd02011-05-03 14:39:21 -04002892 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002893 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002894 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002895 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002896 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2897 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2898 struct ftrace_ops *ops;
2899
2900 ops = ftrace_find_tramp_ops_curr(rec);
2901 if (ops && ops->trampoline)
2902 seq_printf(m, "\ttramp: %pS",
2903 (void *)ops->trampoline);
2904 else
2905 seq_printf(m, "\ttramp: ERROR!");
2906 }
2907 }
2908
Steven Rostedt647bcd02011-05-03 14:39:21 -04002909 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002910
2911 return 0;
2912}
2913
James Morris88e9d342009-09-22 16:43:43 -07002914static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002915 .start = t_start,
2916 .next = t_next,
2917 .stop = t_stop,
2918 .show = t_show,
2919};
2920
Ingo Molnare309b412008-05-12 21:20:51 +02002921static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002922ftrace_avail_open(struct inode *inode, struct file *file)
2923{
2924 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002925
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002926 if (unlikely(ftrace_disabled))
2927 return -ENODEV;
2928
Jiri Olsa50e18b92012-04-25 10:23:39 +02002929 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2930 if (iter) {
2931 iter->pg = ftrace_pages_start;
2932 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002933 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002934
Jiri Olsa50e18b92012-04-25 10:23:39 +02002935 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002936}
2937
Steven Rostedt647bcd02011-05-03 14:39:21 -04002938static int
2939ftrace_enabled_open(struct inode *inode, struct file *file)
2940{
2941 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002942
2943 if (unlikely(ftrace_disabled))
2944 return -ENODEV;
2945
Jiri Olsa50e18b92012-04-25 10:23:39 +02002946 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2947 if (iter) {
2948 iter->pg = ftrace_pages_start;
2949 iter->flags = FTRACE_ITER_ENABLED;
2950 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002951 }
2952
Jiri Olsa50e18b92012-04-25 10:23:39 +02002953 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002954}
2955
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002956static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002957{
Steven Rostedt52baf112009-02-14 01:15:39 -05002958 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002959 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002960 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002961}
2962
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002963/**
2964 * ftrace_regex_open - initialize function tracer filter files
2965 * @ops: The ftrace_ops that hold the hash filters
2966 * @flag: The type of filter to process
2967 * @inode: The inode, usually passed in to your open routine
2968 * @file: The file, usually passed in to your open routine
2969 *
2970 * ftrace_regex_open() initializes the filter files for the
2971 * @ops. Depending on @flag it may process the filter hash or
2972 * the notrace hash of @ops. With this called from the open
2973 * routine, you can use ftrace_filter_write() for the write
2974 * routine if @flag has FTRACE_ITER_FILTER set, or
2975 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002976 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002977 * release must call ftrace_regex_release().
2978 */
2979int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002980ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002981 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002982{
2983 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002984 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002985 int ret = 0;
2986
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002987 ftrace_ops_init(ops);
2988
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002989 if (unlikely(ftrace_disabled))
2990 return -ENODEV;
2991
Steven Rostedt5072c592008-05-12 21:20:43 +02002992 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2993 if (!iter)
2994 return -ENOMEM;
2995
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002996 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2997 kfree(iter);
2998 return -ENOMEM;
2999 }
3000
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003001 iter->ops = ops;
3002 iter->flags = flag;
3003
3004 mutex_lock(&ops->regex_lock);
3005
Steven Rostedtf45948e2011-05-02 12:29:25 -04003006 if (flag & FTRACE_ITER_NOTRACE)
3007 hash = ops->notrace_hash;
3008 else
3009 hash = ops->filter_hash;
3010
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003011 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003012 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003013 if (!iter->hash) {
3014 trace_parser_put(&iter->parser);
3015 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003016 ret = -ENOMEM;
3017 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003018 }
3019 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003020
Steven Rostedt5072c592008-05-12 21:20:43 +02003021 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003022 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003023 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02003024
3025 if (file->f_mode & FMODE_READ) {
3026 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003027
3028 ret = seq_open(file, &show_ftrace_seq_ops);
3029 if (!ret) {
3030 struct seq_file *m = file->private_data;
3031 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003032 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003033 /* Failed */
3034 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003035 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003036 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003037 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003038 } else
3039 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003040
3041 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003042 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003043
3044 return ret;
3045}
3046
Steven Rostedt41c52c02008-05-22 11:46:33 -04003047static int
3048ftrace_filter_open(struct inode *inode, struct file *file)
3049{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003050 struct ftrace_ops *ops = inode->i_private;
3051
3052 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003053 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3054 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003055}
3056
3057static int
3058ftrace_notrace_open(struct inode *inode, struct file *file)
3059{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003060 struct ftrace_ops *ops = inode->i_private;
3061
3062 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003063 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003064}
3065
Steven Rostedt64e7c442009-02-13 17:08:48 -05003066static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003067{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003068 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003069 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003070
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003071 switch (type) {
3072 case MATCH_FULL:
3073 if (strcmp(str, regex) == 0)
3074 matched = 1;
3075 break;
3076 case MATCH_FRONT_ONLY:
3077 if (strncmp(str, regex, len) == 0)
3078 matched = 1;
3079 break;
3080 case MATCH_MIDDLE_ONLY:
3081 if (strstr(str, regex))
3082 matched = 1;
3083 break;
3084 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003085 slen = strlen(str);
3086 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003087 matched = 1;
3088 break;
3089 }
3090
3091 return matched;
3092}
3093
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003094static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003095enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003096{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003097 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003098 int ret = 0;
3099
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003100 entry = ftrace_lookup_ip(hash, rec->ip);
3101 if (not) {
3102 /* Do nothing if it doesn't exist */
3103 if (!entry)
3104 return 0;
3105
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003106 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003107 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003108 /* Do nothing if it exists */
3109 if (entry)
3110 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003111
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003112 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003113 }
3114 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003115}
3116
Steven Rostedt64e7c442009-02-13 17:08:48 -05003117static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003118ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3119 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003120{
3121 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003122 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003123
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003124 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3125
3126 if (mod) {
3127 /* module lookup requires matching the module */
3128 if (!modname || strcmp(modname, mod))
3129 return 0;
3130
3131 /* blank search means to match all funcs in the mod */
3132 if (!len)
3133 return 1;
3134 }
3135
Steven Rostedt64e7c442009-02-13 17:08:48 -05003136 return ftrace_match(str, regex, len, type);
3137}
3138
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003139static int
3140match_records(struct ftrace_hash *hash, char *buff,
3141 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003142{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003143 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003144 struct ftrace_page *pg;
3145 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003146 int type = MATCH_FULL;
3147 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003148 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003149 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003150
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003151 if (len) {
3152 type = filter_parse_regex(buff, len, &search, &not);
3153 search_len = strlen(search);
3154 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003155
Steven Rostedt52baf112009-02-14 01:15:39 -05003156 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003157
3158 if (unlikely(ftrace_disabled))
3159 goto out_unlock;
3160
Steven Rostedt265c8312009-02-13 12:43:56 -05003161 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003162 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003163 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003164 if (ret < 0) {
3165 found = ret;
3166 goto out_unlock;
3167 }
Li Zefan311d16d2009-12-08 11:15:11 +08003168 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003169 }
3170 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003171 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003172 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003173
3174 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003175}
3176
Steven Rostedt64e7c442009-02-13 17:08:48 -05003177static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003178ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003179{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003180 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003181}
3182
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003183static int
3184ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003185{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003186 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003187
Steven Rostedt64e7c442009-02-13 17:08:48 -05003188 /* blank or '*' mean the same */
3189 if (strcmp(buff, "*") == 0)
3190 buff[0] = 0;
3191
3192 /* handle the case of 'dont filter this module' */
3193 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3194 buff[0] = 0;
3195 not = 1;
3196 }
3197
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003198 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003199}
3200
Steven Rostedtf6180772009-02-14 00:40:25 -05003201/*
3202 * We register the module command as a template to show others how
3203 * to register the a command as well.
3204 */
3205
3206static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003207ftrace_mod_callback(struct ftrace_hash *hash,
3208 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003209{
3210 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003211 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003212
3213 /*
3214 * cmd == 'mod' because we only registered this func
3215 * for the 'mod' ftrace_func_command.
3216 * But if you register one func with multiple commands,
3217 * you can tell which command was used by the cmd
3218 * parameter.
3219 */
3220
3221 /* we must have a module name */
3222 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003223 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003224
3225 mod = strsep(&param, ":");
3226 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003227 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003228
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003229 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003230 if (!ret)
3231 ret = -EINVAL;
3232 if (ret < 0)
3233 return ret;
3234
3235 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003236}
3237
3238static struct ftrace_func_command ftrace_mod_cmd = {
3239 .name = "mod",
3240 .func = ftrace_mod_callback,
3241};
3242
3243static int __init ftrace_mod_cmd_init(void)
3244{
3245 return register_ftrace_command(&ftrace_mod_cmd);
3246}
Steven Rostedt6f415672012-10-05 12:13:07 -04003247core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003248
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003249static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003250 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003251{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003252 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003253 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003254 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003255
3256 key = hash_long(ip, FTRACE_HASH_BITS);
3257
3258 hhd = &ftrace_func_hash[key];
3259
3260 if (hlist_empty(hhd))
3261 return;
3262
3263 /*
3264 * Disable preemption for these calls to prevent a RCU grace
3265 * period. This syncs the hash iteration and freeing of items
3266 * on the hash. rcu_read_lock is too dangerous here.
3267 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003268 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003269 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003270 if (entry->ip == ip)
3271 entry->ops->func(ip, parent_ip, &entry->data);
3272 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003273 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003274}
3275
Steven Rostedtb6887d72009-02-17 12:32:04 -05003276static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003277{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003278 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003279 .flags = FTRACE_OPS_FL_INITIALIZED,
3280 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003281};
3282
Steven Rostedtb6887d72009-02-17 12:32:04 -05003283static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003284
Steven Rostedtb6887d72009-02-17 12:32:04 -05003285static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003286{
Steven Rostedtb8489142011-05-04 09:27:52 -04003287 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003288 int i;
3289
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003290 if (ftrace_probe_registered) {
3291 /* still need to update the function call sites */
3292 if (ftrace_enabled)
3293 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003294 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003295 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003296
3297 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3298 struct hlist_head *hhd = &ftrace_func_hash[i];
3299 if (hhd->first)
3300 break;
3301 }
3302 /* Nothing registered? */
3303 if (i == FTRACE_FUNC_HASHSIZE)
3304 return;
3305
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003306 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003307
Steven Rostedtb6887d72009-02-17 12:32:04 -05003308 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003309}
3310
Steven Rostedtb6887d72009-02-17 12:32:04 -05003311static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003312{
3313 int i;
3314
Steven Rostedtb6887d72009-02-17 12:32:04 -05003315 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003316 return;
3317
3318 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3319 struct hlist_head *hhd = &ftrace_func_hash[i];
3320 if (hhd->first)
3321 return;
3322 }
3323
3324 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003325 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003326
Steven Rostedtb6887d72009-02-17 12:32:04 -05003327 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003328}
3329
3330
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003331static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003332{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003333 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003334 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003335 kfree(entry);
3336}
3337
Steven Rostedt59df055f2009-02-14 15:29:06 -05003338int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003339register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003340 void *data)
3341{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003342 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003343 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3344 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003345 struct ftrace_page *pg;
3346 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003347 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003348 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003349 int count = 0;
3350 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003351 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003352
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003353 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003354 len = strlen(search);
3355
Steven Rostedtb6887d72009-02-17 12:32:04 -05003356 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003357 if (WARN_ON(not))
3358 return -EINVAL;
3359
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003360 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003361
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003362 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3363 if (!hash) {
3364 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003365 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003366 }
3367
3368 if (unlikely(ftrace_disabled)) {
3369 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003370 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003371 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003372
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003373 mutex_lock(&ftrace_lock);
3374
Steven Rostedt59df055f2009-02-14 15:29:06 -05003375 do_for_each_ftrace_rec(pg, rec) {
3376
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003377 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003378 continue;
3379
3380 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3381 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003382 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003383 if (!count)
3384 count = -ENOMEM;
3385 goto out_unlock;
3386 }
3387
3388 count++;
3389
3390 entry->data = data;
3391
3392 /*
3393 * The caller might want to do something special
3394 * for each function we find. We call the callback
3395 * to give the caller an opportunity to do so.
3396 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003397 if (ops->init) {
3398 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003399 /* caller does not like this func */
3400 kfree(entry);
3401 continue;
3402 }
3403 }
3404
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003405 ret = enter_record(hash, rec, 0);
3406 if (ret < 0) {
3407 kfree(entry);
3408 count = ret;
3409 goto out_unlock;
3410 }
3411
Steven Rostedt59df055f2009-02-14 15:29:06 -05003412 entry->ops = ops;
3413 entry->ip = rec->ip;
3414
3415 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3416 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3417
3418 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003419
3420 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3421 if (ret < 0)
3422 count = ret;
3423
Steven Rostedtb6887d72009-02-17 12:32:04 -05003424 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003425
3426 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003427 mutex_unlock(&ftrace_lock);
3428 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003429 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003430 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003431
3432 return count;
3433}
3434
3435enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003436 PROBE_TEST_FUNC = 1,
3437 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003438};
3439
3440static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003441__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003442 void *data, int flags)
3443{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003444 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003445 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003446 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003447 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003448 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003449 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003450 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003451 char str[KSYM_SYMBOL_LEN];
3452 int type = MATCH_FULL;
3453 int i, len = 0;
3454 char *search;
3455
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003456 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003457 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003458 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003459 int not;
3460
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003461 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003462 len = strlen(search);
3463
Steven Rostedtb6887d72009-02-17 12:32:04 -05003464 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003465 if (WARN_ON(not))
3466 return;
3467 }
3468
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003469 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003470
3471 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3472 if (!hash)
3473 /* Hmm, should report this somehow */
3474 goto out_unlock;
3475
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003476 INIT_LIST_HEAD(&free_list);
3477
Steven Rostedt59df055f2009-02-14 15:29:06 -05003478 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3479 struct hlist_head *hhd = &ftrace_func_hash[i];
3480
Sasha Levinb67bfe02013-02-27 17:06:00 -08003481 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003482
3483 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003484 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003485 continue;
3486
Steven Rostedtb6887d72009-02-17 12:32:04 -05003487 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003488 continue;
3489
3490 /* do this last, since it is the most expensive */
3491 if (glob) {
3492 kallsyms_lookup(entry->ip, NULL, NULL,
3493 NULL, str);
3494 if (!ftrace_match(str, glob, len, type))
3495 continue;
3496 }
3497
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003498 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3499 /* It is possible more than one entry had this ip */
3500 if (rec_entry)
3501 free_hash_entry(hash, rec_entry);
3502
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003503 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003504 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003505 }
3506 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003507 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003508 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003509 /*
3510 * Remove after the disable is called. Otherwise, if the last
3511 * probe is removed, a null hash means *all enabled*.
3512 */
3513 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003514 synchronize_sched();
3515 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3516 list_del(&entry->free_list);
3517 ftrace_free_entry(entry);
3518 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003519 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003520
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003521 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003522 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003523 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003524}
3525
3526void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003527unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003528 void *data)
3529{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003530 __unregister_ftrace_function_probe(glob, ops, data,
3531 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003532}
3533
3534void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003535unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003536{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003537 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003538}
3539
Steven Rostedtb6887d72009-02-17 12:32:04 -05003540void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003541{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003542 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003543}
3544
Steven Rostedtf6180772009-02-14 00:40:25 -05003545static LIST_HEAD(ftrace_commands);
3546static DEFINE_MUTEX(ftrace_cmd_mutex);
3547
Tom Zanussi38de93a2013-10-24 08:34:18 -05003548/*
3549 * Currently we only register ftrace commands from __init, so mark this
3550 * __init too.
3551 */
3552__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003553{
3554 struct ftrace_func_command *p;
3555 int ret = 0;
3556
3557 mutex_lock(&ftrace_cmd_mutex);
3558 list_for_each_entry(p, &ftrace_commands, list) {
3559 if (strcmp(cmd->name, p->name) == 0) {
3560 ret = -EBUSY;
3561 goto out_unlock;
3562 }
3563 }
3564 list_add(&cmd->list, &ftrace_commands);
3565 out_unlock:
3566 mutex_unlock(&ftrace_cmd_mutex);
3567
3568 return ret;
3569}
3570
Tom Zanussi38de93a2013-10-24 08:34:18 -05003571/*
3572 * Currently we only unregister ftrace commands from __init, so mark
3573 * this __init too.
3574 */
3575__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003576{
3577 struct ftrace_func_command *p, *n;
3578 int ret = -ENODEV;
3579
3580 mutex_lock(&ftrace_cmd_mutex);
3581 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3582 if (strcmp(cmd->name, p->name) == 0) {
3583 ret = 0;
3584 list_del_init(&p->list);
3585 goto out_unlock;
3586 }
3587 }
3588 out_unlock:
3589 mutex_unlock(&ftrace_cmd_mutex);
3590
3591 return ret;
3592}
3593
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003594static int ftrace_process_regex(struct ftrace_hash *hash,
3595 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003596{
Steven Rostedtf6180772009-02-14 00:40:25 -05003597 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003598 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003599 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003600
3601 func = strsep(&next, ":");
3602
3603 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003604 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003605 if (!ret)
3606 ret = -EINVAL;
3607 if (ret < 0)
3608 return ret;
3609 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003610 }
3611
Steven Rostedtf6180772009-02-14 00:40:25 -05003612 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003613
3614 command = strsep(&next, ":");
3615
Steven Rostedtf6180772009-02-14 00:40:25 -05003616 mutex_lock(&ftrace_cmd_mutex);
3617 list_for_each_entry(p, &ftrace_commands, list) {
3618 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003619 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003620 goto out_unlock;
3621 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003622 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003623 out_unlock:
3624 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003625
Steven Rostedtf6180772009-02-14 00:40:25 -05003626 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003627}
3628
Ingo Molnare309b412008-05-12 21:20:51 +02003629static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003630ftrace_regex_write(struct file *file, const char __user *ubuf,
3631 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003632{
3633 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003634 struct trace_parser *parser;
3635 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003636
Li Zefan4ba79782009-09-22 13:52:20 +08003637 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003638 return 0;
3639
Steven Rostedt5072c592008-05-12 21:20:43 +02003640 if (file->f_mode & FMODE_READ) {
3641 struct seq_file *m = file->private_data;
3642 iter = m->private;
3643 } else
3644 iter = file->private_data;
3645
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003646 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003647 return -ENODEV;
3648
3649 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003650
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003651 parser = &iter->parser;
3652 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003653
Li Zefan4ba79782009-09-22 13:52:20 +08003654 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003655 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003656 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003657 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003658 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003659 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003660 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003661 }
3662
Steven Rostedt5072c592008-05-12 21:20:43 +02003663 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003664 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003665 return ret;
3666}
3667
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003668ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003669ftrace_filter_write(struct file *file, const char __user *ubuf,
3670 size_t cnt, loff_t *ppos)
3671{
3672 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3673}
3674
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003675ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003676ftrace_notrace_write(struct file *file, const char __user *ubuf,
3677 size_t cnt, loff_t *ppos)
3678{
3679 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3680}
3681
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003682static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003683ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3684{
3685 struct ftrace_func_entry *entry;
3686
3687 if (!ftrace_location(ip))
3688 return -EINVAL;
3689
3690 if (remove) {
3691 entry = ftrace_lookup_ip(hash, ip);
3692 if (!entry)
3693 return -ENOENT;
3694 free_hash_entry(hash, entry);
3695 return 0;
3696 }
3697
3698 return add_hash_entry(hash, ip);
3699}
3700
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003701static void ftrace_ops_update_code(struct ftrace_ops *ops)
3702{
3703 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3704 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3705}
3706
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003707static int
3708ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3709 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003710{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003711 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003712 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003713 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003714
Steven Rostedt41c52c02008-05-22 11:46:33 -04003715 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003716 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003717
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003718 mutex_lock(&ops->regex_lock);
3719
Steven Rostedtf45948e2011-05-02 12:29:25 -04003720 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003721 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003722 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003723 orig_hash = &ops->notrace_hash;
3724
3725 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003726 if (!hash) {
3727 ret = -ENOMEM;
3728 goto out_regex_unlock;
3729 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003730
Steven Rostedt41c52c02008-05-22 11:46:33 -04003731 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003732 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003733 if (buf && !ftrace_match_records(hash, buf, len)) {
3734 ret = -EINVAL;
3735 goto out_regex_unlock;
3736 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003737 if (ip) {
3738 ret = ftrace_match_addr(hash, ip, remove);
3739 if (ret < 0)
3740 goto out_regex_unlock;
3741 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003742
3743 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003744 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003745 if (!ret)
3746 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003747
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003748 mutex_unlock(&ftrace_lock);
3749
Jiri Olsaac483c42012-01-02 10:04:14 +01003750 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003751 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003752
3753 free_ftrace_hash(hash);
3754 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003755}
3756
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003757static int
3758ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3759 int reset, int enable)
3760{
3761 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3762}
3763
3764/**
3765 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3766 * @ops - the ops to set the filter with
3767 * @ip - the address to add to or remove from the filter.
3768 * @remove - non zero to remove the ip from the filter
3769 * @reset - non zero to reset all filters before applying this filter.
3770 *
3771 * Filters denote which functions should be enabled when tracing is enabled
3772 * If @ip is NULL, it failes to update filter.
3773 */
3774int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3775 int remove, int reset)
3776{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003777 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003778 return ftrace_set_addr(ops, ip, remove, reset, 1);
3779}
3780EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3781
3782static int
3783ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3784 int reset, int enable)
3785{
3786 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3787}
3788
Steven Rostedt77a2b372008-05-12 21:20:45 +02003789/**
3790 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003791 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003792 * @buf - the string that holds the function filter text.
3793 * @len - the length of the string.
3794 * @reset - non zero to reset all filters before applying this filter.
3795 *
3796 * Filters denote which functions should be enabled when tracing is enabled.
3797 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3798 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003799int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003800 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003801{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003802 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003803 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003804}
Steven Rostedt936e0742011-05-05 22:54:01 -04003805EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003806
Steven Rostedt41c52c02008-05-22 11:46:33 -04003807/**
3808 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003809 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003810 * @buf - the string that holds the function notrace text.
3811 * @len - the length of the string.
3812 * @reset - non zero to reset all filters before applying this filter.
3813 *
3814 * Notrace Filters denote which functions should not be enabled when tracing
3815 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3816 * for tracing.
3817 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003818int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003819 int len, int reset)
3820{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003821 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003822 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003823}
3824EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3825/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003826 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003827 * @buf - the string that holds the function filter text.
3828 * @len - the length of the string.
3829 * @reset - non zero to reset all filters before applying this filter.
3830 *
3831 * Filters denote which functions should be enabled when tracing is enabled.
3832 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3833 */
3834void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3835{
3836 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3837}
3838EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3839
3840/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003841 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003842 * @buf - the string that holds the function notrace text.
3843 * @len - the length of the string.
3844 * @reset - non zero to reset all filters before applying this filter.
3845 *
3846 * Notrace Filters denote which functions should not be enabled when tracing
3847 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3848 * for tracing.
3849 */
3850void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003851{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003852 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003853}
Steven Rostedt936e0742011-05-05 22:54:01 -04003854EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003855
Steven Rostedt2af15d62009-05-28 13:37:24 -04003856/*
3857 * command line interface to allow users to set filters on boot up.
3858 */
3859#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3860static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3861static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3862
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003863/* Used by function selftest to not test if filter is set */
3864bool ftrace_filter_param __initdata;
3865
Steven Rostedt2af15d62009-05-28 13:37:24 -04003866static int __init set_ftrace_notrace(char *str)
3867{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003868 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003869 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003870 return 1;
3871}
3872__setup("ftrace_notrace=", set_ftrace_notrace);
3873
3874static int __init set_ftrace_filter(char *str)
3875{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003876 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003877 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003878 return 1;
3879}
3880__setup("ftrace_filter=", set_ftrace_filter);
3881
Stefan Assmann369bc182009-10-12 22:17:21 +02003882#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003883static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003884static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003885
Stefan Assmann369bc182009-10-12 22:17:21 +02003886static int __init set_graph_function(char *str)
3887{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003888 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003889 return 1;
3890}
3891__setup("ftrace_graph_filter=", set_graph_function);
3892
3893static void __init set_ftrace_early_graph(char *buf)
3894{
3895 int ret;
3896 char *func;
3897
3898 while (buf) {
3899 func = strsep(&buf, ",");
3900 /* we allow only one expression at a time */
3901 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003902 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003903 if (ret)
3904 printk(KERN_DEBUG "ftrace: function %s not "
3905 "traceable\n", func);
3906 }
3907}
3908#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3909
Steven Rostedt2a85a372011-12-19 21:57:44 -05003910void __init
3911ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003912{
3913 char *func;
3914
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003915 ftrace_ops_init(ops);
3916
Steven Rostedt2af15d62009-05-28 13:37:24 -04003917 while (buf) {
3918 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003919 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003920 }
3921}
3922
3923static void __init set_ftrace_early_filters(void)
3924{
3925 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003926 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003927 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003928 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003929#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3930 if (ftrace_graph_buf[0])
3931 set_ftrace_early_graph(ftrace_graph_buf);
3932#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003933}
3934
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003935int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003936{
3937 struct seq_file *m = (struct seq_file *)file->private_data;
3938 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003939 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003940 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003941 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003942 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003943
Steven Rostedt5072c592008-05-12 21:20:43 +02003944 if (file->f_mode & FMODE_READ) {
3945 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003946 seq_release(inode, file);
3947 } else
3948 iter = file->private_data;
3949
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003950 parser = &iter->parser;
3951 if (trace_parser_loaded(parser)) {
3952 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003953 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003954 }
3955
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003956 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003957
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003958 mutex_lock(&iter->ops->regex_lock);
3959
Steven Rostedt058e2972011-04-29 22:35:33 -04003960 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003961 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3962
3963 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003964 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003965 else
3966 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003967
Steven Rostedt058e2972011-04-29 22:35:33 -04003968 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003969 ret = ftrace_hash_move(iter->ops, filter_hash,
3970 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003971 if (!ret)
3972 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003973
Steven Rostedt058e2972011-04-29 22:35:33 -04003974 mutex_unlock(&ftrace_lock);
3975 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003976
3977 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003978 free_ftrace_hash(iter->hash);
3979 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003980
Steven Rostedt5072c592008-05-12 21:20:43 +02003981 return 0;
3982}
3983
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003984static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003985 .open = ftrace_avail_open,
3986 .read = seq_read,
3987 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003988 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003989};
3990
Steven Rostedt647bcd02011-05-03 14:39:21 -04003991static const struct file_operations ftrace_enabled_fops = {
3992 .open = ftrace_enabled_open,
3993 .read = seq_read,
3994 .llseek = seq_lseek,
3995 .release = seq_release_private,
3996};
3997
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003998static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003999 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004000 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004001 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004002 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004003 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004004};
4005
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004006static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004007 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004008 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004009 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004010 .llseek = tracing_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04004011 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004012};
4013
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004014#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4015
4016static DEFINE_MUTEX(graph_lock);
4017
4018int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004019int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004020unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004021unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004022
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004023struct ftrace_graph_data {
4024 unsigned long *table;
4025 size_t size;
4026 int *count;
4027 const struct seq_operations *seq_ops;
4028};
4029
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004030static void *
Li Zefan85951842009-06-24 09:54:00 +08004031__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004032{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004033 struct ftrace_graph_data *fgd = m->private;
4034
4035 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004036 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004037 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004038}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004039
Li Zefan85951842009-06-24 09:54:00 +08004040static void *
4041g_next(struct seq_file *m, void *v, loff_t *pos)
4042{
4043 (*pos)++;
4044 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004045}
4046
4047static void *g_start(struct seq_file *m, loff_t *pos)
4048{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004049 struct ftrace_graph_data *fgd = m->private;
4050
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004051 mutex_lock(&graph_lock);
4052
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004053 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004054 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004055 return (void *)1;
4056
Li Zefan85951842009-06-24 09:54:00 +08004057 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004058}
4059
4060static void g_stop(struct seq_file *m, void *p)
4061{
4062 mutex_unlock(&graph_lock);
4063}
4064
4065static int g_show(struct seq_file *m, void *v)
4066{
4067 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004068
4069 if (!ptr)
4070 return 0;
4071
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004072 if (ptr == (unsigned long *)1) {
4073 seq_printf(m, "#### all functions enabled ####\n");
4074 return 0;
4075 }
4076
Steven Rostedtb375a112009-09-17 00:05:58 -04004077 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004078
4079 return 0;
4080}
4081
James Morris88e9d342009-09-22 16:43:43 -07004082static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004083 .start = g_start,
4084 .next = g_next,
4085 .stop = g_stop,
4086 .show = g_show,
4087};
4088
4089static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004090__ftrace_graph_open(struct inode *inode, struct file *file,
4091 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004092{
4093 int ret = 0;
4094
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004095 mutex_lock(&graph_lock);
4096 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004097 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004098 *fgd->count = 0;
4099 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004100 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004101 mutex_unlock(&graph_lock);
4102
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004103 if (file->f_mode & FMODE_READ) {
4104 ret = seq_open(file, fgd->seq_ops);
4105 if (!ret) {
4106 struct seq_file *m = file->private_data;
4107 m->private = fgd;
4108 }
4109 } else
4110 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004111
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004112 return ret;
4113}
4114
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004115static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004116ftrace_graph_open(struct inode *inode, struct file *file)
4117{
4118 struct ftrace_graph_data *fgd;
4119
4120 if (unlikely(ftrace_disabled))
4121 return -ENODEV;
4122
4123 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4124 if (fgd == NULL)
4125 return -ENOMEM;
4126
4127 fgd->table = ftrace_graph_funcs;
4128 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4129 fgd->count = &ftrace_graph_count;
4130 fgd->seq_ops = &ftrace_graph_seq_ops;
4131
4132 return __ftrace_graph_open(inode, file, fgd);
4133}
4134
4135static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004136ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4137{
4138 struct ftrace_graph_data *fgd;
4139
4140 if (unlikely(ftrace_disabled))
4141 return -ENODEV;
4142
4143 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4144 if (fgd == NULL)
4145 return -ENOMEM;
4146
4147 fgd->table = ftrace_graph_notrace_funcs;
4148 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4149 fgd->count = &ftrace_graph_notrace_count;
4150 fgd->seq_ops = &ftrace_graph_seq_ops;
4151
4152 return __ftrace_graph_open(inode, file, fgd);
4153}
4154
4155static int
Li Zefan87827112009-07-23 11:29:11 +08004156ftrace_graph_release(struct inode *inode, struct file *file)
4157{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004158 if (file->f_mode & FMODE_READ) {
4159 struct seq_file *m = file->private_data;
4160
4161 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004162 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004163 } else {
4164 kfree(file->private_data);
4165 }
4166
Li Zefan87827112009-07-23 11:29:11 +08004167 return 0;
4168}
4169
4170static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004171ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004172{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004173 struct dyn_ftrace *rec;
4174 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004175 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004176 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004177 int type, not;
4178 char *search;
4179 bool exists;
4180 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004181
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004182 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004183 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004184 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004185 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004186
4187 search_len = strlen(search);
4188
Steven Rostedt52baf112009-02-14 01:15:39 -05004189 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004190
4191 if (unlikely(ftrace_disabled)) {
4192 mutex_unlock(&ftrace_lock);
4193 return -ENODEV;
4194 }
4195
Steven Rostedt265c8312009-02-13 12:43:56 -05004196 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004197
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004198 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004199 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004200 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004201 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004202 if (array[i] == rec->ip) {
4203 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004204 break;
4205 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004206 }
4207
4208 if (!not) {
4209 fail = 0;
4210 if (!exists) {
4211 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004212 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004213 goto out;
4214 }
4215 } else {
4216 if (exists) {
4217 array[i] = array[--(*idx)];
4218 array[*idx] = 0;
4219 fail = 0;
4220 }
4221 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004222 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004223 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004224out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004225 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004226
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004227 if (fail)
4228 return -EINVAL;
4229
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004230 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004231}
4232
4233static ssize_t
4234ftrace_graph_write(struct file *file, const char __user *ubuf,
4235 size_t cnt, loff_t *ppos)
4236{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004237 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004238 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004239 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004240
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004241 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004242 return 0;
4243
Namhyung Kim6a101082013-10-14 17:24:25 +09004244 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4245 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004246
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004247 read = trace_get_user(&parser, ubuf, cnt, ppos);
4248
Li Zefan4ba79782009-09-22 13:52:20 +08004249 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004250 parser.buffer[parser.idx] = 0;
4251
Namhyung Kim6a101082013-10-14 17:24:25 +09004252 mutex_lock(&graph_lock);
4253
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004254 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004255 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4256 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004257
4258 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004259 }
4260
Namhyung Kim6a101082013-10-14 17:24:25 +09004261 if (!ret)
4262 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004263
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004264 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004265
4266 return ret;
4267}
4268
4269static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004270 .open = ftrace_graph_open,
4271 .read = seq_read,
4272 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004273 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004274 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004275};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004276
4277static const struct file_operations ftrace_graph_notrace_fops = {
4278 .open = ftrace_graph_notrace_open,
4279 .read = seq_read,
4280 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004281 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004282 .release = ftrace_graph_release,
4283};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004284#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4285
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004286void ftrace_create_filter_files(struct ftrace_ops *ops,
4287 struct dentry *parent)
4288{
4289
4290 trace_create_file("set_ftrace_filter", 0644, parent,
4291 ops, &ftrace_filter_fops);
4292
4293 trace_create_file("set_ftrace_notrace", 0644, parent,
4294 ops, &ftrace_notrace_fops);
4295}
4296
4297/*
4298 * The name "destroy_filter_files" is really a misnomer. Although
4299 * in the future, it may actualy delete the files, but this is
4300 * really intended to make sure the ops passed in are disabled
4301 * and that when this function returns, the caller is free to
4302 * free the ops.
4303 *
4304 * The "destroy" name is only to match the "create" name that this
4305 * should be paired with.
4306 */
4307void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4308{
4309 mutex_lock(&ftrace_lock);
4310 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4311 ftrace_shutdown(ops, 0);
4312 ops->flags |= FTRACE_OPS_FL_DELETED;
4313 mutex_unlock(&ftrace_lock);
4314}
4315
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004316static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004317{
Steven Rostedt5072c592008-05-12 21:20:43 +02004318
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004319 trace_create_file("available_filter_functions", 0444,
4320 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004321
Steven Rostedt647bcd02011-05-03 14:39:21 -04004322 trace_create_file("enabled_functions", 0444,
4323 d_tracer, NULL, &ftrace_enabled_fops);
4324
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004325 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004326
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004327#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004328 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004329 NULL,
4330 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004331 trace_create_file("set_graph_notrace", 0444, d_tracer,
4332 NULL,
4333 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004334#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4335
Steven Rostedt5072c592008-05-12 21:20:43 +02004336 return 0;
4337}
4338
Steven Rostedt9fd49322012-04-24 22:32:06 -04004339static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004340{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004341 const unsigned long *ipa = a;
4342 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004343
Steven Rostedt9fd49322012-04-24 22:32:06 -04004344 if (*ipa > *ipb)
4345 return 1;
4346 if (*ipa < *ipb)
4347 return -1;
4348 return 0;
4349}
4350
4351static void ftrace_swap_ips(void *a, void *b, int size)
4352{
4353 unsigned long *ipa = a;
4354 unsigned long *ipb = b;
4355 unsigned long t;
4356
4357 t = *ipa;
4358 *ipa = *ipb;
4359 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004360}
4361
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004362static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004363 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004364 unsigned long *end)
4365{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004366 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004367 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004368 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004369 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004370 unsigned long *p;
4371 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004372 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004373 int ret = -ENOMEM;
4374
4375 count = end - start;
4376
4377 if (!count)
4378 return 0;
4379
Steven Rostedt9fd49322012-04-24 22:32:06 -04004380 sort(start, count, sizeof(*start),
4381 ftrace_cmp_ips, ftrace_swap_ips);
4382
Steven Rostedt706c81f2012-04-24 23:45:26 -04004383 start_pg = ftrace_allocate_pages(count);
4384 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004385 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004386
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004387 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004388
Steven Rostedt32082302011-12-16 14:42:37 -05004389 /*
4390 * Core and each module needs their own pages, as
4391 * modules will free them when they are removed.
4392 * Force a new page to be allocated for modules.
4393 */
Steven Rostedta7900872011-12-16 16:23:44 -05004394 if (!mod) {
4395 WARN_ON(ftrace_pages || ftrace_pages_start);
4396 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004397 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004398 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004399 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004400 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004401
Steven Rostedta7900872011-12-16 16:23:44 -05004402 if (WARN_ON(ftrace_pages->next)) {
4403 /* Hmm, we have free pages? */
4404 while (ftrace_pages->next)
4405 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004406 }
Steven Rostedta7900872011-12-16 16:23:44 -05004407
Steven Rostedt706c81f2012-04-24 23:45:26 -04004408 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004409 }
4410
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004411 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004412 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004413 while (p < end) {
4414 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004415 /*
4416 * Some architecture linkers will pad between
4417 * the different mcount_loc sections of different
4418 * object files to satisfy alignments.
4419 * Skip any NULL pointers.
4420 */
4421 if (!addr)
4422 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004423
4424 if (pg->index == pg->size) {
4425 /* We should have allocated enough */
4426 if (WARN_ON(!pg->next))
4427 break;
4428 pg = pg->next;
4429 }
4430
4431 rec = &pg->records[pg->index++];
4432 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004433 }
4434
Steven Rostedt706c81f2012-04-24 23:45:26 -04004435 /* We should have used all pages */
4436 WARN_ON(pg->next);
4437
4438 /* Assign the last page to ftrace_pages */
4439 ftrace_pages = pg;
4440
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004441 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004442 * We only need to disable interrupts on start up
4443 * because we are modifying code that an interrupt
4444 * may execute, and the modification is not atomic.
4445 * But for modules, nothing runs the code we modify
4446 * until we are finished with it, and there's no
4447 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004448 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004449 if (!mod)
4450 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004451 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004452 if (!mod)
4453 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004454 ret = 0;
4455 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004456 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004457
Steven Rostedta7900872011-12-16 16:23:44 -05004458 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004459}
4460
Steven Rostedt93eb6772009-04-15 13:24:06 -04004461#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004462
4463#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4464
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004465void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004466{
4467 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004468 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004469 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004470 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004471
Steven Rostedt93eb6772009-04-15 13:24:06 -04004472 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004473
4474 if (ftrace_disabled)
4475 goto out_unlock;
4476
Steven Rostedt32082302011-12-16 14:42:37 -05004477 /*
4478 * Each module has its own ftrace_pages, remove
4479 * them from the list.
4480 */
4481 last_pg = &ftrace_pages_start;
4482 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4483 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004484 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004485 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004486 * As core pages are first, the first
4487 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004488 */
Steven Rostedt32082302011-12-16 14:42:37 -05004489 if (WARN_ON(pg == ftrace_pages_start))
4490 goto out_unlock;
4491
4492 /* Check if we are deleting the last page */
4493 if (pg == ftrace_pages)
4494 ftrace_pages = next_to_ftrace_page(last_pg);
4495
4496 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004497 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4498 free_pages((unsigned long)pg->records, order);
4499 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004500 } else
4501 last_pg = &pg->next;
4502 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004503 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004504 mutex_unlock(&ftrace_lock);
4505}
4506
4507static void ftrace_init_module(struct module *mod,
4508 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004509{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004510 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004511 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004512 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004513}
4514
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004515void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004516{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004517 ftrace_init_module(mod, mod->ftrace_callsites,
4518 mod->ftrace_callsites +
4519 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004520}
4521
4522static int ftrace_module_notify_exit(struct notifier_block *self,
4523 unsigned long val, void *data)
4524{
4525 struct module *mod = data;
4526
4527 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004528 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004529
4530 return 0;
4531}
4532#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004533static int ftrace_module_notify_exit(struct notifier_block *self,
4534 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004535{
4536 return 0;
4537}
4538#endif /* CONFIG_MODULES */
4539
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004540struct notifier_block ftrace_module_exit_nb = {
4541 .notifier_call = ftrace_module_notify_exit,
4542 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4543};
4544
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004545void __init ftrace_init(void)
4546{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004547 extern unsigned long __start_mcount_loc[];
4548 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004549 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004550 int ret;
4551
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004552 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004553 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004554 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004555 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004556 goto failed;
4557
4558 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004559 if (!count) {
4560 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004561 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004562 }
4563
4564 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4565 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004566
4567 last_ftrace_enabled = ftrace_enabled = 1;
4568
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004569 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004570 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004571 __stop_mcount_loc);
4572
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004573 ret = register_module_notifier(&ftrace_module_exit_nb);
4574 if (ret)
4575 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004576
Steven Rostedt2af15d62009-05-28 13:37:24 -04004577 set_ftrace_early_filters();
4578
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004579 return;
4580 failed:
4581 ftrace_disabled = 1;
4582}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004583
Steven Rostedt3d083392008-05-12 21:20:42 +02004584#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004585
Steven Rostedt2b499382011-05-03 22:49:52 -04004586static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004587 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004588 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4589 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004590};
4591
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004592static int __init ftrace_nodyn_init(void)
4593{
4594 ftrace_enabled = 1;
4595 return 0;
4596}
Steven Rostedt6f415672012-10-05 12:13:07 -04004597core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004598
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004599static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4600static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004601/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004602# define ftrace_startup(ops, command) \
4603 ({ \
4604 int ___ret = __register_ftrace_function(ops); \
4605 if (!___ret) \
4606 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4607 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004608 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004609# define ftrace_shutdown(ops, command) \
4610 ({ \
4611 int ___ret = __unregister_ftrace_function(ops); \
4612 if (!___ret) \
4613 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4614 ___ret; \
4615 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004616
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004617# define ftrace_startup_sysctl() do { } while (0)
4618# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004619
4620static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004621ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004622{
4623 return 1;
4624}
4625
Steven Rostedt3d083392008-05-12 21:20:42 +02004626#endif /* CONFIG_DYNAMIC_FTRACE */
4627
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004628__init void ftrace_init_global_array_ops(struct trace_array *tr)
4629{
4630 tr->ops = &global_ops;
4631 tr->ops->private = tr;
4632}
4633
4634void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4635{
4636 /* If we filter on pids, update to use the pid function */
4637 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4638 if (WARN_ON(tr->ops->func != ftrace_stub))
4639 printk("ftrace ops had %pS for function\n",
4640 tr->ops->func);
4641 /* Only the top level instance does pid tracing */
4642 if (!list_empty(&ftrace_pids)) {
4643 set_ftrace_pid_function(func);
4644 func = ftrace_pid_func;
4645 }
4646 }
4647 tr->ops->func = func;
4648 tr->ops->private = tr;
4649}
4650
4651void ftrace_reset_array_ops(struct trace_array *tr)
4652{
4653 tr->ops->func = ftrace_stub;
4654}
4655
Steven Rostedtb8489142011-05-04 09:27:52 -04004656static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004657ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004658 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004659{
Jiri Olsae2484912012-02-15 15:51:48 +01004660 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4661 return;
4662
4663 /*
4664 * Some of the ops may be dynamically allocated,
4665 * they must be freed after a synchronize_sched().
4666 */
4667 preempt_disable_notrace();
4668 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004669
4670 /*
4671 * Control funcs (perf) uses RCU. Only trace if
4672 * RCU is currently active.
4673 */
4674 if (!rcu_is_watching())
4675 goto out;
4676
Steven Rostedt0a016402012-11-02 17:03:03 -04004677 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004678 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4679 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004680 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004681 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004682 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004683 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004684 trace_recursion_clear(TRACE_CONTROL_BIT);
4685 preempt_enable_notrace();
4686}
4687
4688static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004689 .func = ftrace_ops_control_func,
4690 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4691 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004692};
4693
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004694static inline void
4695__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004696 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004697{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004698 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004699 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004700
Steven Rostedtccf36722012-06-05 09:44:25 -04004701 if (function_trace_stop)
4702 return;
4703
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004704 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4705 if (bit < 0)
4706 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004707
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004708 /*
4709 * Some of the ops may be dynamically allocated,
4710 * they must be freed after a synchronize_sched().
4711 */
4712 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004713 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004714 if (ftrace_ops_test(op, ip, regs)) {
4715 if (WARN_ON(!op->func)) {
4716 function_trace_stop = 1;
4717 printk("op=%p %pS\n", op, op);
4718 goto out;
4719 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004720 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004721 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004722 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004723out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004724 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004725 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004726}
4727
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004728/*
4729 * Some archs only support passing ip and parent_ip. Even though
4730 * the list function ignores the op parameter, we do not want any
4731 * C side effects, where a function is called without the caller
4732 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004733 * Archs are to support both the regs and ftrace_ops at the same time.
4734 * If they support ftrace_ops, it is assumed they support regs.
4735 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004736 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4737 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004738 * An architecture can pass partial regs with ftrace_ops and still
4739 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004740 */
4741#if ARCH_SUPPORTS_FTRACE_OPS
4742static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004743 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004744{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004745 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004746}
4747#else
4748static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4749{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004750 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004751}
4752#endif
4753
Steven Rostedte32d8952008-12-04 00:26:41 -05004754static void clear_ftrace_swapper(void)
4755{
4756 struct task_struct *p;
4757 int cpu;
4758
4759 get_online_cpus();
4760 for_each_online_cpu(cpu) {
4761 p = idle_task(cpu);
4762 clear_tsk_trace_trace(p);
4763 }
4764 put_online_cpus();
4765}
4766
4767static void set_ftrace_swapper(void)
4768{
4769 struct task_struct *p;
4770 int cpu;
4771
4772 get_online_cpus();
4773 for_each_online_cpu(cpu) {
4774 p = idle_task(cpu);
4775 set_tsk_trace_trace(p);
4776 }
4777 put_online_cpus();
4778}
4779
4780static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004781{
4782 struct task_struct *p;
4783
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004784 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004785 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004786 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004787 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004788 rcu_read_unlock();
4789
Steven Rostedte32d8952008-12-04 00:26:41 -05004790 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004791}
4792
Steven Rostedte32d8952008-12-04 00:26:41 -05004793static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004794{
4795 struct task_struct *p;
4796
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004797 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004798 do_each_pid_task(pid, PIDTYPE_PID, p) {
4799 set_tsk_trace_trace(p);
4800 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004801 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004802}
4803
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004804static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004805{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004806 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004807 clear_ftrace_swapper();
4808 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004809 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004810}
4811
4812static void set_ftrace_pid_task(struct pid *pid)
4813{
4814 if (pid == ftrace_swapper_pid)
4815 set_ftrace_swapper();
4816 else
4817 set_ftrace_pid(pid);
4818}
4819
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004820static int ftrace_pid_add(int p)
4821{
4822 struct pid *pid;
4823 struct ftrace_pid *fpid;
4824 int ret = -EINVAL;
4825
4826 mutex_lock(&ftrace_lock);
4827
4828 if (!p)
4829 pid = ftrace_swapper_pid;
4830 else
4831 pid = find_get_pid(p);
4832
4833 if (!pid)
4834 goto out;
4835
4836 ret = 0;
4837
4838 list_for_each_entry(fpid, &ftrace_pids, list)
4839 if (fpid->pid == pid)
4840 goto out_put;
4841
4842 ret = -ENOMEM;
4843
4844 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4845 if (!fpid)
4846 goto out_put;
4847
4848 list_add(&fpid->list, &ftrace_pids);
4849 fpid->pid = pid;
4850
4851 set_ftrace_pid_task(pid);
4852
4853 ftrace_update_pid_func();
4854 ftrace_startup_enable(0);
4855
4856 mutex_unlock(&ftrace_lock);
4857 return 0;
4858
4859out_put:
4860 if (pid != ftrace_swapper_pid)
4861 put_pid(pid);
4862
4863out:
4864 mutex_unlock(&ftrace_lock);
4865 return ret;
4866}
4867
4868static void ftrace_pid_reset(void)
4869{
4870 struct ftrace_pid *fpid, *safe;
4871
4872 mutex_lock(&ftrace_lock);
4873 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4874 struct pid *pid = fpid->pid;
4875
4876 clear_ftrace_pid_task(pid);
4877
4878 list_del(&fpid->list);
4879 kfree(fpid);
4880 }
4881
4882 ftrace_update_pid_func();
4883 ftrace_startup_enable(0);
4884
4885 mutex_unlock(&ftrace_lock);
4886}
4887
4888static void *fpid_start(struct seq_file *m, loff_t *pos)
4889{
4890 mutex_lock(&ftrace_lock);
4891
4892 if (list_empty(&ftrace_pids) && (!*pos))
4893 return (void *) 1;
4894
4895 return seq_list_start(&ftrace_pids, *pos);
4896}
4897
4898static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4899{
4900 if (v == (void *)1)
4901 return NULL;
4902
4903 return seq_list_next(v, &ftrace_pids, pos);
4904}
4905
4906static void fpid_stop(struct seq_file *m, void *p)
4907{
4908 mutex_unlock(&ftrace_lock);
4909}
4910
4911static int fpid_show(struct seq_file *m, void *v)
4912{
4913 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4914
4915 if (v == (void *)1) {
4916 seq_printf(m, "no pid\n");
4917 return 0;
4918 }
4919
4920 if (fpid->pid == ftrace_swapper_pid)
4921 seq_printf(m, "swapper tasks\n");
4922 else
4923 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4924
4925 return 0;
4926}
4927
4928static const struct seq_operations ftrace_pid_sops = {
4929 .start = fpid_start,
4930 .next = fpid_next,
4931 .stop = fpid_stop,
4932 .show = fpid_show,
4933};
4934
4935static int
4936ftrace_pid_open(struct inode *inode, struct file *file)
4937{
4938 int ret = 0;
4939
4940 if ((file->f_mode & FMODE_WRITE) &&
4941 (file->f_flags & O_TRUNC))
4942 ftrace_pid_reset();
4943
4944 if (file->f_mode & FMODE_READ)
4945 ret = seq_open(file, &ftrace_pid_sops);
4946
4947 return ret;
4948}
4949
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004950static ssize_t
4951ftrace_pid_write(struct file *filp, const char __user *ubuf,
4952 size_t cnt, loff_t *ppos)
4953{
Ingo Molnar457dc922009-11-23 11:03:28 +01004954 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004955 long val;
4956 int ret;
4957
4958 if (cnt >= sizeof(buf))
4959 return -EINVAL;
4960
4961 if (copy_from_user(&buf, ubuf, cnt))
4962 return -EFAULT;
4963
4964 buf[cnt] = 0;
4965
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004966 /*
4967 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4968 * to clean the filter quietly.
4969 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004970 tmp = strstrip(buf);
4971 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004972 return 1;
4973
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004974 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004975 if (ret < 0)
4976 return ret;
4977
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004978 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004979
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004980 return ret ? ret : cnt;
4981}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004982
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004983static int
4984ftrace_pid_release(struct inode *inode, struct file *file)
4985{
4986 if (file->f_mode & FMODE_READ)
4987 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004988
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004989 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004990}
4991
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004992static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004993 .open = ftrace_pid_open,
4994 .write = ftrace_pid_write,
4995 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004996 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004997 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004998};
4999
5000static __init int ftrace_init_debugfs(void)
5001{
5002 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005003
5004 d_tracer = tracing_init_dentry();
5005 if (!d_tracer)
5006 return 0;
5007
5008 ftrace_init_dyn_debugfs(d_tracer);
5009
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005010 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5011 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005012
5013 ftrace_profile_debugfs(d_tracer);
5014
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005015 return 0;
5016}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005017fs_initcall(ftrace_init_debugfs);
5018
Steven Rostedt3d083392008-05-12 21:20:42 +02005019/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005020 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005021 *
5022 * This function should be used by panic code. It stops ftrace
5023 * but in a not so nice way. If you need to simply kill ftrace
5024 * from a non-atomic section, use ftrace_kill.
5025 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005026void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005027{
5028 ftrace_disabled = 1;
5029 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005030 clear_ftrace_function();
5031}
5032
5033/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005034 * Test if ftrace is dead or not.
5035 */
5036int ftrace_is_dead(void)
5037{
5038 return ftrace_disabled;
5039}
5040
5041/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005042 * register_ftrace_function - register a function for profiling
5043 * @ops - ops structure that holds the function for profiling.
5044 *
5045 * Register a function to be called by all functions in the
5046 * kernel.
5047 *
5048 * Note: @ops->func and all the functions it calls must be labeled
5049 * with "notrace", otherwise it will go into a
5050 * recursive loop.
5051 */
5052int register_ftrace_function(struct ftrace_ops *ops)
5053{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005054 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005055
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005056 ftrace_ops_init(ops);
5057
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005058 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005059
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005060 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005061
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005062 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005063
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005064 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005065}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005066EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005067
5068/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005069 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005070 * @ops - ops structure that holds the function to unregister
5071 *
5072 * Unregister a function that was added to be called by ftrace profiling.
5073 */
5074int unregister_ftrace_function(struct ftrace_ops *ops)
5075{
5076 int ret;
5077
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005078 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005079 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005080 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005081
5082 return ret;
5083}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005084EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005085
Ingo Molnare309b412008-05-12 21:20:51 +02005086int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005087ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005088 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005089 loff_t *ppos)
5090{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005091 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005092
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005093 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005094
Steven Rostedt45a4a232011-04-21 23:16:46 -04005095 if (unlikely(ftrace_disabled))
5096 goto out;
5097
5098 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005099
Li Zefana32c7762009-06-26 16:55:51 +08005100 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005101 goto out;
5102
Li Zefana32c7762009-06-26 16:55:51 +08005103 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005104
5105 if (ftrace_enabled) {
5106
5107 ftrace_startup_sysctl();
5108
5109 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005110 if (ftrace_ops_list != &ftrace_list_end)
5111 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005112
5113 } else {
5114 /* stopping ftrace calls (just send to ftrace_stub) */
5115 ftrace_trace_function = ftrace_stub;
5116
5117 ftrace_shutdown_sysctl();
5118 }
5119
5120 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005121 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005122 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005123}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005124
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005125#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005126
Steven Rostedt597af812009-04-03 15:24:12 -04005127static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005128
Steven Rostedte49dc192008-12-02 23:50:05 -05005129int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5130{
5131 return 0;
5132}
5133
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005134/* The callbacks that hook a function */
5135trace_func_graph_ret_t ftrace_graph_return =
5136 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005137trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005138static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005139
5140/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5141static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5142{
5143 int i;
5144 int ret = 0;
5145 unsigned long flags;
5146 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5147 struct task_struct *g, *t;
5148
5149 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5150 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5151 * sizeof(struct ftrace_ret_stack),
5152 GFP_KERNEL);
5153 if (!ret_stack_list[i]) {
5154 start = 0;
5155 end = i;
5156 ret = -ENOMEM;
5157 goto free;
5158 }
5159 }
5160
5161 read_lock_irqsave(&tasklist_lock, flags);
5162 do_each_thread(g, t) {
5163 if (start == end) {
5164 ret = -EAGAIN;
5165 goto unlock;
5166 }
5167
5168 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005169 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005170 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005171 t->curr_ret_stack = -1;
5172 /* Make sure the tasks see the -1 first: */
5173 smp_wmb();
5174 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005175 }
5176 } while_each_thread(g, t);
5177
5178unlock:
5179 read_unlock_irqrestore(&tasklist_lock, flags);
5180free:
5181 for (i = start; i < end; i++)
5182 kfree(ret_stack_list[i]);
5183 return ret;
5184}
5185
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005186static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005187ftrace_graph_probe_sched_switch(void *ignore,
5188 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005189{
5190 unsigned long long timestamp;
5191 int index;
5192
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005193 /*
5194 * Does the user want to count the time a function was asleep.
5195 * If so, do not update the time stamps.
5196 */
5197 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5198 return;
5199
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005200 timestamp = trace_clock_local();
5201
5202 prev->ftrace_timestamp = timestamp;
5203
5204 /* only process tasks that we timestamped */
5205 if (!next->ftrace_timestamp)
5206 return;
5207
5208 /*
5209 * Update all the counters in next to make up for the
5210 * time next was sleeping.
5211 */
5212 timestamp -= next->ftrace_timestamp;
5213
5214 for (index = next->curr_ret_stack; index >= 0; index--)
5215 next->ret_stack[index].calltime += timestamp;
5216}
5217
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005218/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005219static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005220{
5221 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005222 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005223
5224 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5225 sizeof(struct ftrace_ret_stack *),
5226 GFP_KERNEL);
5227
5228 if (!ret_stack_list)
5229 return -ENOMEM;
5230
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005231 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005232 for_each_online_cpu(cpu) {
5233 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005234 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005235 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005236
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005237 do {
5238 ret = alloc_retstack_tasklist(ret_stack_list);
5239 } while (ret == -EAGAIN);
5240
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005241 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005242 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005243 if (ret)
5244 pr_info("ftrace_graph: Couldn't activate tracepoint"
5245 " probe to kernel_sched_switch\n");
5246 }
5247
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005248 kfree(ret_stack_list);
5249 return ret;
5250}
5251
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005252/*
5253 * Hibernation protection.
5254 * The state of the current task is too much unstable during
5255 * suspend/restore to disk. We want to protect against that.
5256 */
5257static int
5258ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5259 void *unused)
5260{
5261 switch (state) {
5262 case PM_HIBERNATION_PREPARE:
5263 pause_graph_tracing();
5264 break;
5265
5266 case PM_POST_HIBERNATION:
5267 unpause_graph_tracing();
5268 break;
5269 }
5270 return NOTIFY_DONE;
5271}
5272
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005273static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5274{
5275 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5276 return 0;
5277 return __ftrace_graph_entry(trace);
5278}
5279
5280/*
5281 * The function graph tracer should only trace the functions defined
5282 * by set_ftrace_filter and set_ftrace_notrace. If another function
5283 * tracer ops is registered, the graph tracer requires testing the
5284 * function against the global ops, and not just trace any function
5285 * that any ftrace_ops registered.
5286 */
5287static void update_function_graph_func(void)
5288{
5289 if (ftrace_ops_list == &ftrace_list_end ||
5290 (ftrace_ops_list == &global_ops &&
5291 global_ops.next == &ftrace_list_end))
5292 ftrace_graph_entry = __ftrace_graph_entry;
5293 else
5294 ftrace_graph_entry = ftrace_graph_entry_test;
5295}
5296
Mathias Krause8275f692014-03-30 15:31:50 +02005297static struct notifier_block ftrace_suspend_notifier = {
5298 .notifier_call = ftrace_suspend_notifier_call,
5299};
5300
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005301int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5302 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005303{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005304 int ret = 0;
5305
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005306 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005307
Steven Rostedt05ce5812009-03-24 00:18:31 -04005308 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005309 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005310 ret = -EBUSY;
5311 goto out;
5312 }
5313
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005314 register_pm_notifier(&ftrace_suspend_notifier);
5315
Steven Rostedt597af812009-04-03 15:24:12 -04005316 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005317 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005318 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005319 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005320 goto out;
5321 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005322
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005323 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005324
5325 /*
5326 * Update the indirect function to the entryfunc, and the
5327 * function that gets called to the entry_test first. Then
5328 * call the update fgraph entry function to determine if
5329 * the entryfunc should be called directly or not.
5330 */
5331 __ftrace_graph_entry = entryfunc;
5332 ftrace_graph_entry = ftrace_graph_entry_test;
5333 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005334
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005335 /* Function graph doesn't use the .func field of global_ops */
5336 global_ops.flags |= FTRACE_OPS_FL_STUB;
5337
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005338#ifdef CONFIG_DYNAMIC_FTRACE
5339 /* Optimize function graph calling (if implemented by arch) */
5340 global_ops.trampoline = FTRACE_GRAPH_ADDR;
5341#endif
5342
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005343 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005344
5345out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005346 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005347 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005348}
5349
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005350void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005351{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005352 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005353
Steven Rostedt597af812009-04-03 15:24:12 -04005354 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005355 goto out;
5356
Steven Rostedt597af812009-04-03 15:24:12 -04005357 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005358 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005359 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005360 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005361 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5362 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005363#ifdef CONFIG_DYNAMIC_FTRACE
5364 global_ops.trampoline = 0;
5365#endif
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005366 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005367 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005368
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005369 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005370 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005371}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005372
Steven Rostedt868baf02011-02-10 21:26:13 -05005373static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5374
5375static void
5376graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5377{
5378 atomic_set(&t->tracing_graph_pause, 0);
5379 atomic_set(&t->trace_overrun, 0);
5380 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005381 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005382 smp_wmb();
5383 t->ret_stack = ret_stack;
5384}
5385
5386/*
5387 * Allocate a return stack for the idle task. May be the first
5388 * time through, or it may be done by CPU hotplug online.
5389 */
5390void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5391{
5392 t->curr_ret_stack = -1;
5393 /*
5394 * The idle task has no parent, it either has its own
5395 * stack or no stack at all.
5396 */
5397 if (t->ret_stack)
5398 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5399
5400 if (ftrace_graph_active) {
5401 struct ftrace_ret_stack *ret_stack;
5402
5403 ret_stack = per_cpu(idle_ret_stack, cpu);
5404 if (!ret_stack) {
5405 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5406 * sizeof(struct ftrace_ret_stack),
5407 GFP_KERNEL);
5408 if (!ret_stack)
5409 return;
5410 per_cpu(idle_ret_stack, cpu) = ret_stack;
5411 }
5412 graph_init_task(t, ret_stack);
5413 }
5414}
5415
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005416/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005417void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005418{
Steven Rostedt84047e32009-06-02 16:51:55 -04005419 /* Make sure we do not use the parent ret_stack */
5420 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005421 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005422
Steven Rostedt597af812009-04-03 15:24:12 -04005423 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005424 struct ftrace_ret_stack *ret_stack;
5425
5426 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005427 * sizeof(struct ftrace_ret_stack),
5428 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005429 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005430 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005431 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005432 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005433}
5434
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005435void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005436{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005437 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5438
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005439 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005440 /* NULL must become visible to IRQs before we free it: */
5441 barrier();
5442
5443 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005444}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005445
5446void ftrace_graph_stop(void)
5447{
5448 ftrace_stop();
5449}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005450#endif