blob: 14947e014b787c9ab6652f8c8cdf07b41ec18cad [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 Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001120/*
1121 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001122 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001123 * not return true for either core_kernel_text() or
1124 * is_module_text_address().
1125 */
1126bool is_ftrace_trampoline(unsigned long addr)
1127{
1128 struct ftrace_ops *op;
1129 bool ret = false;
1130
1131 /*
1132 * Some of the ops may be dynamically allocated,
1133 * they are freed after a synchronize_sched().
1134 */
1135 preempt_disable_notrace();
1136
1137 do_for_each_ftrace_op(op, ftrace_ops_list) {
1138 /*
1139 * This is to check for dynamically allocated trampolines.
1140 * Trampolines that are in kernel text will have
1141 * core_kernel_text() return true.
1142 */
1143 if (op->trampoline && op->trampoline_size)
1144 if (addr >= op->trampoline &&
1145 addr < op->trampoline + op->trampoline_size) {
1146 ret = true;
1147 goto out;
1148 }
1149 } while_for_each_ftrace_op(op);
1150
1151 out:
1152 preempt_enable_notrace();
1153
1154 return ret;
1155}
1156
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001157struct ftrace_page {
1158 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001159 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001160 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001161 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001162};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001163
Steven Rostedta7900872011-12-16 16:23:44 -05001164#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1165#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001166
1167/* estimate from running different kernels */
1168#define NR_TO_INIT 10000
1169
1170static struct ftrace_page *ftrace_pages_start;
1171static struct ftrace_page *ftrace_pages;
1172
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001173static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001174{
1175 return !hash || !hash->count;
1176}
1177
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178static struct ftrace_func_entry *
1179ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1180{
1181 unsigned long key;
1182 struct ftrace_func_entry *entry;
1183 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184
Steven Rostedt06a51d92011-12-19 19:07:36 -05001185 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001186 return NULL;
1187
1188 if (hash->size_bits > 0)
1189 key = hash_long(ip, hash->size_bits);
1190 else
1191 key = 0;
1192
1193 hhd = &hash->buckets[key];
1194
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001195 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001196 if (entry->ip == ip)
1197 return entry;
1198 }
1199 return NULL;
1200}
1201
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001202static void __add_hash_entry(struct ftrace_hash *hash,
1203 struct ftrace_func_entry *entry)
1204{
1205 struct hlist_head *hhd;
1206 unsigned long key;
1207
1208 if (hash->size_bits)
1209 key = hash_long(entry->ip, hash->size_bits);
1210 else
1211 key = 0;
1212
1213 hhd = &hash->buckets[key];
1214 hlist_add_head(&entry->hlist, hhd);
1215 hash->count++;
1216}
1217
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001218static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1219{
1220 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001221
1222 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1223 if (!entry)
1224 return -ENOMEM;
1225
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001226 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001227 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001228
1229 return 0;
1230}
1231
1232static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001233free_hash_entry(struct ftrace_hash *hash,
1234 struct ftrace_func_entry *entry)
1235{
1236 hlist_del(&entry->hlist);
1237 kfree(entry);
1238 hash->count--;
1239}
1240
1241static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001242remove_hash_entry(struct ftrace_hash *hash,
1243 struct ftrace_func_entry *entry)
1244{
1245 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001246 hash->count--;
1247}
1248
1249static void ftrace_hash_clear(struct ftrace_hash *hash)
1250{
1251 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001252 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001253 struct ftrace_func_entry *entry;
1254 int size = 1 << hash->size_bits;
1255 int i;
1256
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001257 if (!hash->count)
1258 return;
1259
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001260 for (i = 0; i < size; i++) {
1261 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001262 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001263 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001264 }
1265 FTRACE_WARN_ON(hash->count);
1266}
1267
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001268static void free_ftrace_hash(struct ftrace_hash *hash)
1269{
1270 if (!hash || hash == EMPTY_HASH)
1271 return;
1272 ftrace_hash_clear(hash);
1273 kfree(hash->buckets);
1274 kfree(hash);
1275}
1276
Steven Rostedt07fd5512011-05-05 18:03:47 -04001277static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1278{
1279 struct ftrace_hash *hash;
1280
1281 hash = container_of(rcu, struct ftrace_hash, rcu);
1282 free_ftrace_hash(hash);
1283}
1284
1285static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1286{
1287 if (!hash || hash == EMPTY_HASH)
1288 return;
1289 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1290}
1291
Jiri Olsa5500fa52012-02-15 15:51:54 +01001292void ftrace_free_filter(struct ftrace_ops *ops)
1293{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001294 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001295 free_ftrace_hash(ops->func_hash->filter_hash);
1296 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001297}
1298
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001299static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1300{
1301 struct ftrace_hash *hash;
1302 int size;
1303
1304 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1305 if (!hash)
1306 return NULL;
1307
1308 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001309 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001310
1311 if (!hash->buckets) {
1312 kfree(hash);
1313 return NULL;
1314 }
1315
1316 hash->size_bits = size_bits;
1317
1318 return hash;
1319}
1320
1321static struct ftrace_hash *
1322alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1323{
1324 struct ftrace_func_entry *entry;
1325 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326 int size;
1327 int ret;
1328 int i;
1329
1330 new_hash = alloc_ftrace_hash(size_bits);
1331 if (!new_hash)
1332 return NULL;
1333
1334 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001335 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001336 return new_hash;
1337
1338 size = 1 << hash->size_bits;
1339 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001340 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001341 ret = add_hash_entry(new_hash, entry->ip);
1342 if (ret < 0)
1343 goto free_hash;
1344 }
1345 }
1346
1347 FTRACE_WARN_ON(new_hash->count != hash->count);
1348
1349 return new_hash;
1350
1351 free_hash:
1352 free_ftrace_hash(new_hash);
1353 return NULL;
1354}
1355
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001356static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001357ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001358static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001359ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001360
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001361static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1362 struct ftrace_hash *new_hash);
1363
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001364static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001365ftrace_hash_move(struct ftrace_ops *ops, int enable,
1366 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001367{
1368 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001369 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001370 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001371 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001372 int size = src->count;
1373 int bits = 0;
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001374 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001375 int i;
1376
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001377 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1378 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1379 return -EINVAL;
1380
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001381 /*
1382 * If the new source is empty, just free dst and assign it
1383 * the empty_hash.
1384 */
1385 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001386 new_hash = EMPTY_HASH;
1387 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001388 }
1389
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001390 /*
1391 * Make the hash size about 1/2 the # found
1392 */
1393 for (size /= 2; size; size >>= 1)
1394 bits++;
1395
1396 /* Don't allocate too much */
1397 if (bits > FTRACE_HASH_MAX_BITS)
1398 bits = FTRACE_HASH_MAX_BITS;
1399
Steven Rostedt07fd5512011-05-05 18:03:47 -04001400 new_hash = alloc_ftrace_hash(bits);
1401 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001402 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001403
1404 size = 1 << src->size_bits;
1405 for (i = 0; i < size; i++) {
1406 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001407 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001408 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001409 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001410 }
1411 }
1412
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001413update:
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001414 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1415 if (enable) {
1416 /* IPMODIFY should be updated only when filter_hash updating */
1417 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1418 if (ret < 0) {
1419 free_ftrace_hash(new_hash);
1420 return ret;
1421 }
1422 }
1423
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001424 /*
1425 * Remove the current set, update the hash and add
1426 * them back.
1427 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001428 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001429
Steven Rostedt07fd5512011-05-05 18:03:47 -04001430 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001431
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001432 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001433
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001434 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001435}
1436
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001437static bool hash_contains_ip(unsigned long ip,
1438 struct ftrace_ops_hash *hash)
1439{
1440 /*
1441 * The function record is a match if it exists in the filter
1442 * hash and not in the notrace hash. Note, an emty hash is
1443 * considered a match for the filter hash, but an empty
1444 * notrace hash is considered not in the notrace hash.
1445 */
1446 return (ftrace_hash_empty(hash->filter_hash) ||
1447 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1448 (ftrace_hash_empty(hash->notrace_hash) ||
1449 !ftrace_lookup_ip(hash->notrace_hash, ip));
1450}
1451
Steven Rostedt265c8312009-02-13 12:43:56 -05001452/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001453 * Test the hashes for this ops to see if we want to call
1454 * the ops->func or not.
1455 *
1456 * It's a match if the ip is in the ops->filter_hash or
1457 * the filter_hash does not exist or is empty,
1458 * AND
1459 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001460 *
1461 * This needs to be called with preemption disabled as
1462 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001463 */
1464static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001465ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001466{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001467 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001468 int ret;
1469
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001470#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1471 /*
1472 * There's a small race when adding ops that the ftrace handler
1473 * that wants regs, may be called without them. We can not
1474 * allow that handler to be called if regs is NULL.
1475 */
1476 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1477 return 0;
1478#endif
1479
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001480 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1481 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001482
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001483 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001484 ret = 1;
1485 else
1486 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001487
1488 return ret;
1489}
1490
1491/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001492 * This is a double for. Do not use 'break' to break out of the loop,
1493 * you must use a goto.
1494 */
1495#define do_for_each_ftrace_rec(pg, rec) \
1496 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1497 int _____i; \
1498 for (_____i = 0; _____i < pg->index; _____i++) { \
1499 rec = &pg->records[_____i];
1500
1501#define while_for_each_ftrace_rec() \
1502 } \
1503 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301504
Steven Rostedt5855fea2011-12-16 19:27:42 -05001505
1506static int ftrace_cmp_recs(const void *a, const void *b)
1507{
Steven Rostedta650e022012-04-25 13:48:13 -04001508 const struct dyn_ftrace *key = a;
1509 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001510
Steven Rostedta650e022012-04-25 13:48:13 -04001511 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001512 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001513 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1514 return 1;
1515 return 0;
1516}
1517
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001518static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001519{
1520 struct ftrace_page *pg;
1521 struct dyn_ftrace *rec;
1522 struct dyn_ftrace key;
1523
1524 key.ip = start;
1525 key.flags = end; /* overload flags, as it is unsigned long */
1526
1527 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1528 if (end < pg->records[0].ip ||
1529 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1530 continue;
1531 rec = bsearch(&key, pg->records, pg->index,
1532 sizeof(struct dyn_ftrace),
1533 ftrace_cmp_recs);
1534 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001535 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001536 }
1537
Steven Rostedt5855fea2011-12-16 19:27:42 -05001538 return 0;
1539}
1540
Steven Rostedtc88fd862011-08-16 09:53:39 -04001541/**
1542 * ftrace_location - return true if the ip giving is a traced location
1543 * @ip: the instruction pointer to check
1544 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001545 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001546 * That is, the instruction that is either a NOP or call to
1547 * the function tracer. It checks the ftrace internal tables to
1548 * determine if the address belongs or not.
1549 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001550unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001551{
Steven Rostedta650e022012-04-25 13:48:13 -04001552 return ftrace_location_range(ip, ip);
1553}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001554
Steven Rostedta650e022012-04-25 13:48:13 -04001555/**
1556 * ftrace_text_reserved - return true if range contains an ftrace location
1557 * @start: start of range to search
1558 * @end: end of range to search (inclusive). @end points to the last byte to check.
1559 *
1560 * Returns 1 if @start and @end contains a ftrace location.
1561 * That is, the instruction that is either a NOP or call to
1562 * the function tracer. It checks the ftrace internal tables to
1563 * determine if the address belongs or not.
1564 */
Sasha Levind88471c2013-01-09 18:09:20 -05001565int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001566{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001567 unsigned long ret;
1568
1569 ret = ftrace_location_range((unsigned long)start,
1570 (unsigned long)end);
1571
1572 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001573}
1574
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001575/* Test if ops registered to this rec needs regs */
1576static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1577{
1578 struct ftrace_ops *ops;
1579 bool keep_regs = false;
1580
1581 for (ops = ftrace_ops_list;
1582 ops != &ftrace_list_end; ops = ops->next) {
1583 /* pass rec in as regs to have non-NULL val */
1584 if (ftrace_ops_test(ops, rec->ip, rec)) {
1585 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1586 keep_regs = true;
1587 break;
1588 }
1589 }
1590 }
1591
1592 return keep_regs;
1593}
1594
Steven Rostedted926f92011-05-03 13:25:24 -04001595static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1596 int filter_hash,
1597 bool inc)
1598{
1599 struct ftrace_hash *hash;
1600 struct ftrace_hash *other_hash;
1601 struct ftrace_page *pg;
1602 struct dyn_ftrace *rec;
1603 int count = 0;
1604 int all = 0;
1605
1606 /* Only update if the ops has been registered */
1607 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1608 return;
1609
1610 /*
1611 * In the filter_hash case:
1612 * If the count is zero, we update all records.
1613 * Otherwise we just update the items in the hash.
1614 *
1615 * In the notrace_hash case:
1616 * We enable the update in the hash.
1617 * As disabling notrace means enabling the tracing,
1618 * and enabling notrace means disabling, the inc variable
1619 * gets inversed.
1620 */
1621 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001622 hash = ops->func_hash->filter_hash;
1623 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001624 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001625 all = 1;
1626 } else {
1627 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001628 hash = ops->func_hash->notrace_hash;
1629 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001630 /*
1631 * If the notrace hash has no items,
1632 * then there's nothing to do.
1633 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001634 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001635 return;
1636 }
1637
1638 do_for_each_ftrace_rec(pg, rec) {
1639 int in_other_hash = 0;
1640 int in_hash = 0;
1641 int match = 0;
1642
1643 if (all) {
1644 /*
1645 * Only the filter_hash affects all records.
1646 * Update if the record is not in the notrace hash.
1647 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001648 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001649 match = 1;
1650 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001651 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1652 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001653
1654 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001655 * If filter_hash is set, we want to match all functions
1656 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001657 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001658 * If filter_hash is not set, then we are decrementing.
1659 * That means we match anything that is in the hash
1660 * and also in the other_hash. That is, we need to turn
1661 * off functions in the other hash because they are disabled
1662 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001663 */
1664 if (filter_hash && in_hash && !in_other_hash)
1665 match = 1;
1666 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001667 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001668 match = 1;
1669 }
1670 if (!match)
1671 continue;
1672
1673 if (inc) {
1674 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001675 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001676 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001677
1678 /*
1679 * If there's only a single callback registered to a
1680 * function, and the ops has a trampoline registered
1681 * for it, then we can call it directly.
1682 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001683 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001684 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001685 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001686 /*
1687 * If we are adding another function callback
1688 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001689 * custom trampoline in use, then we need to go
1690 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001691 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001692 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001693
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001694 /*
1695 * If any ops wants regs saved for this function
1696 * then all ops will get saved regs.
1697 */
1698 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1699 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001700 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001701 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001702 return;
1703 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001704
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001705 /*
1706 * If the rec had REGS enabled and the ops that is
1707 * being removed had REGS set, then see if there is
1708 * still any ops for this record that wants regs.
1709 * If not, we can stop recording them.
1710 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001711 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001712 rec->flags & FTRACE_FL_REGS &&
1713 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1714 if (!test_rec_ops_needs_regs(rec))
1715 rec->flags &= ~FTRACE_FL_REGS;
1716 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001717
1718 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001719 * If the rec had TRAMP enabled, then it needs to
1720 * be cleared. As TRAMP can only be enabled iff
1721 * there is only a single ops attached to it.
1722 * In otherwords, always disable it on decrementing.
1723 * In the future, we may set it if rec count is
1724 * decremented to one, and the ops that is left
1725 * has a trampoline.
1726 */
1727 rec->flags &= ~FTRACE_FL_TRAMP;
1728
1729 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001730 * flags will be cleared in ftrace_check_record()
1731 * if rec count is zero.
1732 */
Steven Rostedted926f92011-05-03 13:25:24 -04001733 }
1734 count++;
1735 /* Shortcut, if we handled all records, we are done. */
1736 if (!all && count == hash->count)
1737 return;
1738 } while_for_each_ftrace_rec();
1739}
1740
1741static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1742 int filter_hash)
1743{
1744 __ftrace_hash_rec_update(ops, filter_hash, 0);
1745}
1746
1747static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1748 int filter_hash)
1749{
1750 __ftrace_hash_rec_update(ops, filter_hash, 1);
1751}
1752
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001753static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1754 int filter_hash, int inc)
1755{
1756 struct ftrace_ops *op;
1757
1758 __ftrace_hash_rec_update(ops, filter_hash, inc);
1759
1760 if (ops->func_hash != &global_ops.local_hash)
1761 return;
1762
1763 /*
1764 * If the ops shares the global_ops hash, then we need to update
1765 * all ops that are enabled and use this hash.
1766 */
1767 do_for_each_ftrace_op(op, ftrace_ops_list) {
1768 /* Already done */
1769 if (op == ops)
1770 continue;
1771 if (op->func_hash == &global_ops.local_hash)
1772 __ftrace_hash_rec_update(op, filter_hash, inc);
1773 } while_for_each_ftrace_op(op);
1774}
1775
1776static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1777 int filter_hash)
1778{
1779 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1780}
1781
1782static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1783 int filter_hash)
1784{
1785 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1786}
1787
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001788/*
1789 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1790 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1791 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1792 * Note that old_hash and new_hash has below meanings
1793 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1794 * - If the hash is EMPTY_HASH, it hits nothing
1795 * - Anything else hits the recs which match the hash entries.
1796 */
1797static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1798 struct ftrace_hash *old_hash,
1799 struct ftrace_hash *new_hash)
1800{
1801 struct ftrace_page *pg;
1802 struct dyn_ftrace *rec, *end = NULL;
1803 int in_old, in_new;
1804
1805 /* Only update if the ops has been registered */
1806 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1807 return 0;
1808
1809 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1810 return 0;
1811
1812 /*
1813 * Since the IPMODIFY is a very address sensitive action, we do not
1814 * allow ftrace_ops to set all functions to new hash.
1815 */
1816 if (!new_hash || !old_hash)
1817 return -EINVAL;
1818
1819 /* Update rec->flags */
1820 do_for_each_ftrace_rec(pg, rec) {
1821 /* We need to update only differences of filter_hash */
1822 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1823 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1824 if (in_old == in_new)
1825 continue;
1826
1827 if (in_new) {
1828 /* New entries must ensure no others are using it */
1829 if (rec->flags & FTRACE_FL_IPMODIFY)
1830 goto rollback;
1831 rec->flags |= FTRACE_FL_IPMODIFY;
1832 } else /* Removed entry */
1833 rec->flags &= ~FTRACE_FL_IPMODIFY;
1834 } while_for_each_ftrace_rec();
1835
1836 return 0;
1837
1838rollback:
1839 end = rec;
1840
1841 /* Roll back what we did above */
1842 do_for_each_ftrace_rec(pg, rec) {
1843 if (rec == end)
1844 goto err_out;
1845
1846 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1847 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1848 if (in_old == in_new)
1849 continue;
1850
1851 if (in_new)
1852 rec->flags &= ~FTRACE_FL_IPMODIFY;
1853 else
1854 rec->flags |= FTRACE_FL_IPMODIFY;
1855 } while_for_each_ftrace_rec();
1856
1857err_out:
1858 return -EBUSY;
1859}
1860
1861static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1862{
1863 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1864
1865 if (ftrace_hash_empty(hash))
1866 hash = NULL;
1867
1868 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1869}
1870
1871/* Disabling always succeeds */
1872static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1873{
1874 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1875
1876 if (ftrace_hash_empty(hash))
1877 hash = NULL;
1878
1879 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1880}
1881
1882static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1883 struct ftrace_hash *new_hash)
1884{
1885 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1886
1887 if (ftrace_hash_empty(old_hash))
1888 old_hash = NULL;
1889
1890 if (ftrace_hash_empty(new_hash))
1891 new_hash = NULL;
1892
1893 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1894}
1895
Steven Rostedt05736a42008-09-22 14:55:47 -07001896static void print_ip_ins(const char *fmt, unsigned char *p)
1897{
1898 int i;
1899
1900 printk(KERN_CONT "%s", fmt);
1901
1902 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1903 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1904}
1905
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001906static struct ftrace_ops *
1907ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1908
Steven Rostedtc88fd862011-08-16 09:53:39 -04001909/**
1910 * ftrace_bug - report and shutdown function tracer
1911 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001912 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001913 *
1914 * The arch code that enables or disables the function tracing
1915 * can call ftrace_bug() when it has detected a problem in
1916 * modifying the code. @failed should be one of either:
1917 * EFAULT - if the problem happens on reading the @ip address
1918 * EINVAL - if what is read at @ip is not what was expected
1919 * EPERM - if the problem happens on writting to the @ip address
1920 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001921void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001922{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001923 unsigned long ip = rec ? rec->ip : 0;
1924
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001925 switch (failed) {
1926 case -EFAULT:
1927 FTRACE_WARN_ON_ONCE(1);
1928 pr_info("ftrace faulted on modifying ");
1929 print_ip_sym(ip);
1930 break;
1931 case -EINVAL:
1932 FTRACE_WARN_ON_ONCE(1);
1933 pr_info("ftrace failed to modify ");
1934 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001935 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001936 pr_cont("\n");
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001937 break;
1938 case -EPERM:
1939 FTRACE_WARN_ON_ONCE(1);
1940 pr_info("ftrace faulted on writing ");
1941 print_ip_sym(ip);
1942 break;
1943 default:
1944 FTRACE_WARN_ON_ONCE(1);
1945 pr_info("ftrace faulted on unknown error ");
1946 print_ip_sym(ip);
1947 }
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001948 if (rec) {
1949 struct ftrace_ops *ops = NULL;
1950
1951 pr_info("ftrace record flags: %lx\n", rec->flags);
1952 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
1953 rec->flags & FTRACE_FL_REGS ? " R" : " ");
1954 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1955 ops = ftrace_find_tramp_ops_any(rec);
1956 if (ops)
1957 pr_cont("\ttramp: %pS",
1958 (void *)ops->trampoline);
1959 else
1960 pr_cont("\ttramp: ERROR!");
1961
1962 }
1963 ip = ftrace_get_addr_curr(rec);
1964 pr_cont(" expected tramp: %lx\n", ip);
1965 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001966}
1967
Steven Rostedtc88fd862011-08-16 09:53:39 -04001968static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001969{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001970 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001971
Steven Rostedt982c3502008-11-15 16:31:41 -05001972 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001973 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001974 *
Steven Rostedted926f92011-05-03 13:25:24 -04001975 * If the record has a ref count, then we need to enable it
1976 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001977 *
Steven Rostedted926f92011-05-03 13:25:24 -04001978 * Otherwise we make sure its disabled.
1979 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001980 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001981 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001982 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001983 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001984 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001985
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001986 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001987 * If enabling and the REGS flag does not match the REGS_EN, or
1988 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1989 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001990 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001991 if (flag) {
1992 if (!(rec->flags & FTRACE_FL_REGS) !=
1993 !(rec->flags & FTRACE_FL_REGS_EN))
1994 flag |= FTRACE_FL_REGS;
1995
1996 if (!(rec->flags & FTRACE_FL_TRAMP) !=
1997 !(rec->flags & FTRACE_FL_TRAMP_EN))
1998 flag |= FTRACE_FL_TRAMP;
1999 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002000
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002001 /* If the state of this record hasn't changed, then do nothing */
2002 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002003 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002004
2005 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002006 /* Save off if rec is being enabled (for return value) */
2007 flag ^= rec->flags & FTRACE_FL_ENABLED;
2008
2009 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002010 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002011 if (flag & FTRACE_FL_REGS) {
2012 if (rec->flags & FTRACE_FL_REGS)
2013 rec->flags |= FTRACE_FL_REGS_EN;
2014 else
2015 rec->flags &= ~FTRACE_FL_REGS_EN;
2016 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002017 if (flag & FTRACE_FL_TRAMP) {
2018 if (rec->flags & FTRACE_FL_TRAMP)
2019 rec->flags |= FTRACE_FL_TRAMP_EN;
2020 else
2021 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2022 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002023 }
2024
2025 /*
2026 * If this record is being updated from a nop, then
2027 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002028 * Otherwise,
2029 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002030 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002031 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002032 */
2033 if (flag & FTRACE_FL_ENABLED)
2034 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002035
2036 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002037 }
2038
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002039 if (update) {
2040 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002041 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002042 rec->flags = 0;
2043 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002044 /*
2045 * Just disable the record, but keep the ops TRAMP
2046 * and REGS states. The _EN flags must be disabled though.
2047 */
2048 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2049 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002050 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002051
2052 return FTRACE_UPDATE_MAKE_NOP;
2053}
2054
2055/**
2056 * ftrace_update_record, set a record that now is tracing or not
2057 * @rec: the record to update
2058 * @enable: set to 1 if the record is tracing, zero to force disable
2059 *
2060 * The records that represent all functions that can be traced need
2061 * to be updated when tracing has been enabled.
2062 */
2063int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2064{
2065 return ftrace_check_record(rec, enable, 1);
2066}
2067
2068/**
2069 * ftrace_test_record, check if the record has been enabled or not
2070 * @rec: the record to test
2071 * @enable: set to 1 to check if enabled, 0 if it is disabled
2072 *
2073 * The arch code may need to test if a record is already set to
2074 * tracing to determine how to modify the function code that it
2075 * represents.
2076 */
2077int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2078{
2079 return ftrace_check_record(rec, enable, 0);
2080}
2081
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002082static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002083ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2084{
2085 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002086 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002087
2088 do_for_each_ftrace_op(op, ftrace_ops_list) {
2089
2090 if (!op->trampoline)
2091 continue;
2092
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002093 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002094 return op;
2095 } while_for_each_ftrace_op(op);
2096
2097 return NULL;
2098}
2099
2100static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002101ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2102{
2103 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002104 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002105
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002106 /*
2107 * Need to check removed ops first.
2108 * If they are being removed, and this rec has a tramp,
2109 * and this rec is in the ops list, then it would be the
2110 * one with the tramp.
2111 */
2112 if (removed_ops) {
2113 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002114 return removed_ops;
2115 }
2116
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002117 /*
2118 * Need to find the current trampoline for a rec.
2119 * Now, a trampoline is only attached to a rec if there
2120 * was a single 'ops' attached to it. But this can be called
2121 * when we are adding another op to the rec or removing the
2122 * current one. Thus, if the op is being added, we can
2123 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002124 * yet.
2125 *
2126 * If an ops is being modified (hooking to different functions)
2127 * then we don't care about the new functions that are being
2128 * added, just the old ones (that are probably being removed).
2129 *
2130 * If we are adding an ops to a function that already is using
2131 * a trampoline, it needs to be removed (trampolines are only
2132 * for single ops connected), then an ops that is not being
2133 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002134 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002135 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002136
2137 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002138 continue;
2139
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002140 /*
2141 * If the ops is being added, it hasn't gotten to
2142 * the point to be removed from this tree yet.
2143 */
2144 if (op->flags & FTRACE_OPS_FL_ADDING)
2145 continue;
2146
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002147
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002148 /*
2149 * If the ops is being modified and is in the old
2150 * hash, then it is probably being removed from this
2151 * function.
2152 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002153 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2154 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002155 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002156 /*
2157 * If the ops is not being added or modified, and it's
2158 * in its normal filter hash, then this must be the one
2159 * we want!
2160 */
2161 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2162 hash_contains_ip(ip, op->func_hash))
2163 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002164
2165 } while_for_each_ftrace_op(op);
2166
2167 return NULL;
2168}
2169
2170static struct ftrace_ops *
2171ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2172{
2173 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002174 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002175
2176 do_for_each_ftrace_op(op, ftrace_ops_list) {
2177 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002178 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002179 return op;
2180 } while_for_each_ftrace_op(op);
2181
2182 return NULL;
2183}
2184
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002185/**
2186 * ftrace_get_addr_new - Get the call address to set to
2187 * @rec: The ftrace record descriptor
2188 *
2189 * If the record has the FTRACE_FL_REGS set, that means that it
2190 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2191 * is not not set, then it wants to convert to the normal callback.
2192 *
2193 * Returns the address of the trampoline to set to
2194 */
2195unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2196{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002197 struct ftrace_ops *ops;
2198
2199 /* Trampolines take precedence over regs */
2200 if (rec->flags & FTRACE_FL_TRAMP) {
2201 ops = ftrace_find_tramp_ops_new(rec);
2202 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002203 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2204 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002205 /* Ftrace is shutting down, return anything */
2206 return (unsigned long)FTRACE_ADDR;
2207 }
2208 return ops->trampoline;
2209 }
2210
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002211 if (rec->flags & FTRACE_FL_REGS)
2212 return (unsigned long)FTRACE_REGS_ADDR;
2213 else
2214 return (unsigned long)FTRACE_ADDR;
2215}
2216
2217/**
2218 * ftrace_get_addr_curr - Get the call address that is already there
2219 * @rec: The ftrace record descriptor
2220 *
2221 * The FTRACE_FL_REGS_EN is set when the record already points to
2222 * a function that saves all the regs. Basically the '_EN' version
2223 * represents the current state of the function.
2224 *
2225 * Returns the address of the trampoline that is currently being called
2226 */
2227unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2228{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002229 struct ftrace_ops *ops;
2230
2231 /* Trampolines take precedence over regs */
2232 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2233 ops = ftrace_find_tramp_ops_curr(rec);
2234 if (FTRACE_WARN_ON(!ops)) {
2235 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2236 (void *)rec->ip, (void *)rec->ip);
2237 /* Ftrace is shutting down, return anything */
2238 return (unsigned long)FTRACE_ADDR;
2239 }
2240 return ops->trampoline;
2241 }
2242
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002243 if (rec->flags & FTRACE_FL_REGS_EN)
2244 return (unsigned long)FTRACE_REGS_ADDR;
2245 else
2246 return (unsigned long)FTRACE_ADDR;
2247}
2248
Steven Rostedtc88fd862011-08-16 09:53:39 -04002249static int
2250__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2251{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002252 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002253 unsigned long ftrace_addr;
2254 int ret;
2255
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002256 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002257
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002258 /* This needs to be done before we call ftrace_update_record */
2259 ftrace_old_addr = ftrace_get_addr_curr(rec);
2260
2261 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002262
Steven Rostedtc88fd862011-08-16 09:53:39 -04002263 switch (ret) {
2264 case FTRACE_UPDATE_IGNORE:
2265 return 0;
2266
2267 case FTRACE_UPDATE_MAKE_CALL:
2268 return ftrace_make_call(rec, ftrace_addr);
2269
2270 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002271 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002272
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002273 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002274 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002275 }
2276
2277 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002278}
2279
Steven Rostedte4f5d542012-04-27 09:13:18 -04002280void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002281{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002282 struct dyn_ftrace *rec;
2283 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002284 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002285
Steven Rostedt45a4a232011-04-21 23:16:46 -04002286 if (unlikely(ftrace_disabled))
2287 return;
2288
Steven Rostedt265c8312009-02-13 12:43:56 -05002289 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002290 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002291 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002292 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002293 /* Stop processing */
2294 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002295 }
2296 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002297}
2298
Steven Rostedtc88fd862011-08-16 09:53:39 -04002299struct ftrace_rec_iter {
2300 struct ftrace_page *pg;
2301 int index;
2302};
2303
2304/**
2305 * ftrace_rec_iter_start, start up iterating over traced functions
2306 *
2307 * Returns an iterator handle that is used to iterate over all
2308 * the records that represent address locations where functions
2309 * are traced.
2310 *
2311 * May return NULL if no records are available.
2312 */
2313struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2314{
2315 /*
2316 * We only use a single iterator.
2317 * Protected by the ftrace_lock mutex.
2318 */
2319 static struct ftrace_rec_iter ftrace_rec_iter;
2320 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2321
2322 iter->pg = ftrace_pages_start;
2323 iter->index = 0;
2324
2325 /* Could have empty pages */
2326 while (iter->pg && !iter->pg->index)
2327 iter->pg = iter->pg->next;
2328
2329 if (!iter->pg)
2330 return NULL;
2331
2332 return iter;
2333}
2334
2335/**
2336 * ftrace_rec_iter_next, get the next record to process.
2337 * @iter: The handle to the iterator.
2338 *
2339 * Returns the next iterator after the given iterator @iter.
2340 */
2341struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2342{
2343 iter->index++;
2344
2345 if (iter->index >= iter->pg->index) {
2346 iter->pg = iter->pg->next;
2347 iter->index = 0;
2348
2349 /* Could have empty pages */
2350 while (iter->pg && !iter->pg->index)
2351 iter->pg = iter->pg->next;
2352 }
2353
2354 if (!iter->pg)
2355 return NULL;
2356
2357 return iter;
2358}
2359
2360/**
2361 * ftrace_rec_iter_record, get the record at the iterator location
2362 * @iter: The current iterator location
2363 *
2364 * Returns the record that the current @iter is at.
2365 */
2366struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2367{
2368 return &iter->pg->records[iter->index];
2369}
2370
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302371static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002372ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002373{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002374 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002375
Steven Rostedt45a4a232011-04-21 23:16:46 -04002376 if (unlikely(ftrace_disabled))
2377 return 0;
2378
Shaohua Li25aac9d2009-01-09 11:29:40 +08002379 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002380 if (ret) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002381 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302382 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002383 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302384 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002385}
2386
Steven Rostedt000ab692009-02-17 13:35:06 -05002387/*
2388 * archs can override this function if they must do something
2389 * before the modifying code is performed.
2390 */
2391int __weak ftrace_arch_code_modify_prepare(void)
2392{
2393 return 0;
2394}
2395
2396/*
2397 * archs can override this function if they must do something
2398 * after the modifying code is performed.
2399 */
2400int __weak ftrace_arch_code_modify_post_process(void)
2401{
2402 return 0;
2403}
2404
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002405void ftrace_modify_all_code(int command)
2406{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002407 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002408 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002409
2410 /*
2411 * If the ftrace_caller calls a ftrace_ops func directly,
2412 * we need to make sure that it only traces functions it
2413 * expects to trace. When doing the switch of functions,
2414 * we need to update to the ftrace_ops_list_func first
2415 * before the transition between old and new calls are set,
2416 * as the ftrace_ops_list_func will check the ops hashes
2417 * to make sure the ops are having the right functions
2418 * traced.
2419 */
Petr Mladekcd210672014-02-24 17:12:21 +01002420 if (update) {
2421 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2422 if (FTRACE_WARN_ON(err))
2423 return;
2424 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002425
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002426 if (command & FTRACE_UPDATE_CALLS)
2427 ftrace_replace_code(1);
2428 else if (command & FTRACE_DISABLE_CALLS)
2429 ftrace_replace_code(0);
2430
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002431 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2432 function_trace_op = set_function_trace_op;
2433 smp_wmb();
2434 /* If irqs are disabled, we are in stop machine */
2435 if (!irqs_disabled())
2436 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002437 err = ftrace_update_ftrace_func(ftrace_trace_function);
2438 if (FTRACE_WARN_ON(err))
2439 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002440 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002441
2442 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002443 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002444 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002445 err = ftrace_disable_ftrace_graph_caller();
2446 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002447}
2448
Ingo Molnare309b412008-05-12 21:20:51 +02002449static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002450{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002451 int *command = data;
2452
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002453 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002454
Steven Rostedtc88fd862011-08-16 09:53:39 -04002455 return 0;
2456}
2457
2458/**
2459 * ftrace_run_stop_machine, go back to the stop machine method
2460 * @command: The command to tell ftrace what to do
2461 *
2462 * If an arch needs to fall back to the stop machine method, the
2463 * it can call this function.
2464 */
2465void ftrace_run_stop_machine(int command)
2466{
2467 stop_machine(__ftrace_modify_code, &command, NULL);
2468}
2469
2470/**
2471 * arch_ftrace_update_code, modify the code to trace or not trace
2472 * @command: The command that needs to be done
2473 *
2474 * Archs can override this function if it does not need to
2475 * run stop_machine() to modify code.
2476 */
2477void __weak arch_ftrace_update_code(int command)
2478{
2479 ftrace_run_stop_machine(command);
2480}
2481
2482static void ftrace_run_update_code(int command)
2483{
2484 int ret;
2485
2486 ret = ftrace_arch_code_modify_prepare();
2487 FTRACE_WARN_ON(ret);
2488 if (ret)
2489 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002490
2491 /*
2492 * By default we use stop_machine() to modify the code.
2493 * But archs can do what ever they want as long as it
2494 * is safe. The stop_machine() is the safest, but also
2495 * produces the most overhead.
2496 */
2497 arch_ftrace_update_code(command);
2498
Steven Rostedt000ab692009-02-17 13:35:06 -05002499 ret = ftrace_arch_code_modify_post_process();
2500 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002501}
2502
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002503static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002504 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002505{
2506 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002507 ops->old_hash.filter_hash = old_hash->filter_hash;
2508 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002509 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002510 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002511 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002512 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2513}
2514
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002515static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002516static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002517
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002518void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2519{
2520}
2521
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002522static void control_ops_free(struct ftrace_ops *ops)
2523{
2524 free_percpu(ops->disabled);
2525}
2526
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002527static void ftrace_startup_enable(int command)
2528{
2529 if (saved_ftrace_func != ftrace_trace_function) {
2530 saved_ftrace_func = ftrace_trace_function;
2531 command |= FTRACE_UPDATE_TRACE_FUNC;
2532 }
2533
2534 if (!command || !ftrace_enabled)
2535 return;
2536
2537 ftrace_run_update_code(command);
2538}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002539
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002540static void ftrace_startup_all(int command)
2541{
2542 update_all_ops = true;
2543 ftrace_startup_enable(command);
2544 update_all_ops = false;
2545}
2546
Steven Rostedta1cd6172011-05-23 15:24:25 -04002547static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002548{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002549 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002550
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002551 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002552 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002553
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002554 ret = __register_ftrace_function(ops);
2555 if (ret)
2556 return ret;
2557
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002558 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002559 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002560
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002561 /*
2562 * Note that ftrace probes uses this to start up
2563 * and modify functions it will probe. But we still
2564 * set the ADDING flag for modification, as probes
2565 * do not have trampolines. If they add them in the
2566 * future, then the probes will need to distinguish
2567 * between adding and updating probes.
2568 */
2569 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002570
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002571 ret = ftrace_hash_ipmodify_enable(ops);
2572 if (ret < 0) {
2573 /* Rollback registration process */
2574 __unregister_ftrace_function(ops);
2575 ftrace_start_up--;
2576 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2577 return ret;
2578 }
2579
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002580 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002581
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002582 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002583
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002584 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2585
Steven Rostedta1cd6172011-05-23 15:24:25 -04002586 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002587}
2588
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002589static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002590{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002591 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002592
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002593 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002594 return -ENODEV;
2595
2596 ret = __unregister_ftrace_function(ops);
2597 if (ret)
2598 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002599
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002600 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002601 /*
2602 * Just warn in case of unbalance, no need to kill ftrace, it's not
2603 * critical but the ftrace_call callers may be never nopped again after
2604 * further ftrace uses.
2605 */
2606 WARN_ON_ONCE(ftrace_start_up < 0);
2607
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002608 /* Disabling ipmodify never fails */
2609 ftrace_hash_ipmodify_disable(ops);
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002610 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002611
Namhyung Kima737e6d2014-06-12 23:56:12 +09002612 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002613
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002614 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002615
2616 if (saved_ftrace_func != ftrace_trace_function) {
2617 saved_ftrace_func = ftrace_trace_function;
2618 command |= FTRACE_UPDATE_TRACE_FUNC;
2619 }
2620
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002621 if (!command || !ftrace_enabled) {
2622 /*
2623 * If these are control ops, they still need their
2624 * per_cpu field freed. Since, function tracing is
2625 * not currently active, we can just free them
2626 * without synchronizing all CPUs.
2627 */
2628 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2629 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002630 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002631 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002632
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002633 /*
2634 * If the ops uses a trampoline, then it needs to be
2635 * tested first on update.
2636 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002637 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002638 removed_ops = ops;
2639
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002640 /* The trampoline logic checks the old hashes */
2641 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2642 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2643
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002644 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002645
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002646 /*
2647 * If there's no more ops registered with ftrace, run a
2648 * sanity check to make sure all rec flags are cleared.
2649 */
2650 if (ftrace_ops_list == &ftrace_list_end) {
2651 struct ftrace_page *pg;
2652 struct dyn_ftrace *rec;
2653
2654 do_for_each_ftrace_rec(pg, rec) {
2655 if (FTRACE_WARN_ON_ONCE(rec->flags))
2656 pr_warn(" %pS flags:%lx\n",
2657 (void *)rec->ip, rec->flags);
2658 } while_for_each_ftrace_rec();
2659 }
2660
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002661 ops->old_hash.filter_hash = NULL;
2662 ops->old_hash.notrace_hash = NULL;
2663
2664 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002665 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002666
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002667 /*
2668 * Dynamic ops may be freed, we must make sure that all
2669 * callers are done before leaving this function.
2670 * The same goes for freeing the per_cpu data of the control
2671 * ops.
2672 *
2673 * Again, normal synchronize_sched() is not good enough.
2674 * We need to do a hard force of sched synchronization.
2675 * This is because we use preempt_disable() to do RCU, but
2676 * the function tracers can be called where RCU is not watching
2677 * (like before user_exit()). We can not rely on the RCU
2678 * infrastructure to do the synchronization, thus we must do it
2679 * ourselves.
2680 */
2681 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2682 schedule_on_each_cpu(ftrace_sync);
2683
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002684 arch_ftrace_trampoline_free(ops);
2685
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002686 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2687 control_ops_free(ops);
2688 }
2689
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002690 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002691}
2692
Ingo Molnare309b412008-05-12 21:20:51 +02002693static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002694{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002695 if (unlikely(ftrace_disabled))
2696 return;
2697
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002698 /* Force update next time */
2699 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002700 /* ftrace_start_up is true if we want ftrace running */
2701 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002702 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002703}
2704
Ingo Molnare309b412008-05-12 21:20:51 +02002705static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002706{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002707 if (unlikely(ftrace_disabled))
2708 return;
2709
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002710 /* ftrace_start_up is true if ftrace is running */
2711 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002712 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002713}
2714
Steven Rostedt3d083392008-05-12 21:20:42 +02002715static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002716unsigned long ftrace_update_tot_cnt;
2717
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002718static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002719{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002720 /*
2721 * Filter_hash being empty will default to trace module.
2722 * But notrace hash requires a test of individual module functions.
2723 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002724 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2725 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002726}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002727
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002728/*
2729 * Check if the current ops references the record.
2730 *
2731 * If the ops traces all functions, then it was already accounted for.
2732 * If the ops does not trace the current record function, skip it.
2733 * If the ops ignores the function via notrace filter, skip it.
2734 */
2735static inline bool
2736ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2737{
2738 /* If ops isn't enabled, ignore it */
2739 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2740 return 0;
2741
2742 /* If ops traces all mods, we already accounted for it */
2743 if (ops_traces_mod(ops))
2744 return 0;
2745
2746 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002747 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2748 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002749 return 0;
2750
2751 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002752 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002753 return 0;
2754
2755 return 1;
2756}
2757
2758static int referenced_filters(struct dyn_ftrace *rec)
2759{
2760 struct ftrace_ops *ops;
2761 int cnt = 0;
2762
2763 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2764 if (ops_references_rec(ops, rec))
2765 cnt++;
2766 }
2767
2768 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002769}
2770
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002771static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002772{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002773 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002774 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302775 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002776 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002777 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002778 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002779 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002780
2781 /*
2782 * When adding a module, we need to check if tracers are
2783 * currently enabled and if they are set to trace all functions.
2784 * If they are, we need to enable the module functions as well
2785 * as update the reference counts for those function records.
2786 */
2787 if (mod) {
2788 struct ftrace_ops *ops;
2789
2790 for (ops = ftrace_ops_list;
2791 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002792 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2793 if (ops_traces_mod(ops))
2794 ref++;
2795 else
2796 test = true;
2797 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002798 }
2799 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002800
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002801 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002802
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002803 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302804
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002805 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002806 int cnt = ref;
2807
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002808 /* If something went wrong, bail without enabling anything */
2809 if (unlikely(ftrace_disabled))
2810 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002811
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002812 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002813 if (test)
2814 cnt += referenced_filters(p);
2815 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302816
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002817 /*
2818 * Do the initial record conversion from mcount jump
2819 * to the NOP instructions.
2820 */
2821 if (!ftrace_code_disable(mod, p))
2822 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002823
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002824 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002825
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002826 /*
2827 * If the tracing is enabled, go ahead and enable the record.
2828 *
2829 * The reason not to enable the record immediatelly is the
2830 * inherent check of ftrace_make_nop/ftrace_make_call for
2831 * correct previous instructions. Making first the NOP
2832 * conversion puts the module to the correct state, thus
2833 * passing the ftrace_make_call check.
2834 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002835 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002836 int failed = __ftrace_replace_code(p, 1);
2837 if (failed)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002838 ftrace_bug(failed, p);
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002839 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002840 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002841 }
2842
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002843 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002844 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002845 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002846
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002847 return 0;
2848}
2849
Steven Rostedta7900872011-12-16 16:23:44 -05002850static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002851{
Steven Rostedta7900872011-12-16 16:23:44 -05002852 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002853 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002854
Steven Rostedta7900872011-12-16 16:23:44 -05002855 if (WARN_ON(!count))
2856 return -EINVAL;
2857
2858 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002859
2860 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002861 * We want to fill as much as possible. No more than a page
2862 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002863 */
Steven Rostedta7900872011-12-16 16:23:44 -05002864 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2865 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002866
Steven Rostedta7900872011-12-16 16:23:44 -05002867 again:
2868 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2869
2870 if (!pg->records) {
2871 /* if we can't allocate this size, try something smaller */
2872 if (!order)
2873 return -ENOMEM;
2874 order >>= 1;
2875 goto again;
2876 }
2877
2878 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2879 pg->size = cnt;
2880
2881 if (cnt > count)
2882 cnt = count;
2883
2884 return cnt;
2885}
2886
2887static struct ftrace_page *
2888ftrace_allocate_pages(unsigned long num_to_init)
2889{
2890 struct ftrace_page *start_pg;
2891 struct ftrace_page *pg;
2892 int order;
2893 int cnt;
2894
2895 if (!num_to_init)
2896 return 0;
2897
2898 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2899 if (!pg)
2900 return NULL;
2901
2902 /*
2903 * Try to allocate as much as possible in one continues
2904 * location that fills in all of the space. We want to
2905 * waste as little space as possible.
2906 */
2907 for (;;) {
2908 cnt = ftrace_allocate_records(pg, num_to_init);
2909 if (cnt < 0)
2910 goto free_pages;
2911
2912 num_to_init -= cnt;
2913 if (!num_to_init)
2914 break;
2915
2916 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2917 if (!pg->next)
2918 goto free_pages;
2919
2920 pg = pg->next;
2921 }
2922
2923 return start_pg;
2924
2925 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002926 pg = start_pg;
2927 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002928 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2929 free_pages((unsigned long)pg->records, order);
2930 start_pg = pg->next;
2931 kfree(pg);
2932 pg = start_pg;
2933 }
2934 pr_info("ftrace: FAILED to allocate memory for functions\n");
2935 return NULL;
2936}
2937
Steven Rostedt5072c592008-05-12 21:20:43 +02002938#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2939
2940struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002941 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002942 loff_t func_pos;
2943 struct ftrace_page *pg;
2944 struct dyn_ftrace *func;
2945 struct ftrace_func_probe *probe;
2946 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002947 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002948 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002949 int hidx;
2950 int idx;
2951 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002952};
2953
Ingo Molnare309b412008-05-12 21:20:51 +02002954static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002955t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002956{
2957 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002958 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002959 struct hlist_head *hhd;
2960
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002961 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002962 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002963
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002964 if (iter->probe)
2965 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002966 retry:
2967 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2968 return NULL;
2969
2970 hhd = &ftrace_func_hash[iter->hidx];
2971
2972 if (hlist_empty(hhd)) {
2973 iter->hidx++;
2974 hnd = NULL;
2975 goto retry;
2976 }
2977
2978 if (!hnd)
2979 hnd = hhd->first;
2980 else {
2981 hnd = hnd->next;
2982 if (!hnd) {
2983 iter->hidx++;
2984 goto retry;
2985 }
2986 }
2987
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002988 if (WARN_ON_ONCE(!hnd))
2989 return NULL;
2990
2991 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2992
2993 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002994}
2995
2996static void *t_hash_start(struct seq_file *m, loff_t *pos)
2997{
2998 struct ftrace_iterator *iter = m->private;
2999 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003000 loff_t l;
3001
Steven Rostedt69a30832011-12-19 15:21:16 -05003002 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3003 return NULL;
3004
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003005 if (iter->func_pos > *pos)
3006 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003007
Li Zefand82d6242009-06-24 09:54:54 +08003008 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003009 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003010 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003011 if (!p)
3012 break;
3013 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003014 if (!p)
3015 return NULL;
3016
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003017 /* Only set this if we have an item */
3018 iter->flags |= FTRACE_ITER_HASH;
3019
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003020 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003021}
3022
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003023static int
3024t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003025{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003026 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003027
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003028 rec = iter->probe;
3029 if (WARN_ON_ONCE(!rec))
3030 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003031
Steven Rostedt809dcf22009-02-16 23:06:01 -05003032 if (rec->ops->print)
3033 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3034
Steven Rostedtb375a112009-09-17 00:05:58 -04003035 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003036
3037 if (rec->data)
3038 seq_printf(m, ":%p", rec->data);
3039 seq_putc(m, '\n');
3040
3041 return 0;
3042}
3043
3044static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02003045t_next(struct seq_file *m, void *v, loff_t *pos)
3046{
3047 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003048 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003049 struct dyn_ftrace *rec = NULL;
3050
Steven Rostedt45a4a232011-04-21 23:16:46 -04003051 if (unlikely(ftrace_disabled))
3052 return NULL;
3053
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003054 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003055 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003056
Steven Rostedt5072c592008-05-12 21:20:43 +02003057 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01003058 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02003059
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003060 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003061 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003062
Steven Rostedt5072c592008-05-12 21:20:43 +02003063 retry:
3064 if (iter->idx >= iter->pg->index) {
3065 if (iter->pg->next) {
3066 iter->pg = iter->pg->next;
3067 iter->idx = 0;
3068 goto retry;
3069 }
3070 } else {
3071 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05003072 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003073 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05003074
Steven Rostedt41c52c02008-05-22 11:46:33 -04003075 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003076 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003077
3078 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003079 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003080
Steven Rostedt5072c592008-05-12 21:20:43 +02003081 rec = NULL;
3082 goto retry;
3083 }
3084 }
3085
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003086 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003087 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003088
3089 iter->func = rec;
3090
3091 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003092}
3093
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003094static void reset_iter_read(struct ftrace_iterator *iter)
3095{
3096 iter->pos = 0;
3097 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03003098 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003099}
3100
3101static void *t_start(struct seq_file *m, loff_t *pos)
3102{
3103 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003104 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003105 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003106 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003107
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003108 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003109
3110 if (unlikely(ftrace_disabled))
3111 return NULL;
3112
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003113 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003114 * If an lseek was done, then reset and start from beginning.
3115 */
3116 if (*pos < iter->pos)
3117 reset_iter_read(iter);
3118
3119 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003120 * For set_ftrace_filter reading, if we have the filter
3121 * off, we can short cut and just print out that all
3122 * functions are enabled.
3123 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003124 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003125 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003126 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003127 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003128 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003129 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003130 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003131 /* reset in case of seek/pread */
3132 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003133 return iter;
3134 }
3135
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003136 if (iter->flags & FTRACE_ITER_HASH)
3137 return t_hash_start(m, pos);
3138
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003139 /*
3140 * Unfortunately, we need to restart at ftrace_pages_start
3141 * every time we let go of the ftrace_mutex. This is because
3142 * those pointers can change without the lock.
3143 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003144 iter->pg = ftrace_pages_start;
3145 iter->idx = 0;
3146 for (l = 0; l <= *pos; ) {
3147 p = t_next(m, p, &l);
3148 if (!p)
3149 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003150 }
walimis5821e1b2008-11-15 15:19:06 +08003151
Steven Rostedt69a30832011-12-19 15:21:16 -05003152 if (!p)
3153 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003154
3155 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003156}
3157
3158static void t_stop(struct seq_file *m, void *p)
3159{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003160 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003161}
3162
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003163void * __weak
3164arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3165{
3166 return NULL;
3167}
3168
3169static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3170 struct dyn_ftrace *rec)
3171{
3172 void *ptr;
3173
3174 ptr = arch_ftrace_trampoline_func(ops, rec);
3175 if (ptr)
3176 seq_printf(m, " ->%pS", ptr);
3177}
3178
Steven Rostedt5072c592008-05-12 21:20:43 +02003179static int t_show(struct seq_file *m, void *v)
3180{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003181 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003182 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003183
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003184 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003185 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003186
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003187 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003188 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003189 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003190 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003191 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003192 return 0;
3193 }
3194
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003195 rec = iter->func;
3196
Steven Rostedt5072c592008-05-12 21:20:43 +02003197 if (!rec)
3198 return 0;
3199
Steven Rostedt647bcd02011-05-03 14:39:21 -04003200 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003201 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003202 struct ftrace_ops *ops = NULL;
3203
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003204 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003205 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003206 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3207 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003208 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003209 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04003210 if (ops)
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003211 seq_printf(m, "\ttramp: %pS",
3212 (void *)ops->trampoline);
3213 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003214 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003215
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003216 }
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003217 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003218 }
3219
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003220 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003221
3222 return 0;
3223}
3224
James Morris88e9d342009-09-22 16:43:43 -07003225static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003226 .start = t_start,
3227 .next = t_next,
3228 .stop = t_stop,
3229 .show = t_show,
3230};
3231
Ingo Molnare309b412008-05-12 21:20:51 +02003232static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003233ftrace_avail_open(struct inode *inode, struct file *file)
3234{
3235 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003236
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003237 if (unlikely(ftrace_disabled))
3238 return -ENODEV;
3239
Jiri Olsa50e18b92012-04-25 10:23:39 +02003240 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3241 if (iter) {
3242 iter->pg = ftrace_pages_start;
3243 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003244 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003245
Jiri Olsa50e18b92012-04-25 10:23:39 +02003246 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003247}
3248
Steven Rostedt647bcd02011-05-03 14:39:21 -04003249static int
3250ftrace_enabled_open(struct inode *inode, struct file *file)
3251{
3252 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003253
Jiri Olsa50e18b92012-04-25 10:23:39 +02003254 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3255 if (iter) {
3256 iter->pg = ftrace_pages_start;
3257 iter->flags = FTRACE_ITER_ENABLED;
3258 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003259 }
3260
Jiri Olsa50e18b92012-04-25 10:23:39 +02003261 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003262}
3263
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003264/**
3265 * ftrace_regex_open - initialize function tracer filter files
3266 * @ops: The ftrace_ops that hold the hash filters
3267 * @flag: The type of filter to process
3268 * @inode: The inode, usually passed in to your open routine
3269 * @file: The file, usually passed in to your open routine
3270 *
3271 * ftrace_regex_open() initializes the filter files for the
3272 * @ops. Depending on @flag it may process the filter hash or
3273 * the notrace hash of @ops. With this called from the open
3274 * routine, you can use ftrace_filter_write() for the write
3275 * routine if @flag has FTRACE_ITER_FILTER set, or
3276 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003277 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003278 * release must call ftrace_regex_release().
3279 */
3280int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003281ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003282 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003283{
3284 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003285 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003286 int ret = 0;
3287
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003288 ftrace_ops_init(ops);
3289
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003290 if (unlikely(ftrace_disabled))
3291 return -ENODEV;
3292
Steven Rostedt5072c592008-05-12 21:20:43 +02003293 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3294 if (!iter)
3295 return -ENOMEM;
3296
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003297 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3298 kfree(iter);
3299 return -ENOMEM;
3300 }
3301
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003302 iter->ops = ops;
3303 iter->flags = flag;
3304
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003305 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003306
Steven Rostedtf45948e2011-05-02 12:29:25 -04003307 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003308 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003309 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003310 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003311
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003312 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003313 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3314
3315 if (file->f_flags & O_TRUNC)
3316 iter->hash = alloc_ftrace_hash(size_bits);
3317 else
3318 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3319
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003320 if (!iter->hash) {
3321 trace_parser_put(&iter->parser);
3322 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003323 ret = -ENOMEM;
3324 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003325 }
3326 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003327
Steven Rostedt5072c592008-05-12 21:20:43 +02003328 if (file->f_mode & FMODE_READ) {
3329 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003330
3331 ret = seq_open(file, &show_ftrace_seq_ops);
3332 if (!ret) {
3333 struct seq_file *m = file->private_data;
3334 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003335 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003336 /* Failed */
3337 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003338 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003339 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003340 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003341 } else
3342 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003343
3344 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003345 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003346
3347 return ret;
3348}
3349
Steven Rostedt41c52c02008-05-22 11:46:33 -04003350static int
3351ftrace_filter_open(struct inode *inode, struct file *file)
3352{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003353 struct ftrace_ops *ops = inode->i_private;
3354
3355 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003356 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3357 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003358}
3359
3360static int
3361ftrace_notrace_open(struct inode *inode, struct file *file)
3362{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003363 struct ftrace_ops *ops = inode->i_private;
3364
3365 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003366 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003367}
3368
Steven Rostedt64e7c442009-02-13 17:08:48 -05003369static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003370{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003371 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003372 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003373
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003374 switch (type) {
3375 case MATCH_FULL:
3376 if (strcmp(str, regex) == 0)
3377 matched = 1;
3378 break;
3379 case MATCH_FRONT_ONLY:
3380 if (strncmp(str, regex, len) == 0)
3381 matched = 1;
3382 break;
3383 case MATCH_MIDDLE_ONLY:
3384 if (strstr(str, regex))
3385 matched = 1;
3386 break;
3387 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003388 slen = strlen(str);
3389 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003390 matched = 1;
3391 break;
3392 }
3393
3394 return matched;
3395}
3396
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003397static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003398enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003399{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003400 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003401 int ret = 0;
3402
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003403 entry = ftrace_lookup_ip(hash, rec->ip);
3404 if (not) {
3405 /* Do nothing if it doesn't exist */
3406 if (!entry)
3407 return 0;
3408
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003409 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003410 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003411 /* Do nothing if it exists */
3412 if (entry)
3413 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003414
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003415 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003416 }
3417 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003418}
3419
Steven Rostedt64e7c442009-02-13 17:08:48 -05003420static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003421ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3422 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003423{
3424 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003425 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003426
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003427 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3428
3429 if (mod) {
3430 /* module lookup requires matching the module */
3431 if (!modname || strcmp(modname, mod))
3432 return 0;
3433
3434 /* blank search means to match all funcs in the mod */
3435 if (!len)
3436 return 1;
3437 }
3438
Steven Rostedt64e7c442009-02-13 17:08:48 -05003439 return ftrace_match(str, regex, len, type);
3440}
3441
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003442static int
3443match_records(struct ftrace_hash *hash, char *buff,
3444 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003445{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003446 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003447 struct ftrace_page *pg;
3448 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003449 int type = MATCH_FULL;
3450 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003451 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003452 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003453
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003454 if (len) {
3455 type = filter_parse_regex(buff, len, &search, &not);
3456 search_len = strlen(search);
3457 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003458
Steven Rostedt52baf112009-02-14 01:15:39 -05003459 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003460
3461 if (unlikely(ftrace_disabled))
3462 goto out_unlock;
3463
Steven Rostedt265c8312009-02-13 12:43:56 -05003464 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003465 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003466 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003467 if (ret < 0) {
3468 found = ret;
3469 goto out_unlock;
3470 }
Li Zefan311d16d2009-12-08 11:15:11 +08003471 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003472 }
3473 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003474 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003475 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003476
3477 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003478}
3479
Steven Rostedt64e7c442009-02-13 17:08:48 -05003480static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003481ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003482{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003483 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003484}
3485
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003486static int
3487ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003488{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003489 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003490
Steven Rostedt64e7c442009-02-13 17:08:48 -05003491 /* blank or '*' mean the same */
3492 if (strcmp(buff, "*") == 0)
3493 buff[0] = 0;
3494
3495 /* handle the case of 'dont filter this module' */
3496 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3497 buff[0] = 0;
3498 not = 1;
3499 }
3500
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003501 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003502}
3503
Steven Rostedtf6180772009-02-14 00:40:25 -05003504/*
3505 * We register the module command as a template to show others how
3506 * to register the a command as well.
3507 */
3508
3509static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003510ftrace_mod_callback(struct ftrace_hash *hash,
3511 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003512{
3513 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003514 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003515
3516 /*
3517 * cmd == 'mod' because we only registered this func
3518 * for the 'mod' ftrace_func_command.
3519 * But if you register one func with multiple commands,
3520 * you can tell which command was used by the cmd
3521 * parameter.
3522 */
3523
3524 /* we must have a module name */
3525 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003526 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003527
3528 mod = strsep(&param, ":");
3529 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003530 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003531
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003532 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003533 if (!ret)
3534 ret = -EINVAL;
3535 if (ret < 0)
3536 return ret;
3537
3538 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003539}
3540
3541static struct ftrace_func_command ftrace_mod_cmd = {
3542 .name = "mod",
3543 .func = ftrace_mod_callback,
3544};
3545
3546static int __init ftrace_mod_cmd_init(void)
3547{
3548 return register_ftrace_command(&ftrace_mod_cmd);
3549}
Steven Rostedt6f415672012-10-05 12:13:07 -04003550core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003551
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003552static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003553 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003554{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003555 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003556 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003557 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003558
3559 key = hash_long(ip, FTRACE_HASH_BITS);
3560
3561 hhd = &ftrace_func_hash[key];
3562
3563 if (hlist_empty(hhd))
3564 return;
3565
3566 /*
3567 * Disable preemption for these calls to prevent a RCU grace
3568 * period. This syncs the hash iteration and freeing of items
3569 * on the hash. rcu_read_lock is too dangerous here.
3570 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003571 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003572 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003573 if (entry->ip == ip)
3574 entry->ops->func(ip, parent_ip, &entry->data);
3575 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003576 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003577}
3578
Steven Rostedtb6887d72009-02-17 12:32:04 -05003579static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003580{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003581 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003582 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003583 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003584};
3585
Steven Rostedtb6887d72009-02-17 12:32:04 -05003586static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003587
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003588static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003589{
Steven Rostedtb8489142011-05-04 09:27:52 -04003590 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003591 int i;
3592
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003593 if (ftrace_probe_registered) {
3594 /* still need to update the function call sites */
3595 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003596 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3597 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003598 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003599 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003600
3601 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3602 struct hlist_head *hhd = &ftrace_func_hash[i];
3603 if (hhd->first)
3604 break;
3605 }
3606 /* Nothing registered? */
3607 if (i == FTRACE_FUNC_HASHSIZE)
3608 return;
3609
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003610 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003611
Steven Rostedtb6887d72009-02-17 12:32:04 -05003612 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003613}
3614
Steven Rostedtb6887d72009-02-17 12:32:04 -05003615static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003616{
3617 int i;
3618
Steven Rostedtb6887d72009-02-17 12:32:04 -05003619 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003620 return;
3621
3622 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3623 struct hlist_head *hhd = &ftrace_func_hash[i];
3624 if (hhd->first)
3625 return;
3626 }
3627
3628 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003629 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003630
Steven Rostedtb6887d72009-02-17 12:32:04 -05003631 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003632}
3633
3634
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003635static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003636{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003637 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003638 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003639 kfree(entry);
3640}
3641
Steven Rostedt59df055f2009-02-14 15:29:06 -05003642int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003643register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003644 void *data)
3645{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003646 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003647 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003648 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003649 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003650 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003651 struct ftrace_page *pg;
3652 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003653 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003654 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003655 int count = 0;
3656 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003657 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003658
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003659 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003660 len = strlen(search);
3661
Steven Rostedtb6887d72009-02-17 12:32:04 -05003662 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003663 if (WARN_ON(not))
3664 return -EINVAL;
3665
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003666 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003667
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003668 old_hash_ops.filter_hash = old_hash;
3669 /* Probes only have filters */
3670 old_hash_ops.notrace_hash = NULL;
3671
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003672 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003673 if (!hash) {
3674 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003675 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003676 }
3677
3678 if (unlikely(ftrace_disabled)) {
3679 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003680 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003681 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003682
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003683 mutex_lock(&ftrace_lock);
3684
Steven Rostedt59df055f2009-02-14 15:29:06 -05003685 do_for_each_ftrace_rec(pg, rec) {
3686
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003687 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003688 continue;
3689
3690 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3691 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003692 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003693 if (!count)
3694 count = -ENOMEM;
3695 goto out_unlock;
3696 }
3697
3698 count++;
3699
3700 entry->data = data;
3701
3702 /*
3703 * The caller might want to do something special
3704 * for each function we find. We call the callback
3705 * to give the caller an opportunity to do so.
3706 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003707 if (ops->init) {
3708 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003709 /* caller does not like this func */
3710 kfree(entry);
3711 continue;
3712 }
3713 }
3714
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003715 ret = enter_record(hash, rec, 0);
3716 if (ret < 0) {
3717 kfree(entry);
3718 count = ret;
3719 goto out_unlock;
3720 }
3721
Steven Rostedt59df055f2009-02-14 15:29:06 -05003722 entry->ops = ops;
3723 entry->ip = rec->ip;
3724
3725 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3726 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3727
3728 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003729
3730 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003731
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003732 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003733
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003734 if (!ret)
3735 free_ftrace_hash_rcu(old_hash);
3736 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003737 count = ret;
3738
Steven Rostedt59df055f2009-02-14 15:29:06 -05003739 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003740 mutex_unlock(&ftrace_lock);
3741 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003742 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003743 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003744
3745 return count;
3746}
3747
3748enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003749 PROBE_TEST_FUNC = 1,
3750 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003751};
3752
3753static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003754__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003755 void *data, int flags)
3756{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003757 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003758 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003759 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003760 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003761 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003762 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003763 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003764 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003765 char str[KSYM_SYMBOL_LEN];
3766 int type = MATCH_FULL;
3767 int i, len = 0;
3768 char *search;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003769 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003770
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003771 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003772 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003773 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003774 int not;
3775
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003776 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003777 len = strlen(search);
3778
Steven Rostedtb6887d72009-02-17 12:32:04 -05003779 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003780 if (WARN_ON(not))
3781 return;
3782 }
3783
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003784 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003785
3786 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3787 if (!hash)
3788 /* Hmm, should report this somehow */
3789 goto out_unlock;
3790
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003791 INIT_LIST_HEAD(&free_list);
3792
Steven Rostedt59df055f2009-02-14 15:29:06 -05003793 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3794 struct hlist_head *hhd = &ftrace_func_hash[i];
3795
Sasha Levinb67bfe02013-02-27 17:06:00 -08003796 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003797
3798 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003799 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003800 continue;
3801
Steven Rostedtb6887d72009-02-17 12:32:04 -05003802 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003803 continue;
3804
3805 /* do this last, since it is the most expensive */
3806 if (glob) {
3807 kallsyms_lookup(entry->ip, NULL, NULL,
3808 NULL, str);
3809 if (!ftrace_match(str, glob, len, type))
3810 continue;
3811 }
3812
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003813 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3814 /* It is possible more than one entry had this ip */
3815 if (rec_entry)
3816 free_hash_entry(hash, rec_entry);
3817
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003818 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003819 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003820 }
3821 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003822 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003823 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003824 /*
3825 * Remove after the disable is called. Otherwise, if the last
3826 * probe is removed, a null hash means *all enabled*.
3827 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003828 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003829 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003830 if (!ret)
3831 free_ftrace_hash_rcu(old_hash);
3832
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003833 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3834 list_del(&entry->free_list);
3835 ftrace_free_entry(entry);
3836 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003837 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003838
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003839 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003840 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003841 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003842}
3843
3844void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003845unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003846 void *data)
3847{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003848 __unregister_ftrace_function_probe(glob, ops, data,
3849 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003850}
3851
3852void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003853unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003854{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003855 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003856}
3857
Steven Rostedtb6887d72009-02-17 12:32:04 -05003858void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003859{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003860 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003861}
3862
Steven Rostedtf6180772009-02-14 00:40:25 -05003863static LIST_HEAD(ftrace_commands);
3864static DEFINE_MUTEX(ftrace_cmd_mutex);
3865
Tom Zanussi38de93a2013-10-24 08:34:18 -05003866/*
3867 * Currently we only register ftrace commands from __init, so mark this
3868 * __init too.
3869 */
3870__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003871{
3872 struct ftrace_func_command *p;
3873 int ret = 0;
3874
3875 mutex_lock(&ftrace_cmd_mutex);
3876 list_for_each_entry(p, &ftrace_commands, list) {
3877 if (strcmp(cmd->name, p->name) == 0) {
3878 ret = -EBUSY;
3879 goto out_unlock;
3880 }
3881 }
3882 list_add(&cmd->list, &ftrace_commands);
3883 out_unlock:
3884 mutex_unlock(&ftrace_cmd_mutex);
3885
3886 return ret;
3887}
3888
Tom Zanussi38de93a2013-10-24 08:34:18 -05003889/*
3890 * Currently we only unregister ftrace commands from __init, so mark
3891 * this __init too.
3892 */
3893__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003894{
3895 struct ftrace_func_command *p, *n;
3896 int ret = -ENODEV;
3897
3898 mutex_lock(&ftrace_cmd_mutex);
3899 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3900 if (strcmp(cmd->name, p->name) == 0) {
3901 ret = 0;
3902 list_del_init(&p->list);
3903 goto out_unlock;
3904 }
3905 }
3906 out_unlock:
3907 mutex_unlock(&ftrace_cmd_mutex);
3908
3909 return ret;
3910}
3911
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003912static int ftrace_process_regex(struct ftrace_hash *hash,
3913 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003914{
Steven Rostedtf6180772009-02-14 00:40:25 -05003915 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003916 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003917 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003918
3919 func = strsep(&next, ":");
3920
3921 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003922 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003923 if (!ret)
3924 ret = -EINVAL;
3925 if (ret < 0)
3926 return ret;
3927 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003928 }
3929
Steven Rostedtf6180772009-02-14 00:40:25 -05003930 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003931
3932 command = strsep(&next, ":");
3933
Steven Rostedtf6180772009-02-14 00:40:25 -05003934 mutex_lock(&ftrace_cmd_mutex);
3935 list_for_each_entry(p, &ftrace_commands, list) {
3936 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003937 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003938 goto out_unlock;
3939 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003940 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003941 out_unlock:
3942 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003943
Steven Rostedtf6180772009-02-14 00:40:25 -05003944 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003945}
3946
Ingo Molnare309b412008-05-12 21:20:51 +02003947static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003948ftrace_regex_write(struct file *file, const char __user *ubuf,
3949 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003950{
3951 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003952 struct trace_parser *parser;
3953 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003954
Li Zefan4ba79782009-09-22 13:52:20 +08003955 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003956 return 0;
3957
Steven Rostedt5072c592008-05-12 21:20:43 +02003958 if (file->f_mode & FMODE_READ) {
3959 struct seq_file *m = file->private_data;
3960 iter = m->private;
3961 } else
3962 iter = file->private_data;
3963
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003964 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003965 return -ENODEV;
3966
3967 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003968
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003969 parser = &iter->parser;
3970 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003971
Li Zefan4ba79782009-09-22 13:52:20 +08003972 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003973 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003974 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003975 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003976 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003977 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003978 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003979 }
3980
Steven Rostedt5072c592008-05-12 21:20:43 +02003981 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003982 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003983 return ret;
3984}
3985
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003986ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003987ftrace_filter_write(struct file *file, const char __user *ubuf,
3988 size_t cnt, loff_t *ppos)
3989{
3990 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3991}
3992
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003993ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003994ftrace_notrace_write(struct file *file, const char __user *ubuf,
3995 size_t cnt, loff_t *ppos)
3996{
3997 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3998}
3999
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004000static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004001ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4002{
4003 struct ftrace_func_entry *entry;
4004
4005 if (!ftrace_location(ip))
4006 return -EINVAL;
4007
4008 if (remove) {
4009 entry = ftrace_lookup_ip(hash, ip);
4010 if (!entry)
4011 return -ENOENT;
4012 free_hash_entry(hash, entry);
4013 return 0;
4014 }
4015
4016 return add_hash_entry(hash, ip);
4017}
4018
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004019static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004020 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004021{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004022 struct ftrace_ops *op;
4023
4024 if (!ftrace_enabled)
4025 return;
4026
4027 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004028 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004029 return;
4030 }
4031
4032 /*
4033 * If this is the shared global_ops filter, then we need to
4034 * check if there is another ops that shares it, is enabled.
4035 * If so, we still need to run the modify code.
4036 */
4037 if (ops->func_hash != &global_ops.local_hash)
4038 return;
4039
4040 do_for_each_ftrace_op(op, ftrace_ops_list) {
4041 if (op->func_hash == &global_ops.local_hash &&
4042 op->flags & FTRACE_OPS_FL_ENABLED) {
4043 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4044 /* Only need to do this once */
4045 return;
4046 }
4047 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004048}
4049
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004050static int
4051ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4052 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004053{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004054 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004055 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004056 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004057 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004058 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004059
Steven Rostedt41c52c02008-05-22 11:46:33 -04004060 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004061 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004062
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004063 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004064
Steven Rostedtf45948e2011-05-02 12:29:25 -04004065 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004066 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004067 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004068 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004069
Wang Nanb972cc52014-07-15 08:40:20 +08004070 if (reset)
4071 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4072 else
4073 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4074
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004075 if (!hash) {
4076 ret = -ENOMEM;
4077 goto out_regex_unlock;
4078 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004079
Jiri Olsaac483c42012-01-02 10:04:14 +01004080 if (buf && !ftrace_match_records(hash, buf, len)) {
4081 ret = -EINVAL;
4082 goto out_regex_unlock;
4083 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004084 if (ip) {
4085 ret = ftrace_match_addr(hash, ip, remove);
4086 if (ret < 0)
4087 goto out_regex_unlock;
4088 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004089
4090 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004091 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004092 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4093 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004094 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004095 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004096 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004097 free_ftrace_hash_rcu(old_hash);
4098 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004099 mutex_unlock(&ftrace_lock);
4100
Jiri Olsaac483c42012-01-02 10:04:14 +01004101 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004102 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004103
4104 free_ftrace_hash(hash);
4105 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004106}
4107
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004108static int
4109ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4110 int reset, int enable)
4111{
4112 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4113}
4114
4115/**
4116 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4117 * @ops - the ops to set the filter with
4118 * @ip - the address to add to or remove from the filter.
4119 * @remove - non zero to remove the ip from the filter
4120 * @reset - non zero to reset all filters before applying this filter.
4121 *
4122 * Filters denote which functions should be enabled when tracing is enabled
4123 * If @ip is NULL, it failes to update filter.
4124 */
4125int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4126 int remove, int reset)
4127{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004128 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004129 return ftrace_set_addr(ops, ip, remove, reset, 1);
4130}
4131EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4132
4133static int
4134ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4135 int reset, int enable)
4136{
4137 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4138}
4139
Steven Rostedt77a2b372008-05-12 21:20:45 +02004140/**
4141 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004142 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004143 * @buf - the string that holds the function filter text.
4144 * @len - the length of the string.
4145 * @reset - non zero to reset all filters before applying this filter.
4146 *
4147 * Filters denote which functions should be enabled when tracing is enabled.
4148 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4149 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004150int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004151 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004152{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004153 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004154 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004155}
Steven Rostedt936e0742011-05-05 22:54:01 -04004156EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004157
Steven Rostedt41c52c02008-05-22 11:46:33 -04004158/**
4159 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004160 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004161 * @buf - the string that holds the function notrace text.
4162 * @len - the length of the string.
4163 * @reset - non zero to reset all filters before applying this filter.
4164 *
4165 * Notrace Filters denote which functions should not be enabled when tracing
4166 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4167 * for tracing.
4168 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004169int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004170 int len, int reset)
4171{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004172 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004173 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004174}
4175EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4176/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004177 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004178 * @buf - the string that holds the function filter text.
4179 * @len - the length of the string.
4180 * @reset - non zero to reset all filters before applying this filter.
4181 *
4182 * Filters denote which functions should be enabled when tracing is enabled.
4183 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4184 */
4185void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4186{
4187 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4188}
4189EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4190
4191/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004192 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004193 * @buf - the string that holds the function notrace text.
4194 * @len - the length of the string.
4195 * @reset - non zero to reset all filters before applying this filter.
4196 *
4197 * Notrace Filters denote which functions should not be enabled when tracing
4198 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4199 * for tracing.
4200 */
4201void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004202{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004203 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004204}
Steven Rostedt936e0742011-05-05 22:54:01 -04004205EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004206
Steven Rostedt2af15d62009-05-28 13:37:24 -04004207/*
4208 * command line interface to allow users to set filters on boot up.
4209 */
4210#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4211static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4212static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4213
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004214/* Used by function selftest to not test if filter is set */
4215bool ftrace_filter_param __initdata;
4216
Steven Rostedt2af15d62009-05-28 13:37:24 -04004217static int __init set_ftrace_notrace(char *str)
4218{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004219 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004220 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004221 return 1;
4222}
4223__setup("ftrace_notrace=", set_ftrace_notrace);
4224
4225static int __init set_ftrace_filter(char *str)
4226{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004227 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004228 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004229 return 1;
4230}
4231__setup("ftrace_filter=", set_ftrace_filter);
4232
Stefan Assmann369bc182009-10-12 22:17:21 +02004233#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004234static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004235static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004236static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004237
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004238static unsigned long save_global_trampoline;
4239static unsigned long save_global_flags;
4240
Stefan Assmann369bc182009-10-12 22:17:21 +02004241static int __init set_graph_function(char *str)
4242{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004243 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004244 return 1;
4245}
4246__setup("ftrace_graph_filter=", set_graph_function);
4247
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004248static int __init set_graph_notrace_function(char *str)
4249{
4250 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4251 return 1;
4252}
4253__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4254
4255static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004256{
4257 int ret;
4258 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004259 unsigned long *table = ftrace_graph_funcs;
4260 int *count = &ftrace_graph_count;
4261
4262 if (!enable) {
4263 table = ftrace_graph_notrace_funcs;
4264 count = &ftrace_graph_notrace_count;
4265 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004266
4267 while (buf) {
4268 func = strsep(&buf, ",");
4269 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004270 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004271 if (ret)
4272 printk(KERN_DEBUG "ftrace: function %s not "
4273 "traceable\n", func);
4274 }
4275}
4276#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4277
Steven Rostedt2a85a372011-12-19 21:57:44 -05004278void __init
4279ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004280{
4281 char *func;
4282
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004283 ftrace_ops_init(ops);
4284
Steven Rostedt2af15d62009-05-28 13:37:24 -04004285 while (buf) {
4286 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004287 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004288 }
4289}
4290
4291static void __init set_ftrace_early_filters(void)
4292{
4293 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004294 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004295 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004296 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004297#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4298 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004299 set_ftrace_early_graph(ftrace_graph_buf, 1);
4300 if (ftrace_graph_notrace_buf[0])
4301 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004302#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004303}
4304
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004305int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004306{
4307 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004308 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004309 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004310 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004311 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004312 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004313 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004314 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004315
Steven Rostedt5072c592008-05-12 21:20:43 +02004316 if (file->f_mode & FMODE_READ) {
4317 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004318 seq_release(inode, file);
4319 } else
4320 iter = file->private_data;
4321
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004322 parser = &iter->parser;
4323 if (trace_parser_loaded(parser)) {
4324 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004325 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004326 }
4327
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004328 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004329
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004330 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004331
Steven Rostedt058e2972011-04-29 22:35:33 -04004332 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004333 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4334
4335 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004336 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004337 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004338 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004339
Steven Rostedt058e2972011-04-29 22:35:33 -04004340 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004341 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004342 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4343 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004344 ret = ftrace_hash_move(iter->ops, filter_hash,
4345 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004346 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004347 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004348 free_ftrace_hash_rcu(old_hash);
4349 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004350 mutex_unlock(&ftrace_lock);
4351 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004352
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004353 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004354 free_ftrace_hash(iter->hash);
4355 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004356
Steven Rostedt5072c592008-05-12 21:20:43 +02004357 return 0;
4358}
4359
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004360static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004361 .open = ftrace_avail_open,
4362 .read = seq_read,
4363 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004364 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004365};
4366
Steven Rostedt647bcd02011-05-03 14:39:21 -04004367static const struct file_operations ftrace_enabled_fops = {
4368 .open = ftrace_enabled_open,
4369 .read = seq_read,
4370 .llseek = seq_lseek,
4371 .release = seq_release_private,
4372};
4373
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004374static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004375 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004376 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004377 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004378 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004379 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004380};
4381
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004382static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004383 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004384 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004385 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004386 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004387 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004388};
4389
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004390#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4391
4392static DEFINE_MUTEX(graph_lock);
4393
4394int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004395int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004396unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004397unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004398
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004399struct ftrace_graph_data {
4400 unsigned long *table;
4401 size_t size;
4402 int *count;
4403 const struct seq_operations *seq_ops;
4404};
4405
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004406static void *
Li Zefan85951842009-06-24 09:54:00 +08004407__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004408{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004409 struct ftrace_graph_data *fgd = m->private;
4410
4411 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004412 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004413 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004414}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004415
Li Zefan85951842009-06-24 09:54:00 +08004416static void *
4417g_next(struct seq_file *m, void *v, loff_t *pos)
4418{
4419 (*pos)++;
4420 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004421}
4422
4423static void *g_start(struct seq_file *m, loff_t *pos)
4424{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004425 struct ftrace_graph_data *fgd = m->private;
4426
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004427 mutex_lock(&graph_lock);
4428
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004429 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004430 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004431 return (void *)1;
4432
Li Zefan85951842009-06-24 09:54:00 +08004433 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004434}
4435
4436static void g_stop(struct seq_file *m, void *p)
4437{
4438 mutex_unlock(&graph_lock);
4439}
4440
4441static int g_show(struct seq_file *m, void *v)
4442{
4443 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004444
4445 if (!ptr)
4446 return 0;
4447
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004448 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004449 struct ftrace_graph_data *fgd = m->private;
4450
4451 if (fgd->table == ftrace_graph_funcs)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004452 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004453 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004454 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004455 return 0;
4456 }
4457
Steven Rostedtb375a112009-09-17 00:05:58 -04004458 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004459
4460 return 0;
4461}
4462
James Morris88e9d342009-09-22 16:43:43 -07004463static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004464 .start = g_start,
4465 .next = g_next,
4466 .stop = g_stop,
4467 .show = g_show,
4468};
4469
4470static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004471__ftrace_graph_open(struct inode *inode, struct file *file,
4472 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004473{
4474 int ret = 0;
4475
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004476 mutex_lock(&graph_lock);
4477 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004478 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004479 *fgd->count = 0;
4480 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004481 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004482 mutex_unlock(&graph_lock);
4483
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004484 if (file->f_mode & FMODE_READ) {
4485 ret = seq_open(file, fgd->seq_ops);
4486 if (!ret) {
4487 struct seq_file *m = file->private_data;
4488 m->private = fgd;
4489 }
4490 } else
4491 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004492
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004493 return ret;
4494}
4495
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004496static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004497ftrace_graph_open(struct inode *inode, struct file *file)
4498{
4499 struct ftrace_graph_data *fgd;
4500
4501 if (unlikely(ftrace_disabled))
4502 return -ENODEV;
4503
4504 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4505 if (fgd == NULL)
4506 return -ENOMEM;
4507
4508 fgd->table = ftrace_graph_funcs;
4509 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4510 fgd->count = &ftrace_graph_count;
4511 fgd->seq_ops = &ftrace_graph_seq_ops;
4512
4513 return __ftrace_graph_open(inode, file, fgd);
4514}
4515
4516static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004517ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4518{
4519 struct ftrace_graph_data *fgd;
4520
4521 if (unlikely(ftrace_disabled))
4522 return -ENODEV;
4523
4524 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4525 if (fgd == NULL)
4526 return -ENOMEM;
4527
4528 fgd->table = ftrace_graph_notrace_funcs;
4529 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4530 fgd->count = &ftrace_graph_notrace_count;
4531 fgd->seq_ops = &ftrace_graph_seq_ops;
4532
4533 return __ftrace_graph_open(inode, file, fgd);
4534}
4535
4536static int
Li Zefan87827112009-07-23 11:29:11 +08004537ftrace_graph_release(struct inode *inode, struct file *file)
4538{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004539 if (file->f_mode & FMODE_READ) {
4540 struct seq_file *m = file->private_data;
4541
4542 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004543 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004544 } else {
4545 kfree(file->private_data);
4546 }
4547
Li Zefan87827112009-07-23 11:29:11 +08004548 return 0;
4549}
4550
4551static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004552ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004553{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004554 struct dyn_ftrace *rec;
4555 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004556 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004557 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004558 int type, not;
4559 char *search;
4560 bool exists;
4561 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004562
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004563 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004564 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004565 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004566 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004567
4568 search_len = strlen(search);
4569
Steven Rostedt52baf112009-02-14 01:15:39 -05004570 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004571
4572 if (unlikely(ftrace_disabled)) {
4573 mutex_unlock(&ftrace_lock);
4574 return -ENODEV;
4575 }
4576
Steven Rostedt265c8312009-02-13 12:43:56 -05004577 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004578
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004579 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004580 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004581 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004582 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004583 if (array[i] == rec->ip) {
4584 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004585 break;
4586 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004587 }
4588
4589 if (!not) {
4590 fail = 0;
4591 if (!exists) {
4592 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004593 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004594 goto out;
4595 }
4596 } else {
4597 if (exists) {
4598 array[i] = array[--(*idx)];
4599 array[*idx] = 0;
4600 fail = 0;
4601 }
4602 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004603 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004604 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004605out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004606 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004607
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004608 if (fail)
4609 return -EINVAL;
4610
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004611 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004612}
4613
4614static ssize_t
4615ftrace_graph_write(struct file *file, const char __user *ubuf,
4616 size_t cnt, loff_t *ppos)
4617{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004618 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004619 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004620 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004621
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004622 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004623 return 0;
4624
Namhyung Kim6a101082013-10-14 17:24:25 +09004625 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4626 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004627
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004628 read = trace_get_user(&parser, ubuf, cnt, ppos);
4629
Li Zefan4ba79782009-09-22 13:52:20 +08004630 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004631 parser.buffer[parser.idx] = 0;
4632
Namhyung Kim6a101082013-10-14 17:24:25 +09004633 mutex_lock(&graph_lock);
4634
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004635 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004636 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4637 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004638
4639 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004640 }
4641
Namhyung Kim6a101082013-10-14 17:24:25 +09004642 if (!ret)
4643 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004644
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004645 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004646
4647 return ret;
4648}
4649
4650static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004651 .open = ftrace_graph_open,
4652 .read = seq_read,
4653 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004654 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004655 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004656};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004657
4658static const struct file_operations ftrace_graph_notrace_fops = {
4659 .open = ftrace_graph_notrace_open,
4660 .read = seq_read,
4661 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004662 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004663 .release = ftrace_graph_release,
4664};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004665#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4666
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004667void ftrace_create_filter_files(struct ftrace_ops *ops,
4668 struct dentry *parent)
4669{
4670
4671 trace_create_file("set_ftrace_filter", 0644, parent,
4672 ops, &ftrace_filter_fops);
4673
4674 trace_create_file("set_ftrace_notrace", 0644, parent,
4675 ops, &ftrace_notrace_fops);
4676}
4677
4678/*
4679 * The name "destroy_filter_files" is really a misnomer. Although
4680 * in the future, it may actualy delete the files, but this is
4681 * really intended to make sure the ops passed in are disabled
4682 * and that when this function returns, the caller is free to
4683 * free the ops.
4684 *
4685 * The "destroy" name is only to match the "create" name that this
4686 * should be paired with.
4687 */
4688void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4689{
4690 mutex_lock(&ftrace_lock);
4691 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4692 ftrace_shutdown(ops, 0);
4693 ops->flags |= FTRACE_OPS_FL_DELETED;
4694 mutex_unlock(&ftrace_lock);
4695}
4696
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004697static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004698{
Steven Rostedt5072c592008-05-12 21:20:43 +02004699
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004700 trace_create_file("available_filter_functions", 0444,
4701 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004702
Steven Rostedt647bcd02011-05-03 14:39:21 -04004703 trace_create_file("enabled_functions", 0444,
4704 d_tracer, NULL, &ftrace_enabled_fops);
4705
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004706 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004707
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004708#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004709 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004710 NULL,
4711 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004712 trace_create_file("set_graph_notrace", 0444, d_tracer,
4713 NULL,
4714 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004715#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4716
Steven Rostedt5072c592008-05-12 21:20:43 +02004717 return 0;
4718}
4719
Steven Rostedt9fd49322012-04-24 22:32:06 -04004720static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004721{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004722 const unsigned long *ipa = a;
4723 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004724
Steven Rostedt9fd49322012-04-24 22:32:06 -04004725 if (*ipa > *ipb)
4726 return 1;
4727 if (*ipa < *ipb)
4728 return -1;
4729 return 0;
4730}
4731
4732static void ftrace_swap_ips(void *a, void *b, int size)
4733{
4734 unsigned long *ipa = a;
4735 unsigned long *ipb = b;
4736 unsigned long t;
4737
4738 t = *ipa;
4739 *ipa = *ipb;
4740 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004741}
4742
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004743static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004744 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004745 unsigned long *end)
4746{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004747 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004748 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004749 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004750 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004751 unsigned long *p;
4752 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004753 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004754 int ret = -ENOMEM;
4755
4756 count = end - start;
4757
4758 if (!count)
4759 return 0;
4760
Steven Rostedt9fd49322012-04-24 22:32:06 -04004761 sort(start, count, sizeof(*start),
4762 ftrace_cmp_ips, ftrace_swap_ips);
4763
Steven Rostedt706c81f2012-04-24 23:45:26 -04004764 start_pg = ftrace_allocate_pages(count);
4765 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004766 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004767
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004768 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004769
Steven Rostedt32082302011-12-16 14:42:37 -05004770 /*
4771 * Core and each module needs their own pages, as
4772 * modules will free them when they are removed.
4773 * Force a new page to be allocated for modules.
4774 */
Steven Rostedta7900872011-12-16 16:23:44 -05004775 if (!mod) {
4776 WARN_ON(ftrace_pages || ftrace_pages_start);
4777 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004778 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004779 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004780 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004781 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004782
Steven Rostedta7900872011-12-16 16:23:44 -05004783 if (WARN_ON(ftrace_pages->next)) {
4784 /* Hmm, we have free pages? */
4785 while (ftrace_pages->next)
4786 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004787 }
Steven Rostedta7900872011-12-16 16:23:44 -05004788
Steven Rostedt706c81f2012-04-24 23:45:26 -04004789 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004790 }
4791
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004792 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004793 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004794 while (p < end) {
4795 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004796 /*
4797 * Some architecture linkers will pad between
4798 * the different mcount_loc sections of different
4799 * object files to satisfy alignments.
4800 * Skip any NULL pointers.
4801 */
4802 if (!addr)
4803 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004804
4805 if (pg->index == pg->size) {
4806 /* We should have allocated enough */
4807 if (WARN_ON(!pg->next))
4808 break;
4809 pg = pg->next;
4810 }
4811
4812 rec = &pg->records[pg->index++];
4813 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004814 }
4815
Steven Rostedt706c81f2012-04-24 23:45:26 -04004816 /* We should have used all pages */
4817 WARN_ON(pg->next);
4818
4819 /* Assign the last page to ftrace_pages */
4820 ftrace_pages = pg;
4821
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004822 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004823 * We only need to disable interrupts on start up
4824 * because we are modifying code that an interrupt
4825 * may execute, and the modification is not atomic.
4826 * But for modules, nothing runs the code we modify
4827 * until we are finished with it, and there's no
4828 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004829 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004830 if (!mod)
4831 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004832 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004833 if (!mod)
4834 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004835 ret = 0;
4836 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004837 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004838
Steven Rostedta7900872011-12-16 16:23:44 -05004839 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004840}
4841
Steven Rostedt93eb6772009-04-15 13:24:06 -04004842#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004843
4844#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4845
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004846void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004847{
4848 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004849 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004850 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004851 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004852
Steven Rostedt93eb6772009-04-15 13:24:06 -04004853 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004854
4855 if (ftrace_disabled)
4856 goto out_unlock;
4857
Steven Rostedt32082302011-12-16 14:42:37 -05004858 /*
4859 * Each module has its own ftrace_pages, remove
4860 * them from the list.
4861 */
4862 last_pg = &ftrace_pages_start;
4863 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4864 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004865 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004866 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004867 * As core pages are first, the first
4868 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004869 */
Steven Rostedt32082302011-12-16 14:42:37 -05004870 if (WARN_ON(pg == ftrace_pages_start))
4871 goto out_unlock;
4872
4873 /* Check if we are deleting the last page */
4874 if (pg == ftrace_pages)
4875 ftrace_pages = next_to_ftrace_page(last_pg);
4876
4877 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004878 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4879 free_pages((unsigned long)pg->records, order);
4880 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004881 } else
4882 last_pg = &pg->next;
4883 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004884 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004885 mutex_unlock(&ftrace_lock);
4886}
4887
4888static void ftrace_init_module(struct module *mod,
4889 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004890{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004891 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004892 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004893 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004894}
4895
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004896void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004897{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004898 ftrace_init_module(mod, mod->ftrace_callsites,
4899 mod->ftrace_callsites +
4900 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004901}
4902
4903static int ftrace_module_notify_exit(struct notifier_block *self,
4904 unsigned long val, void *data)
4905{
4906 struct module *mod = data;
4907
4908 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004909 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004910
4911 return 0;
4912}
4913#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004914static int ftrace_module_notify_exit(struct notifier_block *self,
4915 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004916{
4917 return 0;
4918}
4919#endif /* CONFIG_MODULES */
4920
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004921struct notifier_block ftrace_module_exit_nb = {
4922 .notifier_call = ftrace_module_notify_exit,
4923 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4924};
4925
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004926void __init ftrace_init(void)
4927{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004928 extern unsigned long __start_mcount_loc[];
4929 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004930 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004931 int ret;
4932
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004933 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004934 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004935 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004936 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004937 goto failed;
4938
4939 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004940 if (!count) {
4941 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004942 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004943 }
4944
4945 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4946 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004947
4948 last_ftrace_enabled = ftrace_enabled = 1;
4949
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004950 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004951 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004952 __stop_mcount_loc);
4953
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004954 ret = register_module_notifier(&ftrace_module_exit_nb);
4955 if (ret)
4956 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004957
Steven Rostedt2af15d62009-05-28 13:37:24 -04004958 set_ftrace_early_filters();
4959
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004960 return;
4961 failed:
4962 ftrace_disabled = 1;
4963}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004964
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004965/* Do nothing if arch does not support this */
4966void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
4967{
4968}
4969
4970static void ftrace_update_trampoline(struct ftrace_ops *ops)
4971{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04004972
4973/*
4974 * Currently there's no safe way to free a trampoline when the kernel
4975 * is configured with PREEMPT. That is because a task could be preempted
4976 * when it jumped to the trampoline, it may be preempted for a long time
4977 * depending on the system load, and currently there's no way to know
4978 * when it will be off the trampoline. If the trampoline is freed
4979 * too early, when the task runs again, it will be executing on freed
4980 * memory and crash.
4981 */
4982#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004983 /* Currently, only non dynamic ops can have a trampoline */
4984 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
4985 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04004986#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004987
4988 arch_ftrace_update_trampoline(ops);
4989}
4990
Steven Rostedt3d083392008-05-12 21:20:42 +02004991#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004992
Steven Rostedt2b499382011-05-03 22:49:52 -04004993static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004994 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004995 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004996};
4997
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004998static int __init ftrace_nodyn_init(void)
4999{
5000 ftrace_enabled = 1;
5001 return 0;
5002}
Steven Rostedt6f415672012-10-05 12:13:07 -04005003core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005004
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005005static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
5006static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005007static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005008/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005009# define ftrace_startup(ops, command) \
5010 ({ \
5011 int ___ret = __register_ftrace_function(ops); \
5012 if (!___ret) \
5013 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5014 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005015 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005016# define ftrace_shutdown(ops, command) \
5017 ({ \
5018 int ___ret = __unregister_ftrace_function(ops); \
5019 if (!___ret) \
5020 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5021 ___ret; \
5022 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005023
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005024# define ftrace_startup_sysctl() do { } while (0)
5025# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005026
5027static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005028ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005029{
5030 return 1;
5031}
5032
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005033static void ftrace_update_trampoline(struct ftrace_ops *ops)
5034{
5035}
5036
Steven Rostedt3d083392008-05-12 21:20:42 +02005037#endif /* CONFIG_DYNAMIC_FTRACE */
5038
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005039__init void ftrace_init_global_array_ops(struct trace_array *tr)
5040{
5041 tr->ops = &global_ops;
5042 tr->ops->private = tr;
5043}
5044
5045void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5046{
5047 /* If we filter on pids, update to use the pid function */
5048 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5049 if (WARN_ON(tr->ops->func != ftrace_stub))
5050 printk("ftrace ops had %pS for function\n",
5051 tr->ops->func);
5052 /* Only the top level instance does pid tracing */
5053 if (!list_empty(&ftrace_pids)) {
5054 set_ftrace_pid_function(func);
5055 func = ftrace_pid_func;
5056 }
5057 }
5058 tr->ops->func = func;
5059 tr->ops->private = tr;
5060}
5061
5062void ftrace_reset_array_ops(struct trace_array *tr)
5063{
5064 tr->ops->func = ftrace_stub;
5065}
5066
Steven Rostedtb8489142011-05-04 09:27:52 -04005067static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005068ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005069 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01005070{
Jiri Olsae2484912012-02-15 15:51:48 +01005071 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
5072 return;
5073
5074 /*
5075 * Some of the ops may be dynamically allocated,
5076 * they must be freed after a synchronize_sched().
5077 */
5078 preempt_disable_notrace();
5079 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05005080
5081 /*
5082 * Control funcs (perf) uses RCU. Only trace if
5083 * RCU is currently active.
5084 */
5085 if (!rcu_is_watching())
5086 goto out;
5087
Steven Rostedt0a016402012-11-02 17:03:03 -04005088 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04005089 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
5090 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005091 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04005092 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04005093 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05005094 out:
Jiri Olsae2484912012-02-15 15:51:48 +01005095 trace_recursion_clear(TRACE_CONTROL_BIT);
5096 preempt_enable_notrace();
5097}
5098
5099static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005100 .func = ftrace_ops_control_func,
5101 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005102 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01005103};
5104
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005105static inline void
5106__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005107 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005108{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005109 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005110 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005111
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005112 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5113 if (bit < 0)
5114 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005115
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005116 /*
5117 * Some of the ops may be dynamically allocated,
5118 * they must be freed after a synchronize_sched().
5119 */
5120 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04005121 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005122 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005123 if (FTRACE_WARN_ON(!op->func)) {
5124 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005125 goto out;
5126 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005127 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005128 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005129 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005130out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005131 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005132 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005133}
5134
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005135/*
5136 * Some archs only support passing ip and parent_ip. Even though
5137 * the list function ignores the op parameter, we do not want any
5138 * C side effects, where a function is called without the caller
5139 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005140 * Archs are to support both the regs and ftrace_ops at the same time.
5141 * If they support ftrace_ops, it is assumed they support regs.
5142 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005143 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5144 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005145 * An architecture can pass partial regs with ftrace_ops and still
5146 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005147 */
5148#if ARCH_SUPPORTS_FTRACE_OPS
5149static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005150 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005151{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005152 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005153}
5154#else
5155static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5156{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005157 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005158}
5159#endif
5160
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005161/*
5162 * If there's only one function registered but it does not support
5163 * recursion, this function will be called by the mcount trampoline.
5164 * This function will handle recursion protection.
5165 */
5166static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
5167 struct ftrace_ops *op, struct pt_regs *regs)
5168{
5169 int bit;
5170
5171 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5172 if (bit < 0)
5173 return;
5174
5175 op->func(ip, parent_ip, op, regs);
5176
5177 trace_clear_recursion(bit);
5178}
5179
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005180/**
5181 * ftrace_ops_get_func - get the function a trampoline should call
5182 * @ops: the ops to get the function for
5183 *
5184 * Normally the mcount trampoline will call the ops->func, but there
5185 * are times that it should not. For example, if the ops does not
5186 * have its own recursion protection, then it should call the
5187 * ftrace_ops_recurs_func() instead.
5188 *
5189 * Returns the function that the trampoline should call for @ops.
5190 */
5191ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5192{
5193 /*
5194 * If this is a dynamic ops or we force list func,
5195 * then it needs to call the list anyway.
5196 */
5197 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
5198 return ftrace_ops_list_func;
5199
5200 /*
5201 * If the func handles its own recursion, call it directly.
5202 * Otherwise call the recursion protected function that
5203 * will call the ftrace ops function.
5204 */
5205 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
5206 return ftrace_ops_recurs_func;
5207
5208 return ops->func;
5209}
5210
Steven Rostedte32d8952008-12-04 00:26:41 -05005211static void clear_ftrace_swapper(void)
5212{
5213 struct task_struct *p;
5214 int cpu;
5215
5216 get_online_cpus();
5217 for_each_online_cpu(cpu) {
5218 p = idle_task(cpu);
5219 clear_tsk_trace_trace(p);
5220 }
5221 put_online_cpus();
5222}
5223
5224static void set_ftrace_swapper(void)
5225{
5226 struct task_struct *p;
5227 int cpu;
5228
5229 get_online_cpus();
5230 for_each_online_cpu(cpu) {
5231 p = idle_task(cpu);
5232 set_tsk_trace_trace(p);
5233 }
5234 put_online_cpus();
5235}
5236
5237static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005238{
5239 struct task_struct *p;
5240
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005241 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05005242 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05005243 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05005244 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005245 rcu_read_unlock();
5246
Steven Rostedte32d8952008-12-04 00:26:41 -05005247 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005248}
5249
Steven Rostedte32d8952008-12-04 00:26:41 -05005250static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005251{
5252 struct task_struct *p;
5253
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005254 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005255 do_each_pid_task(pid, PIDTYPE_PID, p) {
5256 set_tsk_trace_trace(p);
5257 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005258 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005259}
5260
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005261static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005262{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005263 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005264 clear_ftrace_swapper();
5265 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005266 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05005267}
5268
5269static void set_ftrace_pid_task(struct pid *pid)
5270{
5271 if (pid == ftrace_swapper_pid)
5272 set_ftrace_swapper();
5273 else
5274 set_ftrace_pid(pid);
5275}
5276
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005277static int ftrace_pid_add(int p)
5278{
5279 struct pid *pid;
5280 struct ftrace_pid *fpid;
5281 int ret = -EINVAL;
5282
5283 mutex_lock(&ftrace_lock);
5284
5285 if (!p)
5286 pid = ftrace_swapper_pid;
5287 else
5288 pid = find_get_pid(p);
5289
5290 if (!pid)
5291 goto out;
5292
5293 ret = 0;
5294
5295 list_for_each_entry(fpid, &ftrace_pids, list)
5296 if (fpid->pid == pid)
5297 goto out_put;
5298
5299 ret = -ENOMEM;
5300
5301 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5302 if (!fpid)
5303 goto out_put;
5304
5305 list_add(&fpid->list, &ftrace_pids);
5306 fpid->pid = pid;
5307
5308 set_ftrace_pid_task(pid);
5309
5310 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005311
5312 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005313
5314 mutex_unlock(&ftrace_lock);
5315 return 0;
5316
5317out_put:
5318 if (pid != ftrace_swapper_pid)
5319 put_pid(pid);
5320
5321out:
5322 mutex_unlock(&ftrace_lock);
5323 return ret;
5324}
5325
5326static void ftrace_pid_reset(void)
5327{
5328 struct ftrace_pid *fpid, *safe;
5329
5330 mutex_lock(&ftrace_lock);
5331 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5332 struct pid *pid = fpid->pid;
5333
5334 clear_ftrace_pid_task(pid);
5335
5336 list_del(&fpid->list);
5337 kfree(fpid);
5338 }
5339
5340 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005341 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005342
5343 mutex_unlock(&ftrace_lock);
5344}
5345
5346static void *fpid_start(struct seq_file *m, loff_t *pos)
5347{
5348 mutex_lock(&ftrace_lock);
5349
5350 if (list_empty(&ftrace_pids) && (!*pos))
5351 return (void *) 1;
5352
5353 return seq_list_start(&ftrace_pids, *pos);
5354}
5355
5356static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5357{
5358 if (v == (void *)1)
5359 return NULL;
5360
5361 return seq_list_next(v, &ftrace_pids, pos);
5362}
5363
5364static void fpid_stop(struct seq_file *m, void *p)
5365{
5366 mutex_unlock(&ftrace_lock);
5367}
5368
5369static int fpid_show(struct seq_file *m, void *v)
5370{
5371 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5372
5373 if (v == (void *)1) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005374 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005375 return 0;
5376 }
5377
5378 if (fpid->pid == ftrace_swapper_pid)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005379 seq_puts(m, "swapper tasks\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005380 else
5381 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5382
5383 return 0;
5384}
5385
5386static const struct seq_operations ftrace_pid_sops = {
5387 .start = fpid_start,
5388 .next = fpid_next,
5389 .stop = fpid_stop,
5390 .show = fpid_show,
5391};
5392
5393static int
5394ftrace_pid_open(struct inode *inode, struct file *file)
5395{
5396 int ret = 0;
5397
5398 if ((file->f_mode & FMODE_WRITE) &&
5399 (file->f_flags & O_TRUNC))
5400 ftrace_pid_reset();
5401
5402 if (file->f_mode & FMODE_READ)
5403 ret = seq_open(file, &ftrace_pid_sops);
5404
5405 return ret;
5406}
5407
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005408static ssize_t
5409ftrace_pid_write(struct file *filp, const char __user *ubuf,
5410 size_t cnt, loff_t *ppos)
5411{
Ingo Molnar457dc922009-11-23 11:03:28 +01005412 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005413 long val;
5414 int ret;
5415
5416 if (cnt >= sizeof(buf))
5417 return -EINVAL;
5418
5419 if (copy_from_user(&buf, ubuf, cnt))
5420 return -EFAULT;
5421
5422 buf[cnt] = 0;
5423
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005424 /*
5425 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5426 * to clean the filter quietly.
5427 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005428 tmp = strstrip(buf);
5429 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005430 return 1;
5431
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005432 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005433 if (ret < 0)
5434 return ret;
5435
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005436 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005437
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005438 return ret ? ret : cnt;
5439}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005440
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005441static int
5442ftrace_pid_release(struct inode *inode, struct file *file)
5443{
5444 if (file->f_mode & FMODE_READ)
5445 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005446
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005447 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005448}
5449
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005450static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005451 .open = ftrace_pid_open,
5452 .write = ftrace_pid_write,
5453 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005454 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005455 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005456};
5457
5458static __init int ftrace_init_debugfs(void)
5459{
5460 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005461
5462 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05005463 if (IS_ERR(d_tracer))
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005464 return 0;
5465
5466 ftrace_init_dyn_debugfs(d_tracer);
5467
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005468 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5469 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005470
5471 ftrace_profile_debugfs(d_tracer);
5472
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005473 return 0;
5474}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005475fs_initcall(ftrace_init_debugfs);
5476
Steven Rostedt3d083392008-05-12 21:20:42 +02005477/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005478 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005479 *
5480 * This function should be used by panic code. It stops ftrace
5481 * but in a not so nice way. If you need to simply kill ftrace
5482 * from a non-atomic section, use ftrace_kill.
5483 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005484void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005485{
5486 ftrace_disabled = 1;
5487 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005488 clear_ftrace_function();
5489}
5490
5491/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005492 * Test if ftrace is dead or not.
5493 */
5494int ftrace_is_dead(void)
5495{
5496 return ftrace_disabled;
5497}
5498
5499/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005500 * register_ftrace_function - register a function for profiling
5501 * @ops - ops structure that holds the function for profiling.
5502 *
5503 * Register a function to be called by all functions in the
5504 * kernel.
5505 *
5506 * Note: @ops->func and all the functions it calls must be labeled
5507 * with "notrace", otherwise it will go into a
5508 * recursive loop.
5509 */
5510int register_ftrace_function(struct ftrace_ops *ops)
5511{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005512 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005513
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005514 ftrace_ops_init(ops);
5515
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005516 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005517
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005518 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005519
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005520 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005521
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005522 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005523}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005524EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005525
5526/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005527 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005528 * @ops - ops structure that holds the function to unregister
5529 *
5530 * Unregister a function that was added to be called by ftrace profiling.
5531 */
5532int unregister_ftrace_function(struct ftrace_ops *ops)
5533{
5534 int ret;
5535
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005536 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005537 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005538 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005539
5540 return ret;
5541}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005542EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005543
Ingo Molnare309b412008-05-12 21:20:51 +02005544int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005545ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005546 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005547 loff_t *ppos)
5548{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005549 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005550
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005551 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005552
Steven Rostedt45a4a232011-04-21 23:16:46 -04005553 if (unlikely(ftrace_disabled))
5554 goto out;
5555
5556 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005557
Li Zefana32c7762009-06-26 16:55:51 +08005558 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005559 goto out;
5560
Li Zefana32c7762009-06-26 16:55:51 +08005561 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005562
5563 if (ftrace_enabled) {
5564
5565 ftrace_startup_sysctl();
5566
5567 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005568 if (ftrace_ops_list != &ftrace_list_end)
5569 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005570
5571 } else {
5572 /* stopping ftrace calls (just send to ftrace_stub) */
5573 ftrace_trace_function = ftrace_stub;
5574
5575 ftrace_shutdown_sysctl();
5576 }
5577
5578 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005579 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005580 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005581}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005582
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005583#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005584
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005585static struct ftrace_ops graph_ops = {
5586 .func = ftrace_stub,
5587 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5588 FTRACE_OPS_FL_INITIALIZED |
5589 FTRACE_OPS_FL_STUB,
5590#ifdef FTRACE_GRAPH_TRAMP_ADDR
5591 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05005592 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005593#endif
5594 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5595};
5596
Steven Rostedt597af812009-04-03 15:24:12 -04005597static int ftrace_graph_active;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005598
Steven Rostedte49dc192008-12-02 23:50:05 -05005599int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5600{
5601 return 0;
5602}
5603
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005604/* The callbacks that hook a function */
5605trace_func_graph_ret_t ftrace_graph_return =
5606 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005607trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005608static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005609
5610/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5611static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5612{
5613 int i;
5614 int ret = 0;
5615 unsigned long flags;
5616 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5617 struct task_struct *g, *t;
5618
5619 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5620 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5621 * sizeof(struct ftrace_ret_stack),
5622 GFP_KERNEL);
5623 if (!ret_stack_list[i]) {
5624 start = 0;
5625 end = i;
5626 ret = -ENOMEM;
5627 goto free;
5628 }
5629 }
5630
5631 read_lock_irqsave(&tasklist_lock, flags);
5632 do_each_thread(g, t) {
5633 if (start == end) {
5634 ret = -EAGAIN;
5635 goto unlock;
5636 }
5637
5638 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005639 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005640 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005641 t->curr_ret_stack = -1;
5642 /* Make sure the tasks see the -1 first: */
5643 smp_wmb();
5644 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005645 }
5646 } while_each_thread(g, t);
5647
5648unlock:
5649 read_unlock_irqrestore(&tasklist_lock, flags);
5650free:
5651 for (i = start; i < end; i++)
5652 kfree(ret_stack_list[i]);
5653 return ret;
5654}
5655
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005656static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005657ftrace_graph_probe_sched_switch(void *ignore,
5658 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005659{
5660 unsigned long long timestamp;
5661 int index;
5662
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005663 /*
5664 * Does the user want to count the time a function was asleep.
5665 * If so, do not update the time stamps.
5666 */
5667 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5668 return;
5669
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005670 timestamp = trace_clock_local();
5671
5672 prev->ftrace_timestamp = timestamp;
5673
5674 /* only process tasks that we timestamped */
5675 if (!next->ftrace_timestamp)
5676 return;
5677
5678 /*
5679 * Update all the counters in next to make up for the
5680 * time next was sleeping.
5681 */
5682 timestamp -= next->ftrace_timestamp;
5683
5684 for (index = next->curr_ret_stack; index >= 0; index--)
5685 next->ret_stack[index].calltime += timestamp;
5686}
5687
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005688/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005689static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005690{
5691 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005692 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005693
5694 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5695 sizeof(struct ftrace_ret_stack *),
5696 GFP_KERNEL);
5697
5698 if (!ret_stack_list)
5699 return -ENOMEM;
5700
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005701 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005702 for_each_online_cpu(cpu) {
5703 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005704 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005705 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005706
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005707 do {
5708 ret = alloc_retstack_tasklist(ret_stack_list);
5709 } while (ret == -EAGAIN);
5710
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005711 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005712 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005713 if (ret)
5714 pr_info("ftrace_graph: Couldn't activate tracepoint"
5715 " probe to kernel_sched_switch\n");
5716 }
5717
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005718 kfree(ret_stack_list);
5719 return ret;
5720}
5721
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005722/*
5723 * Hibernation protection.
5724 * The state of the current task is too much unstable during
5725 * suspend/restore to disk. We want to protect against that.
5726 */
5727static int
5728ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5729 void *unused)
5730{
5731 switch (state) {
5732 case PM_HIBERNATION_PREPARE:
5733 pause_graph_tracing();
5734 break;
5735
5736 case PM_POST_HIBERNATION:
5737 unpause_graph_tracing();
5738 break;
5739 }
5740 return NOTIFY_DONE;
5741}
5742
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005743static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5744{
5745 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5746 return 0;
5747 return __ftrace_graph_entry(trace);
5748}
5749
5750/*
5751 * The function graph tracer should only trace the functions defined
5752 * by set_ftrace_filter and set_ftrace_notrace. If another function
5753 * tracer ops is registered, the graph tracer requires testing the
5754 * function against the global ops, and not just trace any function
5755 * that any ftrace_ops registered.
5756 */
5757static void update_function_graph_func(void)
5758{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005759 struct ftrace_ops *op;
5760 bool do_test = false;
5761
5762 /*
5763 * The graph and global ops share the same set of functions
5764 * to test. If any other ops is on the list, then
5765 * the graph tracing needs to test if its the function
5766 * it should call.
5767 */
5768 do_for_each_ftrace_op(op, ftrace_ops_list) {
5769 if (op != &global_ops && op != &graph_ops &&
5770 op != &ftrace_list_end) {
5771 do_test = true;
5772 /* in double loop, break out with goto */
5773 goto out;
5774 }
5775 } while_for_each_ftrace_op(op);
5776 out:
5777 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005778 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005779 else
5780 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005781}
5782
Mathias Krause8275f692014-03-30 15:31:50 +02005783static struct notifier_block ftrace_suspend_notifier = {
5784 .notifier_call = ftrace_suspend_notifier_call,
5785};
5786
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005787int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5788 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005789{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005790 int ret = 0;
5791
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005792 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005793
Steven Rostedt05ce5812009-03-24 00:18:31 -04005794 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005795 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005796 ret = -EBUSY;
5797 goto out;
5798 }
5799
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005800 register_pm_notifier(&ftrace_suspend_notifier);
5801
Steven Rostedt597af812009-04-03 15:24:12 -04005802 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005803 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005804 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005805 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005806 goto out;
5807 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005808
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005809 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005810
5811 /*
5812 * Update the indirect function to the entryfunc, and the
5813 * function that gets called to the entry_test first. Then
5814 * call the update fgraph entry function to determine if
5815 * the entryfunc should be called directly or not.
5816 */
5817 __ftrace_graph_entry = entryfunc;
5818 ftrace_graph_entry = ftrace_graph_entry_test;
5819 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005820
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005821 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005822out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005823 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005824 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005825}
5826
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005827void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005828{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005829 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005830
Steven Rostedt597af812009-04-03 15:24:12 -04005831 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005832 goto out;
5833
Steven Rostedt597af812009-04-03 15:24:12 -04005834 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005835 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005836 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005837 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005838 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005839 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005840 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005841
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005842#ifdef CONFIG_DYNAMIC_FTRACE
5843 /*
5844 * Function graph does not allocate the trampoline, but
5845 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5846 * if one was used.
5847 */
5848 global_ops.trampoline = save_global_trampoline;
5849 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5850 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5851#endif
5852
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005853 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005854 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005855}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005856
Steven Rostedt868baf02011-02-10 21:26:13 -05005857static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5858
5859static void
5860graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5861{
5862 atomic_set(&t->tracing_graph_pause, 0);
5863 atomic_set(&t->trace_overrun, 0);
5864 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005865 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005866 smp_wmb();
5867 t->ret_stack = ret_stack;
5868}
5869
5870/*
5871 * Allocate a return stack for the idle task. May be the first
5872 * time through, or it may be done by CPU hotplug online.
5873 */
5874void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5875{
5876 t->curr_ret_stack = -1;
5877 /*
5878 * The idle task has no parent, it either has its own
5879 * stack or no stack at all.
5880 */
5881 if (t->ret_stack)
5882 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5883
5884 if (ftrace_graph_active) {
5885 struct ftrace_ret_stack *ret_stack;
5886
5887 ret_stack = per_cpu(idle_ret_stack, cpu);
5888 if (!ret_stack) {
5889 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5890 * sizeof(struct ftrace_ret_stack),
5891 GFP_KERNEL);
5892 if (!ret_stack)
5893 return;
5894 per_cpu(idle_ret_stack, cpu) = ret_stack;
5895 }
5896 graph_init_task(t, ret_stack);
5897 }
5898}
5899
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005900/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005901void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005902{
Steven Rostedt84047e32009-06-02 16:51:55 -04005903 /* Make sure we do not use the parent ret_stack */
5904 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005905 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005906
Steven Rostedt597af812009-04-03 15:24:12 -04005907 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005908 struct ftrace_ret_stack *ret_stack;
5909
5910 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005911 * sizeof(struct ftrace_ret_stack),
5912 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005913 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005914 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005915 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005916 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005917}
5918
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005919void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005920{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005921 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5922
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005923 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005924 /* NULL must become visible to IRQs before we free it: */
5925 barrier();
5926
5927 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005928}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005929#endif