blob: 3ded796e10e05beeb4c3504af9be48cbf8cae563 [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 Rostedt69128962008-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 Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-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 Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-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;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001309 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001310 int i;
1311
1312 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001313 * Remove the current set, update the hash and add
1314 * them back.
1315 */
1316 ftrace_hash_rec_disable(ops, enable);
1317
1318 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319 * If the new source is empty, just free dst and assign it
1320 * the empty_hash.
1321 */
1322 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001323 free_ftrace_hash_rcu(*dst);
1324 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001325 /* still need to update the function records */
1326 ret = 0;
1327 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 }
1329
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001330 /*
1331 * Make the hash size about 1/2 the # found
1332 */
1333 for (size /= 2; size; size >>= 1)
1334 bits++;
1335
1336 /* Don't allocate too much */
1337 if (bits > FTRACE_HASH_MAX_BITS)
1338 bits = FTRACE_HASH_MAX_BITS;
1339
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001340 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001341 new_hash = alloc_ftrace_hash(bits);
1342 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001343 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001344
1345 size = 1 << src->size_bits;
1346 for (i = 0; i < size; i++) {
1347 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001348 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001349 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001350 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001351 }
1352 }
1353
Steven Rostedt07fd5512011-05-05 18:03:47 -04001354 old_hash = *dst;
1355 rcu_assign_pointer(*dst, new_hash);
1356 free_ftrace_hash_rcu(old_hash);
1357
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001358 ret = 0;
1359 out:
1360 /*
1361 * Enable regardless of ret:
1362 * On success, we enable the new hash.
1363 * On failure, we re-enable the original hash.
1364 */
1365 ftrace_hash_rec_enable(ops, enable);
1366
1367 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001368}
1369
Steven Rostedt265c8312009-02-13 12:43:56 -05001370/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001371 * Test the hashes for this ops to see if we want to call
1372 * the ops->func or not.
1373 *
1374 * It's a match if the ip is in the ops->filter_hash or
1375 * the filter_hash does not exist or is empty,
1376 * AND
1377 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001378 *
1379 * This needs to be called with preemption disabled as
1380 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001381 */
1382static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001383ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001384{
1385 struct ftrace_hash *filter_hash;
1386 struct ftrace_hash *notrace_hash;
1387 int ret;
1388
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001389#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1390 /*
1391 * There's a small race when adding ops that the ftrace handler
1392 * that wants regs, may be called without them. We can not
1393 * allow that handler to be called if regs is NULL.
1394 */
1395 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1396 return 0;
1397#endif
1398
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001399 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1400 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001401
Steven Rostedt06a51d92011-12-19 19:07:36 -05001402 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001403 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001404 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001405 !ftrace_lookup_ip(notrace_hash, ip)))
1406 ret = 1;
1407 else
1408 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001409
1410 return ret;
1411}
1412
1413/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001414 * This is a double for. Do not use 'break' to break out of the loop,
1415 * you must use a goto.
1416 */
1417#define do_for_each_ftrace_rec(pg, rec) \
1418 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1419 int _____i; \
1420 for (_____i = 0; _____i < pg->index; _____i++) { \
1421 rec = &pg->records[_____i];
1422
1423#define while_for_each_ftrace_rec() \
1424 } \
1425 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301426
Steven Rostedt5855fea2011-12-16 19:27:42 -05001427
1428static int ftrace_cmp_recs(const void *a, const void *b)
1429{
Steven Rostedta650e022012-04-25 13:48:13 -04001430 const struct dyn_ftrace *key = a;
1431 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001432
Steven Rostedta650e022012-04-25 13:48:13 -04001433 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001434 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001435 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1436 return 1;
1437 return 0;
1438}
1439
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001440static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001441{
1442 struct ftrace_page *pg;
1443 struct dyn_ftrace *rec;
1444 struct dyn_ftrace key;
1445
1446 key.ip = start;
1447 key.flags = end; /* overload flags, as it is unsigned long */
1448
1449 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1450 if (end < pg->records[0].ip ||
1451 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1452 continue;
1453 rec = bsearch(&key, pg->records, pg->index,
1454 sizeof(struct dyn_ftrace),
1455 ftrace_cmp_recs);
1456 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001457 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001458 }
1459
Steven Rostedt5855fea2011-12-16 19:27:42 -05001460 return 0;
1461}
1462
Steven Rostedtc88fd862011-08-16 09:53:39 -04001463/**
1464 * ftrace_location - return true if the ip giving is a traced location
1465 * @ip: the instruction pointer to check
1466 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001467 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001468 * That is, the instruction that is either a NOP or call to
1469 * the function tracer. It checks the ftrace internal tables to
1470 * determine if the address belongs or not.
1471 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001472unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001473{
Steven Rostedta650e022012-04-25 13:48:13 -04001474 return ftrace_location_range(ip, ip);
1475}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001476
Steven Rostedta650e022012-04-25 13:48:13 -04001477/**
1478 * ftrace_text_reserved - return true if range contains an ftrace location
1479 * @start: start of range to search
1480 * @end: end of range to search (inclusive). @end points to the last byte to check.
1481 *
1482 * Returns 1 if @start and @end contains a ftrace location.
1483 * That is, the instruction that is either a NOP or call to
1484 * the function tracer. It checks the ftrace internal tables to
1485 * determine if the address belongs or not.
1486 */
Sasha Levind88471c2013-01-09 18:09:20 -05001487int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001488{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001489 unsigned long ret;
1490
1491 ret = ftrace_location_range((unsigned long)start,
1492 (unsigned long)end);
1493
1494 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001495}
1496
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001497/* Test if ops registered to this rec needs regs */
1498static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1499{
1500 struct ftrace_ops *ops;
1501 bool keep_regs = false;
1502
1503 for (ops = ftrace_ops_list;
1504 ops != &ftrace_list_end; ops = ops->next) {
1505 /* pass rec in as regs to have non-NULL val */
1506 if (ftrace_ops_test(ops, rec->ip, rec)) {
1507 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1508 keep_regs = true;
1509 break;
1510 }
1511 }
1512 }
1513
1514 return keep_regs;
1515}
1516
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001517static void ftrace_remove_tramp(struct ftrace_ops *ops,
1518 struct dyn_ftrace *rec)
1519{
1520 struct ftrace_func_entry *entry;
1521
1522 entry = ftrace_lookup_ip(ops->tramp_hash, rec->ip);
1523 if (!entry)
1524 return;
1525
1526 /*
1527 * The tramp_hash entry will be removed at time
1528 * of update.
1529 */
1530 ops->trampolines--;
1531 rec->flags &= ~FTRACE_FL_TRAMP;
1532}
1533
1534static void ftrace_clear_tramps(struct dyn_ftrace *rec)
1535{
1536 struct ftrace_ops *op;
1537
1538 do_for_each_ftrace_op(op, ftrace_ops_list) {
1539 if (op->trampolines)
1540 ftrace_remove_tramp(op, rec);
1541 } while_for_each_ftrace_op(op);
1542}
1543
Steven Rostedted926f92011-05-03 13:25:24 -04001544static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1545 int filter_hash,
1546 bool inc)
1547{
1548 struct ftrace_hash *hash;
1549 struct ftrace_hash *other_hash;
1550 struct ftrace_page *pg;
1551 struct dyn_ftrace *rec;
1552 int count = 0;
1553 int all = 0;
1554
1555 /* Only update if the ops has been registered */
1556 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1557 return;
1558
1559 /*
1560 * In the filter_hash case:
1561 * If the count is zero, we update all records.
1562 * Otherwise we just update the items in the hash.
1563 *
1564 * In the notrace_hash case:
1565 * We enable the update in the hash.
1566 * As disabling notrace means enabling the tracing,
1567 * and enabling notrace means disabling, the inc variable
1568 * gets inversed.
1569 */
1570 if (filter_hash) {
1571 hash = ops->filter_hash;
1572 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001573 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001574 all = 1;
1575 } else {
1576 inc = !inc;
1577 hash = ops->notrace_hash;
1578 other_hash = ops->filter_hash;
1579 /*
1580 * If the notrace hash has no items,
1581 * then there's nothing to do.
1582 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001583 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001584 return;
1585 }
1586
1587 do_for_each_ftrace_rec(pg, rec) {
1588 int in_other_hash = 0;
1589 int in_hash = 0;
1590 int match = 0;
1591
1592 if (all) {
1593 /*
1594 * Only the filter_hash affects all records.
1595 * Update if the record is not in the notrace hash.
1596 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001597 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001598 match = 1;
1599 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001600 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1601 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001602
1603 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001604 * If filter_hash is set, we want to match all functions
1605 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001606 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001607 * If filter_hash is not set, then we are decrementing.
1608 * That means we match anything that is in the hash
1609 * and also in the other_hash. That is, we need to turn
1610 * off functions in the other hash because they are disabled
1611 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001612 */
1613 if (filter_hash && in_hash && !in_other_hash)
1614 match = 1;
1615 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001616 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001617 match = 1;
1618 }
1619 if (!match)
1620 continue;
1621
1622 if (inc) {
1623 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001624 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001625 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001626
1627 /*
1628 * If there's only a single callback registered to a
1629 * function, and the ops has a trampoline registered
1630 * for it, then we can call it directly.
1631 */
1632 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1633 rec->flags |= FTRACE_FL_TRAMP;
1634 ops->trampolines++;
1635 } else {
1636 /*
1637 * If we are adding another function callback
1638 * to this function, and the previous had a
1639 * trampoline used, then we need to go back to
1640 * the default trampoline.
1641 */
1642 rec->flags &= ~FTRACE_FL_TRAMP;
1643
1644 /* remove trampolines from any ops for this rec */
1645 ftrace_clear_tramps(rec);
1646 }
1647
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001648 /*
1649 * If any ops wants regs saved for this function
1650 * then all ops will get saved regs.
1651 */
1652 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1653 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001654 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001655 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001656 return;
1657 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001658
1659 if (ops->trampoline && !ftrace_rec_count(rec))
1660 ftrace_remove_tramp(ops, rec);
1661
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001662 /*
1663 * If the rec had REGS enabled and the ops that is
1664 * being removed had REGS set, then see if there is
1665 * still any ops for this record that wants regs.
1666 * If not, we can stop recording them.
1667 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001668 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001669 rec->flags & FTRACE_FL_REGS &&
1670 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1671 if (!test_rec_ops_needs_regs(rec))
1672 rec->flags &= ~FTRACE_FL_REGS;
1673 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001674
1675 /*
1676 * flags will be cleared in ftrace_check_record()
1677 * if rec count is zero.
1678 */
Steven Rostedted926f92011-05-03 13:25:24 -04001679 }
1680 count++;
1681 /* Shortcut, if we handled all records, we are done. */
1682 if (!all && count == hash->count)
1683 return;
1684 } while_for_each_ftrace_rec();
1685}
1686
1687static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1688 int filter_hash)
1689{
1690 __ftrace_hash_rec_update(ops, filter_hash, 0);
1691}
1692
1693static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1694 int filter_hash)
1695{
1696 __ftrace_hash_rec_update(ops, filter_hash, 1);
1697}
1698
Steven Rostedt05736a42008-09-22 14:55:47 -07001699static void print_ip_ins(const char *fmt, unsigned char *p)
1700{
1701 int i;
1702
1703 printk(KERN_CONT "%s", fmt);
1704
1705 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1706 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1707}
1708
Steven Rostedtc88fd862011-08-16 09:53:39 -04001709/**
1710 * ftrace_bug - report and shutdown function tracer
1711 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1712 * @ip: The address that failed
1713 *
1714 * The arch code that enables or disables the function tracing
1715 * can call ftrace_bug() when it has detected a problem in
1716 * modifying the code. @failed should be one of either:
1717 * EFAULT - if the problem happens on reading the @ip address
1718 * EINVAL - if what is read at @ip is not what was expected
1719 * EPERM - if the problem happens on writting to the @ip address
1720 */
1721void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001722{
1723 switch (failed) {
1724 case -EFAULT:
1725 FTRACE_WARN_ON_ONCE(1);
1726 pr_info("ftrace faulted on modifying ");
1727 print_ip_sym(ip);
1728 break;
1729 case -EINVAL:
1730 FTRACE_WARN_ON_ONCE(1);
1731 pr_info("ftrace failed to modify ");
1732 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001733 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001734 printk(KERN_CONT "\n");
1735 break;
1736 case -EPERM:
1737 FTRACE_WARN_ON_ONCE(1);
1738 pr_info("ftrace faulted on writing ");
1739 print_ip_sym(ip);
1740 break;
1741 default:
1742 FTRACE_WARN_ON_ONCE(1);
1743 pr_info("ftrace faulted on unknown error ");
1744 print_ip_sym(ip);
1745 }
1746}
1747
Steven Rostedtc88fd862011-08-16 09:53:39 -04001748static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001749{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001750 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001751
Steven Rostedt982c3502008-11-15 16:31:41 -05001752 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001753 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001754 *
Steven Rostedted926f92011-05-03 13:25:24 -04001755 * If the record has a ref count, then we need to enable it
1756 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001757 *
Steven Rostedted926f92011-05-03 13:25:24 -04001758 * Otherwise we make sure its disabled.
1759 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001760 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001761 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001762 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001763 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001764 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001765
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001766 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001767 * If enabling and the REGS flag does not match the REGS_EN, or
1768 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1769 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001770 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001771 if (flag) {
1772 if (!(rec->flags & FTRACE_FL_REGS) !=
1773 !(rec->flags & FTRACE_FL_REGS_EN))
1774 flag |= FTRACE_FL_REGS;
1775
1776 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1777 !(rec->flags & FTRACE_FL_TRAMP_EN))
1778 flag |= FTRACE_FL_TRAMP;
1779 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001780
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001781 /* If the state of this record hasn't changed, then do nothing */
1782 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001783 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001784
1785 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001786 /* Save off if rec is being enabled (for return value) */
1787 flag ^= rec->flags & FTRACE_FL_ENABLED;
1788
1789 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001790 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001791 if (flag & FTRACE_FL_REGS) {
1792 if (rec->flags & FTRACE_FL_REGS)
1793 rec->flags |= FTRACE_FL_REGS_EN;
1794 else
1795 rec->flags &= ~FTRACE_FL_REGS_EN;
1796 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001797 if (flag & FTRACE_FL_TRAMP) {
1798 if (rec->flags & FTRACE_FL_TRAMP)
1799 rec->flags |= FTRACE_FL_TRAMP_EN;
1800 else
1801 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1802 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001803 }
1804
1805 /*
1806 * If this record is being updated from a nop, then
1807 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001808 * Otherwise,
1809 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001810 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001811 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001812 */
1813 if (flag & FTRACE_FL_ENABLED)
1814 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001815
1816 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001817 }
1818
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001819 if (update) {
1820 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001821 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001822 rec->flags = 0;
1823 else
1824 /* Just disable the record (keep REGS state) */
1825 rec->flags &= ~FTRACE_FL_ENABLED;
1826 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001827
1828 return FTRACE_UPDATE_MAKE_NOP;
1829}
1830
1831/**
1832 * ftrace_update_record, set a record that now is tracing or not
1833 * @rec: the record to update
1834 * @enable: set to 1 if the record is tracing, zero to force disable
1835 *
1836 * The records that represent all functions that can be traced need
1837 * to be updated when tracing has been enabled.
1838 */
1839int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1840{
1841 return ftrace_check_record(rec, enable, 1);
1842}
1843
1844/**
1845 * ftrace_test_record, check if the record has been enabled or not
1846 * @rec: the record to test
1847 * @enable: set to 1 to check if enabled, 0 if it is disabled
1848 *
1849 * The arch code may need to test if a record is already set to
1850 * tracing to determine how to modify the function code that it
1851 * represents.
1852 */
1853int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1854{
1855 return ftrace_check_record(rec, enable, 0);
1856}
1857
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001858static struct ftrace_ops *
1859ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1860{
1861 struct ftrace_ops *op;
1862
1863 /* Removed ops need to be tested first */
1864 if (removed_ops && removed_ops->tramp_hash) {
1865 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1866 return removed_ops;
1867 }
1868
1869 do_for_each_ftrace_op(op, ftrace_ops_list) {
1870 if (!op->tramp_hash)
1871 continue;
1872
1873 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1874 return op;
1875
1876 } while_for_each_ftrace_op(op);
1877
1878 return NULL;
1879}
1880
1881static struct ftrace_ops *
1882ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1883{
1884 struct ftrace_ops *op;
1885
1886 do_for_each_ftrace_op(op, ftrace_ops_list) {
1887 /* pass rec in as regs to have non-NULL val */
1888 if (ftrace_ops_test(op, rec->ip, rec))
1889 return op;
1890 } while_for_each_ftrace_op(op);
1891
1892 return NULL;
1893}
1894
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001895/**
1896 * ftrace_get_addr_new - Get the call address to set to
1897 * @rec: The ftrace record descriptor
1898 *
1899 * If the record has the FTRACE_FL_REGS set, that means that it
1900 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1901 * is not not set, then it wants to convert to the normal callback.
1902 *
1903 * Returns the address of the trampoline to set to
1904 */
1905unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1906{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001907 struct ftrace_ops *ops;
1908
1909 /* Trampolines take precedence over regs */
1910 if (rec->flags & FTRACE_FL_TRAMP) {
1911 ops = ftrace_find_tramp_ops_new(rec);
1912 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
1913 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1914 (void *)rec->ip, (void *)rec->ip);
1915 /* Ftrace is shutting down, return anything */
1916 return (unsigned long)FTRACE_ADDR;
1917 }
1918 return ops->trampoline;
1919 }
1920
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001921 if (rec->flags & FTRACE_FL_REGS)
1922 return (unsigned long)FTRACE_REGS_ADDR;
1923 else
1924 return (unsigned long)FTRACE_ADDR;
1925}
1926
1927/**
1928 * ftrace_get_addr_curr - Get the call address that is already there
1929 * @rec: The ftrace record descriptor
1930 *
1931 * The FTRACE_FL_REGS_EN is set when the record already points to
1932 * a function that saves all the regs. Basically the '_EN' version
1933 * represents the current state of the function.
1934 *
1935 * Returns the address of the trampoline that is currently being called
1936 */
1937unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1938{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001939 struct ftrace_ops *ops;
1940
1941 /* Trampolines take precedence over regs */
1942 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1943 ops = ftrace_find_tramp_ops_curr(rec);
1944 if (FTRACE_WARN_ON(!ops)) {
1945 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1946 (void *)rec->ip, (void *)rec->ip);
1947 /* Ftrace is shutting down, return anything */
1948 return (unsigned long)FTRACE_ADDR;
1949 }
1950 return ops->trampoline;
1951 }
1952
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001953 if (rec->flags & FTRACE_FL_REGS_EN)
1954 return (unsigned long)FTRACE_REGS_ADDR;
1955 else
1956 return (unsigned long)FTRACE_ADDR;
1957}
1958
Steven Rostedtc88fd862011-08-16 09:53:39 -04001959static int
1960__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1961{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001962 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001963 unsigned long ftrace_addr;
1964 int ret;
1965
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001966 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001967
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001968 /* This needs to be done before we call ftrace_update_record */
1969 ftrace_old_addr = ftrace_get_addr_curr(rec);
1970
1971 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001972
Steven Rostedtc88fd862011-08-16 09:53:39 -04001973 switch (ret) {
1974 case FTRACE_UPDATE_IGNORE:
1975 return 0;
1976
1977 case FTRACE_UPDATE_MAKE_CALL:
1978 return ftrace_make_call(rec, ftrace_addr);
1979
1980 case FTRACE_UPDATE_MAKE_NOP:
1981 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001982
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001983 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001984 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001985 }
1986
1987 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001988}
1989
Steven Rostedte4f5d542012-04-27 09:13:18 -04001990void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001991{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001992 struct dyn_ftrace *rec;
1993 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001994 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001995
Steven Rostedt45a4a232011-04-21 23:16:46 -04001996 if (unlikely(ftrace_disabled))
1997 return;
1998
Steven Rostedt265c8312009-02-13 12:43:56 -05001999 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002000 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002001 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04002002 ftrace_bug(failed, rec->ip);
2003 /* Stop processing */
2004 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002005 }
2006 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002007}
2008
Steven Rostedtc88fd862011-08-16 09:53:39 -04002009struct ftrace_rec_iter {
2010 struct ftrace_page *pg;
2011 int index;
2012};
2013
2014/**
2015 * ftrace_rec_iter_start, start up iterating over traced functions
2016 *
2017 * Returns an iterator handle that is used to iterate over all
2018 * the records that represent address locations where functions
2019 * are traced.
2020 *
2021 * May return NULL if no records are available.
2022 */
2023struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2024{
2025 /*
2026 * We only use a single iterator.
2027 * Protected by the ftrace_lock mutex.
2028 */
2029 static struct ftrace_rec_iter ftrace_rec_iter;
2030 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2031
2032 iter->pg = ftrace_pages_start;
2033 iter->index = 0;
2034
2035 /* Could have empty pages */
2036 while (iter->pg && !iter->pg->index)
2037 iter->pg = iter->pg->next;
2038
2039 if (!iter->pg)
2040 return NULL;
2041
2042 return iter;
2043}
2044
2045/**
2046 * ftrace_rec_iter_next, get the next record to process.
2047 * @iter: The handle to the iterator.
2048 *
2049 * Returns the next iterator after the given iterator @iter.
2050 */
2051struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2052{
2053 iter->index++;
2054
2055 if (iter->index >= iter->pg->index) {
2056 iter->pg = iter->pg->next;
2057 iter->index = 0;
2058
2059 /* Could have empty pages */
2060 while (iter->pg && !iter->pg->index)
2061 iter->pg = iter->pg->next;
2062 }
2063
2064 if (!iter->pg)
2065 return NULL;
2066
2067 return iter;
2068}
2069
2070/**
2071 * ftrace_rec_iter_record, get the record at the iterator location
2072 * @iter: The current iterator location
2073 *
2074 * Returns the record that the current @iter is at.
2075 */
2076struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2077{
2078 return &iter->pg->records[iter->index];
2079}
2080
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302081static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002082ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002083{
2084 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002085 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002086
2087 ip = rec->ip;
2088
Steven Rostedt45a4a232011-04-21 23:16:46 -04002089 if (unlikely(ftrace_disabled))
2090 return 0;
2091
Shaohua Li25aac9d2009-01-09 11:29:40 +08002092 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002093 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002094 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302095 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002096 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302097 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002098}
2099
Steven Rostedt000ab692009-02-17 13:35:06 -05002100/*
2101 * archs can override this function if they must do something
2102 * before the modifying code is performed.
2103 */
2104int __weak ftrace_arch_code_modify_prepare(void)
2105{
2106 return 0;
2107}
2108
2109/*
2110 * archs can override this function if they must do something
2111 * after the modifying code is performed.
2112 */
2113int __weak ftrace_arch_code_modify_post_process(void)
2114{
2115 return 0;
2116}
2117
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002118void ftrace_modify_all_code(int command)
2119{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002120 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002121 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002122
2123 /*
2124 * If the ftrace_caller calls a ftrace_ops func directly,
2125 * we need to make sure that it only traces functions it
2126 * expects to trace. When doing the switch of functions,
2127 * we need to update to the ftrace_ops_list_func first
2128 * before the transition between old and new calls are set,
2129 * as the ftrace_ops_list_func will check the ops hashes
2130 * to make sure the ops are having the right functions
2131 * traced.
2132 */
Petr Mladekcd210672014-02-24 17:12:21 +01002133 if (update) {
2134 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2135 if (FTRACE_WARN_ON(err))
2136 return;
2137 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002138
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002139 if (command & FTRACE_UPDATE_CALLS)
2140 ftrace_replace_code(1);
2141 else if (command & FTRACE_DISABLE_CALLS)
2142 ftrace_replace_code(0);
2143
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002144 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2145 function_trace_op = set_function_trace_op;
2146 smp_wmb();
2147 /* If irqs are disabled, we are in stop machine */
2148 if (!irqs_disabled())
2149 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002150 err = ftrace_update_ftrace_func(ftrace_trace_function);
2151 if (FTRACE_WARN_ON(err))
2152 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002153 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002154
2155 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002156 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002157 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002158 err = ftrace_disable_ftrace_graph_caller();
2159 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002160}
2161
Ingo Molnare309b412008-05-12 21:20:51 +02002162static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002163{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002164 int *command = data;
2165
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002166 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002167
Steven Rostedtc88fd862011-08-16 09:53:39 -04002168 return 0;
2169}
2170
2171/**
2172 * ftrace_run_stop_machine, go back to the stop machine method
2173 * @command: The command to tell ftrace what to do
2174 *
2175 * If an arch needs to fall back to the stop machine method, the
2176 * it can call this function.
2177 */
2178void ftrace_run_stop_machine(int command)
2179{
2180 stop_machine(__ftrace_modify_code, &command, NULL);
2181}
2182
2183/**
2184 * arch_ftrace_update_code, modify the code to trace or not trace
2185 * @command: The command that needs to be done
2186 *
2187 * Archs can override this function if it does not need to
2188 * run stop_machine() to modify code.
2189 */
2190void __weak arch_ftrace_update_code(int command)
2191{
2192 ftrace_run_stop_machine(command);
2193}
2194
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002195static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2196{
2197 struct ftrace_page *pg;
2198 struct dyn_ftrace *rec;
2199 int size, bits;
2200 int ret;
2201
2202 size = ops->trampolines;
2203 bits = 0;
2204 /*
2205 * Make the hash size about 1/2 the # found
2206 */
2207 for (size /= 2; size; size >>= 1)
2208 bits++;
2209
2210 ops->tramp_hash = alloc_ftrace_hash(bits);
2211 /*
2212 * TODO: a failed allocation is going to screw up
2213 * the accounting of what needs to be modified
2214 * and not. For now, we kill ftrace if we fail
2215 * to allocate here. But there are ways around this,
2216 * but that will take a little more work.
2217 */
2218 if (!ops->tramp_hash)
2219 return -ENOMEM;
2220
2221 do_for_each_ftrace_rec(pg, rec) {
2222 if (ftrace_rec_count(rec) == 1 &&
2223 ftrace_ops_test(ops, rec->ip, rec)) {
2224
2225 /* This record had better have a trampoline */
2226 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2227 return -1;
2228
2229 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2230 if (ret < 0)
2231 return ret;
2232 }
2233 } while_for_each_ftrace_rec();
2234
2235 return 0;
2236}
2237
2238static int ftrace_save_tramp_hashes(void)
2239{
2240 struct ftrace_ops *op;
2241 int ret;
2242
2243 /*
2244 * Now that any trampoline is being used, we need to save the
2245 * hashes for the ops that have them. This allows the mapping
2246 * back from the record to the ops that has the trampoline to
2247 * know what code is being replaced. Modifying code must always
2248 * verify what it is changing.
2249 */
2250 do_for_each_ftrace_op(op, ftrace_ops_list) {
2251
2252 /* The tramp_hash is recreated each time. */
2253 free_ftrace_hash(op->tramp_hash);
2254 op->tramp_hash = NULL;
2255
2256 if (op->trampolines) {
2257 ret = ftrace_save_ops_tramp_hash(op);
2258 if (ret)
2259 return ret;
2260 }
2261
2262 } while_for_each_ftrace_op(op);
2263
2264 return 0;
2265}
2266
Steven Rostedtc88fd862011-08-16 09:53:39 -04002267static void ftrace_run_update_code(int command)
2268{
2269 int ret;
2270
2271 ret = ftrace_arch_code_modify_prepare();
2272 FTRACE_WARN_ON(ret);
2273 if (ret)
2274 return;
2275 /*
2276 * Do not call function tracer while we update the code.
2277 * We are in stop machine.
2278 */
2279 function_trace_stop++;
2280
2281 /*
2282 * By default we use stop_machine() to modify the code.
2283 * But archs can do what ever they want as long as it
2284 * is safe. The stop_machine() is the safest, but also
2285 * produces the most overhead.
2286 */
2287 arch_ftrace_update_code(command);
2288
Steven Rostedt6331c282011-07-13 15:11:02 -04002289 function_trace_stop--;
2290
Steven Rostedt000ab692009-02-17 13:35:06 -05002291 ret = ftrace_arch_code_modify_post_process();
2292 FTRACE_WARN_ON(ret);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002293
2294 ret = ftrace_save_tramp_hashes();
2295 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002296}
2297
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002298static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002299static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002300static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002301
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002302static void control_ops_free(struct ftrace_ops *ops)
2303{
2304 free_percpu(ops->disabled);
2305}
2306
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002307static void ftrace_startup_enable(int command)
2308{
2309 if (saved_ftrace_func != ftrace_trace_function) {
2310 saved_ftrace_func = ftrace_trace_function;
2311 command |= FTRACE_UPDATE_TRACE_FUNC;
2312 }
2313
2314 if (!command || !ftrace_enabled)
2315 return;
2316
2317 ftrace_run_update_code(command);
2318}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002319
Steven Rostedta1cd6172011-05-23 15:24:25 -04002320static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002321{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002322 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002323
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002324 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002325 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002326
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002327 ret = __register_ftrace_function(ops);
2328 if (ret)
2329 return ret;
2330
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002331 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002332 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002333
Steven Rostedted926f92011-05-03 13:25:24 -04002334 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002335
2336 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002337
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002338 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002339
2340 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002341}
2342
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002343static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002344{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002345 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002346
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002347 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002348 return -ENODEV;
2349
2350 ret = __unregister_ftrace_function(ops);
2351 if (ret)
2352 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002353
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002354 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002355 /*
2356 * Just warn in case of unbalance, no need to kill ftrace, it's not
2357 * critical but the ftrace_call callers may be never nopped again after
2358 * further ftrace uses.
2359 */
2360 WARN_ON_ONCE(ftrace_start_up < 0);
2361
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002362 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002363
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05002364 if (!global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002365 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002366
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002367 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002368
2369 if (saved_ftrace_func != ftrace_trace_function) {
2370 saved_ftrace_func = ftrace_trace_function;
2371 command |= FTRACE_UPDATE_TRACE_FUNC;
2372 }
2373
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002374 if (!command || !ftrace_enabled) {
2375 /*
2376 * If these are control ops, they still need their
2377 * per_cpu field freed. Since, function tracing is
2378 * not currently active, we can just free them
2379 * without synchronizing all CPUs.
2380 */
2381 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2382 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002383 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002384 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002385
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002386 /*
2387 * If the ops uses a trampoline, then it needs to be
2388 * tested first on update.
2389 */
2390 removed_ops = ops;
2391
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002392 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002393
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002394 removed_ops = NULL;
2395
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002396 /*
2397 * Dynamic ops may be freed, we must make sure that all
2398 * callers are done before leaving this function.
2399 * The same goes for freeing the per_cpu data of the control
2400 * ops.
2401 *
2402 * Again, normal synchronize_sched() is not good enough.
2403 * We need to do a hard force of sched synchronization.
2404 * This is because we use preempt_disable() to do RCU, but
2405 * the function tracers can be called where RCU is not watching
2406 * (like before user_exit()). We can not rely on the RCU
2407 * infrastructure to do the synchronization, thus we must do it
2408 * ourselves.
2409 */
2410 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2411 schedule_on_each_cpu(ftrace_sync);
2412
2413 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2414 control_ops_free(ops);
2415 }
2416
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002417 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002418}
2419
Ingo Molnare309b412008-05-12 21:20:51 +02002420static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002421{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002422 if (unlikely(ftrace_disabled))
2423 return;
2424
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002425 /* Force update next time */
2426 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002427 /* ftrace_start_up is true if we want ftrace running */
2428 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002429 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002430}
2431
Ingo Molnare309b412008-05-12 21:20:51 +02002432static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002433{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002434 if (unlikely(ftrace_disabled))
2435 return;
2436
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002437 /* ftrace_start_up is true if ftrace is running */
2438 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002439 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002440}
2441
Steven Rostedt3d083392008-05-12 21:20:42 +02002442static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002443unsigned long ftrace_update_tot_cnt;
2444
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002445static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002446{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002447 /*
2448 * Filter_hash being empty will default to trace module.
2449 * But notrace hash requires a test of individual module functions.
2450 */
2451 return ftrace_hash_empty(ops->filter_hash) &&
2452 ftrace_hash_empty(ops->notrace_hash);
2453}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002454
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002455/*
2456 * Check if the current ops references the record.
2457 *
2458 * If the ops traces all functions, then it was already accounted for.
2459 * If the ops does not trace the current record function, skip it.
2460 * If the ops ignores the function via notrace filter, skip it.
2461 */
2462static inline bool
2463ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2464{
2465 /* If ops isn't enabled, ignore it */
2466 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2467 return 0;
2468
2469 /* If ops traces all mods, we already accounted for it */
2470 if (ops_traces_mod(ops))
2471 return 0;
2472
2473 /* The function must be in the filter */
2474 if (!ftrace_hash_empty(ops->filter_hash) &&
2475 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2476 return 0;
2477
2478 /* If in notrace hash, we ignore it too */
2479 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2480 return 0;
2481
2482 return 1;
2483}
2484
2485static int referenced_filters(struct dyn_ftrace *rec)
2486{
2487 struct ftrace_ops *ops;
2488 int cnt = 0;
2489
2490 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2491 if (ops_references_rec(ops, rec))
2492 cnt++;
2493 }
2494
2495 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002496}
2497
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002498static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002499{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002500 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002501 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302502 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002503 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002504 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002505 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002506 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002507
2508 /*
2509 * When adding a module, we need to check if tracers are
2510 * currently enabled and if they are set to trace all functions.
2511 * If they are, we need to enable the module functions as well
2512 * as update the reference counts for those function records.
2513 */
2514 if (mod) {
2515 struct ftrace_ops *ops;
2516
2517 for (ops = ftrace_ops_list;
2518 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002519 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2520 if (ops_traces_mod(ops))
2521 ref++;
2522 else
2523 test = true;
2524 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002525 }
2526 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002527
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002528 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002529
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002530 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302531
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002532 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002533 int cnt = ref;
2534
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002535 /* If something went wrong, bail without enabling anything */
2536 if (unlikely(ftrace_disabled))
2537 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002538
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002539 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002540 if (test)
2541 cnt += referenced_filters(p);
2542 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302543
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002544 /*
2545 * Do the initial record conversion from mcount jump
2546 * to the NOP instructions.
2547 */
2548 if (!ftrace_code_disable(mod, p))
2549 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002550
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002551 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002552
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002553 /*
2554 * If the tracing is enabled, go ahead and enable the record.
2555 *
2556 * The reason not to enable the record immediatelly is the
2557 * inherent check of ftrace_make_nop/ftrace_make_call for
2558 * correct previous instructions. Making first the NOP
2559 * conversion puts the module to the correct state, thus
2560 * passing the ftrace_make_call check.
2561 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002562 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002563 int failed = __ftrace_replace_code(p, 1);
2564 if (failed)
2565 ftrace_bug(failed, p->ip);
2566 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002567 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002568 }
2569
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002570 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002571 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002572 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002573
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002574 return 0;
2575}
2576
Steven Rostedta7900872011-12-16 16:23:44 -05002577static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002578{
Steven Rostedta7900872011-12-16 16:23:44 -05002579 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002580 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002581
Steven Rostedta7900872011-12-16 16:23:44 -05002582 if (WARN_ON(!count))
2583 return -EINVAL;
2584
2585 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002586
2587 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002588 * We want to fill as much as possible. No more than a page
2589 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002590 */
Steven Rostedta7900872011-12-16 16:23:44 -05002591 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2592 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002593
Steven Rostedta7900872011-12-16 16:23:44 -05002594 again:
2595 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2596
2597 if (!pg->records) {
2598 /* if we can't allocate this size, try something smaller */
2599 if (!order)
2600 return -ENOMEM;
2601 order >>= 1;
2602 goto again;
2603 }
2604
2605 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2606 pg->size = cnt;
2607
2608 if (cnt > count)
2609 cnt = count;
2610
2611 return cnt;
2612}
2613
2614static struct ftrace_page *
2615ftrace_allocate_pages(unsigned long num_to_init)
2616{
2617 struct ftrace_page *start_pg;
2618 struct ftrace_page *pg;
2619 int order;
2620 int cnt;
2621
2622 if (!num_to_init)
2623 return 0;
2624
2625 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2626 if (!pg)
2627 return NULL;
2628
2629 /*
2630 * Try to allocate as much as possible in one continues
2631 * location that fills in all of the space. We want to
2632 * waste as little space as possible.
2633 */
2634 for (;;) {
2635 cnt = ftrace_allocate_records(pg, num_to_init);
2636 if (cnt < 0)
2637 goto free_pages;
2638
2639 num_to_init -= cnt;
2640 if (!num_to_init)
2641 break;
2642
2643 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2644 if (!pg->next)
2645 goto free_pages;
2646
2647 pg = pg->next;
2648 }
2649
2650 return start_pg;
2651
2652 free_pages:
2653 while (start_pg) {
2654 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2655 free_pages((unsigned long)pg->records, order);
2656 start_pg = pg->next;
2657 kfree(pg);
2658 pg = start_pg;
2659 }
2660 pr_info("ftrace: FAILED to allocate memory for functions\n");
2661 return NULL;
2662}
2663
Steven Rostedt5072c592008-05-12 21:20:43 +02002664#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2665
2666struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002667 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002668 loff_t func_pos;
2669 struct ftrace_page *pg;
2670 struct dyn_ftrace *func;
2671 struct ftrace_func_probe *probe;
2672 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002673 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002674 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002675 int hidx;
2676 int idx;
2677 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002678};
2679
Ingo Molnare309b412008-05-12 21:20:51 +02002680static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002681t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002682{
2683 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002684 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002685 struct hlist_head *hhd;
2686
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002687 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002688 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002689
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002690 if (iter->probe)
2691 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002692 retry:
2693 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2694 return NULL;
2695
2696 hhd = &ftrace_func_hash[iter->hidx];
2697
2698 if (hlist_empty(hhd)) {
2699 iter->hidx++;
2700 hnd = NULL;
2701 goto retry;
2702 }
2703
2704 if (!hnd)
2705 hnd = hhd->first;
2706 else {
2707 hnd = hnd->next;
2708 if (!hnd) {
2709 iter->hidx++;
2710 goto retry;
2711 }
2712 }
2713
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002714 if (WARN_ON_ONCE(!hnd))
2715 return NULL;
2716
2717 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2718
2719 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002720}
2721
2722static void *t_hash_start(struct seq_file *m, loff_t *pos)
2723{
2724 struct ftrace_iterator *iter = m->private;
2725 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002726 loff_t l;
2727
Steven Rostedt69a30832011-12-19 15:21:16 -05002728 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2729 return NULL;
2730
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002731 if (iter->func_pos > *pos)
2732 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002733
Li Zefand82d6242009-06-24 09:54:54 +08002734 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002735 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002736 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002737 if (!p)
2738 break;
2739 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002740 if (!p)
2741 return NULL;
2742
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002743 /* Only set this if we have an item */
2744 iter->flags |= FTRACE_ITER_HASH;
2745
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002746 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002747}
2748
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002749static int
2750t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002751{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002752 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002753
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002754 rec = iter->probe;
2755 if (WARN_ON_ONCE(!rec))
2756 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002757
Steven Rostedt809dcf22009-02-16 23:06:01 -05002758 if (rec->ops->print)
2759 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2760
Steven Rostedtb375a112009-09-17 00:05:58 -04002761 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002762
2763 if (rec->data)
2764 seq_printf(m, ":%p", rec->data);
2765 seq_putc(m, '\n');
2766
2767 return 0;
2768}
2769
2770static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002771t_next(struct seq_file *m, void *v, loff_t *pos)
2772{
2773 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002774 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002775 struct dyn_ftrace *rec = NULL;
2776
Steven Rostedt45a4a232011-04-21 23:16:46 -04002777 if (unlikely(ftrace_disabled))
2778 return NULL;
2779
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002780 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002781 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002782
Steven Rostedt5072c592008-05-12 21:20:43 +02002783 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002784 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002785
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002786 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002787 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002788
Steven Rostedt5072c592008-05-12 21:20:43 +02002789 retry:
2790 if (iter->idx >= iter->pg->index) {
2791 if (iter->pg->next) {
2792 iter->pg = iter->pg->next;
2793 iter->idx = 0;
2794 goto retry;
2795 }
2796 } else {
2797 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002798 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002799 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002800
Steven Rostedt41c52c02008-05-22 11:46:33 -04002801 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002802 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2803
2804 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002805 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002806
Steven Rostedt5072c592008-05-12 21:20:43 +02002807 rec = NULL;
2808 goto retry;
2809 }
2810 }
2811
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002812 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002813 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002814
2815 iter->func = rec;
2816
2817 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002818}
2819
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002820static void reset_iter_read(struct ftrace_iterator *iter)
2821{
2822 iter->pos = 0;
2823 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002824 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002825}
2826
2827static void *t_start(struct seq_file *m, loff_t *pos)
2828{
2829 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002830 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002831 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002832 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002833
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002834 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002835
2836 if (unlikely(ftrace_disabled))
2837 return NULL;
2838
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002839 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002840 * If an lseek was done, then reset and start from beginning.
2841 */
2842 if (*pos < iter->pos)
2843 reset_iter_read(iter);
2844
2845 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002846 * For set_ftrace_filter reading, if we have the filter
2847 * off, we can short cut and just print out that all
2848 * functions are enabled.
2849 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002850 if (iter->flags & FTRACE_ITER_FILTER &&
2851 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002852 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002853 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002854 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002855 /* reset in case of seek/pread */
2856 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002857 return iter;
2858 }
2859
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002860 if (iter->flags & FTRACE_ITER_HASH)
2861 return t_hash_start(m, pos);
2862
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002863 /*
2864 * Unfortunately, we need to restart at ftrace_pages_start
2865 * every time we let go of the ftrace_mutex. This is because
2866 * those pointers can change without the lock.
2867 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002868 iter->pg = ftrace_pages_start;
2869 iter->idx = 0;
2870 for (l = 0; l <= *pos; ) {
2871 p = t_next(m, p, &l);
2872 if (!p)
2873 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002874 }
walimis5821e1b2008-11-15 15:19:06 +08002875
Steven Rostedt69a30832011-12-19 15:21:16 -05002876 if (!p)
2877 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002878
2879 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002880}
2881
2882static void t_stop(struct seq_file *m, void *p)
2883{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002884 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002885}
2886
2887static int t_show(struct seq_file *m, void *v)
2888{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002889 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002890 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002891
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002892 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002893 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002894
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002895 if (iter->flags & FTRACE_ITER_PRINTALL) {
2896 seq_printf(m, "#### all functions enabled ####\n");
2897 return 0;
2898 }
2899
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002900 rec = iter->func;
2901
Steven Rostedt5072c592008-05-12 21:20:43 +02002902 if (!rec)
2903 return 0;
2904
Steven Rostedt647bcd02011-05-03 14:39:21 -04002905 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002906 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002907 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002908 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002909 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2910 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2911 struct ftrace_ops *ops;
2912
2913 ops = ftrace_find_tramp_ops_curr(rec);
2914 if (ops && ops->trampoline)
2915 seq_printf(m, "\ttramp: %pS",
2916 (void *)ops->trampoline);
2917 else
2918 seq_printf(m, "\ttramp: ERROR!");
2919 }
2920 }
2921
Steven Rostedt647bcd02011-05-03 14:39:21 -04002922 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002923
2924 return 0;
2925}
2926
James Morris88e9d342009-09-22 16:43:43 -07002927static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002928 .start = t_start,
2929 .next = t_next,
2930 .stop = t_stop,
2931 .show = t_show,
2932};
2933
Ingo Molnare309b412008-05-12 21:20:51 +02002934static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002935ftrace_avail_open(struct inode *inode, struct file *file)
2936{
2937 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002938
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002939 if (unlikely(ftrace_disabled))
2940 return -ENODEV;
2941
Jiri Olsa50e18b92012-04-25 10:23:39 +02002942 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2943 if (iter) {
2944 iter->pg = ftrace_pages_start;
2945 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002946 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002947
Jiri Olsa50e18b92012-04-25 10:23:39 +02002948 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002949}
2950
Steven Rostedt647bcd02011-05-03 14:39:21 -04002951static int
2952ftrace_enabled_open(struct inode *inode, struct file *file)
2953{
2954 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002955
2956 if (unlikely(ftrace_disabled))
2957 return -ENODEV;
2958
Jiri Olsa50e18b92012-04-25 10:23:39 +02002959 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2960 if (iter) {
2961 iter->pg = ftrace_pages_start;
2962 iter->flags = FTRACE_ITER_ENABLED;
2963 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002964 }
2965
Jiri Olsa50e18b92012-04-25 10:23:39 +02002966 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002967}
2968
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002969static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002970{
Steven Rostedt52baf112009-02-14 01:15:39 -05002971 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002972 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002973 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002974}
2975
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002976/**
2977 * ftrace_regex_open - initialize function tracer filter files
2978 * @ops: The ftrace_ops that hold the hash filters
2979 * @flag: The type of filter to process
2980 * @inode: The inode, usually passed in to your open routine
2981 * @file: The file, usually passed in to your open routine
2982 *
2983 * ftrace_regex_open() initializes the filter files for the
2984 * @ops. Depending on @flag it may process the filter hash or
2985 * the notrace hash of @ops. With this called from the open
2986 * routine, you can use ftrace_filter_write() for the write
2987 * routine if @flag has FTRACE_ITER_FILTER set, or
2988 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002989 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002990 * release must call ftrace_regex_release().
2991 */
2992int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002993ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002994 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002995{
2996 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002997 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002998 int ret = 0;
2999
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003000 ftrace_ops_init(ops);
3001
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003002 if (unlikely(ftrace_disabled))
3003 return -ENODEV;
3004
Steven Rostedt5072c592008-05-12 21:20:43 +02003005 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3006 if (!iter)
3007 return -ENOMEM;
3008
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003009 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3010 kfree(iter);
3011 return -ENOMEM;
3012 }
3013
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003014 iter->ops = ops;
3015 iter->flags = flag;
3016
3017 mutex_lock(&ops->regex_lock);
3018
Steven Rostedtf45948e2011-05-02 12:29:25 -04003019 if (flag & FTRACE_ITER_NOTRACE)
3020 hash = ops->notrace_hash;
3021 else
3022 hash = ops->filter_hash;
3023
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003024 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003025 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003026 if (!iter->hash) {
3027 trace_parser_put(&iter->parser);
3028 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003029 ret = -ENOMEM;
3030 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003031 }
3032 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003033
Steven Rostedt5072c592008-05-12 21:20:43 +02003034 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003035 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003036 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02003037
3038 if (file->f_mode & FMODE_READ) {
3039 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003040
3041 ret = seq_open(file, &show_ftrace_seq_ops);
3042 if (!ret) {
3043 struct seq_file *m = file->private_data;
3044 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003045 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003046 /* Failed */
3047 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003048 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003049 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003050 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003051 } else
3052 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003053
3054 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003055 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003056
3057 return ret;
3058}
3059
Steven Rostedt41c52c02008-05-22 11:46:33 -04003060static int
3061ftrace_filter_open(struct inode *inode, struct file *file)
3062{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003063 struct ftrace_ops *ops = inode->i_private;
3064
3065 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003066 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3067 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003068}
3069
3070static int
3071ftrace_notrace_open(struct inode *inode, struct file *file)
3072{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003073 struct ftrace_ops *ops = inode->i_private;
3074
3075 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003076 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003077}
3078
Steven Rostedt64e7c442009-02-13 17:08:48 -05003079static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003080{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003081 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003082 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003083
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003084 switch (type) {
3085 case MATCH_FULL:
3086 if (strcmp(str, regex) == 0)
3087 matched = 1;
3088 break;
3089 case MATCH_FRONT_ONLY:
3090 if (strncmp(str, regex, len) == 0)
3091 matched = 1;
3092 break;
3093 case MATCH_MIDDLE_ONLY:
3094 if (strstr(str, regex))
3095 matched = 1;
3096 break;
3097 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003098 slen = strlen(str);
3099 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003100 matched = 1;
3101 break;
3102 }
3103
3104 return matched;
3105}
3106
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003107static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003108enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003109{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003110 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003111 int ret = 0;
3112
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003113 entry = ftrace_lookup_ip(hash, rec->ip);
3114 if (not) {
3115 /* Do nothing if it doesn't exist */
3116 if (!entry)
3117 return 0;
3118
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003119 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003120 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003121 /* Do nothing if it exists */
3122 if (entry)
3123 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003124
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003125 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003126 }
3127 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003128}
3129
Steven Rostedt64e7c442009-02-13 17:08:48 -05003130static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003131ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3132 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003133{
3134 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003135 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003136
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003137 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3138
3139 if (mod) {
3140 /* module lookup requires matching the module */
3141 if (!modname || strcmp(modname, mod))
3142 return 0;
3143
3144 /* blank search means to match all funcs in the mod */
3145 if (!len)
3146 return 1;
3147 }
3148
Steven Rostedt64e7c442009-02-13 17:08:48 -05003149 return ftrace_match(str, regex, len, type);
3150}
3151
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003152static int
3153match_records(struct ftrace_hash *hash, char *buff,
3154 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003155{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003156 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003157 struct ftrace_page *pg;
3158 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003159 int type = MATCH_FULL;
3160 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003161 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003162 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003163
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003164 if (len) {
3165 type = filter_parse_regex(buff, len, &search, &not);
3166 search_len = strlen(search);
3167 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003168
Steven Rostedt52baf112009-02-14 01:15:39 -05003169 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003170
3171 if (unlikely(ftrace_disabled))
3172 goto out_unlock;
3173
Steven Rostedt265c8312009-02-13 12:43:56 -05003174 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003175 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003176 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003177 if (ret < 0) {
3178 found = ret;
3179 goto out_unlock;
3180 }
Li Zefan311d16d2009-12-08 11:15:11 +08003181 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003182 }
3183 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003184 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003185 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003186
3187 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003188}
3189
Steven Rostedt64e7c442009-02-13 17:08:48 -05003190static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003191ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003192{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003193 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003194}
3195
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003196static int
3197ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003198{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003199 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003200
Steven Rostedt64e7c442009-02-13 17:08:48 -05003201 /* blank or '*' mean the same */
3202 if (strcmp(buff, "*") == 0)
3203 buff[0] = 0;
3204
3205 /* handle the case of 'dont filter this module' */
3206 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3207 buff[0] = 0;
3208 not = 1;
3209 }
3210
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003211 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003212}
3213
Steven Rostedtf6180772009-02-14 00:40:25 -05003214/*
3215 * We register the module command as a template to show others how
3216 * to register the a command as well.
3217 */
3218
3219static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003220ftrace_mod_callback(struct ftrace_hash *hash,
3221 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003222{
3223 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003224 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003225
3226 /*
3227 * cmd == 'mod' because we only registered this func
3228 * for the 'mod' ftrace_func_command.
3229 * But if you register one func with multiple commands,
3230 * you can tell which command was used by the cmd
3231 * parameter.
3232 */
3233
3234 /* we must have a module name */
3235 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003236 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003237
3238 mod = strsep(&param, ":");
3239 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003240 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003241
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003242 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003243 if (!ret)
3244 ret = -EINVAL;
3245 if (ret < 0)
3246 return ret;
3247
3248 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003249}
3250
3251static struct ftrace_func_command ftrace_mod_cmd = {
3252 .name = "mod",
3253 .func = ftrace_mod_callback,
3254};
3255
3256static int __init ftrace_mod_cmd_init(void)
3257{
3258 return register_ftrace_command(&ftrace_mod_cmd);
3259}
Steven Rostedt6f415672012-10-05 12:13:07 -04003260core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003261
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003262static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003263 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003264{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003265 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003266 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003267 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003268
3269 key = hash_long(ip, FTRACE_HASH_BITS);
3270
3271 hhd = &ftrace_func_hash[key];
3272
3273 if (hlist_empty(hhd))
3274 return;
3275
3276 /*
3277 * Disable preemption for these calls to prevent a RCU grace
3278 * period. This syncs the hash iteration and freeing of items
3279 * on the hash. rcu_read_lock is too dangerous here.
3280 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003281 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003282 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003283 if (entry->ip == ip)
3284 entry->ops->func(ip, parent_ip, &entry->data);
3285 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003286 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003287}
3288
Steven Rostedtb6887d72009-02-17 12:32:04 -05003289static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003290{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003291 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003292 .flags = FTRACE_OPS_FL_INITIALIZED,
3293 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003294};
3295
Steven Rostedtb6887d72009-02-17 12:32:04 -05003296static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003297
Steven Rostedtb6887d72009-02-17 12:32:04 -05003298static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003299{
Steven Rostedtb8489142011-05-04 09:27:52 -04003300 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003301 int i;
3302
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003303 if (ftrace_probe_registered) {
3304 /* still need to update the function call sites */
3305 if (ftrace_enabled)
3306 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003307 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003308 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003309
3310 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3311 struct hlist_head *hhd = &ftrace_func_hash[i];
3312 if (hhd->first)
3313 break;
3314 }
3315 /* Nothing registered? */
3316 if (i == FTRACE_FUNC_HASHSIZE)
3317 return;
3318
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003319 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003320
Steven Rostedtb6887d72009-02-17 12:32:04 -05003321 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003322}
3323
Steven Rostedtb6887d72009-02-17 12:32:04 -05003324static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003325{
3326 int i;
3327
Steven Rostedtb6887d72009-02-17 12:32:04 -05003328 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003329 return;
3330
3331 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3332 struct hlist_head *hhd = &ftrace_func_hash[i];
3333 if (hhd->first)
3334 return;
3335 }
3336
3337 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003338 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003339
Steven Rostedtb6887d72009-02-17 12:32:04 -05003340 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003341}
3342
3343
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003344static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003345{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003346 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003347 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003348 kfree(entry);
3349}
3350
Steven Rostedt59df055f2009-02-14 15:29:06 -05003351int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003352register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003353 void *data)
3354{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003355 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003356 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3357 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003358 struct ftrace_page *pg;
3359 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003360 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003361 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003362 int count = 0;
3363 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003364 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003365
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003366 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003367 len = strlen(search);
3368
Steven Rostedtb6887d72009-02-17 12:32:04 -05003369 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003370 if (WARN_ON(not))
3371 return -EINVAL;
3372
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003373 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003374
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003375 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3376 if (!hash) {
3377 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003378 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003379 }
3380
3381 if (unlikely(ftrace_disabled)) {
3382 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003383 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003384 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003385
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003386 mutex_lock(&ftrace_lock);
3387
Steven Rostedt59df055f2009-02-14 15:29:06 -05003388 do_for_each_ftrace_rec(pg, rec) {
3389
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003390 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003391 continue;
3392
3393 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3394 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003395 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003396 if (!count)
3397 count = -ENOMEM;
3398 goto out_unlock;
3399 }
3400
3401 count++;
3402
3403 entry->data = data;
3404
3405 /*
3406 * The caller might want to do something special
3407 * for each function we find. We call the callback
3408 * to give the caller an opportunity to do so.
3409 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003410 if (ops->init) {
3411 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003412 /* caller does not like this func */
3413 kfree(entry);
3414 continue;
3415 }
3416 }
3417
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003418 ret = enter_record(hash, rec, 0);
3419 if (ret < 0) {
3420 kfree(entry);
3421 count = ret;
3422 goto out_unlock;
3423 }
3424
Steven Rostedt59df055f2009-02-14 15:29:06 -05003425 entry->ops = ops;
3426 entry->ip = rec->ip;
3427
3428 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3429 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3430
3431 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003432
3433 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3434 if (ret < 0)
3435 count = ret;
3436
Steven Rostedtb6887d72009-02-17 12:32:04 -05003437 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003438
3439 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003440 mutex_unlock(&ftrace_lock);
3441 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003442 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003443 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003444
3445 return count;
3446}
3447
3448enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003449 PROBE_TEST_FUNC = 1,
3450 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003451};
3452
3453static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003454__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003455 void *data, int flags)
3456{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003457 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003458 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003459 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003460 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003461 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003462 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003463 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003464 char str[KSYM_SYMBOL_LEN];
3465 int type = MATCH_FULL;
3466 int i, len = 0;
3467 char *search;
3468
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003469 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003470 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003471 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003472 int not;
3473
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003474 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003475 len = strlen(search);
3476
Steven Rostedtb6887d72009-02-17 12:32:04 -05003477 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003478 if (WARN_ON(not))
3479 return;
3480 }
3481
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003482 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003483
3484 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3485 if (!hash)
3486 /* Hmm, should report this somehow */
3487 goto out_unlock;
3488
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003489 INIT_LIST_HEAD(&free_list);
3490
Steven Rostedt59df055f2009-02-14 15:29:06 -05003491 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3492 struct hlist_head *hhd = &ftrace_func_hash[i];
3493
Sasha Levinb67bfe02013-02-27 17:06:00 -08003494 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003495
3496 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003497 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003498 continue;
3499
Steven Rostedtb6887d72009-02-17 12:32:04 -05003500 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003501 continue;
3502
3503 /* do this last, since it is the most expensive */
3504 if (glob) {
3505 kallsyms_lookup(entry->ip, NULL, NULL,
3506 NULL, str);
3507 if (!ftrace_match(str, glob, len, type))
3508 continue;
3509 }
3510
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003511 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3512 /* It is possible more than one entry had this ip */
3513 if (rec_entry)
3514 free_hash_entry(hash, rec_entry);
3515
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003516 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003517 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003518 }
3519 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003520 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003521 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003522 /*
3523 * Remove after the disable is called. Otherwise, if the last
3524 * probe is removed, a null hash means *all enabled*.
3525 */
3526 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003527 synchronize_sched();
3528 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3529 list_del(&entry->free_list);
3530 ftrace_free_entry(entry);
3531 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003532 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003533
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003534 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003535 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003536 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003537}
3538
3539void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003540unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003541 void *data)
3542{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003543 __unregister_ftrace_function_probe(glob, ops, data,
3544 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003545}
3546
3547void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003548unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003549{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003550 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003551}
3552
Steven Rostedtb6887d72009-02-17 12:32:04 -05003553void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003554{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003555 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003556}
3557
Steven Rostedtf6180772009-02-14 00:40:25 -05003558static LIST_HEAD(ftrace_commands);
3559static DEFINE_MUTEX(ftrace_cmd_mutex);
3560
Tom Zanussi38de93a2013-10-24 08:34:18 -05003561/*
3562 * Currently we only register ftrace commands from __init, so mark this
3563 * __init too.
3564 */
3565__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003566{
3567 struct ftrace_func_command *p;
3568 int ret = 0;
3569
3570 mutex_lock(&ftrace_cmd_mutex);
3571 list_for_each_entry(p, &ftrace_commands, list) {
3572 if (strcmp(cmd->name, p->name) == 0) {
3573 ret = -EBUSY;
3574 goto out_unlock;
3575 }
3576 }
3577 list_add(&cmd->list, &ftrace_commands);
3578 out_unlock:
3579 mutex_unlock(&ftrace_cmd_mutex);
3580
3581 return ret;
3582}
3583
Tom Zanussi38de93a2013-10-24 08:34:18 -05003584/*
3585 * Currently we only unregister ftrace commands from __init, so mark
3586 * this __init too.
3587 */
3588__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003589{
3590 struct ftrace_func_command *p, *n;
3591 int ret = -ENODEV;
3592
3593 mutex_lock(&ftrace_cmd_mutex);
3594 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3595 if (strcmp(cmd->name, p->name) == 0) {
3596 ret = 0;
3597 list_del_init(&p->list);
3598 goto out_unlock;
3599 }
3600 }
3601 out_unlock:
3602 mutex_unlock(&ftrace_cmd_mutex);
3603
3604 return ret;
3605}
3606
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003607static int ftrace_process_regex(struct ftrace_hash *hash,
3608 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003609{
Steven Rostedtf6180772009-02-14 00:40:25 -05003610 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003611 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003612 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003613
3614 func = strsep(&next, ":");
3615
3616 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003617 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003618 if (!ret)
3619 ret = -EINVAL;
3620 if (ret < 0)
3621 return ret;
3622 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003623 }
3624
Steven Rostedtf6180772009-02-14 00:40:25 -05003625 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003626
3627 command = strsep(&next, ":");
3628
Steven Rostedtf6180772009-02-14 00:40:25 -05003629 mutex_lock(&ftrace_cmd_mutex);
3630 list_for_each_entry(p, &ftrace_commands, list) {
3631 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003632 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003633 goto out_unlock;
3634 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003635 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003636 out_unlock:
3637 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003638
Steven Rostedtf6180772009-02-14 00:40:25 -05003639 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003640}
3641
Ingo Molnare309b412008-05-12 21:20:51 +02003642static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003643ftrace_regex_write(struct file *file, const char __user *ubuf,
3644 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003645{
3646 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003647 struct trace_parser *parser;
3648 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003649
Li Zefan4ba79782009-09-22 13:52:20 +08003650 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003651 return 0;
3652
Steven Rostedt5072c592008-05-12 21:20:43 +02003653 if (file->f_mode & FMODE_READ) {
3654 struct seq_file *m = file->private_data;
3655 iter = m->private;
3656 } else
3657 iter = file->private_data;
3658
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003659 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003660 return -ENODEV;
3661
3662 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003663
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003664 parser = &iter->parser;
3665 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003666
Li Zefan4ba79782009-09-22 13:52:20 +08003667 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003668 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003669 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003670 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003671 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003672 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003673 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003674 }
3675
Steven Rostedt5072c592008-05-12 21:20:43 +02003676 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003677 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003678 return ret;
3679}
3680
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003681ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003682ftrace_filter_write(struct file *file, const char __user *ubuf,
3683 size_t cnt, loff_t *ppos)
3684{
3685 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3686}
3687
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003688ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003689ftrace_notrace_write(struct file *file, const char __user *ubuf,
3690 size_t cnt, loff_t *ppos)
3691{
3692 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3693}
3694
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003695static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003696ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3697{
3698 struct ftrace_func_entry *entry;
3699
3700 if (!ftrace_location(ip))
3701 return -EINVAL;
3702
3703 if (remove) {
3704 entry = ftrace_lookup_ip(hash, ip);
3705 if (!entry)
3706 return -ENOENT;
3707 free_hash_entry(hash, entry);
3708 return 0;
3709 }
3710
3711 return add_hash_entry(hash, ip);
3712}
3713
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003714static void ftrace_ops_update_code(struct ftrace_ops *ops)
3715{
3716 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3717 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3718}
3719
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003720static int
3721ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3722 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003723{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003724 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003725 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003726 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003727
Steven Rostedt41c52c02008-05-22 11:46:33 -04003728 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003729 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003730
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003731 mutex_lock(&ops->regex_lock);
3732
Steven Rostedtf45948e2011-05-02 12:29:25 -04003733 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003734 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003735 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003736 orig_hash = &ops->notrace_hash;
3737
3738 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003739 if (!hash) {
3740 ret = -ENOMEM;
3741 goto out_regex_unlock;
3742 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003743
Steven Rostedt41c52c02008-05-22 11:46:33 -04003744 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003745 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003746 if (buf && !ftrace_match_records(hash, buf, len)) {
3747 ret = -EINVAL;
3748 goto out_regex_unlock;
3749 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003750 if (ip) {
3751 ret = ftrace_match_addr(hash, ip, remove);
3752 if (ret < 0)
3753 goto out_regex_unlock;
3754 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003755
3756 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003757 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003758 if (!ret)
3759 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003760
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003761 mutex_unlock(&ftrace_lock);
3762
Jiri Olsaac483c42012-01-02 10:04:14 +01003763 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003764 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003765
3766 free_ftrace_hash(hash);
3767 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003768}
3769
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003770static int
3771ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3772 int reset, int enable)
3773{
3774 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3775}
3776
3777/**
3778 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3779 * @ops - the ops to set the filter with
3780 * @ip - the address to add to or remove from the filter.
3781 * @remove - non zero to remove the ip from the filter
3782 * @reset - non zero to reset all filters before applying this filter.
3783 *
3784 * Filters denote which functions should be enabled when tracing is enabled
3785 * If @ip is NULL, it failes to update filter.
3786 */
3787int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3788 int remove, int reset)
3789{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003790 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003791 return ftrace_set_addr(ops, ip, remove, reset, 1);
3792}
3793EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3794
3795static int
3796ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3797 int reset, int enable)
3798{
3799 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3800}
3801
Steven Rostedt77a2b372008-05-12 21:20:45 +02003802/**
3803 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003804 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003805 * @buf - the string that holds the function filter text.
3806 * @len - the length of the string.
3807 * @reset - non zero to reset all filters before applying this filter.
3808 *
3809 * Filters denote which functions should be enabled when tracing is enabled.
3810 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3811 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003812int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003813 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003814{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003815 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003816 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003817}
Steven Rostedt936e0742011-05-05 22:54:01 -04003818EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003819
Steven Rostedt41c52c02008-05-22 11:46:33 -04003820/**
3821 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003822 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003823 * @buf - the string that holds the function notrace text.
3824 * @len - the length of the string.
3825 * @reset - non zero to reset all filters before applying this filter.
3826 *
3827 * Notrace Filters denote which functions should not be enabled when tracing
3828 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3829 * for tracing.
3830 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003831int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003832 int len, int reset)
3833{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003834 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003835 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003836}
3837EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3838/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003839 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003840 * @buf - the string that holds the function filter text.
3841 * @len - the length of the string.
3842 * @reset - non zero to reset all filters before applying this filter.
3843 *
3844 * Filters denote which functions should be enabled when tracing is enabled.
3845 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3846 */
3847void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3848{
3849 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3850}
3851EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3852
3853/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003854 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003855 * @buf - the string that holds the function notrace text.
3856 * @len - the length of the string.
3857 * @reset - non zero to reset all filters before applying this filter.
3858 *
3859 * Notrace Filters denote which functions should not be enabled when tracing
3860 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3861 * for tracing.
3862 */
3863void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003864{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003865 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003866}
Steven Rostedt936e0742011-05-05 22:54:01 -04003867EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003868
Steven Rostedt2af15d62009-05-28 13:37:24 -04003869/*
3870 * command line interface to allow users to set filters on boot up.
3871 */
3872#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3873static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3874static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3875
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003876/* Used by function selftest to not test if filter is set */
3877bool ftrace_filter_param __initdata;
3878
Steven Rostedt2af15d62009-05-28 13:37:24 -04003879static int __init set_ftrace_notrace(char *str)
3880{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003881 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003882 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003883 return 1;
3884}
3885__setup("ftrace_notrace=", set_ftrace_notrace);
3886
3887static int __init set_ftrace_filter(char *str)
3888{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003889 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003890 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003891 return 1;
3892}
3893__setup("ftrace_filter=", set_ftrace_filter);
3894
Stefan Assmann369bc182009-10-12 22:17:21 +02003895#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003896static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003897static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003898
Stefan Assmann369bc182009-10-12 22:17:21 +02003899static int __init set_graph_function(char *str)
3900{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003901 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003902 return 1;
3903}
3904__setup("ftrace_graph_filter=", set_graph_function);
3905
3906static void __init set_ftrace_early_graph(char *buf)
3907{
3908 int ret;
3909 char *func;
3910
3911 while (buf) {
3912 func = strsep(&buf, ",");
3913 /* we allow only one expression at a time */
3914 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003915 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003916 if (ret)
3917 printk(KERN_DEBUG "ftrace: function %s not "
3918 "traceable\n", func);
3919 }
3920}
3921#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3922
Steven Rostedt2a85a372011-12-19 21:57:44 -05003923void __init
3924ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003925{
3926 char *func;
3927
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003928 ftrace_ops_init(ops);
3929
Steven Rostedt2af15d62009-05-28 13:37:24 -04003930 while (buf) {
3931 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003932 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003933 }
3934}
3935
3936static void __init set_ftrace_early_filters(void)
3937{
3938 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003939 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003940 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003941 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003942#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3943 if (ftrace_graph_buf[0])
3944 set_ftrace_early_graph(ftrace_graph_buf);
3945#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003946}
3947
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003948int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003949{
3950 struct seq_file *m = (struct seq_file *)file->private_data;
3951 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003952 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003953 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003954 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003955 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003956
Steven Rostedt5072c592008-05-12 21:20:43 +02003957 if (file->f_mode & FMODE_READ) {
3958 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003959 seq_release(inode, file);
3960 } else
3961 iter = file->private_data;
3962
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003963 parser = &iter->parser;
3964 if (trace_parser_loaded(parser)) {
3965 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003966 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003967 }
3968
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003969 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003970
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003971 mutex_lock(&iter->ops->regex_lock);
3972
Steven Rostedt058e2972011-04-29 22:35:33 -04003973 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003974 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3975
3976 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003977 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003978 else
3979 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003980
Steven Rostedt058e2972011-04-29 22:35:33 -04003981 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003982 ret = ftrace_hash_move(iter->ops, filter_hash,
3983 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003984 if (!ret)
3985 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003986
Steven Rostedt058e2972011-04-29 22:35:33 -04003987 mutex_unlock(&ftrace_lock);
3988 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003989
3990 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003991 free_ftrace_hash(iter->hash);
3992 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003993
Steven Rostedt5072c592008-05-12 21:20:43 +02003994 return 0;
3995}
3996
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003997static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003998 .open = ftrace_avail_open,
3999 .read = seq_read,
4000 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004001 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004002};
4003
Steven Rostedt647bcd02011-05-03 14:39:21 -04004004static const struct file_operations ftrace_enabled_fops = {
4005 .open = ftrace_enabled_open,
4006 .read = seq_read,
4007 .llseek = seq_lseek,
4008 .release = seq_release_private,
4009};
4010
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004011static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004012 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004013 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004014 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004015 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004016 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004017};
4018
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004019static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004020 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004021 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004022 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004023 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004024 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004025};
4026
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004027#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4028
4029static DEFINE_MUTEX(graph_lock);
4030
4031int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004032int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004033unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004034unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004035
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004036struct ftrace_graph_data {
4037 unsigned long *table;
4038 size_t size;
4039 int *count;
4040 const struct seq_operations *seq_ops;
4041};
4042
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004043static void *
Li Zefan85951842009-06-24 09:54:00 +08004044__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004045{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004046 struct ftrace_graph_data *fgd = m->private;
4047
4048 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004049 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004050 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004051}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004052
Li Zefan85951842009-06-24 09:54:00 +08004053static void *
4054g_next(struct seq_file *m, void *v, loff_t *pos)
4055{
4056 (*pos)++;
4057 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004058}
4059
4060static void *g_start(struct seq_file *m, loff_t *pos)
4061{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004062 struct ftrace_graph_data *fgd = m->private;
4063
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004064 mutex_lock(&graph_lock);
4065
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004066 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004067 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004068 return (void *)1;
4069
Li Zefan85951842009-06-24 09:54:00 +08004070 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004071}
4072
4073static void g_stop(struct seq_file *m, void *p)
4074{
4075 mutex_unlock(&graph_lock);
4076}
4077
4078static int g_show(struct seq_file *m, void *v)
4079{
4080 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004081
4082 if (!ptr)
4083 return 0;
4084
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004085 if (ptr == (unsigned long *)1) {
4086 seq_printf(m, "#### all functions enabled ####\n");
4087 return 0;
4088 }
4089
Steven Rostedtb375a112009-09-17 00:05:58 -04004090 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004091
4092 return 0;
4093}
4094
James Morris88e9d342009-09-22 16:43:43 -07004095static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004096 .start = g_start,
4097 .next = g_next,
4098 .stop = g_stop,
4099 .show = g_show,
4100};
4101
4102static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004103__ftrace_graph_open(struct inode *inode, struct file *file,
4104 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004105{
4106 int ret = 0;
4107
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004108 mutex_lock(&graph_lock);
4109 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004110 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004111 *fgd->count = 0;
4112 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004113 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004114 mutex_unlock(&graph_lock);
4115
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004116 if (file->f_mode & FMODE_READ) {
4117 ret = seq_open(file, fgd->seq_ops);
4118 if (!ret) {
4119 struct seq_file *m = file->private_data;
4120 m->private = fgd;
4121 }
4122 } else
4123 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004124
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004125 return ret;
4126}
4127
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004128static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004129ftrace_graph_open(struct inode *inode, struct file *file)
4130{
4131 struct ftrace_graph_data *fgd;
4132
4133 if (unlikely(ftrace_disabled))
4134 return -ENODEV;
4135
4136 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4137 if (fgd == NULL)
4138 return -ENOMEM;
4139
4140 fgd->table = ftrace_graph_funcs;
4141 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4142 fgd->count = &ftrace_graph_count;
4143 fgd->seq_ops = &ftrace_graph_seq_ops;
4144
4145 return __ftrace_graph_open(inode, file, fgd);
4146}
4147
4148static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004149ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4150{
4151 struct ftrace_graph_data *fgd;
4152
4153 if (unlikely(ftrace_disabled))
4154 return -ENODEV;
4155
4156 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4157 if (fgd == NULL)
4158 return -ENOMEM;
4159
4160 fgd->table = ftrace_graph_notrace_funcs;
4161 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4162 fgd->count = &ftrace_graph_notrace_count;
4163 fgd->seq_ops = &ftrace_graph_seq_ops;
4164
4165 return __ftrace_graph_open(inode, file, fgd);
4166}
4167
4168static int
Li Zefan87827112009-07-23 11:29:11 +08004169ftrace_graph_release(struct inode *inode, struct file *file)
4170{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004171 if (file->f_mode & FMODE_READ) {
4172 struct seq_file *m = file->private_data;
4173
4174 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004175 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004176 } else {
4177 kfree(file->private_data);
4178 }
4179
Li Zefan87827112009-07-23 11:29:11 +08004180 return 0;
4181}
4182
4183static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004184ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004185{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004186 struct dyn_ftrace *rec;
4187 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004188 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004189 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004190 int type, not;
4191 char *search;
4192 bool exists;
4193 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004194
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004195 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004196 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004197 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004198 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004199
4200 search_len = strlen(search);
4201
Steven Rostedt52baf112009-02-14 01:15:39 -05004202 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004203
4204 if (unlikely(ftrace_disabled)) {
4205 mutex_unlock(&ftrace_lock);
4206 return -ENODEV;
4207 }
4208
Steven Rostedt265c8312009-02-13 12:43:56 -05004209 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004210
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004211 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004212 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004213 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004214 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004215 if (array[i] == rec->ip) {
4216 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004217 break;
4218 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004219 }
4220
4221 if (!not) {
4222 fail = 0;
4223 if (!exists) {
4224 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004225 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004226 goto out;
4227 }
4228 } else {
4229 if (exists) {
4230 array[i] = array[--(*idx)];
4231 array[*idx] = 0;
4232 fail = 0;
4233 }
4234 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004235 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004236 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004237out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004238 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004239
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004240 if (fail)
4241 return -EINVAL;
4242
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004243 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004244}
4245
4246static ssize_t
4247ftrace_graph_write(struct file *file, const char __user *ubuf,
4248 size_t cnt, loff_t *ppos)
4249{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004250 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004251 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004252 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004253
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004254 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004255 return 0;
4256
Namhyung Kim6a101082013-10-14 17:24:25 +09004257 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4258 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004259
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004260 read = trace_get_user(&parser, ubuf, cnt, ppos);
4261
Li Zefan4ba79782009-09-22 13:52:20 +08004262 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004263 parser.buffer[parser.idx] = 0;
4264
Namhyung Kim6a101082013-10-14 17:24:25 +09004265 mutex_lock(&graph_lock);
4266
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004267 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004268 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4269 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004270
4271 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004272 }
4273
Namhyung Kim6a101082013-10-14 17:24:25 +09004274 if (!ret)
4275 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004276
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004277 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004278
4279 return ret;
4280}
4281
4282static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004283 .open = ftrace_graph_open,
4284 .read = seq_read,
4285 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004286 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004287 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004288};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004289
4290static const struct file_operations ftrace_graph_notrace_fops = {
4291 .open = ftrace_graph_notrace_open,
4292 .read = seq_read,
4293 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004294 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004295 .release = ftrace_graph_release,
4296};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004297#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4298
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004299void ftrace_create_filter_files(struct ftrace_ops *ops,
4300 struct dentry *parent)
4301{
4302
4303 trace_create_file("set_ftrace_filter", 0644, parent,
4304 ops, &ftrace_filter_fops);
4305
4306 trace_create_file("set_ftrace_notrace", 0644, parent,
4307 ops, &ftrace_notrace_fops);
4308}
4309
4310/*
4311 * The name "destroy_filter_files" is really a misnomer. Although
4312 * in the future, it may actualy delete the files, but this is
4313 * really intended to make sure the ops passed in are disabled
4314 * and that when this function returns, the caller is free to
4315 * free the ops.
4316 *
4317 * The "destroy" name is only to match the "create" name that this
4318 * should be paired with.
4319 */
4320void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4321{
4322 mutex_lock(&ftrace_lock);
4323 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4324 ftrace_shutdown(ops, 0);
4325 ops->flags |= FTRACE_OPS_FL_DELETED;
4326 mutex_unlock(&ftrace_lock);
4327}
4328
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004329static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004330{
Steven Rostedt5072c592008-05-12 21:20:43 +02004331
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004332 trace_create_file("available_filter_functions", 0444,
4333 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004334
Steven Rostedt647bcd02011-05-03 14:39:21 -04004335 trace_create_file("enabled_functions", 0444,
4336 d_tracer, NULL, &ftrace_enabled_fops);
4337
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004338 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004339
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004340#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004341 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004342 NULL,
4343 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004344 trace_create_file("set_graph_notrace", 0444, d_tracer,
4345 NULL,
4346 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004347#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4348
Steven Rostedt5072c592008-05-12 21:20:43 +02004349 return 0;
4350}
4351
Steven Rostedt9fd49322012-04-24 22:32:06 -04004352static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004353{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004354 const unsigned long *ipa = a;
4355 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004356
Steven Rostedt9fd49322012-04-24 22:32:06 -04004357 if (*ipa > *ipb)
4358 return 1;
4359 if (*ipa < *ipb)
4360 return -1;
4361 return 0;
4362}
4363
4364static void ftrace_swap_ips(void *a, void *b, int size)
4365{
4366 unsigned long *ipa = a;
4367 unsigned long *ipb = b;
4368 unsigned long t;
4369
4370 t = *ipa;
4371 *ipa = *ipb;
4372 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004373}
4374
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004375static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004376 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004377 unsigned long *end)
4378{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004379 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004380 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004381 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004382 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004383 unsigned long *p;
4384 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004385 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004386 int ret = -ENOMEM;
4387
4388 count = end - start;
4389
4390 if (!count)
4391 return 0;
4392
Steven Rostedt9fd49322012-04-24 22:32:06 -04004393 sort(start, count, sizeof(*start),
4394 ftrace_cmp_ips, ftrace_swap_ips);
4395
Steven Rostedt706c81f2012-04-24 23:45:26 -04004396 start_pg = ftrace_allocate_pages(count);
4397 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004398 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004399
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004400 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004401
Steven Rostedt32082302011-12-16 14:42:37 -05004402 /*
4403 * Core and each module needs their own pages, as
4404 * modules will free them when they are removed.
4405 * Force a new page to be allocated for modules.
4406 */
Steven Rostedta7900872011-12-16 16:23:44 -05004407 if (!mod) {
4408 WARN_ON(ftrace_pages || ftrace_pages_start);
4409 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004410 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004411 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004412 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004413 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004414
Steven Rostedta7900872011-12-16 16:23:44 -05004415 if (WARN_ON(ftrace_pages->next)) {
4416 /* Hmm, we have free pages? */
4417 while (ftrace_pages->next)
4418 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004419 }
Steven Rostedta7900872011-12-16 16:23:44 -05004420
Steven Rostedt706c81f2012-04-24 23:45:26 -04004421 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004422 }
4423
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004424 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004425 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004426 while (p < end) {
4427 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004428 /*
4429 * Some architecture linkers will pad between
4430 * the different mcount_loc sections of different
4431 * object files to satisfy alignments.
4432 * Skip any NULL pointers.
4433 */
4434 if (!addr)
4435 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004436
4437 if (pg->index == pg->size) {
4438 /* We should have allocated enough */
4439 if (WARN_ON(!pg->next))
4440 break;
4441 pg = pg->next;
4442 }
4443
4444 rec = &pg->records[pg->index++];
4445 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004446 }
4447
Steven Rostedt706c81f2012-04-24 23:45:26 -04004448 /* We should have used all pages */
4449 WARN_ON(pg->next);
4450
4451 /* Assign the last page to ftrace_pages */
4452 ftrace_pages = pg;
4453
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004454 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004455 * We only need to disable interrupts on start up
4456 * because we are modifying code that an interrupt
4457 * may execute, and the modification is not atomic.
4458 * But for modules, nothing runs the code we modify
4459 * until we are finished with it, and there's no
4460 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004461 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004462 if (!mod)
4463 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004464 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004465 if (!mod)
4466 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004467 ret = 0;
4468 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004469 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004470
Steven Rostedta7900872011-12-16 16:23:44 -05004471 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004472}
4473
Steven Rostedt93eb6772009-04-15 13:24:06 -04004474#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004475
4476#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4477
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004478void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004479{
4480 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004481 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004482 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004483 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004484
Steven Rostedt93eb6772009-04-15 13:24:06 -04004485 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004486
4487 if (ftrace_disabled)
4488 goto out_unlock;
4489
Steven Rostedt32082302011-12-16 14:42:37 -05004490 /*
4491 * Each module has its own ftrace_pages, remove
4492 * them from the list.
4493 */
4494 last_pg = &ftrace_pages_start;
4495 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4496 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004497 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004498 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004499 * As core pages are first, the first
4500 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004501 */
Steven Rostedt32082302011-12-16 14:42:37 -05004502 if (WARN_ON(pg == ftrace_pages_start))
4503 goto out_unlock;
4504
4505 /* Check if we are deleting the last page */
4506 if (pg == ftrace_pages)
4507 ftrace_pages = next_to_ftrace_page(last_pg);
4508
4509 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004510 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4511 free_pages((unsigned long)pg->records, order);
4512 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004513 } else
4514 last_pg = &pg->next;
4515 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004516 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004517 mutex_unlock(&ftrace_lock);
4518}
4519
4520static void ftrace_init_module(struct module *mod,
4521 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004522{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004523 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004524 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004525 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004526}
4527
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004528void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004529{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004530 ftrace_init_module(mod, mod->ftrace_callsites,
4531 mod->ftrace_callsites +
4532 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004533}
4534
4535static int ftrace_module_notify_exit(struct notifier_block *self,
4536 unsigned long val, void *data)
4537{
4538 struct module *mod = data;
4539
4540 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004541 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004542
4543 return 0;
4544}
4545#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004546static int ftrace_module_notify_exit(struct notifier_block *self,
4547 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004548{
4549 return 0;
4550}
4551#endif /* CONFIG_MODULES */
4552
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004553struct notifier_block ftrace_module_exit_nb = {
4554 .notifier_call = ftrace_module_notify_exit,
4555 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4556};
4557
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004558void __init ftrace_init(void)
4559{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004560 extern unsigned long __start_mcount_loc[];
4561 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004562 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004563 int ret;
4564
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004565 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004566 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004567 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004568 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004569 goto failed;
4570
4571 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004572 if (!count) {
4573 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004574 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004575 }
4576
4577 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4578 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004579
4580 last_ftrace_enabled = ftrace_enabled = 1;
4581
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004582 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004583 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004584 __stop_mcount_loc);
4585
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004586 ret = register_module_notifier(&ftrace_module_exit_nb);
4587 if (ret)
4588 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004589
Steven Rostedt2af15d62009-05-28 13:37:24 -04004590 set_ftrace_early_filters();
4591
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004592 return;
4593 failed:
4594 ftrace_disabled = 1;
4595}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004596
Steven Rostedt3d083392008-05-12 21:20:42 +02004597#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004598
Steven Rostedt2b499382011-05-03 22:49:52 -04004599static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004600 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004601 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4602 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004603};
4604
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004605static int __init ftrace_nodyn_init(void)
4606{
4607 ftrace_enabled = 1;
4608 return 0;
4609}
Steven Rostedt6f415672012-10-05 12:13:07 -04004610core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004611
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004612static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4613static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004614/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004615# define ftrace_startup(ops, command) \
4616 ({ \
4617 int ___ret = __register_ftrace_function(ops); \
4618 if (!___ret) \
4619 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4620 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004621 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004622# define ftrace_shutdown(ops, command) \
4623 ({ \
4624 int ___ret = __unregister_ftrace_function(ops); \
4625 if (!___ret) \
4626 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4627 ___ret; \
4628 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004629
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004630# define ftrace_startup_sysctl() do { } while (0)
4631# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004632
4633static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004634ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004635{
4636 return 1;
4637}
4638
Steven Rostedt3d083392008-05-12 21:20:42 +02004639#endif /* CONFIG_DYNAMIC_FTRACE */
4640
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004641__init void ftrace_init_global_array_ops(struct trace_array *tr)
4642{
4643 tr->ops = &global_ops;
4644 tr->ops->private = tr;
4645}
4646
4647void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4648{
4649 /* If we filter on pids, update to use the pid function */
4650 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4651 if (WARN_ON(tr->ops->func != ftrace_stub))
4652 printk("ftrace ops had %pS for function\n",
4653 tr->ops->func);
4654 /* Only the top level instance does pid tracing */
4655 if (!list_empty(&ftrace_pids)) {
4656 set_ftrace_pid_function(func);
4657 func = ftrace_pid_func;
4658 }
4659 }
4660 tr->ops->func = func;
4661 tr->ops->private = tr;
4662}
4663
4664void ftrace_reset_array_ops(struct trace_array *tr)
4665{
4666 tr->ops->func = ftrace_stub;
4667}
4668
Steven Rostedtb8489142011-05-04 09:27:52 -04004669static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004670ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004671 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004672{
Jiri Olsae2484912012-02-15 15:51:48 +01004673 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4674 return;
4675
4676 /*
4677 * Some of the ops may be dynamically allocated,
4678 * they must be freed after a synchronize_sched().
4679 */
4680 preempt_disable_notrace();
4681 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004682
4683 /*
4684 * Control funcs (perf) uses RCU. Only trace if
4685 * RCU is currently active.
4686 */
4687 if (!rcu_is_watching())
4688 goto out;
4689
Steven Rostedt0a016402012-11-02 17:03:03 -04004690 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004691 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4692 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004693 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004694 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004695 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004696 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004697 trace_recursion_clear(TRACE_CONTROL_BIT);
4698 preempt_enable_notrace();
4699}
4700
4701static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004702 .func = ftrace_ops_control_func,
4703 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4704 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004705};
4706
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004707static inline void
4708__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004709 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004710{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004711 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004712 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004713
Steven Rostedtccf36722012-06-05 09:44:25 -04004714 if (function_trace_stop)
4715 return;
4716
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004717 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4718 if (bit < 0)
4719 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004720
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004721 /*
4722 * Some of the ops may be dynamically allocated,
4723 * they must be freed after a synchronize_sched().
4724 */
4725 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004726 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004727 if (ftrace_ops_test(op, ip, regs)) {
4728 if (WARN_ON(!op->func)) {
4729 function_trace_stop = 1;
4730 printk("op=%p %pS\n", op, op);
4731 goto out;
4732 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004733 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004734 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004735 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004736out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004737 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004738 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004739}
4740
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004741/*
4742 * Some archs only support passing ip and parent_ip. Even though
4743 * the list function ignores the op parameter, we do not want any
4744 * C side effects, where a function is called without the caller
4745 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004746 * Archs are to support both the regs and ftrace_ops at the same time.
4747 * If they support ftrace_ops, it is assumed they support regs.
4748 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004749 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4750 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004751 * An architecture can pass partial regs with ftrace_ops and still
4752 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004753 */
4754#if ARCH_SUPPORTS_FTRACE_OPS
4755static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004756 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004757{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004758 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004759}
4760#else
4761static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4762{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004763 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004764}
4765#endif
4766
Steven Rostedte32d8952008-12-04 00:26:41 -05004767static void clear_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 clear_tsk_trace_trace(p);
4776 }
4777 put_online_cpus();
4778}
4779
4780static void set_ftrace_swapper(void)
4781{
4782 struct task_struct *p;
4783 int cpu;
4784
4785 get_online_cpus();
4786 for_each_online_cpu(cpu) {
4787 p = idle_task(cpu);
4788 set_tsk_trace_trace(p);
4789 }
4790 put_online_cpus();
4791}
4792
4793static void clear_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 Rostedte32d8952008-12-04 00:26:41 -05004798 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004799 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004800 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004801 rcu_read_unlock();
4802
Steven Rostedte32d8952008-12-04 00:26:41 -05004803 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004804}
4805
Steven Rostedte32d8952008-12-04 00:26:41 -05004806static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004807{
4808 struct task_struct *p;
4809
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004810 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004811 do_each_pid_task(pid, PIDTYPE_PID, p) {
4812 set_tsk_trace_trace(p);
4813 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004814 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004815}
4816
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004817static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004818{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004819 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004820 clear_ftrace_swapper();
4821 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004822 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004823}
4824
4825static void set_ftrace_pid_task(struct pid *pid)
4826{
4827 if (pid == ftrace_swapper_pid)
4828 set_ftrace_swapper();
4829 else
4830 set_ftrace_pid(pid);
4831}
4832
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004833static int ftrace_pid_add(int p)
4834{
4835 struct pid *pid;
4836 struct ftrace_pid *fpid;
4837 int ret = -EINVAL;
4838
4839 mutex_lock(&ftrace_lock);
4840
4841 if (!p)
4842 pid = ftrace_swapper_pid;
4843 else
4844 pid = find_get_pid(p);
4845
4846 if (!pid)
4847 goto out;
4848
4849 ret = 0;
4850
4851 list_for_each_entry(fpid, &ftrace_pids, list)
4852 if (fpid->pid == pid)
4853 goto out_put;
4854
4855 ret = -ENOMEM;
4856
4857 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4858 if (!fpid)
4859 goto out_put;
4860
4861 list_add(&fpid->list, &ftrace_pids);
4862 fpid->pid = pid;
4863
4864 set_ftrace_pid_task(pid);
4865
4866 ftrace_update_pid_func();
4867 ftrace_startup_enable(0);
4868
4869 mutex_unlock(&ftrace_lock);
4870 return 0;
4871
4872out_put:
4873 if (pid != ftrace_swapper_pid)
4874 put_pid(pid);
4875
4876out:
4877 mutex_unlock(&ftrace_lock);
4878 return ret;
4879}
4880
4881static void ftrace_pid_reset(void)
4882{
4883 struct ftrace_pid *fpid, *safe;
4884
4885 mutex_lock(&ftrace_lock);
4886 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4887 struct pid *pid = fpid->pid;
4888
4889 clear_ftrace_pid_task(pid);
4890
4891 list_del(&fpid->list);
4892 kfree(fpid);
4893 }
4894
4895 ftrace_update_pid_func();
4896 ftrace_startup_enable(0);
4897
4898 mutex_unlock(&ftrace_lock);
4899}
4900
4901static void *fpid_start(struct seq_file *m, loff_t *pos)
4902{
4903 mutex_lock(&ftrace_lock);
4904
4905 if (list_empty(&ftrace_pids) && (!*pos))
4906 return (void *) 1;
4907
4908 return seq_list_start(&ftrace_pids, *pos);
4909}
4910
4911static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4912{
4913 if (v == (void *)1)
4914 return NULL;
4915
4916 return seq_list_next(v, &ftrace_pids, pos);
4917}
4918
4919static void fpid_stop(struct seq_file *m, void *p)
4920{
4921 mutex_unlock(&ftrace_lock);
4922}
4923
4924static int fpid_show(struct seq_file *m, void *v)
4925{
4926 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4927
4928 if (v == (void *)1) {
4929 seq_printf(m, "no pid\n");
4930 return 0;
4931 }
4932
4933 if (fpid->pid == ftrace_swapper_pid)
4934 seq_printf(m, "swapper tasks\n");
4935 else
4936 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4937
4938 return 0;
4939}
4940
4941static const struct seq_operations ftrace_pid_sops = {
4942 .start = fpid_start,
4943 .next = fpid_next,
4944 .stop = fpid_stop,
4945 .show = fpid_show,
4946};
4947
4948static int
4949ftrace_pid_open(struct inode *inode, struct file *file)
4950{
4951 int ret = 0;
4952
4953 if ((file->f_mode & FMODE_WRITE) &&
4954 (file->f_flags & O_TRUNC))
4955 ftrace_pid_reset();
4956
4957 if (file->f_mode & FMODE_READ)
4958 ret = seq_open(file, &ftrace_pid_sops);
4959
4960 return ret;
4961}
4962
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004963static ssize_t
4964ftrace_pid_write(struct file *filp, const char __user *ubuf,
4965 size_t cnt, loff_t *ppos)
4966{
Ingo Molnar457dc922009-11-23 11:03:28 +01004967 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004968 long val;
4969 int ret;
4970
4971 if (cnt >= sizeof(buf))
4972 return -EINVAL;
4973
4974 if (copy_from_user(&buf, ubuf, cnt))
4975 return -EFAULT;
4976
4977 buf[cnt] = 0;
4978
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004979 /*
4980 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4981 * to clean the filter quietly.
4982 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004983 tmp = strstrip(buf);
4984 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004985 return 1;
4986
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004987 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004988 if (ret < 0)
4989 return ret;
4990
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004991 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004992
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004993 return ret ? ret : cnt;
4994}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004995
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004996static int
4997ftrace_pid_release(struct inode *inode, struct file *file)
4998{
4999 if (file->f_mode & FMODE_READ)
5000 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005001
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005002 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005003}
5004
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005005static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005006 .open = ftrace_pid_open,
5007 .write = ftrace_pid_write,
5008 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005009 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005010 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005011};
5012
5013static __init int ftrace_init_debugfs(void)
5014{
5015 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005016
5017 d_tracer = tracing_init_dentry();
5018 if (!d_tracer)
5019 return 0;
5020
5021 ftrace_init_dyn_debugfs(d_tracer);
5022
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005023 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5024 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005025
5026 ftrace_profile_debugfs(d_tracer);
5027
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005028 return 0;
5029}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005030fs_initcall(ftrace_init_debugfs);
5031
Steven Rostedt3d083392008-05-12 21:20:42 +02005032/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005033 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005034 *
5035 * This function should be used by panic code. It stops ftrace
5036 * but in a not so nice way. If you need to simply kill ftrace
5037 * from a non-atomic section, use ftrace_kill.
5038 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005039void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005040{
5041 ftrace_disabled = 1;
5042 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005043 clear_ftrace_function();
5044}
5045
5046/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005047 * Test if ftrace is dead or not.
5048 */
5049int ftrace_is_dead(void)
5050{
5051 return ftrace_disabled;
5052}
5053
5054/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005055 * register_ftrace_function - register a function for profiling
5056 * @ops - ops structure that holds the function for profiling.
5057 *
5058 * Register a function to be called by all functions in the
5059 * kernel.
5060 *
5061 * Note: @ops->func and all the functions it calls must be labeled
5062 * with "notrace", otherwise it will go into a
5063 * recursive loop.
5064 */
5065int register_ftrace_function(struct ftrace_ops *ops)
5066{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005067 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005068
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005069 ftrace_ops_init(ops);
5070
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005071 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005072
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005073 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005074
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005075 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005076
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005077 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005078}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005079EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005080
5081/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005082 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005083 * @ops - ops structure that holds the function to unregister
5084 *
5085 * Unregister a function that was added to be called by ftrace profiling.
5086 */
5087int unregister_ftrace_function(struct ftrace_ops *ops)
5088{
5089 int ret;
5090
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005091 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005092 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005093 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005094
5095 return ret;
5096}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005097EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005098
Ingo Molnare309b412008-05-12 21:20:51 +02005099int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005100ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005101 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005102 loff_t *ppos)
5103{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005104 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005105
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005106 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005107
Steven Rostedt45a4a232011-04-21 23:16:46 -04005108 if (unlikely(ftrace_disabled))
5109 goto out;
5110
5111 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005112
Li Zefana32c7762009-06-26 16:55:51 +08005113 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005114 goto out;
5115
Li Zefana32c7762009-06-26 16:55:51 +08005116 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005117
5118 if (ftrace_enabled) {
5119
5120 ftrace_startup_sysctl();
5121
5122 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005123 if (ftrace_ops_list != &ftrace_list_end)
5124 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005125
5126 } else {
5127 /* stopping ftrace calls (just send to ftrace_stub) */
5128 ftrace_trace_function = ftrace_stub;
5129
5130 ftrace_shutdown_sysctl();
5131 }
5132
5133 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005134 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005135 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005136}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005137
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005138#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005139
Steven Rostedt597af812009-04-03 15:24:12 -04005140static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005141
Steven Rostedte49dc192008-12-02 23:50:05 -05005142int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5143{
5144 return 0;
5145}
5146
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005147/* The callbacks that hook a function */
5148trace_func_graph_ret_t ftrace_graph_return =
5149 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005150trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005151static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005152
5153/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5154static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5155{
5156 int i;
5157 int ret = 0;
5158 unsigned long flags;
5159 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5160 struct task_struct *g, *t;
5161
5162 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5163 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5164 * sizeof(struct ftrace_ret_stack),
5165 GFP_KERNEL);
5166 if (!ret_stack_list[i]) {
5167 start = 0;
5168 end = i;
5169 ret = -ENOMEM;
5170 goto free;
5171 }
5172 }
5173
5174 read_lock_irqsave(&tasklist_lock, flags);
5175 do_each_thread(g, t) {
5176 if (start == end) {
5177 ret = -EAGAIN;
5178 goto unlock;
5179 }
5180
5181 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005182 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005183 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005184 t->curr_ret_stack = -1;
5185 /* Make sure the tasks see the -1 first: */
5186 smp_wmb();
5187 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005188 }
5189 } while_each_thread(g, t);
5190
5191unlock:
5192 read_unlock_irqrestore(&tasklist_lock, flags);
5193free:
5194 for (i = start; i < end; i++)
5195 kfree(ret_stack_list[i]);
5196 return ret;
5197}
5198
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005199static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005200ftrace_graph_probe_sched_switch(void *ignore,
5201 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005202{
5203 unsigned long long timestamp;
5204 int index;
5205
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005206 /*
5207 * Does the user want to count the time a function was asleep.
5208 * If so, do not update the time stamps.
5209 */
5210 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5211 return;
5212
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005213 timestamp = trace_clock_local();
5214
5215 prev->ftrace_timestamp = timestamp;
5216
5217 /* only process tasks that we timestamped */
5218 if (!next->ftrace_timestamp)
5219 return;
5220
5221 /*
5222 * Update all the counters in next to make up for the
5223 * time next was sleeping.
5224 */
5225 timestamp -= next->ftrace_timestamp;
5226
5227 for (index = next->curr_ret_stack; index >= 0; index--)
5228 next->ret_stack[index].calltime += timestamp;
5229}
5230
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005231/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005232static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005233{
5234 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005235 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005236
5237 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5238 sizeof(struct ftrace_ret_stack *),
5239 GFP_KERNEL);
5240
5241 if (!ret_stack_list)
5242 return -ENOMEM;
5243
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005244 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005245 for_each_online_cpu(cpu) {
5246 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005247 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005248 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005249
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005250 do {
5251 ret = alloc_retstack_tasklist(ret_stack_list);
5252 } while (ret == -EAGAIN);
5253
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005254 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005255 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005256 if (ret)
5257 pr_info("ftrace_graph: Couldn't activate tracepoint"
5258 " probe to kernel_sched_switch\n");
5259 }
5260
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005261 kfree(ret_stack_list);
5262 return ret;
5263}
5264
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005265/*
5266 * Hibernation protection.
5267 * The state of the current task is too much unstable during
5268 * suspend/restore to disk. We want to protect against that.
5269 */
5270static int
5271ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5272 void *unused)
5273{
5274 switch (state) {
5275 case PM_HIBERNATION_PREPARE:
5276 pause_graph_tracing();
5277 break;
5278
5279 case PM_POST_HIBERNATION:
5280 unpause_graph_tracing();
5281 break;
5282 }
5283 return NOTIFY_DONE;
5284}
5285
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005286static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5287{
5288 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5289 return 0;
5290 return __ftrace_graph_entry(trace);
5291}
5292
5293/*
5294 * The function graph tracer should only trace the functions defined
5295 * by set_ftrace_filter and set_ftrace_notrace. If another function
5296 * tracer ops is registered, the graph tracer requires testing the
5297 * function against the global ops, and not just trace any function
5298 * that any ftrace_ops registered.
5299 */
5300static void update_function_graph_func(void)
5301{
5302 if (ftrace_ops_list == &ftrace_list_end ||
5303 (ftrace_ops_list == &global_ops &&
5304 global_ops.next == &ftrace_list_end))
5305 ftrace_graph_entry = __ftrace_graph_entry;
5306 else
5307 ftrace_graph_entry = ftrace_graph_entry_test;
5308}
5309
Mathias Krause8275f692014-03-30 15:31:50 +02005310static struct notifier_block ftrace_suspend_notifier = {
5311 .notifier_call = ftrace_suspend_notifier_call,
5312};
5313
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005314int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5315 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005316{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005317 int ret = 0;
5318
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005319 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005320
Steven Rostedt05ce5812009-03-24 00:18:31 -04005321 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005322 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005323 ret = -EBUSY;
5324 goto out;
5325 }
5326
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005327 register_pm_notifier(&ftrace_suspend_notifier);
5328
Steven Rostedt597af812009-04-03 15:24:12 -04005329 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005330 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005331 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005332 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005333 goto out;
5334 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005335
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005336 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005337
5338 /*
5339 * Update the indirect function to the entryfunc, and the
5340 * function that gets called to the entry_test first. Then
5341 * call the update fgraph entry function to determine if
5342 * the entryfunc should be called directly or not.
5343 */
5344 __ftrace_graph_entry = entryfunc;
5345 ftrace_graph_entry = ftrace_graph_entry_test;
5346 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005347
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005348 /* Function graph doesn't use the .func field of global_ops */
5349 global_ops.flags |= FTRACE_OPS_FL_STUB;
5350
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005351#ifdef CONFIG_DYNAMIC_FTRACE
5352 /* Optimize function graph calling (if implemented by arch) */
5353 global_ops.trampoline = FTRACE_GRAPH_ADDR;
5354#endif
5355
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005356 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005357
5358out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005359 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005360 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005361}
5362
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005363void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005364{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005365 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005366
Steven Rostedt597af812009-04-03 15:24:12 -04005367 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005368 goto out;
5369
Steven Rostedt597af812009-04-03 15:24:12 -04005370 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005371 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005372 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005373 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005374 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5375 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04005376#ifdef CONFIG_DYNAMIC_FTRACE
5377 global_ops.trampoline = 0;
5378#endif
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005379 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005380 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005381
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005382 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005383 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005384}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005385
Steven Rostedt868baf02011-02-10 21:26:13 -05005386static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5387
5388static void
5389graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5390{
5391 atomic_set(&t->tracing_graph_pause, 0);
5392 atomic_set(&t->trace_overrun, 0);
5393 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005394 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005395 smp_wmb();
5396 t->ret_stack = ret_stack;
5397}
5398
5399/*
5400 * Allocate a return stack for the idle task. May be the first
5401 * time through, or it may be done by CPU hotplug online.
5402 */
5403void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5404{
5405 t->curr_ret_stack = -1;
5406 /*
5407 * The idle task has no parent, it either has its own
5408 * stack or no stack at all.
5409 */
5410 if (t->ret_stack)
5411 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5412
5413 if (ftrace_graph_active) {
5414 struct ftrace_ret_stack *ret_stack;
5415
5416 ret_stack = per_cpu(idle_ret_stack, cpu);
5417 if (!ret_stack) {
5418 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5419 * sizeof(struct ftrace_ret_stack),
5420 GFP_KERNEL);
5421 if (!ret_stack)
5422 return;
5423 per_cpu(idle_ret_stack, cpu) = ret_stack;
5424 }
5425 graph_init_task(t, ret_stack);
5426 }
5427}
5428
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005429/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005430void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005431{
Steven Rostedt84047e32009-06-02 16:51:55 -04005432 /* Make sure we do not use the parent ret_stack */
5433 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005434 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005435
Steven Rostedt597af812009-04-03 15:24:12 -04005436 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005437 struct ftrace_ret_stack *ret_stack;
5438
5439 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005440 * sizeof(struct ftrace_ret_stack),
5441 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005442 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005443 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005444 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005445 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005446}
5447
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005448void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005449{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005450 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5451
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005452 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005453 /* NULL must become visible to IRQs before we free it: */
5454 barrier();
5455
5456 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005457}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005458
5459void ftrace_graph_stop(void)
5460{
5461 ftrace_stop();
5462}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005463#endif