blob: cc07b7fc4372c7d21e7150424314562b4da5b019 [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 Rostedt99ecdc42008-08-15 21:40:05 -04001045#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001046# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001047#endif
1048
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001049static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1050
Steven Rostedtb6887d72009-02-17 12:32:04 -05001051struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001052 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001053 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001054 unsigned long flags;
1055 unsigned long ip;
1056 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001057 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001058};
1059
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001060struct ftrace_func_entry {
1061 struct hlist_node hlist;
1062 unsigned long ip;
1063};
1064
1065struct ftrace_hash {
1066 unsigned long size_bits;
1067 struct hlist_head *buckets;
1068 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001069 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001070};
1071
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001072/*
1073 * We make these constant because no one should touch them,
1074 * but they are used as the default "empty hash", to avoid allocating
1075 * it all the time. These are in a read only section such that if
1076 * anyone does try to modify it, it will cause an exception.
1077 */
1078static const struct hlist_head empty_buckets[1];
1079static const struct ftrace_hash empty_hash = {
1080 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001081};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001082#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001083
Steven Rostedt2b499382011-05-03 22:49:52 -04001084static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001085 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001086 .notrace_hash = EMPTY_HASH,
1087 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001088 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1089 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001090};
1091
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001092struct ftrace_page {
1093 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001094 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001095 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001096 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001097};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001098
Steven Rostedta7900872011-12-16 16:23:44 -05001099#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1100#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101
1102/* estimate from running different kernels */
1103#define NR_TO_INIT 10000
1104
1105static struct ftrace_page *ftrace_pages_start;
1106static struct ftrace_page *ftrace_pages;
1107
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001108static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001109{
1110 return !hash || !hash->count;
1111}
1112
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001113static struct ftrace_func_entry *
1114ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1115{
1116 unsigned long key;
1117 struct ftrace_func_entry *entry;
1118 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001119
Steven Rostedt06a51d92011-12-19 19:07:36 -05001120 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001121 return NULL;
1122
1123 if (hash->size_bits > 0)
1124 key = hash_long(ip, hash->size_bits);
1125 else
1126 key = 0;
1127
1128 hhd = &hash->buckets[key];
1129
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001130 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001131 if (entry->ip == ip)
1132 return entry;
1133 }
1134 return NULL;
1135}
1136
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001137static void __add_hash_entry(struct ftrace_hash *hash,
1138 struct ftrace_func_entry *entry)
1139{
1140 struct hlist_head *hhd;
1141 unsigned long key;
1142
1143 if (hash->size_bits)
1144 key = hash_long(entry->ip, hash->size_bits);
1145 else
1146 key = 0;
1147
1148 hhd = &hash->buckets[key];
1149 hlist_add_head(&entry->hlist, hhd);
1150 hash->count++;
1151}
1152
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001153static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1154{
1155 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001156
1157 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1158 if (!entry)
1159 return -ENOMEM;
1160
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001161 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001162 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001163
1164 return 0;
1165}
1166
1167static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001168free_hash_entry(struct ftrace_hash *hash,
1169 struct ftrace_func_entry *entry)
1170{
1171 hlist_del(&entry->hlist);
1172 kfree(entry);
1173 hash->count--;
1174}
1175
1176static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001177remove_hash_entry(struct ftrace_hash *hash,
1178 struct ftrace_func_entry *entry)
1179{
1180 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001181 hash->count--;
1182}
1183
1184static void ftrace_hash_clear(struct ftrace_hash *hash)
1185{
1186 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001187 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001188 struct ftrace_func_entry *entry;
1189 int size = 1 << hash->size_bits;
1190 int i;
1191
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001192 if (!hash->count)
1193 return;
1194
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001195 for (i = 0; i < size; i++) {
1196 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001197 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001198 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001199 }
1200 FTRACE_WARN_ON(hash->count);
1201}
1202
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001203static void free_ftrace_hash(struct ftrace_hash *hash)
1204{
1205 if (!hash || hash == EMPTY_HASH)
1206 return;
1207 ftrace_hash_clear(hash);
1208 kfree(hash->buckets);
1209 kfree(hash);
1210}
1211
Steven Rostedt07fd5512011-05-05 18:03:47 -04001212static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1213{
1214 struct ftrace_hash *hash;
1215
1216 hash = container_of(rcu, struct ftrace_hash, rcu);
1217 free_ftrace_hash(hash);
1218}
1219
1220static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1221{
1222 if (!hash || hash == EMPTY_HASH)
1223 return;
1224 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1225}
1226
Jiri Olsa5500fa52012-02-15 15:51:54 +01001227void ftrace_free_filter(struct ftrace_ops *ops)
1228{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001229 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001230 free_ftrace_hash(ops->filter_hash);
1231 free_ftrace_hash(ops->notrace_hash);
1232}
1233
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001234static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1235{
1236 struct ftrace_hash *hash;
1237 int size;
1238
1239 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1240 if (!hash)
1241 return NULL;
1242
1243 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001244 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001245
1246 if (!hash->buckets) {
1247 kfree(hash);
1248 return NULL;
1249 }
1250
1251 hash->size_bits = size_bits;
1252
1253 return hash;
1254}
1255
1256static struct ftrace_hash *
1257alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1258{
1259 struct ftrace_func_entry *entry;
1260 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001261 int size;
1262 int ret;
1263 int i;
1264
1265 new_hash = alloc_ftrace_hash(size_bits);
1266 if (!new_hash)
1267 return NULL;
1268
1269 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001270 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001271 return new_hash;
1272
1273 size = 1 << hash->size_bits;
1274 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001275 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276 ret = add_hash_entry(new_hash, entry->ip);
1277 if (ret < 0)
1278 goto free_hash;
1279 }
1280 }
1281
1282 FTRACE_WARN_ON(new_hash->count != hash->count);
1283
1284 return new_hash;
1285
1286 free_hash:
1287 free_ftrace_hash(new_hash);
1288 return NULL;
1289}
1290
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001291static void
1292ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1293static void
1294ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1295
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001296static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001297ftrace_hash_move(struct ftrace_ops *ops, int enable,
1298 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001299{
1300 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001301 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001303 struct ftrace_hash *old_hash;
1304 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001305 int size = src->count;
1306 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001307 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001308 int i;
1309
1310 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001311 * Remove the current set, update the hash and add
1312 * them back.
1313 */
1314 ftrace_hash_rec_disable(ops, enable);
1315
1316 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001317 * If the new source is empty, just free dst and assign it
1318 * the empty_hash.
1319 */
1320 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001321 free_ftrace_hash_rcu(*dst);
1322 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001323 /* still need to update the function records */
1324 ret = 0;
1325 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326 }
1327
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 /*
1329 * Make the hash size about 1/2 the # found
1330 */
1331 for (size /= 2; size; size >>= 1)
1332 bits++;
1333
1334 /* Don't allocate too much */
1335 if (bits > FTRACE_HASH_MAX_BITS)
1336 bits = FTRACE_HASH_MAX_BITS;
1337
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001338 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001339 new_hash = alloc_ftrace_hash(bits);
1340 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001341 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342
1343 size = 1 << src->size_bits;
1344 for (i = 0; i < size; i++) {
1345 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001346 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001347 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001348 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001349 }
1350 }
1351
Steven Rostedt07fd5512011-05-05 18:03:47 -04001352 old_hash = *dst;
1353 rcu_assign_pointer(*dst, new_hash);
1354 free_ftrace_hash_rcu(old_hash);
1355
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001356 ret = 0;
1357 out:
1358 /*
1359 * Enable regardless of ret:
1360 * On success, we enable the new hash.
1361 * On failure, we re-enable the original hash.
1362 */
1363 ftrace_hash_rec_enable(ops, enable);
1364
1365 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001366}
1367
Steven Rostedt265c8312009-02-13 12:43:56 -05001368/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001369 * Test the hashes for this ops to see if we want to call
1370 * the ops->func or not.
1371 *
1372 * It's a match if the ip is in the ops->filter_hash or
1373 * the filter_hash does not exist or is empty,
1374 * AND
1375 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001376 *
1377 * This needs to be called with preemption disabled as
1378 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001379 */
1380static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001381ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001382{
1383 struct ftrace_hash *filter_hash;
1384 struct ftrace_hash *notrace_hash;
1385 int ret;
1386
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001387#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1388 /*
1389 * There's a small race when adding ops that the ftrace handler
1390 * that wants regs, may be called without them. We can not
1391 * allow that handler to be called if regs is NULL.
1392 */
1393 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1394 return 0;
1395#endif
1396
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001397 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1398 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001399
Steven Rostedt06a51d92011-12-19 19:07:36 -05001400 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001401 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001402 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001403 !ftrace_lookup_ip(notrace_hash, ip)))
1404 ret = 1;
1405 else
1406 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001407
1408 return ret;
1409}
1410
1411/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001412 * This is a double for. Do not use 'break' to break out of the loop,
1413 * you must use a goto.
1414 */
1415#define do_for_each_ftrace_rec(pg, rec) \
1416 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1417 int _____i; \
1418 for (_____i = 0; _____i < pg->index; _____i++) { \
1419 rec = &pg->records[_____i];
1420
1421#define while_for_each_ftrace_rec() \
1422 } \
1423 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301424
Steven Rostedt5855fea2011-12-16 19:27:42 -05001425
1426static int ftrace_cmp_recs(const void *a, const void *b)
1427{
Steven Rostedta650e022012-04-25 13:48:13 -04001428 const struct dyn_ftrace *key = a;
1429 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001430
Steven Rostedta650e022012-04-25 13:48:13 -04001431 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001432 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001433 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1434 return 1;
1435 return 0;
1436}
1437
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001438static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001439{
1440 struct ftrace_page *pg;
1441 struct dyn_ftrace *rec;
1442 struct dyn_ftrace key;
1443
1444 key.ip = start;
1445 key.flags = end; /* overload flags, as it is unsigned long */
1446
1447 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1448 if (end < pg->records[0].ip ||
1449 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1450 continue;
1451 rec = bsearch(&key, pg->records, pg->index,
1452 sizeof(struct dyn_ftrace),
1453 ftrace_cmp_recs);
1454 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001455 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001456 }
1457
Steven Rostedt5855fea2011-12-16 19:27:42 -05001458 return 0;
1459}
1460
Steven Rostedtc88fd862011-08-16 09:53:39 -04001461/**
1462 * ftrace_location - return true if the ip giving is a traced location
1463 * @ip: the instruction pointer to check
1464 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001465 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001466 * That is, the instruction that is either a NOP or call to
1467 * the function tracer. It checks the ftrace internal tables to
1468 * determine if the address belongs or not.
1469 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001470unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001471{
Steven Rostedta650e022012-04-25 13:48:13 -04001472 return ftrace_location_range(ip, ip);
1473}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001474
Steven Rostedta650e022012-04-25 13:48:13 -04001475/**
1476 * ftrace_text_reserved - return true if range contains an ftrace location
1477 * @start: start of range to search
1478 * @end: end of range to search (inclusive). @end points to the last byte to check.
1479 *
1480 * Returns 1 if @start and @end contains a ftrace location.
1481 * That is, the instruction that is either a NOP or call to
1482 * the function tracer. It checks the ftrace internal tables to
1483 * determine if the address belongs or not.
1484 */
Sasha Levind88471c2013-01-09 18:09:20 -05001485int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001486{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001487 unsigned long ret;
1488
1489 ret = ftrace_location_range((unsigned long)start,
1490 (unsigned long)end);
1491
1492 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001493}
1494
Steven Rostedted926f92011-05-03 13:25:24 -04001495static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1496 int filter_hash,
1497 bool inc)
1498{
1499 struct ftrace_hash *hash;
1500 struct ftrace_hash *other_hash;
1501 struct ftrace_page *pg;
1502 struct dyn_ftrace *rec;
1503 int count = 0;
1504 int all = 0;
1505
1506 /* Only update if the ops has been registered */
1507 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1508 return;
1509
1510 /*
1511 * In the filter_hash case:
1512 * If the count is zero, we update all records.
1513 * Otherwise we just update the items in the hash.
1514 *
1515 * In the notrace_hash case:
1516 * We enable the update in the hash.
1517 * As disabling notrace means enabling the tracing,
1518 * and enabling notrace means disabling, the inc variable
1519 * gets inversed.
1520 */
1521 if (filter_hash) {
1522 hash = ops->filter_hash;
1523 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001524 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001525 all = 1;
1526 } else {
1527 inc = !inc;
1528 hash = ops->notrace_hash;
1529 other_hash = ops->filter_hash;
1530 /*
1531 * If the notrace hash has no items,
1532 * then there's nothing to do.
1533 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001534 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001535 return;
1536 }
1537
1538 do_for_each_ftrace_rec(pg, rec) {
1539 int in_other_hash = 0;
1540 int in_hash = 0;
1541 int match = 0;
1542
1543 if (all) {
1544 /*
1545 * Only the filter_hash affects all records.
1546 * Update if the record is not in the notrace hash.
1547 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001548 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001549 match = 1;
1550 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001551 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1552 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001553
1554 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001555 * If filter_hash is set, we want to match all functions
1556 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001557 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001558 * If filter_hash is not set, then we are decrementing.
1559 * That means we match anything that is in the hash
1560 * and also in the other_hash. That is, we need to turn
1561 * off functions in the other hash because they are disabled
1562 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001563 */
1564 if (filter_hash && in_hash && !in_other_hash)
1565 match = 1;
1566 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001567 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001568 match = 1;
1569 }
1570 if (!match)
1571 continue;
1572
1573 if (inc) {
1574 rec->flags++;
1575 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1576 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001577 /*
1578 * If any ops wants regs saved for this function
1579 * then all ops will get saved regs.
1580 */
1581 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1582 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001583 } else {
1584 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1585 return;
1586 rec->flags--;
1587 }
1588 count++;
1589 /* Shortcut, if we handled all records, we are done. */
1590 if (!all && count == hash->count)
1591 return;
1592 } while_for_each_ftrace_rec();
1593}
1594
1595static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1596 int filter_hash)
1597{
1598 __ftrace_hash_rec_update(ops, filter_hash, 0);
1599}
1600
1601static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1602 int filter_hash)
1603{
1604 __ftrace_hash_rec_update(ops, filter_hash, 1);
1605}
1606
Steven Rostedt05736a42008-09-22 14:55:47 -07001607static void print_ip_ins(const char *fmt, unsigned char *p)
1608{
1609 int i;
1610
1611 printk(KERN_CONT "%s", fmt);
1612
1613 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1614 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1615}
1616
Steven Rostedtc88fd862011-08-16 09:53:39 -04001617/**
1618 * ftrace_bug - report and shutdown function tracer
1619 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1620 * @ip: The address that failed
1621 *
1622 * The arch code that enables or disables the function tracing
1623 * can call ftrace_bug() when it has detected a problem in
1624 * modifying the code. @failed should be one of either:
1625 * EFAULT - if the problem happens on reading the @ip address
1626 * EINVAL - if what is read at @ip is not what was expected
1627 * EPERM - if the problem happens on writting to the @ip address
1628 */
1629void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001630{
1631 switch (failed) {
1632 case -EFAULT:
1633 FTRACE_WARN_ON_ONCE(1);
1634 pr_info("ftrace faulted on modifying ");
1635 print_ip_sym(ip);
1636 break;
1637 case -EINVAL:
1638 FTRACE_WARN_ON_ONCE(1);
1639 pr_info("ftrace failed to modify ");
1640 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001641 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001642 printk(KERN_CONT "\n");
1643 break;
1644 case -EPERM:
1645 FTRACE_WARN_ON_ONCE(1);
1646 pr_info("ftrace faulted on writing ");
1647 print_ip_sym(ip);
1648 break;
1649 default:
1650 FTRACE_WARN_ON_ONCE(1);
1651 pr_info("ftrace faulted on unknown error ");
1652 print_ip_sym(ip);
1653 }
1654}
1655
Steven Rostedtc88fd862011-08-16 09:53:39 -04001656static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001657{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001658 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001659
Steven Rostedt982c3502008-11-15 16:31:41 -05001660 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001661 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001662 *
Steven Rostedted926f92011-05-03 13:25:24 -04001663 * If the record has a ref count, then we need to enable it
1664 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001665 *
Steven Rostedted926f92011-05-03 13:25:24 -04001666 * Otherwise we make sure its disabled.
1667 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001668 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001669 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001670 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001671 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001672 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001673
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001674 /*
1675 * If enabling and the REGS flag does not match the REGS_EN, then
1676 * do not ignore this record. Set flags to fail the compare against
1677 * ENABLED.
1678 */
1679 if (flag &&
1680 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1681 flag |= FTRACE_FL_REGS;
1682
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001683 /* If the state of this record hasn't changed, then do nothing */
1684 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001685 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001686
1687 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001688 /* Save off if rec is being enabled (for return value) */
1689 flag ^= rec->flags & FTRACE_FL_ENABLED;
1690
1691 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001692 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001693 if (flag & FTRACE_FL_REGS) {
1694 if (rec->flags & FTRACE_FL_REGS)
1695 rec->flags |= FTRACE_FL_REGS_EN;
1696 else
1697 rec->flags &= ~FTRACE_FL_REGS_EN;
1698 }
1699 }
1700
1701 /*
1702 * If this record is being updated from a nop, then
1703 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001704 * Otherwise,
1705 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001706 * from the save regs, to a non-save regs function or
1707 * vice versa.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001708 */
1709 if (flag & FTRACE_FL_ENABLED)
1710 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001711
1712 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001713 }
1714
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001715 if (update) {
1716 /* If there's no more users, clear all flags */
1717 if (!(rec->flags & ~FTRACE_FL_MASK))
1718 rec->flags = 0;
1719 else
1720 /* Just disable the record (keep REGS state) */
1721 rec->flags &= ~FTRACE_FL_ENABLED;
1722 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001723
1724 return FTRACE_UPDATE_MAKE_NOP;
1725}
1726
1727/**
1728 * ftrace_update_record, set a record that now is tracing or not
1729 * @rec: the record to update
1730 * @enable: set to 1 if the record is tracing, zero to force disable
1731 *
1732 * The records that represent all functions that can be traced need
1733 * to be updated when tracing has been enabled.
1734 */
1735int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1736{
1737 return ftrace_check_record(rec, enable, 1);
1738}
1739
1740/**
1741 * ftrace_test_record, check if the record has been enabled or not
1742 * @rec: the record to test
1743 * @enable: set to 1 to check if enabled, 0 if it is disabled
1744 *
1745 * The arch code may need to test if a record is already set to
1746 * tracing to determine how to modify the function code that it
1747 * represents.
1748 */
1749int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1750{
1751 return ftrace_check_record(rec, enable, 0);
1752}
1753
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001754/**
1755 * ftrace_get_addr_new - Get the call address to set to
1756 * @rec: The ftrace record descriptor
1757 *
1758 * If the record has the FTRACE_FL_REGS set, that means that it
1759 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1760 * is not not set, then it wants to convert to the normal callback.
1761 *
1762 * Returns the address of the trampoline to set to
1763 */
1764unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1765{
1766 if (rec->flags & FTRACE_FL_REGS)
1767 return (unsigned long)FTRACE_REGS_ADDR;
1768 else
1769 return (unsigned long)FTRACE_ADDR;
1770}
1771
1772/**
1773 * ftrace_get_addr_curr - Get the call address that is already there
1774 * @rec: The ftrace record descriptor
1775 *
1776 * The FTRACE_FL_REGS_EN is set when the record already points to
1777 * a function that saves all the regs. Basically the '_EN' version
1778 * represents the current state of the function.
1779 *
1780 * Returns the address of the trampoline that is currently being called
1781 */
1782unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1783{
1784 if (rec->flags & FTRACE_FL_REGS_EN)
1785 return (unsigned long)FTRACE_REGS_ADDR;
1786 else
1787 return (unsigned long)FTRACE_ADDR;
1788}
1789
Steven Rostedtc88fd862011-08-16 09:53:39 -04001790static int
1791__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1792{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001793 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001794 unsigned long ftrace_addr;
1795 int ret;
1796
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001797 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001798
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001799 /* This needs to be done before we call ftrace_update_record */
1800 ftrace_old_addr = ftrace_get_addr_curr(rec);
1801
1802 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001803
Steven Rostedtc88fd862011-08-16 09:53:39 -04001804 switch (ret) {
1805 case FTRACE_UPDATE_IGNORE:
1806 return 0;
1807
1808 case FTRACE_UPDATE_MAKE_CALL:
1809 return ftrace_make_call(rec, ftrace_addr);
1810
1811 case FTRACE_UPDATE_MAKE_NOP:
1812 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001813
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001814 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001815 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001816 }
1817
1818 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001819}
1820
Steven Rostedte4f5d542012-04-27 09:13:18 -04001821void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001822{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001823 struct dyn_ftrace *rec;
1824 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001825 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001826
Steven Rostedt45a4a232011-04-21 23:16:46 -04001827 if (unlikely(ftrace_disabled))
1828 return;
1829
Steven Rostedt265c8312009-02-13 12:43:56 -05001830 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001831 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001832 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001833 ftrace_bug(failed, rec->ip);
1834 /* Stop processing */
1835 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001836 }
1837 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001838}
1839
Steven Rostedtc88fd862011-08-16 09:53:39 -04001840struct ftrace_rec_iter {
1841 struct ftrace_page *pg;
1842 int index;
1843};
1844
1845/**
1846 * ftrace_rec_iter_start, start up iterating over traced functions
1847 *
1848 * Returns an iterator handle that is used to iterate over all
1849 * the records that represent address locations where functions
1850 * are traced.
1851 *
1852 * May return NULL if no records are available.
1853 */
1854struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1855{
1856 /*
1857 * We only use a single iterator.
1858 * Protected by the ftrace_lock mutex.
1859 */
1860 static struct ftrace_rec_iter ftrace_rec_iter;
1861 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1862
1863 iter->pg = ftrace_pages_start;
1864 iter->index = 0;
1865
1866 /* Could have empty pages */
1867 while (iter->pg && !iter->pg->index)
1868 iter->pg = iter->pg->next;
1869
1870 if (!iter->pg)
1871 return NULL;
1872
1873 return iter;
1874}
1875
1876/**
1877 * ftrace_rec_iter_next, get the next record to process.
1878 * @iter: The handle to the iterator.
1879 *
1880 * Returns the next iterator after the given iterator @iter.
1881 */
1882struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1883{
1884 iter->index++;
1885
1886 if (iter->index >= iter->pg->index) {
1887 iter->pg = iter->pg->next;
1888 iter->index = 0;
1889
1890 /* Could have empty pages */
1891 while (iter->pg && !iter->pg->index)
1892 iter->pg = iter->pg->next;
1893 }
1894
1895 if (!iter->pg)
1896 return NULL;
1897
1898 return iter;
1899}
1900
1901/**
1902 * ftrace_rec_iter_record, get the record at the iterator location
1903 * @iter: The current iterator location
1904 *
1905 * Returns the record that the current @iter is at.
1906 */
1907struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1908{
1909 return &iter->pg->records[iter->index];
1910}
1911
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301912static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001913ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001914{
1915 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001916 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001917
1918 ip = rec->ip;
1919
Steven Rostedt45a4a232011-04-21 23:16:46 -04001920 if (unlikely(ftrace_disabled))
1921 return 0;
1922
Shaohua Li25aac9d2009-01-09 11:29:40 +08001923 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001924 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001925 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301926 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001927 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301928 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001929}
1930
Steven Rostedt000ab692009-02-17 13:35:06 -05001931/*
1932 * archs can override this function if they must do something
1933 * before the modifying code is performed.
1934 */
1935int __weak ftrace_arch_code_modify_prepare(void)
1936{
1937 return 0;
1938}
1939
1940/*
1941 * archs can override this function if they must do something
1942 * after the modifying code is performed.
1943 */
1944int __weak ftrace_arch_code_modify_post_process(void)
1945{
1946 return 0;
1947}
1948
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001949void ftrace_modify_all_code(int command)
1950{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001951 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01001952 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001953
1954 /*
1955 * If the ftrace_caller calls a ftrace_ops func directly,
1956 * we need to make sure that it only traces functions it
1957 * expects to trace. When doing the switch of functions,
1958 * we need to update to the ftrace_ops_list_func first
1959 * before the transition between old and new calls are set,
1960 * as the ftrace_ops_list_func will check the ops hashes
1961 * to make sure the ops are having the right functions
1962 * traced.
1963 */
Petr Mladekcd210672014-02-24 17:12:21 +01001964 if (update) {
1965 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
1966 if (FTRACE_WARN_ON(err))
1967 return;
1968 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001969
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001970 if (command & FTRACE_UPDATE_CALLS)
1971 ftrace_replace_code(1);
1972 else if (command & FTRACE_DISABLE_CALLS)
1973 ftrace_replace_code(0);
1974
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05001975 if (update && ftrace_trace_function != ftrace_ops_list_func) {
1976 function_trace_op = set_function_trace_op;
1977 smp_wmb();
1978 /* If irqs are disabled, we are in stop machine */
1979 if (!irqs_disabled())
1980 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01001981 err = ftrace_update_ftrace_func(ftrace_trace_function);
1982 if (FTRACE_WARN_ON(err))
1983 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05001984 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001985
1986 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01001987 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001988 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01001989 err = ftrace_disable_ftrace_graph_caller();
1990 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001991}
1992
Ingo Molnare309b412008-05-12 21:20:51 +02001993static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001994{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001995 int *command = data;
1996
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001997 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001998
Steven Rostedtc88fd862011-08-16 09:53:39 -04001999 return 0;
2000}
2001
2002/**
2003 * ftrace_run_stop_machine, go back to the stop machine method
2004 * @command: The command to tell ftrace what to do
2005 *
2006 * If an arch needs to fall back to the stop machine method, the
2007 * it can call this function.
2008 */
2009void ftrace_run_stop_machine(int command)
2010{
2011 stop_machine(__ftrace_modify_code, &command, NULL);
2012}
2013
2014/**
2015 * arch_ftrace_update_code, modify the code to trace or not trace
2016 * @command: The command that needs to be done
2017 *
2018 * Archs can override this function if it does not need to
2019 * run stop_machine() to modify code.
2020 */
2021void __weak arch_ftrace_update_code(int command)
2022{
2023 ftrace_run_stop_machine(command);
2024}
2025
2026static void ftrace_run_update_code(int command)
2027{
2028 int ret;
2029
2030 ret = ftrace_arch_code_modify_prepare();
2031 FTRACE_WARN_ON(ret);
2032 if (ret)
2033 return;
2034 /*
2035 * Do not call function tracer while we update the code.
2036 * We are in stop machine.
2037 */
2038 function_trace_stop++;
2039
2040 /*
2041 * By default we use stop_machine() to modify the code.
2042 * But archs can do what ever they want as long as it
2043 * is safe. The stop_machine() is the safest, but also
2044 * produces the most overhead.
2045 */
2046 arch_ftrace_update_code(command);
2047
Steven Rostedt6331c282011-07-13 15:11:02 -04002048 function_trace_stop--;
2049
Steven Rostedt000ab692009-02-17 13:35:06 -05002050 ret = ftrace_arch_code_modify_post_process();
2051 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002052}
2053
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002054static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002055static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002056static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002057
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002058static void control_ops_free(struct ftrace_ops *ops)
2059{
2060 free_percpu(ops->disabled);
2061}
2062
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002063static void ftrace_startup_enable(int command)
2064{
2065 if (saved_ftrace_func != ftrace_trace_function) {
2066 saved_ftrace_func = ftrace_trace_function;
2067 command |= FTRACE_UPDATE_TRACE_FUNC;
2068 }
2069
2070 if (!command || !ftrace_enabled)
2071 return;
2072
2073 ftrace_run_update_code(command);
2074}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002075
Steven Rostedta1cd6172011-05-23 15:24:25 -04002076static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002077{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002078 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002079
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002080 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002081 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002082
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002083 ret = __register_ftrace_function(ops);
2084 if (ret)
2085 return ret;
2086
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002087 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002088 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002089
Steven Rostedted926f92011-05-03 13:25:24 -04002090 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002091
2092 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002093
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002094 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002095
2096 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002097}
2098
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002099static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002100{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002101 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002102
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002103 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002104 return -ENODEV;
2105
2106 ret = __unregister_ftrace_function(ops);
2107 if (ret)
2108 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002109
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002110 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002111 /*
2112 * Just warn in case of unbalance, no need to kill ftrace, it's not
2113 * critical but the ftrace_call callers may be never nopped again after
2114 * further ftrace uses.
2115 */
2116 WARN_ON_ONCE(ftrace_start_up < 0);
2117
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002118 ftrace_hash_rec_disable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002119
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05002120 if (!global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002121 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002122
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002123 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002124
2125 if (saved_ftrace_func != ftrace_trace_function) {
2126 saved_ftrace_func = ftrace_trace_function;
2127 command |= FTRACE_UPDATE_TRACE_FUNC;
2128 }
2129
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002130 if (!command || !ftrace_enabled) {
2131 /*
2132 * If these are control ops, they still need their
2133 * per_cpu field freed. Since, function tracing is
2134 * not currently active, we can just free them
2135 * without synchronizing all CPUs.
2136 */
2137 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2138 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002139 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002140 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002141
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002142 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002143
2144 /*
2145 * Dynamic ops may be freed, we must make sure that all
2146 * callers are done before leaving this function.
2147 * The same goes for freeing the per_cpu data of the control
2148 * ops.
2149 *
2150 * Again, normal synchronize_sched() is not good enough.
2151 * We need to do a hard force of sched synchronization.
2152 * This is because we use preempt_disable() to do RCU, but
2153 * the function tracers can be called where RCU is not watching
2154 * (like before user_exit()). We can not rely on the RCU
2155 * infrastructure to do the synchronization, thus we must do it
2156 * ourselves.
2157 */
2158 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2159 schedule_on_each_cpu(ftrace_sync);
2160
2161 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2162 control_ops_free(ops);
2163 }
2164
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002165 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002166}
2167
Ingo Molnare309b412008-05-12 21:20:51 +02002168static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002169{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002170 if (unlikely(ftrace_disabled))
2171 return;
2172
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002173 /* Force update next time */
2174 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002175 /* ftrace_start_up is true if we want ftrace running */
2176 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002177 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002178}
2179
Ingo Molnare309b412008-05-12 21:20:51 +02002180static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002181{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002182 if (unlikely(ftrace_disabled))
2183 return;
2184
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002185 /* ftrace_start_up is true if ftrace is running */
2186 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002187 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002188}
2189
Steven Rostedt3d083392008-05-12 21:20:42 +02002190static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002191unsigned long ftrace_update_tot_cnt;
2192
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002193static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002194{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002195 /*
2196 * Filter_hash being empty will default to trace module.
2197 * But notrace hash requires a test of individual module functions.
2198 */
2199 return ftrace_hash_empty(ops->filter_hash) &&
2200 ftrace_hash_empty(ops->notrace_hash);
2201}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002202
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002203/*
2204 * Check if the current ops references the record.
2205 *
2206 * If the ops traces all functions, then it was already accounted for.
2207 * If the ops does not trace the current record function, skip it.
2208 * If the ops ignores the function via notrace filter, skip it.
2209 */
2210static inline bool
2211ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2212{
2213 /* If ops isn't enabled, ignore it */
2214 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2215 return 0;
2216
2217 /* If ops traces all mods, we already accounted for it */
2218 if (ops_traces_mod(ops))
2219 return 0;
2220
2221 /* The function must be in the filter */
2222 if (!ftrace_hash_empty(ops->filter_hash) &&
2223 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2224 return 0;
2225
2226 /* If in notrace hash, we ignore it too */
2227 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2228 return 0;
2229
2230 return 1;
2231}
2232
2233static int referenced_filters(struct dyn_ftrace *rec)
2234{
2235 struct ftrace_ops *ops;
2236 int cnt = 0;
2237
2238 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2239 if (ops_references_rec(ops, rec))
2240 cnt++;
2241 }
2242
2243 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002244}
2245
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002246static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002247{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002248 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002249 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302250 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002251 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002252 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002253 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002254 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002255
2256 /*
2257 * When adding a module, we need to check if tracers are
2258 * currently enabled and if they are set to trace all functions.
2259 * If they are, we need to enable the module functions as well
2260 * as update the reference counts for those function records.
2261 */
2262 if (mod) {
2263 struct ftrace_ops *ops;
2264
2265 for (ops = ftrace_ops_list;
2266 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002267 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2268 if (ops_traces_mod(ops))
2269 ref++;
2270 else
2271 test = true;
2272 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002273 }
2274 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002275
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002276 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002277
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002278 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302279
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002280 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002281 int cnt = ref;
2282
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002283 /* If something went wrong, bail without enabling anything */
2284 if (unlikely(ftrace_disabled))
2285 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002286
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002287 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002288 if (test)
2289 cnt += referenced_filters(p);
2290 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302291
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002292 /*
2293 * Do the initial record conversion from mcount jump
2294 * to the NOP instructions.
2295 */
2296 if (!ftrace_code_disable(mod, p))
2297 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002298
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002299 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002300
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002301 /*
2302 * If the tracing is enabled, go ahead and enable the record.
2303 *
2304 * The reason not to enable the record immediatelly is the
2305 * inherent check of ftrace_make_nop/ftrace_make_call for
2306 * correct previous instructions. Making first the NOP
2307 * conversion puts the module to the correct state, thus
2308 * passing the ftrace_make_call check.
2309 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002310 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002311 int failed = __ftrace_replace_code(p, 1);
2312 if (failed)
2313 ftrace_bug(failed, p->ip);
2314 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002315 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002316 }
2317
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002318 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002319 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002320 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002321
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002322 return 0;
2323}
2324
Steven Rostedta7900872011-12-16 16:23:44 -05002325static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002326{
Steven Rostedta7900872011-12-16 16:23:44 -05002327 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002328 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002329
Steven Rostedta7900872011-12-16 16:23:44 -05002330 if (WARN_ON(!count))
2331 return -EINVAL;
2332
2333 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002334
2335 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002336 * We want to fill as much as possible. No more than a page
2337 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002338 */
Steven Rostedta7900872011-12-16 16:23:44 -05002339 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2340 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002341
Steven Rostedta7900872011-12-16 16:23:44 -05002342 again:
2343 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2344
2345 if (!pg->records) {
2346 /* if we can't allocate this size, try something smaller */
2347 if (!order)
2348 return -ENOMEM;
2349 order >>= 1;
2350 goto again;
2351 }
2352
2353 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2354 pg->size = cnt;
2355
2356 if (cnt > count)
2357 cnt = count;
2358
2359 return cnt;
2360}
2361
2362static struct ftrace_page *
2363ftrace_allocate_pages(unsigned long num_to_init)
2364{
2365 struct ftrace_page *start_pg;
2366 struct ftrace_page *pg;
2367 int order;
2368 int cnt;
2369
2370 if (!num_to_init)
2371 return 0;
2372
2373 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2374 if (!pg)
2375 return NULL;
2376
2377 /*
2378 * Try to allocate as much as possible in one continues
2379 * location that fills in all of the space. We want to
2380 * waste as little space as possible.
2381 */
2382 for (;;) {
2383 cnt = ftrace_allocate_records(pg, num_to_init);
2384 if (cnt < 0)
2385 goto free_pages;
2386
2387 num_to_init -= cnt;
2388 if (!num_to_init)
2389 break;
2390
2391 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2392 if (!pg->next)
2393 goto free_pages;
2394
2395 pg = pg->next;
2396 }
2397
2398 return start_pg;
2399
2400 free_pages:
2401 while (start_pg) {
2402 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2403 free_pages((unsigned long)pg->records, order);
2404 start_pg = pg->next;
2405 kfree(pg);
2406 pg = start_pg;
2407 }
2408 pr_info("ftrace: FAILED to allocate memory for functions\n");
2409 return NULL;
2410}
2411
Steven Rostedt5072c592008-05-12 21:20:43 +02002412#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2413
2414struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002415 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002416 loff_t func_pos;
2417 struct ftrace_page *pg;
2418 struct dyn_ftrace *func;
2419 struct ftrace_func_probe *probe;
2420 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002421 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002422 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002423 int hidx;
2424 int idx;
2425 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002426};
2427
Ingo Molnare309b412008-05-12 21:20:51 +02002428static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002429t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002430{
2431 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002432 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002433 struct hlist_head *hhd;
2434
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002435 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002436 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002437
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002438 if (iter->probe)
2439 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002440 retry:
2441 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2442 return NULL;
2443
2444 hhd = &ftrace_func_hash[iter->hidx];
2445
2446 if (hlist_empty(hhd)) {
2447 iter->hidx++;
2448 hnd = NULL;
2449 goto retry;
2450 }
2451
2452 if (!hnd)
2453 hnd = hhd->first;
2454 else {
2455 hnd = hnd->next;
2456 if (!hnd) {
2457 iter->hidx++;
2458 goto retry;
2459 }
2460 }
2461
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002462 if (WARN_ON_ONCE(!hnd))
2463 return NULL;
2464
2465 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2466
2467 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002468}
2469
2470static void *t_hash_start(struct seq_file *m, loff_t *pos)
2471{
2472 struct ftrace_iterator *iter = m->private;
2473 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002474 loff_t l;
2475
Steven Rostedt69a30832011-12-19 15:21:16 -05002476 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2477 return NULL;
2478
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002479 if (iter->func_pos > *pos)
2480 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002481
Li Zefand82d6242009-06-24 09:54:54 +08002482 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002483 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002484 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002485 if (!p)
2486 break;
2487 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002488 if (!p)
2489 return NULL;
2490
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002491 /* Only set this if we have an item */
2492 iter->flags |= FTRACE_ITER_HASH;
2493
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002494 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002495}
2496
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002497static int
2498t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002499{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002500 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002501
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002502 rec = iter->probe;
2503 if (WARN_ON_ONCE(!rec))
2504 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002505
Steven Rostedt809dcf22009-02-16 23:06:01 -05002506 if (rec->ops->print)
2507 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2508
Steven Rostedtb375a112009-09-17 00:05:58 -04002509 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002510
2511 if (rec->data)
2512 seq_printf(m, ":%p", rec->data);
2513 seq_putc(m, '\n');
2514
2515 return 0;
2516}
2517
2518static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002519t_next(struct seq_file *m, void *v, loff_t *pos)
2520{
2521 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002522 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002523 struct dyn_ftrace *rec = NULL;
2524
Steven Rostedt45a4a232011-04-21 23:16:46 -04002525 if (unlikely(ftrace_disabled))
2526 return NULL;
2527
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002528 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002529 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002530
Steven Rostedt5072c592008-05-12 21:20:43 +02002531 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002532 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002533
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002534 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002535 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002536
Steven Rostedt5072c592008-05-12 21:20:43 +02002537 retry:
2538 if (iter->idx >= iter->pg->index) {
2539 if (iter->pg->next) {
2540 iter->pg = iter->pg->next;
2541 iter->idx = 0;
2542 goto retry;
2543 }
2544 } else {
2545 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002546 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002547 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002548
Steven Rostedt41c52c02008-05-22 11:46:33 -04002549 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002550 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2551
2552 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002553 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002554
Steven Rostedt5072c592008-05-12 21:20:43 +02002555 rec = NULL;
2556 goto retry;
2557 }
2558 }
2559
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002560 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002561 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002562
2563 iter->func = rec;
2564
2565 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002566}
2567
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002568static void reset_iter_read(struct ftrace_iterator *iter)
2569{
2570 iter->pos = 0;
2571 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002572 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002573}
2574
2575static void *t_start(struct seq_file *m, loff_t *pos)
2576{
2577 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002578 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002579 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002580 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002581
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002582 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002583
2584 if (unlikely(ftrace_disabled))
2585 return NULL;
2586
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002587 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002588 * If an lseek was done, then reset and start from beginning.
2589 */
2590 if (*pos < iter->pos)
2591 reset_iter_read(iter);
2592
2593 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002594 * For set_ftrace_filter reading, if we have the filter
2595 * off, we can short cut and just print out that all
2596 * functions are enabled.
2597 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002598 if (iter->flags & FTRACE_ITER_FILTER &&
2599 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002600 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002601 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002602 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002603 /* reset in case of seek/pread */
2604 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002605 return iter;
2606 }
2607
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002608 if (iter->flags & FTRACE_ITER_HASH)
2609 return t_hash_start(m, pos);
2610
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002611 /*
2612 * Unfortunately, we need to restart at ftrace_pages_start
2613 * every time we let go of the ftrace_mutex. This is because
2614 * those pointers can change without the lock.
2615 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002616 iter->pg = ftrace_pages_start;
2617 iter->idx = 0;
2618 for (l = 0; l <= *pos; ) {
2619 p = t_next(m, p, &l);
2620 if (!p)
2621 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002622 }
walimis5821e1b2008-11-15 15:19:06 +08002623
Steven Rostedt69a30832011-12-19 15:21:16 -05002624 if (!p)
2625 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002626
2627 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002628}
2629
2630static void t_stop(struct seq_file *m, void *p)
2631{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002632 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002633}
2634
2635static int t_show(struct seq_file *m, void *v)
2636{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002637 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002638 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002639
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002640 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002641 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002642
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002643 if (iter->flags & FTRACE_ITER_PRINTALL) {
2644 seq_printf(m, "#### all functions enabled ####\n");
2645 return 0;
2646 }
2647
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002648 rec = iter->func;
2649
Steven Rostedt5072c592008-05-12 21:20:43 +02002650 if (!rec)
2651 return 0;
2652
Steven Rostedt647bcd02011-05-03 14:39:21 -04002653 seq_printf(m, "%ps", (void *)rec->ip);
2654 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002655 seq_printf(m, " (%ld)%s",
2656 rec->flags & ~FTRACE_FL_MASK,
2657 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002658 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002659
2660 return 0;
2661}
2662
James Morris88e9d342009-09-22 16:43:43 -07002663static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002664 .start = t_start,
2665 .next = t_next,
2666 .stop = t_stop,
2667 .show = t_show,
2668};
2669
Ingo Molnare309b412008-05-12 21:20:51 +02002670static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002671ftrace_avail_open(struct inode *inode, struct file *file)
2672{
2673 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002674
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002675 if (unlikely(ftrace_disabled))
2676 return -ENODEV;
2677
Jiri Olsa50e18b92012-04-25 10:23:39 +02002678 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2679 if (iter) {
2680 iter->pg = ftrace_pages_start;
2681 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002682 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002683
Jiri Olsa50e18b92012-04-25 10:23:39 +02002684 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002685}
2686
Steven Rostedt647bcd02011-05-03 14:39:21 -04002687static int
2688ftrace_enabled_open(struct inode *inode, struct file *file)
2689{
2690 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002691
2692 if (unlikely(ftrace_disabled))
2693 return -ENODEV;
2694
Jiri Olsa50e18b92012-04-25 10:23:39 +02002695 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2696 if (iter) {
2697 iter->pg = ftrace_pages_start;
2698 iter->flags = FTRACE_ITER_ENABLED;
2699 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002700 }
2701
Jiri Olsa50e18b92012-04-25 10:23:39 +02002702 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002703}
2704
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002705static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002706{
Steven Rostedt52baf112009-02-14 01:15:39 -05002707 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002708 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002709 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002710}
2711
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002712/**
2713 * ftrace_regex_open - initialize function tracer filter files
2714 * @ops: The ftrace_ops that hold the hash filters
2715 * @flag: The type of filter to process
2716 * @inode: The inode, usually passed in to your open routine
2717 * @file: The file, usually passed in to your open routine
2718 *
2719 * ftrace_regex_open() initializes the filter files for the
2720 * @ops. Depending on @flag it may process the filter hash or
2721 * the notrace hash of @ops. With this called from the open
2722 * routine, you can use ftrace_filter_write() for the write
2723 * routine if @flag has FTRACE_ITER_FILTER set, or
2724 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002725 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002726 * release must call ftrace_regex_release().
2727 */
2728int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002729ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002730 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002731{
2732 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002733 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002734 int ret = 0;
2735
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002736 ftrace_ops_init(ops);
2737
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002738 if (unlikely(ftrace_disabled))
2739 return -ENODEV;
2740
Steven Rostedt5072c592008-05-12 21:20:43 +02002741 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2742 if (!iter)
2743 return -ENOMEM;
2744
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002745 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2746 kfree(iter);
2747 return -ENOMEM;
2748 }
2749
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002750 iter->ops = ops;
2751 iter->flags = flag;
2752
2753 mutex_lock(&ops->regex_lock);
2754
Steven Rostedtf45948e2011-05-02 12:29:25 -04002755 if (flag & FTRACE_ITER_NOTRACE)
2756 hash = ops->notrace_hash;
2757 else
2758 hash = ops->filter_hash;
2759
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002760 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002761 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002762 if (!iter->hash) {
2763 trace_parser_put(&iter->parser);
2764 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002765 ret = -ENOMEM;
2766 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002767 }
2768 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002769
Steven Rostedt5072c592008-05-12 21:20:43 +02002770 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002771 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002772 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002773
2774 if (file->f_mode & FMODE_READ) {
2775 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002776
2777 ret = seq_open(file, &show_ftrace_seq_ops);
2778 if (!ret) {
2779 struct seq_file *m = file->private_data;
2780 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002781 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002782 /* Failed */
2783 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002784 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002785 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002786 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002787 } else
2788 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002789
2790 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002791 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002792
2793 return ret;
2794}
2795
Steven Rostedt41c52c02008-05-22 11:46:33 -04002796static int
2797ftrace_filter_open(struct inode *inode, struct file *file)
2798{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002799 struct ftrace_ops *ops = inode->i_private;
2800
2801 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05002802 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2803 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002804}
2805
2806static int
2807ftrace_notrace_open(struct inode *inode, struct file *file)
2808{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002809 struct ftrace_ops *ops = inode->i_private;
2810
2811 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002812 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002813}
2814
Steven Rostedt64e7c442009-02-13 17:08:48 -05002815static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002816{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002817 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002818 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002819
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002820 switch (type) {
2821 case MATCH_FULL:
2822 if (strcmp(str, regex) == 0)
2823 matched = 1;
2824 break;
2825 case MATCH_FRONT_ONLY:
2826 if (strncmp(str, regex, len) == 0)
2827 matched = 1;
2828 break;
2829 case MATCH_MIDDLE_ONLY:
2830 if (strstr(str, regex))
2831 matched = 1;
2832 break;
2833 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002834 slen = strlen(str);
2835 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002836 matched = 1;
2837 break;
2838 }
2839
2840 return matched;
2841}
2842
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002843static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002844enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002845{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002846 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002847 int ret = 0;
2848
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002849 entry = ftrace_lookup_ip(hash, rec->ip);
2850 if (not) {
2851 /* Do nothing if it doesn't exist */
2852 if (!entry)
2853 return 0;
2854
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002855 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002856 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002857 /* Do nothing if it exists */
2858 if (entry)
2859 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002860
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002861 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002862 }
2863 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002864}
2865
Steven Rostedt64e7c442009-02-13 17:08:48 -05002866static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002867ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2868 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002869{
2870 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002871 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002872
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002873 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2874
2875 if (mod) {
2876 /* module lookup requires matching the module */
2877 if (!modname || strcmp(modname, mod))
2878 return 0;
2879
2880 /* blank search means to match all funcs in the mod */
2881 if (!len)
2882 return 1;
2883 }
2884
Steven Rostedt64e7c442009-02-13 17:08:48 -05002885 return ftrace_match(str, regex, len, type);
2886}
2887
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002888static int
2889match_records(struct ftrace_hash *hash, char *buff,
2890 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002891{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002892 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002893 struct ftrace_page *pg;
2894 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002895 int type = MATCH_FULL;
2896 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002897 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002898 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002899
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002900 if (len) {
2901 type = filter_parse_regex(buff, len, &search, &not);
2902 search_len = strlen(search);
2903 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002904
Steven Rostedt52baf112009-02-14 01:15:39 -05002905 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002906
2907 if (unlikely(ftrace_disabled))
2908 goto out_unlock;
2909
Steven Rostedt265c8312009-02-13 12:43:56 -05002910 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002911 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002912 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002913 if (ret < 0) {
2914 found = ret;
2915 goto out_unlock;
2916 }
Li Zefan311d16d2009-12-08 11:15:11 +08002917 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002918 }
2919 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002920 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002921 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002922
2923 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002924}
2925
Steven Rostedt64e7c442009-02-13 17:08:48 -05002926static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002927ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002928{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002929 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002930}
2931
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002932static int
2933ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002934{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002935 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002936
Steven Rostedt64e7c442009-02-13 17:08:48 -05002937 /* blank or '*' mean the same */
2938 if (strcmp(buff, "*") == 0)
2939 buff[0] = 0;
2940
2941 /* handle the case of 'dont filter this module' */
2942 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2943 buff[0] = 0;
2944 not = 1;
2945 }
2946
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002947 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002948}
2949
Steven Rostedtf6180772009-02-14 00:40:25 -05002950/*
2951 * We register the module command as a template to show others how
2952 * to register the a command as well.
2953 */
2954
2955static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002956ftrace_mod_callback(struct ftrace_hash *hash,
2957 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002958{
2959 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002960 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002961
2962 /*
2963 * cmd == 'mod' because we only registered this func
2964 * for the 'mod' ftrace_func_command.
2965 * But if you register one func with multiple commands,
2966 * you can tell which command was used by the cmd
2967 * parameter.
2968 */
2969
2970 /* we must have a module name */
2971 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002972 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002973
2974 mod = strsep(&param, ":");
2975 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002976 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002977
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002978 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002979 if (!ret)
2980 ret = -EINVAL;
2981 if (ret < 0)
2982 return ret;
2983
2984 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002985}
2986
2987static struct ftrace_func_command ftrace_mod_cmd = {
2988 .name = "mod",
2989 .func = ftrace_mod_callback,
2990};
2991
2992static int __init ftrace_mod_cmd_init(void)
2993{
2994 return register_ftrace_command(&ftrace_mod_cmd);
2995}
Steven Rostedt6f415672012-10-05 12:13:07 -04002996core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002997
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002998static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002999 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003000{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003001 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003002 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003003 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003004
3005 key = hash_long(ip, FTRACE_HASH_BITS);
3006
3007 hhd = &ftrace_func_hash[key];
3008
3009 if (hlist_empty(hhd))
3010 return;
3011
3012 /*
3013 * Disable preemption for these calls to prevent a RCU grace
3014 * period. This syncs the hash iteration and freeing of items
3015 * on the hash. rcu_read_lock is too dangerous here.
3016 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003017 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003018 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003019 if (entry->ip == ip)
3020 entry->ops->func(ip, parent_ip, &entry->data);
3021 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003022 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003023}
3024
Steven Rostedtb6887d72009-02-17 12:32:04 -05003025static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003026{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003027 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003028 .flags = FTRACE_OPS_FL_INITIALIZED,
3029 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003030};
3031
Steven Rostedtb6887d72009-02-17 12:32:04 -05003032static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003033
Steven Rostedtb6887d72009-02-17 12:32:04 -05003034static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003035{
Steven Rostedtb8489142011-05-04 09:27:52 -04003036 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003037 int i;
3038
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003039 if (ftrace_probe_registered) {
3040 /* still need to update the function call sites */
3041 if (ftrace_enabled)
3042 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003043 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003044 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003045
3046 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3047 struct hlist_head *hhd = &ftrace_func_hash[i];
3048 if (hhd->first)
3049 break;
3050 }
3051 /* Nothing registered? */
3052 if (i == FTRACE_FUNC_HASHSIZE)
3053 return;
3054
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003055 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003056
Steven Rostedtb6887d72009-02-17 12:32:04 -05003057 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003058}
3059
Steven Rostedtb6887d72009-02-17 12:32:04 -05003060static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003061{
3062 int i;
3063
Steven Rostedtb6887d72009-02-17 12:32:04 -05003064 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003065 return;
3066
3067 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3068 struct hlist_head *hhd = &ftrace_func_hash[i];
3069 if (hhd->first)
3070 return;
3071 }
3072
3073 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003074 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003075
Steven Rostedtb6887d72009-02-17 12:32:04 -05003076 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003077}
3078
3079
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003080static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003081{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003082 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003083 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003084 kfree(entry);
3085}
3086
Steven Rostedt59df055f2009-02-14 15:29:06 -05003087int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003088register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089 void *data)
3090{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003091 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003092 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3093 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003094 struct ftrace_page *pg;
3095 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003096 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003097 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003098 int count = 0;
3099 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003100 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003101
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003102 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003103 len = strlen(search);
3104
Steven Rostedtb6887d72009-02-17 12:32:04 -05003105 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106 if (WARN_ON(not))
3107 return -EINVAL;
3108
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003109 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003110
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003111 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3112 if (!hash) {
3113 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003114 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003115 }
3116
3117 if (unlikely(ftrace_disabled)) {
3118 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003119 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003120 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003121
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003122 mutex_lock(&ftrace_lock);
3123
Steven Rostedt59df055f2009-02-14 15:29:06 -05003124 do_for_each_ftrace_rec(pg, rec) {
3125
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003126 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003127 continue;
3128
3129 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3130 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003131 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003132 if (!count)
3133 count = -ENOMEM;
3134 goto out_unlock;
3135 }
3136
3137 count++;
3138
3139 entry->data = data;
3140
3141 /*
3142 * The caller might want to do something special
3143 * for each function we find. We call the callback
3144 * to give the caller an opportunity to do so.
3145 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003146 if (ops->init) {
3147 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003148 /* caller does not like this func */
3149 kfree(entry);
3150 continue;
3151 }
3152 }
3153
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003154 ret = enter_record(hash, rec, 0);
3155 if (ret < 0) {
3156 kfree(entry);
3157 count = ret;
3158 goto out_unlock;
3159 }
3160
Steven Rostedt59df055f2009-02-14 15:29:06 -05003161 entry->ops = ops;
3162 entry->ip = rec->ip;
3163
3164 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3165 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3166
3167 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003168
3169 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3170 if (ret < 0)
3171 count = ret;
3172
Steven Rostedtb6887d72009-02-17 12:32:04 -05003173 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003174
3175 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003176 mutex_unlock(&ftrace_lock);
3177 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003178 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003179 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003180
3181 return count;
3182}
3183
3184enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003185 PROBE_TEST_FUNC = 1,
3186 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003187};
3188
3189static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003190__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003191 void *data, int flags)
3192{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003193 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003194 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003195 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003196 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003197 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003198 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003199 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003200 char str[KSYM_SYMBOL_LEN];
3201 int type = MATCH_FULL;
3202 int i, len = 0;
3203 char *search;
3204
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003205 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003206 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003207 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003208 int not;
3209
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003210 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003211 len = strlen(search);
3212
Steven Rostedtb6887d72009-02-17 12:32:04 -05003213 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003214 if (WARN_ON(not))
3215 return;
3216 }
3217
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003218 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003219
3220 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3221 if (!hash)
3222 /* Hmm, should report this somehow */
3223 goto out_unlock;
3224
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003225 INIT_LIST_HEAD(&free_list);
3226
Steven Rostedt59df055f2009-02-14 15:29:06 -05003227 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3228 struct hlist_head *hhd = &ftrace_func_hash[i];
3229
Sasha Levinb67bfe02013-02-27 17:06:00 -08003230 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003231
3232 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003233 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003234 continue;
3235
Steven Rostedtb6887d72009-02-17 12:32:04 -05003236 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003237 continue;
3238
3239 /* do this last, since it is the most expensive */
3240 if (glob) {
3241 kallsyms_lookup(entry->ip, NULL, NULL,
3242 NULL, str);
3243 if (!ftrace_match(str, glob, len, type))
3244 continue;
3245 }
3246
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003247 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3248 /* It is possible more than one entry had this ip */
3249 if (rec_entry)
3250 free_hash_entry(hash, rec_entry);
3251
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003252 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003253 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003254 }
3255 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003256 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003257 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003258 /*
3259 * Remove after the disable is called. Otherwise, if the last
3260 * probe is removed, a null hash means *all enabled*.
3261 */
3262 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003263 synchronize_sched();
3264 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3265 list_del(&entry->free_list);
3266 ftrace_free_entry(entry);
3267 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003268 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003269
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003270 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003271 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003272 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003273}
3274
3275void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003276unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003277 void *data)
3278{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003279 __unregister_ftrace_function_probe(glob, ops, data,
3280 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003281}
3282
3283void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003284unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003285{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003286 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003287}
3288
Steven Rostedtb6887d72009-02-17 12:32:04 -05003289void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003290{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003291 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003292}
3293
Steven Rostedtf6180772009-02-14 00:40:25 -05003294static LIST_HEAD(ftrace_commands);
3295static DEFINE_MUTEX(ftrace_cmd_mutex);
3296
Tom Zanussi38de93a2013-10-24 08:34:18 -05003297/*
3298 * Currently we only register ftrace commands from __init, so mark this
3299 * __init too.
3300 */
3301__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003302{
3303 struct ftrace_func_command *p;
3304 int ret = 0;
3305
3306 mutex_lock(&ftrace_cmd_mutex);
3307 list_for_each_entry(p, &ftrace_commands, list) {
3308 if (strcmp(cmd->name, p->name) == 0) {
3309 ret = -EBUSY;
3310 goto out_unlock;
3311 }
3312 }
3313 list_add(&cmd->list, &ftrace_commands);
3314 out_unlock:
3315 mutex_unlock(&ftrace_cmd_mutex);
3316
3317 return ret;
3318}
3319
Tom Zanussi38de93a2013-10-24 08:34:18 -05003320/*
3321 * Currently we only unregister ftrace commands from __init, so mark
3322 * this __init too.
3323 */
3324__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003325{
3326 struct ftrace_func_command *p, *n;
3327 int ret = -ENODEV;
3328
3329 mutex_lock(&ftrace_cmd_mutex);
3330 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3331 if (strcmp(cmd->name, p->name) == 0) {
3332 ret = 0;
3333 list_del_init(&p->list);
3334 goto out_unlock;
3335 }
3336 }
3337 out_unlock:
3338 mutex_unlock(&ftrace_cmd_mutex);
3339
3340 return ret;
3341}
3342
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003343static int ftrace_process_regex(struct ftrace_hash *hash,
3344 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003345{
Steven Rostedtf6180772009-02-14 00:40:25 -05003346 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003347 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003348 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003349
3350 func = strsep(&next, ":");
3351
3352 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003353 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003354 if (!ret)
3355 ret = -EINVAL;
3356 if (ret < 0)
3357 return ret;
3358 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003359 }
3360
Steven Rostedtf6180772009-02-14 00:40:25 -05003361 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003362
3363 command = strsep(&next, ":");
3364
Steven Rostedtf6180772009-02-14 00:40:25 -05003365 mutex_lock(&ftrace_cmd_mutex);
3366 list_for_each_entry(p, &ftrace_commands, list) {
3367 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003368 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003369 goto out_unlock;
3370 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003371 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003372 out_unlock:
3373 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003374
Steven Rostedtf6180772009-02-14 00:40:25 -05003375 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003376}
3377
Ingo Molnare309b412008-05-12 21:20:51 +02003378static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003379ftrace_regex_write(struct file *file, const char __user *ubuf,
3380 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003381{
3382 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003383 struct trace_parser *parser;
3384 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003385
Li Zefan4ba79782009-09-22 13:52:20 +08003386 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003387 return 0;
3388
Steven Rostedt5072c592008-05-12 21:20:43 +02003389 if (file->f_mode & FMODE_READ) {
3390 struct seq_file *m = file->private_data;
3391 iter = m->private;
3392 } else
3393 iter = file->private_data;
3394
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003395 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003396 return -ENODEV;
3397
3398 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003399
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003400 parser = &iter->parser;
3401 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003402
Li Zefan4ba79782009-09-22 13:52:20 +08003403 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003404 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003405 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003406 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003407 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003408 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003409 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003410 }
3411
Steven Rostedt5072c592008-05-12 21:20:43 +02003412 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003413 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003414 return ret;
3415}
3416
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003417ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003418ftrace_filter_write(struct file *file, const char __user *ubuf,
3419 size_t cnt, loff_t *ppos)
3420{
3421 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3422}
3423
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003424ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003425ftrace_notrace_write(struct file *file, const char __user *ubuf,
3426 size_t cnt, loff_t *ppos)
3427{
3428 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3429}
3430
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003431static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003432ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3433{
3434 struct ftrace_func_entry *entry;
3435
3436 if (!ftrace_location(ip))
3437 return -EINVAL;
3438
3439 if (remove) {
3440 entry = ftrace_lookup_ip(hash, ip);
3441 if (!entry)
3442 return -ENOENT;
3443 free_hash_entry(hash, entry);
3444 return 0;
3445 }
3446
3447 return add_hash_entry(hash, ip);
3448}
3449
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003450static void ftrace_ops_update_code(struct ftrace_ops *ops)
3451{
3452 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3453 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3454}
3455
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003456static int
3457ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3458 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003459{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003460 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003461 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003462 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003463
Steven Rostedt41c52c02008-05-22 11:46:33 -04003464 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003465 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003466
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003467 mutex_lock(&ops->regex_lock);
3468
Steven Rostedtf45948e2011-05-02 12:29:25 -04003469 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003470 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003471 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003472 orig_hash = &ops->notrace_hash;
3473
3474 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003475 if (!hash) {
3476 ret = -ENOMEM;
3477 goto out_regex_unlock;
3478 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003479
Steven Rostedt41c52c02008-05-22 11:46:33 -04003480 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003481 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003482 if (buf && !ftrace_match_records(hash, buf, len)) {
3483 ret = -EINVAL;
3484 goto out_regex_unlock;
3485 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003486 if (ip) {
3487 ret = ftrace_match_addr(hash, ip, remove);
3488 if (ret < 0)
3489 goto out_regex_unlock;
3490 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003491
3492 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003493 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003494 if (!ret)
3495 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003496
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003497 mutex_unlock(&ftrace_lock);
3498
Jiri Olsaac483c42012-01-02 10:04:14 +01003499 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003500 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003501
3502 free_ftrace_hash(hash);
3503 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003504}
3505
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003506static int
3507ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3508 int reset, int enable)
3509{
3510 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3511}
3512
3513/**
3514 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3515 * @ops - the ops to set the filter with
3516 * @ip - the address to add to or remove from the filter.
3517 * @remove - non zero to remove the ip from the filter
3518 * @reset - non zero to reset all filters before applying this filter.
3519 *
3520 * Filters denote which functions should be enabled when tracing is enabled
3521 * If @ip is NULL, it failes to update filter.
3522 */
3523int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3524 int remove, int reset)
3525{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003526 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003527 return ftrace_set_addr(ops, ip, remove, reset, 1);
3528}
3529EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3530
3531static int
3532ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3533 int reset, int enable)
3534{
3535 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3536}
3537
Steven Rostedt77a2b372008-05-12 21:20:45 +02003538/**
3539 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003540 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003541 * @buf - the string that holds the function filter text.
3542 * @len - the length of the string.
3543 * @reset - non zero to reset all filters before applying this filter.
3544 *
3545 * Filters denote which functions should be enabled when tracing is enabled.
3546 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3547 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003548int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003549 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003550{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003551 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003552 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003553}
Steven Rostedt936e0742011-05-05 22:54:01 -04003554EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003555
Steven Rostedt41c52c02008-05-22 11:46:33 -04003556/**
3557 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003558 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003559 * @buf - the string that holds the function notrace text.
3560 * @len - the length of the string.
3561 * @reset - non zero to reset all filters before applying this filter.
3562 *
3563 * Notrace Filters denote which functions should not be enabled when tracing
3564 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3565 * for tracing.
3566 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003567int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003568 int len, int reset)
3569{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003570 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003571 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003572}
3573EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3574/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003575 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003576 * @buf - the string that holds the function filter text.
3577 * @len - the length of the string.
3578 * @reset - non zero to reset all filters before applying this filter.
3579 *
3580 * Filters denote which functions should be enabled when tracing is enabled.
3581 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3582 */
3583void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3584{
3585 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3586}
3587EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3588
3589/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003590 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003591 * @buf - the string that holds the function notrace text.
3592 * @len - the length of the string.
3593 * @reset - non zero to reset all filters before applying this filter.
3594 *
3595 * Notrace Filters denote which functions should not be enabled when tracing
3596 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3597 * for tracing.
3598 */
3599void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003600{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003601 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003602}
Steven Rostedt936e0742011-05-05 22:54:01 -04003603EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003604
Steven Rostedt2af15d62009-05-28 13:37:24 -04003605/*
3606 * command line interface to allow users to set filters on boot up.
3607 */
3608#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3609static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3610static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3611
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003612/* Used by function selftest to not test if filter is set */
3613bool ftrace_filter_param __initdata;
3614
Steven Rostedt2af15d62009-05-28 13:37:24 -04003615static int __init set_ftrace_notrace(char *str)
3616{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003617 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003618 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003619 return 1;
3620}
3621__setup("ftrace_notrace=", set_ftrace_notrace);
3622
3623static int __init set_ftrace_filter(char *str)
3624{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003625 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003626 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003627 return 1;
3628}
3629__setup("ftrace_filter=", set_ftrace_filter);
3630
Stefan Assmann369bc182009-10-12 22:17:21 +02003631#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003632static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003633static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003634
Stefan Assmann369bc182009-10-12 22:17:21 +02003635static int __init set_graph_function(char *str)
3636{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003637 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003638 return 1;
3639}
3640__setup("ftrace_graph_filter=", set_graph_function);
3641
3642static void __init set_ftrace_early_graph(char *buf)
3643{
3644 int ret;
3645 char *func;
3646
3647 while (buf) {
3648 func = strsep(&buf, ",");
3649 /* we allow only one expression at a time */
3650 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003651 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003652 if (ret)
3653 printk(KERN_DEBUG "ftrace: function %s not "
3654 "traceable\n", func);
3655 }
3656}
3657#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3658
Steven Rostedt2a85a372011-12-19 21:57:44 -05003659void __init
3660ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003661{
3662 char *func;
3663
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003664 ftrace_ops_init(ops);
3665
Steven Rostedt2af15d62009-05-28 13:37:24 -04003666 while (buf) {
3667 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003668 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003669 }
3670}
3671
3672static void __init set_ftrace_early_filters(void)
3673{
3674 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003675 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003676 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003677 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003678#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3679 if (ftrace_graph_buf[0])
3680 set_ftrace_early_graph(ftrace_graph_buf);
3681#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003682}
3683
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003684int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003685{
3686 struct seq_file *m = (struct seq_file *)file->private_data;
3687 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003688 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003689 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003690 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003691 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003692
Steven Rostedt5072c592008-05-12 21:20:43 +02003693 if (file->f_mode & FMODE_READ) {
3694 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003695 seq_release(inode, file);
3696 } else
3697 iter = file->private_data;
3698
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003699 parser = &iter->parser;
3700 if (trace_parser_loaded(parser)) {
3701 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003702 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003703 }
3704
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003705 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003706
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003707 mutex_lock(&iter->ops->regex_lock);
3708
Steven Rostedt058e2972011-04-29 22:35:33 -04003709 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003710 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3711
3712 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003713 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003714 else
3715 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003716
Steven Rostedt058e2972011-04-29 22:35:33 -04003717 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003718 ret = ftrace_hash_move(iter->ops, filter_hash,
3719 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003720 if (!ret)
3721 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003722
Steven Rostedt058e2972011-04-29 22:35:33 -04003723 mutex_unlock(&ftrace_lock);
3724 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003725
3726 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003727 free_ftrace_hash(iter->hash);
3728 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003729
Steven Rostedt5072c592008-05-12 21:20:43 +02003730 return 0;
3731}
3732
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003733static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003734 .open = ftrace_avail_open,
3735 .read = seq_read,
3736 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003737 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003738};
3739
Steven Rostedt647bcd02011-05-03 14:39:21 -04003740static const struct file_operations ftrace_enabled_fops = {
3741 .open = ftrace_enabled_open,
3742 .read = seq_read,
3743 .llseek = seq_lseek,
3744 .release = seq_release_private,
3745};
3746
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003747static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003748 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003749 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003750 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003751 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003752 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003753};
3754
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003755static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003756 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003757 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003758 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003759 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003760 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003761};
3762
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003763#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3764
3765static DEFINE_MUTEX(graph_lock);
3766
3767int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003768int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003769unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003770unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003771
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003772struct ftrace_graph_data {
3773 unsigned long *table;
3774 size_t size;
3775 int *count;
3776 const struct seq_operations *seq_ops;
3777};
3778
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003779static void *
Li Zefan85951842009-06-24 09:54:00 +08003780__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003781{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003782 struct ftrace_graph_data *fgd = m->private;
3783
3784 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003785 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003786 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003787}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003788
Li Zefan85951842009-06-24 09:54:00 +08003789static void *
3790g_next(struct seq_file *m, void *v, loff_t *pos)
3791{
3792 (*pos)++;
3793 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003794}
3795
3796static void *g_start(struct seq_file *m, loff_t *pos)
3797{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003798 struct ftrace_graph_data *fgd = m->private;
3799
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003800 mutex_lock(&graph_lock);
3801
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003802 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003803 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003804 return (void *)1;
3805
Li Zefan85951842009-06-24 09:54:00 +08003806 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003807}
3808
3809static void g_stop(struct seq_file *m, void *p)
3810{
3811 mutex_unlock(&graph_lock);
3812}
3813
3814static int g_show(struct seq_file *m, void *v)
3815{
3816 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003817
3818 if (!ptr)
3819 return 0;
3820
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003821 if (ptr == (unsigned long *)1) {
3822 seq_printf(m, "#### all functions enabled ####\n");
3823 return 0;
3824 }
3825
Steven Rostedtb375a112009-09-17 00:05:58 -04003826 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003827
3828 return 0;
3829}
3830
James Morris88e9d342009-09-22 16:43:43 -07003831static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003832 .start = g_start,
3833 .next = g_next,
3834 .stop = g_stop,
3835 .show = g_show,
3836};
3837
3838static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003839__ftrace_graph_open(struct inode *inode, struct file *file,
3840 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003841{
3842 int ret = 0;
3843
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003844 mutex_lock(&graph_lock);
3845 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003846 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003847 *fgd->count = 0;
3848 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003849 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003850 mutex_unlock(&graph_lock);
3851
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003852 if (file->f_mode & FMODE_READ) {
3853 ret = seq_open(file, fgd->seq_ops);
3854 if (!ret) {
3855 struct seq_file *m = file->private_data;
3856 m->private = fgd;
3857 }
3858 } else
3859 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003860
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003861 return ret;
3862}
3863
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003864static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003865ftrace_graph_open(struct inode *inode, struct file *file)
3866{
3867 struct ftrace_graph_data *fgd;
3868
3869 if (unlikely(ftrace_disabled))
3870 return -ENODEV;
3871
3872 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3873 if (fgd == NULL)
3874 return -ENOMEM;
3875
3876 fgd->table = ftrace_graph_funcs;
3877 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3878 fgd->count = &ftrace_graph_count;
3879 fgd->seq_ops = &ftrace_graph_seq_ops;
3880
3881 return __ftrace_graph_open(inode, file, fgd);
3882}
3883
3884static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003885ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3886{
3887 struct ftrace_graph_data *fgd;
3888
3889 if (unlikely(ftrace_disabled))
3890 return -ENODEV;
3891
3892 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3893 if (fgd == NULL)
3894 return -ENOMEM;
3895
3896 fgd->table = ftrace_graph_notrace_funcs;
3897 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3898 fgd->count = &ftrace_graph_notrace_count;
3899 fgd->seq_ops = &ftrace_graph_seq_ops;
3900
3901 return __ftrace_graph_open(inode, file, fgd);
3902}
3903
3904static int
Li Zefan87827112009-07-23 11:29:11 +08003905ftrace_graph_release(struct inode *inode, struct file *file)
3906{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003907 if (file->f_mode & FMODE_READ) {
3908 struct seq_file *m = file->private_data;
3909
3910 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003911 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003912 } else {
3913 kfree(file->private_data);
3914 }
3915
Li Zefan87827112009-07-23 11:29:11 +08003916 return 0;
3917}
3918
3919static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003920ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003921{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003922 struct dyn_ftrace *rec;
3923 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003924 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003925 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003926 int type, not;
3927 char *search;
3928 bool exists;
3929 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003930
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003931 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003932 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003933 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003934 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003935
3936 search_len = strlen(search);
3937
Steven Rostedt52baf112009-02-14 01:15:39 -05003938 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003939
3940 if (unlikely(ftrace_disabled)) {
3941 mutex_unlock(&ftrace_lock);
3942 return -ENODEV;
3943 }
3944
Steven Rostedt265c8312009-02-13 12:43:56 -05003945 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003946
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003947 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003948 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003949 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003950 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003951 if (array[i] == rec->ip) {
3952 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003953 break;
3954 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003955 }
3956
3957 if (!not) {
3958 fail = 0;
3959 if (!exists) {
3960 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003961 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003962 goto out;
3963 }
3964 } else {
3965 if (exists) {
3966 array[i] = array[--(*idx)];
3967 array[*idx] = 0;
3968 fail = 0;
3969 }
3970 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003971 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003972 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003973out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003974 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003975
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003976 if (fail)
3977 return -EINVAL;
3978
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003979 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003980}
3981
3982static ssize_t
3983ftrace_graph_write(struct file *file, const char __user *ubuf,
3984 size_t cnt, loff_t *ppos)
3985{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003986 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09003987 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003988 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003989
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003990 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003991 return 0;
3992
Namhyung Kim6a101082013-10-14 17:24:25 +09003993 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
3994 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003995
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003996 read = trace_get_user(&parser, ubuf, cnt, ppos);
3997
Li Zefan4ba79782009-09-22 13:52:20 +08003998 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003999 parser.buffer[parser.idx] = 0;
4000
Namhyung Kim6a101082013-10-14 17:24:25 +09004001 mutex_lock(&graph_lock);
4002
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004003 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004004 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4005 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004006
4007 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004008 }
4009
Namhyung Kim6a101082013-10-14 17:24:25 +09004010 if (!ret)
4011 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004012
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004013 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004014
4015 return ret;
4016}
4017
4018static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004019 .open = ftrace_graph_open,
4020 .read = seq_read,
4021 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004022 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004023 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004024};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004025
4026static const struct file_operations ftrace_graph_notrace_fops = {
4027 .open = ftrace_graph_notrace_open,
4028 .read = seq_read,
4029 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004030 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004031 .release = ftrace_graph_release,
4032};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004033#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4034
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004035void ftrace_create_filter_files(struct ftrace_ops *ops,
4036 struct dentry *parent)
4037{
4038
4039 trace_create_file("set_ftrace_filter", 0644, parent,
4040 ops, &ftrace_filter_fops);
4041
4042 trace_create_file("set_ftrace_notrace", 0644, parent,
4043 ops, &ftrace_notrace_fops);
4044}
4045
4046/*
4047 * The name "destroy_filter_files" is really a misnomer. Although
4048 * in the future, it may actualy delete the files, but this is
4049 * really intended to make sure the ops passed in are disabled
4050 * and that when this function returns, the caller is free to
4051 * free the ops.
4052 *
4053 * The "destroy" name is only to match the "create" name that this
4054 * should be paired with.
4055 */
4056void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4057{
4058 mutex_lock(&ftrace_lock);
4059 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4060 ftrace_shutdown(ops, 0);
4061 ops->flags |= FTRACE_OPS_FL_DELETED;
4062 mutex_unlock(&ftrace_lock);
4063}
4064
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004065static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004066{
Steven Rostedt5072c592008-05-12 21:20:43 +02004067
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004068 trace_create_file("available_filter_functions", 0444,
4069 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004070
Steven Rostedt647bcd02011-05-03 14:39:21 -04004071 trace_create_file("enabled_functions", 0444,
4072 d_tracer, NULL, &ftrace_enabled_fops);
4073
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004074 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004075
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004076#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004077 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004078 NULL,
4079 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004080 trace_create_file("set_graph_notrace", 0444, d_tracer,
4081 NULL,
4082 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004083#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4084
Steven Rostedt5072c592008-05-12 21:20:43 +02004085 return 0;
4086}
4087
Steven Rostedt9fd49322012-04-24 22:32:06 -04004088static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004089{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004090 const unsigned long *ipa = a;
4091 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004092
Steven Rostedt9fd49322012-04-24 22:32:06 -04004093 if (*ipa > *ipb)
4094 return 1;
4095 if (*ipa < *ipb)
4096 return -1;
4097 return 0;
4098}
4099
4100static void ftrace_swap_ips(void *a, void *b, int size)
4101{
4102 unsigned long *ipa = a;
4103 unsigned long *ipb = b;
4104 unsigned long t;
4105
4106 t = *ipa;
4107 *ipa = *ipb;
4108 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004109}
4110
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004111static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004112 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004113 unsigned long *end)
4114{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004115 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004116 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004117 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004118 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004119 unsigned long *p;
4120 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004121 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004122 int ret = -ENOMEM;
4123
4124 count = end - start;
4125
4126 if (!count)
4127 return 0;
4128
Steven Rostedt9fd49322012-04-24 22:32:06 -04004129 sort(start, count, sizeof(*start),
4130 ftrace_cmp_ips, ftrace_swap_ips);
4131
Steven Rostedt706c81f2012-04-24 23:45:26 -04004132 start_pg = ftrace_allocate_pages(count);
4133 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004134 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004135
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004136 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004137
Steven Rostedt32082302011-12-16 14:42:37 -05004138 /*
4139 * Core and each module needs their own pages, as
4140 * modules will free them when they are removed.
4141 * Force a new page to be allocated for modules.
4142 */
Steven Rostedta7900872011-12-16 16:23:44 -05004143 if (!mod) {
4144 WARN_ON(ftrace_pages || ftrace_pages_start);
4145 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004146 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004147 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004148 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004149 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004150
Steven Rostedta7900872011-12-16 16:23:44 -05004151 if (WARN_ON(ftrace_pages->next)) {
4152 /* Hmm, we have free pages? */
4153 while (ftrace_pages->next)
4154 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004155 }
Steven Rostedta7900872011-12-16 16:23:44 -05004156
Steven Rostedt706c81f2012-04-24 23:45:26 -04004157 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004158 }
4159
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004160 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004161 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004162 while (p < end) {
4163 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004164 /*
4165 * Some architecture linkers will pad between
4166 * the different mcount_loc sections of different
4167 * object files to satisfy alignments.
4168 * Skip any NULL pointers.
4169 */
4170 if (!addr)
4171 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004172
4173 if (pg->index == pg->size) {
4174 /* We should have allocated enough */
4175 if (WARN_ON(!pg->next))
4176 break;
4177 pg = pg->next;
4178 }
4179
4180 rec = &pg->records[pg->index++];
4181 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004182 }
4183
Steven Rostedt706c81f2012-04-24 23:45:26 -04004184 /* We should have used all pages */
4185 WARN_ON(pg->next);
4186
4187 /* Assign the last page to ftrace_pages */
4188 ftrace_pages = pg;
4189
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004190 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004191 * We only need to disable interrupts on start up
4192 * because we are modifying code that an interrupt
4193 * may execute, and the modification is not atomic.
4194 * But for modules, nothing runs the code we modify
4195 * until we are finished with it, and there's no
4196 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004197 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004198 if (!mod)
4199 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004200 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004201 if (!mod)
4202 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004203 ret = 0;
4204 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004205 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004206
Steven Rostedta7900872011-12-16 16:23:44 -05004207 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004208}
4209
Steven Rostedt93eb6772009-04-15 13:24:06 -04004210#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004211
4212#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4213
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004214void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004215{
4216 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004217 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004218 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004219 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004220
Steven Rostedt93eb6772009-04-15 13:24:06 -04004221 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004222
4223 if (ftrace_disabled)
4224 goto out_unlock;
4225
Steven Rostedt32082302011-12-16 14:42:37 -05004226 /*
4227 * Each module has its own ftrace_pages, remove
4228 * them from the list.
4229 */
4230 last_pg = &ftrace_pages_start;
4231 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4232 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004233 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004234 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004235 * As core pages are first, the first
4236 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004237 */
Steven Rostedt32082302011-12-16 14:42:37 -05004238 if (WARN_ON(pg == ftrace_pages_start))
4239 goto out_unlock;
4240
4241 /* Check if we are deleting the last page */
4242 if (pg == ftrace_pages)
4243 ftrace_pages = next_to_ftrace_page(last_pg);
4244
4245 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004246 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4247 free_pages((unsigned long)pg->records, order);
4248 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004249 } else
4250 last_pg = &pg->next;
4251 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004252 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004253 mutex_unlock(&ftrace_lock);
4254}
4255
4256static void ftrace_init_module(struct module *mod,
4257 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004258{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004259 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004260 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004261 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004262}
4263
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004264static int ftrace_module_notify_enter(struct notifier_block *self,
4265 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004266{
4267 struct module *mod = data;
4268
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004269 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004270 ftrace_init_module(mod, mod->ftrace_callsites,
4271 mod->ftrace_callsites +
4272 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004273 return 0;
4274}
4275
4276static int ftrace_module_notify_exit(struct notifier_block *self,
4277 unsigned long val, void *data)
4278{
4279 struct module *mod = data;
4280
4281 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004282 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004283
4284 return 0;
4285}
4286#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004287static int ftrace_module_notify_enter(struct notifier_block *self,
4288 unsigned long val, void *data)
4289{
4290 return 0;
4291}
4292static int ftrace_module_notify_exit(struct notifier_block *self,
4293 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004294{
4295 return 0;
4296}
4297#endif /* CONFIG_MODULES */
4298
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004299struct notifier_block ftrace_module_enter_nb = {
4300 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004301 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004302};
4303
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004304struct notifier_block ftrace_module_exit_nb = {
4305 .notifier_call = ftrace_module_notify_exit,
4306 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4307};
4308
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004309void __init ftrace_init(void)
4310{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004311 extern unsigned long __start_mcount_loc[];
4312 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004313 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004314 int ret;
4315
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004316 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004317 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004318 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004319 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004320 goto failed;
4321
4322 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004323 if (!count) {
4324 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004325 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004326 }
4327
4328 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4329 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004330
4331 last_ftrace_enabled = ftrace_enabled = 1;
4332
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004333 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004334 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004335 __stop_mcount_loc);
4336
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004337 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004338 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004339 pr_warning("Failed to register trace ftrace module enter notifier\n");
4340
4341 ret = register_module_notifier(&ftrace_module_exit_nb);
4342 if (ret)
4343 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004344
Steven Rostedt2af15d62009-05-28 13:37:24 -04004345 set_ftrace_early_filters();
4346
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004347 return;
4348 failed:
4349 ftrace_disabled = 1;
4350}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004351
Steven Rostedt3d083392008-05-12 21:20:42 +02004352#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004353
Steven Rostedt2b499382011-05-03 22:49:52 -04004354static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004355 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004356 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4357 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004358};
4359
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004360static int __init ftrace_nodyn_init(void)
4361{
4362 ftrace_enabled = 1;
4363 return 0;
4364}
Steven Rostedt6f415672012-10-05 12:13:07 -04004365core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004366
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004367static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4368static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004369/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004370# define ftrace_startup(ops, command) \
4371 ({ \
4372 int ___ret = __register_ftrace_function(ops); \
4373 if (!___ret) \
4374 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4375 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004376 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004377# define ftrace_shutdown(ops, command) \
4378 ({ \
4379 int ___ret = __unregister_ftrace_function(ops); \
4380 if (!___ret) \
4381 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4382 ___ret; \
4383 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004384
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004385# define ftrace_startup_sysctl() do { } while (0)
4386# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004387
4388static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004389ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004390{
4391 return 1;
4392}
4393
Steven Rostedt3d083392008-05-12 21:20:42 +02004394#endif /* CONFIG_DYNAMIC_FTRACE */
4395
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004396__init void ftrace_init_global_array_ops(struct trace_array *tr)
4397{
4398 tr->ops = &global_ops;
4399 tr->ops->private = tr;
4400}
4401
4402void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4403{
4404 /* If we filter on pids, update to use the pid function */
4405 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4406 if (WARN_ON(tr->ops->func != ftrace_stub))
4407 printk("ftrace ops had %pS for function\n",
4408 tr->ops->func);
4409 /* Only the top level instance does pid tracing */
4410 if (!list_empty(&ftrace_pids)) {
4411 set_ftrace_pid_function(func);
4412 func = ftrace_pid_func;
4413 }
4414 }
4415 tr->ops->func = func;
4416 tr->ops->private = tr;
4417}
4418
4419void ftrace_reset_array_ops(struct trace_array *tr)
4420{
4421 tr->ops->func = ftrace_stub;
4422}
4423
Steven Rostedtb8489142011-05-04 09:27:52 -04004424static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004425ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004426 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004427{
Jiri Olsae2484912012-02-15 15:51:48 +01004428 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4429 return;
4430
4431 /*
4432 * Some of the ops may be dynamically allocated,
4433 * they must be freed after a synchronize_sched().
4434 */
4435 preempt_disable_notrace();
4436 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004437
4438 /*
4439 * Control funcs (perf) uses RCU. Only trace if
4440 * RCU is currently active.
4441 */
4442 if (!rcu_is_watching())
4443 goto out;
4444
Steven Rostedt0a016402012-11-02 17:03:03 -04004445 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004446 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4447 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004448 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004449 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004450 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004451 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004452 trace_recursion_clear(TRACE_CONTROL_BIT);
4453 preempt_enable_notrace();
4454}
4455
4456static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004457 .func = ftrace_ops_control_func,
4458 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4459 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004460};
4461
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004462static inline void
4463__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004464 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004465{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004466 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004467 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004468
Steven Rostedtccf36722012-06-05 09:44:25 -04004469 if (function_trace_stop)
4470 return;
4471
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004472 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4473 if (bit < 0)
4474 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004475
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004476 /*
4477 * Some of the ops may be dynamically allocated,
4478 * they must be freed after a synchronize_sched().
4479 */
4480 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004481 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004482 if (ftrace_ops_test(op, ip, regs)) {
4483 if (WARN_ON(!op->func)) {
4484 function_trace_stop = 1;
4485 printk("op=%p %pS\n", op, op);
4486 goto out;
4487 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004488 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004489 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004490 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004491out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004492 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004493 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004494}
4495
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004496/*
4497 * Some archs only support passing ip and parent_ip. Even though
4498 * the list function ignores the op parameter, we do not want any
4499 * C side effects, where a function is called without the caller
4500 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004501 * Archs are to support both the regs and ftrace_ops at the same time.
4502 * If they support ftrace_ops, it is assumed they support regs.
4503 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004504 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4505 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004506 * An architecture can pass partial regs with ftrace_ops and still
4507 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004508 */
4509#if ARCH_SUPPORTS_FTRACE_OPS
4510static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004511 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004512{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004513 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004514}
4515#else
4516static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4517{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004518 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004519}
4520#endif
4521
Steven Rostedte32d8952008-12-04 00:26:41 -05004522static void clear_ftrace_swapper(void)
4523{
4524 struct task_struct *p;
4525 int cpu;
4526
4527 get_online_cpus();
4528 for_each_online_cpu(cpu) {
4529 p = idle_task(cpu);
4530 clear_tsk_trace_trace(p);
4531 }
4532 put_online_cpus();
4533}
4534
4535static void set_ftrace_swapper(void)
4536{
4537 struct task_struct *p;
4538 int cpu;
4539
4540 get_online_cpus();
4541 for_each_online_cpu(cpu) {
4542 p = idle_task(cpu);
4543 set_tsk_trace_trace(p);
4544 }
4545 put_online_cpus();
4546}
4547
4548static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004549{
4550 struct task_struct *p;
4551
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004552 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004553 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004554 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004555 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004556 rcu_read_unlock();
4557
Steven Rostedte32d8952008-12-04 00:26:41 -05004558 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004559}
4560
Steven Rostedte32d8952008-12-04 00:26:41 -05004561static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004562{
4563 struct task_struct *p;
4564
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004565 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004566 do_each_pid_task(pid, PIDTYPE_PID, p) {
4567 set_tsk_trace_trace(p);
4568 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004569 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004570}
4571
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004572static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004573{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004574 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004575 clear_ftrace_swapper();
4576 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004577 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004578}
4579
4580static void set_ftrace_pid_task(struct pid *pid)
4581{
4582 if (pid == ftrace_swapper_pid)
4583 set_ftrace_swapper();
4584 else
4585 set_ftrace_pid(pid);
4586}
4587
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004588static int ftrace_pid_add(int p)
4589{
4590 struct pid *pid;
4591 struct ftrace_pid *fpid;
4592 int ret = -EINVAL;
4593
4594 mutex_lock(&ftrace_lock);
4595
4596 if (!p)
4597 pid = ftrace_swapper_pid;
4598 else
4599 pid = find_get_pid(p);
4600
4601 if (!pid)
4602 goto out;
4603
4604 ret = 0;
4605
4606 list_for_each_entry(fpid, &ftrace_pids, list)
4607 if (fpid->pid == pid)
4608 goto out_put;
4609
4610 ret = -ENOMEM;
4611
4612 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4613 if (!fpid)
4614 goto out_put;
4615
4616 list_add(&fpid->list, &ftrace_pids);
4617 fpid->pid = pid;
4618
4619 set_ftrace_pid_task(pid);
4620
4621 ftrace_update_pid_func();
4622 ftrace_startup_enable(0);
4623
4624 mutex_unlock(&ftrace_lock);
4625 return 0;
4626
4627out_put:
4628 if (pid != ftrace_swapper_pid)
4629 put_pid(pid);
4630
4631out:
4632 mutex_unlock(&ftrace_lock);
4633 return ret;
4634}
4635
4636static void ftrace_pid_reset(void)
4637{
4638 struct ftrace_pid *fpid, *safe;
4639
4640 mutex_lock(&ftrace_lock);
4641 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4642 struct pid *pid = fpid->pid;
4643
4644 clear_ftrace_pid_task(pid);
4645
4646 list_del(&fpid->list);
4647 kfree(fpid);
4648 }
4649
4650 ftrace_update_pid_func();
4651 ftrace_startup_enable(0);
4652
4653 mutex_unlock(&ftrace_lock);
4654}
4655
4656static void *fpid_start(struct seq_file *m, loff_t *pos)
4657{
4658 mutex_lock(&ftrace_lock);
4659
4660 if (list_empty(&ftrace_pids) && (!*pos))
4661 return (void *) 1;
4662
4663 return seq_list_start(&ftrace_pids, *pos);
4664}
4665
4666static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4667{
4668 if (v == (void *)1)
4669 return NULL;
4670
4671 return seq_list_next(v, &ftrace_pids, pos);
4672}
4673
4674static void fpid_stop(struct seq_file *m, void *p)
4675{
4676 mutex_unlock(&ftrace_lock);
4677}
4678
4679static int fpid_show(struct seq_file *m, void *v)
4680{
4681 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4682
4683 if (v == (void *)1) {
4684 seq_printf(m, "no pid\n");
4685 return 0;
4686 }
4687
4688 if (fpid->pid == ftrace_swapper_pid)
4689 seq_printf(m, "swapper tasks\n");
4690 else
4691 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4692
4693 return 0;
4694}
4695
4696static const struct seq_operations ftrace_pid_sops = {
4697 .start = fpid_start,
4698 .next = fpid_next,
4699 .stop = fpid_stop,
4700 .show = fpid_show,
4701};
4702
4703static int
4704ftrace_pid_open(struct inode *inode, struct file *file)
4705{
4706 int ret = 0;
4707
4708 if ((file->f_mode & FMODE_WRITE) &&
4709 (file->f_flags & O_TRUNC))
4710 ftrace_pid_reset();
4711
4712 if (file->f_mode & FMODE_READ)
4713 ret = seq_open(file, &ftrace_pid_sops);
4714
4715 return ret;
4716}
4717
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004718static ssize_t
4719ftrace_pid_write(struct file *filp, const char __user *ubuf,
4720 size_t cnt, loff_t *ppos)
4721{
Ingo Molnar457dc922009-11-23 11:03:28 +01004722 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004723 long val;
4724 int ret;
4725
4726 if (cnt >= sizeof(buf))
4727 return -EINVAL;
4728
4729 if (copy_from_user(&buf, ubuf, cnt))
4730 return -EFAULT;
4731
4732 buf[cnt] = 0;
4733
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004734 /*
4735 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4736 * to clean the filter quietly.
4737 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004738 tmp = strstrip(buf);
4739 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004740 return 1;
4741
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004742 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004743 if (ret < 0)
4744 return ret;
4745
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004746 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004747
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004748 return ret ? ret : cnt;
4749}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004750
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004751static int
4752ftrace_pid_release(struct inode *inode, struct file *file)
4753{
4754 if (file->f_mode & FMODE_READ)
4755 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004756
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004757 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004758}
4759
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004760static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004761 .open = ftrace_pid_open,
4762 .write = ftrace_pid_write,
4763 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004764 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004765 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004766};
4767
4768static __init int ftrace_init_debugfs(void)
4769{
4770 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004771
4772 d_tracer = tracing_init_dentry();
4773 if (!d_tracer)
4774 return 0;
4775
4776 ftrace_init_dyn_debugfs(d_tracer);
4777
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004778 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4779 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004780
4781 ftrace_profile_debugfs(d_tracer);
4782
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004783 return 0;
4784}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004785fs_initcall(ftrace_init_debugfs);
4786
Steven Rostedt3d083392008-05-12 21:20:42 +02004787/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004788 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004789 *
4790 * This function should be used by panic code. It stops ftrace
4791 * but in a not so nice way. If you need to simply kill ftrace
4792 * from a non-atomic section, use ftrace_kill.
4793 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004794void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004795{
4796 ftrace_disabled = 1;
4797 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004798 clear_ftrace_function();
4799}
4800
4801/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004802 * Test if ftrace is dead or not.
4803 */
4804int ftrace_is_dead(void)
4805{
4806 return ftrace_disabled;
4807}
4808
4809/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004810 * register_ftrace_function - register a function for profiling
4811 * @ops - ops structure that holds the function for profiling.
4812 *
4813 * Register a function to be called by all functions in the
4814 * kernel.
4815 *
4816 * Note: @ops->func and all the functions it calls must be labeled
4817 * with "notrace", otherwise it will go into a
4818 * recursive loop.
4819 */
4820int register_ftrace_function(struct ftrace_ops *ops)
4821{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004822 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004823
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004824 ftrace_ops_init(ops);
4825
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004826 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004827
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004828 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004829
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004830 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004831
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004832 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004833}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004834EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004835
4836/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004837 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004838 * @ops - ops structure that holds the function to unregister
4839 *
4840 * Unregister a function that was added to be called by ftrace profiling.
4841 */
4842int unregister_ftrace_function(struct ftrace_ops *ops)
4843{
4844 int ret;
4845
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004846 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004847 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004848 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004849
4850 return ret;
4851}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004852EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004853
Ingo Molnare309b412008-05-12 21:20:51 +02004854int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004855ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004856 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004857 loff_t *ppos)
4858{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004859 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004860
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004861 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004862
Steven Rostedt45a4a232011-04-21 23:16:46 -04004863 if (unlikely(ftrace_disabled))
4864 goto out;
4865
4866 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004867
Li Zefana32c7762009-06-26 16:55:51 +08004868 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004869 goto out;
4870
Li Zefana32c7762009-06-26 16:55:51 +08004871 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004872
4873 if (ftrace_enabled) {
4874
4875 ftrace_startup_sysctl();
4876
4877 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004878 if (ftrace_ops_list != &ftrace_list_end)
4879 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004880
4881 } else {
4882 /* stopping ftrace calls (just send to ftrace_stub) */
4883 ftrace_trace_function = ftrace_stub;
4884
4885 ftrace_shutdown_sysctl();
4886 }
4887
4888 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004889 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004890 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004891}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004892
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004893#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004894
Steven Rostedt597af812009-04-03 15:24:12 -04004895static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004896
Steven Rostedte49dc192008-12-02 23:50:05 -05004897int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4898{
4899 return 0;
4900}
4901
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004902/* The callbacks that hook a function */
4903trace_func_graph_ret_t ftrace_graph_return =
4904 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004905trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05004906static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004907
4908/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4909static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4910{
4911 int i;
4912 int ret = 0;
4913 unsigned long flags;
4914 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4915 struct task_struct *g, *t;
4916
4917 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4918 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4919 * sizeof(struct ftrace_ret_stack),
4920 GFP_KERNEL);
4921 if (!ret_stack_list[i]) {
4922 start = 0;
4923 end = i;
4924 ret = -ENOMEM;
4925 goto free;
4926 }
4927 }
4928
4929 read_lock_irqsave(&tasklist_lock, flags);
4930 do_each_thread(g, t) {
4931 if (start == end) {
4932 ret = -EAGAIN;
4933 goto unlock;
4934 }
4935
4936 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004937 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004938 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004939 t->curr_ret_stack = -1;
4940 /* Make sure the tasks see the -1 first: */
4941 smp_wmb();
4942 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004943 }
4944 } while_each_thread(g, t);
4945
4946unlock:
4947 read_unlock_irqrestore(&tasklist_lock, flags);
4948free:
4949 for (i = start; i < end; i++)
4950 kfree(ret_stack_list[i]);
4951 return ret;
4952}
4953
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004954static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004955ftrace_graph_probe_sched_switch(void *ignore,
4956 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004957{
4958 unsigned long long timestamp;
4959 int index;
4960
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004961 /*
4962 * Does the user want to count the time a function was asleep.
4963 * If so, do not update the time stamps.
4964 */
4965 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4966 return;
4967
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004968 timestamp = trace_clock_local();
4969
4970 prev->ftrace_timestamp = timestamp;
4971
4972 /* only process tasks that we timestamped */
4973 if (!next->ftrace_timestamp)
4974 return;
4975
4976 /*
4977 * Update all the counters in next to make up for the
4978 * time next was sleeping.
4979 */
4980 timestamp -= next->ftrace_timestamp;
4981
4982 for (index = next->curr_ret_stack; index >= 0; index--)
4983 next->ret_stack[index].calltime += timestamp;
4984}
4985
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004986/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004987static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004988{
4989 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004990 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004991
4992 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4993 sizeof(struct ftrace_ret_stack *),
4994 GFP_KERNEL);
4995
4996 if (!ret_stack_list)
4997 return -ENOMEM;
4998
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004999 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005000 for_each_online_cpu(cpu) {
5001 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005002 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005003 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005004
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005005 do {
5006 ret = alloc_retstack_tasklist(ret_stack_list);
5007 } while (ret == -EAGAIN);
5008
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005009 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005010 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005011 if (ret)
5012 pr_info("ftrace_graph: Couldn't activate tracepoint"
5013 " probe to kernel_sched_switch\n");
5014 }
5015
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005016 kfree(ret_stack_list);
5017 return ret;
5018}
5019
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005020/*
5021 * Hibernation protection.
5022 * The state of the current task is too much unstable during
5023 * suspend/restore to disk. We want to protect against that.
5024 */
5025static int
5026ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5027 void *unused)
5028{
5029 switch (state) {
5030 case PM_HIBERNATION_PREPARE:
5031 pause_graph_tracing();
5032 break;
5033
5034 case PM_POST_HIBERNATION:
5035 unpause_graph_tracing();
5036 break;
5037 }
5038 return NOTIFY_DONE;
5039}
5040
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005041static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5042{
5043 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5044 return 0;
5045 return __ftrace_graph_entry(trace);
5046}
5047
5048/*
5049 * The function graph tracer should only trace the functions defined
5050 * by set_ftrace_filter and set_ftrace_notrace. If another function
5051 * tracer ops is registered, the graph tracer requires testing the
5052 * function against the global ops, and not just trace any function
5053 * that any ftrace_ops registered.
5054 */
5055static void update_function_graph_func(void)
5056{
5057 if (ftrace_ops_list == &ftrace_list_end ||
5058 (ftrace_ops_list == &global_ops &&
5059 global_ops.next == &ftrace_list_end))
5060 ftrace_graph_entry = __ftrace_graph_entry;
5061 else
5062 ftrace_graph_entry = ftrace_graph_entry_test;
5063}
5064
Mathias Krause8275f692014-03-30 15:31:50 +02005065static struct notifier_block ftrace_suspend_notifier = {
5066 .notifier_call = ftrace_suspend_notifier_call,
5067};
5068
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005069int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5070 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005071{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005072 int ret = 0;
5073
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005074 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005075
Steven Rostedt05ce5812009-03-24 00:18:31 -04005076 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005077 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005078 ret = -EBUSY;
5079 goto out;
5080 }
5081
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005082 register_pm_notifier(&ftrace_suspend_notifier);
5083
Steven Rostedt597af812009-04-03 15:24:12 -04005084 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005085 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005086 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005087 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005088 goto out;
5089 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005090
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005091 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005092
5093 /*
5094 * Update the indirect function to the entryfunc, and the
5095 * function that gets called to the entry_test first. Then
5096 * call the update fgraph entry function to determine if
5097 * the entryfunc should be called directly or not.
5098 */
5099 __ftrace_graph_entry = entryfunc;
5100 ftrace_graph_entry = ftrace_graph_entry_test;
5101 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005102
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005103 /* Function graph doesn't use the .func field of global_ops */
5104 global_ops.flags |= FTRACE_OPS_FL_STUB;
5105
5106 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005107
5108out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005109 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005110 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005111}
5112
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005113void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005114{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005115 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005116
Steven Rostedt597af812009-04-03 15:24:12 -04005117 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005118 goto out;
5119
Steven Rostedt597af812009-04-03 15:24:12 -04005120 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005121 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005122 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005123 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005124 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5125 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005126 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005127 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005128
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005129 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005130 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005131}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005132
Steven Rostedt868baf02011-02-10 21:26:13 -05005133static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5134
5135static void
5136graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5137{
5138 atomic_set(&t->tracing_graph_pause, 0);
5139 atomic_set(&t->trace_overrun, 0);
5140 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005141 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005142 smp_wmb();
5143 t->ret_stack = ret_stack;
5144}
5145
5146/*
5147 * Allocate a return stack for the idle task. May be the first
5148 * time through, or it may be done by CPU hotplug online.
5149 */
5150void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5151{
5152 t->curr_ret_stack = -1;
5153 /*
5154 * The idle task has no parent, it either has its own
5155 * stack or no stack at all.
5156 */
5157 if (t->ret_stack)
5158 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5159
5160 if (ftrace_graph_active) {
5161 struct ftrace_ret_stack *ret_stack;
5162
5163 ret_stack = per_cpu(idle_ret_stack, cpu);
5164 if (!ret_stack) {
5165 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5166 * sizeof(struct ftrace_ret_stack),
5167 GFP_KERNEL);
5168 if (!ret_stack)
5169 return;
5170 per_cpu(idle_ret_stack, cpu) = ret_stack;
5171 }
5172 graph_init_task(t, ret_stack);
5173 }
5174}
5175
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005176/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005177void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005178{
Steven Rostedt84047e32009-06-02 16:51:55 -04005179 /* Make sure we do not use the parent ret_stack */
5180 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005181 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005182
Steven Rostedt597af812009-04-03 15:24:12 -04005183 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005184 struct ftrace_ret_stack *ret_stack;
5185
5186 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005187 * sizeof(struct ftrace_ret_stack),
5188 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005189 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005190 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005191 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005192 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005193}
5194
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005195void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005196{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005197 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5198
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005199 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005200 /* NULL must become visible to IRQs before we free it: */
5201 barrier();
5202
5203 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005204}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005205
5206void ftrace_graph_stop(void)
5207{
5208 ftrace_stop();
5209}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005210#endif