blob: 6233f910217914593364a8da95361990335152c5 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -050065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_CONTROL)
Jiri Olsae2484912012-02-15 15:51:48 +010066
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040068#define INIT_OPS_HASH(opsname) \
69 .func_hash = &opsname.local_hash, \
70 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040071#define ASSIGN_OPS_HASH(opsname, val) \
72 .func_hash = val, \
73 .local_hash.regex_lock = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090074#else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040075#define INIT_OPS_HASH(opsname)
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -040076#define ASSIGN_OPS_HASH(opsname, val)
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090077#endif
78
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040079static struct ftrace_ops ftrace_list_end __read_mostly = {
80 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040081 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -040082 INIT_OPS_HASH(ftrace_list_end)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040083};
84
Steven Rostedt4eebcc82008-05-12 21:20:48 +020085/* ftrace_enabled is a method to turn ftrace on or off */
86int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020087static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020088
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040089/* Current function tracing op */
90struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050091/* What to set function_trace_op to */
92static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050093
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040094/* List for set_ftrace_pid's pids. */
95LIST_HEAD(ftrace_pids);
96struct ftrace_pid {
97 struct list_head list;
98 struct pid *pid;
99};
100
Steven Rostedt4eebcc82008-05-12 21:20:48 +0200101/*
102 * ftrace_disabled is set when an anomaly is discovered.
103 * ftrace_disabled is much stronger than ftrace_enabled.
104 */
105static int ftrace_disabled __read_mostly;
106
Steven Rostedt52baf112009-02-14 01:15:39 -0500107static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200108
Jiri Olsae2484912012-02-15 15:51:48 +0100109static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400110static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200111ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500112ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400113static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100114static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200115
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -0400116static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
117 struct ftrace_ops *op, struct pt_regs *regs);
118
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400119#if ARCH_SUPPORTS_FTRACE_OPS
120static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400121 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122#else
123/* See comment below, where ftrace_ops_list_func is defined */
124static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
125#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
126#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400127
Steven Rostedt0a016402012-11-02 17:03:03 -0400128/*
129 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400130 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400131 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400132 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400133 * concurrent insertions into the ftrace_global_list.
134 *
135 * Silly Alpha and silly pointer-speculation compiler optimizations!
136 */
137#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400138 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400139 do
140
141/*
142 * Optimized for just a single item in the list (as that is the normal case).
143 */
144#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400145 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400146 unlikely((op) != &ftrace_list_end))
147
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900148static inline void ftrace_ops_init(struct ftrace_ops *ops)
149{
150#ifdef CONFIG_DYNAMIC_FTRACE
151 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400152 mutex_init(&ops->local_hash.regex_lock);
153 ops->func_hash = &ops->local_hash;
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900154 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
155 }
156#endif
157}
158
Steven Rostedtea701f12012-07-20 13:08:05 -0400159/**
160 * ftrace_nr_registered_ops - return number of ops registered
161 *
162 * Returns the number of ftrace_ops registered and tracing functions
163 */
164int ftrace_nr_registered_ops(void)
165{
166 struct ftrace_ops *ops;
167 int cnt = 0;
168
169 mutex_lock(&ftrace_lock);
170
171 for (ops = ftrace_ops_list;
172 ops != &ftrace_list_end; ops = ops->next)
173 cnt++;
174
175 mutex_unlock(&ftrace_lock);
176
177 return cnt;
178}
179
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400180static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400181 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500182{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500183 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500184 return;
185
Steven Rostedta1e2e312011-08-09 12:50:46 -0400186 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500187}
188
189static void set_ftrace_pid_function(ftrace_func_t func)
190{
191 /* do not set ftrace_pid_function to itself! */
192 if (func != ftrace_pid_func)
193 ftrace_pid_function = func;
194}
195
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200197 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200198 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200199 * This NULLs the ftrace function and in essence stops
200 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200201 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200202void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200203{
Steven Rostedt3d083392008-05-12 21:20:42 +0200204 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500205 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200206}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207
Jiri Olsae2484912012-02-15 15:51:48 +0100208static void control_ops_disable_all(struct ftrace_ops *ops)
209{
210 int cpu;
211
212 for_each_possible_cpu(cpu)
213 *per_cpu_ptr(ops->disabled, cpu) = 1;
214}
215
216static int control_ops_alloc(struct ftrace_ops *ops)
217{
218 int __percpu *disabled;
219
220 disabled = alloc_percpu(int);
221 if (!disabled)
222 return -ENOMEM;
223
224 ops->disabled = disabled;
225 control_ops_disable_all(ops);
226 return 0;
227}
228
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500229static void ftrace_sync(struct work_struct *work)
230{
231 /*
232 * This function is just a stub to implement a hard force
233 * of synchronize_sched(). This requires synchronizing
234 * tasks even in userspace and idle.
235 *
236 * Yes, function tracing is rude.
237 */
238}
239
240static void ftrace_sync_ipi(void *data)
241{
242 /* Probably not needed, but do it anyway */
243 smp_rmb();
244}
245
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500246#ifdef CONFIG_FUNCTION_GRAPH_TRACER
247static void update_function_graph_func(void);
248#else
249static inline void update_function_graph_func(void) { }
250#endif
251
Steven Rostedt2b499382011-05-03 22:49:52 -0400252static void update_ftrace_function(void)
253{
254 ftrace_func_t func;
255
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400256 /*
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400257 * Prepare the ftrace_ops that the arch callback will use.
258 * If there's only one ftrace_ops registered, the ftrace_ops_list
259 * will point to the ops we want.
260 */
261 set_function_trace_op = ftrace_ops_list;
262
263 /* If there's no ftrace_ops registered, just call the stub function */
264 if (ftrace_ops_list == &ftrace_list_end) {
265 func = ftrace_stub;
266
267 /*
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400268 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400269 * recursion safe and not dynamic and the arch supports passing ops,
270 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400271 */
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400272 } else if (ftrace_ops_list->next == &ftrace_list_end) {
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -0400273 func = ftrace_ops_get_func(ftrace_ops_list);
Steven Rostedt (Red Hat)f7aad4e2014-09-10 10:42:46 -0400274
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400275 } else {
276 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500277 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400278 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400279 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400280
Steven Rostedt (Red Hat)5f8bf2d22014-07-15 11:05:12 -0400281 update_function_graph_func();
282
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500283 /* If there's no change, then do nothing more here */
284 if (ftrace_trace_function == func)
285 return;
286
287 /*
288 * If we are using the list function, it doesn't care
289 * about the function_trace_ops.
290 */
291 if (func == ftrace_ops_list_func) {
292 ftrace_trace_function = func;
293 /*
294 * Don't even bother setting function_trace_ops,
295 * it would be racy to do so anyway.
296 */
297 return;
298 }
299
300#ifndef CONFIG_DYNAMIC_FTRACE
301 /*
302 * For static tracing, we need to be a bit more careful.
303 * The function change takes affect immediately. Thus,
304 * we need to coorditate the setting of the function_trace_ops
305 * with the setting of the ftrace_trace_function.
306 *
307 * Set the function to the list ops, which will call the
308 * function we want, albeit indirectly, but it handles the
309 * ftrace_ops and doesn't depend on function_trace_op.
310 */
311 ftrace_trace_function = ftrace_ops_list_func;
312 /*
313 * Make sure all CPUs see this. Yes this is slow, but static
314 * tracing is slow and nasty to have enabled.
315 */
316 schedule_on_each_cpu(ftrace_sync);
317 /* Now all cpus are using the list ops. */
318 function_trace_op = set_function_trace_op;
319 /* Make sure the function_trace_op is visible on all CPUs */
320 smp_wmb();
321 /* Nasty way to force a rmb on all cpus */
322 smp_call_function(ftrace_sync_ipi, NULL, 1);
323 /* OK, we are all set to update the ftrace_trace_function now! */
324#endif /* !CONFIG_DYNAMIC_FTRACE */
325
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400326 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400327}
328
Jiaxing Wang7eea4fc2014-04-20 23:10:43 +0800329int using_ftrace_ops_list_func(void)
330{
331 return ftrace_trace_function == ftrace_ops_list_func;
332}
333
Steven Rostedt2b499382011-05-03 22:49:52 -0400334static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200335{
Steven Rostedt2b499382011-05-03 22:49:52 -0400336 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200337 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400338 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200339 * CPU might be walking that list. We need to make sure
340 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400341 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200342 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400343 rcu_assign_pointer(*list, ops);
344}
Steven Rostedt3d083392008-05-12 21:20:42 +0200345
Steven Rostedt2b499382011-05-03 22:49:52 -0400346static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
347{
348 struct ftrace_ops **p;
349
350 /*
351 * If we are removing the last function, then simply point
352 * to the ftrace_stub.
353 */
354 if (*list == ops && ops->next == &ftrace_list_end) {
355 *list = &ftrace_list_end;
356 return 0;
357 }
358
359 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
360 if (*p == ops)
361 break;
362
363 if (*p != ops)
364 return -1;
365
366 *p = (*p)->next;
367 return 0;
368}
369
Jiri Olsae2484912012-02-15 15:51:48 +0100370static void add_ftrace_list_ops(struct ftrace_ops **list,
371 struct ftrace_ops *main_ops,
372 struct ftrace_ops *ops)
373{
374 int first = *list == &ftrace_list_end;
375 add_ftrace_ops(list, ops);
376 if (first)
377 add_ftrace_ops(&ftrace_ops_list, main_ops);
378}
379
380static int remove_ftrace_list_ops(struct ftrace_ops **list,
381 struct ftrace_ops *main_ops,
382 struct ftrace_ops *ops)
383{
384 int ret = remove_ftrace_ops(list, ops);
385 if (!ret && *list == &ftrace_list_end)
386 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
387 return ret;
388}
389
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400390static void ftrace_update_trampoline(struct ftrace_ops *ops);
391
Steven Rostedt2b499382011-05-03 22:49:52 -0400392static int __register_ftrace_function(struct ftrace_ops *ops)
393{
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -0500394 if (ops->flags & FTRACE_OPS_FL_DELETED)
395 return -EINVAL;
396
Steven Rostedtb8489142011-05-04 09:27:52 -0400397 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
398 return -EBUSY;
399
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900400#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400401 /*
402 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
403 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
404 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
405 */
406 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
407 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
408 return -EINVAL;
409
410 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
411 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
412#endif
413
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400414 if (!core_kernel_data((unsigned long)ops))
415 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
416
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500417 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100418 if (control_ops_alloc(ops))
419 return -ENOMEM;
420 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedt (Red Hat)fe578ba2014-11-13 19:40:56 -0500421 /* The control_ops needs the trampoline update */
422 ops = &control_ops;
Steven Rostedtb8489142011-05-04 09:27:52 -0400423 } else
424 add_ftrace_ops(&ftrace_ops_list, ops);
425
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -0400426 ftrace_update_trampoline(ops);
427
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400428 if (ftrace_enabled)
429 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200430
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200431 return 0;
432}
433
Ingo Molnare309b412008-05-12 21:20:51 +0200434static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200435{
Steven Rostedt2b499382011-05-03 22:49:52 -0400436 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200437
Steven Rostedtb8489142011-05-04 09:27:52 -0400438 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
439 return -EBUSY;
440
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -0500441 if (ops->flags & FTRACE_OPS_FL_CONTROL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100442 ret = remove_ftrace_list_ops(&ftrace_control_list,
443 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400444 } else
445 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
446
Steven Rostedt2b499382011-05-03 22:49:52 -0400447 if (ret < 0)
448 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400449
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400450 if (ftrace_enabled)
451 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200452
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500453 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200454}
455
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500456static void ftrace_update_pid_func(void)
457{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400458 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500459 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900460 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500461
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400462 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500463}
464
Steven Rostedt493762f2009-03-23 17:12:36 -0400465#ifdef CONFIG_FUNCTION_PROFILER
466struct ftrace_profile {
467 struct hlist_node node;
468 unsigned long ip;
469 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400470#ifdef CONFIG_FUNCTION_GRAPH_TRACER
471 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400472 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400473#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400474};
475
476struct ftrace_profile_page {
477 struct ftrace_profile_page *next;
478 unsigned long index;
479 struct ftrace_profile records[];
480};
481
Steven Rostedtcafb1682009-03-24 20:50:39 -0400482struct ftrace_profile_stat {
483 atomic_t disabled;
484 struct hlist_head *hash;
485 struct ftrace_profile_page *pages;
486 struct ftrace_profile_page *start;
487 struct tracer_stat stat;
488};
489
Steven Rostedt493762f2009-03-23 17:12:36 -0400490#define PROFILE_RECORDS_SIZE \
491 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
492
493#define PROFILES_PER_PAGE \
494 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
495
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400496static int ftrace_profile_enabled __read_mostly;
497
498/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400499static DEFINE_MUTEX(ftrace_profile_lock);
500
Steven Rostedtcafb1682009-03-24 20:50:39 -0400501static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400502
Namhyung Kim20079eb2013-04-10 08:55:50 +0900503#define FTRACE_PROFILE_HASH_BITS 10
504#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400505
Steven Rostedt493762f2009-03-23 17:12:36 -0400506static void *
507function_stat_next(void *v, int idx)
508{
509 struct ftrace_profile *rec = v;
510 struct ftrace_profile_page *pg;
511
512 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
513
514 again:
Li Zefan0296e422009-06-26 11:15:37 +0800515 if (idx != 0)
516 rec++;
517
Steven Rostedt493762f2009-03-23 17:12:36 -0400518 if ((void *)rec >= (void *)&pg->records[pg->index]) {
519 pg = pg->next;
520 if (!pg)
521 return NULL;
522 rec = &pg->records[0];
523 if (!rec->counter)
524 goto again;
525 }
526
527 return rec;
528}
529
530static void *function_stat_start(struct tracer_stat *trace)
531{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400532 struct ftrace_profile_stat *stat =
533 container_of(trace, struct ftrace_profile_stat, stat);
534
535 if (!stat || !stat->start)
536 return NULL;
537
538 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400539}
540
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400541#ifdef CONFIG_FUNCTION_GRAPH_TRACER
542/* function graph compares on total time */
543static int function_stat_cmp(void *p1, void *p2)
544{
545 struct ftrace_profile *a = p1;
546 struct ftrace_profile *b = p2;
547
548 if (a->time < b->time)
549 return -1;
550 if (a->time > b->time)
551 return 1;
552 else
553 return 0;
554}
555#else
556/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400557static int function_stat_cmp(void *p1, void *p2)
558{
559 struct ftrace_profile *a = p1;
560 struct ftrace_profile *b = p2;
561
562 if (a->counter < b->counter)
563 return -1;
564 if (a->counter > b->counter)
565 return 1;
566 else
567 return 0;
568}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400569#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400570
571static int function_stat_headers(struct seq_file *m)
572{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400573#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100574 seq_puts(m, " Function "
575 "Hit Time Avg s^2\n"
576 " -------- "
577 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400578#else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100579 seq_puts(m, " Function Hit\n"
580 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400581#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400582 return 0;
583}
584
585static int function_stat_show(struct seq_file *m, void *v)
586{
587 struct ftrace_profile *rec = v;
588 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800589 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400590#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400591 static struct trace_seq s;
592 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400593 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400594#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800595 mutex_lock(&ftrace_profile_lock);
596
597 /* we raced with function_profile_reset() */
598 if (unlikely(rec->counter == 0)) {
599 ret = -EBUSY;
600 goto out;
601 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400602
603 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400604 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400605
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400606#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100607 seq_puts(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400608 avg = rec->time;
609 do_div(avg, rec->counter);
610
Chase Douglase330b3b2010-04-26 14:02:05 -0400611 /* Sample standard deviation (s^2) */
612 if (rec->counter <= 1)
613 stddev = 0;
614 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200615 /*
616 * Apply Welford's method:
617 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
618 */
619 stddev = rec->counter * rec->time_squared -
620 rec->time * rec->time;
621
Chase Douglase330b3b2010-04-26 14:02:05 -0400622 /*
623 * Divide only 1000 for ns^2 -> us^2 conversion.
624 * trace_print_graph_duration will divide 1000 again.
625 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200626 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400627 }
628
Steven Rostedt34886c82009-03-25 21:00:47 -0400629 trace_seq_init(&s);
630 trace_print_graph_duration(rec->time, &s);
631 trace_seq_puts(&s, " ");
632 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400633 trace_seq_puts(&s, " ");
634 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400635 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400636#endif
637 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800638out:
639 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400640
Li Zefan3aaba202010-08-23 16:50:12 +0800641 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400642}
643
Steven Rostedtcafb1682009-03-24 20:50:39 -0400644static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400645{
646 struct ftrace_profile_page *pg;
647
Steven Rostedtcafb1682009-03-24 20:50:39 -0400648 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400649
650 while (pg) {
651 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
652 pg->index = 0;
653 pg = pg->next;
654 }
655
Steven Rostedtcafb1682009-03-24 20:50:39 -0400656 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400657 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
658}
659
Steven Rostedtcafb1682009-03-24 20:50:39 -0400660int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400661{
662 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400663 int functions;
664 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400665 int i;
666
667 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400668 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400669 return 0;
670
Steven Rostedtcafb1682009-03-24 20:50:39 -0400671 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
672 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400673 return -ENOMEM;
674
Steven Rostedt318e0a72009-03-25 20:06:34 -0400675#ifdef CONFIG_DYNAMIC_FTRACE
676 functions = ftrace_update_tot_cnt;
677#else
678 /*
679 * We do not know the number of functions that exist because
680 * dynamic tracing is what counts them. With past experience
681 * we have around 20K functions. That should be more than enough.
682 * It is highly unlikely we will execute every function in
683 * the kernel.
684 */
685 functions = 20000;
686#endif
687
Steven Rostedtcafb1682009-03-24 20:50:39 -0400688 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400689
Steven Rostedt318e0a72009-03-25 20:06:34 -0400690 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
691
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900692 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400693 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400694 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400695 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400696 pg = pg->next;
697 }
698
699 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400700
701 out_free:
702 pg = stat->start;
703 while (pg) {
704 unsigned long tmp = (unsigned long)pg;
705
706 pg = pg->next;
707 free_page(tmp);
708 }
709
Steven Rostedt318e0a72009-03-25 20:06:34 -0400710 stat->pages = NULL;
711 stat->start = NULL;
712
713 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400714}
715
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400717{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400718 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400719 int size;
720
Steven Rostedtcafb1682009-03-24 20:50:39 -0400721 stat = &per_cpu(ftrace_profile_stats, cpu);
722
723 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400724 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400725 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400726 return 0;
727 }
728
729 /*
730 * We are profiling all functions, but usually only a few thousand
731 * functions are hit. We'll make a hash of 1024 items.
732 */
733 size = FTRACE_PROFILE_HASH_SIZE;
734
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400736
Steven Rostedtcafb1682009-03-24 20:50:39 -0400737 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400738 return -ENOMEM;
739
Steven Rostedt318e0a72009-03-25 20:06:34 -0400740 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 if (ftrace_profile_pages_init(stat) < 0) {
742 kfree(stat->hash);
743 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 return -ENOMEM;
745 }
746
747 return 0;
748}
749
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750static int ftrace_profile_init(void)
751{
752 int cpu;
753 int ret = 0;
754
Miao Xiec4602c12013-12-16 15:20:01 +0800755 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400756 ret = ftrace_profile_init_cpu(cpu);
757 if (ret)
758 break;
759 }
760
761 return ret;
762}
763
Steven Rostedt493762f2009-03-23 17:12:36 -0400764/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400765static struct ftrace_profile *
766ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400767{
768 struct ftrace_profile *rec;
769 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400770 unsigned long key;
771
Namhyung Kim20079eb2013-04-10 08:55:50 +0900772 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400774
775 if (hlist_empty(hhd))
776 return NULL;
777
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400778 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400779 if (rec->ip == ip)
780 return rec;
781 }
782
783 return NULL;
784}
785
Steven Rostedtcafb1682009-03-24 20:50:39 -0400786static void ftrace_add_profile(struct ftrace_profile_stat *stat,
787 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400788{
789 unsigned long key;
790
Namhyung Kim20079eb2013-04-10 08:55:50 +0900791 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400793}
794
Steven Rostedt318e0a72009-03-25 20:06:34 -0400795/*
796 * The memory is already allocated, this simply finds a new record to use.
797 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400798static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400799ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400800{
801 struct ftrace_profile *rec = NULL;
802
Steven Rostedt318e0a72009-03-25 20:06:34 -0400803 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400805 goto out;
806
Steven Rostedt493762f2009-03-23 17:12:36 -0400807 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400808 * Try to find the function again since an NMI
809 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400810 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400812 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400814
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 if (stat->pages->index == PROFILES_PER_PAGE) {
816 if (!stat->pages->next)
817 goto out;
818 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400819 }
820
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400822 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400823 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400824
Steven Rostedt493762f2009-03-23 17:12:36 -0400825 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400826 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400827
828 return rec;
829}
830
Steven Rostedt493762f2009-03-23 17:12:36 -0400831static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400832function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400833 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400834{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400835 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 struct ftrace_profile *rec;
837 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400838
839 if (!ftrace_profile_enabled)
840 return;
841
Steven Rostedt493762f2009-03-23 17:12:36 -0400842 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400843
Christoph Lameterbdffd892014-04-29 14:17:40 -0500844 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400845 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400846 goto out;
847
848 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400849 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400850 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400851 if (!rec)
852 goto out;
853 }
854
855 rec->counter++;
856 out:
857 local_irq_restore(flags);
858}
859
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400860#ifdef CONFIG_FUNCTION_GRAPH_TRACER
861static int profile_graph_entry(struct ftrace_graph_ent *trace)
862{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400863 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400864 return 1;
865}
866
867static void profile_graph_return(struct ftrace_graph_ret *trace)
868{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400869 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400870 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400871 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400872 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400873
874 local_irq_save(flags);
Christoph Lameterbdffd892014-04-29 14:17:40 -0500875 stat = this_cpu_ptr(&ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400876 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400877 goto out;
878
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400879 /* If the calltime was zero'd ignore it */
880 if (!trace->calltime)
881 goto out;
882
Steven Rostedta2a16d62009-03-24 23:17:58 -0400883 calltime = trace->rettime - trace->calltime;
884
885 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
886 int index;
887
888 index = trace->depth;
889
890 /* Append this call time to the parent time to subtract */
891 if (index)
892 current->ret_stack[index - 1].subtime += calltime;
893
894 if (current->ret_stack[index].subtime < calltime)
895 calltime -= current->ret_stack[index].subtime;
896 else
897 calltime = 0;
898 }
899
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400901 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400902 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400903 rec->time_squared += calltime * calltime;
904 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400905
Steven Rostedtcafb1682009-03-24 20:50:39 -0400906 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400907 local_irq_restore(flags);
908}
909
910static int register_ftrace_profiler(void)
911{
912 return register_ftrace_graph(&profile_graph_return,
913 &profile_graph_entry);
914}
915
916static void unregister_ftrace_profiler(void)
917{
918 unregister_ftrace_graph();
919}
920#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100921static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400922 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900923 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -0400924 INIT_OPS_HASH(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400925};
926
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400927static int register_ftrace_profiler(void)
928{
929 return register_ftrace_function(&ftrace_profile_ops);
930}
931
932static void unregister_ftrace_profiler(void)
933{
934 unregister_ftrace_function(&ftrace_profile_ops);
935}
936#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
937
Steven Rostedt493762f2009-03-23 17:12:36 -0400938static ssize_t
939ftrace_profile_write(struct file *filp, const char __user *ubuf,
940 size_t cnt, loff_t *ppos)
941{
942 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400943 int ret;
944
Peter Huewe22fe9b52011-06-07 21:58:27 +0200945 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
946 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400947 return ret;
948
949 val = !!val;
950
951 mutex_lock(&ftrace_profile_lock);
952 if (ftrace_profile_enabled ^ val) {
953 if (val) {
954 ret = ftrace_profile_init();
955 if (ret < 0) {
956 cnt = ret;
957 goto out;
958 }
959
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400960 ret = register_ftrace_profiler();
961 if (ret < 0) {
962 cnt = ret;
963 goto out;
964 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400965 ftrace_profile_enabled = 1;
966 } else {
967 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400968 /*
969 * unregister_ftrace_profiler calls stop_machine
970 * so this acts like an synchronize_sched.
971 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400972 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400973 }
974 }
975 out:
976 mutex_unlock(&ftrace_profile_lock);
977
Jiri Olsacf8517c2009-10-23 19:36:16 -0400978 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400979
980 return cnt;
981}
982
983static ssize_t
984ftrace_profile_read(struct file *filp, char __user *ubuf,
985 size_t cnt, loff_t *ppos)
986{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400987 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400988 int r;
989
990 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
991 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
992}
993
994static const struct file_operations ftrace_profile_fops = {
995 .open = tracing_open_generic,
996 .read = ftrace_profile_read,
997 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200998 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400999};
1000
Steven Rostedtcafb1682009-03-24 20:50:39 -04001001/* used to initialize the real stat files */
1002static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001003 .name = "functions",
1004 .stat_start = function_stat_start,
1005 .stat_next = function_stat_next,
1006 .stat_cmp = function_stat_cmp,
1007 .stat_headers = function_stat_headers,
1008 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001009};
1010
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001011static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001012{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001013 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001014 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001015 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001016 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001017 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001018
Steven Rostedtcafb1682009-03-24 20:50:39 -04001019 for_each_possible_cpu(cpu) {
1020 stat = &per_cpu(ftrace_profile_stats, cpu);
1021
1022 /* allocate enough for function name + cpu number */
1023 name = kmalloc(32, GFP_KERNEL);
1024 if (!name) {
1025 /*
1026 * The files created are permanent, if something happens
1027 * we still do not free memory.
1028 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001029 WARN(1,
1030 "Could not allocate stat file for cpu %d\n",
1031 cpu);
1032 return;
1033 }
1034 stat->stat = function_stats;
1035 snprintf(name, 32, "function%d", cpu);
1036 stat->stat.name = name;
1037 ret = register_stat_tracer(&stat->stat);
1038 if (ret) {
1039 WARN(1,
1040 "Could not register function stat for cpu %d\n",
1041 cpu);
1042 kfree(name);
1043 return;
1044 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001045 }
1046
1047 entry = debugfs_create_file("function_profile_enabled", 0644,
1048 d_tracer, NULL, &ftrace_profile_fops);
1049 if (!entry)
1050 pr_warning("Could not create debugfs "
1051 "'function_profile_enabled' entry\n");
1052}
1053
1054#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001055static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001056{
1057}
1058#endif /* CONFIG_FUNCTION_PROFILER */
1059
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001060static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1061
Steven Rostedt3d083392008-05-12 21:20:42 +02001062#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001063
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001064static struct ftrace_ops *removed_ops;
1065
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001066/*
1067 * Set when doing a global update, like enabling all recs or disabling them.
1068 * It is not set when just updating a single ftrace_ops.
1069 */
1070static bool update_all_ops;
1071
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001072#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001073# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001074#endif
1075
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001076static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1077
Steven Rostedtb6887d72009-02-17 12:32:04 -05001078struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001079 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001080 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001081 unsigned long flags;
1082 unsigned long ip;
1083 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001084 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001085};
1086
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001087struct ftrace_func_entry {
1088 struct hlist_node hlist;
1089 unsigned long ip;
1090};
1091
1092struct ftrace_hash {
1093 unsigned long size_bits;
1094 struct hlist_head *buckets;
1095 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001096 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001097};
1098
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001099/*
1100 * We make these constant because no one should touch them,
1101 * but they are used as the default "empty hash", to avoid allocating
1102 * it all the time. These are in a read only section such that if
1103 * anyone does try to modify it, it will cause an exception.
1104 */
1105static const struct hlist_head empty_buckets[1];
1106static const struct ftrace_hash empty_hash = {
1107 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001108};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001109#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001110
Steven Rostedt2b499382011-05-03 22:49:52 -04001111static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001112 .func = ftrace_stub,
1113 .local_hash.notrace_hash = EMPTY_HASH,
1114 .local_hash.filter_hash = EMPTY_HASH,
1115 INIT_OPS_HASH(global_ops)
1116 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1117 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001118};
1119
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001120struct ftrace_page {
1121 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001122 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001123 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001124 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001125};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001126
Steven Rostedta7900872011-12-16 16:23:44 -05001127#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1128#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001129
1130/* estimate from running different kernels */
1131#define NR_TO_INIT 10000
1132
1133static struct ftrace_page *ftrace_pages_start;
1134static struct ftrace_page *ftrace_pages;
1135
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001136static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001137{
1138 return !hash || !hash->count;
1139}
1140
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001141static struct ftrace_func_entry *
1142ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1143{
1144 unsigned long key;
1145 struct ftrace_func_entry *entry;
1146 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001147
Steven Rostedt06a51d92011-12-19 19:07:36 -05001148 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001149 return NULL;
1150
1151 if (hash->size_bits > 0)
1152 key = hash_long(ip, hash->size_bits);
1153 else
1154 key = 0;
1155
1156 hhd = &hash->buckets[key];
1157
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001158 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001159 if (entry->ip == ip)
1160 return entry;
1161 }
1162 return NULL;
1163}
1164
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001165static void __add_hash_entry(struct ftrace_hash *hash,
1166 struct ftrace_func_entry *entry)
1167{
1168 struct hlist_head *hhd;
1169 unsigned long key;
1170
1171 if (hash->size_bits)
1172 key = hash_long(entry->ip, hash->size_bits);
1173 else
1174 key = 0;
1175
1176 hhd = &hash->buckets[key];
1177 hlist_add_head(&entry->hlist, hhd);
1178 hash->count++;
1179}
1180
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001181static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1182{
1183 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184
1185 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1186 if (!entry)
1187 return -ENOMEM;
1188
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001189 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001190 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001191
1192 return 0;
1193}
1194
1195static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001196free_hash_entry(struct ftrace_hash *hash,
1197 struct ftrace_func_entry *entry)
1198{
1199 hlist_del(&entry->hlist);
1200 kfree(entry);
1201 hash->count--;
1202}
1203
1204static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001205remove_hash_entry(struct ftrace_hash *hash,
1206 struct ftrace_func_entry *entry)
1207{
1208 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001209 hash->count--;
1210}
1211
1212static void ftrace_hash_clear(struct ftrace_hash *hash)
1213{
1214 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001215 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001216 struct ftrace_func_entry *entry;
1217 int size = 1 << hash->size_bits;
1218 int i;
1219
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001220 if (!hash->count)
1221 return;
1222
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001223 for (i = 0; i < size; i++) {
1224 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001225 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001226 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001227 }
1228 FTRACE_WARN_ON(hash->count);
1229}
1230
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001231static void free_ftrace_hash(struct ftrace_hash *hash)
1232{
1233 if (!hash || hash == EMPTY_HASH)
1234 return;
1235 ftrace_hash_clear(hash);
1236 kfree(hash->buckets);
1237 kfree(hash);
1238}
1239
Steven Rostedt07fd5512011-05-05 18:03:47 -04001240static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1241{
1242 struct ftrace_hash *hash;
1243
1244 hash = container_of(rcu, struct ftrace_hash, rcu);
1245 free_ftrace_hash(hash);
1246}
1247
1248static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1249{
1250 if (!hash || hash == EMPTY_HASH)
1251 return;
1252 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1253}
1254
Jiri Olsa5500fa52012-02-15 15:51:54 +01001255void ftrace_free_filter(struct ftrace_ops *ops)
1256{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001257 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001258 free_ftrace_hash(ops->func_hash->filter_hash);
1259 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001260}
1261
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001262static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1263{
1264 struct ftrace_hash *hash;
1265 int size;
1266
1267 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1268 if (!hash)
1269 return NULL;
1270
1271 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001272 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001273
1274 if (!hash->buckets) {
1275 kfree(hash);
1276 return NULL;
1277 }
1278
1279 hash->size_bits = size_bits;
1280
1281 return hash;
1282}
1283
1284static struct ftrace_hash *
1285alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1286{
1287 struct ftrace_func_entry *entry;
1288 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001289 int size;
1290 int ret;
1291 int i;
1292
1293 new_hash = alloc_ftrace_hash(size_bits);
1294 if (!new_hash)
1295 return NULL;
1296
1297 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001298 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001299 return new_hash;
1300
1301 size = 1 << hash->size_bits;
1302 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001303 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001304 ret = add_hash_entry(new_hash, entry->ip);
1305 if (ret < 0)
1306 goto free_hash;
1307 }
1308 }
1309
1310 FTRACE_WARN_ON(new_hash->count != hash->count);
1311
1312 return new_hash;
1313
1314 free_hash:
1315 free_ftrace_hash(new_hash);
1316 return NULL;
1317}
1318
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001319static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001320ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001321static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001322ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001323
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001324static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001325ftrace_hash_move(struct ftrace_ops *ops, int enable,
1326 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001327{
1328 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001329 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001330 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001331 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001332 int size = src->count;
1333 int bits = 0;
1334 int i;
1335
1336 /*
1337 * If the new source is empty, just free dst and assign it
1338 * the empty_hash.
1339 */
1340 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001341 new_hash = EMPTY_HASH;
1342 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001343 }
1344
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001345 /*
1346 * Make the hash size about 1/2 the # found
1347 */
1348 for (size /= 2; size; size >>= 1)
1349 bits++;
1350
1351 /* Don't allocate too much */
1352 if (bits > FTRACE_HASH_MAX_BITS)
1353 bits = FTRACE_HASH_MAX_BITS;
1354
Steven Rostedt07fd5512011-05-05 18:03:47 -04001355 new_hash = alloc_ftrace_hash(bits);
1356 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001357 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001358
1359 size = 1 << src->size_bits;
1360 for (i = 0; i < size; i++) {
1361 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001362 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001363 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001364 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001365 }
1366 }
1367
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001368update:
1369 /*
1370 * Remove the current set, update the hash and add
1371 * them back.
1372 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001373 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001374
Steven Rostedt07fd5512011-05-05 18:03:47 -04001375 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001376
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001377 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001378
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001379 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001380}
1381
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001382static bool hash_contains_ip(unsigned long ip,
1383 struct ftrace_ops_hash *hash)
1384{
1385 /*
1386 * The function record is a match if it exists in the filter
1387 * hash and not in the notrace hash. Note, an emty hash is
1388 * considered a match for the filter hash, but an empty
1389 * notrace hash is considered not in the notrace hash.
1390 */
1391 return (ftrace_hash_empty(hash->filter_hash) ||
1392 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1393 (ftrace_hash_empty(hash->notrace_hash) ||
1394 !ftrace_lookup_ip(hash->notrace_hash, ip));
1395}
1396
Steven Rostedt265c8312009-02-13 12:43:56 -05001397/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001398 * Test the hashes for this ops to see if we want to call
1399 * the ops->func or not.
1400 *
1401 * It's a match if the ip is in the ops->filter_hash or
1402 * the filter_hash does not exist or is empty,
1403 * AND
1404 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001405 *
1406 * This needs to be called with preemption disabled as
1407 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001408 */
1409static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001410ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001411{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001412 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001413 int ret;
1414
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001415#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1416 /*
1417 * There's a small race when adding ops that the ftrace handler
1418 * that wants regs, may be called without them. We can not
1419 * allow that handler to be called if regs is NULL.
1420 */
1421 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1422 return 0;
1423#endif
1424
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001425 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1426 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001427
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001428 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001429 ret = 1;
1430 else
1431 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001432
1433 return ret;
1434}
1435
1436/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001437 * This is a double for. Do not use 'break' to break out of the loop,
1438 * you must use a goto.
1439 */
1440#define do_for_each_ftrace_rec(pg, rec) \
1441 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1442 int _____i; \
1443 for (_____i = 0; _____i < pg->index; _____i++) { \
1444 rec = &pg->records[_____i];
1445
1446#define while_for_each_ftrace_rec() \
1447 } \
1448 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301449
Steven Rostedt5855fea2011-12-16 19:27:42 -05001450
1451static int ftrace_cmp_recs(const void *a, const void *b)
1452{
Steven Rostedta650e022012-04-25 13:48:13 -04001453 const struct dyn_ftrace *key = a;
1454 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001455
Steven Rostedta650e022012-04-25 13:48:13 -04001456 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001457 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001458 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1459 return 1;
1460 return 0;
1461}
1462
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001463static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001464{
1465 struct ftrace_page *pg;
1466 struct dyn_ftrace *rec;
1467 struct dyn_ftrace key;
1468
1469 key.ip = start;
1470 key.flags = end; /* overload flags, as it is unsigned long */
1471
1472 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1473 if (end < pg->records[0].ip ||
1474 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1475 continue;
1476 rec = bsearch(&key, pg->records, pg->index,
1477 sizeof(struct dyn_ftrace),
1478 ftrace_cmp_recs);
1479 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001480 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001481 }
1482
Steven Rostedt5855fea2011-12-16 19:27:42 -05001483 return 0;
1484}
1485
Steven Rostedtc88fd862011-08-16 09:53:39 -04001486/**
1487 * ftrace_location - return true if the ip giving is a traced location
1488 * @ip: the instruction pointer to check
1489 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001490 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001491 * That is, the instruction that is either a NOP or call to
1492 * the function tracer. It checks the ftrace internal tables to
1493 * determine if the address belongs or not.
1494 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001495unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001496{
Steven Rostedta650e022012-04-25 13:48:13 -04001497 return ftrace_location_range(ip, ip);
1498}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001499
Steven Rostedta650e022012-04-25 13:48:13 -04001500/**
1501 * ftrace_text_reserved - return true if range contains an ftrace location
1502 * @start: start of range to search
1503 * @end: end of range to search (inclusive). @end points to the last byte to check.
1504 *
1505 * Returns 1 if @start and @end contains a ftrace location.
1506 * That is, the instruction that is either a NOP or call to
1507 * the function tracer. It checks the ftrace internal tables to
1508 * determine if the address belongs or not.
1509 */
Sasha Levind88471c2013-01-09 18:09:20 -05001510int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001511{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001512 unsigned long ret;
1513
1514 ret = ftrace_location_range((unsigned long)start,
1515 (unsigned long)end);
1516
1517 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001518}
1519
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001520/* Test if ops registered to this rec needs regs */
1521static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1522{
1523 struct ftrace_ops *ops;
1524 bool keep_regs = false;
1525
1526 for (ops = ftrace_ops_list;
1527 ops != &ftrace_list_end; ops = ops->next) {
1528 /* pass rec in as regs to have non-NULL val */
1529 if (ftrace_ops_test(ops, rec->ip, rec)) {
1530 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1531 keep_regs = true;
1532 break;
1533 }
1534 }
1535 }
1536
1537 return keep_regs;
1538}
1539
Steven Rostedted926f92011-05-03 13:25:24 -04001540static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1541 int filter_hash,
1542 bool inc)
1543{
1544 struct ftrace_hash *hash;
1545 struct ftrace_hash *other_hash;
1546 struct ftrace_page *pg;
1547 struct dyn_ftrace *rec;
1548 int count = 0;
1549 int all = 0;
1550
1551 /* Only update if the ops has been registered */
1552 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1553 return;
1554
1555 /*
1556 * In the filter_hash case:
1557 * If the count is zero, we update all records.
1558 * Otherwise we just update the items in the hash.
1559 *
1560 * In the notrace_hash case:
1561 * We enable the update in the hash.
1562 * As disabling notrace means enabling the tracing,
1563 * and enabling notrace means disabling, the inc variable
1564 * gets inversed.
1565 */
1566 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001567 hash = ops->func_hash->filter_hash;
1568 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001569 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001570 all = 1;
1571 } else {
1572 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001573 hash = ops->func_hash->notrace_hash;
1574 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001575 /*
1576 * If the notrace hash has no items,
1577 * then there's nothing to do.
1578 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001579 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001580 return;
1581 }
1582
1583 do_for_each_ftrace_rec(pg, rec) {
1584 int in_other_hash = 0;
1585 int in_hash = 0;
1586 int match = 0;
1587
1588 if (all) {
1589 /*
1590 * Only the filter_hash affects all records.
1591 * Update if the record is not in the notrace hash.
1592 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001593 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001594 match = 1;
1595 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001596 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1597 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001598
1599 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001600 * If filter_hash is set, we want to match all functions
1601 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001602 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001603 * If filter_hash is not set, then we are decrementing.
1604 * That means we match anything that is in the hash
1605 * and also in the other_hash. That is, we need to turn
1606 * off functions in the other hash because they are disabled
1607 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001608 */
1609 if (filter_hash && in_hash && !in_other_hash)
1610 match = 1;
1611 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001612 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001613 match = 1;
1614 }
1615 if (!match)
1616 continue;
1617
1618 if (inc) {
1619 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001620 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001621 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001622
1623 /*
1624 * If there's only a single callback registered to a
1625 * function, and the ops has a trampoline registered
1626 * for it, then we can call it directly.
1627 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001628 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001629 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001630 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001631 /*
1632 * If we are adding another function callback
1633 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001634 * custom trampoline in use, then we need to go
1635 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001636 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001637 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001638
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001639 /*
1640 * If any ops wants regs saved for this function
1641 * then all ops will get saved regs.
1642 */
1643 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1644 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001645 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001646 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001647 return;
1648 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001649
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001650 /*
1651 * If the rec had REGS enabled and the ops that is
1652 * being removed had REGS set, then see if there is
1653 * still any ops for this record that wants regs.
1654 * If not, we can stop recording them.
1655 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001656 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001657 rec->flags & FTRACE_FL_REGS &&
1658 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1659 if (!test_rec_ops_needs_regs(rec))
1660 rec->flags &= ~FTRACE_FL_REGS;
1661 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001662
1663 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001664 * If the rec had TRAMP enabled, then it needs to
1665 * be cleared. As TRAMP can only be enabled iff
1666 * there is only a single ops attached to it.
1667 * In otherwords, always disable it on decrementing.
1668 * In the future, we may set it if rec count is
1669 * decremented to one, and the ops that is left
1670 * has a trampoline.
1671 */
1672 rec->flags &= ~FTRACE_FL_TRAMP;
1673
1674 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001675 * flags will be cleared in ftrace_check_record()
1676 * if rec count is zero.
1677 */
Steven Rostedted926f92011-05-03 13:25:24 -04001678 }
1679 count++;
1680 /* Shortcut, if we handled all records, we are done. */
1681 if (!all && count == hash->count)
1682 return;
1683 } while_for_each_ftrace_rec();
1684}
1685
1686static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1687 int filter_hash)
1688{
1689 __ftrace_hash_rec_update(ops, filter_hash, 0);
1690}
1691
1692static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1693 int filter_hash)
1694{
1695 __ftrace_hash_rec_update(ops, filter_hash, 1);
1696}
1697
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001698static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1699 int filter_hash, int inc)
1700{
1701 struct ftrace_ops *op;
1702
1703 __ftrace_hash_rec_update(ops, filter_hash, inc);
1704
1705 if (ops->func_hash != &global_ops.local_hash)
1706 return;
1707
1708 /*
1709 * If the ops shares the global_ops hash, then we need to update
1710 * all ops that are enabled and use this hash.
1711 */
1712 do_for_each_ftrace_op(op, ftrace_ops_list) {
1713 /* Already done */
1714 if (op == ops)
1715 continue;
1716 if (op->func_hash == &global_ops.local_hash)
1717 __ftrace_hash_rec_update(op, filter_hash, inc);
1718 } while_for_each_ftrace_op(op);
1719}
1720
1721static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1722 int filter_hash)
1723{
1724 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1725}
1726
1727static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1728 int filter_hash)
1729{
1730 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1731}
1732
Steven Rostedt05736a42008-09-22 14:55:47 -07001733static void print_ip_ins(const char *fmt, unsigned char *p)
1734{
1735 int i;
1736
1737 printk(KERN_CONT "%s", fmt);
1738
1739 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1740 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1741}
1742
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001743static struct ftrace_ops *
1744ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1745
Steven Rostedtc88fd862011-08-16 09:53:39 -04001746/**
1747 * ftrace_bug - report and shutdown function tracer
1748 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001749 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001750 *
1751 * The arch code that enables or disables the function tracing
1752 * can call ftrace_bug() when it has detected a problem in
1753 * modifying the code. @failed should be one of either:
1754 * EFAULT - if the problem happens on reading the @ip address
1755 * EINVAL - if what is read at @ip is not what was expected
1756 * EPERM - if the problem happens on writting to the @ip address
1757 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001758void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001759{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001760 unsigned long ip = rec ? rec->ip : 0;
1761
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001762 switch (failed) {
1763 case -EFAULT:
1764 FTRACE_WARN_ON_ONCE(1);
1765 pr_info("ftrace faulted on modifying ");
1766 print_ip_sym(ip);
1767 break;
1768 case -EINVAL:
1769 FTRACE_WARN_ON_ONCE(1);
1770 pr_info("ftrace failed to modify ");
1771 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001772 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001773 pr_cont("\n");
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001774 break;
1775 case -EPERM:
1776 FTRACE_WARN_ON_ONCE(1);
1777 pr_info("ftrace faulted on writing ");
1778 print_ip_sym(ip);
1779 break;
1780 default:
1781 FTRACE_WARN_ON_ONCE(1);
1782 pr_info("ftrace faulted on unknown error ");
1783 print_ip_sym(ip);
1784 }
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001785 if (rec) {
1786 struct ftrace_ops *ops = NULL;
1787
1788 pr_info("ftrace record flags: %lx\n", rec->flags);
1789 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
1790 rec->flags & FTRACE_FL_REGS ? " R" : " ");
1791 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1792 ops = ftrace_find_tramp_ops_any(rec);
1793 if (ops)
1794 pr_cont("\ttramp: %pS",
1795 (void *)ops->trampoline);
1796 else
1797 pr_cont("\ttramp: ERROR!");
1798
1799 }
1800 ip = ftrace_get_addr_curr(rec);
1801 pr_cont(" expected tramp: %lx\n", ip);
1802 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001803}
1804
Steven Rostedtc88fd862011-08-16 09:53:39 -04001805static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001806{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001807 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001808
Steven Rostedt982c3502008-11-15 16:31:41 -05001809 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001810 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001811 *
Steven Rostedted926f92011-05-03 13:25:24 -04001812 * If the record has a ref count, then we need to enable it
1813 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001814 *
Steven Rostedted926f92011-05-03 13:25:24 -04001815 * Otherwise we make sure its disabled.
1816 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001817 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001818 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001819 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001820 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001821 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001822
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001823 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001824 * If enabling and the REGS flag does not match the REGS_EN, or
1825 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1826 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001827 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001828 if (flag) {
1829 if (!(rec->flags & FTRACE_FL_REGS) !=
1830 !(rec->flags & FTRACE_FL_REGS_EN))
1831 flag |= FTRACE_FL_REGS;
1832
1833 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1834 !(rec->flags & FTRACE_FL_TRAMP_EN))
1835 flag |= FTRACE_FL_TRAMP;
1836 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001837
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001838 /* If the state of this record hasn't changed, then do nothing */
1839 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001840 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001841
1842 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001843 /* Save off if rec is being enabled (for return value) */
1844 flag ^= rec->flags & FTRACE_FL_ENABLED;
1845
1846 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001847 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001848 if (flag & FTRACE_FL_REGS) {
1849 if (rec->flags & FTRACE_FL_REGS)
1850 rec->flags |= FTRACE_FL_REGS_EN;
1851 else
1852 rec->flags &= ~FTRACE_FL_REGS_EN;
1853 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001854 if (flag & FTRACE_FL_TRAMP) {
1855 if (rec->flags & FTRACE_FL_TRAMP)
1856 rec->flags |= FTRACE_FL_TRAMP_EN;
1857 else
1858 rec->flags &= ~FTRACE_FL_TRAMP_EN;
1859 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001860 }
1861
1862 /*
1863 * If this record is being updated from a nop, then
1864 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001865 * Otherwise,
1866 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001867 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001868 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001869 */
1870 if (flag & FTRACE_FL_ENABLED)
1871 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04001872
1873 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001874 }
1875
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001876 if (update) {
1877 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001878 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001879 rec->flags = 0;
1880 else
1881 /* Just disable the record (keep REGS state) */
1882 rec->flags &= ~FTRACE_FL_ENABLED;
1883 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001884
1885 return FTRACE_UPDATE_MAKE_NOP;
1886}
1887
1888/**
1889 * ftrace_update_record, set a record that now is tracing or not
1890 * @rec: the record to update
1891 * @enable: set to 1 if the record is tracing, zero to force disable
1892 *
1893 * The records that represent all functions that can be traced need
1894 * to be updated when tracing has been enabled.
1895 */
1896int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1897{
1898 return ftrace_check_record(rec, enable, 1);
1899}
1900
1901/**
1902 * ftrace_test_record, check if the record has been enabled or not
1903 * @rec: the record to test
1904 * @enable: set to 1 to check if enabled, 0 if it is disabled
1905 *
1906 * The arch code may need to test if a record is already set to
1907 * tracing to determine how to modify the function code that it
1908 * represents.
1909 */
1910int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1911{
1912 return ftrace_check_record(rec, enable, 0);
1913}
1914
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001915static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001916ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
1917{
1918 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001919 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001920
1921 do_for_each_ftrace_op(op, ftrace_ops_list) {
1922
1923 if (!op->trampoline)
1924 continue;
1925
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001926 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04001927 return op;
1928 } while_for_each_ftrace_op(op);
1929
1930 return NULL;
1931}
1932
1933static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001934ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
1935{
1936 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001937 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001938
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001939 /*
1940 * Need to check removed ops first.
1941 * If they are being removed, and this rec has a tramp,
1942 * and this rec is in the ops list, then it would be the
1943 * one with the tramp.
1944 */
1945 if (removed_ops) {
1946 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001947 return removed_ops;
1948 }
1949
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001950 /*
1951 * Need to find the current trampoline for a rec.
1952 * Now, a trampoline is only attached to a rec if there
1953 * was a single 'ops' attached to it. But this can be called
1954 * when we are adding another op to the rec or removing the
1955 * current one. Thus, if the op is being added, we can
1956 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04001957 * yet.
1958 *
1959 * If an ops is being modified (hooking to different functions)
1960 * then we don't care about the new functions that are being
1961 * added, just the old ones (that are probably being removed).
1962 *
1963 * If we are adding an ops to a function that already is using
1964 * a trampoline, it needs to be removed (trampolines are only
1965 * for single ops connected), then an ops that is not being
1966 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001967 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001968 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001969
1970 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001971 continue;
1972
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001973 /*
1974 * If the ops is being added, it hasn't gotten to
1975 * the point to be removed from this tree yet.
1976 */
1977 if (op->flags & FTRACE_OPS_FL_ADDING)
1978 continue;
1979
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001980
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04001981 /*
1982 * If the ops is being modified and is in the old
1983 * hash, then it is probably being removed from this
1984 * function.
1985 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001986 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
1987 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001988 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04001989 /*
1990 * If the ops is not being added or modified, and it's
1991 * in its normal filter hash, then this must be the one
1992 * we want!
1993 */
1994 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
1995 hash_contains_ip(ip, op->func_hash))
1996 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001997
1998 } while_for_each_ftrace_op(op);
1999
2000 return NULL;
2001}
2002
2003static struct ftrace_ops *
2004ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2005{
2006 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002007 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002008
2009 do_for_each_ftrace_op(op, ftrace_ops_list) {
2010 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002011 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002012 return op;
2013 } while_for_each_ftrace_op(op);
2014
2015 return NULL;
2016}
2017
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002018/**
2019 * ftrace_get_addr_new - Get the call address to set to
2020 * @rec: The ftrace record descriptor
2021 *
2022 * If the record has the FTRACE_FL_REGS set, that means that it
2023 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2024 * is not not set, then it wants to convert to the normal callback.
2025 *
2026 * Returns the address of the trampoline to set to
2027 */
2028unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2029{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002030 struct ftrace_ops *ops;
2031
2032 /* Trampolines take precedence over regs */
2033 if (rec->flags & FTRACE_FL_TRAMP) {
2034 ops = ftrace_find_tramp_ops_new(rec);
2035 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002036 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2037 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002038 /* Ftrace is shutting down, return anything */
2039 return (unsigned long)FTRACE_ADDR;
2040 }
2041 return ops->trampoline;
2042 }
2043
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002044 if (rec->flags & FTRACE_FL_REGS)
2045 return (unsigned long)FTRACE_REGS_ADDR;
2046 else
2047 return (unsigned long)FTRACE_ADDR;
2048}
2049
2050/**
2051 * ftrace_get_addr_curr - Get the call address that is already there
2052 * @rec: The ftrace record descriptor
2053 *
2054 * The FTRACE_FL_REGS_EN is set when the record already points to
2055 * a function that saves all the regs. Basically the '_EN' version
2056 * represents the current state of the function.
2057 *
2058 * Returns the address of the trampoline that is currently being called
2059 */
2060unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2061{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002062 struct ftrace_ops *ops;
2063
2064 /* Trampolines take precedence over regs */
2065 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2066 ops = ftrace_find_tramp_ops_curr(rec);
2067 if (FTRACE_WARN_ON(!ops)) {
2068 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2069 (void *)rec->ip, (void *)rec->ip);
2070 /* Ftrace is shutting down, return anything */
2071 return (unsigned long)FTRACE_ADDR;
2072 }
2073 return ops->trampoline;
2074 }
2075
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002076 if (rec->flags & FTRACE_FL_REGS_EN)
2077 return (unsigned long)FTRACE_REGS_ADDR;
2078 else
2079 return (unsigned long)FTRACE_ADDR;
2080}
2081
Steven Rostedtc88fd862011-08-16 09:53:39 -04002082static int
2083__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2084{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002085 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002086 unsigned long ftrace_addr;
2087 int ret;
2088
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002089 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002090
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002091 /* This needs to be done before we call ftrace_update_record */
2092 ftrace_old_addr = ftrace_get_addr_curr(rec);
2093
2094 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002095
Steven Rostedtc88fd862011-08-16 09:53:39 -04002096 switch (ret) {
2097 case FTRACE_UPDATE_IGNORE:
2098 return 0;
2099
2100 case FTRACE_UPDATE_MAKE_CALL:
2101 return ftrace_make_call(rec, ftrace_addr);
2102
2103 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002104 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002105
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002106 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002107 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002108 }
2109
2110 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002111}
2112
Steven Rostedte4f5d542012-04-27 09:13:18 -04002113void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002114{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002115 struct dyn_ftrace *rec;
2116 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002117 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002118
Steven Rostedt45a4a232011-04-21 23:16:46 -04002119 if (unlikely(ftrace_disabled))
2120 return;
2121
Steven Rostedt265c8312009-02-13 12:43:56 -05002122 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002123 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002124 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002125 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002126 /* Stop processing */
2127 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002128 }
2129 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002130}
2131
Steven Rostedtc88fd862011-08-16 09:53:39 -04002132struct ftrace_rec_iter {
2133 struct ftrace_page *pg;
2134 int index;
2135};
2136
2137/**
2138 * ftrace_rec_iter_start, start up iterating over traced functions
2139 *
2140 * Returns an iterator handle that is used to iterate over all
2141 * the records that represent address locations where functions
2142 * are traced.
2143 *
2144 * May return NULL if no records are available.
2145 */
2146struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2147{
2148 /*
2149 * We only use a single iterator.
2150 * Protected by the ftrace_lock mutex.
2151 */
2152 static struct ftrace_rec_iter ftrace_rec_iter;
2153 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2154
2155 iter->pg = ftrace_pages_start;
2156 iter->index = 0;
2157
2158 /* Could have empty pages */
2159 while (iter->pg && !iter->pg->index)
2160 iter->pg = iter->pg->next;
2161
2162 if (!iter->pg)
2163 return NULL;
2164
2165 return iter;
2166}
2167
2168/**
2169 * ftrace_rec_iter_next, get the next record to process.
2170 * @iter: The handle to the iterator.
2171 *
2172 * Returns the next iterator after the given iterator @iter.
2173 */
2174struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2175{
2176 iter->index++;
2177
2178 if (iter->index >= iter->pg->index) {
2179 iter->pg = iter->pg->next;
2180 iter->index = 0;
2181
2182 /* Could have empty pages */
2183 while (iter->pg && !iter->pg->index)
2184 iter->pg = iter->pg->next;
2185 }
2186
2187 if (!iter->pg)
2188 return NULL;
2189
2190 return iter;
2191}
2192
2193/**
2194 * ftrace_rec_iter_record, get the record at the iterator location
2195 * @iter: The current iterator location
2196 *
2197 * Returns the record that the current @iter is at.
2198 */
2199struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2200{
2201 return &iter->pg->records[iter->index];
2202}
2203
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302204static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002205ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002206{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002207 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002208
Steven Rostedt45a4a232011-04-21 23:16:46 -04002209 if (unlikely(ftrace_disabled))
2210 return 0;
2211
Shaohua Li25aac9d2009-01-09 11:29:40 +08002212 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002213 if (ret) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002214 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302215 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002216 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302217 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002218}
2219
Steven Rostedt000ab692009-02-17 13:35:06 -05002220/*
2221 * archs can override this function if they must do something
2222 * before the modifying code is performed.
2223 */
2224int __weak ftrace_arch_code_modify_prepare(void)
2225{
2226 return 0;
2227}
2228
2229/*
2230 * archs can override this function if they must do something
2231 * after the modifying code is performed.
2232 */
2233int __weak ftrace_arch_code_modify_post_process(void)
2234{
2235 return 0;
2236}
2237
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002238void ftrace_modify_all_code(int command)
2239{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002240 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002241 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002242
2243 /*
2244 * If the ftrace_caller calls a ftrace_ops func directly,
2245 * we need to make sure that it only traces functions it
2246 * expects to trace. When doing the switch of functions,
2247 * we need to update to the ftrace_ops_list_func first
2248 * before the transition between old and new calls are set,
2249 * as the ftrace_ops_list_func will check the ops hashes
2250 * to make sure the ops are having the right functions
2251 * traced.
2252 */
Petr Mladekcd210672014-02-24 17:12:21 +01002253 if (update) {
2254 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2255 if (FTRACE_WARN_ON(err))
2256 return;
2257 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002258
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002259 if (command & FTRACE_UPDATE_CALLS)
2260 ftrace_replace_code(1);
2261 else if (command & FTRACE_DISABLE_CALLS)
2262 ftrace_replace_code(0);
2263
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002264 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2265 function_trace_op = set_function_trace_op;
2266 smp_wmb();
2267 /* If irqs are disabled, we are in stop machine */
2268 if (!irqs_disabled())
2269 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002270 err = ftrace_update_ftrace_func(ftrace_trace_function);
2271 if (FTRACE_WARN_ON(err))
2272 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002273 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002274
2275 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002276 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002277 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002278 err = ftrace_disable_ftrace_graph_caller();
2279 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002280}
2281
Ingo Molnare309b412008-05-12 21:20:51 +02002282static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002283{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002284 int *command = data;
2285
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002286 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002287
Steven Rostedtc88fd862011-08-16 09:53:39 -04002288 return 0;
2289}
2290
2291/**
2292 * ftrace_run_stop_machine, go back to the stop machine method
2293 * @command: The command to tell ftrace what to do
2294 *
2295 * If an arch needs to fall back to the stop machine method, the
2296 * it can call this function.
2297 */
2298void ftrace_run_stop_machine(int command)
2299{
2300 stop_machine(__ftrace_modify_code, &command, NULL);
2301}
2302
2303/**
2304 * arch_ftrace_update_code, modify the code to trace or not trace
2305 * @command: The command that needs to be done
2306 *
2307 * Archs can override this function if it does not need to
2308 * run stop_machine() to modify code.
2309 */
2310void __weak arch_ftrace_update_code(int command)
2311{
2312 ftrace_run_stop_machine(command);
2313}
2314
2315static void ftrace_run_update_code(int command)
2316{
2317 int ret;
2318
2319 ret = ftrace_arch_code_modify_prepare();
2320 FTRACE_WARN_ON(ret);
2321 if (ret)
2322 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002323
2324 /*
2325 * By default we use stop_machine() to modify the code.
2326 * But archs can do what ever they want as long as it
2327 * is safe. The stop_machine() is the safest, but also
2328 * produces the most overhead.
2329 */
2330 arch_ftrace_update_code(command);
2331
Steven Rostedt000ab692009-02-17 13:35:06 -05002332 ret = ftrace_arch_code_modify_post_process();
2333 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002334}
2335
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002336static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2337 struct ftrace_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002338{
2339 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002340 ops->old_hash.filter_hash = old_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002341 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002342 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002343 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2344}
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
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002349void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2350{
2351}
2352
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002353static void control_ops_free(struct ftrace_ops *ops)
2354{
2355 free_percpu(ops->disabled);
2356}
2357
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002358static void ftrace_startup_enable(int command)
2359{
2360 if (saved_ftrace_func != ftrace_trace_function) {
2361 saved_ftrace_func = ftrace_trace_function;
2362 command |= FTRACE_UPDATE_TRACE_FUNC;
2363 }
2364
2365 if (!command || !ftrace_enabled)
2366 return;
2367
2368 ftrace_run_update_code(command);
2369}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002370
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002371static void ftrace_startup_all(int command)
2372{
2373 update_all_ops = true;
2374 ftrace_startup_enable(command);
2375 update_all_ops = false;
2376}
2377
Steven Rostedta1cd6172011-05-23 15:24:25 -04002378static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002379{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002380 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002381
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002382 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002383 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002384
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002385 ret = __register_ftrace_function(ops);
2386 if (ret)
2387 return ret;
2388
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002389 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002390 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002391
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002392 /*
2393 * Note that ftrace probes uses this to start up
2394 * and modify functions it will probe. But we still
2395 * set the ADDING flag for modification, as probes
2396 * do not have trampolines. If they add them in the
2397 * future, then the probes will need to distinguish
2398 * between adding and updating probes.
2399 */
2400 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002401
2402 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002403
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002404 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002405
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002406 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2407
Steven Rostedta1cd6172011-05-23 15:24:25 -04002408 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002409}
2410
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002411static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002412{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002413 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002414
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002415 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002416 return -ENODEV;
2417
2418 ret = __unregister_ftrace_function(ops);
2419 if (ret)
2420 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002421
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002422 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002423 /*
2424 * Just warn in case of unbalance, no need to kill ftrace, it's not
2425 * critical but the ftrace_call callers may be never nopped again after
2426 * further ftrace uses.
2427 */
2428 WARN_ON_ONCE(ftrace_start_up < 0);
2429
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002430 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002431
Namhyung Kima737e6d2014-06-12 23:56:12 +09002432 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002433
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002434 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002435
2436 if (saved_ftrace_func != ftrace_trace_function) {
2437 saved_ftrace_func = ftrace_trace_function;
2438 command |= FTRACE_UPDATE_TRACE_FUNC;
2439 }
2440
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002441 if (!command || !ftrace_enabled) {
2442 /*
2443 * If these are control ops, they still need their
2444 * per_cpu field freed. Since, function tracing is
2445 * not currently active, we can just free them
2446 * without synchronizing all CPUs.
2447 */
2448 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2449 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002450 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002451 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002452
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002453 /*
2454 * If the ops uses a trampoline, then it needs to be
2455 * tested first on update.
2456 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002457 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002458 removed_ops = ops;
2459
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002460 /* The trampoline logic checks the old hashes */
2461 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2462 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2463
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002464 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002465
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002466 /*
2467 * If there's no more ops registered with ftrace, run a
2468 * sanity check to make sure all rec flags are cleared.
2469 */
2470 if (ftrace_ops_list == &ftrace_list_end) {
2471 struct ftrace_page *pg;
2472 struct dyn_ftrace *rec;
2473
2474 do_for_each_ftrace_rec(pg, rec) {
2475 if (FTRACE_WARN_ON_ONCE(rec->flags))
2476 pr_warn(" %pS flags:%lx\n",
2477 (void *)rec->ip, rec->flags);
2478 } while_for_each_ftrace_rec();
2479 }
2480
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002481 ops->old_hash.filter_hash = NULL;
2482 ops->old_hash.notrace_hash = NULL;
2483
2484 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002485 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002486
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002487 /*
2488 * Dynamic ops may be freed, we must make sure that all
2489 * callers are done before leaving this function.
2490 * The same goes for freeing the per_cpu data of the control
2491 * ops.
2492 *
2493 * Again, normal synchronize_sched() is not good enough.
2494 * We need to do a hard force of sched synchronization.
2495 * This is because we use preempt_disable() to do RCU, but
2496 * the function tracers can be called where RCU is not watching
2497 * (like before user_exit()). We can not rely on the RCU
2498 * infrastructure to do the synchronization, thus we must do it
2499 * ourselves.
2500 */
2501 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2502 schedule_on_each_cpu(ftrace_sync);
2503
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002504 arch_ftrace_trampoline_free(ops);
2505
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002506 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2507 control_ops_free(ops);
2508 }
2509
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002510 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002511}
2512
Ingo Molnare309b412008-05-12 21:20:51 +02002513static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002514{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002515 if (unlikely(ftrace_disabled))
2516 return;
2517
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002518 /* Force update next time */
2519 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002520 /* ftrace_start_up is true if we want ftrace running */
2521 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002522 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002523}
2524
Ingo Molnare309b412008-05-12 21:20:51 +02002525static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002526{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002527 if (unlikely(ftrace_disabled))
2528 return;
2529
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002530 /* ftrace_start_up is true if ftrace is running */
2531 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002532 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002533}
2534
Steven Rostedt3d083392008-05-12 21:20:42 +02002535static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002536unsigned long ftrace_update_tot_cnt;
2537
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002538static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002539{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002540 /*
2541 * Filter_hash being empty will default to trace module.
2542 * But notrace hash requires a test of individual module functions.
2543 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002544 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2545 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002546}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002547
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002548/*
2549 * Check if the current ops references the record.
2550 *
2551 * If the ops traces all functions, then it was already accounted for.
2552 * If the ops does not trace the current record function, skip it.
2553 * If the ops ignores the function via notrace filter, skip it.
2554 */
2555static inline bool
2556ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2557{
2558 /* If ops isn't enabled, ignore it */
2559 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2560 return 0;
2561
2562 /* If ops traces all mods, we already accounted for it */
2563 if (ops_traces_mod(ops))
2564 return 0;
2565
2566 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002567 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2568 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002569 return 0;
2570
2571 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002572 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002573 return 0;
2574
2575 return 1;
2576}
2577
2578static int referenced_filters(struct dyn_ftrace *rec)
2579{
2580 struct ftrace_ops *ops;
2581 int cnt = 0;
2582
2583 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2584 if (ops_references_rec(ops, rec))
2585 cnt++;
2586 }
2587
2588 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002589}
2590
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002591static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002592{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002593 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002594 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302595 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002596 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002597 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002598 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002599 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002600
2601 /*
2602 * When adding a module, we need to check if tracers are
2603 * currently enabled and if they are set to trace all functions.
2604 * If they are, we need to enable the module functions as well
2605 * as update the reference counts for those function records.
2606 */
2607 if (mod) {
2608 struct ftrace_ops *ops;
2609
2610 for (ops = ftrace_ops_list;
2611 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002612 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2613 if (ops_traces_mod(ops))
2614 ref++;
2615 else
2616 test = true;
2617 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002618 }
2619 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002620
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002621 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002622
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002623 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302624
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002625 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002626 int cnt = ref;
2627
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002628 /* If something went wrong, bail without enabling anything */
2629 if (unlikely(ftrace_disabled))
2630 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002631
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002632 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002633 if (test)
2634 cnt += referenced_filters(p);
2635 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302636
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002637 /*
2638 * Do the initial record conversion from mcount jump
2639 * to the NOP instructions.
2640 */
2641 if (!ftrace_code_disable(mod, p))
2642 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002643
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002644 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002645
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002646 /*
2647 * If the tracing is enabled, go ahead and enable the record.
2648 *
2649 * The reason not to enable the record immediatelly is the
2650 * inherent check of ftrace_make_nop/ftrace_make_call for
2651 * correct previous instructions. Making first the NOP
2652 * conversion puts the module to the correct state, thus
2653 * passing the ftrace_make_call check.
2654 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002655 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002656 int failed = __ftrace_replace_code(p, 1);
2657 if (failed)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002658 ftrace_bug(failed, p);
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002659 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002660 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002661 }
2662
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002663 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002664 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002665 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002666
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002667 return 0;
2668}
2669
Steven Rostedta7900872011-12-16 16:23:44 -05002670static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002671{
Steven Rostedta7900872011-12-16 16:23:44 -05002672 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002673 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002674
Steven Rostedta7900872011-12-16 16:23:44 -05002675 if (WARN_ON(!count))
2676 return -EINVAL;
2677
2678 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002679
2680 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002681 * We want to fill as much as possible. No more than a page
2682 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002683 */
Steven Rostedta7900872011-12-16 16:23:44 -05002684 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2685 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002686
Steven Rostedta7900872011-12-16 16:23:44 -05002687 again:
2688 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2689
2690 if (!pg->records) {
2691 /* if we can't allocate this size, try something smaller */
2692 if (!order)
2693 return -ENOMEM;
2694 order >>= 1;
2695 goto again;
2696 }
2697
2698 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2699 pg->size = cnt;
2700
2701 if (cnt > count)
2702 cnt = count;
2703
2704 return cnt;
2705}
2706
2707static struct ftrace_page *
2708ftrace_allocate_pages(unsigned long num_to_init)
2709{
2710 struct ftrace_page *start_pg;
2711 struct ftrace_page *pg;
2712 int order;
2713 int cnt;
2714
2715 if (!num_to_init)
2716 return 0;
2717
2718 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2719 if (!pg)
2720 return NULL;
2721
2722 /*
2723 * Try to allocate as much as possible in one continues
2724 * location that fills in all of the space. We want to
2725 * waste as little space as possible.
2726 */
2727 for (;;) {
2728 cnt = ftrace_allocate_records(pg, num_to_init);
2729 if (cnt < 0)
2730 goto free_pages;
2731
2732 num_to_init -= cnt;
2733 if (!num_to_init)
2734 break;
2735
2736 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2737 if (!pg->next)
2738 goto free_pages;
2739
2740 pg = pg->next;
2741 }
2742
2743 return start_pg;
2744
2745 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002746 pg = start_pg;
2747 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002748 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2749 free_pages((unsigned long)pg->records, order);
2750 start_pg = pg->next;
2751 kfree(pg);
2752 pg = start_pg;
2753 }
2754 pr_info("ftrace: FAILED to allocate memory for functions\n");
2755 return NULL;
2756}
2757
Steven Rostedt5072c592008-05-12 21:20:43 +02002758#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2759
2760struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002761 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002762 loff_t func_pos;
2763 struct ftrace_page *pg;
2764 struct dyn_ftrace *func;
2765 struct ftrace_func_probe *probe;
2766 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002767 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002768 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002769 int hidx;
2770 int idx;
2771 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002772};
2773
Ingo Molnare309b412008-05-12 21:20:51 +02002774static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002775t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002776{
2777 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002778 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002779 struct hlist_head *hhd;
2780
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002781 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002782 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002783
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002784 if (iter->probe)
2785 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002786 retry:
2787 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2788 return NULL;
2789
2790 hhd = &ftrace_func_hash[iter->hidx];
2791
2792 if (hlist_empty(hhd)) {
2793 iter->hidx++;
2794 hnd = NULL;
2795 goto retry;
2796 }
2797
2798 if (!hnd)
2799 hnd = hhd->first;
2800 else {
2801 hnd = hnd->next;
2802 if (!hnd) {
2803 iter->hidx++;
2804 goto retry;
2805 }
2806 }
2807
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002808 if (WARN_ON_ONCE(!hnd))
2809 return NULL;
2810
2811 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2812
2813 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002814}
2815
2816static void *t_hash_start(struct seq_file *m, loff_t *pos)
2817{
2818 struct ftrace_iterator *iter = m->private;
2819 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002820 loff_t l;
2821
Steven Rostedt69a30832011-12-19 15:21:16 -05002822 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2823 return NULL;
2824
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002825 if (iter->func_pos > *pos)
2826 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002827
Li Zefand82d6242009-06-24 09:54:54 +08002828 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002829 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002830 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002831 if (!p)
2832 break;
2833 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002834 if (!p)
2835 return NULL;
2836
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002837 /* Only set this if we have an item */
2838 iter->flags |= FTRACE_ITER_HASH;
2839
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002840 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002841}
2842
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002843static int
2844t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002845{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002846 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002847
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002848 rec = iter->probe;
2849 if (WARN_ON_ONCE(!rec))
2850 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002851
Steven Rostedt809dcf22009-02-16 23:06:01 -05002852 if (rec->ops->print)
2853 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2854
Steven Rostedtb375a112009-09-17 00:05:58 -04002855 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002856
2857 if (rec->data)
2858 seq_printf(m, ":%p", rec->data);
2859 seq_putc(m, '\n');
2860
2861 return 0;
2862}
2863
2864static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002865t_next(struct seq_file *m, void *v, loff_t *pos)
2866{
2867 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002868 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002869 struct dyn_ftrace *rec = NULL;
2870
Steven Rostedt45a4a232011-04-21 23:16:46 -04002871 if (unlikely(ftrace_disabled))
2872 return NULL;
2873
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002874 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002875 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002876
Steven Rostedt5072c592008-05-12 21:20:43 +02002877 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002878 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002879
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002880 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002881 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002882
Steven Rostedt5072c592008-05-12 21:20:43 +02002883 retry:
2884 if (iter->idx >= iter->pg->index) {
2885 if (iter->pg->next) {
2886 iter->pg = iter->pg->next;
2887 iter->idx = 0;
2888 goto retry;
2889 }
2890 } else {
2891 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002892 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002893 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002894
Steven Rostedt41c52c02008-05-22 11:46:33 -04002895 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002896 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04002897
2898 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002899 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002900
Steven Rostedt5072c592008-05-12 21:20:43 +02002901 rec = NULL;
2902 goto retry;
2903 }
2904 }
2905
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002906 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002907 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002908
2909 iter->func = rec;
2910
2911 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002912}
2913
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002914static void reset_iter_read(struct ftrace_iterator *iter)
2915{
2916 iter->pos = 0;
2917 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002918 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002919}
2920
2921static void *t_start(struct seq_file *m, loff_t *pos)
2922{
2923 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002924 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002925 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002926 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002927
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002928 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002929
2930 if (unlikely(ftrace_disabled))
2931 return NULL;
2932
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002933 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002934 * If an lseek was done, then reset and start from beginning.
2935 */
2936 if (*pos < iter->pos)
2937 reset_iter_read(iter);
2938
2939 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002940 * For set_ftrace_filter reading, if we have the filter
2941 * off, we can short cut and just print out that all
2942 * functions are enabled.
2943 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002944 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002945 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09002946 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002947 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002948 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002949 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002950 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002951 /* reset in case of seek/pread */
2952 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002953 return iter;
2954 }
2955
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002956 if (iter->flags & FTRACE_ITER_HASH)
2957 return t_hash_start(m, pos);
2958
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002959 /*
2960 * Unfortunately, we need to restart at ftrace_pages_start
2961 * every time we let go of the ftrace_mutex. This is because
2962 * those pointers can change without the lock.
2963 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002964 iter->pg = ftrace_pages_start;
2965 iter->idx = 0;
2966 for (l = 0; l <= *pos; ) {
2967 p = t_next(m, p, &l);
2968 if (!p)
2969 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002970 }
walimis5821e1b2008-11-15 15:19:06 +08002971
Steven Rostedt69a30832011-12-19 15:21:16 -05002972 if (!p)
2973 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002974
2975 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002976}
2977
2978static void t_stop(struct seq_file *m, void *p)
2979{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002980 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002981}
2982
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04002983void * __weak
2984arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2985{
2986 return NULL;
2987}
2988
2989static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
2990 struct dyn_ftrace *rec)
2991{
2992 void *ptr;
2993
2994 ptr = arch_ftrace_trampoline_func(ops, rec);
2995 if (ptr)
2996 seq_printf(m, " ->%pS", ptr);
2997}
2998
Steven Rostedt5072c592008-05-12 21:20:43 +02002999static int t_show(struct seq_file *m, void *v)
3000{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003001 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003002 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003003
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003004 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003005 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003006
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003007 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003008 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003009 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003010 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003011 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003012 return 0;
3013 }
3014
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003015 rec = iter->func;
3016
Steven Rostedt5072c592008-05-12 21:20:43 +02003017 if (!rec)
3018 return 0;
3019
Steven Rostedt647bcd02011-05-03 14:39:21 -04003020 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003021 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003022 struct ftrace_ops *ops = NULL;
3023
Steven Rostedt08f6fba2012-04-30 16:20:23 -04003024 seq_printf(m, " (%ld)%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003025 ftrace_rec_count(rec),
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003026 rec->flags & FTRACE_FL_REGS ? " R" : " ");
3027 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003028 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04003029 if (ops)
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003030 seq_printf(m, "\ttramp: %pS",
3031 (void *)ops->trampoline);
3032 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003033 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003034
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003035 }
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003036 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003037 }
3038
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003039 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003040
3041 return 0;
3042}
3043
James Morris88e9d342009-09-22 16:43:43 -07003044static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003045 .start = t_start,
3046 .next = t_next,
3047 .stop = t_stop,
3048 .show = t_show,
3049};
3050
Ingo Molnare309b412008-05-12 21:20:51 +02003051static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003052ftrace_avail_open(struct inode *inode, struct file *file)
3053{
3054 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003055
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003056 if (unlikely(ftrace_disabled))
3057 return -ENODEV;
3058
Jiri Olsa50e18b92012-04-25 10:23:39 +02003059 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3060 if (iter) {
3061 iter->pg = ftrace_pages_start;
3062 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003063 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003064
Jiri Olsa50e18b92012-04-25 10:23:39 +02003065 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003066}
3067
Steven Rostedt647bcd02011-05-03 14:39:21 -04003068static int
3069ftrace_enabled_open(struct inode *inode, struct file *file)
3070{
3071 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003072
Jiri Olsa50e18b92012-04-25 10:23:39 +02003073 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3074 if (iter) {
3075 iter->pg = ftrace_pages_start;
3076 iter->flags = FTRACE_ITER_ENABLED;
3077 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003078 }
3079
Jiri Olsa50e18b92012-04-25 10:23:39 +02003080 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003081}
3082
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003083/**
3084 * ftrace_regex_open - initialize function tracer filter files
3085 * @ops: The ftrace_ops that hold the hash filters
3086 * @flag: The type of filter to process
3087 * @inode: The inode, usually passed in to your open routine
3088 * @file: The file, usually passed in to your open routine
3089 *
3090 * ftrace_regex_open() initializes the filter files for the
3091 * @ops. Depending on @flag it may process the filter hash or
3092 * the notrace hash of @ops. With this called from the open
3093 * routine, you can use ftrace_filter_write() for the write
3094 * routine if @flag has FTRACE_ITER_FILTER set, or
3095 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003096 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003097 * release must call ftrace_regex_release().
3098 */
3099int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003100ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003101 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003102{
3103 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003104 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003105 int ret = 0;
3106
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003107 ftrace_ops_init(ops);
3108
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003109 if (unlikely(ftrace_disabled))
3110 return -ENODEV;
3111
Steven Rostedt5072c592008-05-12 21:20:43 +02003112 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3113 if (!iter)
3114 return -ENOMEM;
3115
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003116 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3117 kfree(iter);
3118 return -ENOMEM;
3119 }
3120
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003121 iter->ops = ops;
3122 iter->flags = flag;
3123
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003124 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003125
Steven Rostedtf45948e2011-05-02 12:29:25 -04003126 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003127 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003128 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003129 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003130
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003131 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003132 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3133
3134 if (file->f_flags & O_TRUNC)
3135 iter->hash = alloc_ftrace_hash(size_bits);
3136 else
3137 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3138
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003139 if (!iter->hash) {
3140 trace_parser_put(&iter->parser);
3141 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003142 ret = -ENOMEM;
3143 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003144 }
3145 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003146
Steven Rostedt5072c592008-05-12 21:20:43 +02003147 if (file->f_mode & FMODE_READ) {
3148 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003149
3150 ret = seq_open(file, &show_ftrace_seq_ops);
3151 if (!ret) {
3152 struct seq_file *m = file->private_data;
3153 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003154 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003155 /* Failed */
3156 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003157 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003158 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003159 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003160 } else
3161 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003162
3163 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003164 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003165
3166 return ret;
3167}
3168
Steven Rostedt41c52c02008-05-22 11:46:33 -04003169static int
3170ftrace_filter_open(struct inode *inode, struct file *file)
3171{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003172 struct ftrace_ops *ops = inode->i_private;
3173
3174 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003175 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3176 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003177}
3178
3179static int
3180ftrace_notrace_open(struct inode *inode, struct file *file)
3181{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003182 struct ftrace_ops *ops = inode->i_private;
3183
3184 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003185 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003186}
3187
Steven Rostedt64e7c442009-02-13 17:08:48 -05003188static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003189{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003190 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003191 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003192
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003193 switch (type) {
3194 case MATCH_FULL:
3195 if (strcmp(str, regex) == 0)
3196 matched = 1;
3197 break;
3198 case MATCH_FRONT_ONLY:
3199 if (strncmp(str, regex, len) == 0)
3200 matched = 1;
3201 break;
3202 case MATCH_MIDDLE_ONLY:
3203 if (strstr(str, regex))
3204 matched = 1;
3205 break;
3206 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003207 slen = strlen(str);
3208 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003209 matched = 1;
3210 break;
3211 }
3212
3213 return matched;
3214}
3215
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003216static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003217enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003218{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003219 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003220 int ret = 0;
3221
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003222 entry = ftrace_lookup_ip(hash, rec->ip);
3223 if (not) {
3224 /* Do nothing if it doesn't exist */
3225 if (!entry)
3226 return 0;
3227
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003228 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003229 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003230 /* Do nothing if it exists */
3231 if (entry)
3232 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003233
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003234 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003235 }
3236 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003237}
3238
Steven Rostedt64e7c442009-02-13 17:08:48 -05003239static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003240ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3241 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003242{
3243 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003244 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003245
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003246 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3247
3248 if (mod) {
3249 /* module lookup requires matching the module */
3250 if (!modname || strcmp(modname, mod))
3251 return 0;
3252
3253 /* blank search means to match all funcs in the mod */
3254 if (!len)
3255 return 1;
3256 }
3257
Steven Rostedt64e7c442009-02-13 17:08:48 -05003258 return ftrace_match(str, regex, len, type);
3259}
3260
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003261static int
3262match_records(struct ftrace_hash *hash, char *buff,
3263 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003264{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003265 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003266 struct ftrace_page *pg;
3267 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003268 int type = MATCH_FULL;
3269 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003270 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003271 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003272
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003273 if (len) {
3274 type = filter_parse_regex(buff, len, &search, &not);
3275 search_len = strlen(search);
3276 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003277
Steven Rostedt52baf112009-02-14 01:15:39 -05003278 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003279
3280 if (unlikely(ftrace_disabled))
3281 goto out_unlock;
3282
Steven Rostedt265c8312009-02-13 12:43:56 -05003283 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003284 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003285 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003286 if (ret < 0) {
3287 found = ret;
3288 goto out_unlock;
3289 }
Li Zefan311d16d2009-12-08 11:15:11 +08003290 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003291 }
3292 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003293 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003294 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003295
3296 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003297}
3298
Steven Rostedt64e7c442009-02-13 17:08:48 -05003299static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003300ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003301{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003302 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003303}
3304
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003305static int
3306ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003307{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003308 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003309
Steven Rostedt64e7c442009-02-13 17:08:48 -05003310 /* blank or '*' mean the same */
3311 if (strcmp(buff, "*") == 0)
3312 buff[0] = 0;
3313
3314 /* handle the case of 'dont filter this module' */
3315 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3316 buff[0] = 0;
3317 not = 1;
3318 }
3319
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003320 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003321}
3322
Steven Rostedtf6180772009-02-14 00:40:25 -05003323/*
3324 * We register the module command as a template to show others how
3325 * to register the a command as well.
3326 */
3327
3328static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003329ftrace_mod_callback(struct ftrace_hash *hash,
3330 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003331{
3332 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003333 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003334
3335 /*
3336 * cmd == 'mod' because we only registered this func
3337 * for the 'mod' ftrace_func_command.
3338 * But if you register one func with multiple commands,
3339 * you can tell which command was used by the cmd
3340 * parameter.
3341 */
3342
3343 /* we must have a module name */
3344 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003345 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003346
3347 mod = strsep(&param, ":");
3348 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003349 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003350
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003351 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003352 if (!ret)
3353 ret = -EINVAL;
3354 if (ret < 0)
3355 return ret;
3356
3357 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003358}
3359
3360static struct ftrace_func_command ftrace_mod_cmd = {
3361 .name = "mod",
3362 .func = ftrace_mod_callback,
3363};
3364
3365static int __init ftrace_mod_cmd_init(void)
3366{
3367 return register_ftrace_command(&ftrace_mod_cmd);
3368}
Steven Rostedt6f415672012-10-05 12:13:07 -04003369core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003370
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003371static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003372 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003373{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003374 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003375 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003376 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003377
3378 key = hash_long(ip, FTRACE_HASH_BITS);
3379
3380 hhd = &ftrace_func_hash[key];
3381
3382 if (hlist_empty(hhd))
3383 return;
3384
3385 /*
3386 * Disable preemption for these calls to prevent a RCU grace
3387 * period. This syncs the hash iteration and freeing of items
3388 * on the hash. rcu_read_lock is too dangerous here.
3389 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003390 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003391 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003392 if (entry->ip == ip)
3393 entry->ops->func(ip, parent_ip, &entry->data);
3394 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003395 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003396}
3397
Steven Rostedtb6887d72009-02-17 12:32:04 -05003398static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003399{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003400 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003401 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003402 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003403};
3404
Steven Rostedtb6887d72009-02-17 12:32:04 -05003405static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003406
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003407static void __enable_ftrace_function_probe(struct ftrace_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003408{
Steven Rostedtb8489142011-05-04 09:27:52 -04003409 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003410 int i;
3411
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003412 if (ftrace_probe_registered) {
3413 /* still need to update the function call sites */
3414 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003415 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3416 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003417 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003418 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003419
3420 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3421 struct hlist_head *hhd = &ftrace_func_hash[i];
3422 if (hhd->first)
3423 break;
3424 }
3425 /* Nothing registered? */
3426 if (i == FTRACE_FUNC_HASHSIZE)
3427 return;
3428
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003429 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003430
Steven Rostedtb6887d72009-02-17 12:32:04 -05003431 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003432}
3433
Steven Rostedtb6887d72009-02-17 12:32:04 -05003434static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003435{
3436 int i;
3437
Steven Rostedtb6887d72009-02-17 12:32:04 -05003438 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003439 return;
3440
3441 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3442 struct hlist_head *hhd = &ftrace_func_hash[i];
3443 if (hhd->first)
3444 return;
3445 }
3446
3447 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003448 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003449
Steven Rostedtb6887d72009-02-17 12:32:04 -05003450 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003451}
3452
3453
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003454static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003455{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003456 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003457 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003458 kfree(entry);
3459}
3460
Steven Rostedt59df055f2009-02-14 15:29:06 -05003461int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003462register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003463 void *data)
3464{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003465 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003466 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003467 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003468 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003469 struct ftrace_page *pg;
3470 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003471 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003472 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003473 int count = 0;
3474 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003475 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003476
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003477 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003478 len = strlen(search);
3479
Steven Rostedtb6887d72009-02-17 12:32:04 -05003480 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003481 if (WARN_ON(not))
3482 return -EINVAL;
3483
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003484 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003485
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003486 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003487 if (!hash) {
3488 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003489 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003490 }
3491
3492 if (unlikely(ftrace_disabled)) {
3493 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003494 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003495 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003496
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003497 mutex_lock(&ftrace_lock);
3498
Steven Rostedt59df055f2009-02-14 15:29:06 -05003499 do_for_each_ftrace_rec(pg, rec) {
3500
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003501 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003502 continue;
3503
3504 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3505 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003506 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003507 if (!count)
3508 count = -ENOMEM;
3509 goto out_unlock;
3510 }
3511
3512 count++;
3513
3514 entry->data = data;
3515
3516 /*
3517 * The caller might want to do something special
3518 * for each function we find. We call the callback
3519 * to give the caller an opportunity to do so.
3520 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003521 if (ops->init) {
3522 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003523 /* caller does not like this func */
3524 kfree(entry);
3525 continue;
3526 }
3527 }
3528
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003529 ret = enter_record(hash, rec, 0);
3530 if (ret < 0) {
3531 kfree(entry);
3532 count = ret;
3533 goto out_unlock;
3534 }
3535
Steven Rostedt59df055f2009-02-14 15:29:06 -05003536 entry->ops = ops;
3537 entry->ip = rec->ip;
3538
3539 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3540 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3541
3542 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003543
3544 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003545
3546 __enable_ftrace_function_probe(old_hash);
3547
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003548 if (!ret)
3549 free_ftrace_hash_rcu(old_hash);
3550 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003551 count = ret;
3552
Steven Rostedt59df055f2009-02-14 15:29:06 -05003553 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003554 mutex_unlock(&ftrace_lock);
3555 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003556 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003557 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003558
3559 return count;
3560}
3561
3562enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003563 PROBE_TEST_FUNC = 1,
3564 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003565};
3566
3567static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003568__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003569 void *data, int flags)
3570{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003571 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003572 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003573 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003574 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003575 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003576 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003577 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003578 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003579 char str[KSYM_SYMBOL_LEN];
3580 int type = MATCH_FULL;
3581 int i, len = 0;
3582 char *search;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003583 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003584
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003585 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003586 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003587 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003588 int not;
3589
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003590 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003591 len = strlen(search);
3592
Steven Rostedtb6887d72009-02-17 12:32:04 -05003593 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003594 if (WARN_ON(not))
3595 return;
3596 }
3597
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003598 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003599
3600 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3601 if (!hash)
3602 /* Hmm, should report this somehow */
3603 goto out_unlock;
3604
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003605 INIT_LIST_HEAD(&free_list);
3606
Steven Rostedt59df055f2009-02-14 15:29:06 -05003607 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3608 struct hlist_head *hhd = &ftrace_func_hash[i];
3609
Sasha Levinb67bfe02013-02-27 17:06:00 -08003610 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003611
3612 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003613 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003614 continue;
3615
Steven Rostedtb6887d72009-02-17 12:32:04 -05003616 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003617 continue;
3618
3619 /* do this last, since it is the most expensive */
3620 if (glob) {
3621 kallsyms_lookup(entry->ip, NULL, NULL,
3622 NULL, str);
3623 if (!ftrace_match(str, glob, len, type))
3624 continue;
3625 }
3626
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003627 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3628 /* It is possible more than one entry had this ip */
3629 if (rec_entry)
3630 free_hash_entry(hash, rec_entry);
3631
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003632 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003633 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003634 }
3635 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003636 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003637 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003638 /*
3639 * Remove after the disable is called. Otherwise, if the last
3640 * probe is removed, a null hash means *all enabled*.
3641 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003642 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003643 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003644 if (!ret)
3645 free_ftrace_hash_rcu(old_hash);
3646
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003647 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3648 list_del(&entry->free_list);
3649 ftrace_free_entry(entry);
3650 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003651 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003652
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003653 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003654 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003655 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003656}
3657
3658void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003659unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003660 void *data)
3661{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003662 __unregister_ftrace_function_probe(glob, ops, data,
3663 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003664}
3665
3666void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003667unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003668{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003669 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003670}
3671
Steven Rostedtb6887d72009-02-17 12:32:04 -05003672void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003673{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003674 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003675}
3676
Steven Rostedtf6180772009-02-14 00:40:25 -05003677static LIST_HEAD(ftrace_commands);
3678static DEFINE_MUTEX(ftrace_cmd_mutex);
3679
Tom Zanussi38de93a2013-10-24 08:34:18 -05003680/*
3681 * Currently we only register ftrace commands from __init, so mark this
3682 * __init too.
3683 */
3684__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003685{
3686 struct ftrace_func_command *p;
3687 int ret = 0;
3688
3689 mutex_lock(&ftrace_cmd_mutex);
3690 list_for_each_entry(p, &ftrace_commands, list) {
3691 if (strcmp(cmd->name, p->name) == 0) {
3692 ret = -EBUSY;
3693 goto out_unlock;
3694 }
3695 }
3696 list_add(&cmd->list, &ftrace_commands);
3697 out_unlock:
3698 mutex_unlock(&ftrace_cmd_mutex);
3699
3700 return ret;
3701}
3702
Tom Zanussi38de93a2013-10-24 08:34:18 -05003703/*
3704 * Currently we only unregister ftrace commands from __init, so mark
3705 * this __init too.
3706 */
3707__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003708{
3709 struct ftrace_func_command *p, *n;
3710 int ret = -ENODEV;
3711
3712 mutex_lock(&ftrace_cmd_mutex);
3713 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3714 if (strcmp(cmd->name, p->name) == 0) {
3715 ret = 0;
3716 list_del_init(&p->list);
3717 goto out_unlock;
3718 }
3719 }
3720 out_unlock:
3721 mutex_unlock(&ftrace_cmd_mutex);
3722
3723 return ret;
3724}
3725
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003726static int ftrace_process_regex(struct ftrace_hash *hash,
3727 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003728{
Steven Rostedtf6180772009-02-14 00:40:25 -05003729 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003730 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003731 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003732
3733 func = strsep(&next, ":");
3734
3735 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003736 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003737 if (!ret)
3738 ret = -EINVAL;
3739 if (ret < 0)
3740 return ret;
3741 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003742 }
3743
Steven Rostedtf6180772009-02-14 00:40:25 -05003744 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003745
3746 command = strsep(&next, ":");
3747
Steven Rostedtf6180772009-02-14 00:40:25 -05003748 mutex_lock(&ftrace_cmd_mutex);
3749 list_for_each_entry(p, &ftrace_commands, list) {
3750 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003751 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003752 goto out_unlock;
3753 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003754 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003755 out_unlock:
3756 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003757
Steven Rostedtf6180772009-02-14 00:40:25 -05003758 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003759}
3760
Ingo Molnare309b412008-05-12 21:20:51 +02003761static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003762ftrace_regex_write(struct file *file, const char __user *ubuf,
3763 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003764{
3765 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003766 struct trace_parser *parser;
3767 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003768
Li Zefan4ba79782009-09-22 13:52:20 +08003769 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003770 return 0;
3771
Steven Rostedt5072c592008-05-12 21:20:43 +02003772 if (file->f_mode & FMODE_READ) {
3773 struct seq_file *m = file->private_data;
3774 iter = m->private;
3775 } else
3776 iter = file->private_data;
3777
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003778 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003779 return -ENODEV;
3780
3781 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003782
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003783 parser = &iter->parser;
3784 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003785
Li Zefan4ba79782009-09-22 13:52:20 +08003786 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003787 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003788 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003789 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003790 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003791 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003792 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003793 }
3794
Steven Rostedt5072c592008-05-12 21:20:43 +02003795 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003796 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003797 return ret;
3798}
3799
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003800ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003801ftrace_filter_write(struct file *file, const char __user *ubuf,
3802 size_t cnt, loff_t *ppos)
3803{
3804 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3805}
3806
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003807ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003808ftrace_notrace_write(struct file *file, const char __user *ubuf,
3809 size_t cnt, loff_t *ppos)
3810{
3811 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3812}
3813
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003814static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003815ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3816{
3817 struct ftrace_func_entry *entry;
3818
3819 if (!ftrace_location(ip))
3820 return -EINVAL;
3821
3822 if (remove) {
3823 entry = ftrace_lookup_ip(hash, ip);
3824 if (!entry)
3825 return -ENOENT;
3826 free_hash_entry(hash, entry);
3827 return 0;
3828 }
3829
3830 return add_hash_entry(hash, ip);
3831}
3832
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003833static void ftrace_ops_update_code(struct ftrace_ops *ops,
3834 struct ftrace_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003835{
3836 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003837 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003838}
3839
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003840static int
3841ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3842 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003843{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003844 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003845 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003846 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003847 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003848
Steven Rostedt41c52c02008-05-22 11:46:33 -04003849 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003850 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003851
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003852 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003853
Steven Rostedtf45948e2011-05-02 12:29:25 -04003854 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003855 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003856 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003857 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003858
Wang Nanb972cc52014-07-15 08:40:20 +08003859 if (reset)
3860 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
3861 else
3862 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3863
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003864 if (!hash) {
3865 ret = -ENOMEM;
3866 goto out_regex_unlock;
3867 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003868
Jiri Olsaac483c42012-01-02 10:04:14 +01003869 if (buf && !ftrace_match_records(hash, buf, len)) {
3870 ret = -EINVAL;
3871 goto out_regex_unlock;
3872 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003873 if (ip) {
3874 ret = ftrace_match_addr(hash, ip, remove);
3875 if (ret < 0)
3876 goto out_regex_unlock;
3877 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003878
3879 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003880 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003881 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003882 if (!ret) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003883 ftrace_ops_update_code(ops, old_hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003884 free_ftrace_hash_rcu(old_hash);
3885 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003886 mutex_unlock(&ftrace_lock);
3887
Jiri Olsaac483c42012-01-02 10:04:14 +01003888 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003889 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003890
3891 free_ftrace_hash(hash);
3892 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003893}
3894
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003895static int
3896ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3897 int reset, int enable)
3898{
3899 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3900}
3901
3902/**
3903 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3904 * @ops - the ops to set the filter with
3905 * @ip - the address to add to or remove from the filter.
3906 * @remove - non zero to remove the ip from the filter
3907 * @reset - non zero to reset all filters before applying this filter.
3908 *
3909 * Filters denote which functions should be enabled when tracing is enabled
3910 * If @ip is NULL, it failes to update filter.
3911 */
3912int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3913 int remove, int reset)
3914{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003915 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003916 return ftrace_set_addr(ops, ip, remove, reset, 1);
3917}
3918EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3919
3920static int
3921ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3922 int reset, int enable)
3923{
3924 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3925}
3926
Steven Rostedt77a2b372008-05-12 21:20:45 +02003927/**
3928 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003929 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003930 * @buf - the string that holds the function filter text.
3931 * @len - the length of the string.
3932 * @reset - non zero to reset all filters before applying this filter.
3933 *
3934 * Filters denote which functions should be enabled when tracing is enabled.
3935 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3936 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003937int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003938 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003939{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003940 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003941 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003942}
Steven Rostedt936e0742011-05-05 22:54:01 -04003943EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003944
Steven Rostedt41c52c02008-05-22 11:46:33 -04003945/**
3946 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003947 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003948 * @buf - the string that holds the function notrace text.
3949 * @len - the length of the string.
3950 * @reset - non zero to reset all filters before applying this filter.
3951 *
3952 * Notrace Filters denote which functions should not be enabled when tracing
3953 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3954 * for tracing.
3955 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003956int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003957 int len, int reset)
3958{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003959 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003960 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003961}
3962EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3963/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003964 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003965 * @buf - the string that holds the function filter text.
3966 * @len - the length of the string.
3967 * @reset - non zero to reset all filters before applying this filter.
3968 *
3969 * Filters denote which functions should be enabled when tracing is enabled.
3970 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3971 */
3972void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3973{
3974 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3975}
3976EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3977
3978/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08003979 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04003980 * @buf - the string that holds the function notrace text.
3981 * @len - the length of the string.
3982 * @reset - non zero to reset all filters before applying this filter.
3983 *
3984 * Notrace Filters denote which functions should not be enabled when tracing
3985 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3986 * for tracing.
3987 */
3988void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003989{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003990 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003991}
Steven Rostedt936e0742011-05-05 22:54:01 -04003992EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003993
Steven Rostedt2af15d62009-05-28 13:37:24 -04003994/*
3995 * command line interface to allow users to set filters on boot up.
3996 */
3997#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3998static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3999static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4000
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004001/* Used by function selftest to not test if filter is set */
4002bool ftrace_filter_param __initdata;
4003
Steven Rostedt2af15d62009-05-28 13:37:24 -04004004static int __init set_ftrace_notrace(char *str)
4005{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004006 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004007 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004008 return 1;
4009}
4010__setup("ftrace_notrace=", set_ftrace_notrace);
4011
4012static int __init set_ftrace_filter(char *str)
4013{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004014 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004015 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004016 return 1;
4017}
4018__setup("ftrace_filter=", set_ftrace_filter);
4019
Stefan Assmann369bc182009-10-12 22:17:21 +02004020#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004021static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004022static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004023static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004024
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004025static unsigned long save_global_trampoline;
4026static unsigned long save_global_flags;
4027
Stefan Assmann369bc182009-10-12 22:17:21 +02004028static int __init set_graph_function(char *str)
4029{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004030 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004031 return 1;
4032}
4033__setup("ftrace_graph_filter=", set_graph_function);
4034
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004035static int __init set_graph_notrace_function(char *str)
4036{
4037 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4038 return 1;
4039}
4040__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4041
4042static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004043{
4044 int ret;
4045 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004046 unsigned long *table = ftrace_graph_funcs;
4047 int *count = &ftrace_graph_count;
4048
4049 if (!enable) {
4050 table = ftrace_graph_notrace_funcs;
4051 count = &ftrace_graph_notrace_count;
4052 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004053
4054 while (buf) {
4055 func = strsep(&buf, ",");
4056 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004057 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004058 if (ret)
4059 printk(KERN_DEBUG "ftrace: function %s not "
4060 "traceable\n", func);
4061 }
4062}
4063#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4064
Steven Rostedt2a85a372011-12-19 21:57:44 -05004065void __init
4066ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004067{
4068 char *func;
4069
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004070 ftrace_ops_init(ops);
4071
Steven Rostedt2af15d62009-05-28 13:37:24 -04004072 while (buf) {
4073 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004074 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004075 }
4076}
4077
4078static void __init set_ftrace_early_filters(void)
4079{
4080 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004081 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004082 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004083 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004084#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4085 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004086 set_ftrace_early_graph(ftrace_graph_buf, 1);
4087 if (ftrace_graph_notrace_buf[0])
4088 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004089#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004090}
4091
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004092int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004093{
4094 struct seq_file *m = (struct seq_file *)file->private_data;
4095 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004096 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004097 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004098 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004099 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004100 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004101
Steven Rostedt5072c592008-05-12 21:20:43 +02004102 if (file->f_mode & FMODE_READ) {
4103 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004104 seq_release(inode, file);
4105 } else
4106 iter = file->private_data;
4107
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004108 parser = &iter->parser;
4109 if (trace_parser_loaded(parser)) {
4110 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004111 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004112 }
4113
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004114 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004115
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004116 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004117
Steven Rostedt058e2972011-04-29 22:35:33 -04004118 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004119 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4120
4121 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004122 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004123 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004124 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004125
Steven Rostedt058e2972011-04-29 22:35:33 -04004126 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004127 old_hash = *orig_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004128 ret = ftrace_hash_move(iter->ops, filter_hash,
4129 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004130 if (!ret) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004131 ftrace_ops_update_code(iter->ops, old_hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004132 free_ftrace_hash_rcu(old_hash);
4133 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004134 mutex_unlock(&ftrace_lock);
4135 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004136
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004137 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004138 free_ftrace_hash(iter->hash);
4139 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004140
Steven Rostedt5072c592008-05-12 21:20:43 +02004141 return 0;
4142}
4143
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004144static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004145 .open = ftrace_avail_open,
4146 .read = seq_read,
4147 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004148 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004149};
4150
Steven Rostedt647bcd02011-05-03 14:39:21 -04004151static const struct file_operations ftrace_enabled_fops = {
4152 .open = ftrace_enabled_open,
4153 .read = seq_read,
4154 .llseek = seq_lseek,
4155 .release = seq_release_private,
4156};
4157
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004158static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004159 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004160 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004161 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004162 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004163 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004164};
4165
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004166static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004167 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004168 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004169 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004170 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004171 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004172};
4173
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004174#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4175
4176static DEFINE_MUTEX(graph_lock);
4177
4178int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004179int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004180unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004181unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004182
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004183struct ftrace_graph_data {
4184 unsigned long *table;
4185 size_t size;
4186 int *count;
4187 const struct seq_operations *seq_ops;
4188};
4189
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004190static void *
Li Zefan85951842009-06-24 09:54:00 +08004191__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004192{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004193 struct ftrace_graph_data *fgd = m->private;
4194
4195 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004196 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004197 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004198}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004199
Li Zefan85951842009-06-24 09:54:00 +08004200static void *
4201g_next(struct seq_file *m, void *v, loff_t *pos)
4202{
4203 (*pos)++;
4204 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004205}
4206
4207static void *g_start(struct seq_file *m, loff_t *pos)
4208{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004209 struct ftrace_graph_data *fgd = m->private;
4210
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004211 mutex_lock(&graph_lock);
4212
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004213 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004214 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004215 return (void *)1;
4216
Li Zefan85951842009-06-24 09:54:00 +08004217 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004218}
4219
4220static void g_stop(struct seq_file *m, void *p)
4221{
4222 mutex_unlock(&graph_lock);
4223}
4224
4225static int g_show(struct seq_file *m, void *v)
4226{
4227 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004228
4229 if (!ptr)
4230 return 0;
4231
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004232 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004233 struct ftrace_graph_data *fgd = m->private;
4234
4235 if (fgd->table == ftrace_graph_funcs)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004236 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004237 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004238 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004239 return 0;
4240 }
4241
Steven Rostedtb375a112009-09-17 00:05:58 -04004242 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004243
4244 return 0;
4245}
4246
James Morris88e9d342009-09-22 16:43:43 -07004247static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004248 .start = g_start,
4249 .next = g_next,
4250 .stop = g_stop,
4251 .show = g_show,
4252};
4253
4254static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004255__ftrace_graph_open(struct inode *inode, struct file *file,
4256 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004257{
4258 int ret = 0;
4259
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004260 mutex_lock(&graph_lock);
4261 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004262 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004263 *fgd->count = 0;
4264 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004265 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004266 mutex_unlock(&graph_lock);
4267
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004268 if (file->f_mode & FMODE_READ) {
4269 ret = seq_open(file, fgd->seq_ops);
4270 if (!ret) {
4271 struct seq_file *m = file->private_data;
4272 m->private = fgd;
4273 }
4274 } else
4275 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004276
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004277 return ret;
4278}
4279
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004280static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004281ftrace_graph_open(struct inode *inode, struct file *file)
4282{
4283 struct ftrace_graph_data *fgd;
4284
4285 if (unlikely(ftrace_disabled))
4286 return -ENODEV;
4287
4288 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4289 if (fgd == NULL)
4290 return -ENOMEM;
4291
4292 fgd->table = ftrace_graph_funcs;
4293 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4294 fgd->count = &ftrace_graph_count;
4295 fgd->seq_ops = &ftrace_graph_seq_ops;
4296
4297 return __ftrace_graph_open(inode, file, fgd);
4298}
4299
4300static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004301ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4302{
4303 struct ftrace_graph_data *fgd;
4304
4305 if (unlikely(ftrace_disabled))
4306 return -ENODEV;
4307
4308 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4309 if (fgd == NULL)
4310 return -ENOMEM;
4311
4312 fgd->table = ftrace_graph_notrace_funcs;
4313 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4314 fgd->count = &ftrace_graph_notrace_count;
4315 fgd->seq_ops = &ftrace_graph_seq_ops;
4316
4317 return __ftrace_graph_open(inode, file, fgd);
4318}
4319
4320static int
Li Zefan87827112009-07-23 11:29:11 +08004321ftrace_graph_release(struct inode *inode, struct file *file)
4322{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004323 if (file->f_mode & FMODE_READ) {
4324 struct seq_file *m = file->private_data;
4325
4326 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004327 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004328 } else {
4329 kfree(file->private_data);
4330 }
4331
Li Zefan87827112009-07-23 11:29:11 +08004332 return 0;
4333}
4334
4335static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004336ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004337{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004338 struct dyn_ftrace *rec;
4339 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004340 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004341 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004342 int type, not;
4343 char *search;
4344 bool exists;
4345 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004346
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004347 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004348 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004349 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004350 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004351
4352 search_len = strlen(search);
4353
Steven Rostedt52baf112009-02-14 01:15:39 -05004354 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004355
4356 if (unlikely(ftrace_disabled)) {
4357 mutex_unlock(&ftrace_lock);
4358 return -ENODEV;
4359 }
4360
Steven Rostedt265c8312009-02-13 12:43:56 -05004361 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004362
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004363 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004364 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004365 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004366 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004367 if (array[i] == rec->ip) {
4368 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004369 break;
4370 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004371 }
4372
4373 if (!not) {
4374 fail = 0;
4375 if (!exists) {
4376 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004377 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004378 goto out;
4379 }
4380 } else {
4381 if (exists) {
4382 array[i] = array[--(*idx)];
4383 array[*idx] = 0;
4384 fail = 0;
4385 }
4386 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004387 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004388 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004389out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004390 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004391
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004392 if (fail)
4393 return -EINVAL;
4394
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004395 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004396}
4397
4398static ssize_t
4399ftrace_graph_write(struct file *file, const char __user *ubuf,
4400 size_t cnt, loff_t *ppos)
4401{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004402 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004403 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004404 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004405
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004406 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004407 return 0;
4408
Namhyung Kim6a101082013-10-14 17:24:25 +09004409 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4410 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004411
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004412 read = trace_get_user(&parser, ubuf, cnt, ppos);
4413
Li Zefan4ba79782009-09-22 13:52:20 +08004414 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004415 parser.buffer[parser.idx] = 0;
4416
Namhyung Kim6a101082013-10-14 17:24:25 +09004417 mutex_lock(&graph_lock);
4418
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004419 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004420 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4421 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004422
4423 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004424 }
4425
Namhyung Kim6a101082013-10-14 17:24:25 +09004426 if (!ret)
4427 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004428
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004429 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004430
4431 return ret;
4432}
4433
4434static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004435 .open = ftrace_graph_open,
4436 .read = seq_read,
4437 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004438 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004439 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004440};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004441
4442static const struct file_operations ftrace_graph_notrace_fops = {
4443 .open = ftrace_graph_notrace_open,
4444 .read = seq_read,
4445 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004446 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004447 .release = ftrace_graph_release,
4448};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004449#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4450
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004451void ftrace_create_filter_files(struct ftrace_ops *ops,
4452 struct dentry *parent)
4453{
4454
4455 trace_create_file("set_ftrace_filter", 0644, parent,
4456 ops, &ftrace_filter_fops);
4457
4458 trace_create_file("set_ftrace_notrace", 0644, parent,
4459 ops, &ftrace_notrace_fops);
4460}
4461
4462/*
4463 * The name "destroy_filter_files" is really a misnomer. Although
4464 * in the future, it may actualy delete the files, but this is
4465 * really intended to make sure the ops passed in are disabled
4466 * and that when this function returns, the caller is free to
4467 * free the ops.
4468 *
4469 * The "destroy" name is only to match the "create" name that this
4470 * should be paired with.
4471 */
4472void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4473{
4474 mutex_lock(&ftrace_lock);
4475 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4476 ftrace_shutdown(ops, 0);
4477 ops->flags |= FTRACE_OPS_FL_DELETED;
4478 mutex_unlock(&ftrace_lock);
4479}
4480
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004481static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004482{
Steven Rostedt5072c592008-05-12 21:20:43 +02004483
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004484 trace_create_file("available_filter_functions", 0444,
4485 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004486
Steven Rostedt647bcd02011-05-03 14:39:21 -04004487 trace_create_file("enabled_functions", 0444,
4488 d_tracer, NULL, &ftrace_enabled_fops);
4489
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004490 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004491
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004492#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004493 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004494 NULL,
4495 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004496 trace_create_file("set_graph_notrace", 0444, d_tracer,
4497 NULL,
4498 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004499#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4500
Steven Rostedt5072c592008-05-12 21:20:43 +02004501 return 0;
4502}
4503
Steven Rostedt9fd49322012-04-24 22:32:06 -04004504static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004505{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004506 const unsigned long *ipa = a;
4507 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004508
Steven Rostedt9fd49322012-04-24 22:32:06 -04004509 if (*ipa > *ipb)
4510 return 1;
4511 if (*ipa < *ipb)
4512 return -1;
4513 return 0;
4514}
4515
4516static void ftrace_swap_ips(void *a, void *b, int size)
4517{
4518 unsigned long *ipa = a;
4519 unsigned long *ipb = b;
4520 unsigned long t;
4521
4522 t = *ipa;
4523 *ipa = *ipb;
4524 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004525}
4526
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004527static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004528 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004529 unsigned long *end)
4530{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004531 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004532 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004533 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004534 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004535 unsigned long *p;
4536 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004537 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004538 int ret = -ENOMEM;
4539
4540 count = end - start;
4541
4542 if (!count)
4543 return 0;
4544
Steven Rostedt9fd49322012-04-24 22:32:06 -04004545 sort(start, count, sizeof(*start),
4546 ftrace_cmp_ips, ftrace_swap_ips);
4547
Steven Rostedt706c81f2012-04-24 23:45:26 -04004548 start_pg = ftrace_allocate_pages(count);
4549 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004550 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004551
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004552 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004553
Steven Rostedt32082302011-12-16 14:42:37 -05004554 /*
4555 * Core and each module needs their own pages, as
4556 * modules will free them when they are removed.
4557 * Force a new page to be allocated for modules.
4558 */
Steven Rostedta7900872011-12-16 16:23:44 -05004559 if (!mod) {
4560 WARN_ON(ftrace_pages || ftrace_pages_start);
4561 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004562 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004563 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004564 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004565 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004566
Steven Rostedta7900872011-12-16 16:23:44 -05004567 if (WARN_ON(ftrace_pages->next)) {
4568 /* Hmm, we have free pages? */
4569 while (ftrace_pages->next)
4570 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004571 }
Steven Rostedta7900872011-12-16 16:23:44 -05004572
Steven Rostedt706c81f2012-04-24 23:45:26 -04004573 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004574 }
4575
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004576 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004577 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004578 while (p < end) {
4579 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004580 /*
4581 * Some architecture linkers will pad between
4582 * the different mcount_loc sections of different
4583 * object files to satisfy alignments.
4584 * Skip any NULL pointers.
4585 */
4586 if (!addr)
4587 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004588
4589 if (pg->index == pg->size) {
4590 /* We should have allocated enough */
4591 if (WARN_ON(!pg->next))
4592 break;
4593 pg = pg->next;
4594 }
4595
4596 rec = &pg->records[pg->index++];
4597 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004598 }
4599
Steven Rostedt706c81f2012-04-24 23:45:26 -04004600 /* We should have used all pages */
4601 WARN_ON(pg->next);
4602
4603 /* Assign the last page to ftrace_pages */
4604 ftrace_pages = pg;
4605
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004606 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004607 * We only need to disable interrupts on start up
4608 * because we are modifying code that an interrupt
4609 * may execute, and the modification is not atomic.
4610 * But for modules, nothing runs the code we modify
4611 * until we are finished with it, and there's no
4612 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004613 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004614 if (!mod)
4615 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004616 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004617 if (!mod)
4618 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004619 ret = 0;
4620 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004621 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004622
Steven Rostedta7900872011-12-16 16:23:44 -05004623 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004624}
4625
Steven Rostedt93eb6772009-04-15 13:24:06 -04004626#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004627
4628#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4629
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004630void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004631{
4632 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004633 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004634 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004635 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004636
Steven Rostedt93eb6772009-04-15 13:24:06 -04004637 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004638
4639 if (ftrace_disabled)
4640 goto out_unlock;
4641
Steven Rostedt32082302011-12-16 14:42:37 -05004642 /*
4643 * Each module has its own ftrace_pages, remove
4644 * them from the list.
4645 */
4646 last_pg = &ftrace_pages_start;
4647 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4648 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004649 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004650 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004651 * As core pages are first, the first
4652 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004653 */
Steven Rostedt32082302011-12-16 14:42:37 -05004654 if (WARN_ON(pg == ftrace_pages_start))
4655 goto out_unlock;
4656
4657 /* Check if we are deleting the last page */
4658 if (pg == ftrace_pages)
4659 ftrace_pages = next_to_ftrace_page(last_pg);
4660
4661 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004662 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4663 free_pages((unsigned long)pg->records, order);
4664 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004665 } else
4666 last_pg = &pg->next;
4667 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004668 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004669 mutex_unlock(&ftrace_lock);
4670}
4671
4672static void ftrace_init_module(struct module *mod,
4673 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004674{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004675 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004676 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004677 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004678}
4679
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004680void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004681{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004682 ftrace_init_module(mod, mod->ftrace_callsites,
4683 mod->ftrace_callsites +
4684 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004685}
4686
4687static int ftrace_module_notify_exit(struct notifier_block *self,
4688 unsigned long val, void *data)
4689{
4690 struct module *mod = data;
4691
4692 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004693 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004694
4695 return 0;
4696}
4697#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004698static int ftrace_module_notify_exit(struct notifier_block *self,
4699 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004700{
4701 return 0;
4702}
4703#endif /* CONFIG_MODULES */
4704
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004705struct notifier_block ftrace_module_exit_nb = {
4706 .notifier_call = ftrace_module_notify_exit,
4707 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4708};
4709
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004710void __init ftrace_init(void)
4711{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004712 extern unsigned long __start_mcount_loc[];
4713 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004714 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004715 int ret;
4716
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004717 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004718 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004719 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004720 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004721 goto failed;
4722
4723 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004724 if (!count) {
4725 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004726 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004727 }
4728
4729 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4730 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004731
4732 last_ftrace_enabled = ftrace_enabled = 1;
4733
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004734 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004735 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004736 __stop_mcount_loc);
4737
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004738 ret = register_module_notifier(&ftrace_module_exit_nb);
4739 if (ret)
4740 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004741
Steven Rostedt2af15d62009-05-28 13:37:24 -04004742 set_ftrace_early_filters();
4743
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004744 return;
4745 failed:
4746 ftrace_disabled = 1;
4747}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004748
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004749/* Do nothing if arch does not support this */
4750void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
4751{
4752}
4753
4754static void ftrace_update_trampoline(struct ftrace_ops *ops)
4755{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04004756
4757/*
4758 * Currently there's no safe way to free a trampoline when the kernel
4759 * is configured with PREEMPT. That is because a task could be preempted
4760 * when it jumped to the trampoline, it may be preempted for a long time
4761 * depending on the system load, and currently there's no way to know
4762 * when it will be off the trampoline. If the trampoline is freed
4763 * too early, when the task runs again, it will be executing on freed
4764 * memory and crash.
4765 */
4766#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004767 /* Currently, only non dynamic ops can have a trampoline */
4768 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
4769 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04004770#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004771
4772 arch_ftrace_update_trampoline(ops);
4773}
4774
Steven Rostedt3d083392008-05-12 21:20:42 +02004775#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004776
Steven Rostedt2b499382011-05-03 22:49:52 -04004777static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004778 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004779 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004780};
4781
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004782static int __init ftrace_nodyn_init(void)
4783{
4784 ftrace_enabled = 1;
4785 return 0;
4786}
Steven Rostedt6f415672012-10-05 12:13:07 -04004787core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004788
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004789static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4790static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04004791static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004792/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004793# define ftrace_startup(ops, command) \
4794 ({ \
4795 int ___ret = __register_ftrace_function(ops); \
4796 if (!___ret) \
4797 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4798 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004799 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05004800# define ftrace_shutdown(ops, command) \
4801 ({ \
4802 int ___ret = __unregister_ftrace_function(ops); \
4803 if (!___ret) \
4804 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
4805 ___ret; \
4806 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004807
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004808# define ftrace_startup_sysctl() do { } while (0)
4809# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004810
4811static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004812ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004813{
4814 return 1;
4815}
4816
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004817static void ftrace_update_trampoline(struct ftrace_ops *ops)
4818{
4819}
4820
Steven Rostedt3d083392008-05-12 21:20:42 +02004821#endif /* CONFIG_DYNAMIC_FTRACE */
4822
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004823__init void ftrace_init_global_array_ops(struct trace_array *tr)
4824{
4825 tr->ops = &global_ops;
4826 tr->ops->private = tr;
4827}
4828
4829void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4830{
4831 /* If we filter on pids, update to use the pid function */
4832 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4833 if (WARN_ON(tr->ops->func != ftrace_stub))
4834 printk("ftrace ops had %pS for function\n",
4835 tr->ops->func);
4836 /* Only the top level instance does pid tracing */
4837 if (!list_empty(&ftrace_pids)) {
4838 set_ftrace_pid_function(func);
4839 func = ftrace_pid_func;
4840 }
4841 }
4842 tr->ops->func = func;
4843 tr->ops->private = tr;
4844}
4845
4846void ftrace_reset_array_ops(struct trace_array *tr)
4847{
4848 tr->ops->func = ftrace_stub;
4849}
4850
Steven Rostedtb8489142011-05-04 09:27:52 -04004851static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004852ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004853 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004854{
Jiri Olsae2484912012-02-15 15:51:48 +01004855 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4856 return;
4857
4858 /*
4859 * Some of the ops may be dynamically allocated,
4860 * they must be freed after a synchronize_sched().
4861 */
4862 preempt_disable_notrace();
4863 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004864
4865 /*
4866 * Control funcs (perf) uses RCU. Only trace if
4867 * RCU is currently active.
4868 */
4869 if (!rcu_is_watching())
4870 goto out;
4871
Steven Rostedt0a016402012-11-02 17:03:03 -04004872 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004873 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4874 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004875 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004876 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004877 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004878 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004879 trace_recursion_clear(TRACE_CONTROL_BIT);
4880 preempt_enable_notrace();
4881}
4882
4883static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004884 .func = ftrace_ops_control_func,
4885 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004886 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004887};
4888
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004889static inline void
4890__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004891 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004892{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004893 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004894 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004895
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004896 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4897 if (bit < 0)
4898 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004899
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004900 /*
4901 * Some of the ops may be dynamically allocated,
4902 * they must be freed after a synchronize_sched().
4903 */
4904 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004905 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004906 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04004907 if (FTRACE_WARN_ON(!op->func)) {
4908 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004909 goto out;
4910 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04004911 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004912 }
Steven Rostedt0a016402012-11-02 17:03:03 -04004913 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05004914out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004915 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004916 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004917}
4918
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004919/*
4920 * Some archs only support passing ip and parent_ip. Even though
4921 * the list function ignores the op parameter, we do not want any
4922 * C side effects, where a function is called without the caller
4923 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004924 * Archs are to support both the regs and ftrace_ops at the same time.
4925 * If they support ftrace_ops, it is assumed they support regs.
4926 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004927 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4928 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004929 * An architecture can pass partial regs with ftrace_ops and still
4930 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004931 */
4932#if ARCH_SUPPORTS_FTRACE_OPS
4933static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004934 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004935{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004936 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004937}
4938#else
4939static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4940{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004941 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004942}
4943#endif
4944
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04004945/*
4946 * If there's only one function registered but it does not support
4947 * recursion, this function will be called by the mcount trampoline.
4948 * This function will handle recursion protection.
4949 */
4950static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
4951 struct ftrace_ops *op, struct pt_regs *regs)
4952{
4953 int bit;
4954
4955 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4956 if (bit < 0)
4957 return;
4958
4959 op->func(ip, parent_ip, op, regs);
4960
4961 trace_clear_recursion(bit);
4962}
4963
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04004964/**
4965 * ftrace_ops_get_func - get the function a trampoline should call
4966 * @ops: the ops to get the function for
4967 *
4968 * Normally the mcount trampoline will call the ops->func, but there
4969 * are times that it should not. For example, if the ops does not
4970 * have its own recursion protection, then it should call the
4971 * ftrace_ops_recurs_func() instead.
4972 *
4973 * Returns the function that the trampoline should call for @ops.
4974 */
4975ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
4976{
4977 /*
4978 * If this is a dynamic ops or we force list func,
4979 * then it needs to call the list anyway.
4980 */
4981 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
4982 return ftrace_ops_list_func;
4983
4984 /*
4985 * If the func handles its own recursion, call it directly.
4986 * Otherwise call the recursion protected function that
4987 * will call the ftrace ops function.
4988 */
4989 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
4990 return ftrace_ops_recurs_func;
4991
4992 return ops->func;
4993}
4994
Steven Rostedte32d8952008-12-04 00:26:41 -05004995static void clear_ftrace_swapper(void)
4996{
4997 struct task_struct *p;
4998 int cpu;
4999
5000 get_online_cpus();
5001 for_each_online_cpu(cpu) {
5002 p = idle_task(cpu);
5003 clear_tsk_trace_trace(p);
5004 }
5005 put_online_cpus();
5006}
5007
5008static void set_ftrace_swapper(void)
5009{
5010 struct task_struct *p;
5011 int cpu;
5012
5013 get_online_cpus();
5014 for_each_online_cpu(cpu) {
5015 p = idle_task(cpu);
5016 set_tsk_trace_trace(p);
5017 }
5018 put_online_cpus();
5019}
5020
5021static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005022{
5023 struct task_struct *p;
5024
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005025 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05005026 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05005027 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05005028 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005029 rcu_read_unlock();
5030
Steven Rostedte32d8952008-12-04 00:26:41 -05005031 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005032}
5033
Steven Rostedte32d8952008-12-04 00:26:41 -05005034static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005035{
5036 struct task_struct *p;
5037
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005038 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005039 do_each_pid_task(pid, PIDTYPE_PID, p) {
5040 set_tsk_trace_trace(p);
5041 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005042 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005043}
5044
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005045static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005046{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005047 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005048 clear_ftrace_swapper();
5049 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005050 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05005051}
5052
5053static void set_ftrace_pid_task(struct pid *pid)
5054{
5055 if (pid == ftrace_swapper_pid)
5056 set_ftrace_swapper();
5057 else
5058 set_ftrace_pid(pid);
5059}
5060
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005061static int ftrace_pid_add(int p)
5062{
5063 struct pid *pid;
5064 struct ftrace_pid *fpid;
5065 int ret = -EINVAL;
5066
5067 mutex_lock(&ftrace_lock);
5068
5069 if (!p)
5070 pid = ftrace_swapper_pid;
5071 else
5072 pid = find_get_pid(p);
5073
5074 if (!pid)
5075 goto out;
5076
5077 ret = 0;
5078
5079 list_for_each_entry(fpid, &ftrace_pids, list)
5080 if (fpid->pid == pid)
5081 goto out_put;
5082
5083 ret = -ENOMEM;
5084
5085 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5086 if (!fpid)
5087 goto out_put;
5088
5089 list_add(&fpid->list, &ftrace_pids);
5090 fpid->pid = pid;
5091
5092 set_ftrace_pid_task(pid);
5093
5094 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005095
5096 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005097
5098 mutex_unlock(&ftrace_lock);
5099 return 0;
5100
5101out_put:
5102 if (pid != ftrace_swapper_pid)
5103 put_pid(pid);
5104
5105out:
5106 mutex_unlock(&ftrace_lock);
5107 return ret;
5108}
5109
5110static void ftrace_pid_reset(void)
5111{
5112 struct ftrace_pid *fpid, *safe;
5113
5114 mutex_lock(&ftrace_lock);
5115 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5116 struct pid *pid = fpid->pid;
5117
5118 clear_ftrace_pid_task(pid);
5119
5120 list_del(&fpid->list);
5121 kfree(fpid);
5122 }
5123
5124 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005125 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005126
5127 mutex_unlock(&ftrace_lock);
5128}
5129
5130static void *fpid_start(struct seq_file *m, loff_t *pos)
5131{
5132 mutex_lock(&ftrace_lock);
5133
5134 if (list_empty(&ftrace_pids) && (!*pos))
5135 return (void *) 1;
5136
5137 return seq_list_start(&ftrace_pids, *pos);
5138}
5139
5140static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5141{
5142 if (v == (void *)1)
5143 return NULL;
5144
5145 return seq_list_next(v, &ftrace_pids, pos);
5146}
5147
5148static void fpid_stop(struct seq_file *m, void *p)
5149{
5150 mutex_unlock(&ftrace_lock);
5151}
5152
5153static int fpid_show(struct seq_file *m, void *v)
5154{
5155 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5156
5157 if (v == (void *)1) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005158 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005159 return 0;
5160 }
5161
5162 if (fpid->pid == ftrace_swapper_pid)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005163 seq_puts(m, "swapper tasks\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005164 else
5165 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5166
5167 return 0;
5168}
5169
5170static const struct seq_operations ftrace_pid_sops = {
5171 .start = fpid_start,
5172 .next = fpid_next,
5173 .stop = fpid_stop,
5174 .show = fpid_show,
5175};
5176
5177static int
5178ftrace_pid_open(struct inode *inode, struct file *file)
5179{
5180 int ret = 0;
5181
5182 if ((file->f_mode & FMODE_WRITE) &&
5183 (file->f_flags & O_TRUNC))
5184 ftrace_pid_reset();
5185
5186 if (file->f_mode & FMODE_READ)
5187 ret = seq_open(file, &ftrace_pid_sops);
5188
5189 return ret;
5190}
5191
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005192static ssize_t
5193ftrace_pid_write(struct file *filp, const char __user *ubuf,
5194 size_t cnt, loff_t *ppos)
5195{
Ingo Molnar457dc922009-11-23 11:03:28 +01005196 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005197 long val;
5198 int ret;
5199
5200 if (cnt >= sizeof(buf))
5201 return -EINVAL;
5202
5203 if (copy_from_user(&buf, ubuf, cnt))
5204 return -EFAULT;
5205
5206 buf[cnt] = 0;
5207
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005208 /*
5209 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5210 * to clean the filter quietly.
5211 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005212 tmp = strstrip(buf);
5213 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005214 return 1;
5215
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005216 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005217 if (ret < 0)
5218 return ret;
5219
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005220 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005221
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005222 return ret ? ret : cnt;
5223}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005224
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005225static int
5226ftrace_pid_release(struct inode *inode, struct file *file)
5227{
5228 if (file->f_mode & FMODE_READ)
5229 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005230
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005231 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005232}
5233
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005234static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005235 .open = ftrace_pid_open,
5236 .write = ftrace_pid_write,
5237 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005238 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005239 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005240};
5241
5242static __init int ftrace_init_debugfs(void)
5243{
5244 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005245
5246 d_tracer = tracing_init_dentry();
5247 if (!d_tracer)
5248 return 0;
5249
5250 ftrace_init_dyn_debugfs(d_tracer);
5251
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005252 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5253 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005254
5255 ftrace_profile_debugfs(d_tracer);
5256
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005257 return 0;
5258}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005259fs_initcall(ftrace_init_debugfs);
5260
Steven Rostedt3d083392008-05-12 21:20:42 +02005261/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005262 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005263 *
5264 * This function should be used by panic code. It stops ftrace
5265 * but in a not so nice way. If you need to simply kill ftrace
5266 * from a non-atomic section, use ftrace_kill.
5267 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005268void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005269{
5270 ftrace_disabled = 1;
5271 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005272 clear_ftrace_function();
5273}
5274
5275/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005276 * Test if ftrace is dead or not.
5277 */
5278int ftrace_is_dead(void)
5279{
5280 return ftrace_disabled;
5281}
5282
5283/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005284 * register_ftrace_function - register a function for profiling
5285 * @ops - ops structure that holds the function for profiling.
5286 *
5287 * Register a function to be called by all functions in the
5288 * kernel.
5289 *
5290 * Note: @ops->func and all the functions it calls must be labeled
5291 * with "notrace", otherwise it will go into a
5292 * recursive loop.
5293 */
5294int register_ftrace_function(struct ftrace_ops *ops)
5295{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005296 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005297
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005298 ftrace_ops_init(ops);
5299
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005300 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005301
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005302 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005303
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005304 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005305
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005306 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005307}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005308EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005309
5310/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005311 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005312 * @ops - ops structure that holds the function to unregister
5313 *
5314 * Unregister a function that was added to be called by ftrace profiling.
5315 */
5316int unregister_ftrace_function(struct ftrace_ops *ops)
5317{
5318 int ret;
5319
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005320 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005321 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005322 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005323
5324 return ret;
5325}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005326EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005327
Ingo Molnare309b412008-05-12 21:20:51 +02005328int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005329ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005330 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005331 loff_t *ppos)
5332{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005333 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005334
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005335 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005336
Steven Rostedt45a4a232011-04-21 23:16:46 -04005337 if (unlikely(ftrace_disabled))
5338 goto out;
5339
5340 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005341
Li Zefana32c7762009-06-26 16:55:51 +08005342 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005343 goto out;
5344
Li Zefana32c7762009-06-26 16:55:51 +08005345 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005346
5347 if (ftrace_enabled) {
5348
5349 ftrace_startup_sysctl();
5350
5351 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005352 if (ftrace_ops_list != &ftrace_list_end)
5353 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005354
5355 } else {
5356 /* stopping ftrace calls (just send to ftrace_stub) */
5357 ftrace_trace_function = ftrace_stub;
5358
5359 ftrace_shutdown_sysctl();
5360 }
5361
5362 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005363 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005364 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005365}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005366
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005367#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005368
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005369static struct ftrace_ops graph_ops = {
5370 .func = ftrace_stub,
5371 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5372 FTRACE_OPS_FL_INITIALIZED |
5373 FTRACE_OPS_FL_STUB,
5374#ifdef FTRACE_GRAPH_TRAMP_ADDR
5375 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
5376#endif
5377 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5378};
5379
Steven Rostedt597af812009-04-03 15:24:12 -04005380static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005381
Steven Rostedte49dc192008-12-02 23:50:05 -05005382int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5383{
5384 return 0;
5385}
5386
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005387/* The callbacks that hook a function */
5388trace_func_graph_ret_t ftrace_graph_return =
5389 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005390trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005391static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005392
5393/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5394static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5395{
5396 int i;
5397 int ret = 0;
5398 unsigned long flags;
5399 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5400 struct task_struct *g, *t;
5401
5402 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5403 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5404 * sizeof(struct ftrace_ret_stack),
5405 GFP_KERNEL);
5406 if (!ret_stack_list[i]) {
5407 start = 0;
5408 end = i;
5409 ret = -ENOMEM;
5410 goto free;
5411 }
5412 }
5413
5414 read_lock_irqsave(&tasklist_lock, flags);
5415 do_each_thread(g, t) {
5416 if (start == end) {
5417 ret = -EAGAIN;
5418 goto unlock;
5419 }
5420
5421 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005422 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005423 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005424 t->curr_ret_stack = -1;
5425 /* Make sure the tasks see the -1 first: */
5426 smp_wmb();
5427 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005428 }
5429 } while_each_thread(g, t);
5430
5431unlock:
5432 read_unlock_irqrestore(&tasklist_lock, flags);
5433free:
5434 for (i = start; i < end; i++)
5435 kfree(ret_stack_list[i]);
5436 return ret;
5437}
5438
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005439static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005440ftrace_graph_probe_sched_switch(void *ignore,
5441 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005442{
5443 unsigned long long timestamp;
5444 int index;
5445
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005446 /*
5447 * Does the user want to count the time a function was asleep.
5448 * If so, do not update the time stamps.
5449 */
5450 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5451 return;
5452
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005453 timestamp = trace_clock_local();
5454
5455 prev->ftrace_timestamp = timestamp;
5456
5457 /* only process tasks that we timestamped */
5458 if (!next->ftrace_timestamp)
5459 return;
5460
5461 /*
5462 * Update all the counters in next to make up for the
5463 * time next was sleeping.
5464 */
5465 timestamp -= next->ftrace_timestamp;
5466
5467 for (index = next->curr_ret_stack; index >= 0; index--)
5468 next->ret_stack[index].calltime += timestamp;
5469}
5470
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005471/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005472static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005473{
5474 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005475 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005476
5477 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5478 sizeof(struct ftrace_ret_stack *),
5479 GFP_KERNEL);
5480
5481 if (!ret_stack_list)
5482 return -ENOMEM;
5483
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005484 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005485 for_each_online_cpu(cpu) {
5486 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005487 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005488 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005489
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005490 do {
5491 ret = alloc_retstack_tasklist(ret_stack_list);
5492 } while (ret == -EAGAIN);
5493
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005494 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005495 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005496 if (ret)
5497 pr_info("ftrace_graph: Couldn't activate tracepoint"
5498 " probe to kernel_sched_switch\n");
5499 }
5500
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005501 kfree(ret_stack_list);
5502 return ret;
5503}
5504
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005505/*
5506 * Hibernation protection.
5507 * The state of the current task is too much unstable during
5508 * suspend/restore to disk. We want to protect against that.
5509 */
5510static int
5511ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5512 void *unused)
5513{
5514 switch (state) {
5515 case PM_HIBERNATION_PREPARE:
5516 pause_graph_tracing();
5517 break;
5518
5519 case PM_POST_HIBERNATION:
5520 unpause_graph_tracing();
5521 break;
5522 }
5523 return NOTIFY_DONE;
5524}
5525
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005526static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5527{
5528 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5529 return 0;
5530 return __ftrace_graph_entry(trace);
5531}
5532
5533/*
5534 * The function graph tracer should only trace the functions defined
5535 * by set_ftrace_filter and set_ftrace_notrace. If another function
5536 * tracer ops is registered, the graph tracer requires testing the
5537 * function against the global ops, and not just trace any function
5538 * that any ftrace_ops registered.
5539 */
5540static void update_function_graph_func(void)
5541{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005542 struct ftrace_ops *op;
5543 bool do_test = false;
5544
5545 /*
5546 * The graph and global ops share the same set of functions
5547 * to test. If any other ops is on the list, then
5548 * the graph tracing needs to test if its the function
5549 * it should call.
5550 */
5551 do_for_each_ftrace_op(op, ftrace_ops_list) {
5552 if (op != &global_ops && op != &graph_ops &&
5553 op != &ftrace_list_end) {
5554 do_test = true;
5555 /* in double loop, break out with goto */
5556 goto out;
5557 }
5558 } while_for_each_ftrace_op(op);
5559 out:
5560 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005561 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005562 else
5563 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005564}
5565
Mathias Krause8275f692014-03-30 15:31:50 +02005566static struct notifier_block ftrace_suspend_notifier = {
5567 .notifier_call = ftrace_suspend_notifier_call,
5568};
5569
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005570int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5571 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005572{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005573 int ret = 0;
5574
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005575 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005576
Steven Rostedt05ce5812009-03-24 00:18:31 -04005577 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005578 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005579 ret = -EBUSY;
5580 goto out;
5581 }
5582
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005583 register_pm_notifier(&ftrace_suspend_notifier);
5584
Steven Rostedt597af812009-04-03 15:24:12 -04005585 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005586 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005587 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005588 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005589 goto out;
5590 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005591
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005592 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005593
5594 /*
5595 * Update the indirect function to the entryfunc, and the
5596 * function that gets called to the entry_test first. Then
5597 * call the update fgraph entry function to determine if
5598 * the entryfunc should be called directly or not.
5599 */
5600 __ftrace_graph_entry = entryfunc;
5601 ftrace_graph_entry = ftrace_graph_entry_test;
5602 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005603
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005604 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005605out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005606 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005607 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005608}
5609
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005610void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005611{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005612 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005613
Steven Rostedt597af812009-04-03 15:24:12 -04005614 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005615 goto out;
5616
Steven Rostedt597af812009-04-03 15:24:12 -04005617 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005618 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005619 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005620 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005621 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005622 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005623 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005624
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005625#ifdef CONFIG_DYNAMIC_FTRACE
5626 /*
5627 * Function graph does not allocate the trampoline, but
5628 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5629 * if one was used.
5630 */
5631 global_ops.trampoline = save_global_trampoline;
5632 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5633 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5634#endif
5635
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005636 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005637 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005638}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005639
Steven Rostedt868baf02011-02-10 21:26:13 -05005640static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5641
5642static void
5643graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5644{
5645 atomic_set(&t->tracing_graph_pause, 0);
5646 atomic_set(&t->trace_overrun, 0);
5647 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005648 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005649 smp_wmb();
5650 t->ret_stack = ret_stack;
5651}
5652
5653/*
5654 * Allocate a return stack for the idle task. May be the first
5655 * time through, or it may be done by CPU hotplug online.
5656 */
5657void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5658{
5659 t->curr_ret_stack = -1;
5660 /*
5661 * The idle task has no parent, it either has its own
5662 * stack or no stack at all.
5663 */
5664 if (t->ret_stack)
5665 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5666
5667 if (ftrace_graph_active) {
5668 struct ftrace_ret_stack *ret_stack;
5669
5670 ret_stack = per_cpu(idle_ret_stack, cpu);
5671 if (!ret_stack) {
5672 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5673 * sizeof(struct ftrace_ret_stack),
5674 GFP_KERNEL);
5675 if (!ret_stack)
5676 return;
5677 per_cpu(idle_ret_stack, cpu) = ret_stack;
5678 }
5679 graph_init_task(t, ret_stack);
5680 }
5681}
5682
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005683/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005684void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005685{
Steven Rostedt84047e32009-06-02 16:51:55 -04005686 /* Make sure we do not use the parent ret_stack */
5687 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005688 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005689
Steven Rostedt597af812009-04-03 15:24:12 -04005690 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005691 struct ftrace_ret_stack *ret_stack;
5692
5693 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005694 * sizeof(struct ftrace_ret_stack),
5695 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005696 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005697 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005698 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005699 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005700}
5701
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005702void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005703{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005704 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5705
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005706 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005707 /* NULL must become visible to IRQs before we free it: */
5708 barrier();
5709
5710 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005711}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005712#endif