blob: eab3123a1fbe0323b2364e576e91c8452702a262 [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
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040068#define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040071#define ASSIGN_OPS_HASH(opsname, val) \
72 .func_hash = val, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090074#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040075#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040076#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090077#endif
78
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040079static struct ftrace_ops ftrace_list_end __read_mostly = {
80 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040081 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040082 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040083};
84
Steven Rostedt4eebcc82008-05-12 21:20:48 +020085/* ftrace_enabled is a method to turn ftrace on or off */
86int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020087static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020088
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040089/* Current function tracing op */
90struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050091/* What to set function_trace_op to */
92static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050093
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040094/* List for set_ftrace_pid's pids. */
95LIST_HEAD(ftrace_pids);
96struct ftrace_pid {
97 struct list_head list;
98 struct pid *pid;
99};
100
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200101/*
102 * ftrace_disabled is set when an anomaly is discovered.
103 * ftrace_disabled is much stronger than ftrace_enabled.
104 */
105static int ftrace_disabled __read_mostly;
106
Steven Rostedt52baf112009-02-14 01:15:39 -0500107static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200108
Jiri Olsae2484912012-02-15 15:51:48 +0100109static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400110static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500112ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400113static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100114static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -0400116static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
117 struct ftrace_ops *op, struct pt_regs *regs);
118
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400119#if ARCH_SUPPORTS_FTRACE_OPS
120static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400121 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122#else
123/* See comment below, where ftrace_ops_list_func is defined */
124static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
125#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
126#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400127
Steven Rostedt0a016402012-11-02 17:03:03 -0400128/*
129 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400130 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400131 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400132 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400133 * concurrent insertions into the ftrace_global_list.
134 *
135 * Silly Alpha and silly pointer-speculation compiler optimizations!
136 */
137#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 do
140
141/*
142 * Optimized for just a single item in the list (as that is the normal case).
143 */
144#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400145 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400146 unlikely((op) != &ftrace_list_end))
147
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900148static inline void ftrace_ops_init(struct ftrace_ops *ops)
149{
150#ifdef CONFIG_DYNAMIC_FTRACE
151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
155 }
156#endif
157}
158
Steven Rostedtea701f12012-07-20 13:08:05 -0400159/**
160 * ftrace_nr_registered_ops - return number of ops registered
161 *
162 * Returns the number of ftrace_ops registered and tracing functions
163 */
164int ftrace_nr_registered_ops(void)
165{
166 struct ftrace_ops *ops;
167 int cnt = 0;
168
169 mutex_lock(&ftrace_lock);
170
171 for (ops = ftrace_ops_list;
172 ops != &ftrace_list_end; ops = ops->next)
173 cnt++;
174
175 mutex_unlock(&ftrace_lock);
176
177 return cnt;
178}
179
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400180static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400181 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500182{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500183 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500184 return;
185
Steven Rostedta1e2e312011-08-09 12:50:46 -0400186 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500187}
188
189static void set_ftrace_pid_function(ftrace_func_t func)
190{
191 /* do not set ftrace_pid_function to itself! */
192 if (func != ftrace_pid_func)
193 ftrace_pid_function = func;
194}
195
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200197 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200202void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200203{
Steven Rostedt3d083392008-05-12 21:20:42 +0200204 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500205 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200206}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207
Jiri Olsae2484912012-02-15 15:51:48 +0100208static void control_ops_disable_all(struct ftrace_ops *ops)
209{
210 int cpu;
211
212 for_each_possible_cpu(cpu)
213 *per_cpu_ptr(ops->disabled, cpu) = 1;
214}
215
216static int control_ops_alloc(struct ftrace_ops *ops)
217{
218 int __percpu *disabled;
219
220 disabled = alloc_percpu(int);
221 if (!disabled)
222 return -ENOMEM;
223
224 ops->disabled = disabled;
225 control_ops_disable_all(ops);
226 return 0;
227}
228
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500229static void ftrace_sync(struct work_struct *work)
230{
231 /*
232 * This function is just a stub to implement a hard force
233 * of synchronize_sched(). This requires synchronizing
234 * tasks even in userspace and idle.
235 *
236 * Yes, function tracing is rude.
237 */
238}
239
240static void ftrace_sync_ipi(void *data)
241{
242 /* Probably not needed, but do it anyway */
243 smp_rmb();
244}
245
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500246#ifdef CONFIG_FUNCTION_GRAPH_TRACER
247static void update_function_graph_func(void);
248#else
249static inline void update_function_graph_func(void) { }
250#endif
251
Steven Rostedt2b499382011-05-03 22:49:52 -0400252static void update_ftrace_function(void)
253{
254 ftrace_func_t func;
255
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400256 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400257 * Prepare the ftrace_ops that the arch callback will use.
258 * If there's only one ftrace_ops registered, the ftrace_ops_list
259 * will point to the ops we want.
260 */
261 set_function_trace_op = ftrace_ops_list;
262
263 /* If there's no ftrace_ops registered, just call the stub function */
264 if (ftrace_ops_list == &ftrace_list_end) {
265 func = ftrace_stub;
266
267 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400268 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400269 * recursion safe and not dynamic and the arch supports passing ops,
270 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400271 */
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400272 } else if (ftrace_ops_list->next == &ftrace_list_end) {
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -0400273 func = ftrace_ops_get_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400274
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400275 } else {
276 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500277 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400278 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400279 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400280
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400281 update_function_graph_func();
282
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500283 /* If there's no change, then do nothing more here */
284 if (ftrace_trace_function == func)
285 return;
286
287 /*
288 * If we are using the list function, it doesn't care
289 * about the function_trace_ops.
290 */
291 if (func == ftrace_ops_list_func) {
292 ftrace_trace_function = func;
293 /*
294 * Don't even bother setting function_trace_ops,
295 * it would be racy to do so anyway.
296 */
297 return;
298 }
299
300#ifndef CONFIG_DYNAMIC_FTRACE
301 /*
302 * For static tracing, we need to be a bit more careful.
303 * The function change takes affect immediately. Thus,
304 * we need to coorditate the setting of the function_trace_ops
305 * with the setting of the ftrace_trace_function.
306 *
307 * Set the function to the list ops, which will call the
308 * function we want, albeit indirectly, but it handles the
309 * ftrace_ops and doesn't depend on function_trace_op.
310 */
311 ftrace_trace_function = ftrace_ops_list_func;
312 /*
313 * Make sure all CPUs see this. Yes this is slow, but static
314 * tracing is slow and nasty to have enabled.
315 */
316 schedule_on_each_cpu(ftrace_sync);
317 /* Now all cpus are using the list ops. */
318 function_trace_op = set_function_trace_op;
319 /* Make sure the function_trace_op is visible on all CPUs */
320 smp_wmb();
321 /* Nasty way to force a rmb on all cpus */
322 smp_call_function(ftrace_sync_ipi, NULL, 1);
323 /* OK, we are all set to update the ftrace_trace_function now! */
324#endif /* !CONFIG_DYNAMIC_FTRACE */
325
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400326 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400327}
328
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800329int using_ftrace_ops_list_func(void)
330{
331 return ftrace_trace_function == ftrace_ops_list_func;
332}
333
Steven Rostedt2b499382011-05-03 22:49:52 -0400334static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200335{
Steven Rostedt2b499382011-05-03 22:49:52 -0400336 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200337 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400338 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200339 * CPU might be walking that list. We need to make sure
340 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400341 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200342 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400343 rcu_assign_pointer(*list, ops);
344}
Steven Rostedt3d083392008-05-12 21:20:42 +0200345
Steven Rostedt2b499382011-05-03 22:49:52 -0400346static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
347{
348 struct ftrace_ops **p;
349
350 /*
351 * If we are removing the last function, then simply point
352 * to the ftrace_stub.
353 */
354 if (*list == ops && ops->next == &ftrace_list_end) {
355 *list = &ftrace_list_end;
356 return 0;
357 }
358
359 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
360 if (*p == ops)
361 break;
362
363 if (*p != ops)
364 return -1;
365
366 *p = (*p)->next;
367 return 0;
368}
369
Jiri Olsae2484912012-02-15 15:51:48 +0100370static void add_ftrace_list_ops(struct ftrace_ops **list,
371 struct ftrace_ops *main_ops,
372 struct ftrace_ops *ops)
373{
374 int first = *list == &ftrace_list_end;
375 add_ftrace_ops(list, ops);
376 if (first)
377 add_ftrace_ops(&ftrace_ops_list, main_ops);
378}
379
380static int remove_ftrace_list_ops(struct ftrace_ops **list,
381 struct ftrace_ops *main_ops,
382 struct ftrace_ops *ops)
383{
384 int ret = remove_ftrace_ops(list, ops);
385 if (!ret && *list == &ftrace_list_end)
386 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
387 return ret;
388}
389
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400390static void ftrace_update_trampoline(struct ftrace_ops *ops);
391
Steven Rostedt2b499382011-05-03 22:49:52 -0400392static int __register_ftrace_function(struct ftrace_ops *ops)
393{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500394 if (ops->flags & FTRACE_OPS_FL_DELETED)
395 return -EINVAL;
396
Steven Rostedtb8489142011-05-04 09:27:52 -0400397 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
398 return -EBUSY;
399
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900400#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400401 /*
402 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
403 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
404 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
405 */
406 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
407 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
408 return -EINVAL;
409
410 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
411 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
412#endif
413
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400414 if (!core_kernel_data((unsigned long)ops))
415 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
416
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500417 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100418 if (control_ops_alloc(ops))
419 return -ENOMEM;
420 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400421 } else
422 add_ftrace_ops(&ftrace_ops_list, ops);
423
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400424 ftrace_update_trampoline(ops);
425
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400426 if (ftrace_enabled)
427 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200428
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200429 return 0;
430}
431
Ingo Molnare309b412008-05-12 21:20:51 +0200432static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200433{
Steven Rostedt2b499382011-05-03 22:49:52 -0400434 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200435
Steven Rostedtb8489142011-05-04 09:27:52 -0400436 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
437 return -EBUSY;
438
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500439 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100440 ret = remove_ftrace_list_ops(&ftrace_control_list,
441 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400442 } else
443 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
444
Steven Rostedt2b499382011-05-03 22:49:52 -0400445 if (ret < 0)
446 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400447
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400448 if (ftrace_enabled)
449 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200450
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500451 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200452}
453
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500454static void ftrace_update_pid_func(void)
455{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400456 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500457 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900458 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500459
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400460 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500461}
462
Steven Rostedt493762f2009-03-23 17:12:36 -0400463#ifdef CONFIG_FUNCTION_PROFILER
464struct ftrace_profile {
465 struct hlist_node node;
466 unsigned long ip;
467 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400468#ifdef CONFIG_FUNCTION_GRAPH_TRACER
469 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400470 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400471#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400472};
473
474struct ftrace_profile_page {
475 struct ftrace_profile_page *next;
476 unsigned long index;
477 struct ftrace_profile records[];
478};
479
Steven Rostedtcafb1682009-03-24 20:50:39 -0400480struct ftrace_profile_stat {
481 atomic_t disabled;
482 struct hlist_head *hash;
483 struct ftrace_profile_page *pages;
484 struct ftrace_profile_page *start;
485 struct tracer_stat stat;
486};
487
Steven Rostedt493762f2009-03-23 17:12:36 -0400488#define PROFILE_RECORDS_SIZE \
489 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
490
491#define PROFILES_PER_PAGE \
492 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
493
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400494static int ftrace_profile_enabled __read_mostly;
495
496/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400497static DEFINE_MUTEX(ftrace_profile_lock);
498
Steven Rostedtcafb1682009-03-24 20:50:39 -0400499static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400500
Namhyung Kim20079eb2013-04-10 08:55:50 +0900501#define FTRACE_PROFILE_HASH_BITS 10
502#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400503
Steven Rostedt493762f2009-03-23 17:12:36 -0400504static void *
505function_stat_next(void *v, int idx)
506{
507 struct ftrace_profile *rec = v;
508 struct ftrace_profile_page *pg;
509
510 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
511
512 again:
Li Zefan0296e422009-06-26 11:15:37 +0800513 if (idx != 0)
514 rec++;
515
Steven Rostedt493762f2009-03-23 17:12:36 -0400516 if ((void *)rec >= (void *)&pg->records[pg->index]) {
517 pg = pg->next;
518 if (!pg)
519 return NULL;
520 rec = &pg->records[0];
521 if (!rec->counter)
522 goto again;
523 }
524
525 return rec;
526}
527
528static void *function_stat_start(struct tracer_stat *trace)
529{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400530 struct ftrace_profile_stat *stat =
531 container_of(trace, struct ftrace_profile_stat, stat);
532
533 if (!stat || !stat->start)
534 return NULL;
535
536 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400537}
538
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400539#ifdef CONFIG_FUNCTION_GRAPH_TRACER
540/* function graph compares on total time */
541static int function_stat_cmp(void *p1, void *p2)
542{
543 struct ftrace_profile *a = p1;
544 struct ftrace_profile *b = p2;
545
546 if (a->time < b->time)
547 return -1;
548 if (a->time > b->time)
549 return 1;
550 else
551 return 0;
552}
553#else
554/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400555static int function_stat_cmp(void *p1, void *p2)
556{
557 struct ftrace_profile *a = p1;
558 struct ftrace_profile *b = p2;
559
560 if (a->counter < b->counter)
561 return -1;
562 if (a->counter > b->counter)
563 return 1;
564 else
565 return 0;
566}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400567#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400568
569static int function_stat_headers(struct seq_file *m)
570{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400572 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400573 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400574 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400575 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400576#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400577 seq_printf(m, " Function Hit\n"
578 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400579#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400580 return 0;
581}
582
583static int function_stat_show(struct seq_file *m, void *v)
584{
585 struct ftrace_profile *rec = v;
586 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800587 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400588#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400589 static struct trace_seq s;
590 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400591 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400592#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800593 mutex_lock(&ftrace_profile_lock);
594
595 /* we raced with function_profile_reset() */
596 if (unlikely(rec->counter == 0)) {
597 ret = -EBUSY;
598 goto out;
599 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400600
601 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400602 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400603
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400604#ifdef CONFIG_FUNCTION_GRAPH_TRACER
605 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400606 avg = rec->time;
607 do_div(avg, rec->counter);
608
Chase Douglase330b3b2010-04-26 14:02:05 -0400609 /* Sample standard deviation (s^2) */
610 if (rec->counter <= 1)
611 stddev = 0;
612 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200613 /*
614 * Apply Welford's method:
615 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
616 */
617 stddev = rec->counter * rec->time_squared -
618 rec->time * rec->time;
619
Chase Douglase330b3b2010-04-26 14:02:05 -0400620 /*
621 * Divide only 1000 for ns^2 -> us^2 conversion.
622 * trace_print_graph_duration will divide 1000 again.
623 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200624 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400625 }
626
Steven Rostedt34886c82009-03-25 21:00:47 -0400627 trace_seq_init(&s);
628 trace_print_graph_duration(rec->time, &s);
629 trace_seq_puts(&s, " ");
630 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400631 trace_seq_puts(&s, " ");
632 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400633 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400634#endif
635 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800636out:
637 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400638
Li Zefan3aaba202010-08-23 16:50:12 +0800639 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400640}
641
Steven Rostedtcafb1682009-03-24 20:50:39 -0400642static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400643{
644 struct ftrace_profile_page *pg;
645
Steven Rostedtcafb1682009-03-24 20:50:39 -0400646 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400647
648 while (pg) {
649 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
650 pg->index = 0;
651 pg = pg->next;
652 }
653
Steven Rostedtcafb1682009-03-24 20:50:39 -0400654 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400655 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
656}
657
Steven Rostedtcafb1682009-03-24 20:50:39 -0400658int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400659{
660 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400661 int functions;
662 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400663 int i;
664
665 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400667 return 0;
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
670 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400671 return -ENOMEM;
672
Steven Rostedt318e0a72009-03-25 20:06:34 -0400673#ifdef CONFIG_DYNAMIC_FTRACE
674 functions = ftrace_update_tot_cnt;
675#else
676 /*
677 * We do not know the number of functions that exist because
678 * dynamic tracing is what counts them. With past experience
679 * we have around 20K functions. That should be more than enough.
680 * It is highly unlikely we will execute every function in
681 * the kernel.
682 */
683 functions = 20000;
684#endif
685
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400687
Steven Rostedt318e0a72009-03-25 20:06:34 -0400688 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
689
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900690 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400691 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400692 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400693 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400694 pg = pg->next;
695 }
696
697 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400698
699 out_free:
700 pg = stat->start;
701 while (pg) {
702 unsigned long tmp = (unsigned long)pg;
703
704 pg = pg->next;
705 free_page(tmp);
706 }
707
Steven Rostedt318e0a72009-03-25 20:06:34 -0400708 stat->pages = NULL;
709 stat->start = NULL;
710
711 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400712}
713
Steven Rostedtcafb1682009-03-24 20:50:39 -0400714static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400715{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400717 int size;
718
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719 stat = &per_cpu(ftrace_profile_stats, cpu);
720
721 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400722 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400723 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 return 0;
725 }
726
727 /*
728 * We are profiling all functions, but usually only a few thousand
729 * functions are hit. We'll make a hash of 1024 items.
730 */
731 size = FTRACE_PROFILE_HASH_SIZE;
732
Steven Rostedtcafb1682009-03-24 20:50:39 -0400733 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400734
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400736 return -ENOMEM;
737
Steven Rostedt318e0a72009-03-25 20:06:34 -0400738 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739 if (ftrace_profile_pages_init(stat) < 0) {
740 kfree(stat->hash);
741 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 return -ENOMEM;
743 }
744
745 return 0;
746}
747
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748static int ftrace_profile_init(void)
749{
750 int cpu;
751 int ret = 0;
752
Miao Xiec4602c12013-12-16 15:20:01 +0800753 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 ret = ftrace_profile_init_cpu(cpu);
755 if (ret)
756 break;
757 }
758
759 return ret;
760}
761
Steven Rostedt493762f2009-03-23 17:12:36 -0400762/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400763static struct ftrace_profile *
764ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400765{
766 struct ftrace_profile *rec;
767 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400768 unsigned long key;
769
Namhyung Kim20079eb2013-04-10 08:55:50 +0900770 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400772
773 if (hlist_empty(hhd))
774 return NULL;
775
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400776 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400777 if (rec->ip == ip)
778 return rec;
779 }
780
781 return NULL;
782}
783
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784static void ftrace_add_profile(struct ftrace_profile_stat *stat,
785 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400786{
787 unsigned long key;
788
Namhyung Kim20079eb2013-04-10 08:55:50 +0900789 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400791}
792
Steven Rostedt318e0a72009-03-25 20:06:34 -0400793/*
794 * The memory is already allocated, this simply finds a new record to use.
795 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400796static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400797ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400798{
799 struct ftrace_profile *rec = NULL;
800
Steven Rostedt318e0a72009-03-25 20:06:34 -0400801 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 goto out;
804
Steven Rostedt493762f2009-03-23 17:12:36 -0400805 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400806 * Try to find the function again since an NMI
807 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400808 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400810 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400812
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 if (stat->pages->index == PROFILES_PER_PAGE) {
814 if (!stat->pages->next)
815 goto out;
816 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400817 }
818
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400820 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400822
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400825
826 return rec;
827}
828
Steven Rostedt493762f2009-03-23 17:12:36 -0400829static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400830function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400831 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400832{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400833 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400834 struct ftrace_profile *rec;
835 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400836
837 if (!ftrace_profile_enabled)
838 return;
839
Steven Rostedt493762f2009-03-23 17:12:36 -0400840 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400841
Christoph Lameterbdffd892014-04-29 14:17:40 -0500842 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400843 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400844 goto out;
845
846 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400847 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400848 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400849 if (!rec)
850 goto out;
851 }
852
853 rec->counter++;
854 out:
855 local_irq_restore(flags);
856}
857
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400858#ifdef CONFIG_FUNCTION_GRAPH_TRACER
859static int profile_graph_entry(struct ftrace_graph_ent *trace)
860{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400861 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400862 return 1;
863}
864
865static void profile_graph_return(struct ftrace_graph_ret *trace)
866{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400867 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400868 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400869 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400870 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400871
872 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500873 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400874 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400875 goto out;
876
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400877 /* If the calltime was zero'd ignore it */
878 if (!trace->calltime)
879 goto out;
880
Steven Rostedta2a16d62009-03-24 23:17:58 -0400881 calltime = trace->rettime - trace->calltime;
882
883 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
884 int index;
885
886 index = trace->depth;
887
888 /* Append this call time to the parent time to subtract */
889 if (index)
890 current->ret_stack[index - 1].subtime += calltime;
891
892 if (current->ret_stack[index].subtime < calltime)
893 calltime -= current->ret_stack[index].subtime;
894 else
895 calltime = 0;
896 }
897
Steven Rostedtcafb1682009-03-24 20:50:39 -0400898 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400899 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400900 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400901 rec->time_squared += calltime * calltime;
902 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400903
Steven Rostedtcafb1682009-03-24 20:50:39 -0400904 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400905 local_irq_restore(flags);
906}
907
908static int register_ftrace_profiler(void)
909{
910 return register_ftrace_graph(&profile_graph_return,
911 &profile_graph_entry);
912}
913
914static void unregister_ftrace_profiler(void)
915{
916 unregister_ftrace_graph();
917}
918#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100919static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400920 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900921 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400922 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400923};
924
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400925static int register_ftrace_profiler(void)
926{
927 return register_ftrace_function(&ftrace_profile_ops);
928}
929
930static void unregister_ftrace_profiler(void)
931{
932 unregister_ftrace_function(&ftrace_profile_ops);
933}
934#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
935
Steven Rostedt493762f2009-03-23 17:12:36 -0400936static ssize_t
937ftrace_profile_write(struct file *filp, const char __user *ubuf,
938 size_t cnt, loff_t *ppos)
939{
940 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400941 int ret;
942
Peter Huewe22fe9b52011-06-07 21:58:27 +0200943 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
944 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400945 return ret;
946
947 val = !!val;
948
949 mutex_lock(&ftrace_profile_lock);
950 if (ftrace_profile_enabled ^ val) {
951 if (val) {
952 ret = ftrace_profile_init();
953 if (ret < 0) {
954 cnt = ret;
955 goto out;
956 }
957
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400958 ret = register_ftrace_profiler();
959 if (ret < 0) {
960 cnt = ret;
961 goto out;
962 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400963 ftrace_profile_enabled = 1;
964 } else {
965 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400966 /*
967 * unregister_ftrace_profiler calls stop_machine
968 * so this acts like an synchronize_sched.
969 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400970 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400971 }
972 }
973 out:
974 mutex_unlock(&ftrace_profile_lock);
975
Jiri Olsacf8517c2009-10-23 19:36:16 -0400976 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400977
978 return cnt;
979}
980
981static ssize_t
982ftrace_profile_read(struct file *filp, char __user *ubuf,
983 size_t cnt, loff_t *ppos)
984{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400985 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400986 int r;
987
988 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
989 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
990}
991
992static const struct file_operations ftrace_profile_fops = {
993 .open = tracing_open_generic,
994 .read = ftrace_profile_read,
995 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200996 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400997};
998
Steven Rostedtcafb1682009-03-24 20:50:39 -0400999/* used to initialize the real stat files */
1000static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001001 .name = "functions",
1002 .stat_start = function_stat_start,
1003 .stat_next = function_stat_next,
1004 .stat_cmp = function_stat_cmp,
1005 .stat_headers = function_stat_headers,
1006 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001007};
1008
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001009static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001010{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001011 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001012 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001013 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001014 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001015 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001016
Steven Rostedtcafb1682009-03-24 20:50:39 -04001017 for_each_possible_cpu(cpu) {
1018 stat = &per_cpu(ftrace_profile_stats, cpu);
1019
1020 /* allocate enough for function name + cpu number */
1021 name = kmalloc(32, GFP_KERNEL);
1022 if (!name) {
1023 /*
1024 * The files created are permanent, if something happens
1025 * we still do not free memory.
1026 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001027 WARN(1,
1028 "Could not allocate stat file for cpu %d\n",
1029 cpu);
1030 return;
1031 }
1032 stat->stat = function_stats;
1033 snprintf(name, 32, "function%d", cpu);
1034 stat->stat.name = name;
1035 ret = register_stat_tracer(&stat->stat);
1036 if (ret) {
1037 WARN(1,
1038 "Could not register function stat for cpu %d\n",
1039 cpu);
1040 kfree(name);
1041 return;
1042 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001043 }
1044
1045 entry = debugfs_create_file("function_profile_enabled", 0644,
1046 d_tracer, NULL, &ftrace_profile_fops);
1047 if (!entry)
1048 pr_warning("Could not create debugfs "
1049 "'function_profile_enabled' entry\n");
1050}
1051
1052#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001053static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001054{
1055}
1056#endif /* CONFIG_FUNCTION_PROFILER */
1057
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001058static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1059
Steven Rostedt3d083392008-05-12 21:20:42 +02001060#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001061
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001062static struct ftrace_ops *removed_ops;
1063
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001064/*
1065 * Set when doing a global update, like enabling all recs or disabling them.
1066 * It is not set when just updating a single ftrace_ops.
1067 */
1068static bool update_all_ops;
1069
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001070#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001071# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001072#endif
1073
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001074static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1075
Steven Rostedtb6887d72009-02-17 12:32:04 -05001076struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001077 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001078 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001079 unsigned long flags;
1080 unsigned long ip;
1081 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001082 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001083};
1084
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001085struct ftrace_func_entry {
1086 struct hlist_node hlist;
1087 unsigned long ip;
1088};
1089
1090struct ftrace_hash {
1091 unsigned long size_bits;
1092 struct hlist_head *buckets;
1093 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001094 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001095};
1096
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001097/*
1098 * We make these constant because no one should touch them,
1099 * but they are used as the default "empty hash", to avoid allocating
1100 * it all the time. These are in a read only section such that if
1101 * anyone does try to modify it, it will cause an exception.
1102 */
1103static const struct hlist_head empty_buckets[1];
1104static const struct ftrace_hash empty_hash = {
1105 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001106};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001107#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001108
Steven Rostedt2b499382011-05-03 22:49:52 -04001109static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001110 .func = ftrace_stub,
1111 .local_hash.notrace_hash = EMPTY_HASH,
1112 .local_hash.filter_hash = EMPTY_HASH,
1113 INIT_OPS_HASH(global_ops)
1114 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1115 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001116};
1117
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001118struct ftrace_page {
1119 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001120 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001121 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001122 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001123};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001124
Steven Rostedta7900872011-12-16 16:23:44 -05001125#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1126#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001127
1128/* estimate from running different kernels */
1129#define NR_TO_INIT 10000
1130
1131static struct ftrace_page *ftrace_pages_start;
1132static struct ftrace_page *ftrace_pages;
1133
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001134static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001135{
1136 return !hash || !hash->count;
1137}
1138
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001139static struct ftrace_func_entry *
1140ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1141{
1142 unsigned long key;
1143 struct ftrace_func_entry *entry;
1144 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001145
Steven Rostedt06a51d92011-12-19 19:07:36 -05001146 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001147 return NULL;
1148
1149 if (hash->size_bits > 0)
1150 key = hash_long(ip, hash->size_bits);
1151 else
1152 key = 0;
1153
1154 hhd = &hash->buckets[key];
1155
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001156 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001157 if (entry->ip == ip)
1158 return entry;
1159 }
1160 return NULL;
1161}
1162
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001163static void __add_hash_entry(struct ftrace_hash *hash,
1164 struct ftrace_func_entry *entry)
1165{
1166 struct hlist_head *hhd;
1167 unsigned long key;
1168
1169 if (hash->size_bits)
1170 key = hash_long(entry->ip, hash->size_bits);
1171 else
1172 key = 0;
1173
1174 hhd = &hash->buckets[key];
1175 hlist_add_head(&entry->hlist, hhd);
1176 hash->count++;
1177}
1178
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001179static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1180{
1181 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001182
1183 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1184 if (!entry)
1185 return -ENOMEM;
1186
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001187 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001188 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001189
1190 return 0;
1191}
1192
1193static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001194free_hash_entry(struct ftrace_hash *hash,
1195 struct ftrace_func_entry *entry)
1196{
1197 hlist_del(&entry->hlist);
1198 kfree(entry);
1199 hash->count--;
1200}
1201
1202static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001203remove_hash_entry(struct ftrace_hash *hash,
1204 struct ftrace_func_entry *entry)
1205{
1206 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001207 hash->count--;
1208}
1209
1210static void ftrace_hash_clear(struct ftrace_hash *hash)
1211{
1212 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001213 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001214 struct ftrace_func_entry *entry;
1215 int size = 1 << hash->size_bits;
1216 int i;
1217
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001218 if (!hash->count)
1219 return;
1220
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001221 for (i = 0; i < size; i++) {
1222 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001223 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001224 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001225 }
1226 FTRACE_WARN_ON(hash->count);
1227}
1228
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001229static void free_ftrace_hash(struct ftrace_hash *hash)
1230{
1231 if (!hash || hash == EMPTY_HASH)
1232 return;
1233 ftrace_hash_clear(hash);
1234 kfree(hash->buckets);
1235 kfree(hash);
1236}
1237
Steven Rostedt07fd5512011-05-05 18:03:47 -04001238static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1239{
1240 struct ftrace_hash *hash;
1241
1242 hash = container_of(rcu, struct ftrace_hash, rcu);
1243 free_ftrace_hash(hash);
1244}
1245
1246static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1247{
1248 if (!hash || hash == EMPTY_HASH)
1249 return;
1250 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1251}
1252
Jiri Olsa5500fa52012-02-15 15:51:54 +01001253void ftrace_free_filter(struct ftrace_ops *ops)
1254{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001255 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001256 free_ftrace_hash(ops->func_hash->filter_hash);
1257 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001258}
1259
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001260static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1261{
1262 struct ftrace_hash *hash;
1263 int size;
1264
1265 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1266 if (!hash)
1267 return NULL;
1268
1269 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001270 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001271
1272 if (!hash->buckets) {
1273 kfree(hash);
1274 return NULL;
1275 }
1276
1277 hash->size_bits = size_bits;
1278
1279 return hash;
1280}
1281
1282static struct ftrace_hash *
1283alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1284{
1285 struct ftrace_func_entry *entry;
1286 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001287 int size;
1288 int ret;
1289 int i;
1290
1291 new_hash = alloc_ftrace_hash(size_bits);
1292 if (!new_hash)
1293 return NULL;
1294
1295 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001296 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001297 return new_hash;
1298
1299 size = 1 << hash->size_bits;
1300 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001301 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302 ret = add_hash_entry(new_hash, entry->ip);
1303 if (ret < 0)
1304 goto free_hash;
1305 }
1306 }
1307
1308 FTRACE_WARN_ON(new_hash->count != hash->count);
1309
1310 return new_hash;
1311
1312 free_hash:
1313 free_ftrace_hash(new_hash);
1314 return NULL;
1315}
1316
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001317static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001318ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001319static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001320ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001321
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001322static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001323ftrace_hash_move(struct ftrace_ops *ops, int enable,
1324 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001325{
1326 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001327 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001329 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001330 int size = src->count;
1331 int bits = 0;
1332 int i;
1333
1334 /*
1335 * If the new source is empty, just free dst and assign it
1336 * the empty_hash.
1337 */
1338 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001339 new_hash = EMPTY_HASH;
1340 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001341 }
1342
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001343 /*
1344 * Make the hash size about 1/2 the # found
1345 */
1346 for (size /= 2; size; size >>= 1)
1347 bits++;
1348
1349 /* Don't allocate too much */
1350 if (bits > FTRACE_HASH_MAX_BITS)
1351 bits = FTRACE_HASH_MAX_BITS;
1352
Steven Rostedt07fd5512011-05-05 18:03:47 -04001353 new_hash = alloc_ftrace_hash(bits);
1354 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001355 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001356
1357 size = 1 << src->size_bits;
1358 for (i = 0; i < size; i++) {
1359 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001360 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001361 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001362 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001363 }
1364 }
1365
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001366update:
1367 /*
1368 * Remove the current set, update the hash and add
1369 * them back.
1370 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001371 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001372
Steven Rostedt07fd5512011-05-05 18:03:47 -04001373 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001374
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001375 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001376
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001377 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001378}
1379
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001380static bool hash_contains_ip(unsigned long ip,
1381 struct ftrace_ops_hash *hash)
1382{
1383 /*
1384 * The function record is a match if it exists in the filter
1385 * hash and not in the notrace hash. Note, an emty hash is
1386 * considered a match for the filter hash, but an empty
1387 * notrace hash is considered not in the notrace hash.
1388 */
1389 return (ftrace_hash_empty(hash->filter_hash) ||
1390 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1391 (ftrace_hash_empty(hash->notrace_hash) ||
1392 !ftrace_lookup_ip(hash->notrace_hash, ip));
1393}
1394
Steven Rostedt265c8312009-02-13 12:43:56 -05001395/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001396 * Test the hashes for this ops to see if we want to call
1397 * the ops->func or not.
1398 *
1399 * It's a match if the ip is in the ops->filter_hash or
1400 * the filter_hash does not exist or is empty,
1401 * AND
1402 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001403 *
1404 * This needs to be called with preemption disabled as
1405 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001406 */
1407static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001408ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001409{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001410 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001411 int ret;
1412
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001413#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1414 /*
1415 * There's a small race when adding ops that the ftrace handler
1416 * that wants regs, may be called without them. We can not
1417 * allow that handler to be called if regs is NULL.
1418 */
1419 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1420 return 0;
1421#endif
1422
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001423 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1424 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001425
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001426 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001427 ret = 1;
1428 else
1429 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001430
1431 return ret;
1432}
1433
1434/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001435 * This is a double for. Do not use 'break' to break out of the loop,
1436 * you must use a goto.
1437 */
1438#define do_for_each_ftrace_rec(pg, rec) \
1439 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1440 int _____i; \
1441 for (_____i = 0; _____i < pg->index; _____i++) { \
1442 rec = &pg->records[_____i];
1443
1444#define while_for_each_ftrace_rec() \
1445 } \
1446 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301447
Steven Rostedt5855fea2011-12-16 19:27:42 -05001448
1449static int ftrace_cmp_recs(const void *a, const void *b)
1450{
Steven Rostedta650e022012-04-25 13:48:13 -04001451 const struct dyn_ftrace *key = a;
1452 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001453
Steven Rostedta650e022012-04-25 13:48:13 -04001454 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001455 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001456 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1457 return 1;
1458 return 0;
1459}
1460
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001461static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001462{
1463 struct ftrace_page *pg;
1464 struct dyn_ftrace *rec;
1465 struct dyn_ftrace key;
1466
1467 key.ip = start;
1468 key.flags = end; /* overload flags, as it is unsigned long */
1469
1470 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1471 if (end < pg->records[0].ip ||
1472 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1473 continue;
1474 rec = bsearch(&key, pg->records, pg->index,
1475 sizeof(struct dyn_ftrace),
1476 ftrace_cmp_recs);
1477 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001478 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001479 }
1480
Steven Rostedt5855fea2011-12-16 19:27:42 -05001481 return 0;
1482}
1483
Steven Rostedtc88fd862011-08-16 09:53:39 -04001484/**
1485 * ftrace_location - return true if the ip giving is a traced location
1486 * @ip: the instruction pointer to check
1487 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001488 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001489 * That is, the instruction that is either a NOP or call to
1490 * the function tracer. It checks the ftrace internal tables to
1491 * determine if the address belongs or not.
1492 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001493unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001494{
Steven Rostedta650e022012-04-25 13:48:13 -04001495 return ftrace_location_range(ip, ip);
1496}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001497
Steven Rostedta650e022012-04-25 13:48:13 -04001498/**
1499 * ftrace_text_reserved - return true if range contains an ftrace location
1500 * @start: start of range to search
1501 * @end: end of range to search (inclusive). @end points to the last byte to check.
1502 *
1503 * Returns 1 if @start and @end contains a ftrace location.
1504 * That is, the instruction that is either a NOP or call to
1505 * the function tracer. It checks the ftrace internal tables to
1506 * determine if the address belongs or not.
1507 */
Sasha Levind88471c2013-01-09 18:09:20 -05001508int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001509{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001510 unsigned long ret;
1511
1512 ret = ftrace_location_range((unsigned long)start,
1513 (unsigned long)end);
1514
1515 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001516}
1517
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001518/* Test if ops registered to this rec needs regs */
1519static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1520{
1521 struct ftrace_ops *ops;
1522 bool keep_regs = false;
1523
1524 for (ops = ftrace_ops_list;
1525 ops != &ftrace_list_end; ops = ops->next) {
1526 /* pass rec in as regs to have non-NULL val */
1527 if (ftrace_ops_test(ops, rec->ip, rec)) {
1528 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1529 keep_regs = true;
1530 break;
1531 }
1532 }
1533 }
1534
1535 return keep_regs;
1536}
1537
Steven Rostedted926f92011-05-03 13:25:24 -04001538static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1539 int filter_hash,
1540 bool inc)
1541{
1542 struct ftrace_hash *hash;
1543 struct ftrace_hash *other_hash;
1544 struct ftrace_page *pg;
1545 struct dyn_ftrace *rec;
1546 int count = 0;
1547 int all = 0;
1548
1549 /* Only update if the ops has been registered */
1550 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1551 return;
1552
1553 /*
1554 * In the filter_hash case:
1555 * If the count is zero, we update all records.
1556 * Otherwise we just update the items in the hash.
1557 *
1558 * In the notrace_hash case:
1559 * We enable the update in the hash.
1560 * As disabling notrace means enabling the tracing,
1561 * and enabling notrace means disabling, the inc variable
1562 * gets inversed.
1563 */
1564 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001565 hash = ops->func_hash->filter_hash;
1566 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001567 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001568 all = 1;
1569 } else {
1570 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001571 hash = ops->func_hash->notrace_hash;
1572 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001573 /*
1574 * If the notrace hash has no items,
1575 * then there's nothing to do.
1576 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001577 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001578 return;
1579 }
1580
1581 do_for_each_ftrace_rec(pg, rec) {
1582 int in_other_hash = 0;
1583 int in_hash = 0;
1584 int match = 0;
1585
1586 if (all) {
1587 /*
1588 * Only the filter_hash affects all records.
1589 * Update if the record is not in the notrace hash.
1590 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001591 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001592 match = 1;
1593 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001594 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1595 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001596
1597 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001598 * If filter_hash is set, we want to match all functions
1599 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001600 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001601 * If filter_hash is not set, then we are decrementing.
1602 * That means we match anything that is in the hash
1603 * and also in the other_hash. That is, we need to turn
1604 * off functions in the other hash because they are disabled
1605 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001606 */
1607 if (filter_hash && in_hash && !in_other_hash)
1608 match = 1;
1609 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001610 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001611 match = 1;
1612 }
1613 if (!match)
1614 continue;
1615
1616 if (inc) {
1617 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001618 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001619 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001620
1621 /*
1622 * If there's only a single callback registered to a
1623 * function, and the ops has a trampoline registered
1624 * for it, then we can call it directly.
1625 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001626 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001627 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001628 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001629 /*
1630 * If we are adding another function callback
1631 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001632 * custom trampoline in use, then we need to go
1633 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001634 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001635 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001636
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001637 /*
1638 * If any ops wants regs saved for this function
1639 * then all ops will get saved regs.
1640 */
1641 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1642 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001643 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001644 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001645 return;
1646 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001647
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001648 /*
1649 * If the rec had REGS enabled and the ops that is
1650 * being removed had REGS set, then see if there is
1651 * still any ops for this record that wants regs.
1652 * If not, we can stop recording them.
1653 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001654 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001655 rec->flags & FTRACE_FL_REGS &&
1656 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1657 if (!test_rec_ops_needs_regs(rec))
1658 rec->flags &= ~FTRACE_FL_REGS;
1659 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001660
1661 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001662 * If the rec had TRAMP enabled, then it needs to
1663 * be cleared. As TRAMP can only be enabled iff
1664 * there is only a single ops attached to it.
1665 * In otherwords, always disable it on decrementing.
1666 * In the future, we may set it if rec count is
1667 * decremented to one, and the ops that is left
1668 * has a trampoline.
1669 */
1670 rec->flags &= ~FTRACE_FL_TRAMP;
1671
1672 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001673 * flags will be cleared in ftrace_check_record()
1674 * if rec count is zero.
1675 */
Steven Rostedted926f92011-05-03 13:25:24 -04001676 }
1677 count++;
1678 /* Shortcut, if we handled all records, we are done. */
1679 if (!all && count == hash->count)
1680 return;
1681 } while_for_each_ftrace_rec();
1682}
1683
1684static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1685 int filter_hash)
1686{
1687 __ftrace_hash_rec_update(ops, filter_hash, 0);
1688}
1689
1690static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1691 int filter_hash)
1692{
1693 __ftrace_hash_rec_update(ops, filter_hash, 1);
1694}
1695
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001696static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1697 int filter_hash, int inc)
1698{
1699 struct ftrace_ops *op;
1700
1701 __ftrace_hash_rec_update(ops, filter_hash, inc);
1702
1703 if (ops->func_hash != &global_ops.local_hash)
1704 return;
1705
1706 /*
1707 * If the ops shares the global_ops hash, then we need to update
1708 * all ops that are enabled and use this hash.
1709 */
1710 do_for_each_ftrace_op(op, ftrace_ops_list) {
1711 /* Already done */
1712 if (op == ops)
1713 continue;
1714 if (op->func_hash == &global_ops.local_hash)
1715 __ftrace_hash_rec_update(op, filter_hash, inc);
1716 } while_for_each_ftrace_op(op);
1717}
1718
1719static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1720 int filter_hash)
1721{
1722 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1723}
1724
1725static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1726 int filter_hash)
1727{
1728 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1729}
1730
Steven Rostedt05736a42008-09-22 14:55:47 -07001731static void print_ip_ins(const char *fmt, unsigned char *p)
1732{
1733 int i;
1734
1735 printk(KERN_CONT "%s", fmt);
1736
1737 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1738 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1739}
1740
Steven Rostedtc88fd862011-08-16 09:53:39 -04001741/**
1742 * ftrace_bug - report and shutdown function tracer
1743 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1744 * @ip: The address that failed
1745 *
1746 * The arch code that enables or disables the function tracing
1747 * can call ftrace_bug() when it has detected a problem in
1748 * modifying the code. @failed should be one of either:
1749 * EFAULT - if the problem happens on reading the @ip address
1750 * EINVAL - if what is read at @ip is not what was expected
1751 * EPERM - if the problem happens on writting to the @ip address
1752 */
1753void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001754{
1755 switch (failed) {
1756 case -EFAULT:
1757 FTRACE_WARN_ON_ONCE(1);
1758 pr_info("ftrace faulted on modifying ");
1759 print_ip_sym(ip);
1760 break;
1761 case -EINVAL:
1762 FTRACE_WARN_ON_ONCE(1);
1763 pr_info("ftrace failed to modify ");
1764 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001765 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001766 printk(KERN_CONT "\n");
1767 break;
1768 case -EPERM:
1769 FTRACE_WARN_ON_ONCE(1);
1770 pr_info("ftrace faulted on writing ");
1771 print_ip_sym(ip);
1772 break;
1773 default:
1774 FTRACE_WARN_ON_ONCE(1);
1775 pr_info("ftrace faulted on unknown error ");
1776 print_ip_sym(ip);
1777 }
1778}
1779
Steven Rostedtc88fd862011-08-16 09:53:39 -04001780static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001781{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001782 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001783
Steven Rostedt982c3502008-11-15 16:31:41 -05001784 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001785 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001786 *
Steven Rostedted926f92011-05-03 13:25:24 -04001787 * If the record has a ref count, then we need to enable it
1788 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001789 *
Steven Rostedted926f92011-05-03 13:25:24 -04001790 * Otherwise we make sure its disabled.
1791 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001792 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001793 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001794 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001795 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001796 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001797
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001798 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001799 * If enabling and the REGS flag does not match the REGS_EN, or
1800 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1801 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001802 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001803 if (flag) {
1804 if (!(rec->flags & FTRACE_FL_REGS) !=
1805 !(rec->flags & FTRACE_FL_REGS_EN))
1806 flag |= FTRACE_FL_REGS;
1807
1808 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1809 !(rec->flags & FTRACE_FL_TRAMP_EN))
1810 flag |= FTRACE_FL_TRAMP;
1811 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001812
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001813 /* If the state of this record hasn't changed, then do nothing */
1814 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001815 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001816
1817 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001818 /* Save off if rec is being enabled (for return value) */
1819 flag ^= rec->flags & FTRACE_FL_ENABLED;
1820
1821 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001822 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001823 if (flag & FTRACE_FL_REGS) {
1824 if (rec->flags & FTRACE_FL_REGS)
1825 rec->flags |= FTRACE_FL_REGS_EN;
1826 else
1827 rec->flags &= ~FTRACE_FL_REGS_EN;
1828 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001829 if (flag & FTRACE_FL_TRAMP) {
1830 if (rec->flags & FTRACE_FL_TRAMP)
1831 rec->flags |= FTRACE_FL_TRAMP_EN;
1832 else
1833 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1834 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001835 }
1836
1837 /*
1838 * If this record is being updated from a nop, then
1839 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001840 * Otherwise,
1841 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001842 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001843 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001844 */
1845 if (flag & FTRACE_FL_ENABLED)
1846 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001847
1848 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001849 }
1850
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001851 if (update) {
1852 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001853 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001854 rec->flags = 0;
1855 else
1856 /* Just disable the record (keep REGS state) */
1857 rec->flags &= ~FTRACE_FL_ENABLED;
1858 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001859
1860 return FTRACE_UPDATE_MAKE_NOP;
1861}
1862
1863/**
1864 * ftrace_update_record, set a record that now is tracing or not
1865 * @rec: the record to update
1866 * @enable: set to 1 if the record is tracing, zero to force disable
1867 *
1868 * The records that represent all functions that can be traced need
1869 * to be updated when tracing has been enabled.
1870 */
1871int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1872{
1873 return ftrace_check_record(rec, enable, 1);
1874}
1875
1876/**
1877 * ftrace_test_record, check if the record has been enabled or not
1878 * @rec: the record to test
1879 * @enable: set to 1 to check if enabled, 0 if it is disabled
1880 *
1881 * The arch code may need to test if a record is already set to
1882 * tracing to determine how to modify the function code that it
1883 * represents.
1884 */
1885int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1886{
1887 return ftrace_check_record(rec, enable, 0);
1888}
1889
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001890static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001891ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
1892{
1893 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001894 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001895
1896 do_for_each_ftrace_op(op, ftrace_ops_list) {
1897
1898 if (!op->trampoline)
1899 continue;
1900
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001901 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001902 return op;
1903 } while_for_each_ftrace_op(op);
1904
1905 return NULL;
1906}
1907
1908static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001909ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1910{
1911 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001912 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001913
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001914 /*
1915 * Need to check removed ops first.
1916 * If they are being removed, and this rec has a tramp,
1917 * and this rec is in the ops list, then it would be the
1918 * one with the tramp.
1919 */
1920 if (removed_ops) {
1921 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001922 return removed_ops;
1923 }
1924
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001925 /*
1926 * Need to find the current trampoline for a rec.
1927 * Now, a trampoline is only attached to a rec if there
1928 * was a single 'ops' attached to it. But this can be called
1929 * when we are adding another op to the rec or removing the
1930 * current one. Thus, if the op is being added, we can
1931 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04001932 * yet.
1933 *
1934 * If an ops is being modified (hooking to different functions)
1935 * then we don't care about the new functions that are being
1936 * added, just the old ones (that are probably being removed).
1937 *
1938 * If we are adding an ops to a function that already is using
1939 * a trampoline, it needs to be removed (trampolines are only
1940 * for single ops connected), then an ops that is not being
1941 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001942 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001943 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001944
1945 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001946 continue;
1947
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001948 /*
1949 * If the ops is being added, it hasn't gotten to
1950 * the point to be removed from this tree yet.
1951 */
1952 if (op->flags & FTRACE_OPS_FL_ADDING)
1953 continue;
1954
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001955
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04001956 /*
1957 * If the ops is being modified and is in the old
1958 * hash, then it is probably being removed from this
1959 * function.
1960 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001961 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
1962 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001963 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04001964 /*
1965 * If the ops is not being added or modified, and it's
1966 * in its normal filter hash, then this must be the one
1967 * we want!
1968 */
1969 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
1970 hash_contains_ip(ip, op->func_hash))
1971 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001972
1973 } while_for_each_ftrace_op(op);
1974
1975 return NULL;
1976}
1977
1978static struct ftrace_ops *
1979ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1980{
1981 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001982 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001983
1984 do_for_each_ftrace_op(op, ftrace_ops_list) {
1985 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001986 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001987 return op;
1988 } while_for_each_ftrace_op(op);
1989
1990 return NULL;
1991}
1992
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001993/**
1994 * ftrace_get_addr_new - Get the call address to set to
1995 * @rec: The ftrace record descriptor
1996 *
1997 * If the record has the FTRACE_FL_REGS set, that means that it
1998 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1999 * is not not set, then it wants to convert to the normal callback.
2000 *
2001 * Returns the address of the trampoline to set to
2002 */
2003unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2004{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002005 struct ftrace_ops *ops;
2006
2007 /* Trampolines take precedence over regs */
2008 if (rec->flags & FTRACE_FL_TRAMP) {
2009 ops = ftrace_find_tramp_ops_new(rec);
2010 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002011 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2012 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002013 /* Ftrace is shutting down, return anything */
2014 return (unsigned long)FTRACE_ADDR;
2015 }
2016 return ops->trampoline;
2017 }
2018
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002019 if (rec->flags & FTRACE_FL_REGS)
2020 return (unsigned long)FTRACE_REGS_ADDR;
2021 else
2022 return (unsigned long)FTRACE_ADDR;
2023}
2024
2025/**
2026 * ftrace_get_addr_curr - Get the call address that is already there
2027 * @rec: The ftrace record descriptor
2028 *
2029 * The FTRACE_FL_REGS_EN is set when the record already points to
2030 * a function that saves all the regs. Basically the '_EN' version
2031 * represents the current state of the function.
2032 *
2033 * Returns the address of the trampoline that is currently being called
2034 */
2035unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2036{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002037 struct ftrace_ops *ops;
2038
2039 /* Trampolines take precedence over regs */
2040 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2041 ops = ftrace_find_tramp_ops_curr(rec);
2042 if (FTRACE_WARN_ON(!ops)) {
2043 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2044 (void *)rec->ip, (void *)rec->ip);
2045 /* Ftrace is shutting down, return anything */
2046 return (unsigned long)FTRACE_ADDR;
2047 }
2048 return ops->trampoline;
2049 }
2050
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002051 if (rec->flags & FTRACE_FL_REGS_EN)
2052 return (unsigned long)FTRACE_REGS_ADDR;
2053 else
2054 return (unsigned long)FTRACE_ADDR;
2055}
2056
Steven Rostedtc88fd862011-08-16 09:53:39 -04002057static int
2058__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2059{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002060 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002061 unsigned long ftrace_addr;
2062 int ret;
2063
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002064 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002065
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002066 /* This needs to be done before we call ftrace_update_record */
2067 ftrace_old_addr = ftrace_get_addr_curr(rec);
2068
2069 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002070
Steven Rostedtc88fd862011-08-16 09:53:39 -04002071 switch (ret) {
2072 case FTRACE_UPDATE_IGNORE:
2073 return 0;
2074
2075 case FTRACE_UPDATE_MAKE_CALL:
2076 return ftrace_make_call(rec, ftrace_addr);
2077
2078 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002079 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002080
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002081 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002082 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002083 }
2084
2085 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002086}
2087
Steven Rostedte4f5d542012-04-27 09:13:18 -04002088void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002089{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002090 struct dyn_ftrace *rec;
2091 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002092 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002093
Steven Rostedt45a4a232011-04-21 23:16:46 -04002094 if (unlikely(ftrace_disabled))
2095 return;
2096
Steven Rostedt265c8312009-02-13 12:43:56 -05002097 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002098 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002099 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04002100 ftrace_bug(failed, rec->ip);
2101 /* Stop processing */
2102 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002103 }
2104 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002105}
2106
Steven Rostedtc88fd862011-08-16 09:53:39 -04002107struct ftrace_rec_iter {
2108 struct ftrace_page *pg;
2109 int index;
2110};
2111
2112/**
2113 * ftrace_rec_iter_start, start up iterating over traced functions
2114 *
2115 * Returns an iterator handle that is used to iterate over all
2116 * the records that represent address locations where functions
2117 * are traced.
2118 *
2119 * May return NULL if no records are available.
2120 */
2121struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2122{
2123 /*
2124 * We only use a single iterator.
2125 * Protected by the ftrace_lock mutex.
2126 */
2127 static struct ftrace_rec_iter ftrace_rec_iter;
2128 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2129
2130 iter->pg = ftrace_pages_start;
2131 iter->index = 0;
2132
2133 /* Could have empty pages */
2134 while (iter->pg && !iter->pg->index)
2135 iter->pg = iter->pg->next;
2136
2137 if (!iter->pg)
2138 return NULL;
2139
2140 return iter;
2141}
2142
2143/**
2144 * ftrace_rec_iter_next, get the next record to process.
2145 * @iter: The handle to the iterator.
2146 *
2147 * Returns the next iterator after the given iterator @iter.
2148 */
2149struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2150{
2151 iter->index++;
2152
2153 if (iter->index >= iter->pg->index) {
2154 iter->pg = iter->pg->next;
2155 iter->index = 0;
2156
2157 /* Could have empty pages */
2158 while (iter->pg && !iter->pg->index)
2159 iter->pg = iter->pg->next;
2160 }
2161
2162 if (!iter->pg)
2163 return NULL;
2164
2165 return iter;
2166}
2167
2168/**
2169 * ftrace_rec_iter_record, get the record at the iterator location
2170 * @iter: The current iterator location
2171 *
2172 * Returns the record that the current @iter is at.
2173 */
2174struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2175{
2176 return &iter->pg->records[iter->index];
2177}
2178
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302179static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002180ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002181{
2182 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002183 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002184
2185 ip = rec->ip;
2186
Steven Rostedt45a4a232011-04-21 23:16:46 -04002187 if (unlikely(ftrace_disabled))
2188 return 0;
2189
Shaohua Li25aac9d2009-01-09 11:29:40 +08002190 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002191 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002192 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302193 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002194 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302195 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002196}
2197
Steven Rostedt000ab692009-02-17 13:35:06 -05002198/*
2199 * archs can override this function if they must do something
2200 * before the modifying code is performed.
2201 */
2202int __weak ftrace_arch_code_modify_prepare(void)
2203{
2204 return 0;
2205}
2206
2207/*
2208 * archs can override this function if they must do something
2209 * after the modifying code is performed.
2210 */
2211int __weak ftrace_arch_code_modify_post_process(void)
2212{
2213 return 0;
2214}
2215
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002216void ftrace_modify_all_code(int command)
2217{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002218 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002219 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002220
2221 /*
2222 * If the ftrace_caller calls a ftrace_ops func directly,
2223 * we need to make sure that it only traces functions it
2224 * expects to trace. When doing the switch of functions,
2225 * we need to update to the ftrace_ops_list_func first
2226 * before the transition between old and new calls are set,
2227 * as the ftrace_ops_list_func will check the ops hashes
2228 * to make sure the ops are having the right functions
2229 * traced.
2230 */
Petr Mladekcd210672014-02-24 17:12:21 +01002231 if (update) {
2232 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2233 if (FTRACE_WARN_ON(err))
2234 return;
2235 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002236
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002237 if (command & FTRACE_UPDATE_CALLS)
2238 ftrace_replace_code(1);
2239 else if (command & FTRACE_DISABLE_CALLS)
2240 ftrace_replace_code(0);
2241
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002242 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2243 function_trace_op = set_function_trace_op;
2244 smp_wmb();
2245 /* If irqs are disabled, we are in stop machine */
2246 if (!irqs_disabled())
2247 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002248 err = ftrace_update_ftrace_func(ftrace_trace_function);
2249 if (FTRACE_WARN_ON(err))
2250 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002251 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002252
2253 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002254 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002255 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002256 err = ftrace_disable_ftrace_graph_caller();
2257 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002258}
2259
Ingo Molnare309b412008-05-12 21:20:51 +02002260static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002261{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002262 int *command = data;
2263
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002264 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002265
Steven Rostedtc88fd862011-08-16 09:53:39 -04002266 return 0;
2267}
2268
2269/**
2270 * ftrace_run_stop_machine, go back to the stop machine method
2271 * @command: The command to tell ftrace what to do
2272 *
2273 * If an arch needs to fall back to the stop machine method, the
2274 * it can call this function.
2275 */
2276void ftrace_run_stop_machine(int command)
2277{
2278 stop_machine(__ftrace_modify_code, &command, NULL);
2279}
2280
2281/**
2282 * arch_ftrace_update_code, modify the code to trace or not trace
2283 * @command: The command that needs to be done
2284 *
2285 * Archs can override this function if it does not need to
2286 * run stop_machine() to modify code.
2287 */
2288void __weak arch_ftrace_update_code(int command)
2289{
2290 ftrace_run_stop_machine(command);
2291}
2292
2293static void ftrace_run_update_code(int command)
2294{
2295 int ret;
2296
2297 ret = ftrace_arch_code_modify_prepare();
2298 FTRACE_WARN_ON(ret);
2299 if (ret)
2300 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002301
2302 /*
2303 * By default we use stop_machine() to modify the code.
2304 * But archs can do what ever they want as long as it
2305 * is safe. The stop_machine() is the safest, but also
2306 * produces the most overhead.
2307 */
2308 arch_ftrace_update_code(command);
2309
Steven Rostedt000ab692009-02-17 13:35:06 -05002310 ret = ftrace_arch_code_modify_post_process();
2311 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002312}
2313
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002314static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2315 struct ftrace_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002316{
2317 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002318 ops->old_hash.filter_hash = old_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002319 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002320 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002321 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2322}
2323
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002324static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002325static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002326
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002327void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2328{
2329}
2330
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002331static void control_ops_free(struct ftrace_ops *ops)
2332{
2333 free_percpu(ops->disabled);
2334}
2335
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002336static void ftrace_startup_enable(int command)
2337{
2338 if (saved_ftrace_func != ftrace_trace_function) {
2339 saved_ftrace_func = ftrace_trace_function;
2340 command |= FTRACE_UPDATE_TRACE_FUNC;
2341 }
2342
2343 if (!command || !ftrace_enabled)
2344 return;
2345
2346 ftrace_run_update_code(command);
2347}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002348
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002349static void ftrace_startup_all(int command)
2350{
2351 update_all_ops = true;
2352 ftrace_startup_enable(command);
2353 update_all_ops = false;
2354}
2355
Steven Rostedta1cd6172011-05-23 15:24:25 -04002356static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002357{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002358 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002359
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002360 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002361 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002362
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002363 ret = __register_ftrace_function(ops);
2364 if (ret)
2365 return ret;
2366
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002367 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002368 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002369
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002370 /*
2371 * Note that ftrace probes uses this to start up
2372 * and modify functions it will probe. But we still
2373 * set the ADDING flag for modification, as probes
2374 * do not have trampolines. If they add them in the
2375 * future, then the probes will need to distinguish
2376 * between adding and updating probes.
2377 */
2378 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002379
2380 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002381
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002382 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002383
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002384 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2385
Steven Rostedta1cd6172011-05-23 15:24:25 -04002386 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002387}
2388
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002389static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002390{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002391 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002392
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002393 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002394 return -ENODEV;
2395
2396 ret = __unregister_ftrace_function(ops);
2397 if (ret)
2398 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002399
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002400 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002401 /*
2402 * Just warn in case of unbalance, no need to kill ftrace, it's not
2403 * critical but the ftrace_call callers may be never nopped again after
2404 * further ftrace uses.
2405 */
2406 WARN_ON_ONCE(ftrace_start_up < 0);
2407
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002408 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002409
Namhyung Kima737e6d2014-06-12 23:56:12 +09002410 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002411
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002412 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002413
2414 if (saved_ftrace_func != ftrace_trace_function) {
2415 saved_ftrace_func = ftrace_trace_function;
2416 command |= FTRACE_UPDATE_TRACE_FUNC;
2417 }
2418
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002419 if (!command || !ftrace_enabled) {
2420 /*
2421 * If these are control ops, they still need their
2422 * per_cpu field freed. Since, function tracing is
2423 * not currently active, we can just free them
2424 * without synchronizing all CPUs.
2425 */
2426 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2427 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002428 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002429 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002430
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002431 /*
2432 * If the ops uses a trampoline, then it needs to be
2433 * tested first on update.
2434 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002435 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002436 removed_ops = ops;
2437
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002438 /* The trampoline logic checks the old hashes */
2439 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2440 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2441
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002442 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002443
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002444 /*
2445 * If there's no more ops registered with ftrace, run a
2446 * sanity check to make sure all rec flags are cleared.
2447 */
2448 if (ftrace_ops_list == &ftrace_list_end) {
2449 struct ftrace_page *pg;
2450 struct dyn_ftrace *rec;
2451
2452 do_for_each_ftrace_rec(pg, rec) {
2453 if (FTRACE_WARN_ON_ONCE(rec->flags))
2454 pr_warn(" %pS flags:%lx\n",
2455 (void *)rec->ip, rec->flags);
2456 } while_for_each_ftrace_rec();
2457 }
2458
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002459 ops->old_hash.filter_hash = NULL;
2460 ops->old_hash.notrace_hash = NULL;
2461
2462 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002463 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002464
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002465 /*
2466 * Dynamic ops may be freed, we must make sure that all
2467 * callers are done before leaving this function.
2468 * The same goes for freeing the per_cpu data of the control
2469 * ops.
2470 *
2471 * Again, normal synchronize_sched() is not good enough.
2472 * We need to do a hard force of sched synchronization.
2473 * This is because we use preempt_disable() to do RCU, but
2474 * the function tracers can be called where RCU is not watching
2475 * (like before user_exit()). We can not rely on the RCU
2476 * infrastructure to do the synchronization, thus we must do it
2477 * ourselves.
2478 */
2479 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2480 schedule_on_each_cpu(ftrace_sync);
2481
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002482 arch_ftrace_trampoline_free(ops);
2483
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002484 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2485 control_ops_free(ops);
2486 }
2487
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002488 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002489}
2490
Ingo Molnare309b412008-05-12 21:20:51 +02002491static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002492{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002493 if (unlikely(ftrace_disabled))
2494 return;
2495
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002496 /* Force update next time */
2497 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002498 /* ftrace_start_up is true if we want ftrace running */
2499 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002500 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002501}
2502
Ingo Molnare309b412008-05-12 21:20:51 +02002503static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002504{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002505 if (unlikely(ftrace_disabled))
2506 return;
2507
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002508 /* ftrace_start_up is true if ftrace is running */
2509 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002510 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002511}
2512
Steven Rostedt3d083392008-05-12 21:20:42 +02002513static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002514unsigned long ftrace_update_tot_cnt;
2515
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002516static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002517{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002518 /*
2519 * Filter_hash being empty will default to trace module.
2520 * But notrace hash requires a test of individual module functions.
2521 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002522 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2523 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002524}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002525
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002526/*
2527 * Check if the current ops references the record.
2528 *
2529 * If the ops traces all functions, then it was already accounted for.
2530 * If the ops does not trace the current record function, skip it.
2531 * If the ops ignores the function via notrace filter, skip it.
2532 */
2533static inline bool
2534ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2535{
2536 /* If ops isn't enabled, ignore it */
2537 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2538 return 0;
2539
2540 /* If ops traces all mods, we already accounted for it */
2541 if (ops_traces_mod(ops))
2542 return 0;
2543
2544 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002545 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2546 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002547 return 0;
2548
2549 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002550 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002551 return 0;
2552
2553 return 1;
2554}
2555
2556static int referenced_filters(struct dyn_ftrace *rec)
2557{
2558 struct ftrace_ops *ops;
2559 int cnt = 0;
2560
2561 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2562 if (ops_references_rec(ops, rec))
2563 cnt++;
2564 }
2565
2566 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002567}
2568
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002569static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002570{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002571 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002572 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302573 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002574 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002575 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002576 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002577 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002578
2579 /*
2580 * When adding a module, we need to check if tracers are
2581 * currently enabled and if they are set to trace all functions.
2582 * If they are, we need to enable the module functions as well
2583 * as update the reference counts for those function records.
2584 */
2585 if (mod) {
2586 struct ftrace_ops *ops;
2587
2588 for (ops = ftrace_ops_list;
2589 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002590 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2591 if (ops_traces_mod(ops))
2592 ref++;
2593 else
2594 test = true;
2595 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002596 }
2597 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002598
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002599 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002600
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002601 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302602
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002603 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002604 int cnt = ref;
2605
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002606 /* If something went wrong, bail without enabling anything */
2607 if (unlikely(ftrace_disabled))
2608 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002609
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002610 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002611 if (test)
2612 cnt += referenced_filters(p);
2613 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302614
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002615 /*
2616 * Do the initial record conversion from mcount jump
2617 * to the NOP instructions.
2618 */
2619 if (!ftrace_code_disable(mod, p))
2620 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002621
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002622 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002623
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002624 /*
2625 * If the tracing is enabled, go ahead and enable the record.
2626 *
2627 * The reason not to enable the record immediatelly is the
2628 * inherent check of ftrace_make_nop/ftrace_make_call for
2629 * correct previous instructions. Making first the NOP
2630 * conversion puts the module to the correct state, thus
2631 * passing the ftrace_make_call check.
2632 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002633 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002634 int failed = __ftrace_replace_code(p, 1);
2635 if (failed)
2636 ftrace_bug(failed, p->ip);
2637 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002638 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002639 }
2640
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002641 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002642 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002643 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002644
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002645 return 0;
2646}
2647
Steven Rostedta7900872011-12-16 16:23:44 -05002648static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002649{
Steven Rostedta7900872011-12-16 16:23:44 -05002650 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002651 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002652
Steven Rostedta7900872011-12-16 16:23:44 -05002653 if (WARN_ON(!count))
2654 return -EINVAL;
2655
2656 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002657
2658 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002659 * We want to fill as much as possible. No more than a page
2660 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002661 */
Steven Rostedta7900872011-12-16 16:23:44 -05002662 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2663 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002664
Steven Rostedta7900872011-12-16 16:23:44 -05002665 again:
2666 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2667
2668 if (!pg->records) {
2669 /* if we can't allocate this size, try something smaller */
2670 if (!order)
2671 return -ENOMEM;
2672 order >>= 1;
2673 goto again;
2674 }
2675
2676 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2677 pg->size = cnt;
2678
2679 if (cnt > count)
2680 cnt = count;
2681
2682 return cnt;
2683}
2684
2685static struct ftrace_page *
2686ftrace_allocate_pages(unsigned long num_to_init)
2687{
2688 struct ftrace_page *start_pg;
2689 struct ftrace_page *pg;
2690 int order;
2691 int cnt;
2692
2693 if (!num_to_init)
2694 return 0;
2695
2696 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2697 if (!pg)
2698 return NULL;
2699
2700 /*
2701 * Try to allocate as much as possible in one continues
2702 * location that fills in all of the space. We want to
2703 * waste as little space as possible.
2704 */
2705 for (;;) {
2706 cnt = ftrace_allocate_records(pg, num_to_init);
2707 if (cnt < 0)
2708 goto free_pages;
2709
2710 num_to_init -= cnt;
2711 if (!num_to_init)
2712 break;
2713
2714 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2715 if (!pg->next)
2716 goto free_pages;
2717
2718 pg = pg->next;
2719 }
2720
2721 return start_pg;
2722
2723 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002724 pg = start_pg;
2725 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002726 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2727 free_pages((unsigned long)pg->records, order);
2728 start_pg = pg->next;
2729 kfree(pg);
2730 pg = start_pg;
2731 }
2732 pr_info("ftrace: FAILED to allocate memory for functions\n");
2733 return NULL;
2734}
2735
Steven Rostedt5072c592008-05-12 21:20:43 +02002736#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2737
2738struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002739 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002740 loff_t func_pos;
2741 struct ftrace_page *pg;
2742 struct dyn_ftrace *func;
2743 struct ftrace_func_probe *probe;
2744 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002745 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002746 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002747 int hidx;
2748 int idx;
2749 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002750};
2751
Ingo Molnare309b412008-05-12 21:20:51 +02002752static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002753t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002754{
2755 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002756 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002757 struct hlist_head *hhd;
2758
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002759 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002760 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002761
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002762 if (iter->probe)
2763 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002764 retry:
2765 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2766 return NULL;
2767
2768 hhd = &ftrace_func_hash[iter->hidx];
2769
2770 if (hlist_empty(hhd)) {
2771 iter->hidx++;
2772 hnd = NULL;
2773 goto retry;
2774 }
2775
2776 if (!hnd)
2777 hnd = hhd->first;
2778 else {
2779 hnd = hnd->next;
2780 if (!hnd) {
2781 iter->hidx++;
2782 goto retry;
2783 }
2784 }
2785
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002786 if (WARN_ON_ONCE(!hnd))
2787 return NULL;
2788
2789 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2790
2791 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002792}
2793
2794static void *t_hash_start(struct seq_file *m, loff_t *pos)
2795{
2796 struct ftrace_iterator *iter = m->private;
2797 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002798 loff_t l;
2799
Steven Rostedt69a30832011-12-19 15:21:16 -05002800 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2801 return NULL;
2802
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002803 if (iter->func_pos > *pos)
2804 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002805
Li Zefand82d6242009-06-24 09:54:54 +08002806 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002807 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002808 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002809 if (!p)
2810 break;
2811 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002812 if (!p)
2813 return NULL;
2814
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002815 /* Only set this if we have an item */
2816 iter->flags |= FTRACE_ITER_HASH;
2817
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002818 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002819}
2820
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002821static int
2822t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002823{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002824 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002825
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002826 rec = iter->probe;
2827 if (WARN_ON_ONCE(!rec))
2828 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002829
Steven Rostedt809dcf22009-02-16 23:06:01 -05002830 if (rec->ops->print)
2831 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2832
Steven Rostedtb375a112009-09-17 00:05:58 -04002833 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002834
2835 if (rec->data)
2836 seq_printf(m, ":%p", rec->data);
2837 seq_putc(m, '\n');
2838
2839 return 0;
2840}
2841
2842static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002843t_next(struct seq_file *m, void *v, loff_t *pos)
2844{
2845 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002846 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002847 struct dyn_ftrace *rec = NULL;
2848
Steven Rostedt45a4a232011-04-21 23:16:46 -04002849 if (unlikely(ftrace_disabled))
2850 return NULL;
2851
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002852 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002853 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002854
Steven Rostedt5072c592008-05-12 21:20:43 +02002855 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002856 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002857
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002858 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002859 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002860
Steven Rostedt5072c592008-05-12 21:20:43 +02002861 retry:
2862 if (iter->idx >= iter->pg->index) {
2863 if (iter->pg->next) {
2864 iter->pg = iter->pg->next;
2865 iter->idx = 0;
2866 goto retry;
2867 }
2868 } else {
2869 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002870 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002871 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002872
Steven Rostedt41c52c02008-05-22 11:46:33 -04002873 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002874 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04002875
2876 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002877 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002878
Steven Rostedt5072c592008-05-12 21:20:43 +02002879 rec = NULL;
2880 goto retry;
2881 }
2882 }
2883
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002884 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002885 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002886
2887 iter->func = rec;
2888
2889 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002890}
2891
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002892static void reset_iter_read(struct ftrace_iterator *iter)
2893{
2894 iter->pos = 0;
2895 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002896 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002897}
2898
2899static void *t_start(struct seq_file *m, loff_t *pos)
2900{
2901 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002902 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002903 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002904 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002905
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002906 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002907
2908 if (unlikely(ftrace_disabled))
2909 return NULL;
2910
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002911 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002912 * If an lseek was done, then reset and start from beginning.
2913 */
2914 if (*pos < iter->pos)
2915 reset_iter_read(iter);
2916
2917 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002918 * For set_ftrace_filter reading, if we have the filter
2919 * off, we can short cut and just print out that all
2920 * functions are enabled.
2921 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002922 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002923 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002924 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002925 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002926 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002927 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002928 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002929 /* reset in case of seek/pread */
2930 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002931 return iter;
2932 }
2933
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002934 if (iter->flags & FTRACE_ITER_HASH)
2935 return t_hash_start(m, pos);
2936
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002937 /*
2938 * Unfortunately, we need to restart at ftrace_pages_start
2939 * every time we let go of the ftrace_mutex. This is because
2940 * those pointers can change without the lock.
2941 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002942 iter->pg = ftrace_pages_start;
2943 iter->idx = 0;
2944 for (l = 0; l <= *pos; ) {
2945 p = t_next(m, p, &l);
2946 if (!p)
2947 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002948 }
walimis5821e1b2008-11-15 15:19:06 +08002949
Steven Rostedt69a30832011-12-19 15:21:16 -05002950 if (!p)
2951 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002952
2953 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002954}
2955
2956static void t_stop(struct seq_file *m, void *p)
2957{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002958 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002959}
2960
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04002961void * __weak
2962arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2963{
2964 return NULL;
2965}
2966
2967static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
2968 struct dyn_ftrace *rec)
2969{
2970 void *ptr;
2971
2972 ptr = arch_ftrace_trampoline_func(ops, rec);
2973 if (ptr)
2974 seq_printf(m, " ->%pS", ptr);
2975}
2976
Steven Rostedt5072c592008-05-12 21:20:43 +02002977static int t_show(struct seq_file *m, void *v)
2978{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002979 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002980 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002981
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002982 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002983 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002984
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002985 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002986 if (iter->flags & FTRACE_ITER_NOTRACE)
2987 seq_printf(m, "#### no functions disabled ####\n");
2988 else
2989 seq_printf(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002990 return 0;
2991 }
2992
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002993 rec = iter->func;
2994
Steven Rostedt5072c592008-05-12 21:20:43 +02002995 if (!rec)
2996 return 0;
2997
Steven Rostedt647bcd02011-05-03 14:39:21 -04002998 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002999 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003000 struct ftrace_ops *ops = NULL;
3001
Steven Rostedt08f6fba2012-04-30 16:20:23 -04003002 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003003 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003004 rec->flags & FTRACE_FL_REGS ? " R" : " ");
3005 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003006 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04003007 if (ops)
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003008 seq_printf(m, "\ttramp: %pS",
3009 (void *)ops->trampoline);
3010 else
3011 seq_printf(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003012
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003013 }
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003014 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003015 }
3016
Steven Rostedt647bcd02011-05-03 14:39:21 -04003017 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02003018
3019 return 0;
3020}
3021
James Morris88e9d342009-09-22 16:43:43 -07003022static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003023 .start = t_start,
3024 .next = t_next,
3025 .stop = t_stop,
3026 .show = t_show,
3027};
3028
Ingo Molnare309b412008-05-12 21:20:51 +02003029static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003030ftrace_avail_open(struct inode *inode, struct file *file)
3031{
3032 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003033
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003034 if (unlikely(ftrace_disabled))
3035 return -ENODEV;
3036
Jiri Olsa50e18b92012-04-25 10:23:39 +02003037 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3038 if (iter) {
3039 iter->pg = ftrace_pages_start;
3040 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003041 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003042
Jiri Olsa50e18b92012-04-25 10:23:39 +02003043 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003044}
3045
Steven Rostedt647bcd02011-05-03 14:39:21 -04003046static int
3047ftrace_enabled_open(struct inode *inode, struct file *file)
3048{
3049 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003050
Jiri Olsa50e18b92012-04-25 10:23:39 +02003051 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3052 if (iter) {
3053 iter->pg = ftrace_pages_start;
3054 iter->flags = FTRACE_ITER_ENABLED;
3055 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003056 }
3057
Jiri Olsa50e18b92012-04-25 10:23:39 +02003058 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003059}
3060
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003061/**
3062 * ftrace_regex_open - initialize function tracer filter files
3063 * @ops: The ftrace_ops that hold the hash filters
3064 * @flag: The type of filter to process
3065 * @inode: The inode, usually passed in to your open routine
3066 * @file: The file, usually passed in to your open routine
3067 *
3068 * ftrace_regex_open() initializes the filter files for the
3069 * @ops. Depending on @flag it may process the filter hash or
3070 * the notrace hash of @ops. With this called from the open
3071 * routine, you can use ftrace_filter_write() for the write
3072 * routine if @flag has FTRACE_ITER_FILTER set, or
3073 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003074 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003075 * release must call ftrace_regex_release().
3076 */
3077int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003078ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003079 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003080{
3081 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003082 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003083 int ret = 0;
3084
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003085 ftrace_ops_init(ops);
3086
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003087 if (unlikely(ftrace_disabled))
3088 return -ENODEV;
3089
Steven Rostedt5072c592008-05-12 21:20:43 +02003090 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3091 if (!iter)
3092 return -ENOMEM;
3093
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003094 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3095 kfree(iter);
3096 return -ENOMEM;
3097 }
3098
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003099 iter->ops = ops;
3100 iter->flags = flag;
3101
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003102 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003103
Steven Rostedtf45948e2011-05-02 12:29:25 -04003104 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003105 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003106 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003107 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003108
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003109 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003110 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3111
3112 if (file->f_flags & O_TRUNC)
3113 iter->hash = alloc_ftrace_hash(size_bits);
3114 else
3115 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3116
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003117 if (!iter->hash) {
3118 trace_parser_put(&iter->parser);
3119 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003120 ret = -ENOMEM;
3121 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003122 }
3123 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003124
Steven Rostedt5072c592008-05-12 21:20:43 +02003125 if (file->f_mode & FMODE_READ) {
3126 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003127
3128 ret = seq_open(file, &show_ftrace_seq_ops);
3129 if (!ret) {
3130 struct seq_file *m = file->private_data;
3131 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003132 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003133 /* Failed */
3134 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003135 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003136 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003137 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003138 } else
3139 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003140
3141 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003142 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003143
3144 return ret;
3145}
3146
Steven Rostedt41c52c02008-05-22 11:46:33 -04003147static int
3148ftrace_filter_open(struct inode *inode, struct file *file)
3149{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003150 struct ftrace_ops *ops = inode->i_private;
3151
3152 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003153 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3154 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003155}
3156
3157static int
3158ftrace_notrace_open(struct inode *inode, struct file *file)
3159{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003160 struct ftrace_ops *ops = inode->i_private;
3161
3162 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003163 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003164}
3165
Steven Rostedt64e7c442009-02-13 17:08:48 -05003166static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003167{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003168 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003169 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003170
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003171 switch (type) {
3172 case MATCH_FULL:
3173 if (strcmp(str, regex) == 0)
3174 matched = 1;
3175 break;
3176 case MATCH_FRONT_ONLY:
3177 if (strncmp(str, regex, len) == 0)
3178 matched = 1;
3179 break;
3180 case MATCH_MIDDLE_ONLY:
3181 if (strstr(str, regex))
3182 matched = 1;
3183 break;
3184 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003185 slen = strlen(str);
3186 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003187 matched = 1;
3188 break;
3189 }
3190
3191 return matched;
3192}
3193
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003194static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003195enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003196{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003197 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003198 int ret = 0;
3199
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003200 entry = ftrace_lookup_ip(hash, rec->ip);
3201 if (not) {
3202 /* Do nothing if it doesn't exist */
3203 if (!entry)
3204 return 0;
3205
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003206 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003207 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003208 /* Do nothing if it exists */
3209 if (entry)
3210 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003211
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003212 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003213 }
3214 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003215}
3216
Steven Rostedt64e7c442009-02-13 17:08:48 -05003217static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003218ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3219 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003220{
3221 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003222 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003223
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003224 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3225
3226 if (mod) {
3227 /* module lookup requires matching the module */
3228 if (!modname || strcmp(modname, mod))
3229 return 0;
3230
3231 /* blank search means to match all funcs in the mod */
3232 if (!len)
3233 return 1;
3234 }
3235
Steven Rostedt64e7c442009-02-13 17:08:48 -05003236 return ftrace_match(str, regex, len, type);
3237}
3238
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003239static int
3240match_records(struct ftrace_hash *hash, char *buff,
3241 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003242{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003243 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003244 struct ftrace_page *pg;
3245 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003246 int type = MATCH_FULL;
3247 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003248 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003249 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003250
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003251 if (len) {
3252 type = filter_parse_regex(buff, len, &search, &not);
3253 search_len = strlen(search);
3254 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003255
Steven Rostedt52baf112009-02-14 01:15:39 -05003256 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003257
3258 if (unlikely(ftrace_disabled))
3259 goto out_unlock;
3260
Steven Rostedt265c8312009-02-13 12:43:56 -05003261 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003262 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003263 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003264 if (ret < 0) {
3265 found = ret;
3266 goto out_unlock;
3267 }
Li Zefan311d16d2009-12-08 11:15:11 +08003268 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003269 }
3270 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003271 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003272 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003273
3274 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003275}
3276
Steven Rostedt64e7c442009-02-13 17:08:48 -05003277static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003278ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003279{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003280 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003281}
3282
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003283static int
3284ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003285{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003286 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003287
Steven Rostedt64e7c442009-02-13 17:08:48 -05003288 /* blank or '*' mean the same */
3289 if (strcmp(buff, "*") == 0)
3290 buff[0] = 0;
3291
3292 /* handle the case of 'dont filter this module' */
3293 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3294 buff[0] = 0;
3295 not = 1;
3296 }
3297
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003298 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003299}
3300
Steven Rostedtf6180772009-02-14 00:40:25 -05003301/*
3302 * We register the module command as a template to show others how
3303 * to register the a command as well.
3304 */
3305
3306static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003307ftrace_mod_callback(struct ftrace_hash *hash,
3308 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003309{
3310 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003311 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003312
3313 /*
3314 * cmd == 'mod' because we only registered this func
3315 * for the 'mod' ftrace_func_command.
3316 * But if you register one func with multiple commands,
3317 * you can tell which command was used by the cmd
3318 * parameter.
3319 */
3320
3321 /* we must have a module name */
3322 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003323 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003324
3325 mod = strsep(&param, ":");
3326 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003327 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003328
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003329 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003330 if (!ret)
3331 ret = -EINVAL;
3332 if (ret < 0)
3333 return ret;
3334
3335 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003336}
3337
3338static struct ftrace_func_command ftrace_mod_cmd = {
3339 .name = "mod",
3340 .func = ftrace_mod_callback,
3341};
3342
3343static int __init ftrace_mod_cmd_init(void)
3344{
3345 return register_ftrace_command(&ftrace_mod_cmd);
3346}
Steven Rostedt6f415672012-10-05 12:13:07 -04003347core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003348
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003349static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003350 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003351{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003352 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003353 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003354 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003355
3356 key = hash_long(ip, FTRACE_HASH_BITS);
3357
3358 hhd = &ftrace_func_hash[key];
3359
3360 if (hlist_empty(hhd))
3361 return;
3362
3363 /*
3364 * Disable preemption for these calls to prevent a RCU grace
3365 * period. This syncs the hash iteration and freeing of items
3366 * on the hash. rcu_read_lock is too dangerous here.
3367 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003368 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003369 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003370 if (entry->ip == ip)
3371 entry->ops->func(ip, parent_ip, &entry->data);
3372 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003373 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003374}
3375
Steven Rostedtb6887d72009-02-17 12:32:04 -05003376static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003377{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003378 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003379 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003380 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003381};
3382
Steven Rostedtb6887d72009-02-17 12:32:04 -05003383static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003384
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003385static void __enable_ftrace_function_probe(struct ftrace_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003386{
Steven Rostedtb8489142011-05-04 09:27:52 -04003387 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003388 int i;
3389
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003390 if (ftrace_probe_registered) {
3391 /* still need to update the function call sites */
3392 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003393 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3394 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003395 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003396 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003397
3398 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3399 struct hlist_head *hhd = &ftrace_func_hash[i];
3400 if (hhd->first)
3401 break;
3402 }
3403 /* Nothing registered? */
3404 if (i == FTRACE_FUNC_HASHSIZE)
3405 return;
3406
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003407 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003408
Steven Rostedtb6887d72009-02-17 12:32:04 -05003409 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003410}
3411
Steven Rostedtb6887d72009-02-17 12:32:04 -05003412static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003413{
3414 int i;
3415
Steven Rostedtb6887d72009-02-17 12:32:04 -05003416 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003417 return;
3418
3419 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3420 struct hlist_head *hhd = &ftrace_func_hash[i];
3421 if (hhd->first)
3422 return;
3423 }
3424
3425 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003426 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003427
Steven Rostedtb6887d72009-02-17 12:32:04 -05003428 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003429}
3430
3431
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003432static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003433{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003434 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003435 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003436 kfree(entry);
3437}
3438
Steven Rostedt59df055f2009-02-14 15:29:06 -05003439int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003440register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003441 void *data)
3442{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003443 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003444 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003445 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003446 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003447 struct ftrace_page *pg;
3448 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003449 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003450 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003451 int count = 0;
3452 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003453 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003454
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003455 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003456 len = strlen(search);
3457
Steven Rostedtb6887d72009-02-17 12:32:04 -05003458 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003459 if (WARN_ON(not))
3460 return -EINVAL;
3461
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003462 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003463
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003464 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003465 if (!hash) {
3466 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003467 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003468 }
3469
3470 if (unlikely(ftrace_disabled)) {
3471 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003472 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003473 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003474
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003475 mutex_lock(&ftrace_lock);
3476
Steven Rostedt59df055f2009-02-14 15:29:06 -05003477 do_for_each_ftrace_rec(pg, rec) {
3478
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003479 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003480 continue;
3481
3482 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3483 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003484 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003485 if (!count)
3486 count = -ENOMEM;
3487 goto out_unlock;
3488 }
3489
3490 count++;
3491
3492 entry->data = data;
3493
3494 /*
3495 * The caller might want to do something special
3496 * for each function we find. We call the callback
3497 * to give the caller an opportunity to do so.
3498 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003499 if (ops->init) {
3500 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003501 /* caller does not like this func */
3502 kfree(entry);
3503 continue;
3504 }
3505 }
3506
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003507 ret = enter_record(hash, rec, 0);
3508 if (ret < 0) {
3509 kfree(entry);
3510 count = ret;
3511 goto out_unlock;
3512 }
3513
Steven Rostedt59df055f2009-02-14 15:29:06 -05003514 entry->ops = ops;
3515 entry->ip = rec->ip;
3516
3517 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3518 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3519
3520 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003521
3522 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003523
3524 __enable_ftrace_function_probe(old_hash);
3525
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003526 if (!ret)
3527 free_ftrace_hash_rcu(old_hash);
3528 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003529 count = ret;
3530
Steven Rostedt59df055f2009-02-14 15:29:06 -05003531 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003532 mutex_unlock(&ftrace_lock);
3533 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003534 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003535 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003536
3537 return count;
3538}
3539
3540enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003541 PROBE_TEST_FUNC = 1,
3542 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003543};
3544
3545static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003546__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003547 void *data, int flags)
3548{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003549 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003550 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003551 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003552 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003553 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003554 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003555 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003556 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003557 char str[KSYM_SYMBOL_LEN];
3558 int type = MATCH_FULL;
3559 int i, len = 0;
3560 char *search;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003561 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003562
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003563 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003564 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003565 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003566 int not;
3567
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003568 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003569 len = strlen(search);
3570
Steven Rostedtb6887d72009-02-17 12:32:04 -05003571 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003572 if (WARN_ON(not))
3573 return;
3574 }
3575
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003576 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003577
3578 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3579 if (!hash)
3580 /* Hmm, should report this somehow */
3581 goto out_unlock;
3582
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003583 INIT_LIST_HEAD(&free_list);
3584
Steven Rostedt59df055f2009-02-14 15:29:06 -05003585 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3586 struct hlist_head *hhd = &ftrace_func_hash[i];
3587
Sasha Levinb67bfe02013-02-27 17:06:00 -08003588 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003589
3590 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003591 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003592 continue;
3593
Steven Rostedtb6887d72009-02-17 12:32:04 -05003594 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003595 continue;
3596
3597 /* do this last, since it is the most expensive */
3598 if (glob) {
3599 kallsyms_lookup(entry->ip, NULL, NULL,
3600 NULL, str);
3601 if (!ftrace_match(str, glob, len, type))
3602 continue;
3603 }
3604
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003605 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3606 /* It is possible more than one entry had this ip */
3607 if (rec_entry)
3608 free_hash_entry(hash, rec_entry);
3609
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003610 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003611 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003612 }
3613 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003614 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003615 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003616 /*
3617 * Remove after the disable is called. Otherwise, if the last
3618 * probe is removed, a null hash means *all enabled*.
3619 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003620 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003621 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003622 if (!ret)
3623 free_ftrace_hash_rcu(old_hash);
3624
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003625 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3626 list_del(&entry->free_list);
3627 ftrace_free_entry(entry);
3628 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003629 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003630
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003631 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003632 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003633 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003634}
3635
3636void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003637unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003638 void *data)
3639{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003640 __unregister_ftrace_function_probe(glob, ops, data,
3641 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003642}
3643
3644void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003645unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003646{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003647 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003648}
3649
Steven Rostedtb6887d72009-02-17 12:32:04 -05003650void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003651{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003652 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003653}
3654
Steven Rostedtf6180772009-02-14 00:40:25 -05003655static LIST_HEAD(ftrace_commands);
3656static DEFINE_MUTEX(ftrace_cmd_mutex);
3657
Tom Zanussi38de93a2013-10-24 08:34:18 -05003658/*
3659 * Currently we only register ftrace commands from __init, so mark this
3660 * __init too.
3661 */
3662__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003663{
3664 struct ftrace_func_command *p;
3665 int ret = 0;
3666
3667 mutex_lock(&ftrace_cmd_mutex);
3668 list_for_each_entry(p, &ftrace_commands, list) {
3669 if (strcmp(cmd->name, p->name) == 0) {
3670 ret = -EBUSY;
3671 goto out_unlock;
3672 }
3673 }
3674 list_add(&cmd->list, &ftrace_commands);
3675 out_unlock:
3676 mutex_unlock(&ftrace_cmd_mutex);
3677
3678 return ret;
3679}
3680
Tom Zanussi38de93a2013-10-24 08:34:18 -05003681/*
3682 * Currently we only unregister ftrace commands from __init, so mark
3683 * this __init too.
3684 */
3685__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003686{
3687 struct ftrace_func_command *p, *n;
3688 int ret = -ENODEV;
3689
3690 mutex_lock(&ftrace_cmd_mutex);
3691 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3692 if (strcmp(cmd->name, p->name) == 0) {
3693 ret = 0;
3694 list_del_init(&p->list);
3695 goto out_unlock;
3696 }
3697 }
3698 out_unlock:
3699 mutex_unlock(&ftrace_cmd_mutex);
3700
3701 return ret;
3702}
3703
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003704static int ftrace_process_regex(struct ftrace_hash *hash,
3705 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003706{
Steven Rostedtf6180772009-02-14 00:40:25 -05003707 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003708 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003709 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003710
3711 func = strsep(&next, ":");
3712
3713 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003714 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003715 if (!ret)
3716 ret = -EINVAL;
3717 if (ret < 0)
3718 return ret;
3719 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003720 }
3721
Steven Rostedtf6180772009-02-14 00:40:25 -05003722 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003723
3724 command = strsep(&next, ":");
3725
Steven Rostedtf6180772009-02-14 00:40:25 -05003726 mutex_lock(&ftrace_cmd_mutex);
3727 list_for_each_entry(p, &ftrace_commands, list) {
3728 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003729 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003730 goto out_unlock;
3731 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003732 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003733 out_unlock:
3734 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003735
Steven Rostedtf6180772009-02-14 00:40:25 -05003736 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003737}
3738
Ingo Molnare309b412008-05-12 21:20:51 +02003739static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003740ftrace_regex_write(struct file *file, const char __user *ubuf,
3741 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003742{
3743 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003744 struct trace_parser *parser;
3745 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003746
Li Zefan4ba79782009-09-22 13:52:20 +08003747 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003748 return 0;
3749
Steven Rostedt5072c592008-05-12 21:20:43 +02003750 if (file->f_mode & FMODE_READ) {
3751 struct seq_file *m = file->private_data;
3752 iter = m->private;
3753 } else
3754 iter = file->private_data;
3755
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003756 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003757 return -ENODEV;
3758
3759 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003760
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003761 parser = &iter->parser;
3762 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003763
Li Zefan4ba79782009-09-22 13:52:20 +08003764 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003765 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003766 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003767 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003768 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003769 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003770 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003771 }
3772
Steven Rostedt5072c592008-05-12 21:20:43 +02003773 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003774 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003775 return ret;
3776}
3777
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003778ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003779ftrace_filter_write(struct file *file, const char __user *ubuf,
3780 size_t cnt, loff_t *ppos)
3781{
3782 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3783}
3784
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003785ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003786ftrace_notrace_write(struct file *file, const char __user *ubuf,
3787 size_t cnt, loff_t *ppos)
3788{
3789 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3790}
3791
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003792static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003793ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3794{
3795 struct ftrace_func_entry *entry;
3796
3797 if (!ftrace_location(ip))
3798 return -EINVAL;
3799
3800 if (remove) {
3801 entry = ftrace_lookup_ip(hash, ip);
3802 if (!entry)
3803 return -ENOENT;
3804 free_hash_entry(hash, entry);
3805 return 0;
3806 }
3807
3808 return add_hash_entry(hash, ip);
3809}
3810
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003811static void ftrace_ops_update_code(struct ftrace_ops *ops,
3812 struct ftrace_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003813{
3814 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003815 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003816}
3817
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003818static int
3819ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3820 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003821{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003822 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003823 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003824 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003825 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003826
Steven Rostedt41c52c02008-05-22 11:46:33 -04003827 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003828 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003829
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003830 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003831
Steven Rostedtf45948e2011-05-02 12:29:25 -04003832 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003833 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003834 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003835 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003836
Wang Nanb972cc52014-07-15 08:40:20 +08003837 if (reset)
3838 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3839 else
3840 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3841
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003842 if (!hash) {
3843 ret = -ENOMEM;
3844 goto out_regex_unlock;
3845 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003846
Jiri Olsaac483c42012-01-02 10:04:14 +01003847 if (buf && !ftrace_match_records(hash, buf, len)) {
3848 ret = -EINVAL;
3849 goto out_regex_unlock;
3850 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003851 if (ip) {
3852 ret = ftrace_match_addr(hash, ip, remove);
3853 if (ret < 0)
3854 goto out_regex_unlock;
3855 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003856
3857 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003858 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003859 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003860 if (!ret) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003861 ftrace_ops_update_code(ops, old_hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003862 free_ftrace_hash_rcu(old_hash);
3863 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003864 mutex_unlock(&ftrace_lock);
3865
Jiri Olsaac483c42012-01-02 10:04:14 +01003866 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003867 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003868
3869 free_ftrace_hash(hash);
3870 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003871}
3872
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003873static int
3874ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3875 int reset, int enable)
3876{
3877 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3878}
3879
3880/**
3881 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3882 * @ops - the ops to set the filter with
3883 * @ip - the address to add to or remove from the filter.
3884 * @remove - non zero to remove the ip from the filter
3885 * @reset - non zero to reset all filters before applying this filter.
3886 *
3887 * Filters denote which functions should be enabled when tracing is enabled
3888 * If @ip is NULL, it failes to update filter.
3889 */
3890int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3891 int remove, int reset)
3892{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003893 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003894 return ftrace_set_addr(ops, ip, remove, reset, 1);
3895}
3896EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3897
3898static int
3899ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3900 int reset, int enable)
3901{
3902 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3903}
3904
Steven Rostedt77a2b372008-05-12 21:20:45 +02003905/**
3906 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003907 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003908 * @buf - the string that holds the function filter text.
3909 * @len - the length of the string.
3910 * @reset - non zero to reset all filters before applying this filter.
3911 *
3912 * Filters denote which functions should be enabled when tracing is enabled.
3913 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3914 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003915int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003916 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003917{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003918 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003919 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003920}
Steven Rostedt936e0742011-05-05 22:54:01 -04003921EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003922
Steven Rostedt41c52c02008-05-22 11:46:33 -04003923/**
3924 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003925 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003926 * @buf - the string that holds the function notrace text.
3927 * @len - the length of the string.
3928 * @reset - non zero to reset all filters before applying this filter.
3929 *
3930 * Notrace Filters denote which functions should not be enabled when tracing
3931 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3932 * for tracing.
3933 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003934int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003935 int len, int reset)
3936{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003937 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003938 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003939}
3940EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3941/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003942 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003943 * @buf - the string that holds the function filter text.
3944 * @len - the length of the string.
3945 * @reset - non zero to reset all filters before applying this filter.
3946 *
3947 * Filters denote which functions should be enabled when tracing is enabled.
3948 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3949 */
3950void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3951{
3952 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3953}
3954EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3955
3956/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003957 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003958 * @buf - the string that holds the function notrace text.
3959 * @len - the length of the string.
3960 * @reset - non zero to reset all filters before applying this filter.
3961 *
3962 * Notrace Filters denote which functions should not be enabled when tracing
3963 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3964 * for tracing.
3965 */
3966void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003967{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003968 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003969}
Steven Rostedt936e0742011-05-05 22:54:01 -04003970EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003971
Steven Rostedt2af15d62009-05-28 13:37:24 -04003972/*
3973 * command line interface to allow users to set filters on boot up.
3974 */
3975#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3976static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3977static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3978
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003979/* Used by function selftest to not test if filter is set */
3980bool ftrace_filter_param __initdata;
3981
Steven Rostedt2af15d62009-05-28 13:37:24 -04003982static int __init set_ftrace_notrace(char *str)
3983{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003984 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003985 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003986 return 1;
3987}
3988__setup("ftrace_notrace=", set_ftrace_notrace);
3989
3990static int __init set_ftrace_filter(char *str)
3991{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003992 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003993 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003994 return 1;
3995}
3996__setup("ftrace_filter=", set_ftrace_filter);
3997
Stefan Assmann369bc182009-10-12 22:17:21 +02003998#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003999static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004000static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004001static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004002
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004003static unsigned long save_global_trampoline;
4004static unsigned long save_global_flags;
4005
Stefan Assmann369bc182009-10-12 22:17:21 +02004006static int __init set_graph_function(char *str)
4007{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004008 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004009 return 1;
4010}
4011__setup("ftrace_graph_filter=", set_graph_function);
4012
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004013static int __init set_graph_notrace_function(char *str)
4014{
4015 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4016 return 1;
4017}
4018__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4019
4020static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004021{
4022 int ret;
4023 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004024 unsigned long *table = ftrace_graph_funcs;
4025 int *count = &ftrace_graph_count;
4026
4027 if (!enable) {
4028 table = ftrace_graph_notrace_funcs;
4029 count = &ftrace_graph_notrace_count;
4030 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004031
4032 while (buf) {
4033 func = strsep(&buf, ",");
4034 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004035 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004036 if (ret)
4037 printk(KERN_DEBUG "ftrace: function %s not "
4038 "traceable\n", func);
4039 }
4040}
4041#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4042
Steven Rostedt2a85a372011-12-19 21:57:44 -05004043void __init
4044ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004045{
4046 char *func;
4047
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004048 ftrace_ops_init(ops);
4049
Steven Rostedt2af15d62009-05-28 13:37:24 -04004050 while (buf) {
4051 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004052 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004053 }
4054}
4055
4056static void __init set_ftrace_early_filters(void)
4057{
4058 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004059 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004060 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004061 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004062#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4063 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004064 set_ftrace_early_graph(ftrace_graph_buf, 1);
4065 if (ftrace_graph_notrace_buf[0])
4066 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004067#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004068}
4069
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004070int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004071{
4072 struct seq_file *m = (struct seq_file *)file->private_data;
4073 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004074 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004075 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004076 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004077 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004078 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004079
Steven Rostedt5072c592008-05-12 21:20:43 +02004080 if (file->f_mode & FMODE_READ) {
4081 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004082 seq_release(inode, file);
4083 } else
4084 iter = file->private_data;
4085
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004086 parser = &iter->parser;
4087 if (trace_parser_loaded(parser)) {
4088 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004089 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004090 }
4091
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004092 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004093
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004094 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004095
Steven Rostedt058e2972011-04-29 22:35:33 -04004096 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004097 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4098
4099 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004100 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004101 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004102 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004103
Steven Rostedt058e2972011-04-29 22:35:33 -04004104 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004105 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004106 ret = ftrace_hash_move(iter->ops, filter_hash,
4107 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004108 if (!ret) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004109 ftrace_ops_update_code(iter->ops, old_hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004110 free_ftrace_hash_rcu(old_hash);
4111 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004112 mutex_unlock(&ftrace_lock);
4113 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004114
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004115 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004116 free_ftrace_hash(iter->hash);
4117 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004118
Steven Rostedt5072c592008-05-12 21:20:43 +02004119 return 0;
4120}
4121
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004122static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004123 .open = ftrace_avail_open,
4124 .read = seq_read,
4125 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004126 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004127};
4128
Steven Rostedt647bcd02011-05-03 14:39:21 -04004129static const struct file_operations ftrace_enabled_fops = {
4130 .open = ftrace_enabled_open,
4131 .read = seq_read,
4132 .llseek = seq_lseek,
4133 .release = seq_release_private,
4134};
4135
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004136static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004137 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004138 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004139 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004140 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004141 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004142};
4143
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004144static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004145 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004146 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004147 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004148 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004149 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004150};
4151
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004152#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4153
4154static DEFINE_MUTEX(graph_lock);
4155
4156int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004157int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004158unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004159unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004160
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004161struct ftrace_graph_data {
4162 unsigned long *table;
4163 size_t size;
4164 int *count;
4165 const struct seq_operations *seq_ops;
4166};
4167
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004168static void *
Li Zefan85951842009-06-24 09:54:00 +08004169__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004170{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004171 struct ftrace_graph_data *fgd = m->private;
4172
4173 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004174 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004175 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004176}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004177
Li Zefan85951842009-06-24 09:54:00 +08004178static void *
4179g_next(struct seq_file *m, void *v, loff_t *pos)
4180{
4181 (*pos)++;
4182 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004183}
4184
4185static void *g_start(struct seq_file *m, loff_t *pos)
4186{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004187 struct ftrace_graph_data *fgd = m->private;
4188
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004189 mutex_lock(&graph_lock);
4190
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004191 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004192 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004193 return (void *)1;
4194
Li Zefan85951842009-06-24 09:54:00 +08004195 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004196}
4197
4198static void g_stop(struct seq_file *m, void *p)
4199{
4200 mutex_unlock(&graph_lock);
4201}
4202
4203static int g_show(struct seq_file *m, void *v)
4204{
4205 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004206
4207 if (!ptr)
4208 return 0;
4209
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004210 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004211 struct ftrace_graph_data *fgd = m->private;
4212
4213 if (fgd->table == ftrace_graph_funcs)
4214 seq_printf(m, "#### all functions enabled ####\n");
4215 else
4216 seq_printf(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004217 return 0;
4218 }
4219
Steven Rostedtb375a112009-09-17 00:05:58 -04004220 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004221
4222 return 0;
4223}
4224
James Morris88e9d342009-09-22 16:43:43 -07004225static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004226 .start = g_start,
4227 .next = g_next,
4228 .stop = g_stop,
4229 .show = g_show,
4230};
4231
4232static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004233__ftrace_graph_open(struct inode *inode, struct file *file,
4234 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004235{
4236 int ret = 0;
4237
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004238 mutex_lock(&graph_lock);
4239 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004240 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004241 *fgd->count = 0;
4242 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004243 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004244 mutex_unlock(&graph_lock);
4245
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004246 if (file->f_mode & FMODE_READ) {
4247 ret = seq_open(file, fgd->seq_ops);
4248 if (!ret) {
4249 struct seq_file *m = file->private_data;
4250 m->private = fgd;
4251 }
4252 } else
4253 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004254
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004255 return ret;
4256}
4257
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004258static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004259ftrace_graph_open(struct inode *inode, struct file *file)
4260{
4261 struct ftrace_graph_data *fgd;
4262
4263 if (unlikely(ftrace_disabled))
4264 return -ENODEV;
4265
4266 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4267 if (fgd == NULL)
4268 return -ENOMEM;
4269
4270 fgd->table = ftrace_graph_funcs;
4271 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4272 fgd->count = &ftrace_graph_count;
4273 fgd->seq_ops = &ftrace_graph_seq_ops;
4274
4275 return __ftrace_graph_open(inode, file, fgd);
4276}
4277
4278static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004279ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4280{
4281 struct ftrace_graph_data *fgd;
4282
4283 if (unlikely(ftrace_disabled))
4284 return -ENODEV;
4285
4286 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4287 if (fgd == NULL)
4288 return -ENOMEM;
4289
4290 fgd->table = ftrace_graph_notrace_funcs;
4291 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4292 fgd->count = &ftrace_graph_notrace_count;
4293 fgd->seq_ops = &ftrace_graph_seq_ops;
4294
4295 return __ftrace_graph_open(inode, file, fgd);
4296}
4297
4298static int
Li Zefan87827112009-07-23 11:29:11 +08004299ftrace_graph_release(struct inode *inode, struct file *file)
4300{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004301 if (file->f_mode & FMODE_READ) {
4302 struct seq_file *m = file->private_data;
4303
4304 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004305 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004306 } else {
4307 kfree(file->private_data);
4308 }
4309
Li Zefan87827112009-07-23 11:29:11 +08004310 return 0;
4311}
4312
4313static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004314ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004315{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004316 struct dyn_ftrace *rec;
4317 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004318 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004319 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004320 int type, not;
4321 char *search;
4322 bool exists;
4323 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004324
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004325 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004326 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004327 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004328 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004329
4330 search_len = strlen(search);
4331
Steven Rostedt52baf112009-02-14 01:15:39 -05004332 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004333
4334 if (unlikely(ftrace_disabled)) {
4335 mutex_unlock(&ftrace_lock);
4336 return -ENODEV;
4337 }
4338
Steven Rostedt265c8312009-02-13 12:43:56 -05004339 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004340
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004341 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004342 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004343 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004344 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004345 if (array[i] == rec->ip) {
4346 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004347 break;
4348 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004349 }
4350
4351 if (!not) {
4352 fail = 0;
4353 if (!exists) {
4354 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004355 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004356 goto out;
4357 }
4358 } else {
4359 if (exists) {
4360 array[i] = array[--(*idx)];
4361 array[*idx] = 0;
4362 fail = 0;
4363 }
4364 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004365 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004366 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004367out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004368 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004369
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004370 if (fail)
4371 return -EINVAL;
4372
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004373 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004374}
4375
4376static ssize_t
4377ftrace_graph_write(struct file *file, const char __user *ubuf,
4378 size_t cnt, loff_t *ppos)
4379{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004380 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004381 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004382 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004383
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004384 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004385 return 0;
4386
Namhyung Kim6a101082013-10-14 17:24:25 +09004387 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4388 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004389
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004390 read = trace_get_user(&parser, ubuf, cnt, ppos);
4391
Li Zefan4ba79782009-09-22 13:52:20 +08004392 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004393 parser.buffer[parser.idx] = 0;
4394
Namhyung Kim6a101082013-10-14 17:24:25 +09004395 mutex_lock(&graph_lock);
4396
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004397 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004398 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4399 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004400
4401 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004402 }
4403
Namhyung Kim6a101082013-10-14 17:24:25 +09004404 if (!ret)
4405 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004406
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004407 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004408
4409 return ret;
4410}
4411
4412static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004413 .open = ftrace_graph_open,
4414 .read = seq_read,
4415 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004416 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004417 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004418};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004419
4420static const struct file_operations ftrace_graph_notrace_fops = {
4421 .open = ftrace_graph_notrace_open,
4422 .read = seq_read,
4423 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004424 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004425 .release = ftrace_graph_release,
4426};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004427#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4428
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004429void ftrace_create_filter_files(struct ftrace_ops *ops,
4430 struct dentry *parent)
4431{
4432
4433 trace_create_file("set_ftrace_filter", 0644, parent,
4434 ops, &ftrace_filter_fops);
4435
4436 trace_create_file("set_ftrace_notrace", 0644, parent,
4437 ops, &ftrace_notrace_fops);
4438}
4439
4440/*
4441 * The name "destroy_filter_files" is really a misnomer. Although
4442 * in the future, it may actualy delete the files, but this is
4443 * really intended to make sure the ops passed in are disabled
4444 * and that when this function returns, the caller is free to
4445 * free the ops.
4446 *
4447 * The "destroy" name is only to match the "create" name that this
4448 * should be paired with.
4449 */
4450void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4451{
4452 mutex_lock(&ftrace_lock);
4453 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4454 ftrace_shutdown(ops, 0);
4455 ops->flags |= FTRACE_OPS_FL_DELETED;
4456 mutex_unlock(&ftrace_lock);
4457}
4458
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004459static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004460{
Steven Rostedt5072c592008-05-12 21:20:43 +02004461
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004462 trace_create_file("available_filter_functions", 0444,
4463 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004464
Steven Rostedt647bcd02011-05-03 14:39:21 -04004465 trace_create_file("enabled_functions", 0444,
4466 d_tracer, NULL, &ftrace_enabled_fops);
4467
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004468 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004469
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004470#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004471 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004472 NULL,
4473 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004474 trace_create_file("set_graph_notrace", 0444, d_tracer,
4475 NULL,
4476 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004477#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4478
Steven Rostedt5072c592008-05-12 21:20:43 +02004479 return 0;
4480}
4481
Steven Rostedt9fd49322012-04-24 22:32:06 -04004482static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004483{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004484 const unsigned long *ipa = a;
4485 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004486
Steven Rostedt9fd49322012-04-24 22:32:06 -04004487 if (*ipa > *ipb)
4488 return 1;
4489 if (*ipa < *ipb)
4490 return -1;
4491 return 0;
4492}
4493
4494static void ftrace_swap_ips(void *a, void *b, int size)
4495{
4496 unsigned long *ipa = a;
4497 unsigned long *ipb = b;
4498 unsigned long t;
4499
4500 t = *ipa;
4501 *ipa = *ipb;
4502 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004503}
4504
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004505static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004506 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004507 unsigned long *end)
4508{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004509 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004510 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004511 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004512 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004513 unsigned long *p;
4514 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004515 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004516 int ret = -ENOMEM;
4517
4518 count = end - start;
4519
4520 if (!count)
4521 return 0;
4522
Steven Rostedt9fd49322012-04-24 22:32:06 -04004523 sort(start, count, sizeof(*start),
4524 ftrace_cmp_ips, ftrace_swap_ips);
4525
Steven Rostedt706c81f2012-04-24 23:45:26 -04004526 start_pg = ftrace_allocate_pages(count);
4527 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004528 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004529
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004530 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004531
Steven Rostedt32082302011-12-16 14:42:37 -05004532 /*
4533 * Core and each module needs their own pages, as
4534 * modules will free them when they are removed.
4535 * Force a new page to be allocated for modules.
4536 */
Steven Rostedta7900872011-12-16 16:23:44 -05004537 if (!mod) {
4538 WARN_ON(ftrace_pages || ftrace_pages_start);
4539 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004540 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004541 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004542 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004543 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004544
Steven Rostedta7900872011-12-16 16:23:44 -05004545 if (WARN_ON(ftrace_pages->next)) {
4546 /* Hmm, we have free pages? */
4547 while (ftrace_pages->next)
4548 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004549 }
Steven Rostedta7900872011-12-16 16:23:44 -05004550
Steven Rostedt706c81f2012-04-24 23:45:26 -04004551 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004552 }
4553
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004554 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004555 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004556 while (p < end) {
4557 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004558 /*
4559 * Some architecture linkers will pad between
4560 * the different mcount_loc sections of different
4561 * object files to satisfy alignments.
4562 * Skip any NULL pointers.
4563 */
4564 if (!addr)
4565 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004566
4567 if (pg->index == pg->size) {
4568 /* We should have allocated enough */
4569 if (WARN_ON(!pg->next))
4570 break;
4571 pg = pg->next;
4572 }
4573
4574 rec = &pg->records[pg->index++];
4575 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004576 }
4577
Steven Rostedt706c81f2012-04-24 23:45:26 -04004578 /* We should have used all pages */
4579 WARN_ON(pg->next);
4580
4581 /* Assign the last page to ftrace_pages */
4582 ftrace_pages = pg;
4583
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004584 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004585 * We only need to disable interrupts on start up
4586 * because we are modifying code that an interrupt
4587 * may execute, and the modification is not atomic.
4588 * But for modules, nothing runs the code we modify
4589 * until we are finished with it, and there's no
4590 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004591 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004592 if (!mod)
4593 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004594 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004595 if (!mod)
4596 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004597 ret = 0;
4598 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004599 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004600
Steven Rostedta7900872011-12-16 16:23:44 -05004601 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004602}
4603
Steven Rostedt93eb6772009-04-15 13:24:06 -04004604#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004605
4606#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4607
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004608void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004609{
4610 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004611 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004612 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004613 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004614
Steven Rostedt93eb6772009-04-15 13:24:06 -04004615 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004616
4617 if (ftrace_disabled)
4618 goto out_unlock;
4619
Steven Rostedt32082302011-12-16 14:42:37 -05004620 /*
4621 * Each module has its own ftrace_pages, remove
4622 * them from the list.
4623 */
4624 last_pg = &ftrace_pages_start;
4625 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4626 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004627 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004628 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004629 * As core pages are first, the first
4630 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004631 */
Steven Rostedt32082302011-12-16 14:42:37 -05004632 if (WARN_ON(pg == ftrace_pages_start))
4633 goto out_unlock;
4634
4635 /* Check if we are deleting the last page */
4636 if (pg == ftrace_pages)
4637 ftrace_pages = next_to_ftrace_page(last_pg);
4638
4639 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004640 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4641 free_pages((unsigned long)pg->records, order);
4642 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004643 } else
4644 last_pg = &pg->next;
4645 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004646 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004647 mutex_unlock(&ftrace_lock);
4648}
4649
4650static void ftrace_init_module(struct module *mod,
4651 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004652{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004653 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004654 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004655 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004656}
4657
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004658void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004659{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004660 ftrace_init_module(mod, mod->ftrace_callsites,
4661 mod->ftrace_callsites +
4662 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004663}
4664
4665static int ftrace_module_notify_exit(struct notifier_block *self,
4666 unsigned long val, void *data)
4667{
4668 struct module *mod = data;
4669
4670 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004671 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004672
4673 return 0;
4674}
4675#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004676static int ftrace_module_notify_exit(struct notifier_block *self,
4677 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004678{
4679 return 0;
4680}
4681#endif /* CONFIG_MODULES */
4682
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004683struct notifier_block ftrace_module_exit_nb = {
4684 .notifier_call = ftrace_module_notify_exit,
4685 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4686};
4687
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004688void __init ftrace_init(void)
4689{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004690 extern unsigned long __start_mcount_loc[];
4691 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004692 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004693 int ret;
4694
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004695 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004696 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004697 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004698 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004699 goto failed;
4700
4701 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004702 if (!count) {
4703 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004704 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004705 }
4706
4707 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4708 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004709
4710 last_ftrace_enabled = ftrace_enabled = 1;
4711
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004712 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004713 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004714 __stop_mcount_loc);
4715
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004716 ret = register_module_notifier(&ftrace_module_exit_nb);
4717 if (ret)
4718 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004719
Steven Rostedt2af15d62009-05-28 13:37:24 -04004720 set_ftrace_early_filters();
4721
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004722 return;
4723 failed:
4724 ftrace_disabled = 1;
4725}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004726
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004727/* Do nothing if arch does not support this */
4728void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
4729{
4730}
4731
4732static void ftrace_update_trampoline(struct ftrace_ops *ops)
4733{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04004734
4735/*
4736 * Currently there's no safe way to free a trampoline when the kernel
4737 * is configured with PREEMPT. That is because a task could be preempted
4738 * when it jumped to the trampoline, it may be preempted for a long time
4739 * depending on the system load, and currently there's no way to know
4740 * when it will be off the trampoline. If the trampoline is freed
4741 * too early, when the task runs again, it will be executing on freed
4742 * memory and crash.
4743 */
4744#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004745 /* Currently, only non dynamic ops can have a trampoline */
4746 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
4747 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04004748#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004749
4750 arch_ftrace_update_trampoline(ops);
4751}
4752
Steven Rostedt3d083392008-05-12 21:20:42 +02004753#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004754
Steven Rostedt2b499382011-05-03 22:49:52 -04004755static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004756 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004757 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004758};
4759
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004760static int __init ftrace_nodyn_init(void)
4761{
4762 ftrace_enabled = 1;
4763 return 0;
4764}
Steven Rostedt6f415672012-10-05 12:13:07 -04004765core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004766
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004767static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4768static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04004769static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004770/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004771# define ftrace_startup(ops, command) \
4772 ({ \
4773 int ___ret = __register_ftrace_function(ops); \
4774 if (!___ret) \
4775 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4776 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004777 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004778# define ftrace_shutdown(ops, command) \
4779 ({ \
4780 int ___ret = __unregister_ftrace_function(ops); \
4781 if (!___ret) \
4782 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4783 ___ret; \
4784 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004785
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004786# define ftrace_startup_sysctl() do { } while (0)
4787# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004788
4789static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004790ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004791{
4792 return 1;
4793}
4794
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004795static void ftrace_update_trampoline(struct ftrace_ops *ops)
4796{
4797}
4798
Steven Rostedt3d083392008-05-12 21:20:42 +02004799#endif /* CONFIG_DYNAMIC_FTRACE */
4800
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004801__init void ftrace_init_global_array_ops(struct trace_array *tr)
4802{
4803 tr->ops = &global_ops;
4804 tr->ops->private = tr;
4805}
4806
4807void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4808{
4809 /* If we filter on pids, update to use the pid function */
4810 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4811 if (WARN_ON(tr->ops->func != ftrace_stub))
4812 printk("ftrace ops had %pS for function\n",
4813 tr->ops->func);
4814 /* Only the top level instance does pid tracing */
4815 if (!list_empty(&ftrace_pids)) {
4816 set_ftrace_pid_function(func);
4817 func = ftrace_pid_func;
4818 }
4819 }
4820 tr->ops->func = func;
4821 tr->ops->private = tr;
4822}
4823
4824void ftrace_reset_array_ops(struct trace_array *tr)
4825{
4826 tr->ops->func = ftrace_stub;
4827}
4828
Steven Rostedtb8489142011-05-04 09:27:52 -04004829static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004830ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004831 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004832{
Jiri Olsae2484912012-02-15 15:51:48 +01004833 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4834 return;
4835
4836 /*
4837 * Some of the ops may be dynamically allocated,
4838 * they must be freed after a synchronize_sched().
4839 */
4840 preempt_disable_notrace();
4841 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004842
4843 /*
4844 * Control funcs (perf) uses RCU. Only trace if
4845 * RCU is currently active.
4846 */
4847 if (!rcu_is_watching())
4848 goto out;
4849
Steven Rostedt0a016402012-11-02 17:03:03 -04004850 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004851 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4852 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004853 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004854 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004855 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004856 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004857 trace_recursion_clear(TRACE_CONTROL_BIT);
4858 preempt_enable_notrace();
4859}
4860
4861static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004862 .func = ftrace_ops_control_func,
4863 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004864 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004865};
4866
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004867static inline void
4868__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004869 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004870{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004871 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004872 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004873
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004874 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4875 if (bit < 0)
4876 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004877
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004878 /*
4879 * Some of the ops may be dynamically allocated,
4880 * they must be freed after a synchronize_sched().
4881 */
4882 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004883 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004884 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004885 if (FTRACE_WARN_ON(!op->func)) {
4886 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004887 goto out;
4888 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004889 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004890 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004891 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004892out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004893 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004894 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004895}
4896
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004897/*
4898 * Some archs only support passing ip and parent_ip. Even though
4899 * the list function ignores the op parameter, we do not want any
4900 * C side effects, where a function is called without the caller
4901 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004902 * Archs are to support both the regs and ftrace_ops at the same time.
4903 * If they support ftrace_ops, it is assumed they support regs.
4904 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004905 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4906 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004907 * An architecture can pass partial regs with ftrace_ops and still
4908 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004909 */
4910#if ARCH_SUPPORTS_FTRACE_OPS
4911static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004912 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004913{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004914 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004915}
4916#else
4917static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4918{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004919 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004920}
4921#endif
4922
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04004923/*
4924 * If there's only one function registered but it does not support
4925 * recursion, this function will be called by the mcount trampoline.
4926 * This function will handle recursion protection.
4927 */
4928static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
4929 struct ftrace_ops *op, struct pt_regs *regs)
4930{
4931 int bit;
4932
4933 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4934 if (bit < 0)
4935 return;
4936
4937 op->func(ip, parent_ip, op, regs);
4938
4939 trace_clear_recursion(bit);
4940}
4941
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04004942/**
4943 * ftrace_ops_get_func - get the function a trampoline should call
4944 * @ops: the ops to get the function for
4945 *
4946 * Normally the mcount trampoline will call the ops->func, but there
4947 * are times that it should not. For example, if the ops does not
4948 * have its own recursion protection, then it should call the
4949 * ftrace_ops_recurs_func() instead.
4950 *
4951 * Returns the function that the trampoline should call for @ops.
4952 */
4953ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
4954{
4955 /*
4956 * If this is a dynamic ops or we force list func,
4957 * then it needs to call the list anyway.
4958 */
4959 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
4960 return ftrace_ops_list_func;
4961
4962 /*
4963 * If the func handles its own recursion, call it directly.
4964 * Otherwise call the recursion protected function that
4965 * will call the ftrace ops function.
4966 */
4967 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
4968 return ftrace_ops_recurs_func;
4969
4970 return ops->func;
4971}
4972
Steven Rostedte32d8952008-12-04 00:26:41 -05004973static void clear_ftrace_swapper(void)
4974{
4975 struct task_struct *p;
4976 int cpu;
4977
4978 get_online_cpus();
4979 for_each_online_cpu(cpu) {
4980 p = idle_task(cpu);
4981 clear_tsk_trace_trace(p);
4982 }
4983 put_online_cpus();
4984}
4985
4986static void set_ftrace_swapper(void)
4987{
4988 struct task_struct *p;
4989 int cpu;
4990
4991 get_online_cpus();
4992 for_each_online_cpu(cpu) {
4993 p = idle_task(cpu);
4994 set_tsk_trace_trace(p);
4995 }
4996 put_online_cpus();
4997}
4998
4999static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005000{
5001 struct task_struct *p;
5002
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005003 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05005004 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05005005 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05005006 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005007 rcu_read_unlock();
5008
Steven Rostedte32d8952008-12-04 00:26:41 -05005009 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005010}
5011
Steven Rostedte32d8952008-12-04 00:26:41 -05005012static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005013{
5014 struct task_struct *p;
5015
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005016 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005017 do_each_pid_task(pid, PIDTYPE_PID, p) {
5018 set_tsk_trace_trace(p);
5019 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005020 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005021}
5022
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005023static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005024{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005025 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005026 clear_ftrace_swapper();
5027 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005028 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05005029}
5030
5031static void set_ftrace_pid_task(struct pid *pid)
5032{
5033 if (pid == ftrace_swapper_pid)
5034 set_ftrace_swapper();
5035 else
5036 set_ftrace_pid(pid);
5037}
5038
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005039static int ftrace_pid_add(int p)
5040{
5041 struct pid *pid;
5042 struct ftrace_pid *fpid;
5043 int ret = -EINVAL;
5044
5045 mutex_lock(&ftrace_lock);
5046
5047 if (!p)
5048 pid = ftrace_swapper_pid;
5049 else
5050 pid = find_get_pid(p);
5051
5052 if (!pid)
5053 goto out;
5054
5055 ret = 0;
5056
5057 list_for_each_entry(fpid, &ftrace_pids, list)
5058 if (fpid->pid == pid)
5059 goto out_put;
5060
5061 ret = -ENOMEM;
5062
5063 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5064 if (!fpid)
5065 goto out_put;
5066
5067 list_add(&fpid->list, &ftrace_pids);
5068 fpid->pid = pid;
5069
5070 set_ftrace_pid_task(pid);
5071
5072 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005073
5074 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005075
5076 mutex_unlock(&ftrace_lock);
5077 return 0;
5078
5079out_put:
5080 if (pid != ftrace_swapper_pid)
5081 put_pid(pid);
5082
5083out:
5084 mutex_unlock(&ftrace_lock);
5085 return ret;
5086}
5087
5088static void ftrace_pid_reset(void)
5089{
5090 struct ftrace_pid *fpid, *safe;
5091
5092 mutex_lock(&ftrace_lock);
5093 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5094 struct pid *pid = fpid->pid;
5095
5096 clear_ftrace_pid_task(pid);
5097
5098 list_del(&fpid->list);
5099 kfree(fpid);
5100 }
5101
5102 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005103 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005104
5105 mutex_unlock(&ftrace_lock);
5106}
5107
5108static void *fpid_start(struct seq_file *m, loff_t *pos)
5109{
5110 mutex_lock(&ftrace_lock);
5111
5112 if (list_empty(&ftrace_pids) && (!*pos))
5113 return (void *) 1;
5114
5115 return seq_list_start(&ftrace_pids, *pos);
5116}
5117
5118static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5119{
5120 if (v == (void *)1)
5121 return NULL;
5122
5123 return seq_list_next(v, &ftrace_pids, pos);
5124}
5125
5126static void fpid_stop(struct seq_file *m, void *p)
5127{
5128 mutex_unlock(&ftrace_lock);
5129}
5130
5131static int fpid_show(struct seq_file *m, void *v)
5132{
5133 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5134
5135 if (v == (void *)1) {
5136 seq_printf(m, "no pid\n");
5137 return 0;
5138 }
5139
5140 if (fpid->pid == ftrace_swapper_pid)
5141 seq_printf(m, "swapper tasks\n");
5142 else
5143 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5144
5145 return 0;
5146}
5147
5148static const struct seq_operations ftrace_pid_sops = {
5149 .start = fpid_start,
5150 .next = fpid_next,
5151 .stop = fpid_stop,
5152 .show = fpid_show,
5153};
5154
5155static int
5156ftrace_pid_open(struct inode *inode, struct file *file)
5157{
5158 int ret = 0;
5159
5160 if ((file->f_mode & FMODE_WRITE) &&
5161 (file->f_flags & O_TRUNC))
5162 ftrace_pid_reset();
5163
5164 if (file->f_mode & FMODE_READ)
5165 ret = seq_open(file, &ftrace_pid_sops);
5166
5167 return ret;
5168}
5169
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005170static ssize_t
5171ftrace_pid_write(struct file *filp, const char __user *ubuf,
5172 size_t cnt, loff_t *ppos)
5173{
Ingo Molnar457dc922009-11-23 11:03:28 +01005174 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005175 long val;
5176 int ret;
5177
5178 if (cnt >= sizeof(buf))
5179 return -EINVAL;
5180
5181 if (copy_from_user(&buf, ubuf, cnt))
5182 return -EFAULT;
5183
5184 buf[cnt] = 0;
5185
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005186 /*
5187 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5188 * to clean the filter quietly.
5189 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005190 tmp = strstrip(buf);
5191 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005192 return 1;
5193
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005194 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005195 if (ret < 0)
5196 return ret;
5197
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005198 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005199
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005200 return ret ? ret : cnt;
5201}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005202
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005203static int
5204ftrace_pid_release(struct inode *inode, struct file *file)
5205{
5206 if (file->f_mode & FMODE_READ)
5207 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005208
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005209 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005210}
5211
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005212static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005213 .open = ftrace_pid_open,
5214 .write = ftrace_pid_write,
5215 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005216 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005217 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005218};
5219
5220static __init int ftrace_init_debugfs(void)
5221{
5222 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005223
5224 d_tracer = tracing_init_dentry();
5225 if (!d_tracer)
5226 return 0;
5227
5228 ftrace_init_dyn_debugfs(d_tracer);
5229
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005230 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5231 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005232
5233 ftrace_profile_debugfs(d_tracer);
5234
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005235 return 0;
5236}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005237fs_initcall(ftrace_init_debugfs);
5238
Steven Rostedt3d083392008-05-12 21:20:42 +02005239/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005240 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005241 *
5242 * This function should be used by panic code. It stops ftrace
5243 * but in a not so nice way. If you need to simply kill ftrace
5244 * from a non-atomic section, use ftrace_kill.
5245 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005246void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005247{
5248 ftrace_disabled = 1;
5249 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005250 clear_ftrace_function();
5251}
5252
5253/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005254 * Test if ftrace is dead or not.
5255 */
5256int ftrace_is_dead(void)
5257{
5258 return ftrace_disabled;
5259}
5260
5261/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005262 * register_ftrace_function - register a function for profiling
5263 * @ops - ops structure that holds the function for profiling.
5264 *
5265 * Register a function to be called by all functions in the
5266 * kernel.
5267 *
5268 * Note: @ops->func and all the functions it calls must be labeled
5269 * with "notrace", otherwise it will go into a
5270 * recursive loop.
5271 */
5272int register_ftrace_function(struct ftrace_ops *ops)
5273{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005274 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005275
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005276 ftrace_ops_init(ops);
5277
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005278 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005279
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005280 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005281
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005282 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005283
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005284 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005285}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005286EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005287
5288/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005289 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005290 * @ops - ops structure that holds the function to unregister
5291 *
5292 * Unregister a function that was added to be called by ftrace profiling.
5293 */
5294int unregister_ftrace_function(struct ftrace_ops *ops)
5295{
5296 int ret;
5297
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005298 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005299 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005300 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005301
5302 return ret;
5303}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005304EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005305
Ingo Molnare309b412008-05-12 21:20:51 +02005306int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005307ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005308 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005309 loff_t *ppos)
5310{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005311 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005312
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005313 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005314
Steven Rostedt45a4a232011-04-21 23:16:46 -04005315 if (unlikely(ftrace_disabled))
5316 goto out;
5317
5318 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005319
Li Zefana32c7762009-06-26 16:55:51 +08005320 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005321 goto out;
5322
Li Zefana32c7762009-06-26 16:55:51 +08005323 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005324
5325 if (ftrace_enabled) {
5326
5327 ftrace_startup_sysctl();
5328
5329 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005330 if (ftrace_ops_list != &ftrace_list_end)
5331 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005332
5333 } else {
5334 /* stopping ftrace calls (just send to ftrace_stub) */
5335 ftrace_trace_function = ftrace_stub;
5336
5337 ftrace_shutdown_sysctl();
5338 }
5339
5340 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005341 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005342 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005343}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005344
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005345#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005346
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005347static struct ftrace_ops graph_ops = {
5348 .func = ftrace_stub,
5349 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5350 FTRACE_OPS_FL_INITIALIZED |
5351 FTRACE_OPS_FL_STUB,
5352#ifdef FTRACE_GRAPH_TRAMP_ADDR
5353 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5354#endif
5355 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5356};
5357
Steven Rostedt597af812009-04-03 15:24:12 -04005358static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005359
Steven Rostedte49dc192008-12-02 23:50:05 -05005360int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5361{
5362 return 0;
5363}
5364
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005365/* The callbacks that hook a function */
5366trace_func_graph_ret_t ftrace_graph_return =
5367 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005368trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005369static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005370
5371/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5372static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5373{
5374 int i;
5375 int ret = 0;
5376 unsigned long flags;
5377 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5378 struct task_struct *g, *t;
5379
5380 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5381 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5382 * sizeof(struct ftrace_ret_stack),
5383 GFP_KERNEL);
5384 if (!ret_stack_list[i]) {
5385 start = 0;
5386 end = i;
5387 ret = -ENOMEM;
5388 goto free;
5389 }
5390 }
5391
5392 read_lock_irqsave(&tasklist_lock, flags);
5393 do_each_thread(g, t) {
5394 if (start == end) {
5395 ret = -EAGAIN;
5396 goto unlock;
5397 }
5398
5399 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005400 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005401 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005402 t->curr_ret_stack = -1;
5403 /* Make sure the tasks see the -1 first: */
5404 smp_wmb();
5405 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005406 }
5407 } while_each_thread(g, t);
5408
5409unlock:
5410 read_unlock_irqrestore(&tasklist_lock, flags);
5411free:
5412 for (i = start; i < end; i++)
5413 kfree(ret_stack_list[i]);
5414 return ret;
5415}
5416
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005417static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005418ftrace_graph_probe_sched_switch(void *ignore,
5419 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005420{
5421 unsigned long long timestamp;
5422 int index;
5423
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005424 /*
5425 * Does the user want to count the time a function was asleep.
5426 * If so, do not update the time stamps.
5427 */
5428 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5429 return;
5430
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005431 timestamp = trace_clock_local();
5432
5433 prev->ftrace_timestamp = timestamp;
5434
5435 /* only process tasks that we timestamped */
5436 if (!next->ftrace_timestamp)
5437 return;
5438
5439 /*
5440 * Update all the counters in next to make up for the
5441 * time next was sleeping.
5442 */
5443 timestamp -= next->ftrace_timestamp;
5444
5445 for (index = next->curr_ret_stack; index >= 0; index--)
5446 next->ret_stack[index].calltime += timestamp;
5447}
5448
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005449/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005450static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005451{
5452 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005453 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005454
5455 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5456 sizeof(struct ftrace_ret_stack *),
5457 GFP_KERNEL);
5458
5459 if (!ret_stack_list)
5460 return -ENOMEM;
5461
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005462 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005463 for_each_online_cpu(cpu) {
5464 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005465 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005466 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005467
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005468 do {
5469 ret = alloc_retstack_tasklist(ret_stack_list);
5470 } while (ret == -EAGAIN);
5471
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005472 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005473 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005474 if (ret)
5475 pr_info("ftrace_graph: Couldn't activate tracepoint"
5476 " probe to kernel_sched_switch\n");
5477 }
5478
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005479 kfree(ret_stack_list);
5480 return ret;
5481}
5482
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005483/*
5484 * Hibernation protection.
5485 * The state of the current task is too much unstable during
5486 * suspend/restore to disk. We want to protect against that.
5487 */
5488static int
5489ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5490 void *unused)
5491{
5492 switch (state) {
5493 case PM_HIBERNATION_PREPARE:
5494 pause_graph_tracing();
5495 break;
5496
5497 case PM_POST_HIBERNATION:
5498 unpause_graph_tracing();
5499 break;
5500 }
5501 return NOTIFY_DONE;
5502}
5503
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005504static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5505{
5506 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5507 return 0;
5508 return __ftrace_graph_entry(trace);
5509}
5510
5511/*
5512 * The function graph tracer should only trace the functions defined
5513 * by set_ftrace_filter and set_ftrace_notrace. If another function
5514 * tracer ops is registered, the graph tracer requires testing the
5515 * function against the global ops, and not just trace any function
5516 * that any ftrace_ops registered.
5517 */
5518static void update_function_graph_func(void)
5519{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005520 struct ftrace_ops *op;
5521 bool do_test = false;
5522
5523 /*
5524 * The graph and global ops share the same set of functions
5525 * to test. If any other ops is on the list, then
5526 * the graph tracing needs to test if its the function
5527 * it should call.
5528 */
5529 do_for_each_ftrace_op(op, ftrace_ops_list) {
5530 if (op != &global_ops && op != &graph_ops &&
5531 op != &ftrace_list_end) {
5532 do_test = true;
5533 /* in double loop, break out with goto */
5534 goto out;
5535 }
5536 } while_for_each_ftrace_op(op);
5537 out:
5538 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005539 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005540 else
5541 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005542}
5543
Mathias Krause8275f692014-03-30 15:31:50 +02005544static struct notifier_block ftrace_suspend_notifier = {
5545 .notifier_call = ftrace_suspend_notifier_call,
5546};
5547
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005548int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5549 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005550{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005551 int ret = 0;
5552
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005553 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005554
Steven Rostedt05ce5812009-03-24 00:18:31 -04005555 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005556 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005557 ret = -EBUSY;
5558 goto out;
5559 }
5560
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005561 register_pm_notifier(&ftrace_suspend_notifier);
5562
Steven Rostedt597af812009-04-03 15:24:12 -04005563 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005564 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005565 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005566 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005567 goto out;
5568 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005569
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005570 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005571
5572 /*
5573 * Update the indirect function to the entryfunc, and the
5574 * function that gets called to the entry_test first. Then
5575 * call the update fgraph entry function to determine if
5576 * the entryfunc should be called directly or not.
5577 */
5578 __ftrace_graph_entry = entryfunc;
5579 ftrace_graph_entry = ftrace_graph_entry_test;
5580 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005581
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005582 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005583out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005584 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005585 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005586}
5587
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005588void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005589{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005590 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005591
Steven Rostedt597af812009-04-03 15:24:12 -04005592 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005593 goto out;
5594
Steven Rostedt597af812009-04-03 15:24:12 -04005595 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005596 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005597 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005598 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005599 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005600 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005601 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005602
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005603#ifdef CONFIG_DYNAMIC_FTRACE
5604 /*
5605 * Function graph does not allocate the trampoline, but
5606 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5607 * if one was used.
5608 */
5609 global_ops.trampoline = save_global_trampoline;
5610 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5611 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5612#endif
5613
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005614 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005615 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005616}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005617
Steven Rostedt868baf02011-02-10 21:26:13 -05005618static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5619
5620static void
5621graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5622{
5623 atomic_set(&t->tracing_graph_pause, 0);
5624 atomic_set(&t->trace_overrun, 0);
5625 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005626 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005627 smp_wmb();
5628 t->ret_stack = ret_stack;
5629}
5630
5631/*
5632 * Allocate a return stack for the idle task. May be the first
5633 * time through, or it may be done by CPU hotplug online.
5634 */
5635void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5636{
5637 t->curr_ret_stack = -1;
5638 /*
5639 * The idle task has no parent, it either has its own
5640 * stack or no stack at all.
5641 */
5642 if (t->ret_stack)
5643 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5644
5645 if (ftrace_graph_active) {
5646 struct ftrace_ret_stack *ret_stack;
5647
5648 ret_stack = per_cpu(idle_ret_stack, cpu);
5649 if (!ret_stack) {
5650 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5651 * sizeof(struct ftrace_ret_stack),
5652 GFP_KERNEL);
5653 if (!ret_stack)
5654 return;
5655 per_cpu(idle_ret_stack, cpu) = ret_stack;
5656 }
5657 graph_init_task(t, ret_stack);
5658 }
5659}
5660
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005661/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005662void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005663{
Steven Rostedt84047e32009-06-02 16:51:55 -04005664 /* Make sure we do not use the parent ret_stack */
5665 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005666 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005667
Steven Rostedt597af812009-04-03 15:24:12 -04005668 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005669 struct ftrace_ret_stack *ret_stack;
5670
5671 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005672 * sizeof(struct ftrace_ret_stack),
5673 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005674 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005675 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005676 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005677 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005678}
5679
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005680void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005681{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005682 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5683
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005684 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005685 /* NULL must become visible to IRQs before we free it: */
5686 barrier();
5687
5688 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005689}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005690#endif