blob: 52c2b53b79534e0a609bf81c0d17b3fe94381e3c [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.
1704 * Otherwise, if the EN flag is set, then return
1705 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1706 * from the non-save regs, to a save regs function.
1707 * Otherwise,
1708 * return UPDATE_MODIFY_CALL to tell the caller to convert
1709 * from the save regs, to a non-save regs function.
1710 */
1711 if (flag & FTRACE_FL_ENABLED)
1712 return FTRACE_UPDATE_MAKE_CALL;
1713 else if (rec->flags & FTRACE_FL_REGS_EN)
1714 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1715 else
1716 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001717 }
1718
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001719 if (update) {
1720 /* If there's no more users, clear all flags */
1721 if (!(rec->flags & ~FTRACE_FL_MASK))
1722 rec->flags = 0;
1723 else
1724 /* Just disable the record (keep REGS state) */
1725 rec->flags &= ~FTRACE_FL_ENABLED;
1726 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001727
1728 return FTRACE_UPDATE_MAKE_NOP;
1729}
1730
1731/**
1732 * ftrace_update_record, set a record that now is tracing or not
1733 * @rec: the record to update
1734 * @enable: set to 1 if the record is tracing, zero to force disable
1735 *
1736 * The records that represent all functions that can be traced need
1737 * to be updated when tracing has been enabled.
1738 */
1739int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1740{
1741 return ftrace_check_record(rec, enable, 1);
1742}
1743
1744/**
1745 * ftrace_test_record, check if the record has been enabled or not
1746 * @rec: the record to test
1747 * @enable: set to 1 to check if enabled, 0 if it is disabled
1748 *
1749 * The arch code may need to test if a record is already set to
1750 * tracing to determine how to modify the function code that it
1751 * represents.
1752 */
1753int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1754{
1755 return ftrace_check_record(rec, enable, 0);
1756}
1757
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001758/**
1759 * ftrace_get_addr_new - Get the call address to set to
1760 * @rec: The ftrace record descriptor
1761 *
1762 * If the record has the FTRACE_FL_REGS set, that means that it
1763 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1764 * is not not set, then it wants to convert to the normal callback.
1765 *
1766 * Returns the address of the trampoline to set to
1767 */
1768unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1769{
1770 if (rec->flags & FTRACE_FL_REGS)
1771 return (unsigned long)FTRACE_REGS_ADDR;
1772 else
1773 return (unsigned long)FTRACE_ADDR;
1774}
1775
1776/**
1777 * ftrace_get_addr_curr - Get the call address that is already there
1778 * @rec: The ftrace record descriptor
1779 *
1780 * The FTRACE_FL_REGS_EN is set when the record already points to
1781 * a function that saves all the regs. Basically the '_EN' version
1782 * represents the current state of the function.
1783 *
1784 * Returns the address of the trampoline that is currently being called
1785 */
1786unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1787{
1788 if (rec->flags & FTRACE_FL_REGS_EN)
1789 return (unsigned long)FTRACE_REGS_ADDR;
1790 else
1791 return (unsigned long)FTRACE_ADDR;
1792}
1793
Steven Rostedtc88fd862011-08-16 09:53:39 -04001794static int
1795__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1796{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001797 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001798 unsigned long ftrace_addr;
1799 int ret;
1800
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001801 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001802
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04001803 /* This needs to be done before we call ftrace_update_record */
1804 ftrace_old_addr = ftrace_get_addr_curr(rec);
1805
1806 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001807
Steven Rostedtc88fd862011-08-16 09:53:39 -04001808 switch (ret) {
1809 case FTRACE_UPDATE_IGNORE:
1810 return 0;
1811
1812 case FTRACE_UPDATE_MAKE_CALL:
1813 return ftrace_make_call(rec, ftrace_addr);
1814
1815 case FTRACE_UPDATE_MAKE_NOP:
1816 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001817
1818 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1819 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001820 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001821 }
1822
1823 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001824}
1825
Steven Rostedte4f5d542012-04-27 09:13:18 -04001826void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001827{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001828 struct dyn_ftrace *rec;
1829 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001830 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001831
Steven Rostedt45a4a232011-04-21 23:16:46 -04001832 if (unlikely(ftrace_disabled))
1833 return;
1834
Steven Rostedt265c8312009-02-13 12:43:56 -05001835 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001836 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001837 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001838 ftrace_bug(failed, rec->ip);
1839 /* Stop processing */
1840 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001841 }
1842 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001843}
1844
Steven Rostedtc88fd862011-08-16 09:53:39 -04001845struct ftrace_rec_iter {
1846 struct ftrace_page *pg;
1847 int index;
1848};
1849
1850/**
1851 * ftrace_rec_iter_start, start up iterating over traced functions
1852 *
1853 * Returns an iterator handle that is used to iterate over all
1854 * the records that represent address locations where functions
1855 * are traced.
1856 *
1857 * May return NULL if no records are available.
1858 */
1859struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1860{
1861 /*
1862 * We only use a single iterator.
1863 * Protected by the ftrace_lock mutex.
1864 */
1865 static struct ftrace_rec_iter ftrace_rec_iter;
1866 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1867
1868 iter->pg = ftrace_pages_start;
1869 iter->index = 0;
1870
1871 /* Could have empty pages */
1872 while (iter->pg && !iter->pg->index)
1873 iter->pg = iter->pg->next;
1874
1875 if (!iter->pg)
1876 return NULL;
1877
1878 return iter;
1879}
1880
1881/**
1882 * ftrace_rec_iter_next, get the next record to process.
1883 * @iter: The handle to the iterator.
1884 *
1885 * Returns the next iterator after the given iterator @iter.
1886 */
1887struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1888{
1889 iter->index++;
1890
1891 if (iter->index >= iter->pg->index) {
1892 iter->pg = iter->pg->next;
1893 iter->index = 0;
1894
1895 /* Could have empty pages */
1896 while (iter->pg && !iter->pg->index)
1897 iter->pg = iter->pg->next;
1898 }
1899
1900 if (!iter->pg)
1901 return NULL;
1902
1903 return iter;
1904}
1905
1906/**
1907 * ftrace_rec_iter_record, get the record at the iterator location
1908 * @iter: The current iterator location
1909 *
1910 * Returns the record that the current @iter is at.
1911 */
1912struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1913{
1914 return &iter->pg->records[iter->index];
1915}
1916
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301917static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001918ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001919{
1920 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001921 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001922
1923 ip = rec->ip;
1924
Steven Rostedt45a4a232011-04-21 23:16:46 -04001925 if (unlikely(ftrace_disabled))
1926 return 0;
1927
Shaohua Li25aac9d2009-01-09 11:29:40 +08001928 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001929 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001930 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301931 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001932 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301933 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001934}
1935
Steven Rostedt000ab692009-02-17 13:35:06 -05001936/*
1937 * archs can override this function if they must do something
1938 * before the modifying code is performed.
1939 */
1940int __weak ftrace_arch_code_modify_prepare(void)
1941{
1942 return 0;
1943}
1944
1945/*
1946 * archs can override this function if they must do something
1947 * after the modifying code is performed.
1948 */
1949int __weak ftrace_arch_code_modify_post_process(void)
1950{
1951 return 0;
1952}
1953
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001954void ftrace_modify_all_code(int command)
1955{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001956 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01001957 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001958
1959 /*
1960 * If the ftrace_caller calls a ftrace_ops func directly,
1961 * we need to make sure that it only traces functions it
1962 * expects to trace. When doing the switch of functions,
1963 * we need to update to the ftrace_ops_list_func first
1964 * before the transition between old and new calls are set,
1965 * as the ftrace_ops_list_func will check the ops hashes
1966 * to make sure the ops are having the right functions
1967 * traced.
1968 */
Petr Mladekcd210672014-02-24 17:12:21 +01001969 if (update) {
1970 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
1971 if (FTRACE_WARN_ON(err))
1972 return;
1973 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001974
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001975 if (command & FTRACE_UPDATE_CALLS)
1976 ftrace_replace_code(1);
1977 else if (command & FTRACE_DISABLE_CALLS)
1978 ftrace_replace_code(0);
1979
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05001980 if (update && ftrace_trace_function != ftrace_ops_list_func) {
1981 function_trace_op = set_function_trace_op;
1982 smp_wmb();
1983 /* If irqs are disabled, we are in stop machine */
1984 if (!irqs_disabled())
1985 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01001986 err = ftrace_update_ftrace_func(ftrace_trace_function);
1987 if (FTRACE_WARN_ON(err))
1988 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05001989 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001990
1991 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01001992 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001993 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01001994 err = ftrace_disable_ftrace_graph_caller();
1995 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001996}
1997
Ingo Molnare309b412008-05-12 21:20:51 +02001998static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001999{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002000 int *command = data;
2001
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002002 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002003
Steven Rostedtc88fd862011-08-16 09:53:39 -04002004 return 0;
2005}
2006
2007/**
2008 * ftrace_run_stop_machine, go back to the stop machine method
2009 * @command: The command to tell ftrace what to do
2010 *
2011 * If an arch needs to fall back to the stop machine method, the
2012 * it can call this function.
2013 */
2014void ftrace_run_stop_machine(int command)
2015{
2016 stop_machine(__ftrace_modify_code, &command, NULL);
2017}
2018
2019/**
2020 * arch_ftrace_update_code, modify the code to trace or not trace
2021 * @command: The command that needs to be done
2022 *
2023 * Archs can override this function if it does not need to
2024 * run stop_machine() to modify code.
2025 */
2026void __weak arch_ftrace_update_code(int command)
2027{
2028 ftrace_run_stop_machine(command);
2029}
2030
2031static void ftrace_run_update_code(int command)
2032{
2033 int ret;
2034
2035 ret = ftrace_arch_code_modify_prepare();
2036 FTRACE_WARN_ON(ret);
2037 if (ret)
2038 return;
2039 /*
2040 * Do not call function tracer while we update the code.
2041 * We are in stop machine.
2042 */
2043 function_trace_stop++;
2044
2045 /*
2046 * By default we use stop_machine() to modify the code.
2047 * But archs can do what ever they want as long as it
2048 * is safe. The stop_machine() is the safest, but also
2049 * produces the most overhead.
2050 */
2051 arch_ftrace_update_code(command);
2052
Steven Rostedt6331c282011-07-13 15:11:02 -04002053 function_trace_stop--;
2054
Steven Rostedt000ab692009-02-17 13:35:06 -05002055 ret = ftrace_arch_code_modify_post_process();
2056 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002057}
2058
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002059static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002060static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002061static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002062
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002063static void control_ops_free(struct ftrace_ops *ops)
2064{
2065 free_percpu(ops->disabled);
2066}
2067
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002068static void ftrace_startup_enable(int command)
2069{
2070 if (saved_ftrace_func != ftrace_trace_function) {
2071 saved_ftrace_func = ftrace_trace_function;
2072 command |= FTRACE_UPDATE_TRACE_FUNC;
2073 }
2074
2075 if (!command || !ftrace_enabled)
2076 return;
2077
2078 ftrace_run_update_code(command);
2079}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002080
Steven Rostedta1cd6172011-05-23 15:24:25 -04002081static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002082{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002083 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002084
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002085 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002086 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002087
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002088 ret = __register_ftrace_function(ops);
2089 if (ret)
2090 return ret;
2091
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002092 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002093 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002094
Steven Rostedted926f92011-05-03 13:25:24 -04002095 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002096
2097 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002098
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002099 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002100
2101 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002102}
2103
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002104static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002105{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002106 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002107
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002108 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002109 return -ENODEV;
2110
2111 ret = __unregister_ftrace_function(ops);
2112 if (ret)
2113 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002114
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002115 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002116 /*
2117 * Just warn in case of unbalance, no need to kill ftrace, it's not
2118 * critical but the ftrace_call callers may be never nopped again after
2119 * further ftrace uses.
2120 */
2121 WARN_ON_ONCE(ftrace_start_up < 0);
2122
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002123 ftrace_hash_rec_disable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002124
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05002125 if (!global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002126 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002127
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002128 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002129
2130 if (saved_ftrace_func != ftrace_trace_function) {
2131 saved_ftrace_func = ftrace_trace_function;
2132 command |= FTRACE_UPDATE_TRACE_FUNC;
2133 }
2134
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002135 if (!command || !ftrace_enabled) {
2136 /*
2137 * If these are control ops, they still need their
2138 * per_cpu field freed. Since, function tracing is
2139 * not currently active, we can just free them
2140 * without synchronizing all CPUs.
2141 */
2142 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2143 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002144 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002145 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002146
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002147 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002148
2149 /*
2150 * Dynamic ops may be freed, we must make sure that all
2151 * callers are done before leaving this function.
2152 * The same goes for freeing the per_cpu data of the control
2153 * ops.
2154 *
2155 * Again, normal synchronize_sched() is not good enough.
2156 * We need to do a hard force of sched synchronization.
2157 * This is because we use preempt_disable() to do RCU, but
2158 * the function tracers can be called where RCU is not watching
2159 * (like before user_exit()). We can not rely on the RCU
2160 * infrastructure to do the synchronization, thus we must do it
2161 * ourselves.
2162 */
2163 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2164 schedule_on_each_cpu(ftrace_sync);
2165
2166 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2167 control_ops_free(ops);
2168 }
2169
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002170 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002171}
2172
Ingo Molnare309b412008-05-12 21:20:51 +02002173static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002174{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002175 if (unlikely(ftrace_disabled))
2176 return;
2177
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002178 /* Force update next time */
2179 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002180 /* ftrace_start_up is true if we want ftrace running */
2181 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002182 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002183}
2184
Ingo Molnare309b412008-05-12 21:20:51 +02002185static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002186{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002187 if (unlikely(ftrace_disabled))
2188 return;
2189
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002190 /* ftrace_start_up is true if ftrace is running */
2191 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002192 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002193}
2194
Steven Rostedt3d083392008-05-12 21:20:42 +02002195static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002196unsigned long ftrace_update_tot_cnt;
2197
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002198static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002199{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002200 /*
2201 * Filter_hash being empty will default to trace module.
2202 * But notrace hash requires a test of individual module functions.
2203 */
2204 return ftrace_hash_empty(ops->filter_hash) &&
2205 ftrace_hash_empty(ops->notrace_hash);
2206}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002207
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002208/*
2209 * Check if the current ops references the record.
2210 *
2211 * If the ops traces all functions, then it was already accounted for.
2212 * If the ops does not trace the current record function, skip it.
2213 * If the ops ignores the function via notrace filter, skip it.
2214 */
2215static inline bool
2216ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2217{
2218 /* If ops isn't enabled, ignore it */
2219 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2220 return 0;
2221
2222 /* If ops traces all mods, we already accounted for it */
2223 if (ops_traces_mod(ops))
2224 return 0;
2225
2226 /* The function must be in the filter */
2227 if (!ftrace_hash_empty(ops->filter_hash) &&
2228 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2229 return 0;
2230
2231 /* If in notrace hash, we ignore it too */
2232 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2233 return 0;
2234
2235 return 1;
2236}
2237
2238static int referenced_filters(struct dyn_ftrace *rec)
2239{
2240 struct ftrace_ops *ops;
2241 int cnt = 0;
2242
2243 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2244 if (ops_references_rec(ops, rec))
2245 cnt++;
2246 }
2247
2248 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002249}
2250
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002251static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002252{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002253 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002254 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302255 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002256 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002257 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002258 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002259 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002260
2261 /*
2262 * When adding a module, we need to check if tracers are
2263 * currently enabled and if they are set to trace all functions.
2264 * If they are, we need to enable the module functions as well
2265 * as update the reference counts for those function records.
2266 */
2267 if (mod) {
2268 struct ftrace_ops *ops;
2269
2270 for (ops = ftrace_ops_list;
2271 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002272 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2273 if (ops_traces_mod(ops))
2274 ref++;
2275 else
2276 test = true;
2277 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002278 }
2279 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002280
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002281 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002282
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002283 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302284
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002285 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002286 int cnt = ref;
2287
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002288 /* If something went wrong, bail without enabling anything */
2289 if (unlikely(ftrace_disabled))
2290 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002291
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002292 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002293 if (test)
2294 cnt += referenced_filters(p);
2295 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302296
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002297 /*
2298 * Do the initial record conversion from mcount jump
2299 * to the NOP instructions.
2300 */
2301 if (!ftrace_code_disable(mod, p))
2302 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002303
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002304 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002305
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002306 /*
2307 * If the tracing is enabled, go ahead and enable the record.
2308 *
2309 * The reason not to enable the record immediatelly is the
2310 * inherent check of ftrace_make_nop/ftrace_make_call for
2311 * correct previous instructions. Making first the NOP
2312 * conversion puts the module to the correct state, thus
2313 * passing the ftrace_make_call check.
2314 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002315 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002316 int failed = __ftrace_replace_code(p, 1);
2317 if (failed)
2318 ftrace_bug(failed, p->ip);
2319 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002320 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002321 }
2322
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002323 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002324 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002325 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002326
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002327 return 0;
2328}
2329
Steven Rostedta7900872011-12-16 16:23:44 -05002330static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002331{
Steven Rostedta7900872011-12-16 16:23:44 -05002332 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002333 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002334
Steven Rostedta7900872011-12-16 16:23:44 -05002335 if (WARN_ON(!count))
2336 return -EINVAL;
2337
2338 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002339
2340 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002341 * We want to fill as much as possible. No more than a page
2342 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002343 */
Steven Rostedta7900872011-12-16 16:23:44 -05002344 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2345 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002346
Steven Rostedta7900872011-12-16 16:23:44 -05002347 again:
2348 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2349
2350 if (!pg->records) {
2351 /* if we can't allocate this size, try something smaller */
2352 if (!order)
2353 return -ENOMEM;
2354 order >>= 1;
2355 goto again;
2356 }
2357
2358 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2359 pg->size = cnt;
2360
2361 if (cnt > count)
2362 cnt = count;
2363
2364 return cnt;
2365}
2366
2367static struct ftrace_page *
2368ftrace_allocate_pages(unsigned long num_to_init)
2369{
2370 struct ftrace_page *start_pg;
2371 struct ftrace_page *pg;
2372 int order;
2373 int cnt;
2374
2375 if (!num_to_init)
2376 return 0;
2377
2378 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2379 if (!pg)
2380 return NULL;
2381
2382 /*
2383 * Try to allocate as much as possible in one continues
2384 * location that fills in all of the space. We want to
2385 * waste as little space as possible.
2386 */
2387 for (;;) {
2388 cnt = ftrace_allocate_records(pg, num_to_init);
2389 if (cnt < 0)
2390 goto free_pages;
2391
2392 num_to_init -= cnt;
2393 if (!num_to_init)
2394 break;
2395
2396 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2397 if (!pg->next)
2398 goto free_pages;
2399
2400 pg = pg->next;
2401 }
2402
2403 return start_pg;
2404
2405 free_pages:
2406 while (start_pg) {
2407 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2408 free_pages((unsigned long)pg->records, order);
2409 start_pg = pg->next;
2410 kfree(pg);
2411 pg = start_pg;
2412 }
2413 pr_info("ftrace: FAILED to allocate memory for functions\n");
2414 return NULL;
2415}
2416
Steven Rostedt5072c592008-05-12 21:20:43 +02002417#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2418
2419struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002420 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002421 loff_t func_pos;
2422 struct ftrace_page *pg;
2423 struct dyn_ftrace *func;
2424 struct ftrace_func_probe *probe;
2425 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002426 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002427 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002428 int hidx;
2429 int idx;
2430 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002431};
2432
Ingo Molnare309b412008-05-12 21:20:51 +02002433static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002434t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002435{
2436 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002437 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002438 struct hlist_head *hhd;
2439
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002440 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002441 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002442
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002443 if (iter->probe)
2444 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002445 retry:
2446 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2447 return NULL;
2448
2449 hhd = &ftrace_func_hash[iter->hidx];
2450
2451 if (hlist_empty(hhd)) {
2452 iter->hidx++;
2453 hnd = NULL;
2454 goto retry;
2455 }
2456
2457 if (!hnd)
2458 hnd = hhd->first;
2459 else {
2460 hnd = hnd->next;
2461 if (!hnd) {
2462 iter->hidx++;
2463 goto retry;
2464 }
2465 }
2466
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002467 if (WARN_ON_ONCE(!hnd))
2468 return NULL;
2469
2470 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2471
2472 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002473}
2474
2475static void *t_hash_start(struct seq_file *m, loff_t *pos)
2476{
2477 struct ftrace_iterator *iter = m->private;
2478 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002479 loff_t l;
2480
Steven Rostedt69a30832011-12-19 15:21:16 -05002481 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2482 return NULL;
2483
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002484 if (iter->func_pos > *pos)
2485 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002486
Li Zefand82d6242009-06-24 09:54:54 +08002487 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002488 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002489 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002490 if (!p)
2491 break;
2492 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002493 if (!p)
2494 return NULL;
2495
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002496 /* Only set this if we have an item */
2497 iter->flags |= FTRACE_ITER_HASH;
2498
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002499 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002500}
2501
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002502static int
2503t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002504{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002505 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002506
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002507 rec = iter->probe;
2508 if (WARN_ON_ONCE(!rec))
2509 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002510
Steven Rostedt809dcf22009-02-16 23:06:01 -05002511 if (rec->ops->print)
2512 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2513
Steven Rostedtb375a112009-09-17 00:05:58 -04002514 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002515
2516 if (rec->data)
2517 seq_printf(m, ":%p", rec->data);
2518 seq_putc(m, '\n');
2519
2520 return 0;
2521}
2522
2523static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002524t_next(struct seq_file *m, void *v, loff_t *pos)
2525{
2526 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002527 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002528 struct dyn_ftrace *rec = NULL;
2529
Steven Rostedt45a4a232011-04-21 23:16:46 -04002530 if (unlikely(ftrace_disabled))
2531 return NULL;
2532
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002533 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002534 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002535
Steven Rostedt5072c592008-05-12 21:20:43 +02002536 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002537 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002538
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002539 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002540 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002541
Steven Rostedt5072c592008-05-12 21:20:43 +02002542 retry:
2543 if (iter->idx >= iter->pg->index) {
2544 if (iter->pg->next) {
2545 iter->pg = iter->pg->next;
2546 iter->idx = 0;
2547 goto retry;
2548 }
2549 } else {
2550 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002551 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002552 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002553
Steven Rostedt41c52c02008-05-22 11:46:33 -04002554 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002555 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2556
2557 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002558 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002559
Steven Rostedt5072c592008-05-12 21:20:43 +02002560 rec = NULL;
2561 goto retry;
2562 }
2563 }
2564
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002565 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002566 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002567
2568 iter->func = rec;
2569
2570 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002571}
2572
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002573static void reset_iter_read(struct ftrace_iterator *iter)
2574{
2575 iter->pos = 0;
2576 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002577 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002578}
2579
2580static void *t_start(struct seq_file *m, loff_t *pos)
2581{
2582 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002583 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002584 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002585 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002586
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002587 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002588
2589 if (unlikely(ftrace_disabled))
2590 return NULL;
2591
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002592 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002593 * If an lseek was done, then reset and start from beginning.
2594 */
2595 if (*pos < iter->pos)
2596 reset_iter_read(iter);
2597
2598 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002599 * For set_ftrace_filter reading, if we have the filter
2600 * off, we can short cut and just print out that all
2601 * functions are enabled.
2602 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002603 if (iter->flags & FTRACE_ITER_FILTER &&
2604 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002605 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002606 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002607 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002608 /* reset in case of seek/pread */
2609 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002610 return iter;
2611 }
2612
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002613 if (iter->flags & FTRACE_ITER_HASH)
2614 return t_hash_start(m, pos);
2615
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002616 /*
2617 * Unfortunately, we need to restart at ftrace_pages_start
2618 * every time we let go of the ftrace_mutex. This is because
2619 * those pointers can change without the lock.
2620 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002621 iter->pg = ftrace_pages_start;
2622 iter->idx = 0;
2623 for (l = 0; l <= *pos; ) {
2624 p = t_next(m, p, &l);
2625 if (!p)
2626 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002627 }
walimis5821e1b2008-11-15 15:19:06 +08002628
Steven Rostedt69a30832011-12-19 15:21:16 -05002629 if (!p)
2630 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002631
2632 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002633}
2634
2635static void t_stop(struct seq_file *m, void *p)
2636{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002637 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002638}
2639
2640static int t_show(struct seq_file *m, void *v)
2641{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002642 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002643 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002644
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002645 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002646 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002647
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002648 if (iter->flags & FTRACE_ITER_PRINTALL) {
2649 seq_printf(m, "#### all functions enabled ####\n");
2650 return 0;
2651 }
2652
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002653 rec = iter->func;
2654
Steven Rostedt5072c592008-05-12 21:20:43 +02002655 if (!rec)
2656 return 0;
2657
Steven Rostedt647bcd02011-05-03 14:39:21 -04002658 seq_printf(m, "%ps", (void *)rec->ip);
2659 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002660 seq_printf(m, " (%ld)%s",
2661 rec->flags & ~FTRACE_FL_MASK,
2662 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002663 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002664
2665 return 0;
2666}
2667
James Morris88e9d342009-09-22 16:43:43 -07002668static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002669 .start = t_start,
2670 .next = t_next,
2671 .stop = t_stop,
2672 .show = t_show,
2673};
2674
Ingo Molnare309b412008-05-12 21:20:51 +02002675static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002676ftrace_avail_open(struct inode *inode, struct file *file)
2677{
2678 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002679
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002680 if (unlikely(ftrace_disabled))
2681 return -ENODEV;
2682
Jiri Olsa50e18b92012-04-25 10:23:39 +02002683 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2684 if (iter) {
2685 iter->pg = ftrace_pages_start;
2686 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002687 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002688
Jiri Olsa50e18b92012-04-25 10:23:39 +02002689 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002690}
2691
Steven Rostedt647bcd02011-05-03 14:39:21 -04002692static int
2693ftrace_enabled_open(struct inode *inode, struct file *file)
2694{
2695 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002696
2697 if (unlikely(ftrace_disabled))
2698 return -ENODEV;
2699
Jiri Olsa50e18b92012-04-25 10:23:39 +02002700 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2701 if (iter) {
2702 iter->pg = ftrace_pages_start;
2703 iter->flags = FTRACE_ITER_ENABLED;
2704 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002705 }
2706
Jiri Olsa50e18b92012-04-25 10:23:39 +02002707 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002708}
2709
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002710static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002711{
Steven Rostedt52baf112009-02-14 01:15:39 -05002712 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002713 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002714 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002715}
2716
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002717/**
2718 * ftrace_regex_open - initialize function tracer filter files
2719 * @ops: The ftrace_ops that hold the hash filters
2720 * @flag: The type of filter to process
2721 * @inode: The inode, usually passed in to your open routine
2722 * @file: The file, usually passed in to your open routine
2723 *
2724 * ftrace_regex_open() initializes the filter files for the
2725 * @ops. Depending on @flag it may process the filter hash or
2726 * the notrace hash of @ops. With this called from the open
2727 * routine, you can use ftrace_filter_write() for the write
2728 * routine if @flag has FTRACE_ITER_FILTER set, or
2729 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002730 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002731 * release must call ftrace_regex_release().
2732 */
2733int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002734ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002735 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002736{
2737 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002738 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002739 int ret = 0;
2740
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002741 ftrace_ops_init(ops);
2742
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002743 if (unlikely(ftrace_disabled))
2744 return -ENODEV;
2745
Steven Rostedt5072c592008-05-12 21:20:43 +02002746 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2747 if (!iter)
2748 return -ENOMEM;
2749
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002750 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2751 kfree(iter);
2752 return -ENOMEM;
2753 }
2754
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002755 iter->ops = ops;
2756 iter->flags = flag;
2757
2758 mutex_lock(&ops->regex_lock);
2759
Steven Rostedtf45948e2011-05-02 12:29:25 -04002760 if (flag & FTRACE_ITER_NOTRACE)
2761 hash = ops->notrace_hash;
2762 else
2763 hash = ops->filter_hash;
2764
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002765 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002766 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002767 if (!iter->hash) {
2768 trace_parser_put(&iter->parser);
2769 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002770 ret = -ENOMEM;
2771 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002772 }
2773 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002774
Steven Rostedt5072c592008-05-12 21:20:43 +02002775 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002776 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002777 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002778
2779 if (file->f_mode & FMODE_READ) {
2780 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002781
2782 ret = seq_open(file, &show_ftrace_seq_ops);
2783 if (!ret) {
2784 struct seq_file *m = file->private_data;
2785 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002786 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002787 /* Failed */
2788 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002789 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002790 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002791 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002792 } else
2793 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002794
2795 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002796 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002797
2798 return ret;
2799}
2800
Steven Rostedt41c52c02008-05-22 11:46:33 -04002801static int
2802ftrace_filter_open(struct inode *inode, struct file *file)
2803{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002804 struct ftrace_ops *ops = inode->i_private;
2805
2806 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05002807 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2808 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002809}
2810
2811static int
2812ftrace_notrace_open(struct inode *inode, struct file *file)
2813{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05002814 struct ftrace_ops *ops = inode->i_private;
2815
2816 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002817 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002818}
2819
Steven Rostedt64e7c442009-02-13 17:08:48 -05002820static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002821{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002822 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002823 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002824
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002825 switch (type) {
2826 case MATCH_FULL:
2827 if (strcmp(str, regex) == 0)
2828 matched = 1;
2829 break;
2830 case MATCH_FRONT_ONLY:
2831 if (strncmp(str, regex, len) == 0)
2832 matched = 1;
2833 break;
2834 case MATCH_MIDDLE_ONLY:
2835 if (strstr(str, regex))
2836 matched = 1;
2837 break;
2838 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002839 slen = strlen(str);
2840 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002841 matched = 1;
2842 break;
2843 }
2844
2845 return matched;
2846}
2847
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002848static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002849enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002850{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002851 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002852 int ret = 0;
2853
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002854 entry = ftrace_lookup_ip(hash, rec->ip);
2855 if (not) {
2856 /* Do nothing if it doesn't exist */
2857 if (!entry)
2858 return 0;
2859
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002860 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002861 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002862 /* Do nothing if it exists */
2863 if (entry)
2864 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002865
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002866 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002867 }
2868 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002869}
2870
Steven Rostedt64e7c442009-02-13 17:08:48 -05002871static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002872ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2873 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002874{
2875 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002876 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002877
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002878 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2879
2880 if (mod) {
2881 /* module lookup requires matching the module */
2882 if (!modname || strcmp(modname, mod))
2883 return 0;
2884
2885 /* blank search means to match all funcs in the mod */
2886 if (!len)
2887 return 1;
2888 }
2889
Steven Rostedt64e7c442009-02-13 17:08:48 -05002890 return ftrace_match(str, regex, len, type);
2891}
2892
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002893static int
2894match_records(struct ftrace_hash *hash, char *buff,
2895 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002896{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002897 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002898 struct ftrace_page *pg;
2899 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002900 int type = MATCH_FULL;
2901 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002902 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002903 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002904
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002905 if (len) {
2906 type = filter_parse_regex(buff, len, &search, &not);
2907 search_len = strlen(search);
2908 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002909
Steven Rostedt52baf112009-02-14 01:15:39 -05002910 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002911
2912 if (unlikely(ftrace_disabled))
2913 goto out_unlock;
2914
Steven Rostedt265c8312009-02-13 12:43:56 -05002915 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002916 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002917 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002918 if (ret < 0) {
2919 found = ret;
2920 goto out_unlock;
2921 }
Li Zefan311d16d2009-12-08 11:15:11 +08002922 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002923 }
2924 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002925 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002926 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002927
2928 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002929}
2930
Steven Rostedt64e7c442009-02-13 17:08:48 -05002931static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002932ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002933{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002934 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002935}
2936
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002937static int
2938ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002939{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002940 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002941
Steven Rostedt64e7c442009-02-13 17:08:48 -05002942 /* blank or '*' mean the same */
2943 if (strcmp(buff, "*") == 0)
2944 buff[0] = 0;
2945
2946 /* handle the case of 'dont filter this module' */
2947 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2948 buff[0] = 0;
2949 not = 1;
2950 }
2951
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002952 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002953}
2954
Steven Rostedtf6180772009-02-14 00:40:25 -05002955/*
2956 * We register the module command as a template to show others how
2957 * to register the a command as well.
2958 */
2959
2960static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002961ftrace_mod_callback(struct ftrace_hash *hash,
2962 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002963{
2964 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002965 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002966
2967 /*
2968 * cmd == 'mod' because we only registered this func
2969 * for the 'mod' ftrace_func_command.
2970 * But if you register one func with multiple commands,
2971 * you can tell which command was used by the cmd
2972 * parameter.
2973 */
2974
2975 /* we must have a module name */
2976 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002977 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002978
2979 mod = strsep(&param, ":");
2980 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002981 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002982
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002983 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002984 if (!ret)
2985 ret = -EINVAL;
2986 if (ret < 0)
2987 return ret;
2988
2989 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002990}
2991
2992static struct ftrace_func_command ftrace_mod_cmd = {
2993 .name = "mod",
2994 .func = ftrace_mod_callback,
2995};
2996
2997static int __init ftrace_mod_cmd_init(void)
2998{
2999 return register_ftrace_command(&ftrace_mod_cmd);
3000}
Steven Rostedt6f415672012-10-05 12:13:07 -04003001core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003002
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003003static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003004 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003005{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003006 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003007 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003008 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003009
3010 key = hash_long(ip, FTRACE_HASH_BITS);
3011
3012 hhd = &ftrace_func_hash[key];
3013
3014 if (hlist_empty(hhd))
3015 return;
3016
3017 /*
3018 * Disable preemption for these calls to prevent a RCU grace
3019 * period. This syncs the hash iteration and freeing of items
3020 * on the hash. rcu_read_lock is too dangerous here.
3021 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003022 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003023 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003024 if (entry->ip == ip)
3025 entry->ops->func(ip, parent_ip, &entry->data);
3026 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003027 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003028}
3029
Steven Rostedtb6887d72009-02-17 12:32:04 -05003030static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003031{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003032 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003033 .flags = FTRACE_OPS_FL_INITIALIZED,
3034 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003035};
3036
Steven Rostedtb6887d72009-02-17 12:32:04 -05003037static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003038
Steven Rostedtb6887d72009-02-17 12:32:04 -05003039static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003040{
Steven Rostedtb8489142011-05-04 09:27:52 -04003041 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003042 int i;
3043
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003044 if (ftrace_probe_registered) {
3045 /* still need to update the function call sites */
3046 if (ftrace_enabled)
3047 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003048 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003049 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003050
3051 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3052 struct hlist_head *hhd = &ftrace_func_hash[i];
3053 if (hhd->first)
3054 break;
3055 }
3056 /* Nothing registered? */
3057 if (i == FTRACE_FUNC_HASHSIZE)
3058 return;
3059
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003060 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003061
Steven Rostedtb6887d72009-02-17 12:32:04 -05003062 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003063}
3064
Steven Rostedtb6887d72009-02-17 12:32:04 -05003065static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003066{
3067 int i;
3068
Steven Rostedtb6887d72009-02-17 12:32:04 -05003069 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003070 return;
3071
3072 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3073 struct hlist_head *hhd = &ftrace_func_hash[i];
3074 if (hhd->first)
3075 return;
3076 }
3077
3078 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003079 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003080
Steven Rostedtb6887d72009-02-17 12:32:04 -05003081 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003082}
3083
3084
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003085static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003086{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003087 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003088 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089 kfree(entry);
3090}
3091
Steven Rostedt59df055f2009-02-14 15:29:06 -05003092int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003093register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003094 void *data)
3095{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003096 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003097 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3098 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003099 struct ftrace_page *pg;
3100 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003101 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003102 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003103 int count = 0;
3104 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003105 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003106
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003107 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003108 len = strlen(search);
3109
Steven Rostedtb6887d72009-02-17 12:32:04 -05003110 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003111 if (WARN_ON(not))
3112 return -EINVAL;
3113
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003114 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003115
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003116 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3117 if (!hash) {
3118 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003119 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003120 }
3121
3122 if (unlikely(ftrace_disabled)) {
3123 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003124 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003125 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003126
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003127 mutex_lock(&ftrace_lock);
3128
Steven Rostedt59df055f2009-02-14 15:29:06 -05003129 do_for_each_ftrace_rec(pg, rec) {
3130
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003131 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003132 continue;
3133
3134 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3135 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003136 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003137 if (!count)
3138 count = -ENOMEM;
3139 goto out_unlock;
3140 }
3141
3142 count++;
3143
3144 entry->data = data;
3145
3146 /*
3147 * The caller might want to do something special
3148 * for each function we find. We call the callback
3149 * to give the caller an opportunity to do so.
3150 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003151 if (ops->init) {
3152 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003153 /* caller does not like this func */
3154 kfree(entry);
3155 continue;
3156 }
3157 }
3158
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003159 ret = enter_record(hash, rec, 0);
3160 if (ret < 0) {
3161 kfree(entry);
3162 count = ret;
3163 goto out_unlock;
3164 }
3165
Steven Rostedt59df055f2009-02-14 15:29:06 -05003166 entry->ops = ops;
3167 entry->ip = rec->ip;
3168
3169 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3170 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3171
3172 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003173
3174 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3175 if (ret < 0)
3176 count = ret;
3177
Steven Rostedtb6887d72009-02-17 12:32:04 -05003178 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003179
3180 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003181 mutex_unlock(&ftrace_lock);
3182 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003183 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003184 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003185
3186 return count;
3187}
3188
3189enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003190 PROBE_TEST_FUNC = 1,
3191 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003192};
3193
3194static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003195__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003196 void *data, int flags)
3197{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003198 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003199 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003200 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003201 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003202 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003203 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003204 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003205 char str[KSYM_SYMBOL_LEN];
3206 int type = MATCH_FULL;
3207 int i, len = 0;
3208 char *search;
3209
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003210 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003211 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003212 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003213 int not;
3214
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003215 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003216 len = strlen(search);
3217
Steven Rostedtb6887d72009-02-17 12:32:04 -05003218 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003219 if (WARN_ON(not))
3220 return;
3221 }
3222
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003223 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003224
3225 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3226 if (!hash)
3227 /* Hmm, should report this somehow */
3228 goto out_unlock;
3229
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003230 INIT_LIST_HEAD(&free_list);
3231
Steven Rostedt59df055f2009-02-14 15:29:06 -05003232 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3233 struct hlist_head *hhd = &ftrace_func_hash[i];
3234
Sasha Levinb67bfe02013-02-27 17:06:00 -08003235 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003236
3237 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003238 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003239 continue;
3240
Steven Rostedtb6887d72009-02-17 12:32:04 -05003241 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003242 continue;
3243
3244 /* do this last, since it is the most expensive */
3245 if (glob) {
3246 kallsyms_lookup(entry->ip, NULL, NULL,
3247 NULL, str);
3248 if (!ftrace_match(str, glob, len, type))
3249 continue;
3250 }
3251
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003252 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3253 /* It is possible more than one entry had this ip */
3254 if (rec_entry)
3255 free_hash_entry(hash, rec_entry);
3256
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003257 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003258 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003259 }
3260 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003261 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003262 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003263 /*
3264 * Remove after the disable is called. Otherwise, if the last
3265 * probe is removed, a null hash means *all enabled*.
3266 */
3267 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003268 synchronize_sched();
3269 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3270 list_del(&entry->free_list);
3271 ftrace_free_entry(entry);
3272 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003273 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003274
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003275 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003276 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003277 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003278}
3279
3280void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003281unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003282 void *data)
3283{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003284 __unregister_ftrace_function_probe(glob, ops, data,
3285 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003286}
3287
3288void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003289unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003290{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003291 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003292}
3293
Steven Rostedtb6887d72009-02-17 12:32:04 -05003294void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003295{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003296 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003297}
3298
Steven Rostedtf6180772009-02-14 00:40:25 -05003299static LIST_HEAD(ftrace_commands);
3300static DEFINE_MUTEX(ftrace_cmd_mutex);
3301
Tom Zanussi38de93a2013-10-24 08:34:18 -05003302/*
3303 * Currently we only register ftrace commands from __init, so mark this
3304 * __init too.
3305 */
3306__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003307{
3308 struct ftrace_func_command *p;
3309 int ret = 0;
3310
3311 mutex_lock(&ftrace_cmd_mutex);
3312 list_for_each_entry(p, &ftrace_commands, list) {
3313 if (strcmp(cmd->name, p->name) == 0) {
3314 ret = -EBUSY;
3315 goto out_unlock;
3316 }
3317 }
3318 list_add(&cmd->list, &ftrace_commands);
3319 out_unlock:
3320 mutex_unlock(&ftrace_cmd_mutex);
3321
3322 return ret;
3323}
3324
Tom Zanussi38de93a2013-10-24 08:34:18 -05003325/*
3326 * Currently we only unregister ftrace commands from __init, so mark
3327 * this __init too.
3328 */
3329__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003330{
3331 struct ftrace_func_command *p, *n;
3332 int ret = -ENODEV;
3333
3334 mutex_lock(&ftrace_cmd_mutex);
3335 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3336 if (strcmp(cmd->name, p->name) == 0) {
3337 ret = 0;
3338 list_del_init(&p->list);
3339 goto out_unlock;
3340 }
3341 }
3342 out_unlock:
3343 mutex_unlock(&ftrace_cmd_mutex);
3344
3345 return ret;
3346}
3347
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003348static int ftrace_process_regex(struct ftrace_hash *hash,
3349 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003350{
Steven Rostedtf6180772009-02-14 00:40:25 -05003351 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003352 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003353 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003354
3355 func = strsep(&next, ":");
3356
3357 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003358 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003359 if (!ret)
3360 ret = -EINVAL;
3361 if (ret < 0)
3362 return ret;
3363 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003364 }
3365
Steven Rostedtf6180772009-02-14 00:40:25 -05003366 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003367
3368 command = strsep(&next, ":");
3369
Steven Rostedtf6180772009-02-14 00:40:25 -05003370 mutex_lock(&ftrace_cmd_mutex);
3371 list_for_each_entry(p, &ftrace_commands, list) {
3372 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003373 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003374 goto out_unlock;
3375 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003376 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003377 out_unlock:
3378 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003379
Steven Rostedtf6180772009-02-14 00:40:25 -05003380 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003381}
3382
Ingo Molnare309b412008-05-12 21:20:51 +02003383static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003384ftrace_regex_write(struct file *file, const char __user *ubuf,
3385 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003386{
3387 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003388 struct trace_parser *parser;
3389 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003390
Li Zefan4ba79782009-09-22 13:52:20 +08003391 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003392 return 0;
3393
Steven Rostedt5072c592008-05-12 21:20:43 +02003394 if (file->f_mode & FMODE_READ) {
3395 struct seq_file *m = file->private_data;
3396 iter = m->private;
3397 } else
3398 iter = file->private_data;
3399
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003400 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003401 return -ENODEV;
3402
3403 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003404
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003405 parser = &iter->parser;
3406 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003407
Li Zefan4ba79782009-09-22 13:52:20 +08003408 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003409 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003410 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003411 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003412 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003413 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003414 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003415 }
3416
Steven Rostedt5072c592008-05-12 21:20:43 +02003417 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003418 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003419 return ret;
3420}
3421
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003422ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003423ftrace_filter_write(struct file *file, const char __user *ubuf,
3424 size_t cnt, loff_t *ppos)
3425{
3426 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3427}
3428
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003429ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003430ftrace_notrace_write(struct file *file, const char __user *ubuf,
3431 size_t cnt, loff_t *ppos)
3432{
3433 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3434}
3435
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003436static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003437ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3438{
3439 struct ftrace_func_entry *entry;
3440
3441 if (!ftrace_location(ip))
3442 return -EINVAL;
3443
3444 if (remove) {
3445 entry = ftrace_lookup_ip(hash, ip);
3446 if (!entry)
3447 return -ENOENT;
3448 free_hash_entry(hash, entry);
3449 return 0;
3450 }
3451
3452 return add_hash_entry(hash, ip);
3453}
3454
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003455static void ftrace_ops_update_code(struct ftrace_ops *ops)
3456{
3457 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3458 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3459}
3460
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003461static int
3462ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3463 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003464{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003465 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003466 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003467 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003468
Steven Rostedt41c52c02008-05-22 11:46:33 -04003469 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003470 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003471
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003472 mutex_lock(&ops->regex_lock);
3473
Steven Rostedtf45948e2011-05-02 12:29:25 -04003474 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003475 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003476 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003477 orig_hash = &ops->notrace_hash;
3478
3479 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003480 if (!hash) {
3481 ret = -ENOMEM;
3482 goto out_regex_unlock;
3483 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003484
Steven Rostedt41c52c02008-05-22 11:46:33 -04003485 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003486 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003487 if (buf && !ftrace_match_records(hash, buf, len)) {
3488 ret = -EINVAL;
3489 goto out_regex_unlock;
3490 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003491 if (ip) {
3492 ret = ftrace_match_addr(hash, ip, remove);
3493 if (ret < 0)
3494 goto out_regex_unlock;
3495 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003496
3497 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003498 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003499 if (!ret)
3500 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003501
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003502 mutex_unlock(&ftrace_lock);
3503
Jiri Olsaac483c42012-01-02 10:04:14 +01003504 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003505 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003506
3507 free_ftrace_hash(hash);
3508 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003509}
3510
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003511static int
3512ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3513 int reset, int enable)
3514{
3515 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3516}
3517
3518/**
3519 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3520 * @ops - the ops to set the filter with
3521 * @ip - the address to add to or remove from the filter.
3522 * @remove - non zero to remove the ip from the filter
3523 * @reset - non zero to reset all filters before applying this filter.
3524 *
3525 * Filters denote which functions should be enabled when tracing is enabled
3526 * If @ip is NULL, it failes to update filter.
3527 */
3528int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3529 int remove, int reset)
3530{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003531 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003532 return ftrace_set_addr(ops, ip, remove, reset, 1);
3533}
3534EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3535
3536static int
3537ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3538 int reset, int enable)
3539{
3540 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3541}
3542
Steven Rostedt77a2b372008-05-12 21:20:45 +02003543/**
3544 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003545 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003546 * @buf - the string that holds the function filter text.
3547 * @len - the length of the string.
3548 * @reset - non zero to reset all filters before applying this filter.
3549 *
3550 * Filters denote which functions should be enabled when tracing is enabled.
3551 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3552 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003553int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003554 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003555{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003556 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003557 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003558}
Steven Rostedt936e0742011-05-05 22:54:01 -04003559EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003560
Steven Rostedt41c52c02008-05-22 11:46:33 -04003561/**
3562 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003563 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003564 * @buf - the string that holds the function notrace text.
3565 * @len - the length of the string.
3566 * @reset - non zero to reset all filters before applying this filter.
3567 *
3568 * Notrace Filters denote which functions should not be enabled when tracing
3569 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3570 * for tracing.
3571 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003572int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003573 int len, int reset)
3574{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003575 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003576 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003577}
3578EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3579/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003580 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003581 * @buf - the string that holds the function filter text.
3582 * @len - the length of the string.
3583 * @reset - non zero to reset all filters before applying this filter.
3584 *
3585 * Filters denote which functions should be enabled when tracing is enabled.
3586 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3587 */
3588void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3589{
3590 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3591}
3592EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3593
3594/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003595 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003596 * @buf - the string that holds the function notrace text.
3597 * @len - the length of the string.
3598 * @reset - non zero to reset all filters before applying this filter.
3599 *
3600 * Notrace Filters denote which functions should not be enabled when tracing
3601 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3602 * for tracing.
3603 */
3604void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003605{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003606 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003607}
Steven Rostedt936e0742011-05-05 22:54:01 -04003608EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003609
Steven Rostedt2af15d62009-05-28 13:37:24 -04003610/*
3611 * command line interface to allow users to set filters on boot up.
3612 */
3613#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3614static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3615static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3616
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003617/* Used by function selftest to not test if filter is set */
3618bool ftrace_filter_param __initdata;
3619
Steven Rostedt2af15d62009-05-28 13:37:24 -04003620static int __init set_ftrace_notrace(char *str)
3621{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003622 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003623 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003624 return 1;
3625}
3626__setup("ftrace_notrace=", set_ftrace_notrace);
3627
3628static int __init set_ftrace_filter(char *str)
3629{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003630 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003631 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003632 return 1;
3633}
3634__setup("ftrace_filter=", set_ftrace_filter);
3635
Stefan Assmann369bc182009-10-12 22:17:21 +02003636#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003637static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003638static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003639
Stefan Assmann369bc182009-10-12 22:17:21 +02003640static int __init set_graph_function(char *str)
3641{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003642 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003643 return 1;
3644}
3645__setup("ftrace_graph_filter=", set_graph_function);
3646
3647static void __init set_ftrace_early_graph(char *buf)
3648{
3649 int ret;
3650 char *func;
3651
3652 while (buf) {
3653 func = strsep(&buf, ",");
3654 /* we allow only one expression at a time */
3655 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003656 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003657 if (ret)
3658 printk(KERN_DEBUG "ftrace: function %s not "
3659 "traceable\n", func);
3660 }
3661}
3662#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3663
Steven Rostedt2a85a372011-12-19 21:57:44 -05003664void __init
3665ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003666{
3667 char *func;
3668
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003669 ftrace_ops_init(ops);
3670
Steven Rostedt2af15d62009-05-28 13:37:24 -04003671 while (buf) {
3672 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003673 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003674 }
3675}
3676
3677static void __init set_ftrace_early_filters(void)
3678{
3679 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003680 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003681 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003682 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003683#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3684 if (ftrace_graph_buf[0])
3685 set_ftrace_early_graph(ftrace_graph_buf);
3686#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003687}
3688
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003689int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003690{
3691 struct seq_file *m = (struct seq_file *)file->private_data;
3692 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003693 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003694 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003695 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003696 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003697
Steven Rostedt5072c592008-05-12 21:20:43 +02003698 if (file->f_mode & FMODE_READ) {
3699 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003700 seq_release(inode, file);
3701 } else
3702 iter = file->private_data;
3703
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003704 parser = &iter->parser;
3705 if (trace_parser_loaded(parser)) {
3706 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003707 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003708 }
3709
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003710 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003711
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003712 mutex_lock(&iter->ops->regex_lock);
3713
Steven Rostedt058e2972011-04-29 22:35:33 -04003714 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003715 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3716
3717 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003718 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003719 else
3720 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003721
Steven Rostedt058e2972011-04-29 22:35:33 -04003722 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003723 ret = ftrace_hash_move(iter->ops, filter_hash,
3724 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003725 if (!ret)
3726 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003727
Steven Rostedt058e2972011-04-29 22:35:33 -04003728 mutex_unlock(&ftrace_lock);
3729 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003730
3731 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003732 free_ftrace_hash(iter->hash);
3733 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003734
Steven Rostedt5072c592008-05-12 21:20:43 +02003735 return 0;
3736}
3737
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003738static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003739 .open = ftrace_avail_open,
3740 .read = seq_read,
3741 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003742 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003743};
3744
Steven Rostedt647bcd02011-05-03 14:39:21 -04003745static const struct file_operations ftrace_enabled_fops = {
3746 .open = ftrace_enabled_open,
3747 .read = seq_read,
3748 .llseek = seq_lseek,
3749 .release = seq_release_private,
3750};
3751
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003752static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003753 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003754 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003755 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003756 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003757 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003758};
3759
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003760static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003761 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003762 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003763 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003764 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003765 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003766};
3767
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003768#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3769
3770static DEFINE_MUTEX(graph_lock);
3771
3772int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003773int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003774unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003775unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003776
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003777struct ftrace_graph_data {
3778 unsigned long *table;
3779 size_t size;
3780 int *count;
3781 const struct seq_operations *seq_ops;
3782};
3783
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003784static void *
Li Zefan85951842009-06-24 09:54:00 +08003785__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003786{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003787 struct ftrace_graph_data *fgd = m->private;
3788
3789 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003790 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003791 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003792}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003793
Li Zefan85951842009-06-24 09:54:00 +08003794static void *
3795g_next(struct seq_file *m, void *v, loff_t *pos)
3796{
3797 (*pos)++;
3798 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003799}
3800
3801static void *g_start(struct seq_file *m, loff_t *pos)
3802{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003803 struct ftrace_graph_data *fgd = m->private;
3804
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003805 mutex_lock(&graph_lock);
3806
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003807 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003808 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003809 return (void *)1;
3810
Li Zefan85951842009-06-24 09:54:00 +08003811 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003812}
3813
3814static void g_stop(struct seq_file *m, void *p)
3815{
3816 mutex_unlock(&graph_lock);
3817}
3818
3819static int g_show(struct seq_file *m, void *v)
3820{
3821 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003822
3823 if (!ptr)
3824 return 0;
3825
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003826 if (ptr == (unsigned long *)1) {
3827 seq_printf(m, "#### all functions enabled ####\n");
3828 return 0;
3829 }
3830
Steven Rostedtb375a112009-09-17 00:05:58 -04003831 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003832
3833 return 0;
3834}
3835
James Morris88e9d342009-09-22 16:43:43 -07003836static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003837 .start = g_start,
3838 .next = g_next,
3839 .stop = g_stop,
3840 .show = g_show,
3841};
3842
3843static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003844__ftrace_graph_open(struct inode *inode, struct file *file,
3845 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003846{
3847 int ret = 0;
3848
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003849 mutex_lock(&graph_lock);
3850 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003851 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003852 *fgd->count = 0;
3853 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003854 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003855 mutex_unlock(&graph_lock);
3856
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003857 if (file->f_mode & FMODE_READ) {
3858 ret = seq_open(file, fgd->seq_ops);
3859 if (!ret) {
3860 struct seq_file *m = file->private_data;
3861 m->private = fgd;
3862 }
3863 } else
3864 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003865
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003866 return ret;
3867}
3868
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003869static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003870ftrace_graph_open(struct inode *inode, struct file *file)
3871{
3872 struct ftrace_graph_data *fgd;
3873
3874 if (unlikely(ftrace_disabled))
3875 return -ENODEV;
3876
3877 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3878 if (fgd == NULL)
3879 return -ENOMEM;
3880
3881 fgd->table = ftrace_graph_funcs;
3882 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3883 fgd->count = &ftrace_graph_count;
3884 fgd->seq_ops = &ftrace_graph_seq_ops;
3885
3886 return __ftrace_graph_open(inode, file, fgd);
3887}
3888
3889static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003890ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3891{
3892 struct ftrace_graph_data *fgd;
3893
3894 if (unlikely(ftrace_disabled))
3895 return -ENODEV;
3896
3897 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3898 if (fgd == NULL)
3899 return -ENOMEM;
3900
3901 fgd->table = ftrace_graph_notrace_funcs;
3902 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3903 fgd->count = &ftrace_graph_notrace_count;
3904 fgd->seq_ops = &ftrace_graph_seq_ops;
3905
3906 return __ftrace_graph_open(inode, file, fgd);
3907}
3908
3909static int
Li Zefan87827112009-07-23 11:29:11 +08003910ftrace_graph_release(struct inode *inode, struct file *file)
3911{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003912 if (file->f_mode & FMODE_READ) {
3913 struct seq_file *m = file->private_data;
3914
3915 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003916 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003917 } else {
3918 kfree(file->private_data);
3919 }
3920
Li Zefan87827112009-07-23 11:29:11 +08003921 return 0;
3922}
3923
3924static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003925ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003926{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003927 struct dyn_ftrace *rec;
3928 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003929 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003930 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003931 int type, not;
3932 char *search;
3933 bool exists;
3934 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003935
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003936 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003937 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003938 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003939 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003940
3941 search_len = strlen(search);
3942
Steven Rostedt52baf112009-02-14 01:15:39 -05003943 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003944
3945 if (unlikely(ftrace_disabled)) {
3946 mutex_unlock(&ftrace_lock);
3947 return -ENODEV;
3948 }
3949
Steven Rostedt265c8312009-02-13 12:43:56 -05003950 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003951
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003952 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003953 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003954 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003955 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003956 if (array[i] == rec->ip) {
3957 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003958 break;
3959 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003960 }
3961
3962 if (!not) {
3963 fail = 0;
3964 if (!exists) {
3965 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003966 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003967 goto out;
3968 }
3969 } else {
3970 if (exists) {
3971 array[i] = array[--(*idx)];
3972 array[*idx] = 0;
3973 fail = 0;
3974 }
3975 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003976 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003977 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003978out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003979 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003980
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003981 if (fail)
3982 return -EINVAL;
3983
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003984 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003985}
3986
3987static ssize_t
3988ftrace_graph_write(struct file *file, const char __user *ubuf,
3989 size_t cnt, loff_t *ppos)
3990{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003991 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09003992 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003993 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003994
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003995 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003996 return 0;
3997
Namhyung Kim6a101082013-10-14 17:24:25 +09003998 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
3999 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004000
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004001 read = trace_get_user(&parser, ubuf, cnt, ppos);
4002
Li Zefan4ba79782009-09-22 13:52:20 +08004003 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004004 parser.buffer[parser.idx] = 0;
4005
Namhyung Kim6a101082013-10-14 17:24:25 +09004006 mutex_lock(&graph_lock);
4007
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004008 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004009 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4010 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004011
4012 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004013 }
4014
Namhyung Kim6a101082013-10-14 17:24:25 +09004015 if (!ret)
4016 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004017
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004018 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004019
4020 return ret;
4021}
4022
4023static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004024 .open = ftrace_graph_open,
4025 .read = seq_read,
4026 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004027 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004028 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004029};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004030
4031static const struct file_operations ftrace_graph_notrace_fops = {
4032 .open = ftrace_graph_notrace_open,
4033 .read = seq_read,
4034 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004035 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004036 .release = ftrace_graph_release,
4037};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004038#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4039
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004040void ftrace_create_filter_files(struct ftrace_ops *ops,
4041 struct dentry *parent)
4042{
4043
4044 trace_create_file("set_ftrace_filter", 0644, parent,
4045 ops, &ftrace_filter_fops);
4046
4047 trace_create_file("set_ftrace_notrace", 0644, parent,
4048 ops, &ftrace_notrace_fops);
4049}
4050
4051/*
4052 * The name "destroy_filter_files" is really a misnomer. Although
4053 * in the future, it may actualy delete the files, but this is
4054 * really intended to make sure the ops passed in are disabled
4055 * and that when this function returns, the caller is free to
4056 * free the ops.
4057 *
4058 * The "destroy" name is only to match the "create" name that this
4059 * should be paired with.
4060 */
4061void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4062{
4063 mutex_lock(&ftrace_lock);
4064 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4065 ftrace_shutdown(ops, 0);
4066 ops->flags |= FTRACE_OPS_FL_DELETED;
4067 mutex_unlock(&ftrace_lock);
4068}
4069
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004070static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004071{
Steven Rostedt5072c592008-05-12 21:20:43 +02004072
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004073 trace_create_file("available_filter_functions", 0444,
4074 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004075
Steven Rostedt647bcd02011-05-03 14:39:21 -04004076 trace_create_file("enabled_functions", 0444,
4077 d_tracer, NULL, &ftrace_enabled_fops);
4078
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004079 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004080
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004081#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004082 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004083 NULL,
4084 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004085 trace_create_file("set_graph_notrace", 0444, d_tracer,
4086 NULL,
4087 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004088#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4089
Steven Rostedt5072c592008-05-12 21:20:43 +02004090 return 0;
4091}
4092
Steven Rostedt9fd49322012-04-24 22:32:06 -04004093static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004094{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004095 const unsigned long *ipa = a;
4096 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004097
Steven Rostedt9fd49322012-04-24 22:32:06 -04004098 if (*ipa > *ipb)
4099 return 1;
4100 if (*ipa < *ipb)
4101 return -1;
4102 return 0;
4103}
4104
4105static void ftrace_swap_ips(void *a, void *b, int size)
4106{
4107 unsigned long *ipa = a;
4108 unsigned long *ipb = b;
4109 unsigned long t;
4110
4111 t = *ipa;
4112 *ipa = *ipb;
4113 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004114}
4115
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004116static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004117 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004118 unsigned long *end)
4119{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004120 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004121 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004122 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004123 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004124 unsigned long *p;
4125 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004126 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004127 int ret = -ENOMEM;
4128
4129 count = end - start;
4130
4131 if (!count)
4132 return 0;
4133
Steven Rostedt9fd49322012-04-24 22:32:06 -04004134 sort(start, count, sizeof(*start),
4135 ftrace_cmp_ips, ftrace_swap_ips);
4136
Steven Rostedt706c81f2012-04-24 23:45:26 -04004137 start_pg = ftrace_allocate_pages(count);
4138 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004139 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004140
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004141 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004142
Steven Rostedt32082302011-12-16 14:42:37 -05004143 /*
4144 * Core and each module needs their own pages, as
4145 * modules will free them when they are removed.
4146 * Force a new page to be allocated for modules.
4147 */
Steven Rostedta7900872011-12-16 16:23:44 -05004148 if (!mod) {
4149 WARN_ON(ftrace_pages || ftrace_pages_start);
4150 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004151 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004152 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004153 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004154 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004155
Steven Rostedta7900872011-12-16 16:23:44 -05004156 if (WARN_ON(ftrace_pages->next)) {
4157 /* Hmm, we have free pages? */
4158 while (ftrace_pages->next)
4159 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004160 }
Steven Rostedta7900872011-12-16 16:23:44 -05004161
Steven Rostedt706c81f2012-04-24 23:45:26 -04004162 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004163 }
4164
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004165 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004166 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004167 while (p < end) {
4168 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004169 /*
4170 * Some architecture linkers will pad between
4171 * the different mcount_loc sections of different
4172 * object files to satisfy alignments.
4173 * Skip any NULL pointers.
4174 */
4175 if (!addr)
4176 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004177
4178 if (pg->index == pg->size) {
4179 /* We should have allocated enough */
4180 if (WARN_ON(!pg->next))
4181 break;
4182 pg = pg->next;
4183 }
4184
4185 rec = &pg->records[pg->index++];
4186 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004187 }
4188
Steven Rostedt706c81f2012-04-24 23:45:26 -04004189 /* We should have used all pages */
4190 WARN_ON(pg->next);
4191
4192 /* Assign the last page to ftrace_pages */
4193 ftrace_pages = pg;
4194
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004195 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004196 * We only need to disable interrupts on start up
4197 * because we are modifying code that an interrupt
4198 * may execute, and the modification is not atomic.
4199 * But for modules, nothing runs the code we modify
4200 * until we are finished with it, and there's no
4201 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004202 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004203 if (!mod)
4204 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004205 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004206 if (!mod)
4207 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004208 ret = 0;
4209 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004210 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004211
Steven Rostedta7900872011-12-16 16:23:44 -05004212 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004213}
4214
Steven Rostedt93eb6772009-04-15 13:24:06 -04004215#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004216
4217#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4218
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004219void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004220{
4221 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004222 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004223 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004224 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004225
Steven Rostedt93eb6772009-04-15 13:24:06 -04004226 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004227
4228 if (ftrace_disabled)
4229 goto out_unlock;
4230
Steven Rostedt32082302011-12-16 14:42:37 -05004231 /*
4232 * Each module has its own ftrace_pages, remove
4233 * them from the list.
4234 */
4235 last_pg = &ftrace_pages_start;
4236 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4237 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004238 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004239 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004240 * As core pages are first, the first
4241 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004242 */
Steven Rostedt32082302011-12-16 14:42:37 -05004243 if (WARN_ON(pg == ftrace_pages_start))
4244 goto out_unlock;
4245
4246 /* Check if we are deleting the last page */
4247 if (pg == ftrace_pages)
4248 ftrace_pages = next_to_ftrace_page(last_pg);
4249
4250 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004251 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4252 free_pages((unsigned long)pg->records, order);
4253 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004254 } else
4255 last_pg = &pg->next;
4256 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004257 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004258 mutex_unlock(&ftrace_lock);
4259}
4260
4261static void ftrace_init_module(struct module *mod,
4262 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004263{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004264 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004265 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004266 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004267}
4268
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004269static int ftrace_module_notify_enter(struct notifier_block *self,
4270 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004271{
4272 struct module *mod = data;
4273
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004274 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004275 ftrace_init_module(mod, mod->ftrace_callsites,
4276 mod->ftrace_callsites +
4277 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004278 return 0;
4279}
4280
4281static int ftrace_module_notify_exit(struct notifier_block *self,
4282 unsigned long val, void *data)
4283{
4284 struct module *mod = data;
4285
4286 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004287 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004288
4289 return 0;
4290}
4291#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004292static int ftrace_module_notify_enter(struct notifier_block *self,
4293 unsigned long val, void *data)
4294{
4295 return 0;
4296}
4297static int ftrace_module_notify_exit(struct notifier_block *self,
4298 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004299{
4300 return 0;
4301}
4302#endif /* CONFIG_MODULES */
4303
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004304struct notifier_block ftrace_module_enter_nb = {
4305 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004306 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004307};
4308
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004309struct notifier_block ftrace_module_exit_nb = {
4310 .notifier_call = ftrace_module_notify_exit,
4311 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4312};
4313
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004314void __init ftrace_init(void)
4315{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004316 extern unsigned long __start_mcount_loc[];
4317 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004318 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004319 int ret;
4320
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004321 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004322 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004323 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004324 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004325 goto failed;
4326
4327 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004328 if (!count) {
4329 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004330 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004331 }
4332
4333 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4334 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004335
4336 last_ftrace_enabled = ftrace_enabled = 1;
4337
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004338 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004339 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004340 __stop_mcount_loc);
4341
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004342 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004343 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004344 pr_warning("Failed to register trace ftrace module enter notifier\n");
4345
4346 ret = register_module_notifier(&ftrace_module_exit_nb);
4347 if (ret)
4348 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004349
Steven Rostedt2af15d62009-05-28 13:37:24 -04004350 set_ftrace_early_filters();
4351
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004352 return;
4353 failed:
4354 ftrace_disabled = 1;
4355}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004356
Steven Rostedt3d083392008-05-12 21:20:42 +02004357#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004358
Steven Rostedt2b499382011-05-03 22:49:52 -04004359static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004360 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004361 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4362 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004363};
4364
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004365static int __init ftrace_nodyn_init(void)
4366{
4367 ftrace_enabled = 1;
4368 return 0;
4369}
Steven Rostedt6f415672012-10-05 12:13:07 -04004370core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004371
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004372static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4373static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004374/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004375# define ftrace_startup(ops, command) \
4376 ({ \
4377 int ___ret = __register_ftrace_function(ops); \
4378 if (!___ret) \
4379 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4380 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004381 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004382# define ftrace_shutdown(ops, command) \
4383 ({ \
4384 int ___ret = __unregister_ftrace_function(ops); \
4385 if (!___ret) \
4386 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4387 ___ret; \
4388 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004389
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004390# define ftrace_startup_sysctl() do { } while (0)
4391# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004392
4393static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004394ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004395{
4396 return 1;
4397}
4398
Steven Rostedt3d083392008-05-12 21:20:42 +02004399#endif /* CONFIG_DYNAMIC_FTRACE */
4400
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004401__init void ftrace_init_global_array_ops(struct trace_array *tr)
4402{
4403 tr->ops = &global_ops;
4404 tr->ops->private = tr;
4405}
4406
4407void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4408{
4409 /* If we filter on pids, update to use the pid function */
4410 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4411 if (WARN_ON(tr->ops->func != ftrace_stub))
4412 printk("ftrace ops had %pS for function\n",
4413 tr->ops->func);
4414 /* Only the top level instance does pid tracing */
4415 if (!list_empty(&ftrace_pids)) {
4416 set_ftrace_pid_function(func);
4417 func = ftrace_pid_func;
4418 }
4419 }
4420 tr->ops->func = func;
4421 tr->ops->private = tr;
4422}
4423
4424void ftrace_reset_array_ops(struct trace_array *tr)
4425{
4426 tr->ops->func = ftrace_stub;
4427}
4428
Steven Rostedtb8489142011-05-04 09:27:52 -04004429static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004430ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004431 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004432{
Jiri Olsae2484912012-02-15 15:51:48 +01004433 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4434 return;
4435
4436 /*
4437 * Some of the ops may be dynamically allocated,
4438 * they must be freed after a synchronize_sched().
4439 */
4440 preempt_disable_notrace();
4441 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004442
4443 /*
4444 * Control funcs (perf) uses RCU. Only trace if
4445 * RCU is currently active.
4446 */
4447 if (!rcu_is_watching())
4448 goto out;
4449
Steven Rostedt0a016402012-11-02 17:03:03 -04004450 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004451 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4452 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004453 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004454 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004455 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004456 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004457 trace_recursion_clear(TRACE_CONTROL_BIT);
4458 preempt_enable_notrace();
4459}
4460
4461static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004462 .func = ftrace_ops_control_func,
4463 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4464 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004465};
4466
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004467static inline void
4468__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004469 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004470{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004471 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004472 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004473
Steven Rostedtccf36722012-06-05 09:44:25 -04004474 if (function_trace_stop)
4475 return;
4476
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004477 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4478 if (bit < 0)
4479 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004480
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004481 /*
4482 * Some of the ops may be dynamically allocated,
4483 * they must be freed after a synchronize_sched().
4484 */
4485 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004486 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004487 if (ftrace_ops_test(op, ip, regs)) {
4488 if (WARN_ON(!op->func)) {
4489 function_trace_stop = 1;
4490 printk("op=%p %pS\n", op, op);
4491 goto out;
4492 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004493 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004494 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004495 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004496out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004497 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004498 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004499}
4500
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004501/*
4502 * Some archs only support passing ip and parent_ip. Even though
4503 * the list function ignores the op parameter, we do not want any
4504 * C side effects, where a function is called without the caller
4505 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004506 * Archs are to support both the regs and ftrace_ops at the same time.
4507 * If they support ftrace_ops, it is assumed they support regs.
4508 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004509 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4510 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004511 * An architecture can pass partial regs with ftrace_ops and still
4512 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004513 */
4514#if ARCH_SUPPORTS_FTRACE_OPS
4515static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004516 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004517{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004518 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004519}
4520#else
4521static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4522{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004523 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004524}
4525#endif
4526
Steven Rostedte32d8952008-12-04 00:26:41 -05004527static void clear_ftrace_swapper(void)
4528{
4529 struct task_struct *p;
4530 int cpu;
4531
4532 get_online_cpus();
4533 for_each_online_cpu(cpu) {
4534 p = idle_task(cpu);
4535 clear_tsk_trace_trace(p);
4536 }
4537 put_online_cpus();
4538}
4539
4540static void set_ftrace_swapper(void)
4541{
4542 struct task_struct *p;
4543 int cpu;
4544
4545 get_online_cpus();
4546 for_each_online_cpu(cpu) {
4547 p = idle_task(cpu);
4548 set_tsk_trace_trace(p);
4549 }
4550 put_online_cpus();
4551}
4552
4553static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004554{
4555 struct task_struct *p;
4556
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004557 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004558 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004559 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004560 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004561 rcu_read_unlock();
4562
Steven Rostedte32d8952008-12-04 00:26:41 -05004563 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004564}
4565
Steven Rostedte32d8952008-12-04 00:26:41 -05004566static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004567{
4568 struct task_struct *p;
4569
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004570 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004571 do_each_pid_task(pid, PIDTYPE_PID, p) {
4572 set_tsk_trace_trace(p);
4573 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004574 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004575}
4576
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004577static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004578{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004579 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004580 clear_ftrace_swapper();
4581 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004582 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004583}
4584
4585static void set_ftrace_pid_task(struct pid *pid)
4586{
4587 if (pid == ftrace_swapper_pid)
4588 set_ftrace_swapper();
4589 else
4590 set_ftrace_pid(pid);
4591}
4592
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004593static int ftrace_pid_add(int p)
4594{
4595 struct pid *pid;
4596 struct ftrace_pid *fpid;
4597 int ret = -EINVAL;
4598
4599 mutex_lock(&ftrace_lock);
4600
4601 if (!p)
4602 pid = ftrace_swapper_pid;
4603 else
4604 pid = find_get_pid(p);
4605
4606 if (!pid)
4607 goto out;
4608
4609 ret = 0;
4610
4611 list_for_each_entry(fpid, &ftrace_pids, list)
4612 if (fpid->pid == pid)
4613 goto out_put;
4614
4615 ret = -ENOMEM;
4616
4617 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4618 if (!fpid)
4619 goto out_put;
4620
4621 list_add(&fpid->list, &ftrace_pids);
4622 fpid->pid = pid;
4623
4624 set_ftrace_pid_task(pid);
4625
4626 ftrace_update_pid_func();
4627 ftrace_startup_enable(0);
4628
4629 mutex_unlock(&ftrace_lock);
4630 return 0;
4631
4632out_put:
4633 if (pid != ftrace_swapper_pid)
4634 put_pid(pid);
4635
4636out:
4637 mutex_unlock(&ftrace_lock);
4638 return ret;
4639}
4640
4641static void ftrace_pid_reset(void)
4642{
4643 struct ftrace_pid *fpid, *safe;
4644
4645 mutex_lock(&ftrace_lock);
4646 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4647 struct pid *pid = fpid->pid;
4648
4649 clear_ftrace_pid_task(pid);
4650
4651 list_del(&fpid->list);
4652 kfree(fpid);
4653 }
4654
4655 ftrace_update_pid_func();
4656 ftrace_startup_enable(0);
4657
4658 mutex_unlock(&ftrace_lock);
4659}
4660
4661static void *fpid_start(struct seq_file *m, loff_t *pos)
4662{
4663 mutex_lock(&ftrace_lock);
4664
4665 if (list_empty(&ftrace_pids) && (!*pos))
4666 return (void *) 1;
4667
4668 return seq_list_start(&ftrace_pids, *pos);
4669}
4670
4671static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4672{
4673 if (v == (void *)1)
4674 return NULL;
4675
4676 return seq_list_next(v, &ftrace_pids, pos);
4677}
4678
4679static void fpid_stop(struct seq_file *m, void *p)
4680{
4681 mutex_unlock(&ftrace_lock);
4682}
4683
4684static int fpid_show(struct seq_file *m, void *v)
4685{
4686 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4687
4688 if (v == (void *)1) {
4689 seq_printf(m, "no pid\n");
4690 return 0;
4691 }
4692
4693 if (fpid->pid == ftrace_swapper_pid)
4694 seq_printf(m, "swapper tasks\n");
4695 else
4696 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4697
4698 return 0;
4699}
4700
4701static const struct seq_operations ftrace_pid_sops = {
4702 .start = fpid_start,
4703 .next = fpid_next,
4704 .stop = fpid_stop,
4705 .show = fpid_show,
4706};
4707
4708static int
4709ftrace_pid_open(struct inode *inode, struct file *file)
4710{
4711 int ret = 0;
4712
4713 if ((file->f_mode & FMODE_WRITE) &&
4714 (file->f_flags & O_TRUNC))
4715 ftrace_pid_reset();
4716
4717 if (file->f_mode & FMODE_READ)
4718 ret = seq_open(file, &ftrace_pid_sops);
4719
4720 return ret;
4721}
4722
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004723static ssize_t
4724ftrace_pid_write(struct file *filp, const char __user *ubuf,
4725 size_t cnt, loff_t *ppos)
4726{
Ingo Molnar457dc922009-11-23 11:03:28 +01004727 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004728 long val;
4729 int ret;
4730
4731 if (cnt >= sizeof(buf))
4732 return -EINVAL;
4733
4734 if (copy_from_user(&buf, ubuf, cnt))
4735 return -EFAULT;
4736
4737 buf[cnt] = 0;
4738
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004739 /*
4740 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4741 * to clean the filter quietly.
4742 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004743 tmp = strstrip(buf);
4744 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004745 return 1;
4746
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004747 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004748 if (ret < 0)
4749 return ret;
4750
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004751 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004752
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004753 return ret ? ret : cnt;
4754}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004755
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004756static int
4757ftrace_pid_release(struct inode *inode, struct file *file)
4758{
4759 if (file->f_mode & FMODE_READ)
4760 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004761
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004762 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004763}
4764
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004765static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004766 .open = ftrace_pid_open,
4767 .write = ftrace_pid_write,
4768 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004769 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004770 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004771};
4772
4773static __init int ftrace_init_debugfs(void)
4774{
4775 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004776
4777 d_tracer = tracing_init_dentry();
4778 if (!d_tracer)
4779 return 0;
4780
4781 ftrace_init_dyn_debugfs(d_tracer);
4782
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004783 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4784 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004785
4786 ftrace_profile_debugfs(d_tracer);
4787
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004788 return 0;
4789}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004790fs_initcall(ftrace_init_debugfs);
4791
Steven Rostedt3d083392008-05-12 21:20:42 +02004792/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004793 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004794 *
4795 * This function should be used by panic code. It stops ftrace
4796 * but in a not so nice way. If you need to simply kill ftrace
4797 * from a non-atomic section, use ftrace_kill.
4798 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004799void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004800{
4801 ftrace_disabled = 1;
4802 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004803 clear_ftrace_function();
4804}
4805
4806/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004807 * Test if ftrace is dead or not.
4808 */
4809int ftrace_is_dead(void)
4810{
4811 return ftrace_disabled;
4812}
4813
4814/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004815 * register_ftrace_function - register a function for profiling
4816 * @ops - ops structure that holds the function for profiling.
4817 *
4818 * Register a function to be called by all functions in the
4819 * kernel.
4820 *
4821 * Note: @ops->func and all the functions it calls must be labeled
4822 * with "notrace", otherwise it will go into a
4823 * recursive loop.
4824 */
4825int register_ftrace_function(struct ftrace_ops *ops)
4826{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004827 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004828
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004829 ftrace_ops_init(ops);
4830
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004831 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004832
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004833 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004834
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004835 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004836
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004837 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004838}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004839EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004840
4841/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004842 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004843 * @ops - ops structure that holds the function to unregister
4844 *
4845 * Unregister a function that was added to be called by ftrace profiling.
4846 */
4847int unregister_ftrace_function(struct ftrace_ops *ops)
4848{
4849 int ret;
4850
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004851 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004852 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004853 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004854
4855 return ret;
4856}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004857EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004858
Ingo Molnare309b412008-05-12 21:20:51 +02004859int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004860ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004861 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004862 loff_t *ppos)
4863{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004864 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004865
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004866 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004867
Steven Rostedt45a4a232011-04-21 23:16:46 -04004868 if (unlikely(ftrace_disabled))
4869 goto out;
4870
4871 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004872
Li Zefana32c7762009-06-26 16:55:51 +08004873 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004874 goto out;
4875
Li Zefana32c7762009-06-26 16:55:51 +08004876 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004877
4878 if (ftrace_enabled) {
4879
4880 ftrace_startup_sysctl();
4881
4882 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004883 if (ftrace_ops_list != &ftrace_list_end)
4884 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004885
4886 } else {
4887 /* stopping ftrace calls (just send to ftrace_stub) */
4888 ftrace_trace_function = ftrace_stub;
4889
4890 ftrace_shutdown_sysctl();
4891 }
4892
4893 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004894 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004895 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004896}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004897
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004898#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004899
Steven Rostedt597af812009-04-03 15:24:12 -04004900static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004901
Steven Rostedte49dc192008-12-02 23:50:05 -05004902int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4903{
4904 return 0;
4905}
4906
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004907/* The callbacks that hook a function */
4908trace_func_graph_ret_t ftrace_graph_return =
4909 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004910trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05004911static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004912
4913/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4914static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4915{
4916 int i;
4917 int ret = 0;
4918 unsigned long flags;
4919 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4920 struct task_struct *g, *t;
4921
4922 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4923 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4924 * sizeof(struct ftrace_ret_stack),
4925 GFP_KERNEL);
4926 if (!ret_stack_list[i]) {
4927 start = 0;
4928 end = i;
4929 ret = -ENOMEM;
4930 goto free;
4931 }
4932 }
4933
4934 read_lock_irqsave(&tasklist_lock, flags);
4935 do_each_thread(g, t) {
4936 if (start == end) {
4937 ret = -EAGAIN;
4938 goto unlock;
4939 }
4940
4941 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004942 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004943 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004944 t->curr_ret_stack = -1;
4945 /* Make sure the tasks see the -1 first: */
4946 smp_wmb();
4947 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004948 }
4949 } while_each_thread(g, t);
4950
4951unlock:
4952 read_unlock_irqrestore(&tasklist_lock, flags);
4953free:
4954 for (i = start; i < end; i++)
4955 kfree(ret_stack_list[i]);
4956 return ret;
4957}
4958
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004959static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004960ftrace_graph_probe_sched_switch(void *ignore,
4961 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004962{
4963 unsigned long long timestamp;
4964 int index;
4965
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004966 /*
4967 * Does the user want to count the time a function was asleep.
4968 * If so, do not update the time stamps.
4969 */
4970 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4971 return;
4972
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004973 timestamp = trace_clock_local();
4974
4975 prev->ftrace_timestamp = timestamp;
4976
4977 /* only process tasks that we timestamped */
4978 if (!next->ftrace_timestamp)
4979 return;
4980
4981 /*
4982 * Update all the counters in next to make up for the
4983 * time next was sleeping.
4984 */
4985 timestamp -= next->ftrace_timestamp;
4986
4987 for (index = next->curr_ret_stack; index >= 0; index--)
4988 next->ret_stack[index].calltime += timestamp;
4989}
4990
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004991/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004992static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004993{
4994 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004995 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004996
4997 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4998 sizeof(struct ftrace_ret_stack *),
4999 GFP_KERNEL);
5000
5001 if (!ret_stack_list)
5002 return -ENOMEM;
5003
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005004 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005005 for_each_online_cpu(cpu) {
5006 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005007 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005008 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005009
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005010 do {
5011 ret = alloc_retstack_tasklist(ret_stack_list);
5012 } while (ret == -EAGAIN);
5013
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005014 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005015 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005016 if (ret)
5017 pr_info("ftrace_graph: Couldn't activate tracepoint"
5018 " probe to kernel_sched_switch\n");
5019 }
5020
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005021 kfree(ret_stack_list);
5022 return ret;
5023}
5024
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005025/*
5026 * Hibernation protection.
5027 * The state of the current task is too much unstable during
5028 * suspend/restore to disk. We want to protect against that.
5029 */
5030static int
5031ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5032 void *unused)
5033{
5034 switch (state) {
5035 case PM_HIBERNATION_PREPARE:
5036 pause_graph_tracing();
5037 break;
5038
5039 case PM_POST_HIBERNATION:
5040 unpause_graph_tracing();
5041 break;
5042 }
5043 return NOTIFY_DONE;
5044}
5045
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005046static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5047{
5048 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5049 return 0;
5050 return __ftrace_graph_entry(trace);
5051}
5052
5053/*
5054 * The function graph tracer should only trace the functions defined
5055 * by set_ftrace_filter and set_ftrace_notrace. If another function
5056 * tracer ops is registered, the graph tracer requires testing the
5057 * function against the global ops, and not just trace any function
5058 * that any ftrace_ops registered.
5059 */
5060static void update_function_graph_func(void)
5061{
5062 if (ftrace_ops_list == &ftrace_list_end ||
5063 (ftrace_ops_list == &global_ops &&
5064 global_ops.next == &ftrace_list_end))
5065 ftrace_graph_entry = __ftrace_graph_entry;
5066 else
5067 ftrace_graph_entry = ftrace_graph_entry_test;
5068}
5069
Mathias Krause8275f692014-03-30 15:31:50 +02005070static struct notifier_block ftrace_suspend_notifier = {
5071 .notifier_call = ftrace_suspend_notifier_call,
5072};
5073
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005074int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5075 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005076{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005077 int ret = 0;
5078
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005079 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005080
Steven Rostedt05ce5812009-03-24 00:18:31 -04005081 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005082 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005083 ret = -EBUSY;
5084 goto out;
5085 }
5086
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005087 register_pm_notifier(&ftrace_suspend_notifier);
5088
Steven Rostedt597af812009-04-03 15:24:12 -04005089 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005090 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005091 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005092 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005093 goto out;
5094 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005095
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005096 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005097
5098 /*
5099 * Update the indirect function to the entryfunc, and the
5100 * function that gets called to the entry_test first. Then
5101 * call the update fgraph entry function to determine if
5102 * the entryfunc should be called directly or not.
5103 */
5104 __ftrace_graph_entry = entryfunc;
5105 ftrace_graph_entry = ftrace_graph_entry_test;
5106 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005107
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005108 /* Function graph doesn't use the .func field of global_ops */
5109 global_ops.flags |= FTRACE_OPS_FL_STUB;
5110
5111 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005112
5113out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005114 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005115 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005116}
5117
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005118void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005119{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005120 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005121
Steven Rostedt597af812009-04-03 15:24:12 -04005122 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005123 goto out;
5124
Steven Rostedt597af812009-04-03 15:24:12 -04005125 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005126 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005127 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005128 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)fd06a542014-05-01 23:05:31 -04005129 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5130 global_ops.flags &= ~FTRACE_OPS_FL_STUB;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005131 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005132 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005133
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005134 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005135 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005136}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005137
Steven Rostedt868baf02011-02-10 21:26:13 -05005138static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5139
5140static void
5141graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5142{
5143 atomic_set(&t->tracing_graph_pause, 0);
5144 atomic_set(&t->trace_overrun, 0);
5145 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005146 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005147 smp_wmb();
5148 t->ret_stack = ret_stack;
5149}
5150
5151/*
5152 * Allocate a return stack for the idle task. May be the first
5153 * time through, or it may be done by CPU hotplug online.
5154 */
5155void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5156{
5157 t->curr_ret_stack = -1;
5158 /*
5159 * The idle task has no parent, it either has its own
5160 * stack or no stack at all.
5161 */
5162 if (t->ret_stack)
5163 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5164
5165 if (ftrace_graph_active) {
5166 struct ftrace_ret_stack *ret_stack;
5167
5168 ret_stack = per_cpu(idle_ret_stack, cpu);
5169 if (!ret_stack) {
5170 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5171 * sizeof(struct ftrace_ret_stack),
5172 GFP_KERNEL);
5173 if (!ret_stack)
5174 return;
5175 per_cpu(idle_ret_stack, cpu) = ret_stack;
5176 }
5177 graph_init_task(t, ret_stack);
5178 }
5179}
5180
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005181/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005182void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005183{
Steven Rostedt84047e32009-06-02 16:51:55 -04005184 /* Make sure we do not use the parent ret_stack */
5185 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005186 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005187
Steven Rostedt597af812009-04-03 15:24:12 -04005188 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005189 struct ftrace_ret_stack *ret_stack;
5190
5191 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005192 * sizeof(struct ftrace_ret_stack),
5193 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005194 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005195 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005196 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005197 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005198}
5199
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005200void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005201{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005202 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5203
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005204 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005205 /* NULL must become visible to IRQs before we free it: */
5206 barrier();
5207
5208 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005209}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005210
5211void ftrace_graph_stop(void)
5212{
5213 ftrace_stop();
5214}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005215#endif