blob: dabf734f909c3df5c0085a53a77a5bb57798d2fc [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 /*
257 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400258 * recursion safe and not dynamic and the arch supports passing ops,
259 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400260 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400261 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -0400262 (ftrace_ops_list->next == &ftrace_list_end)) {
263
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400264 /* Set the ftrace_ops that the arch callback uses */
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500265 set_function_trace_op = ftrace_ops_list;
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -0400266
267 func = ftrace_ops_get_func(ftrace_ops_list);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400268 } else {
269 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500270 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400271 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400272 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400273
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400274 update_function_graph_func();
275
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500276 /* If there's no change, then do nothing more here */
277 if (ftrace_trace_function == func)
278 return;
279
280 /*
281 * If we are using the list function, it doesn't care
282 * about the function_trace_ops.
283 */
284 if (func == ftrace_ops_list_func) {
285 ftrace_trace_function = func;
286 /*
287 * Don't even bother setting function_trace_ops,
288 * it would be racy to do so anyway.
289 */
290 return;
291 }
292
293#ifndef CONFIG_DYNAMIC_FTRACE
294 /*
295 * For static tracing, we need to be a bit more careful.
296 * The function change takes affect immediately. Thus,
297 * we need to coorditate the setting of the function_trace_ops
298 * with the setting of the ftrace_trace_function.
299 *
300 * Set the function to the list ops, which will call the
301 * function we want, albeit indirectly, but it handles the
302 * ftrace_ops and doesn't depend on function_trace_op.
303 */
304 ftrace_trace_function = ftrace_ops_list_func;
305 /*
306 * Make sure all CPUs see this. Yes this is slow, but static
307 * tracing is slow and nasty to have enabled.
308 */
309 schedule_on_each_cpu(ftrace_sync);
310 /* Now all cpus are using the list ops. */
311 function_trace_op = set_function_trace_op;
312 /* Make sure the function_trace_op is visible on all CPUs */
313 smp_wmb();
314 /* Nasty way to force a rmb on all cpus */
315 smp_call_function(ftrace_sync_ipi, NULL, 1);
316 /* OK, we are all set to update the ftrace_trace_function now! */
317#endif /* !CONFIG_DYNAMIC_FTRACE */
318
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400319 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400320}
321
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800322int using_ftrace_ops_list_func(void)
323{
324 return ftrace_trace_function == ftrace_ops_list_func;
325}
326
Steven Rostedt2b499382011-05-03 22:49:52 -0400327static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200328{
Steven Rostedt2b499382011-05-03 22:49:52 -0400329 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200330 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400331 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200332 * CPU might be walking that list. We need to make sure
333 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400334 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200335 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400336 rcu_assign_pointer(*list, ops);
337}
Steven Rostedt3d083392008-05-12 21:20:42 +0200338
Steven Rostedt2b499382011-05-03 22:49:52 -0400339static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
340{
341 struct ftrace_ops **p;
342
343 /*
344 * If we are removing the last function, then simply point
345 * to the ftrace_stub.
346 */
347 if (*list == ops && ops->next == &ftrace_list_end) {
348 *list = &ftrace_list_end;
349 return 0;
350 }
351
352 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
353 if (*p == ops)
354 break;
355
356 if (*p != ops)
357 return -1;
358
359 *p = (*p)->next;
360 return 0;
361}
362
Jiri Olsae2484912012-02-15 15:51:48 +0100363static void add_ftrace_list_ops(struct ftrace_ops **list,
364 struct ftrace_ops *main_ops,
365 struct ftrace_ops *ops)
366{
367 int first = *list == &ftrace_list_end;
368 add_ftrace_ops(list, ops);
369 if (first)
370 add_ftrace_ops(&ftrace_ops_list, main_ops);
371}
372
373static int remove_ftrace_list_ops(struct ftrace_ops **list,
374 struct ftrace_ops *main_ops,
375 struct ftrace_ops *ops)
376{
377 int ret = remove_ftrace_ops(list, ops);
378 if (!ret && *list == &ftrace_list_end)
379 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
380 return ret;
381}
382
Steven Rostedt2b499382011-05-03 22:49:52 -0400383static int __register_ftrace_function(struct ftrace_ops *ops)
384{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500385 if (ops->flags & FTRACE_OPS_FL_DELETED)
386 return -EINVAL;
387
Steven Rostedtb8489142011-05-04 09:27:52 -0400388 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
389 return -EBUSY;
390
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900391#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400392 /*
393 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
394 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
395 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
396 */
397 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
398 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
399 return -EINVAL;
400
401 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
402 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
403#endif
404
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400405 if (!core_kernel_data((unsigned long)ops))
406 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
407
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500408 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100409 if (control_ops_alloc(ops))
410 return -ENOMEM;
411 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400412 } else
413 add_ftrace_ops(&ftrace_ops_list, ops);
414
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400415 if (ftrace_enabled)
416 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200417
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200418 return 0;
419}
420
Ingo Molnare309b412008-05-12 21:20:51 +0200421static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200422{
Steven Rostedt2b499382011-05-03 22:49:52 -0400423 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200424
Steven Rostedtb8489142011-05-04 09:27:52 -0400425 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
426 return -EBUSY;
427
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500428 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100429 ret = remove_ftrace_list_ops(&ftrace_control_list,
430 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400431 } else
432 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
433
Steven Rostedt2b499382011-05-03 22:49:52 -0400434 if (ret < 0)
435 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400436
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400437 if (ftrace_enabled)
438 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200439
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500440 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200441}
442
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500443static void ftrace_update_pid_func(void)
444{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400445 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500446 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900447 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500448
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400449 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500450}
451
Steven Rostedt493762f2009-03-23 17:12:36 -0400452#ifdef CONFIG_FUNCTION_PROFILER
453struct ftrace_profile {
454 struct hlist_node node;
455 unsigned long ip;
456 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400457#ifdef CONFIG_FUNCTION_GRAPH_TRACER
458 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400459 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400460#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400461};
462
463struct ftrace_profile_page {
464 struct ftrace_profile_page *next;
465 unsigned long index;
466 struct ftrace_profile records[];
467};
468
Steven Rostedtcafb1682009-03-24 20:50:39 -0400469struct ftrace_profile_stat {
470 atomic_t disabled;
471 struct hlist_head *hash;
472 struct ftrace_profile_page *pages;
473 struct ftrace_profile_page *start;
474 struct tracer_stat stat;
475};
476
Steven Rostedt493762f2009-03-23 17:12:36 -0400477#define PROFILE_RECORDS_SIZE \
478 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
479
480#define PROFILES_PER_PAGE \
481 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
482
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400483static int ftrace_profile_enabled __read_mostly;
484
485/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400486static DEFINE_MUTEX(ftrace_profile_lock);
487
Steven Rostedtcafb1682009-03-24 20:50:39 -0400488static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400489
Namhyung Kim20079eb2013-04-10 08:55:50 +0900490#define FTRACE_PROFILE_HASH_BITS 10
491#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400492
Steven Rostedt493762f2009-03-23 17:12:36 -0400493static void *
494function_stat_next(void *v, int idx)
495{
496 struct ftrace_profile *rec = v;
497 struct ftrace_profile_page *pg;
498
499 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
500
501 again:
Li Zefan0296e422009-06-26 11:15:37 +0800502 if (idx != 0)
503 rec++;
504
Steven Rostedt493762f2009-03-23 17:12:36 -0400505 if ((void *)rec >= (void *)&pg->records[pg->index]) {
506 pg = pg->next;
507 if (!pg)
508 return NULL;
509 rec = &pg->records[0];
510 if (!rec->counter)
511 goto again;
512 }
513
514 return rec;
515}
516
517static void *function_stat_start(struct tracer_stat *trace)
518{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400519 struct ftrace_profile_stat *stat =
520 container_of(trace, struct ftrace_profile_stat, stat);
521
522 if (!stat || !stat->start)
523 return NULL;
524
525 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400526}
527
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400528#ifdef CONFIG_FUNCTION_GRAPH_TRACER
529/* function graph compares on total time */
530static int function_stat_cmp(void *p1, void *p2)
531{
532 struct ftrace_profile *a = p1;
533 struct ftrace_profile *b = p2;
534
535 if (a->time < b->time)
536 return -1;
537 if (a->time > b->time)
538 return 1;
539 else
540 return 0;
541}
542#else
543/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400544static int function_stat_cmp(void *p1, void *p2)
545{
546 struct ftrace_profile *a = p1;
547 struct ftrace_profile *b = p2;
548
549 if (a->counter < b->counter)
550 return -1;
551 if (a->counter > b->counter)
552 return 1;
553 else
554 return 0;
555}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400556#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400557
558static int function_stat_headers(struct seq_file *m)
559{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400560#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400561 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400562 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400563 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400564 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400565#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400566 seq_printf(m, " Function Hit\n"
567 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400568#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400569 return 0;
570}
571
572static int function_stat_show(struct seq_file *m, void *v)
573{
574 struct ftrace_profile *rec = v;
575 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800576 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400577#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400578 static struct trace_seq s;
579 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400580 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400581#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800582 mutex_lock(&ftrace_profile_lock);
583
584 /* we raced with function_profile_reset() */
585 if (unlikely(rec->counter == 0)) {
586 ret = -EBUSY;
587 goto out;
588 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400589
590 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400591 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400592
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400593#ifdef CONFIG_FUNCTION_GRAPH_TRACER
594 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400595 avg = rec->time;
596 do_div(avg, rec->counter);
597
Chase Douglase330b3b2010-04-26 14:02:05 -0400598 /* Sample standard deviation (s^2) */
599 if (rec->counter <= 1)
600 stddev = 0;
601 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200602 /*
603 * Apply Welford's method:
604 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
605 */
606 stddev = rec->counter * rec->time_squared -
607 rec->time * rec->time;
608
Chase Douglase330b3b2010-04-26 14:02:05 -0400609 /*
610 * Divide only 1000 for ns^2 -> us^2 conversion.
611 * trace_print_graph_duration will divide 1000 again.
612 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200613 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400614 }
615
Steven Rostedt34886c82009-03-25 21:00:47 -0400616 trace_seq_init(&s);
617 trace_print_graph_duration(rec->time, &s);
618 trace_seq_puts(&s, " ");
619 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400620 trace_seq_puts(&s, " ");
621 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400622 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400623#endif
624 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800625out:
626 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400627
Li Zefan3aaba202010-08-23 16:50:12 +0800628 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400629}
630
Steven Rostedtcafb1682009-03-24 20:50:39 -0400631static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400632{
633 struct ftrace_profile_page *pg;
634
Steven Rostedtcafb1682009-03-24 20:50:39 -0400635 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400636
637 while (pg) {
638 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
639 pg->index = 0;
640 pg = pg->next;
641 }
642
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400644 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
645}
646
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400648{
649 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400650 int functions;
651 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400652 int i;
653
654 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400655 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400656 return 0;
657
Steven Rostedtcafb1682009-03-24 20:50:39 -0400658 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
659 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400660 return -ENOMEM;
661
Steven Rostedt318e0a72009-03-25 20:06:34 -0400662#ifdef CONFIG_DYNAMIC_FTRACE
663 functions = ftrace_update_tot_cnt;
664#else
665 /*
666 * We do not know the number of functions that exist because
667 * dynamic tracing is what counts them. With past experience
668 * we have around 20K functions. That should be more than enough.
669 * It is highly unlikely we will execute every function in
670 * the kernel.
671 */
672 functions = 20000;
673#endif
674
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400676
Steven Rostedt318e0a72009-03-25 20:06:34 -0400677 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
678
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900679 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400682 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400683 pg = pg->next;
684 }
685
686 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400687
688 out_free:
689 pg = stat->start;
690 while (pg) {
691 unsigned long tmp = (unsigned long)pg;
692
693 pg = pg->next;
694 free_page(tmp);
695 }
696
Steven Rostedt318e0a72009-03-25 20:06:34 -0400697 stat->pages = NULL;
698 stat->start = NULL;
699
700 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400701}
702
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400704{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400705 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400706 int size;
707
Steven Rostedtcafb1682009-03-24 20:50:39 -0400708 stat = &per_cpu(ftrace_profile_stats, cpu);
709
710 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400712 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400713 return 0;
714 }
715
716 /*
717 * We are profiling all functions, but usually only a few thousand
718 * functions are hit. We'll make a hash of 1024 items.
719 */
720 size = FTRACE_PROFILE_HASH_SIZE;
721
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400723
Steven Rostedtcafb1682009-03-24 20:50:39 -0400724 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400725 return -ENOMEM;
726
Steven Rostedt318e0a72009-03-25 20:06:34 -0400727 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728 if (ftrace_profile_pages_init(stat) < 0) {
729 kfree(stat->hash);
730 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400731 return -ENOMEM;
732 }
733
734 return 0;
735}
736
Steven Rostedtcafb1682009-03-24 20:50:39 -0400737static int ftrace_profile_init(void)
738{
739 int cpu;
740 int ret = 0;
741
Miao Xiec4602c12013-12-16 15:20:01 +0800742 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400743 ret = ftrace_profile_init_cpu(cpu);
744 if (ret)
745 break;
746 }
747
748 return ret;
749}
750
Steven Rostedt493762f2009-03-23 17:12:36 -0400751/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400752static struct ftrace_profile *
753ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400754{
755 struct ftrace_profile *rec;
756 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400757 unsigned long key;
758
Namhyung Kim20079eb2013-04-10 08:55:50 +0900759 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400761
762 if (hlist_empty(hhd))
763 return NULL;
764
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400765 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400766 if (rec->ip == ip)
767 return rec;
768 }
769
770 return NULL;
771}
772
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773static void ftrace_add_profile(struct ftrace_profile_stat *stat,
774 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400775{
776 unsigned long key;
777
Namhyung Kim20079eb2013-04-10 08:55:50 +0900778 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400780}
781
Steven Rostedt318e0a72009-03-25 20:06:34 -0400782/*
783 * The memory is already allocated, this simply finds a new record to use.
784 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400785static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400786ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400787{
788 struct ftrace_profile *rec = NULL;
789
Steven Rostedt318e0a72009-03-25 20:06:34 -0400790 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400791 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400792 goto out;
793
Steven Rostedt493762f2009-03-23 17:12:36 -0400794 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400795 * Try to find the function again since an NMI
796 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400797 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400801
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 if (stat->pages->index == PROFILES_PER_PAGE) {
803 if (!stat->pages->next)
804 goto out;
805 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 }
807
Steven Rostedtcafb1682009-03-24 20:50:39 -0400808 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400809 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400811
Steven Rostedt493762f2009-03-23 17:12:36 -0400812 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400814
815 return rec;
816}
817
Steven Rostedt493762f2009-03-23 17:12:36 -0400818static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400819function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400820 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400821{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 struct ftrace_profile *rec;
824 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400825
826 if (!ftrace_profile_enabled)
827 return;
828
Steven Rostedt493762f2009-03-23 17:12:36 -0400829 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400830
Christoph Lameterbdffd892014-04-29 14:17:40 -0500831 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400832 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400833 goto out;
834
835 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400837 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 if (!rec)
839 goto out;
840 }
841
842 rec->counter++;
843 out:
844 local_irq_restore(flags);
845}
846
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400847#ifdef CONFIG_FUNCTION_GRAPH_TRACER
848static int profile_graph_entry(struct ftrace_graph_ent *trace)
849{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400850 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400851 return 1;
852}
853
854static void profile_graph_return(struct ftrace_graph_ret *trace)
855{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400856 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400857 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400858 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400859 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400860
861 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500862 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400863 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400864 goto out;
865
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400866 /* If the calltime was zero'd ignore it */
867 if (!trace->calltime)
868 goto out;
869
Steven Rostedta2a16d62009-03-24 23:17:58 -0400870 calltime = trace->rettime - trace->calltime;
871
872 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
873 int index;
874
875 index = trace->depth;
876
877 /* Append this call time to the parent time to subtract */
878 if (index)
879 current->ret_stack[index - 1].subtime += calltime;
880
881 if (current->ret_stack[index].subtime < calltime)
882 calltime -= current->ret_stack[index].subtime;
883 else
884 calltime = 0;
885 }
886
Steven Rostedtcafb1682009-03-24 20:50:39 -0400887 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400888 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400889 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400890 rec->time_squared += calltime * calltime;
891 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400892
Steven Rostedtcafb1682009-03-24 20:50:39 -0400893 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400894 local_irq_restore(flags);
895}
896
897static int register_ftrace_profiler(void)
898{
899 return register_ftrace_graph(&profile_graph_return,
900 &profile_graph_entry);
901}
902
903static void unregister_ftrace_profiler(void)
904{
905 unregister_ftrace_graph();
906}
907#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100908static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400909 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900910 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400911 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400912};
913
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400914static int register_ftrace_profiler(void)
915{
916 return register_ftrace_function(&ftrace_profile_ops);
917}
918
919static void unregister_ftrace_profiler(void)
920{
921 unregister_ftrace_function(&ftrace_profile_ops);
922}
923#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
924
Steven Rostedt493762f2009-03-23 17:12:36 -0400925static ssize_t
926ftrace_profile_write(struct file *filp, const char __user *ubuf,
927 size_t cnt, loff_t *ppos)
928{
929 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400930 int ret;
931
Peter Huewe22fe9b52011-06-07 21:58:27 +0200932 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
933 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400934 return ret;
935
936 val = !!val;
937
938 mutex_lock(&ftrace_profile_lock);
939 if (ftrace_profile_enabled ^ val) {
940 if (val) {
941 ret = ftrace_profile_init();
942 if (ret < 0) {
943 cnt = ret;
944 goto out;
945 }
946
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400947 ret = register_ftrace_profiler();
948 if (ret < 0) {
949 cnt = ret;
950 goto out;
951 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400952 ftrace_profile_enabled = 1;
953 } else {
954 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400955 /*
956 * unregister_ftrace_profiler calls stop_machine
957 * so this acts like an synchronize_sched.
958 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400959 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400960 }
961 }
962 out:
963 mutex_unlock(&ftrace_profile_lock);
964
Jiri Olsacf8517c2009-10-23 19:36:16 -0400965 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400966
967 return cnt;
968}
969
970static ssize_t
971ftrace_profile_read(struct file *filp, char __user *ubuf,
972 size_t cnt, loff_t *ppos)
973{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400974 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400975 int r;
976
977 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
978 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
979}
980
981static const struct file_operations ftrace_profile_fops = {
982 .open = tracing_open_generic,
983 .read = ftrace_profile_read,
984 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200985 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400986};
987
Steven Rostedtcafb1682009-03-24 20:50:39 -0400988/* used to initialize the real stat files */
989static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400990 .name = "functions",
991 .stat_start = function_stat_start,
992 .stat_next = function_stat_next,
993 .stat_cmp = function_stat_cmp,
994 .stat_headers = function_stat_headers,
995 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400996};
997
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400998static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400999{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001000 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001001 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001002 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001003 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001004 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001005
Steven Rostedtcafb1682009-03-24 20:50:39 -04001006 for_each_possible_cpu(cpu) {
1007 stat = &per_cpu(ftrace_profile_stats, cpu);
1008
1009 /* allocate enough for function name + cpu number */
1010 name = kmalloc(32, GFP_KERNEL);
1011 if (!name) {
1012 /*
1013 * The files created are permanent, if something happens
1014 * we still do not free memory.
1015 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001016 WARN(1,
1017 "Could not allocate stat file for cpu %d\n",
1018 cpu);
1019 return;
1020 }
1021 stat->stat = function_stats;
1022 snprintf(name, 32, "function%d", cpu);
1023 stat->stat.name = name;
1024 ret = register_stat_tracer(&stat->stat);
1025 if (ret) {
1026 WARN(1,
1027 "Could not register function stat for cpu %d\n",
1028 cpu);
1029 kfree(name);
1030 return;
1031 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001032 }
1033
1034 entry = debugfs_create_file("function_profile_enabled", 0644,
1035 d_tracer, NULL, &ftrace_profile_fops);
1036 if (!entry)
1037 pr_warning("Could not create debugfs "
1038 "'function_profile_enabled' entry\n");
1039}
1040
1041#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001042static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001043{
1044}
1045#endif /* CONFIG_FUNCTION_PROFILER */
1046
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001047static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1048
Steven Rostedt3d083392008-05-12 21:20:42 +02001049#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001050
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001051static struct ftrace_ops *removed_ops;
1052
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001053#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001054# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001055#endif
1056
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001057static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1058
Steven Rostedtb6887d72009-02-17 12:32:04 -05001059struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001060 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001061 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001062 unsigned long flags;
1063 unsigned long ip;
1064 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001065 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001066};
1067
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001068struct ftrace_func_entry {
1069 struct hlist_node hlist;
1070 unsigned long ip;
1071};
1072
1073struct ftrace_hash {
1074 unsigned long size_bits;
1075 struct hlist_head *buckets;
1076 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001077 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001078};
1079
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001080/*
1081 * We make these constant because no one should touch them,
1082 * but they are used as the default "empty hash", to avoid allocating
1083 * it all the time. These are in a read only section such that if
1084 * anyone does try to modify it, it will cause an exception.
1085 */
1086static const struct hlist_head empty_buckets[1];
1087static const struct ftrace_hash empty_hash = {
1088 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001089};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001090#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001091
Steven Rostedt2b499382011-05-03 22:49:52 -04001092static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001093 .func = ftrace_stub,
1094 .local_hash.notrace_hash = EMPTY_HASH,
1095 .local_hash.filter_hash = EMPTY_HASH,
1096 INIT_OPS_HASH(global_ops)
1097 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1098 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001099};
1100
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001101struct ftrace_page {
1102 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001103 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001104 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001105 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001106};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001107
Steven Rostedta7900872011-12-16 16:23:44 -05001108#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1109#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001110
1111/* estimate from running different kernels */
1112#define NR_TO_INIT 10000
1113
1114static struct ftrace_page *ftrace_pages_start;
1115static struct ftrace_page *ftrace_pages;
1116
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001117static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001118{
1119 return !hash || !hash->count;
1120}
1121
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001122static struct ftrace_func_entry *
1123ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1124{
1125 unsigned long key;
1126 struct ftrace_func_entry *entry;
1127 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001128
Steven Rostedt06a51d92011-12-19 19:07:36 -05001129 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001130 return NULL;
1131
1132 if (hash->size_bits > 0)
1133 key = hash_long(ip, hash->size_bits);
1134 else
1135 key = 0;
1136
1137 hhd = &hash->buckets[key];
1138
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001139 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001140 if (entry->ip == ip)
1141 return entry;
1142 }
1143 return NULL;
1144}
1145
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001146static void __add_hash_entry(struct ftrace_hash *hash,
1147 struct ftrace_func_entry *entry)
1148{
1149 struct hlist_head *hhd;
1150 unsigned long key;
1151
1152 if (hash->size_bits)
1153 key = hash_long(entry->ip, hash->size_bits);
1154 else
1155 key = 0;
1156
1157 hhd = &hash->buckets[key];
1158 hlist_add_head(&entry->hlist, hhd);
1159 hash->count++;
1160}
1161
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001162static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1163{
1164 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001165
1166 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1167 if (!entry)
1168 return -ENOMEM;
1169
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001170 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001171 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001172
1173 return 0;
1174}
1175
1176static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001177free_hash_entry(struct ftrace_hash *hash,
1178 struct ftrace_func_entry *entry)
1179{
1180 hlist_del(&entry->hlist);
1181 kfree(entry);
1182 hash->count--;
1183}
1184
1185static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001186remove_hash_entry(struct ftrace_hash *hash,
1187 struct ftrace_func_entry *entry)
1188{
1189 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001190 hash->count--;
1191}
1192
1193static void ftrace_hash_clear(struct ftrace_hash *hash)
1194{
1195 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001196 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001197 struct ftrace_func_entry *entry;
1198 int size = 1 << hash->size_bits;
1199 int i;
1200
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001201 if (!hash->count)
1202 return;
1203
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001204 for (i = 0; i < size; i++) {
1205 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001206 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001207 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001208 }
1209 FTRACE_WARN_ON(hash->count);
1210}
1211
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001212static void free_ftrace_hash(struct ftrace_hash *hash)
1213{
1214 if (!hash || hash == EMPTY_HASH)
1215 return;
1216 ftrace_hash_clear(hash);
1217 kfree(hash->buckets);
1218 kfree(hash);
1219}
1220
Steven Rostedt07fd5512011-05-05 18:03:47 -04001221static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1222{
1223 struct ftrace_hash *hash;
1224
1225 hash = container_of(rcu, struct ftrace_hash, rcu);
1226 free_ftrace_hash(hash);
1227}
1228
1229static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1230{
1231 if (!hash || hash == EMPTY_HASH)
1232 return;
1233 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1234}
1235
Jiri Olsa5500fa52012-02-15 15:51:54 +01001236void ftrace_free_filter(struct ftrace_ops *ops)
1237{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001238 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001239 free_ftrace_hash(ops->func_hash->filter_hash);
1240 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001241}
1242
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001243static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1244{
1245 struct ftrace_hash *hash;
1246 int size;
1247
1248 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1249 if (!hash)
1250 return NULL;
1251
1252 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001253 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001254
1255 if (!hash->buckets) {
1256 kfree(hash);
1257 return NULL;
1258 }
1259
1260 hash->size_bits = size_bits;
1261
1262 return hash;
1263}
1264
1265static struct ftrace_hash *
1266alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1267{
1268 struct ftrace_func_entry *entry;
1269 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001270 int size;
1271 int ret;
1272 int i;
1273
1274 new_hash = alloc_ftrace_hash(size_bits);
1275 if (!new_hash)
1276 return NULL;
1277
1278 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001279 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001280 return new_hash;
1281
1282 size = 1 << hash->size_bits;
1283 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001284 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001285 ret = add_hash_entry(new_hash, entry->ip);
1286 if (ret < 0)
1287 goto free_hash;
1288 }
1289 }
1290
1291 FTRACE_WARN_ON(new_hash->count != hash->count);
1292
1293 return new_hash;
1294
1295 free_hash:
1296 free_ftrace_hash(new_hash);
1297 return NULL;
1298}
1299
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001300static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001301ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001302static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001303ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001304
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001305static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001306ftrace_hash_move(struct ftrace_ops *ops, int enable,
1307 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001308{
1309 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001310 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001311 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001312 struct ftrace_hash *old_hash;
1313 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001314 int size = src->count;
1315 int bits = 0;
1316 int i;
1317
1318 /*
1319 * If the new source is empty, just free dst and assign it
1320 * the empty_hash.
1321 */
1322 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001323 new_hash = EMPTY_HASH;
1324 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001325 }
1326
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001327 /*
1328 * Make the hash size about 1/2 the # found
1329 */
1330 for (size /= 2; size; size >>= 1)
1331 bits++;
1332
1333 /* Don't allocate too much */
1334 if (bits > FTRACE_HASH_MAX_BITS)
1335 bits = FTRACE_HASH_MAX_BITS;
1336
Steven Rostedt07fd5512011-05-05 18:03:47 -04001337 new_hash = alloc_ftrace_hash(bits);
1338 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001339 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001340
1341 size = 1 << src->size_bits;
1342 for (i = 0; i < size; i++) {
1343 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001344 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001345 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001346 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001347 }
1348 }
1349
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001350update:
1351 /*
1352 * Remove the current set, update the hash and add
1353 * them back.
1354 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001355 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001356
Steven Rostedt07fd5512011-05-05 18:03:47 -04001357 old_hash = *dst;
1358 rcu_assign_pointer(*dst, new_hash);
1359 free_ftrace_hash_rcu(old_hash);
1360
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001361 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001362
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001363 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001364}
1365
Steven Rostedt265c8312009-02-13 12:43:56 -05001366/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001367 * Test the hashes for this ops to see if we want to call
1368 * the ops->func or not.
1369 *
1370 * It's a match if the ip is in the ops->filter_hash or
1371 * the filter_hash does not exist or is empty,
1372 * AND
1373 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001374 *
1375 * This needs to be called with preemption disabled as
1376 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001377 */
1378static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001379ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001380{
1381 struct ftrace_hash *filter_hash;
1382 struct ftrace_hash *notrace_hash;
1383 int ret;
1384
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001385#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1386 /*
1387 * There's a small race when adding ops that the ftrace handler
1388 * that wants regs, may be called without them. We can not
1389 * allow that handler to be called if regs is NULL.
1390 */
1391 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1392 return 0;
1393#endif
1394
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001395 filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1396 notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001397
Steven Rostedt06a51d92011-12-19 19:07:36 -05001398 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001399 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001400 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001401 !ftrace_lookup_ip(notrace_hash, ip)))
1402 ret = 1;
1403 else
1404 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001405
1406 return ret;
1407}
1408
1409/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001410 * This is a double for. Do not use 'break' to break out of the loop,
1411 * you must use a goto.
1412 */
1413#define do_for_each_ftrace_rec(pg, rec) \
1414 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1415 int _____i; \
1416 for (_____i = 0; _____i < pg->index; _____i++) { \
1417 rec = &pg->records[_____i];
1418
1419#define while_for_each_ftrace_rec() \
1420 } \
1421 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301422
Steven Rostedt5855fea2011-12-16 19:27:42 -05001423
1424static int ftrace_cmp_recs(const void *a, const void *b)
1425{
Steven Rostedta650e022012-04-25 13:48:13 -04001426 const struct dyn_ftrace *key = a;
1427 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001428
Steven Rostedta650e022012-04-25 13:48:13 -04001429 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001430 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001431 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1432 return 1;
1433 return 0;
1434}
1435
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001436static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001437{
1438 struct ftrace_page *pg;
1439 struct dyn_ftrace *rec;
1440 struct dyn_ftrace key;
1441
1442 key.ip = start;
1443 key.flags = end; /* overload flags, as it is unsigned long */
1444
1445 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1446 if (end < pg->records[0].ip ||
1447 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1448 continue;
1449 rec = bsearch(&key, pg->records, pg->index,
1450 sizeof(struct dyn_ftrace),
1451 ftrace_cmp_recs);
1452 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001453 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001454 }
1455
Steven Rostedt5855fea2011-12-16 19:27:42 -05001456 return 0;
1457}
1458
Steven Rostedtc88fd862011-08-16 09:53:39 -04001459/**
1460 * ftrace_location - return true if the ip giving is a traced location
1461 * @ip: the instruction pointer to check
1462 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001463 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001464 * That is, the instruction that is either a NOP or call to
1465 * the function tracer. It checks the ftrace internal tables to
1466 * determine if the address belongs or not.
1467 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001468unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001469{
Steven Rostedta650e022012-04-25 13:48:13 -04001470 return ftrace_location_range(ip, ip);
1471}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001472
Steven Rostedta650e022012-04-25 13:48:13 -04001473/**
1474 * ftrace_text_reserved - return true if range contains an ftrace location
1475 * @start: start of range to search
1476 * @end: end of range to search (inclusive). @end points to the last byte to check.
1477 *
1478 * Returns 1 if @start and @end contains a ftrace location.
1479 * That is, the instruction that is either a NOP or call to
1480 * the function tracer. It checks the ftrace internal tables to
1481 * determine if the address belongs or not.
1482 */
Sasha Levind88471c2013-01-09 18:09:20 -05001483int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001484{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001485 unsigned long ret;
1486
1487 ret = ftrace_location_range((unsigned long)start,
1488 (unsigned long)end);
1489
1490 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001491}
1492
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001493/* Test if ops registered to this rec needs regs */
1494static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1495{
1496 struct ftrace_ops *ops;
1497 bool keep_regs = false;
1498
1499 for (ops = ftrace_ops_list;
1500 ops != &ftrace_list_end; ops = ops->next) {
1501 /* pass rec in as regs to have non-NULL val */
1502 if (ftrace_ops_test(ops, rec->ip, rec)) {
1503 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1504 keep_regs = true;
1505 break;
1506 }
1507 }
1508 }
1509
1510 return keep_regs;
1511}
1512
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001513static void ftrace_remove_tramp(struct ftrace_ops *ops,
1514 struct dyn_ftrace *rec)
1515{
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001516 /* If TRAMP is not set, no ops should have a trampoline for this */
1517 if (!(rec->flags & FTRACE_FL_TRAMP))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001518 return;
1519
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001520 rec->flags &= ~FTRACE_FL_TRAMP;
1521
1522 if ((!ftrace_hash_empty(ops->func_hash->filter_hash) &&
1523 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip)) ||
1524 ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
1525 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001526 /*
1527 * The tramp_hash entry will be removed at time
1528 * of update.
1529 */
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001530 ops->nr_trampolines--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001531}
1532
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001533static void ftrace_clear_tramps(struct dyn_ftrace *rec, struct ftrace_ops *ops)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001534{
1535 struct ftrace_ops *op;
1536
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001537 /* If TRAMP is not set, no ops should have a trampoline for this */
1538 if (!(rec->flags & FTRACE_FL_TRAMP))
1539 return;
1540
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001541 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001542 /*
1543 * This function is called to clear other tramps
1544 * not the one that is being updated.
1545 */
1546 if (op == ops)
1547 continue;
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001548 if (op->nr_trampolines)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001549 ftrace_remove_tramp(op, rec);
1550 } while_for_each_ftrace_op(op);
1551}
1552
Steven Rostedted926f92011-05-03 13:25:24 -04001553static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1554 int filter_hash,
1555 bool inc)
1556{
1557 struct ftrace_hash *hash;
1558 struct ftrace_hash *other_hash;
1559 struct ftrace_page *pg;
1560 struct dyn_ftrace *rec;
1561 int count = 0;
1562 int all = 0;
1563
1564 /* Only update if the ops has been registered */
1565 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1566 return;
1567
1568 /*
1569 * In the filter_hash case:
1570 * If the count is zero, we update all records.
1571 * Otherwise we just update the items in the hash.
1572 *
1573 * In the notrace_hash case:
1574 * We enable the update in the hash.
1575 * As disabling notrace means enabling the tracing,
1576 * and enabling notrace means disabling, the inc variable
1577 * gets inversed.
1578 */
1579 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001580 hash = ops->func_hash->filter_hash;
1581 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001582 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001583 all = 1;
1584 } else {
1585 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001586 hash = ops->func_hash->notrace_hash;
1587 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001588 /*
1589 * If the notrace hash has no items,
1590 * then there's nothing to do.
1591 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001592 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001593 return;
1594 }
1595
1596 do_for_each_ftrace_rec(pg, rec) {
1597 int in_other_hash = 0;
1598 int in_hash = 0;
1599 int match = 0;
1600
1601 if (all) {
1602 /*
1603 * Only the filter_hash affects all records.
1604 * Update if the record is not in the notrace hash.
1605 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001606 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001607 match = 1;
1608 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001609 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1610 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001611
1612 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001613 * If filter_hash is set, we want to match all functions
1614 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001615 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001616 * If filter_hash is not set, then we are decrementing.
1617 * That means we match anything that is in the hash
1618 * and also in the other_hash. That is, we need to turn
1619 * off functions in the other hash because they are disabled
1620 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001621 */
1622 if (filter_hash && in_hash && !in_other_hash)
1623 match = 1;
1624 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001625 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001626 match = 1;
1627 }
1628 if (!match)
1629 continue;
1630
1631 if (inc) {
1632 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001633 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001634 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001635
1636 /*
1637 * If there's only a single callback registered to a
1638 * function, and the ops has a trampoline registered
1639 * for it, then we can call it directly.
1640 */
1641 if (ftrace_rec_count(rec) == 1 && ops->trampoline) {
1642 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04001643 ops->nr_trampolines++;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001644 } else {
1645 /*
1646 * If we are adding another function callback
1647 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001648 * custom trampoline in use, then we need to go
1649 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001650 */
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001651 ftrace_clear_tramps(rec, ops);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001652 }
1653
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001654 /*
1655 * If any ops wants regs saved for this function
1656 * then all ops will get saved regs.
1657 */
1658 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1659 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001660 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001661 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001662 return;
1663 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001664
1665 if (ops->trampoline && !ftrace_rec_count(rec))
1666 ftrace_remove_tramp(ops, rec);
1667
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001668 /*
1669 * If the rec had REGS enabled and the ops that is
1670 * being removed had REGS set, then see if there is
1671 * still any ops for this record that wants regs.
1672 * If not, we can stop recording them.
1673 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001674 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001675 rec->flags & FTRACE_FL_REGS &&
1676 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1677 if (!test_rec_ops_needs_regs(rec))
1678 rec->flags &= ~FTRACE_FL_REGS;
1679 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001680
1681 /*
1682 * flags will be cleared in ftrace_check_record()
1683 * if rec count is zero.
1684 */
Steven Rostedted926f92011-05-03 13:25:24 -04001685 }
1686 count++;
1687 /* Shortcut, if we handled all records, we are done. */
1688 if (!all && count == hash->count)
1689 return;
1690 } while_for_each_ftrace_rec();
1691}
1692
1693static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1694 int filter_hash)
1695{
1696 __ftrace_hash_rec_update(ops, filter_hash, 0);
1697}
1698
1699static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1700 int filter_hash)
1701{
1702 __ftrace_hash_rec_update(ops, filter_hash, 1);
1703}
1704
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001705static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1706 int filter_hash, int inc)
1707{
1708 struct ftrace_ops *op;
1709
1710 __ftrace_hash_rec_update(ops, filter_hash, inc);
1711
1712 if (ops->func_hash != &global_ops.local_hash)
1713 return;
1714
1715 /*
1716 * If the ops shares the global_ops hash, then we need to update
1717 * all ops that are enabled and use this hash.
1718 */
1719 do_for_each_ftrace_op(op, ftrace_ops_list) {
1720 /* Already done */
1721 if (op == ops)
1722 continue;
1723 if (op->func_hash == &global_ops.local_hash)
1724 __ftrace_hash_rec_update(op, filter_hash, inc);
1725 } while_for_each_ftrace_op(op);
1726}
1727
1728static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1729 int filter_hash)
1730{
1731 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1732}
1733
1734static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1735 int filter_hash)
1736{
1737 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1738}
1739
Steven Rostedt05736a42008-09-22 14:55:47 -07001740static void print_ip_ins(const char *fmt, unsigned char *p)
1741{
1742 int i;
1743
1744 printk(KERN_CONT "%s", fmt);
1745
1746 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1747 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1748}
1749
Steven Rostedtc88fd862011-08-16 09:53:39 -04001750/**
1751 * ftrace_bug - report and shutdown function tracer
1752 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1753 * @ip: The address that failed
1754 *
1755 * The arch code that enables or disables the function tracing
1756 * can call ftrace_bug() when it has detected a problem in
1757 * modifying the code. @failed should be one of either:
1758 * EFAULT - if the problem happens on reading the @ip address
1759 * EINVAL - if what is read at @ip is not what was expected
1760 * EPERM - if the problem happens on writting to the @ip address
1761 */
1762void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001763{
1764 switch (failed) {
1765 case -EFAULT:
1766 FTRACE_WARN_ON_ONCE(1);
1767 pr_info("ftrace faulted on modifying ");
1768 print_ip_sym(ip);
1769 break;
1770 case -EINVAL:
1771 FTRACE_WARN_ON_ONCE(1);
1772 pr_info("ftrace failed to modify ");
1773 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001774 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001775 printk(KERN_CONT "\n");
1776 break;
1777 case -EPERM:
1778 FTRACE_WARN_ON_ONCE(1);
1779 pr_info("ftrace faulted on writing ");
1780 print_ip_sym(ip);
1781 break;
1782 default:
1783 FTRACE_WARN_ON_ONCE(1);
1784 pr_info("ftrace faulted on unknown error ");
1785 print_ip_sym(ip);
1786 }
1787}
1788
Steven Rostedtc88fd862011-08-16 09:53:39 -04001789static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001790{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001791 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001792
Steven Rostedt982c3502008-11-15 16:31:41 -05001793 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001794 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001795 *
Steven Rostedted926f92011-05-03 13:25:24 -04001796 * If the record has a ref count, then we need to enable it
1797 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001798 *
Steven Rostedted926f92011-05-03 13:25:24 -04001799 * Otherwise we make sure its disabled.
1800 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001801 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001802 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001803 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001804 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001805 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001806
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001807 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001808 * If enabling and the REGS flag does not match the REGS_EN, or
1809 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1810 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001811 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001812 if (flag) {
1813 if (!(rec->flags & FTRACE_FL_REGS) !=
1814 !(rec->flags & FTRACE_FL_REGS_EN))
1815 flag |= FTRACE_FL_REGS;
1816
1817 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1818 !(rec->flags & FTRACE_FL_TRAMP_EN))
1819 flag |= FTRACE_FL_TRAMP;
1820 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001821
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001822 /* If the state of this record hasn't changed, then do nothing */
1823 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001824 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001825
1826 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001827 /* Save off if rec is being enabled (for return value) */
1828 flag ^= rec->flags & FTRACE_FL_ENABLED;
1829
1830 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001831 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001832 if (flag & FTRACE_FL_REGS) {
1833 if (rec->flags & FTRACE_FL_REGS)
1834 rec->flags |= FTRACE_FL_REGS_EN;
1835 else
1836 rec->flags &= ~FTRACE_FL_REGS_EN;
1837 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001838 if (flag & FTRACE_FL_TRAMP) {
1839 if (rec->flags & FTRACE_FL_TRAMP)
1840 rec->flags |= FTRACE_FL_TRAMP_EN;
1841 else
1842 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1843 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001844 }
1845
1846 /*
1847 * If this record is being updated from a nop, then
1848 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001849 * Otherwise,
1850 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001851 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001852 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001853 */
1854 if (flag & FTRACE_FL_ENABLED)
1855 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001856
1857 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001858 }
1859
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001860 if (update) {
1861 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001862 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001863 rec->flags = 0;
1864 else
1865 /* Just disable the record (keep REGS state) */
1866 rec->flags &= ~FTRACE_FL_ENABLED;
1867 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001868
1869 return FTRACE_UPDATE_MAKE_NOP;
1870}
1871
1872/**
1873 * ftrace_update_record, set a record that now is tracing or not
1874 * @rec: the record to update
1875 * @enable: set to 1 if the record is tracing, zero to force disable
1876 *
1877 * The records that represent all functions that can be traced need
1878 * to be updated when tracing has been enabled.
1879 */
1880int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1881{
1882 return ftrace_check_record(rec, enable, 1);
1883}
1884
1885/**
1886 * ftrace_test_record, check if the record has been enabled or not
1887 * @rec: the record to test
1888 * @enable: set to 1 to check if enabled, 0 if it is disabled
1889 *
1890 * The arch code may need to test if a record is already set to
1891 * tracing to determine how to modify the function code that it
1892 * represents.
1893 */
1894int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1895{
1896 return ftrace_check_record(rec, enable, 0);
1897}
1898
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001899static struct ftrace_ops *
1900ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1901{
1902 struct ftrace_ops *op;
1903
1904 /* Removed ops need to be tested first */
1905 if (removed_ops && removed_ops->tramp_hash) {
1906 if (ftrace_lookup_ip(removed_ops->tramp_hash, rec->ip))
1907 return removed_ops;
1908 }
1909
1910 do_for_each_ftrace_op(op, ftrace_ops_list) {
1911 if (!op->tramp_hash)
1912 continue;
1913
1914 if (ftrace_lookup_ip(op->tramp_hash, rec->ip))
1915 return op;
1916
1917 } while_for_each_ftrace_op(op);
1918
1919 return NULL;
1920}
1921
1922static struct ftrace_ops *
1923ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
1924{
1925 struct ftrace_ops *op;
1926
1927 do_for_each_ftrace_op(op, ftrace_ops_list) {
1928 /* pass rec in as regs to have non-NULL val */
1929 if (ftrace_ops_test(op, rec->ip, rec))
1930 return op;
1931 } while_for_each_ftrace_op(op);
1932
1933 return NULL;
1934}
1935
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001936/**
1937 * ftrace_get_addr_new - Get the call address to set to
1938 * @rec: The ftrace record descriptor
1939 *
1940 * If the record has the FTRACE_FL_REGS set, that means that it
1941 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
1942 * is not not set, then it wants to convert to the normal callback.
1943 *
1944 * Returns the address of the trampoline to set to
1945 */
1946unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
1947{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001948 struct ftrace_ops *ops;
1949
1950 /* Trampolines take precedence over regs */
1951 if (rec->flags & FTRACE_FL_TRAMP) {
1952 ops = ftrace_find_tramp_ops_new(rec);
1953 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001954 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
1955 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001956 /* Ftrace is shutting down, return anything */
1957 return (unsigned long)FTRACE_ADDR;
1958 }
1959 return ops->trampoline;
1960 }
1961
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001962 if (rec->flags & FTRACE_FL_REGS)
1963 return (unsigned long)FTRACE_REGS_ADDR;
1964 else
1965 return (unsigned long)FTRACE_ADDR;
1966}
1967
1968/**
1969 * ftrace_get_addr_curr - Get the call address that is already there
1970 * @rec: The ftrace record descriptor
1971 *
1972 * The FTRACE_FL_REGS_EN is set when the record already points to
1973 * a function that saves all the regs. Basically the '_EN' version
1974 * represents the current state of the function.
1975 *
1976 * Returns the address of the trampoline that is currently being called
1977 */
1978unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
1979{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001980 struct ftrace_ops *ops;
1981
1982 /* Trampolines take precedence over regs */
1983 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1984 ops = ftrace_find_tramp_ops_curr(rec);
1985 if (FTRACE_WARN_ON(!ops)) {
1986 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
1987 (void *)rec->ip, (void *)rec->ip);
1988 /* Ftrace is shutting down, return anything */
1989 return (unsigned long)FTRACE_ADDR;
1990 }
1991 return ops->trampoline;
1992 }
1993
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04001994 if (rec->flags & FTRACE_FL_REGS_EN)
1995 return (unsigned long)FTRACE_REGS_ADDR;
1996 else
1997 return (unsigned long)FTRACE_ADDR;
1998}
1999
Steven Rostedtc88fd862011-08-16 09:53:39 -04002000static int
2001__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2002{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002003 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002004 unsigned long ftrace_addr;
2005 int ret;
2006
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002007 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002008
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002009 /* This needs to be done before we call ftrace_update_record */
2010 ftrace_old_addr = ftrace_get_addr_curr(rec);
2011
2012 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002013
Steven Rostedtc88fd862011-08-16 09:53:39 -04002014 switch (ret) {
2015 case FTRACE_UPDATE_IGNORE:
2016 return 0;
2017
2018 case FTRACE_UPDATE_MAKE_CALL:
2019 return ftrace_make_call(rec, ftrace_addr);
2020
2021 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002022 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002023
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002024 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002025 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002026 }
2027
2028 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002029}
2030
Steven Rostedte4f5d542012-04-27 09:13:18 -04002031void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002032{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002033 struct dyn_ftrace *rec;
2034 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002035 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002036
Steven Rostedt45a4a232011-04-21 23:16:46 -04002037 if (unlikely(ftrace_disabled))
2038 return;
2039
Steven Rostedt265c8312009-02-13 12:43:56 -05002040 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002041 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002042 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04002043 ftrace_bug(failed, rec->ip);
2044 /* Stop processing */
2045 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002046 }
2047 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002048}
2049
Steven Rostedtc88fd862011-08-16 09:53:39 -04002050struct ftrace_rec_iter {
2051 struct ftrace_page *pg;
2052 int index;
2053};
2054
2055/**
2056 * ftrace_rec_iter_start, start up iterating over traced functions
2057 *
2058 * Returns an iterator handle that is used to iterate over all
2059 * the records that represent address locations where functions
2060 * are traced.
2061 *
2062 * May return NULL if no records are available.
2063 */
2064struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2065{
2066 /*
2067 * We only use a single iterator.
2068 * Protected by the ftrace_lock mutex.
2069 */
2070 static struct ftrace_rec_iter ftrace_rec_iter;
2071 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2072
2073 iter->pg = ftrace_pages_start;
2074 iter->index = 0;
2075
2076 /* Could have empty pages */
2077 while (iter->pg && !iter->pg->index)
2078 iter->pg = iter->pg->next;
2079
2080 if (!iter->pg)
2081 return NULL;
2082
2083 return iter;
2084}
2085
2086/**
2087 * ftrace_rec_iter_next, get the next record to process.
2088 * @iter: The handle to the iterator.
2089 *
2090 * Returns the next iterator after the given iterator @iter.
2091 */
2092struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2093{
2094 iter->index++;
2095
2096 if (iter->index >= iter->pg->index) {
2097 iter->pg = iter->pg->next;
2098 iter->index = 0;
2099
2100 /* Could have empty pages */
2101 while (iter->pg && !iter->pg->index)
2102 iter->pg = iter->pg->next;
2103 }
2104
2105 if (!iter->pg)
2106 return NULL;
2107
2108 return iter;
2109}
2110
2111/**
2112 * ftrace_rec_iter_record, get the record at the iterator location
2113 * @iter: The current iterator location
2114 *
2115 * Returns the record that the current @iter is at.
2116 */
2117struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2118{
2119 return &iter->pg->records[iter->index];
2120}
2121
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302122static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002123ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002124{
2125 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002126 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002127
2128 ip = rec->ip;
2129
Steven Rostedt45a4a232011-04-21 23:16:46 -04002130 if (unlikely(ftrace_disabled))
2131 return 0;
2132
Shaohua Li25aac9d2009-01-09 11:29:40 +08002133 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002134 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08002135 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302136 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002137 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302138 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002139}
2140
Steven Rostedt000ab692009-02-17 13:35:06 -05002141/*
2142 * archs can override this function if they must do something
2143 * before the modifying code is performed.
2144 */
2145int __weak ftrace_arch_code_modify_prepare(void)
2146{
2147 return 0;
2148}
2149
2150/*
2151 * archs can override this function if they must do something
2152 * after the modifying code is performed.
2153 */
2154int __weak ftrace_arch_code_modify_post_process(void)
2155{
2156 return 0;
2157}
2158
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002159void ftrace_modify_all_code(int command)
2160{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002161 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002162 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002163
2164 /*
2165 * If the ftrace_caller calls a ftrace_ops func directly,
2166 * we need to make sure that it only traces functions it
2167 * expects to trace. When doing the switch of functions,
2168 * we need to update to the ftrace_ops_list_func first
2169 * before the transition between old and new calls are set,
2170 * as the ftrace_ops_list_func will check the ops hashes
2171 * to make sure the ops are having the right functions
2172 * traced.
2173 */
Petr Mladekcd210672014-02-24 17:12:21 +01002174 if (update) {
2175 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2176 if (FTRACE_WARN_ON(err))
2177 return;
2178 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002179
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002180 if (command & FTRACE_UPDATE_CALLS)
2181 ftrace_replace_code(1);
2182 else if (command & FTRACE_DISABLE_CALLS)
2183 ftrace_replace_code(0);
2184
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002185 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2186 function_trace_op = set_function_trace_op;
2187 smp_wmb();
2188 /* If irqs are disabled, we are in stop machine */
2189 if (!irqs_disabled())
2190 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002191 err = ftrace_update_ftrace_func(ftrace_trace_function);
2192 if (FTRACE_WARN_ON(err))
2193 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002194 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002195
2196 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002197 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002198 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002199 err = ftrace_disable_ftrace_graph_caller();
2200 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002201}
2202
Ingo Molnare309b412008-05-12 21:20:51 +02002203static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002204{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002205 int *command = data;
2206
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002207 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002208
Steven Rostedtc88fd862011-08-16 09:53:39 -04002209 return 0;
2210}
2211
2212/**
2213 * ftrace_run_stop_machine, go back to the stop machine method
2214 * @command: The command to tell ftrace what to do
2215 *
2216 * If an arch needs to fall back to the stop machine method, the
2217 * it can call this function.
2218 */
2219void ftrace_run_stop_machine(int command)
2220{
2221 stop_machine(__ftrace_modify_code, &command, NULL);
2222}
2223
2224/**
2225 * arch_ftrace_update_code, modify the code to trace or not trace
2226 * @command: The command that needs to be done
2227 *
2228 * Archs can override this function if it does not need to
2229 * run stop_machine() to modify code.
2230 */
2231void __weak arch_ftrace_update_code(int command)
2232{
2233 ftrace_run_stop_machine(command);
2234}
2235
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002236static int ftrace_save_ops_tramp_hash(struct ftrace_ops *ops)
2237{
2238 struct ftrace_page *pg;
2239 struct dyn_ftrace *rec;
2240 int size, bits;
2241 int ret;
2242
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002243 size = ops->nr_trampolines;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002244 bits = 0;
2245 /*
2246 * Make the hash size about 1/2 the # found
2247 */
2248 for (size /= 2; size; size >>= 1)
2249 bits++;
2250
2251 ops->tramp_hash = alloc_ftrace_hash(bits);
2252 /*
2253 * TODO: a failed allocation is going to screw up
2254 * the accounting of what needs to be modified
2255 * and not. For now, we kill ftrace if we fail
2256 * to allocate here. But there are ways around this,
2257 * but that will take a little more work.
2258 */
2259 if (!ops->tramp_hash)
2260 return -ENOMEM;
2261
2262 do_for_each_ftrace_rec(pg, rec) {
2263 if (ftrace_rec_count(rec) == 1 &&
2264 ftrace_ops_test(ops, rec->ip, rec)) {
2265
Steven Rostedt (Red Hat)2a0343b2014-07-24 09:56:27 -04002266 /*
2267 * If another ops adds to a rec, the rec will
2268 * lose its trampoline and never get it back
2269 * until all ops are off of it.
2270 */
2271 if (!(rec->flags & FTRACE_FL_TRAMP))
2272 continue;
2273
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002274 /* This record had better have a trampoline */
2275 if (FTRACE_WARN_ON(!(rec->flags & FTRACE_FL_TRAMP_EN)))
2276 return -1;
2277
2278 ret = add_hash_entry(ops->tramp_hash, rec->ip);
2279 if (ret < 0)
2280 return ret;
2281 }
2282 } while_for_each_ftrace_rec();
2283
Steven Rostedt (Red Hat)dc6f03f2014-07-24 11:26:11 -04002284 /* The number of recs in the hash must match nr_trampolines */
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002285 if (FTRACE_WARN_ON(ops->tramp_hash->count != ops->nr_trampolines))
2286 pr_warn("count=%ld trampolines=%d\n",
2287 ops->tramp_hash->count,
2288 ops->nr_trampolines);
Steven Rostedt (Red Hat)dc6f03f2014-07-24 11:26:11 -04002289
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002290 return 0;
2291}
2292
2293static int ftrace_save_tramp_hashes(void)
2294{
2295 struct ftrace_ops *op;
2296 int ret;
2297
2298 /*
2299 * Now that any trampoline is being used, we need to save the
2300 * hashes for the ops that have them. This allows the mapping
2301 * back from the record to the ops that has the trampoline to
2302 * know what code is being replaced. Modifying code must always
2303 * verify what it is changing.
2304 */
2305 do_for_each_ftrace_op(op, ftrace_ops_list) {
2306
2307 /* The tramp_hash is recreated each time. */
2308 free_ftrace_hash(op->tramp_hash);
2309 op->tramp_hash = NULL;
2310
Steven Rostedt (Red Hat)0162d622014-07-23 15:03:00 -04002311 if (op->nr_trampolines) {
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002312 ret = ftrace_save_ops_tramp_hash(op);
2313 if (ret)
2314 return ret;
2315 }
2316
2317 } while_for_each_ftrace_op(op);
2318
2319 return 0;
2320}
2321
Steven Rostedtc88fd862011-08-16 09:53:39 -04002322static void ftrace_run_update_code(int command)
2323{
2324 int ret;
2325
2326 ret = ftrace_arch_code_modify_prepare();
2327 FTRACE_WARN_ON(ret);
2328 if (ret)
2329 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002330
2331 /*
2332 * By default we use stop_machine() to modify the code.
2333 * But archs can do what ever they want as long as it
2334 * is safe. The stop_machine() is the safest, but also
2335 * produces the most overhead.
2336 */
2337 arch_ftrace_update_code(command);
2338
Steven Rostedt000ab692009-02-17 13:35:06 -05002339 ret = ftrace_arch_code_modify_post_process();
2340 FTRACE_WARN_ON(ret);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002341
2342 ret = ftrace_save_tramp_hashes();
2343 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002344}
2345
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002346static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002347static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002348
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002349static void control_ops_free(struct ftrace_ops *ops)
2350{
2351 free_percpu(ops->disabled);
2352}
2353
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002354static void ftrace_startup_enable(int command)
2355{
2356 if (saved_ftrace_func != ftrace_trace_function) {
2357 saved_ftrace_func = ftrace_trace_function;
2358 command |= FTRACE_UPDATE_TRACE_FUNC;
2359 }
2360
2361 if (!command || !ftrace_enabled)
2362 return;
2363
2364 ftrace_run_update_code(command);
2365}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002366
Steven Rostedta1cd6172011-05-23 15:24:25 -04002367static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002368{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002369 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002370
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002371 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002372 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002373
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002374 ret = __register_ftrace_function(ops);
2375 if (ret)
2376 return ret;
2377
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002378 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002379 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002380
Steven Rostedted926f92011-05-03 13:25:24 -04002381 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002382
2383 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002384
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002385 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002386
2387 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002388}
2389
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002390static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002391{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002392 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002393
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002394 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002395 return -ENODEV;
2396
2397 ret = __unregister_ftrace_function(ops);
2398 if (ret)
2399 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002400
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002401 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002402 /*
2403 * Just warn in case of unbalance, no need to kill ftrace, it's not
2404 * critical but the ftrace_call callers may be never nopped again after
2405 * further ftrace uses.
2406 */
2407 WARN_ON_ONCE(ftrace_start_up < 0);
2408
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002409 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002410
Namhyung Kima737e6d2014-06-12 23:56:12 +09002411 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002412
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002413 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002414
2415 if (saved_ftrace_func != ftrace_trace_function) {
2416 saved_ftrace_func = ftrace_trace_function;
2417 command |= FTRACE_UPDATE_TRACE_FUNC;
2418 }
2419
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002420 if (!command || !ftrace_enabled) {
2421 /*
2422 * If these are control ops, they still need their
2423 * per_cpu field freed. Since, function tracing is
2424 * not currently active, we can just free them
2425 * without synchronizing all CPUs.
2426 */
2427 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2428 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002429 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002430 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002431
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002432 /*
2433 * If the ops uses a trampoline, then it needs to be
2434 * tested first on update.
2435 */
2436 removed_ops = ops;
2437
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002438 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002439
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002440 removed_ops = NULL;
2441
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002442 /*
2443 * Dynamic ops may be freed, we must make sure that all
2444 * callers are done before leaving this function.
2445 * The same goes for freeing the per_cpu data of the control
2446 * ops.
2447 *
2448 * Again, normal synchronize_sched() is not good enough.
2449 * We need to do a hard force of sched synchronization.
2450 * This is because we use preempt_disable() to do RCU, but
2451 * the function tracers can be called where RCU is not watching
2452 * (like before user_exit()). We can not rely on the RCU
2453 * infrastructure to do the synchronization, thus we must do it
2454 * ourselves.
2455 */
2456 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2457 schedule_on_each_cpu(ftrace_sync);
2458
2459 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2460 control_ops_free(ops);
2461 }
2462
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002463 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002464}
2465
Ingo Molnare309b412008-05-12 21:20:51 +02002466static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002467{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002468 if (unlikely(ftrace_disabled))
2469 return;
2470
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002471 /* Force update next time */
2472 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002473 /* ftrace_start_up is true if we want ftrace running */
2474 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002475 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002476}
2477
Ingo Molnare309b412008-05-12 21:20:51 +02002478static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002479{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002480 if (unlikely(ftrace_disabled))
2481 return;
2482
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002483 /* ftrace_start_up is true if ftrace is running */
2484 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002485 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002486}
2487
Steven Rostedt3d083392008-05-12 21:20:42 +02002488static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002489unsigned long ftrace_update_tot_cnt;
2490
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002491static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002492{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002493 /*
2494 * Filter_hash being empty will default to trace module.
2495 * But notrace hash requires a test of individual module functions.
2496 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002497 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2498 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002499}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002500
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002501/*
2502 * Check if the current ops references the record.
2503 *
2504 * If the ops traces all functions, then it was already accounted for.
2505 * If the ops does not trace the current record function, skip it.
2506 * If the ops ignores the function via notrace filter, skip it.
2507 */
2508static inline bool
2509ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2510{
2511 /* If ops isn't enabled, ignore it */
2512 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2513 return 0;
2514
2515 /* If ops traces all mods, we already accounted for it */
2516 if (ops_traces_mod(ops))
2517 return 0;
2518
2519 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002520 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2521 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002522 return 0;
2523
2524 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002525 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002526 return 0;
2527
2528 return 1;
2529}
2530
2531static int referenced_filters(struct dyn_ftrace *rec)
2532{
2533 struct ftrace_ops *ops;
2534 int cnt = 0;
2535
2536 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2537 if (ops_references_rec(ops, rec))
2538 cnt++;
2539 }
2540
2541 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002542}
2543
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002544static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002545{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002546 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002547 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302548 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002549 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002550 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002551 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002552 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002553
2554 /*
2555 * When adding a module, we need to check if tracers are
2556 * currently enabled and if they are set to trace all functions.
2557 * If they are, we need to enable the module functions as well
2558 * as update the reference counts for those function records.
2559 */
2560 if (mod) {
2561 struct ftrace_ops *ops;
2562
2563 for (ops = ftrace_ops_list;
2564 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002565 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2566 if (ops_traces_mod(ops))
2567 ref++;
2568 else
2569 test = true;
2570 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002571 }
2572 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002573
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002574 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002575
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002576 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302577
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002578 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002579 int cnt = ref;
2580
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002581 /* If something went wrong, bail without enabling anything */
2582 if (unlikely(ftrace_disabled))
2583 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002584
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002585 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002586 if (test)
2587 cnt += referenced_filters(p);
2588 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302589
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002590 /*
2591 * Do the initial record conversion from mcount jump
2592 * to the NOP instructions.
2593 */
2594 if (!ftrace_code_disable(mod, p))
2595 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002596
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002597 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002598
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002599 /*
2600 * If the tracing is enabled, go ahead and enable the record.
2601 *
2602 * The reason not to enable the record immediatelly is the
2603 * inherent check of ftrace_make_nop/ftrace_make_call for
2604 * correct previous instructions. Making first the NOP
2605 * conversion puts the module to the correct state, thus
2606 * passing the ftrace_make_call check.
2607 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002608 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002609 int failed = __ftrace_replace_code(p, 1);
2610 if (failed)
2611 ftrace_bug(failed, p->ip);
2612 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002613 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002614 }
2615
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002616 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002617 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002618 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002619
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002620 return 0;
2621}
2622
Steven Rostedta7900872011-12-16 16:23:44 -05002623static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002624{
Steven Rostedta7900872011-12-16 16:23:44 -05002625 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002626 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002627
Steven Rostedta7900872011-12-16 16:23:44 -05002628 if (WARN_ON(!count))
2629 return -EINVAL;
2630
2631 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002632
2633 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002634 * We want to fill as much as possible. No more than a page
2635 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002636 */
Steven Rostedta7900872011-12-16 16:23:44 -05002637 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2638 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002639
Steven Rostedta7900872011-12-16 16:23:44 -05002640 again:
2641 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2642
2643 if (!pg->records) {
2644 /* if we can't allocate this size, try something smaller */
2645 if (!order)
2646 return -ENOMEM;
2647 order >>= 1;
2648 goto again;
2649 }
2650
2651 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2652 pg->size = cnt;
2653
2654 if (cnt > count)
2655 cnt = count;
2656
2657 return cnt;
2658}
2659
2660static struct ftrace_page *
2661ftrace_allocate_pages(unsigned long num_to_init)
2662{
2663 struct ftrace_page *start_pg;
2664 struct ftrace_page *pg;
2665 int order;
2666 int cnt;
2667
2668 if (!num_to_init)
2669 return 0;
2670
2671 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2672 if (!pg)
2673 return NULL;
2674
2675 /*
2676 * Try to allocate as much as possible in one continues
2677 * location that fills in all of the space. We want to
2678 * waste as little space as possible.
2679 */
2680 for (;;) {
2681 cnt = ftrace_allocate_records(pg, num_to_init);
2682 if (cnt < 0)
2683 goto free_pages;
2684
2685 num_to_init -= cnt;
2686 if (!num_to_init)
2687 break;
2688
2689 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2690 if (!pg->next)
2691 goto free_pages;
2692
2693 pg = pg->next;
2694 }
2695
2696 return start_pg;
2697
2698 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002699 pg = start_pg;
2700 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002701 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2702 free_pages((unsigned long)pg->records, order);
2703 start_pg = pg->next;
2704 kfree(pg);
2705 pg = start_pg;
2706 }
2707 pr_info("ftrace: FAILED to allocate memory for functions\n");
2708 return NULL;
2709}
2710
Steven Rostedt5072c592008-05-12 21:20:43 +02002711#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2712
2713struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002714 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002715 loff_t func_pos;
2716 struct ftrace_page *pg;
2717 struct dyn_ftrace *func;
2718 struct ftrace_func_probe *probe;
2719 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002720 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002721 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002722 int hidx;
2723 int idx;
2724 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002725};
2726
Ingo Molnare309b412008-05-12 21:20:51 +02002727static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002728t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002729{
2730 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002731 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002732 struct hlist_head *hhd;
2733
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002734 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002735 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002736
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002737 if (iter->probe)
2738 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002739 retry:
2740 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2741 return NULL;
2742
2743 hhd = &ftrace_func_hash[iter->hidx];
2744
2745 if (hlist_empty(hhd)) {
2746 iter->hidx++;
2747 hnd = NULL;
2748 goto retry;
2749 }
2750
2751 if (!hnd)
2752 hnd = hhd->first;
2753 else {
2754 hnd = hnd->next;
2755 if (!hnd) {
2756 iter->hidx++;
2757 goto retry;
2758 }
2759 }
2760
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002761 if (WARN_ON_ONCE(!hnd))
2762 return NULL;
2763
2764 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2765
2766 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002767}
2768
2769static void *t_hash_start(struct seq_file *m, loff_t *pos)
2770{
2771 struct ftrace_iterator *iter = m->private;
2772 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002773 loff_t l;
2774
Steven Rostedt69a30832011-12-19 15:21:16 -05002775 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2776 return NULL;
2777
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002778 if (iter->func_pos > *pos)
2779 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002780
Li Zefand82d6242009-06-24 09:54:54 +08002781 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002782 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002783 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002784 if (!p)
2785 break;
2786 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002787 if (!p)
2788 return NULL;
2789
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002790 /* Only set this if we have an item */
2791 iter->flags |= FTRACE_ITER_HASH;
2792
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002793 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002794}
2795
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002796static int
2797t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002798{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002799 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002800
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002801 rec = iter->probe;
2802 if (WARN_ON_ONCE(!rec))
2803 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002804
Steven Rostedt809dcf22009-02-16 23:06:01 -05002805 if (rec->ops->print)
2806 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2807
Steven Rostedtb375a112009-09-17 00:05:58 -04002808 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002809
2810 if (rec->data)
2811 seq_printf(m, ":%p", rec->data);
2812 seq_putc(m, '\n');
2813
2814 return 0;
2815}
2816
2817static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002818t_next(struct seq_file *m, void *v, loff_t *pos)
2819{
2820 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002821 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002822 struct dyn_ftrace *rec = NULL;
2823
Steven Rostedt45a4a232011-04-21 23:16:46 -04002824 if (unlikely(ftrace_disabled))
2825 return NULL;
2826
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002827 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002828 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002829
Steven Rostedt5072c592008-05-12 21:20:43 +02002830 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002831 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002832
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002833 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002834 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002835
Steven Rostedt5072c592008-05-12 21:20:43 +02002836 retry:
2837 if (iter->idx >= iter->pg->index) {
2838 if (iter->pg->next) {
2839 iter->pg = iter->pg->next;
2840 iter->idx = 0;
2841 goto retry;
2842 }
2843 } else {
2844 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002845 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002846 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002847
Steven Rostedt41c52c02008-05-22 11:46:33 -04002848 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002849 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04002850
2851 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002852 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002853
Steven Rostedt5072c592008-05-12 21:20:43 +02002854 rec = NULL;
2855 goto retry;
2856 }
2857 }
2858
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002859 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002860 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002861
2862 iter->func = rec;
2863
2864 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002865}
2866
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002867static void reset_iter_read(struct ftrace_iterator *iter)
2868{
2869 iter->pos = 0;
2870 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002871 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002872}
2873
2874static void *t_start(struct seq_file *m, loff_t *pos)
2875{
2876 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002877 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002878 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002879 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002880
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002881 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002882
2883 if (unlikely(ftrace_disabled))
2884 return NULL;
2885
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002886 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002887 * If an lseek was done, then reset and start from beginning.
2888 */
2889 if (*pos < iter->pos)
2890 reset_iter_read(iter);
2891
2892 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002893 * For set_ftrace_filter reading, if we have the filter
2894 * off, we can short cut and just print out that all
2895 * functions are enabled.
2896 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002897 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002898 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002899 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002900 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002901 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002902 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002903 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002904 /* reset in case of seek/pread */
2905 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002906 return iter;
2907 }
2908
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002909 if (iter->flags & FTRACE_ITER_HASH)
2910 return t_hash_start(m, pos);
2911
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002912 /*
2913 * Unfortunately, we need to restart at ftrace_pages_start
2914 * every time we let go of the ftrace_mutex. This is because
2915 * those pointers can change without the lock.
2916 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002917 iter->pg = ftrace_pages_start;
2918 iter->idx = 0;
2919 for (l = 0; l <= *pos; ) {
2920 p = t_next(m, p, &l);
2921 if (!p)
2922 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002923 }
walimis5821e1b2008-11-15 15:19:06 +08002924
Steven Rostedt69a30832011-12-19 15:21:16 -05002925 if (!p)
2926 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002927
2928 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002929}
2930
2931static void t_stop(struct seq_file *m, void *p)
2932{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002933 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002934}
2935
2936static int t_show(struct seq_file *m, void *v)
2937{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002938 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002939 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002940
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002941 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002942 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002943
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002944 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002945 if (iter->flags & FTRACE_ITER_NOTRACE)
2946 seq_printf(m, "#### no functions disabled ####\n");
2947 else
2948 seq_printf(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002949 return 0;
2950 }
2951
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002952 rec = iter->func;
2953
Steven Rostedt5072c592008-05-12 21:20:43 +02002954 if (!rec)
2955 return 0;
2956
Steven Rostedt647bcd02011-05-03 14:39:21 -04002957 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002958 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002959 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002960 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04002961 rec->flags & FTRACE_FL_REGS ? " R" : " ");
2962 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2963 struct ftrace_ops *ops;
2964
2965 ops = ftrace_find_tramp_ops_curr(rec);
2966 if (ops && ops->trampoline)
2967 seq_printf(m, "\ttramp: %pS",
2968 (void *)ops->trampoline);
2969 else
2970 seq_printf(m, "\ttramp: ERROR!");
2971 }
2972 }
2973
Steven Rostedt647bcd02011-05-03 14:39:21 -04002974 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002975
2976 return 0;
2977}
2978
James Morris88e9d342009-09-22 16:43:43 -07002979static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002980 .start = t_start,
2981 .next = t_next,
2982 .stop = t_stop,
2983 .show = t_show,
2984};
2985
Ingo Molnare309b412008-05-12 21:20:51 +02002986static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002987ftrace_avail_open(struct inode *inode, struct file *file)
2988{
2989 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002990
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002991 if (unlikely(ftrace_disabled))
2992 return -ENODEV;
2993
Jiri Olsa50e18b92012-04-25 10:23:39 +02002994 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2995 if (iter) {
2996 iter->pg = ftrace_pages_start;
2997 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002998 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002999
Jiri Olsa50e18b92012-04-25 10:23:39 +02003000 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003001}
3002
Steven Rostedt647bcd02011-05-03 14:39:21 -04003003static int
3004ftrace_enabled_open(struct inode *inode, struct file *file)
3005{
3006 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003007
3008 if (unlikely(ftrace_disabled))
3009 return -ENODEV;
3010
Jiri Olsa50e18b92012-04-25 10:23:39 +02003011 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3012 if (iter) {
3013 iter->pg = ftrace_pages_start;
3014 iter->flags = FTRACE_ITER_ENABLED;
3015 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003016 }
3017
Jiri Olsa50e18b92012-04-25 10:23:39 +02003018 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003019}
3020
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003021/**
3022 * ftrace_regex_open - initialize function tracer filter files
3023 * @ops: The ftrace_ops that hold the hash filters
3024 * @flag: The type of filter to process
3025 * @inode: The inode, usually passed in to your open routine
3026 * @file: The file, usually passed in to your open routine
3027 *
3028 * ftrace_regex_open() initializes the filter files for the
3029 * @ops. Depending on @flag it may process the filter hash or
3030 * the notrace hash of @ops. With this called from the open
3031 * routine, you can use ftrace_filter_write() for the write
3032 * routine if @flag has FTRACE_ITER_FILTER set, or
3033 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003034 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003035 * release must call ftrace_regex_release().
3036 */
3037int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003038ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003039 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003040{
3041 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003042 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003043 int ret = 0;
3044
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003045 ftrace_ops_init(ops);
3046
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003047 if (unlikely(ftrace_disabled))
3048 return -ENODEV;
3049
Steven Rostedt5072c592008-05-12 21:20:43 +02003050 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3051 if (!iter)
3052 return -ENOMEM;
3053
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003054 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3055 kfree(iter);
3056 return -ENOMEM;
3057 }
3058
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003059 iter->ops = ops;
3060 iter->flags = flag;
3061
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003062 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003063
Steven Rostedtf45948e2011-05-02 12:29:25 -04003064 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003065 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003066 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003067 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003068
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003069 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003070 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3071
3072 if (file->f_flags & O_TRUNC)
3073 iter->hash = alloc_ftrace_hash(size_bits);
3074 else
3075 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3076
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003077 if (!iter->hash) {
3078 trace_parser_put(&iter->parser);
3079 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003080 ret = -ENOMEM;
3081 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003082 }
3083 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003084
Steven Rostedt5072c592008-05-12 21:20:43 +02003085 if (file->f_mode & FMODE_READ) {
3086 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003087
3088 ret = seq_open(file, &show_ftrace_seq_ops);
3089 if (!ret) {
3090 struct seq_file *m = file->private_data;
3091 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003092 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003093 /* Failed */
3094 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003095 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003096 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003097 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003098 } else
3099 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003100
3101 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003102 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003103
3104 return ret;
3105}
3106
Steven Rostedt41c52c02008-05-22 11:46:33 -04003107static int
3108ftrace_filter_open(struct inode *inode, struct file *file)
3109{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003110 struct ftrace_ops *ops = inode->i_private;
3111
3112 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003113 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3114 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003115}
3116
3117static int
3118ftrace_notrace_open(struct inode *inode, struct file *file)
3119{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003120 struct ftrace_ops *ops = inode->i_private;
3121
3122 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003123 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003124}
3125
Steven Rostedt64e7c442009-02-13 17:08:48 -05003126static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003127{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003128 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003129 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003130
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003131 switch (type) {
3132 case MATCH_FULL:
3133 if (strcmp(str, regex) == 0)
3134 matched = 1;
3135 break;
3136 case MATCH_FRONT_ONLY:
3137 if (strncmp(str, regex, len) == 0)
3138 matched = 1;
3139 break;
3140 case MATCH_MIDDLE_ONLY:
3141 if (strstr(str, regex))
3142 matched = 1;
3143 break;
3144 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003145 slen = strlen(str);
3146 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003147 matched = 1;
3148 break;
3149 }
3150
3151 return matched;
3152}
3153
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003154static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003155enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003156{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003157 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003158 int ret = 0;
3159
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003160 entry = ftrace_lookup_ip(hash, rec->ip);
3161 if (not) {
3162 /* Do nothing if it doesn't exist */
3163 if (!entry)
3164 return 0;
3165
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003166 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003167 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003168 /* Do nothing if it exists */
3169 if (entry)
3170 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003171
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003172 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003173 }
3174 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003175}
3176
Steven Rostedt64e7c442009-02-13 17:08:48 -05003177static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003178ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3179 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003180{
3181 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003182 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003183
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003184 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3185
3186 if (mod) {
3187 /* module lookup requires matching the module */
3188 if (!modname || strcmp(modname, mod))
3189 return 0;
3190
3191 /* blank search means to match all funcs in the mod */
3192 if (!len)
3193 return 1;
3194 }
3195
Steven Rostedt64e7c442009-02-13 17:08:48 -05003196 return ftrace_match(str, regex, len, type);
3197}
3198
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003199static int
3200match_records(struct ftrace_hash *hash, char *buff,
3201 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003202{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003203 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003204 struct ftrace_page *pg;
3205 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003206 int type = MATCH_FULL;
3207 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003208 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003209 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003210
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003211 if (len) {
3212 type = filter_parse_regex(buff, len, &search, &not);
3213 search_len = strlen(search);
3214 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003215
Steven Rostedt52baf112009-02-14 01:15:39 -05003216 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003217
3218 if (unlikely(ftrace_disabled))
3219 goto out_unlock;
3220
Steven Rostedt265c8312009-02-13 12:43:56 -05003221 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003222 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003223 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003224 if (ret < 0) {
3225 found = ret;
3226 goto out_unlock;
3227 }
Li Zefan311d16d2009-12-08 11:15:11 +08003228 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003229 }
3230 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003231 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003232 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003233
3234 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003235}
3236
Steven Rostedt64e7c442009-02-13 17:08:48 -05003237static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003238ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003239{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003240 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003241}
3242
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003243static int
3244ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003245{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003246 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003247
Steven Rostedt64e7c442009-02-13 17:08:48 -05003248 /* blank or '*' mean the same */
3249 if (strcmp(buff, "*") == 0)
3250 buff[0] = 0;
3251
3252 /* handle the case of 'dont filter this module' */
3253 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3254 buff[0] = 0;
3255 not = 1;
3256 }
3257
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003258 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003259}
3260
Steven Rostedtf6180772009-02-14 00:40:25 -05003261/*
3262 * We register the module command as a template to show others how
3263 * to register the a command as well.
3264 */
3265
3266static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003267ftrace_mod_callback(struct ftrace_hash *hash,
3268 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003269{
3270 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003271 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003272
3273 /*
3274 * cmd == 'mod' because we only registered this func
3275 * for the 'mod' ftrace_func_command.
3276 * But if you register one func with multiple commands,
3277 * you can tell which command was used by the cmd
3278 * parameter.
3279 */
3280
3281 /* we must have a module name */
3282 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003283 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003284
3285 mod = strsep(&param, ":");
3286 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003287 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003288
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003289 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003290 if (!ret)
3291 ret = -EINVAL;
3292 if (ret < 0)
3293 return ret;
3294
3295 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003296}
3297
3298static struct ftrace_func_command ftrace_mod_cmd = {
3299 .name = "mod",
3300 .func = ftrace_mod_callback,
3301};
3302
3303static int __init ftrace_mod_cmd_init(void)
3304{
3305 return register_ftrace_command(&ftrace_mod_cmd);
3306}
Steven Rostedt6f415672012-10-05 12:13:07 -04003307core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003308
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003309static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003310 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003311{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003312 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003313 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003314 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003315
3316 key = hash_long(ip, FTRACE_HASH_BITS);
3317
3318 hhd = &ftrace_func_hash[key];
3319
3320 if (hlist_empty(hhd))
3321 return;
3322
3323 /*
3324 * Disable preemption for these calls to prevent a RCU grace
3325 * period. This syncs the hash iteration and freeing of items
3326 * on the hash. rcu_read_lock is too dangerous here.
3327 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003328 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003329 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003330 if (entry->ip == ip)
3331 entry->ops->func(ip, parent_ip, &entry->data);
3332 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003333 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003334}
3335
Steven Rostedtb6887d72009-02-17 12:32:04 -05003336static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003337{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003338 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003339 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003340 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003341};
3342
Steven Rostedtb6887d72009-02-17 12:32:04 -05003343static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003344
Steven Rostedtb6887d72009-02-17 12:32:04 -05003345static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003346{
Steven Rostedtb8489142011-05-04 09:27:52 -04003347 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003348 int i;
3349
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003350 if (ftrace_probe_registered) {
3351 /* still need to update the function call sites */
3352 if (ftrace_enabled)
3353 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003354 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003355 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003356
3357 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3358 struct hlist_head *hhd = &ftrace_func_hash[i];
3359 if (hhd->first)
3360 break;
3361 }
3362 /* Nothing registered? */
3363 if (i == FTRACE_FUNC_HASHSIZE)
3364 return;
3365
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003366 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003367
Steven Rostedtb6887d72009-02-17 12:32:04 -05003368 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003369}
3370
Steven Rostedtb6887d72009-02-17 12:32:04 -05003371static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003372{
3373 int i;
3374
Steven Rostedtb6887d72009-02-17 12:32:04 -05003375 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003376 return;
3377
3378 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3379 struct hlist_head *hhd = &ftrace_func_hash[i];
3380 if (hhd->first)
3381 return;
3382 }
3383
3384 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003385 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003386
Steven Rostedtb6887d72009-02-17 12:32:04 -05003387 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003388}
3389
3390
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003391static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003392{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003393 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003394 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003395 kfree(entry);
3396}
3397
Steven Rostedt59df055f2009-02-14 15:29:06 -05003398int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003399register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003400 void *data)
3401{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003402 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003403 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003404 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003405 struct ftrace_page *pg;
3406 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003407 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003408 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003409 int count = 0;
3410 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003411 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003412
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003413 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003414 len = strlen(search);
3415
Steven Rostedtb6887d72009-02-17 12:32:04 -05003416 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003417 if (WARN_ON(not))
3418 return -EINVAL;
3419
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003420 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003421
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003422 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3423 if (!hash) {
3424 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003425 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003426 }
3427
3428 if (unlikely(ftrace_disabled)) {
3429 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003430 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003431 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003432
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003433 mutex_lock(&ftrace_lock);
3434
Steven Rostedt59df055f2009-02-14 15:29:06 -05003435 do_for_each_ftrace_rec(pg, rec) {
3436
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003437 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003438 continue;
3439
3440 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3441 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003442 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003443 if (!count)
3444 count = -ENOMEM;
3445 goto out_unlock;
3446 }
3447
3448 count++;
3449
3450 entry->data = data;
3451
3452 /*
3453 * The caller might want to do something special
3454 * for each function we find. We call the callback
3455 * to give the caller an opportunity to do so.
3456 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003457 if (ops->init) {
3458 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003459 /* caller does not like this func */
3460 kfree(entry);
3461 continue;
3462 }
3463 }
3464
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003465 ret = enter_record(hash, rec, 0);
3466 if (ret < 0) {
3467 kfree(entry);
3468 count = ret;
3469 goto out_unlock;
3470 }
3471
Steven Rostedt59df055f2009-02-14 15:29:06 -05003472 entry->ops = ops;
3473 entry->ip = rec->ip;
3474
3475 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3476 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3477
3478 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003479
3480 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3481 if (ret < 0)
3482 count = ret;
3483
Steven Rostedtb6887d72009-02-17 12:32:04 -05003484 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003485
3486 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003487 mutex_unlock(&ftrace_lock);
3488 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003489 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003490 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003491
3492 return count;
3493}
3494
3495enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003496 PROBE_TEST_FUNC = 1,
3497 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003498};
3499
3500static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003501__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003502 void *data, int flags)
3503{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003504 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003505 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003506 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003507 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003508 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003509 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003510 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003511 char str[KSYM_SYMBOL_LEN];
3512 int type = MATCH_FULL;
3513 int i, len = 0;
3514 char *search;
3515
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003516 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003517 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003518 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003519 int not;
3520
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003521 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003522 len = strlen(search);
3523
Steven Rostedtb6887d72009-02-17 12:32:04 -05003524 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003525 if (WARN_ON(not))
3526 return;
3527 }
3528
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003529 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003530
3531 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3532 if (!hash)
3533 /* Hmm, should report this somehow */
3534 goto out_unlock;
3535
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003536 INIT_LIST_HEAD(&free_list);
3537
Steven Rostedt59df055f2009-02-14 15:29:06 -05003538 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3539 struct hlist_head *hhd = &ftrace_func_hash[i];
3540
Sasha Levinb67bfe02013-02-27 17:06:00 -08003541 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003542
3543 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003544 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003545 continue;
3546
Steven Rostedtb6887d72009-02-17 12:32:04 -05003547 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003548 continue;
3549
3550 /* do this last, since it is the most expensive */
3551 if (glob) {
3552 kallsyms_lookup(entry->ip, NULL, NULL,
3553 NULL, str);
3554 if (!ftrace_match(str, glob, len, type))
3555 continue;
3556 }
3557
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003558 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3559 /* It is possible more than one entry had this ip */
3560 if (rec_entry)
3561 free_hash_entry(hash, rec_entry);
3562
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003563 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003564 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003565 }
3566 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003567 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003568 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003569 /*
3570 * Remove after the disable is called. Otherwise, if the last
3571 * probe is removed, a null hash means *all enabled*.
3572 */
3573 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003574 synchronize_sched();
3575 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3576 list_del(&entry->free_list);
3577 ftrace_free_entry(entry);
3578 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003579 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003580
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003581 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003582 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003583 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003584}
3585
3586void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003587unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003588 void *data)
3589{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003590 __unregister_ftrace_function_probe(glob, ops, data,
3591 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003592}
3593
3594void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003595unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003596{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003597 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003598}
3599
Steven Rostedtb6887d72009-02-17 12:32:04 -05003600void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003601{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003602 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003603}
3604
Steven Rostedtf6180772009-02-14 00:40:25 -05003605static LIST_HEAD(ftrace_commands);
3606static DEFINE_MUTEX(ftrace_cmd_mutex);
3607
Tom Zanussi38de93a2013-10-24 08:34:18 -05003608/*
3609 * Currently we only register ftrace commands from __init, so mark this
3610 * __init too.
3611 */
3612__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003613{
3614 struct ftrace_func_command *p;
3615 int ret = 0;
3616
3617 mutex_lock(&ftrace_cmd_mutex);
3618 list_for_each_entry(p, &ftrace_commands, list) {
3619 if (strcmp(cmd->name, p->name) == 0) {
3620 ret = -EBUSY;
3621 goto out_unlock;
3622 }
3623 }
3624 list_add(&cmd->list, &ftrace_commands);
3625 out_unlock:
3626 mutex_unlock(&ftrace_cmd_mutex);
3627
3628 return ret;
3629}
3630
Tom Zanussi38de93a2013-10-24 08:34:18 -05003631/*
3632 * Currently we only unregister ftrace commands from __init, so mark
3633 * this __init too.
3634 */
3635__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003636{
3637 struct ftrace_func_command *p, *n;
3638 int ret = -ENODEV;
3639
3640 mutex_lock(&ftrace_cmd_mutex);
3641 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3642 if (strcmp(cmd->name, p->name) == 0) {
3643 ret = 0;
3644 list_del_init(&p->list);
3645 goto out_unlock;
3646 }
3647 }
3648 out_unlock:
3649 mutex_unlock(&ftrace_cmd_mutex);
3650
3651 return ret;
3652}
3653
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003654static int ftrace_process_regex(struct ftrace_hash *hash,
3655 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003656{
Steven Rostedtf6180772009-02-14 00:40:25 -05003657 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003658 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003659 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003660
3661 func = strsep(&next, ":");
3662
3663 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003664 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003665 if (!ret)
3666 ret = -EINVAL;
3667 if (ret < 0)
3668 return ret;
3669 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003670 }
3671
Steven Rostedtf6180772009-02-14 00:40:25 -05003672 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003673
3674 command = strsep(&next, ":");
3675
Steven Rostedtf6180772009-02-14 00:40:25 -05003676 mutex_lock(&ftrace_cmd_mutex);
3677 list_for_each_entry(p, &ftrace_commands, list) {
3678 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003679 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003680 goto out_unlock;
3681 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003682 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003683 out_unlock:
3684 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003685
Steven Rostedtf6180772009-02-14 00:40:25 -05003686 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003687}
3688
Ingo Molnare309b412008-05-12 21:20:51 +02003689static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003690ftrace_regex_write(struct file *file, const char __user *ubuf,
3691 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003692{
3693 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003694 struct trace_parser *parser;
3695 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003696
Li Zefan4ba79782009-09-22 13:52:20 +08003697 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003698 return 0;
3699
Steven Rostedt5072c592008-05-12 21:20:43 +02003700 if (file->f_mode & FMODE_READ) {
3701 struct seq_file *m = file->private_data;
3702 iter = m->private;
3703 } else
3704 iter = file->private_data;
3705
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003706 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003707 return -ENODEV;
3708
3709 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003710
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003711 parser = &iter->parser;
3712 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003713
Li Zefan4ba79782009-09-22 13:52:20 +08003714 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003715 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003716 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003717 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003718 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003719 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003720 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003721 }
3722
Steven Rostedt5072c592008-05-12 21:20:43 +02003723 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003724 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003725 return ret;
3726}
3727
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003728ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003729ftrace_filter_write(struct file *file, const char __user *ubuf,
3730 size_t cnt, loff_t *ppos)
3731{
3732 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3733}
3734
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003735ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003736ftrace_notrace_write(struct file *file, const char __user *ubuf,
3737 size_t cnt, loff_t *ppos)
3738{
3739 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3740}
3741
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003742static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003743ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3744{
3745 struct ftrace_func_entry *entry;
3746
3747 if (!ftrace_location(ip))
3748 return -EINVAL;
3749
3750 if (remove) {
3751 entry = ftrace_lookup_ip(hash, ip);
3752 if (!entry)
3753 return -ENOENT;
3754 free_hash_entry(hash, entry);
3755 return 0;
3756 }
3757
3758 return add_hash_entry(hash, ip);
3759}
3760
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003761static void ftrace_ops_update_code(struct ftrace_ops *ops)
3762{
3763 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3764 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3765}
3766
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003767static int
3768ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3769 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003770{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003771 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003772 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003773 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003774
Steven Rostedt41c52c02008-05-22 11:46:33 -04003775 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003776 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003777
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003778 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003779
Steven Rostedtf45948e2011-05-02 12:29:25 -04003780 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003781 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003782 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003783 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003784
Wang Nanb972cc52014-07-15 08:40:20 +08003785 if (reset)
3786 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3787 else
3788 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3789
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003790 if (!hash) {
3791 ret = -ENOMEM;
3792 goto out_regex_unlock;
3793 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003794
Jiri Olsaac483c42012-01-02 10:04:14 +01003795 if (buf && !ftrace_match_records(hash, buf, len)) {
3796 ret = -EINVAL;
3797 goto out_regex_unlock;
3798 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003799 if (ip) {
3800 ret = ftrace_match_addr(hash, ip, remove);
3801 if (ret < 0)
3802 goto out_regex_unlock;
3803 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003804
3805 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003806 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003807 if (!ret)
3808 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003809
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003810 mutex_unlock(&ftrace_lock);
3811
Jiri Olsaac483c42012-01-02 10:04:14 +01003812 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003813 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003814
3815 free_ftrace_hash(hash);
3816 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003817}
3818
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003819static int
3820ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3821 int reset, int enable)
3822{
3823 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3824}
3825
3826/**
3827 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3828 * @ops - the ops to set the filter with
3829 * @ip - the address to add to or remove from the filter.
3830 * @remove - non zero to remove the ip from the filter
3831 * @reset - non zero to reset all filters before applying this filter.
3832 *
3833 * Filters denote which functions should be enabled when tracing is enabled
3834 * If @ip is NULL, it failes to update filter.
3835 */
3836int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3837 int remove, int reset)
3838{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003839 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003840 return ftrace_set_addr(ops, ip, remove, reset, 1);
3841}
3842EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3843
3844static int
3845ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3846 int reset, int enable)
3847{
3848 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3849}
3850
Steven Rostedt77a2b372008-05-12 21:20:45 +02003851/**
3852 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003853 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003854 * @buf - the string that holds the function filter text.
3855 * @len - the length of the string.
3856 * @reset - non zero to reset all filters before applying this filter.
3857 *
3858 * Filters denote which functions should be enabled when tracing is enabled.
3859 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3860 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003861int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003862 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003863{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003864 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003865 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003866}
Steven Rostedt936e0742011-05-05 22:54:01 -04003867EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003868
Steven Rostedt41c52c02008-05-22 11:46:33 -04003869/**
3870 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003871 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003872 * @buf - the string that holds the function notrace text.
3873 * @len - the length of the string.
3874 * @reset - non zero to reset all filters before applying this filter.
3875 *
3876 * Notrace Filters denote which functions should not be enabled when tracing
3877 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3878 * for tracing.
3879 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003880int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003881 int len, int reset)
3882{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003883 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003884 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003885}
3886EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3887/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003888 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003889 * @buf - the string that holds the function filter text.
3890 * @len - the length of the string.
3891 * @reset - non zero to reset all filters before applying this filter.
3892 *
3893 * Filters denote which functions should be enabled when tracing is enabled.
3894 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3895 */
3896void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3897{
3898 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3899}
3900EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3901
3902/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003903 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003904 * @buf - the string that holds the function notrace text.
3905 * @len - the length of the string.
3906 * @reset - non zero to reset all filters before applying this filter.
3907 *
3908 * Notrace Filters denote which functions should not be enabled when tracing
3909 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3910 * for tracing.
3911 */
3912void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003913{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003914 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003915}
Steven Rostedt936e0742011-05-05 22:54:01 -04003916EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003917
Steven Rostedt2af15d62009-05-28 13:37:24 -04003918/*
3919 * command line interface to allow users to set filters on boot up.
3920 */
3921#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3922static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3923static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3924
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003925/* Used by function selftest to not test if filter is set */
3926bool ftrace_filter_param __initdata;
3927
Steven Rostedt2af15d62009-05-28 13:37:24 -04003928static int __init set_ftrace_notrace(char *str)
3929{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003930 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003931 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003932 return 1;
3933}
3934__setup("ftrace_notrace=", set_ftrace_notrace);
3935
3936static int __init set_ftrace_filter(char *str)
3937{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003938 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003939 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003940 return 1;
3941}
3942__setup("ftrace_filter=", set_ftrace_filter);
3943
Stefan Assmann369bc182009-10-12 22:17:21 +02003944#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003945static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003946static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003947static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003948
Stefan Assmann369bc182009-10-12 22:17:21 +02003949static int __init set_graph_function(char *str)
3950{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003951 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003952 return 1;
3953}
3954__setup("ftrace_graph_filter=", set_graph_function);
3955
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003956static int __init set_graph_notrace_function(char *str)
3957{
3958 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
3959 return 1;
3960}
3961__setup("ftrace_graph_notrace=", set_graph_notrace_function);
3962
3963static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02003964{
3965 int ret;
3966 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003967 unsigned long *table = ftrace_graph_funcs;
3968 int *count = &ftrace_graph_count;
3969
3970 if (!enable) {
3971 table = ftrace_graph_notrace_funcs;
3972 count = &ftrace_graph_notrace_count;
3973 }
Stefan Assmann369bc182009-10-12 22:17:21 +02003974
3975 while (buf) {
3976 func = strsep(&buf, ",");
3977 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09003978 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003979 if (ret)
3980 printk(KERN_DEBUG "ftrace: function %s not "
3981 "traceable\n", func);
3982 }
3983}
3984#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3985
Steven Rostedt2a85a372011-12-19 21:57:44 -05003986void __init
3987ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003988{
3989 char *func;
3990
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003991 ftrace_ops_init(ops);
3992
Steven Rostedt2af15d62009-05-28 13:37:24 -04003993 while (buf) {
3994 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003995 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003996 }
3997}
3998
3999static void __init set_ftrace_early_filters(void)
4000{
4001 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004002 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004003 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004004 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004005#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4006 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004007 set_ftrace_early_graph(ftrace_graph_buf, 1);
4008 if (ftrace_graph_notrace_buf[0])
4009 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004010#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004011}
4012
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004013int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004014{
4015 struct seq_file *m = (struct seq_file *)file->private_data;
4016 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004017 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004018 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004019 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004020 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004021
Steven Rostedt5072c592008-05-12 21:20:43 +02004022 if (file->f_mode & FMODE_READ) {
4023 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004024 seq_release(inode, file);
4025 } else
4026 iter = file->private_data;
4027
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004028 parser = &iter->parser;
4029 if (trace_parser_loaded(parser)) {
4030 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004031 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004032 }
4033
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004034 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004035
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004036 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004037
Steven Rostedt058e2972011-04-29 22:35:33 -04004038 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004039 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4040
4041 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004042 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004043 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004044 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004045
Steven Rostedt058e2972011-04-29 22:35:33 -04004046 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004047 ret = ftrace_hash_move(iter->ops, filter_hash,
4048 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004049 if (!ret)
4050 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004051
Steven Rostedt058e2972011-04-29 22:35:33 -04004052 mutex_unlock(&ftrace_lock);
4053 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004054
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004055 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004056 free_ftrace_hash(iter->hash);
4057 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004058
Steven Rostedt5072c592008-05-12 21:20:43 +02004059 return 0;
4060}
4061
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004062static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004063 .open = ftrace_avail_open,
4064 .read = seq_read,
4065 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004066 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004067};
4068
Steven Rostedt647bcd02011-05-03 14:39:21 -04004069static const struct file_operations ftrace_enabled_fops = {
4070 .open = ftrace_enabled_open,
4071 .read = seq_read,
4072 .llseek = seq_lseek,
4073 .release = seq_release_private,
4074};
4075
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004076static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004077 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004078 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004079 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004080 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004081 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004082};
4083
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004084static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004085 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004086 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004087 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004088 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004089 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004090};
4091
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004092#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4093
4094static DEFINE_MUTEX(graph_lock);
4095
4096int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004097int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004098unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004099unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004100
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004101struct ftrace_graph_data {
4102 unsigned long *table;
4103 size_t size;
4104 int *count;
4105 const struct seq_operations *seq_ops;
4106};
4107
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004108static void *
Li Zefan85951842009-06-24 09:54:00 +08004109__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004110{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004111 struct ftrace_graph_data *fgd = m->private;
4112
4113 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004114 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004115 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004116}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004117
Li Zefan85951842009-06-24 09:54:00 +08004118static void *
4119g_next(struct seq_file *m, void *v, loff_t *pos)
4120{
4121 (*pos)++;
4122 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004123}
4124
4125static void *g_start(struct seq_file *m, loff_t *pos)
4126{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004127 struct ftrace_graph_data *fgd = m->private;
4128
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004129 mutex_lock(&graph_lock);
4130
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004131 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004132 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004133 return (void *)1;
4134
Li Zefan85951842009-06-24 09:54:00 +08004135 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004136}
4137
4138static void g_stop(struct seq_file *m, void *p)
4139{
4140 mutex_unlock(&graph_lock);
4141}
4142
4143static int g_show(struct seq_file *m, void *v)
4144{
4145 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004146
4147 if (!ptr)
4148 return 0;
4149
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004150 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004151 struct ftrace_graph_data *fgd = m->private;
4152
4153 if (fgd->table == ftrace_graph_funcs)
4154 seq_printf(m, "#### all functions enabled ####\n");
4155 else
4156 seq_printf(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004157 return 0;
4158 }
4159
Steven Rostedtb375a112009-09-17 00:05:58 -04004160 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004161
4162 return 0;
4163}
4164
James Morris88e9d342009-09-22 16:43:43 -07004165static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004166 .start = g_start,
4167 .next = g_next,
4168 .stop = g_stop,
4169 .show = g_show,
4170};
4171
4172static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004173__ftrace_graph_open(struct inode *inode, struct file *file,
4174 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004175{
4176 int ret = 0;
4177
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004178 mutex_lock(&graph_lock);
4179 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004180 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004181 *fgd->count = 0;
4182 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004183 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004184 mutex_unlock(&graph_lock);
4185
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004186 if (file->f_mode & FMODE_READ) {
4187 ret = seq_open(file, fgd->seq_ops);
4188 if (!ret) {
4189 struct seq_file *m = file->private_data;
4190 m->private = fgd;
4191 }
4192 } else
4193 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004194
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004195 return ret;
4196}
4197
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004198static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004199ftrace_graph_open(struct inode *inode, struct file *file)
4200{
4201 struct ftrace_graph_data *fgd;
4202
4203 if (unlikely(ftrace_disabled))
4204 return -ENODEV;
4205
4206 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4207 if (fgd == NULL)
4208 return -ENOMEM;
4209
4210 fgd->table = ftrace_graph_funcs;
4211 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4212 fgd->count = &ftrace_graph_count;
4213 fgd->seq_ops = &ftrace_graph_seq_ops;
4214
4215 return __ftrace_graph_open(inode, file, fgd);
4216}
4217
4218static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004219ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4220{
4221 struct ftrace_graph_data *fgd;
4222
4223 if (unlikely(ftrace_disabled))
4224 return -ENODEV;
4225
4226 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4227 if (fgd == NULL)
4228 return -ENOMEM;
4229
4230 fgd->table = ftrace_graph_notrace_funcs;
4231 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4232 fgd->count = &ftrace_graph_notrace_count;
4233 fgd->seq_ops = &ftrace_graph_seq_ops;
4234
4235 return __ftrace_graph_open(inode, file, fgd);
4236}
4237
4238static int
Li Zefan87827112009-07-23 11:29:11 +08004239ftrace_graph_release(struct inode *inode, struct file *file)
4240{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004241 if (file->f_mode & FMODE_READ) {
4242 struct seq_file *m = file->private_data;
4243
4244 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004245 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004246 } else {
4247 kfree(file->private_data);
4248 }
4249
Li Zefan87827112009-07-23 11:29:11 +08004250 return 0;
4251}
4252
4253static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004254ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004255{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004256 struct dyn_ftrace *rec;
4257 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004258 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004259 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004260 int type, not;
4261 char *search;
4262 bool exists;
4263 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004264
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004265 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004266 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004267 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004268 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004269
4270 search_len = strlen(search);
4271
Steven Rostedt52baf112009-02-14 01:15:39 -05004272 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004273
4274 if (unlikely(ftrace_disabled)) {
4275 mutex_unlock(&ftrace_lock);
4276 return -ENODEV;
4277 }
4278
Steven Rostedt265c8312009-02-13 12:43:56 -05004279 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004280
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004281 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004282 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004283 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004284 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004285 if (array[i] == rec->ip) {
4286 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004287 break;
4288 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004289 }
4290
4291 if (!not) {
4292 fail = 0;
4293 if (!exists) {
4294 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004295 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004296 goto out;
4297 }
4298 } else {
4299 if (exists) {
4300 array[i] = array[--(*idx)];
4301 array[*idx] = 0;
4302 fail = 0;
4303 }
4304 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004305 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004306 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004307out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004308 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004309
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004310 if (fail)
4311 return -EINVAL;
4312
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004313 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004314}
4315
4316static ssize_t
4317ftrace_graph_write(struct file *file, const char __user *ubuf,
4318 size_t cnt, loff_t *ppos)
4319{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004320 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004321 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004322 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004323
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004324 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004325 return 0;
4326
Namhyung Kim6a101082013-10-14 17:24:25 +09004327 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4328 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004329
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004330 read = trace_get_user(&parser, ubuf, cnt, ppos);
4331
Li Zefan4ba79782009-09-22 13:52:20 +08004332 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004333 parser.buffer[parser.idx] = 0;
4334
Namhyung Kim6a101082013-10-14 17:24:25 +09004335 mutex_lock(&graph_lock);
4336
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004337 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004338 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4339 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004340
4341 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004342 }
4343
Namhyung Kim6a101082013-10-14 17:24:25 +09004344 if (!ret)
4345 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004346
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004347 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004348
4349 return ret;
4350}
4351
4352static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004353 .open = ftrace_graph_open,
4354 .read = seq_read,
4355 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004356 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004357 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004358};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004359
4360static const struct file_operations ftrace_graph_notrace_fops = {
4361 .open = ftrace_graph_notrace_open,
4362 .read = seq_read,
4363 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004364 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004365 .release = ftrace_graph_release,
4366};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004367#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4368
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004369void ftrace_create_filter_files(struct ftrace_ops *ops,
4370 struct dentry *parent)
4371{
4372
4373 trace_create_file("set_ftrace_filter", 0644, parent,
4374 ops, &ftrace_filter_fops);
4375
4376 trace_create_file("set_ftrace_notrace", 0644, parent,
4377 ops, &ftrace_notrace_fops);
4378}
4379
4380/*
4381 * The name "destroy_filter_files" is really a misnomer. Although
4382 * in the future, it may actualy delete the files, but this is
4383 * really intended to make sure the ops passed in are disabled
4384 * and that when this function returns, the caller is free to
4385 * free the ops.
4386 *
4387 * The "destroy" name is only to match the "create" name that this
4388 * should be paired with.
4389 */
4390void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4391{
4392 mutex_lock(&ftrace_lock);
4393 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4394 ftrace_shutdown(ops, 0);
4395 ops->flags |= FTRACE_OPS_FL_DELETED;
4396 mutex_unlock(&ftrace_lock);
4397}
4398
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004399static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004400{
Steven Rostedt5072c592008-05-12 21:20:43 +02004401
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004402 trace_create_file("available_filter_functions", 0444,
4403 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004404
Steven Rostedt647bcd02011-05-03 14:39:21 -04004405 trace_create_file("enabled_functions", 0444,
4406 d_tracer, NULL, &ftrace_enabled_fops);
4407
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004408 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004409
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004410#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004411 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004412 NULL,
4413 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004414 trace_create_file("set_graph_notrace", 0444, d_tracer,
4415 NULL,
4416 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004417#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4418
Steven Rostedt5072c592008-05-12 21:20:43 +02004419 return 0;
4420}
4421
Steven Rostedt9fd49322012-04-24 22:32:06 -04004422static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004423{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004424 const unsigned long *ipa = a;
4425 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004426
Steven Rostedt9fd49322012-04-24 22:32:06 -04004427 if (*ipa > *ipb)
4428 return 1;
4429 if (*ipa < *ipb)
4430 return -1;
4431 return 0;
4432}
4433
4434static void ftrace_swap_ips(void *a, void *b, int size)
4435{
4436 unsigned long *ipa = a;
4437 unsigned long *ipb = b;
4438 unsigned long t;
4439
4440 t = *ipa;
4441 *ipa = *ipb;
4442 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004443}
4444
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004445static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004446 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004447 unsigned long *end)
4448{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004449 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004450 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004451 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004452 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004453 unsigned long *p;
4454 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004455 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004456 int ret = -ENOMEM;
4457
4458 count = end - start;
4459
4460 if (!count)
4461 return 0;
4462
Steven Rostedt9fd49322012-04-24 22:32:06 -04004463 sort(start, count, sizeof(*start),
4464 ftrace_cmp_ips, ftrace_swap_ips);
4465
Steven Rostedt706c81f2012-04-24 23:45:26 -04004466 start_pg = ftrace_allocate_pages(count);
4467 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004468 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004469
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004470 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004471
Steven Rostedt32082302011-12-16 14:42:37 -05004472 /*
4473 * Core and each module needs their own pages, as
4474 * modules will free them when they are removed.
4475 * Force a new page to be allocated for modules.
4476 */
Steven Rostedta7900872011-12-16 16:23:44 -05004477 if (!mod) {
4478 WARN_ON(ftrace_pages || ftrace_pages_start);
4479 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004480 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004481 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004482 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004483 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004484
Steven Rostedta7900872011-12-16 16:23:44 -05004485 if (WARN_ON(ftrace_pages->next)) {
4486 /* Hmm, we have free pages? */
4487 while (ftrace_pages->next)
4488 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004489 }
Steven Rostedta7900872011-12-16 16:23:44 -05004490
Steven Rostedt706c81f2012-04-24 23:45:26 -04004491 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004492 }
4493
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004494 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004495 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004496 while (p < end) {
4497 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004498 /*
4499 * Some architecture linkers will pad between
4500 * the different mcount_loc sections of different
4501 * object files to satisfy alignments.
4502 * Skip any NULL pointers.
4503 */
4504 if (!addr)
4505 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004506
4507 if (pg->index == pg->size) {
4508 /* We should have allocated enough */
4509 if (WARN_ON(!pg->next))
4510 break;
4511 pg = pg->next;
4512 }
4513
4514 rec = &pg->records[pg->index++];
4515 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004516 }
4517
Steven Rostedt706c81f2012-04-24 23:45:26 -04004518 /* We should have used all pages */
4519 WARN_ON(pg->next);
4520
4521 /* Assign the last page to ftrace_pages */
4522 ftrace_pages = pg;
4523
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004524 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004525 * We only need to disable interrupts on start up
4526 * because we are modifying code that an interrupt
4527 * may execute, and the modification is not atomic.
4528 * But for modules, nothing runs the code we modify
4529 * until we are finished with it, and there's no
4530 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004531 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004532 if (!mod)
4533 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004534 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004535 if (!mod)
4536 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004537 ret = 0;
4538 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004539 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004540
Steven Rostedta7900872011-12-16 16:23:44 -05004541 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004542}
4543
Steven Rostedt93eb6772009-04-15 13:24:06 -04004544#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004545
4546#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4547
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004548void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004549{
4550 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004551 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004552 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004553 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004554
Steven Rostedt93eb6772009-04-15 13:24:06 -04004555 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004556
4557 if (ftrace_disabled)
4558 goto out_unlock;
4559
Steven Rostedt32082302011-12-16 14:42:37 -05004560 /*
4561 * Each module has its own ftrace_pages, remove
4562 * them from the list.
4563 */
4564 last_pg = &ftrace_pages_start;
4565 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4566 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004567 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004568 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004569 * As core pages are first, the first
4570 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004571 */
Steven Rostedt32082302011-12-16 14:42:37 -05004572 if (WARN_ON(pg == ftrace_pages_start))
4573 goto out_unlock;
4574
4575 /* Check if we are deleting the last page */
4576 if (pg == ftrace_pages)
4577 ftrace_pages = next_to_ftrace_page(last_pg);
4578
4579 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004580 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4581 free_pages((unsigned long)pg->records, order);
4582 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004583 } else
4584 last_pg = &pg->next;
4585 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004586 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004587 mutex_unlock(&ftrace_lock);
4588}
4589
4590static void ftrace_init_module(struct module *mod,
4591 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004592{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004593 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004594 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004595 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004596}
4597
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004598void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004599{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004600 ftrace_init_module(mod, mod->ftrace_callsites,
4601 mod->ftrace_callsites +
4602 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004603}
4604
4605static int ftrace_module_notify_exit(struct notifier_block *self,
4606 unsigned long val, void *data)
4607{
4608 struct module *mod = data;
4609
4610 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004611 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004612
4613 return 0;
4614}
4615#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004616static int ftrace_module_notify_exit(struct notifier_block *self,
4617 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004618{
4619 return 0;
4620}
4621#endif /* CONFIG_MODULES */
4622
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004623struct notifier_block ftrace_module_exit_nb = {
4624 .notifier_call = ftrace_module_notify_exit,
4625 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4626};
4627
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004628void __init ftrace_init(void)
4629{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004630 extern unsigned long __start_mcount_loc[];
4631 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004632 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004633 int ret;
4634
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004635 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004636 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004637 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004638 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004639 goto failed;
4640
4641 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004642 if (!count) {
4643 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004644 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004645 }
4646
4647 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4648 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004649
4650 last_ftrace_enabled = ftrace_enabled = 1;
4651
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004652 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004653 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004654 __stop_mcount_loc);
4655
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004656 ret = register_module_notifier(&ftrace_module_exit_nb);
4657 if (ret)
4658 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004659
Steven Rostedt2af15d62009-05-28 13:37:24 -04004660 set_ftrace_early_filters();
4661
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004662 return;
4663 failed:
4664 ftrace_disabled = 1;
4665}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004666
Steven Rostedt3d083392008-05-12 21:20:42 +02004667#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004668
Steven Rostedt2b499382011-05-03 22:49:52 -04004669static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004670 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004671 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004672};
4673
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004674static int __init ftrace_nodyn_init(void)
4675{
4676 ftrace_enabled = 1;
4677 return 0;
4678}
Steven Rostedt6f415672012-10-05 12:13:07 -04004679core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004680
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004681static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4682static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004683/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004684# define ftrace_startup(ops, command) \
4685 ({ \
4686 int ___ret = __register_ftrace_function(ops); \
4687 if (!___ret) \
4688 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4689 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004690 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004691# define ftrace_shutdown(ops, command) \
4692 ({ \
4693 int ___ret = __unregister_ftrace_function(ops); \
4694 if (!___ret) \
4695 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4696 ___ret; \
4697 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004698
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004699# define ftrace_startup_sysctl() do { } while (0)
4700# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004701
4702static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004703ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004704{
4705 return 1;
4706}
4707
Steven Rostedt3d083392008-05-12 21:20:42 +02004708#endif /* CONFIG_DYNAMIC_FTRACE */
4709
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004710__init void ftrace_init_global_array_ops(struct trace_array *tr)
4711{
4712 tr->ops = &global_ops;
4713 tr->ops->private = tr;
4714}
4715
4716void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4717{
4718 /* If we filter on pids, update to use the pid function */
4719 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4720 if (WARN_ON(tr->ops->func != ftrace_stub))
4721 printk("ftrace ops had %pS for function\n",
4722 tr->ops->func);
4723 /* Only the top level instance does pid tracing */
4724 if (!list_empty(&ftrace_pids)) {
4725 set_ftrace_pid_function(func);
4726 func = ftrace_pid_func;
4727 }
4728 }
4729 tr->ops->func = func;
4730 tr->ops->private = tr;
4731}
4732
4733void ftrace_reset_array_ops(struct trace_array *tr)
4734{
4735 tr->ops->func = ftrace_stub;
4736}
4737
Steven Rostedtb8489142011-05-04 09:27:52 -04004738static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004739ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004740 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004741{
Jiri Olsae2484912012-02-15 15:51:48 +01004742 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4743 return;
4744
4745 /*
4746 * Some of the ops may be dynamically allocated,
4747 * they must be freed after a synchronize_sched().
4748 */
4749 preempt_disable_notrace();
4750 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004751
4752 /*
4753 * Control funcs (perf) uses RCU. Only trace if
4754 * RCU is currently active.
4755 */
4756 if (!rcu_is_watching())
4757 goto out;
4758
Steven Rostedt0a016402012-11-02 17:03:03 -04004759 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004760 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4761 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004762 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004763 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004764 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004765 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004766 trace_recursion_clear(TRACE_CONTROL_BIT);
4767 preempt_enable_notrace();
4768}
4769
4770static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004771 .func = ftrace_ops_control_func,
4772 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004773 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004774};
4775
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004776static inline void
4777__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004778 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004779{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004780 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004781 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004782
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004783 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4784 if (bit < 0)
4785 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004786
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004787 /*
4788 * Some of the ops may be dynamically allocated,
4789 * they must be freed after a synchronize_sched().
4790 */
4791 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004792 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004793 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004794 if (FTRACE_WARN_ON(!op->func)) {
4795 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004796 goto out;
4797 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004798 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004799 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004800 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004801out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004802 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004803 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004804}
4805
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004806/*
4807 * Some archs only support passing ip and parent_ip. Even though
4808 * the list function ignores the op parameter, we do not want any
4809 * C side effects, where a function is called without the caller
4810 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004811 * Archs are to support both the regs and ftrace_ops at the same time.
4812 * If they support ftrace_ops, it is assumed they support regs.
4813 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004814 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4815 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004816 * An architecture can pass partial regs with ftrace_ops and still
4817 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004818 */
4819#if ARCH_SUPPORTS_FTRACE_OPS
4820static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004821 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004822{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004823 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004824}
4825#else
4826static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4827{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004828 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004829}
4830#endif
4831
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04004832/*
4833 * If there's only one function registered but it does not support
4834 * recursion, this function will be called by the mcount trampoline.
4835 * This function will handle recursion protection.
4836 */
4837static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
4838 struct ftrace_ops *op, struct pt_regs *regs)
4839{
4840 int bit;
4841
4842 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4843 if (bit < 0)
4844 return;
4845
4846 op->func(ip, parent_ip, op, regs);
4847
4848 trace_clear_recursion(bit);
4849}
4850
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04004851/**
4852 * ftrace_ops_get_func - get the function a trampoline should call
4853 * @ops: the ops to get the function for
4854 *
4855 * Normally the mcount trampoline will call the ops->func, but there
4856 * are times that it should not. For example, if the ops does not
4857 * have its own recursion protection, then it should call the
4858 * ftrace_ops_recurs_func() instead.
4859 *
4860 * Returns the function that the trampoline should call for @ops.
4861 */
4862ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
4863{
4864 /*
4865 * If this is a dynamic ops or we force list func,
4866 * then it needs to call the list anyway.
4867 */
4868 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
4869 return ftrace_ops_list_func;
4870
4871 /*
4872 * If the func handles its own recursion, call it directly.
4873 * Otherwise call the recursion protected function that
4874 * will call the ftrace ops function.
4875 */
4876 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
4877 return ftrace_ops_recurs_func;
4878
4879 return ops->func;
4880}
4881
Steven Rostedte32d8952008-12-04 00:26:41 -05004882static void clear_ftrace_swapper(void)
4883{
4884 struct task_struct *p;
4885 int cpu;
4886
4887 get_online_cpus();
4888 for_each_online_cpu(cpu) {
4889 p = idle_task(cpu);
4890 clear_tsk_trace_trace(p);
4891 }
4892 put_online_cpus();
4893}
4894
4895static void set_ftrace_swapper(void)
4896{
4897 struct task_struct *p;
4898 int cpu;
4899
4900 get_online_cpus();
4901 for_each_online_cpu(cpu) {
4902 p = idle_task(cpu);
4903 set_tsk_trace_trace(p);
4904 }
4905 put_online_cpus();
4906}
4907
4908static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004909{
4910 struct task_struct *p;
4911
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004912 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004913 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004914 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004915 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004916 rcu_read_unlock();
4917
Steven Rostedte32d8952008-12-04 00:26:41 -05004918 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004919}
4920
Steven Rostedte32d8952008-12-04 00:26:41 -05004921static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004922{
4923 struct task_struct *p;
4924
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004925 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004926 do_each_pid_task(pid, PIDTYPE_PID, p) {
4927 set_tsk_trace_trace(p);
4928 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004929 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004930}
4931
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004932static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004933{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004934 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004935 clear_ftrace_swapper();
4936 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004937 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004938}
4939
4940static void set_ftrace_pid_task(struct pid *pid)
4941{
4942 if (pid == ftrace_swapper_pid)
4943 set_ftrace_swapper();
4944 else
4945 set_ftrace_pid(pid);
4946}
4947
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004948static int ftrace_pid_add(int p)
4949{
4950 struct pid *pid;
4951 struct ftrace_pid *fpid;
4952 int ret = -EINVAL;
4953
4954 mutex_lock(&ftrace_lock);
4955
4956 if (!p)
4957 pid = ftrace_swapper_pid;
4958 else
4959 pid = find_get_pid(p);
4960
4961 if (!pid)
4962 goto out;
4963
4964 ret = 0;
4965
4966 list_for_each_entry(fpid, &ftrace_pids, list)
4967 if (fpid->pid == pid)
4968 goto out_put;
4969
4970 ret = -ENOMEM;
4971
4972 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4973 if (!fpid)
4974 goto out_put;
4975
4976 list_add(&fpid->list, &ftrace_pids);
4977 fpid->pid = pid;
4978
4979 set_ftrace_pid_task(pid);
4980
4981 ftrace_update_pid_func();
4982 ftrace_startup_enable(0);
4983
4984 mutex_unlock(&ftrace_lock);
4985 return 0;
4986
4987out_put:
4988 if (pid != ftrace_swapper_pid)
4989 put_pid(pid);
4990
4991out:
4992 mutex_unlock(&ftrace_lock);
4993 return ret;
4994}
4995
4996static void ftrace_pid_reset(void)
4997{
4998 struct ftrace_pid *fpid, *safe;
4999
5000 mutex_lock(&ftrace_lock);
5001 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5002 struct pid *pid = fpid->pid;
5003
5004 clear_ftrace_pid_task(pid);
5005
5006 list_del(&fpid->list);
5007 kfree(fpid);
5008 }
5009
5010 ftrace_update_pid_func();
5011 ftrace_startup_enable(0);
5012
5013 mutex_unlock(&ftrace_lock);
5014}
5015
5016static void *fpid_start(struct seq_file *m, loff_t *pos)
5017{
5018 mutex_lock(&ftrace_lock);
5019
5020 if (list_empty(&ftrace_pids) && (!*pos))
5021 return (void *) 1;
5022
5023 return seq_list_start(&ftrace_pids, *pos);
5024}
5025
5026static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5027{
5028 if (v == (void *)1)
5029 return NULL;
5030
5031 return seq_list_next(v, &ftrace_pids, pos);
5032}
5033
5034static void fpid_stop(struct seq_file *m, void *p)
5035{
5036 mutex_unlock(&ftrace_lock);
5037}
5038
5039static int fpid_show(struct seq_file *m, void *v)
5040{
5041 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5042
5043 if (v == (void *)1) {
5044 seq_printf(m, "no pid\n");
5045 return 0;
5046 }
5047
5048 if (fpid->pid == ftrace_swapper_pid)
5049 seq_printf(m, "swapper tasks\n");
5050 else
5051 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5052
5053 return 0;
5054}
5055
5056static const struct seq_operations ftrace_pid_sops = {
5057 .start = fpid_start,
5058 .next = fpid_next,
5059 .stop = fpid_stop,
5060 .show = fpid_show,
5061};
5062
5063static int
5064ftrace_pid_open(struct inode *inode, struct file *file)
5065{
5066 int ret = 0;
5067
5068 if ((file->f_mode & FMODE_WRITE) &&
5069 (file->f_flags & O_TRUNC))
5070 ftrace_pid_reset();
5071
5072 if (file->f_mode & FMODE_READ)
5073 ret = seq_open(file, &ftrace_pid_sops);
5074
5075 return ret;
5076}
5077
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005078static ssize_t
5079ftrace_pid_write(struct file *filp, const char __user *ubuf,
5080 size_t cnt, loff_t *ppos)
5081{
Ingo Molnar457dc922009-11-23 11:03:28 +01005082 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005083 long val;
5084 int ret;
5085
5086 if (cnt >= sizeof(buf))
5087 return -EINVAL;
5088
5089 if (copy_from_user(&buf, ubuf, cnt))
5090 return -EFAULT;
5091
5092 buf[cnt] = 0;
5093
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005094 /*
5095 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5096 * to clean the filter quietly.
5097 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005098 tmp = strstrip(buf);
5099 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005100 return 1;
5101
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005102 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005103 if (ret < 0)
5104 return ret;
5105
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005106 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005107
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005108 return ret ? ret : cnt;
5109}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005110
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005111static int
5112ftrace_pid_release(struct inode *inode, struct file *file)
5113{
5114 if (file->f_mode & FMODE_READ)
5115 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005116
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005117 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005118}
5119
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005120static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005121 .open = ftrace_pid_open,
5122 .write = ftrace_pid_write,
5123 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005124 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005125 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005126};
5127
5128static __init int ftrace_init_debugfs(void)
5129{
5130 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005131
5132 d_tracer = tracing_init_dentry();
5133 if (!d_tracer)
5134 return 0;
5135
5136 ftrace_init_dyn_debugfs(d_tracer);
5137
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005138 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5139 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005140
5141 ftrace_profile_debugfs(d_tracer);
5142
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005143 return 0;
5144}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005145fs_initcall(ftrace_init_debugfs);
5146
Steven Rostedt3d083392008-05-12 21:20:42 +02005147/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005148 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005149 *
5150 * This function should be used by panic code. It stops ftrace
5151 * but in a not so nice way. If you need to simply kill ftrace
5152 * from a non-atomic section, use ftrace_kill.
5153 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005154void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005155{
5156 ftrace_disabled = 1;
5157 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005158 clear_ftrace_function();
5159}
5160
5161/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005162 * Test if ftrace is dead or not.
5163 */
5164int ftrace_is_dead(void)
5165{
5166 return ftrace_disabled;
5167}
5168
5169/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005170 * register_ftrace_function - register a function for profiling
5171 * @ops - ops structure that holds the function for profiling.
5172 *
5173 * Register a function to be called by all functions in the
5174 * kernel.
5175 *
5176 * Note: @ops->func and all the functions it calls must be labeled
5177 * with "notrace", otherwise it will go into a
5178 * recursive loop.
5179 */
5180int register_ftrace_function(struct ftrace_ops *ops)
5181{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005182 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005183
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005184 ftrace_ops_init(ops);
5185
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005186 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005187
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005188 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005189
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005190 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005191
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005192 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005193}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005194EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005195
5196/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005197 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005198 * @ops - ops structure that holds the function to unregister
5199 *
5200 * Unregister a function that was added to be called by ftrace profiling.
5201 */
5202int unregister_ftrace_function(struct ftrace_ops *ops)
5203{
5204 int ret;
5205
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005206 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005207 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005208 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005209
5210 return ret;
5211}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005212EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005213
Ingo Molnare309b412008-05-12 21:20:51 +02005214int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005215ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005216 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005217 loff_t *ppos)
5218{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005219 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005220
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005221 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005222
Steven Rostedt45a4a232011-04-21 23:16:46 -04005223 if (unlikely(ftrace_disabled))
5224 goto out;
5225
5226 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005227
Li Zefana32c7762009-06-26 16:55:51 +08005228 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005229 goto out;
5230
Li Zefana32c7762009-06-26 16:55:51 +08005231 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005232
5233 if (ftrace_enabled) {
5234
5235 ftrace_startup_sysctl();
5236
5237 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005238 if (ftrace_ops_list != &ftrace_list_end)
5239 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005240
5241 } else {
5242 /* stopping ftrace calls (just send to ftrace_stub) */
5243 ftrace_trace_function = ftrace_stub;
5244
5245 ftrace_shutdown_sysctl();
5246 }
5247
5248 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005249 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005250 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005251}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005252
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005253#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005254
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005255static struct ftrace_ops graph_ops = {
5256 .func = ftrace_stub,
5257 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5258 FTRACE_OPS_FL_INITIALIZED |
5259 FTRACE_OPS_FL_STUB,
5260#ifdef FTRACE_GRAPH_TRAMP_ADDR
5261 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5262#endif
5263 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5264};
5265
Steven Rostedt597af812009-04-03 15:24:12 -04005266static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005267
Steven Rostedte49dc192008-12-02 23:50:05 -05005268int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5269{
5270 return 0;
5271}
5272
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005273/* The callbacks that hook a function */
5274trace_func_graph_ret_t ftrace_graph_return =
5275 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005276trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005277static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005278
5279/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5280static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5281{
5282 int i;
5283 int ret = 0;
5284 unsigned long flags;
5285 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5286 struct task_struct *g, *t;
5287
5288 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5289 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5290 * sizeof(struct ftrace_ret_stack),
5291 GFP_KERNEL);
5292 if (!ret_stack_list[i]) {
5293 start = 0;
5294 end = i;
5295 ret = -ENOMEM;
5296 goto free;
5297 }
5298 }
5299
5300 read_lock_irqsave(&tasklist_lock, flags);
5301 do_each_thread(g, t) {
5302 if (start == end) {
5303 ret = -EAGAIN;
5304 goto unlock;
5305 }
5306
5307 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005308 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005309 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005310 t->curr_ret_stack = -1;
5311 /* Make sure the tasks see the -1 first: */
5312 smp_wmb();
5313 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005314 }
5315 } while_each_thread(g, t);
5316
5317unlock:
5318 read_unlock_irqrestore(&tasklist_lock, flags);
5319free:
5320 for (i = start; i < end; i++)
5321 kfree(ret_stack_list[i]);
5322 return ret;
5323}
5324
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005325static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005326ftrace_graph_probe_sched_switch(void *ignore,
5327 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005328{
5329 unsigned long long timestamp;
5330 int index;
5331
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005332 /*
5333 * Does the user want to count the time a function was asleep.
5334 * If so, do not update the time stamps.
5335 */
5336 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5337 return;
5338
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005339 timestamp = trace_clock_local();
5340
5341 prev->ftrace_timestamp = timestamp;
5342
5343 /* only process tasks that we timestamped */
5344 if (!next->ftrace_timestamp)
5345 return;
5346
5347 /*
5348 * Update all the counters in next to make up for the
5349 * time next was sleeping.
5350 */
5351 timestamp -= next->ftrace_timestamp;
5352
5353 for (index = next->curr_ret_stack; index >= 0; index--)
5354 next->ret_stack[index].calltime += timestamp;
5355}
5356
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005357/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005358static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005359{
5360 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005361 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005362
5363 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5364 sizeof(struct ftrace_ret_stack *),
5365 GFP_KERNEL);
5366
5367 if (!ret_stack_list)
5368 return -ENOMEM;
5369
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005370 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005371 for_each_online_cpu(cpu) {
5372 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005373 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005374 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005375
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005376 do {
5377 ret = alloc_retstack_tasklist(ret_stack_list);
5378 } while (ret == -EAGAIN);
5379
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005380 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005381 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005382 if (ret)
5383 pr_info("ftrace_graph: Couldn't activate tracepoint"
5384 " probe to kernel_sched_switch\n");
5385 }
5386
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005387 kfree(ret_stack_list);
5388 return ret;
5389}
5390
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005391/*
5392 * Hibernation protection.
5393 * The state of the current task is too much unstable during
5394 * suspend/restore to disk. We want to protect against that.
5395 */
5396static int
5397ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5398 void *unused)
5399{
5400 switch (state) {
5401 case PM_HIBERNATION_PREPARE:
5402 pause_graph_tracing();
5403 break;
5404
5405 case PM_POST_HIBERNATION:
5406 unpause_graph_tracing();
5407 break;
5408 }
5409 return NOTIFY_DONE;
5410}
5411
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005412static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5413{
5414 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5415 return 0;
5416 return __ftrace_graph_entry(trace);
5417}
5418
5419/*
5420 * The function graph tracer should only trace the functions defined
5421 * by set_ftrace_filter and set_ftrace_notrace. If another function
5422 * tracer ops is registered, the graph tracer requires testing the
5423 * function against the global ops, and not just trace any function
5424 * that any ftrace_ops registered.
5425 */
5426static void update_function_graph_func(void)
5427{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005428 struct ftrace_ops *op;
5429 bool do_test = false;
5430
5431 /*
5432 * The graph and global ops share the same set of functions
5433 * to test. If any other ops is on the list, then
5434 * the graph tracing needs to test if its the function
5435 * it should call.
5436 */
5437 do_for_each_ftrace_op(op, ftrace_ops_list) {
5438 if (op != &global_ops && op != &graph_ops &&
5439 op != &ftrace_list_end) {
5440 do_test = true;
5441 /* in double loop, break out with goto */
5442 goto out;
5443 }
5444 } while_for_each_ftrace_op(op);
5445 out:
5446 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005447 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005448 else
5449 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005450}
5451
Mathias Krause8275f692014-03-30 15:31:50 +02005452static struct notifier_block ftrace_suspend_notifier = {
5453 .notifier_call = ftrace_suspend_notifier_call,
5454};
5455
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005456int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5457 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005458{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005459 int ret = 0;
5460
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005461 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005462
Steven Rostedt05ce5812009-03-24 00:18:31 -04005463 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005464 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005465 ret = -EBUSY;
5466 goto out;
5467 }
5468
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005469 register_pm_notifier(&ftrace_suspend_notifier);
5470
Steven Rostedt597af812009-04-03 15:24:12 -04005471 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005472 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005473 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005474 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005475 goto out;
5476 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005477
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005478 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005479
5480 /*
5481 * Update the indirect function to the entryfunc, and the
5482 * function that gets called to the entry_test first. Then
5483 * call the update fgraph entry function to determine if
5484 * the entryfunc should be called directly or not.
5485 */
5486 __ftrace_graph_entry = entryfunc;
5487 ftrace_graph_entry = ftrace_graph_entry_test;
5488 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005489
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005490 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005491
5492out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005493 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005494 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005495}
5496
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005497void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005498{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005499 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005500
Steven Rostedt597af812009-04-03 15:24:12 -04005501 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005502 goto out;
5503
Steven Rostedt597af812009-04-03 15:24:12 -04005504 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005505 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005506 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005507 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005508 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005509 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005510 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005511
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005512 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005513 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005514}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005515
Steven Rostedt868baf02011-02-10 21:26:13 -05005516static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5517
5518static void
5519graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5520{
5521 atomic_set(&t->tracing_graph_pause, 0);
5522 atomic_set(&t->trace_overrun, 0);
5523 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005524 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005525 smp_wmb();
5526 t->ret_stack = ret_stack;
5527}
5528
5529/*
5530 * Allocate a return stack for the idle task. May be the first
5531 * time through, or it may be done by CPU hotplug online.
5532 */
5533void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5534{
5535 t->curr_ret_stack = -1;
5536 /*
5537 * The idle task has no parent, it either has its own
5538 * stack or no stack at all.
5539 */
5540 if (t->ret_stack)
5541 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5542
5543 if (ftrace_graph_active) {
5544 struct ftrace_ret_stack *ret_stack;
5545
5546 ret_stack = per_cpu(idle_ret_stack, cpu);
5547 if (!ret_stack) {
5548 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5549 * sizeof(struct ftrace_ret_stack),
5550 GFP_KERNEL);
5551 if (!ret_stack)
5552 return;
5553 per_cpu(idle_ret_stack, cpu) = ret_stack;
5554 }
5555 graph_init_task(t, ret_stack);
5556 }
5557}
5558
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005559/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005560void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005561{
Steven Rostedt84047e32009-06-02 16:51:55 -04005562 /* Make sure we do not use the parent ret_stack */
5563 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005564 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005565
Steven Rostedt597af812009-04-03 15:24:12 -04005566 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005567 struct ftrace_ret_stack *ret_stack;
5568
5569 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005570 * sizeof(struct ftrace_ret_stack),
5571 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005572 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005573 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005574 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005575 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005576}
5577
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005578void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005579{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005580 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5581
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005582 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005583 /* NULL must become visible to IRQs before we free it: */
5584 barrier();
5585
5586 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005587}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005588#endif