blob: 4f228024055b119d93279705ec4c42d7475c72f7 [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
Pratyush Anand1619dc32015-03-06 23:58:06 +05301062#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1063static int ftrace_graph_active;
1064#else
1065# define ftrace_graph_active 0
1066#endif
1067
Steven Rostedt3d083392008-05-12 21:20:42 +02001068#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001069
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001070static struct ftrace_ops *removed_ops;
1071
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04001072/*
1073 * Set when doing a global update, like enabling all recs or disabling them.
1074 * It is not set when just updating a single ftrace_ops.
1075 */
1076static bool update_all_ops;
1077
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001078#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001079# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001080#endif
1081
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001082static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1083
Steven Rostedtb6887d72009-02-17 12:32:04 -05001084struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001085 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001086 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001087 unsigned long flags;
1088 unsigned long ip;
1089 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001090 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001091};
1092
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001093struct ftrace_func_entry {
1094 struct hlist_node hlist;
1095 unsigned long ip;
1096};
1097
1098struct ftrace_hash {
1099 unsigned long size_bits;
1100 struct hlist_head *buckets;
1101 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001102 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001103};
1104
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001105/*
1106 * We make these constant because no one should touch them,
1107 * but they are used as the default "empty hash", to avoid allocating
1108 * it all the time. These are in a read only section such that if
1109 * anyone does try to modify it, it will cause an exception.
1110 */
1111static const struct hlist_head empty_buckets[1];
1112static const struct ftrace_hash empty_hash = {
1113 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001114};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001115#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001116
Steven Rostedt2b499382011-05-03 22:49:52 -04001117static struct ftrace_ops global_ops = {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001118 .func = ftrace_stub,
1119 .local_hash.notrace_hash = EMPTY_HASH,
1120 .local_hash.filter_hash = EMPTY_HASH,
1121 INIT_OPS_HASH(global_ops)
1122 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
1123 FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001124};
1125
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001126/*
1127 * This is used by __kernel_text_address() to return true if the
Steven Rostedt (Red Hat)0af26492014-11-20 10:05:36 -05001128 * address is on a dynamically allocated trampoline that would
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05001129 * not return true for either core_kernel_text() or
1130 * is_module_text_address().
1131 */
1132bool is_ftrace_trampoline(unsigned long addr)
1133{
1134 struct ftrace_ops *op;
1135 bool ret = false;
1136
1137 /*
1138 * Some of the ops may be dynamically allocated,
1139 * they are freed after a synchronize_sched().
1140 */
1141 preempt_disable_notrace();
1142
1143 do_for_each_ftrace_op(op, ftrace_ops_list) {
1144 /*
1145 * This is to check for dynamically allocated trampolines.
1146 * Trampolines that are in kernel text will have
1147 * core_kernel_text() return true.
1148 */
1149 if (op->trampoline && op->trampoline_size)
1150 if (addr >= op->trampoline &&
1151 addr < op->trampoline + op->trampoline_size) {
1152 ret = true;
1153 goto out;
1154 }
1155 } while_for_each_ftrace_op(op);
1156
1157 out:
1158 preempt_enable_notrace();
1159
1160 return ret;
1161}
1162
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001163struct ftrace_page {
1164 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001165 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001166 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001167 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001168};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001169
Steven Rostedta7900872011-12-16 16:23:44 -05001170#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1171#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001172
1173/* estimate from running different kernels */
1174#define NR_TO_INIT 10000
1175
1176static struct ftrace_page *ftrace_pages_start;
1177static struct ftrace_page *ftrace_pages;
1178
Steven Rostedt (Red Hat)68f40962014-05-01 12:44:50 -04001179static bool __always_inline ftrace_hash_empty(struct ftrace_hash *hash)
Steven Rostedt06a51d92011-12-19 19:07:36 -05001180{
1181 return !hash || !hash->count;
1182}
1183
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184static struct ftrace_func_entry *
1185ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1186{
1187 unsigned long key;
1188 struct ftrace_func_entry *entry;
1189 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001190
Steven Rostedt06a51d92011-12-19 19:07:36 -05001191 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001192 return NULL;
1193
1194 if (hash->size_bits > 0)
1195 key = hash_long(ip, hash->size_bits);
1196 else
1197 key = 0;
1198
1199 hhd = &hash->buckets[key];
1200
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001201 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001202 if (entry->ip == ip)
1203 return entry;
1204 }
1205 return NULL;
1206}
1207
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001208static void __add_hash_entry(struct ftrace_hash *hash,
1209 struct ftrace_func_entry *entry)
1210{
1211 struct hlist_head *hhd;
1212 unsigned long key;
1213
1214 if (hash->size_bits)
1215 key = hash_long(entry->ip, hash->size_bits);
1216 else
1217 key = 0;
1218
1219 hhd = &hash->buckets[key];
1220 hlist_add_head(&entry->hlist, hhd);
1221 hash->count++;
1222}
1223
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001224static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1225{
1226 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001227
1228 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1229 if (!entry)
1230 return -ENOMEM;
1231
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001232 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001233 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001234
1235 return 0;
1236}
1237
1238static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001239free_hash_entry(struct ftrace_hash *hash,
1240 struct ftrace_func_entry *entry)
1241{
1242 hlist_del(&entry->hlist);
1243 kfree(entry);
1244 hash->count--;
1245}
1246
1247static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001248remove_hash_entry(struct ftrace_hash *hash,
1249 struct ftrace_func_entry *entry)
1250{
1251 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001252 hash->count--;
1253}
1254
1255static void ftrace_hash_clear(struct ftrace_hash *hash)
1256{
1257 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001258 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001259 struct ftrace_func_entry *entry;
1260 int size = 1 << hash->size_bits;
1261 int i;
1262
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001263 if (!hash->count)
1264 return;
1265
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001266 for (i = 0; i < size; i++) {
1267 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001268 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001269 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001270 }
1271 FTRACE_WARN_ON(hash->count);
1272}
1273
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001274static void free_ftrace_hash(struct ftrace_hash *hash)
1275{
1276 if (!hash || hash == EMPTY_HASH)
1277 return;
1278 ftrace_hash_clear(hash);
1279 kfree(hash->buckets);
1280 kfree(hash);
1281}
1282
Steven Rostedt07fd5512011-05-05 18:03:47 -04001283static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1284{
1285 struct ftrace_hash *hash;
1286
1287 hash = container_of(rcu, struct ftrace_hash, rcu);
1288 free_ftrace_hash(hash);
1289}
1290
1291static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1292{
1293 if (!hash || hash == EMPTY_HASH)
1294 return;
1295 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1296}
1297
Jiri Olsa5500fa52012-02-15 15:51:54 +01001298void ftrace_free_filter(struct ftrace_ops *ops)
1299{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001300 ftrace_ops_init(ops);
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001301 free_ftrace_hash(ops->func_hash->filter_hash);
1302 free_ftrace_hash(ops->func_hash->notrace_hash);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001303}
1304
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001305static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1306{
1307 struct ftrace_hash *hash;
1308 int size;
1309
1310 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1311 if (!hash)
1312 return NULL;
1313
1314 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001315 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001316
1317 if (!hash->buckets) {
1318 kfree(hash);
1319 return NULL;
1320 }
1321
1322 hash->size_bits = size_bits;
1323
1324 return hash;
1325}
1326
1327static struct ftrace_hash *
1328alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1329{
1330 struct ftrace_func_entry *entry;
1331 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001332 int size;
1333 int ret;
1334 int i;
1335
1336 new_hash = alloc_ftrace_hash(size_bits);
1337 if (!new_hash)
1338 return NULL;
1339
1340 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001341 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342 return new_hash;
1343
1344 size = 1 << hash->size_bits;
1345 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001346 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001347 ret = add_hash_entry(new_hash, entry->ip);
1348 if (ret < 0)
1349 goto free_hash;
1350 }
1351 }
1352
1353 FTRACE_WARN_ON(new_hash->count != hash->count);
1354
1355 return new_hash;
1356
1357 free_hash:
1358 free_ftrace_hash(new_hash);
1359 return NULL;
1360}
1361
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001362static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001363ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001364static void
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001365ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001366
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001367static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1368 struct ftrace_hash *new_hash);
1369
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001370static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001371ftrace_hash_move(struct ftrace_ops *ops, int enable,
1372 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001373{
1374 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001375 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001376 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001377 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001378 int size = src->count;
1379 int bits = 0;
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001380 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001381 int i;
1382
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001383 /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1384 if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1385 return -EINVAL;
1386
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001387 /*
1388 * If the new source is empty, just free dst and assign it
1389 * the empty_hash.
1390 */
1391 if (!src->count) {
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001392 new_hash = EMPTY_HASH;
1393 goto update;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001394 }
1395
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001396 /*
1397 * Make the hash size about 1/2 the # found
1398 */
1399 for (size /= 2; size; size >>= 1)
1400 bits++;
1401
1402 /* Don't allocate too much */
1403 if (bits > FTRACE_HASH_MAX_BITS)
1404 bits = FTRACE_HASH_MAX_BITS;
1405
Steven Rostedt07fd5512011-05-05 18:03:47 -04001406 new_hash = alloc_ftrace_hash(bits);
1407 if (!new_hash)
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001408 return -ENOMEM;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001409
1410 size = 1 << src->size_bits;
1411 for (i = 0; i < size; i++) {
1412 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001413 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001414 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001415 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001416 }
1417 }
1418
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001419update:
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001420 /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1421 if (enable) {
1422 /* IPMODIFY should be updated only when filter_hash updating */
1423 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1424 if (ret < 0) {
1425 free_ftrace_hash(new_hash);
1426 return ret;
1427 }
1428 }
1429
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001430 /*
1431 * Remove the current set, update the hash and add
1432 * them back.
1433 */
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001434 ftrace_hash_rec_disable_modify(ops, enable);
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001435
Steven Rostedt07fd5512011-05-05 18:03:47 -04001436 rcu_assign_pointer(*dst, new_hash);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001437
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001438 ftrace_hash_rec_enable_modify(ops, enable);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001439
Masami Hiramatsu5c27c772014-06-17 11:04:42 +00001440 return 0;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001441}
1442
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001443static bool hash_contains_ip(unsigned long ip,
1444 struct ftrace_ops_hash *hash)
1445{
1446 /*
1447 * The function record is a match if it exists in the filter
1448 * hash and not in the notrace hash. Note, an emty hash is
1449 * considered a match for the filter hash, but an empty
1450 * notrace hash is considered not in the notrace hash.
1451 */
1452 return (ftrace_hash_empty(hash->filter_hash) ||
1453 ftrace_lookup_ip(hash->filter_hash, ip)) &&
1454 (ftrace_hash_empty(hash->notrace_hash) ||
1455 !ftrace_lookup_ip(hash->notrace_hash, ip));
1456}
1457
Steven Rostedt265c8312009-02-13 12:43:56 -05001458/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001459 * Test the hashes for this ops to see if we want to call
1460 * the ops->func or not.
1461 *
1462 * It's a match if the ip is in the ops->filter_hash or
1463 * the filter_hash does not exist or is empty,
1464 * AND
1465 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001466 *
1467 * This needs to be called with preemption disabled as
1468 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001469 */
1470static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001471ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001472{
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001473 struct ftrace_ops_hash hash;
Steven Rostedtb8489142011-05-04 09:27:52 -04001474 int ret;
1475
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001476#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1477 /*
1478 * There's a small race when adding ops that the ftrace handler
1479 * that wants regs, may be called without them. We can not
1480 * allow that handler to be called if regs is NULL.
1481 */
1482 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1483 return 0;
1484#endif
1485
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001486 hash.filter_hash = rcu_dereference_raw_notrace(ops->func_hash->filter_hash);
1487 hash.notrace_hash = rcu_dereference_raw_notrace(ops->func_hash->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001488
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001489 if (hash_contains_ip(ip, &hash))
Steven Rostedtb8489142011-05-04 09:27:52 -04001490 ret = 1;
1491 else
1492 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001493
1494 return ret;
1495}
1496
1497/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001498 * This is a double for. Do not use 'break' to break out of the loop,
1499 * you must use a goto.
1500 */
1501#define do_for_each_ftrace_rec(pg, rec) \
1502 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1503 int _____i; \
1504 for (_____i = 0; _____i < pg->index; _____i++) { \
1505 rec = &pg->records[_____i];
1506
1507#define while_for_each_ftrace_rec() \
1508 } \
1509 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301510
Steven Rostedt5855fea2011-12-16 19:27:42 -05001511
1512static int ftrace_cmp_recs(const void *a, const void *b)
1513{
Steven Rostedta650e022012-04-25 13:48:13 -04001514 const struct dyn_ftrace *key = a;
1515 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001516
Steven Rostedta650e022012-04-25 13:48:13 -04001517 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001518 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001519 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1520 return 1;
1521 return 0;
1522}
1523
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001524static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001525{
1526 struct ftrace_page *pg;
1527 struct dyn_ftrace *rec;
1528 struct dyn_ftrace key;
1529
1530 key.ip = start;
1531 key.flags = end; /* overload flags, as it is unsigned long */
1532
1533 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1534 if (end < pg->records[0].ip ||
1535 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1536 continue;
1537 rec = bsearch(&key, pg->records, pg->index,
1538 sizeof(struct dyn_ftrace),
1539 ftrace_cmp_recs);
1540 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001541 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001542 }
1543
Steven Rostedt5855fea2011-12-16 19:27:42 -05001544 return 0;
1545}
1546
Steven Rostedtc88fd862011-08-16 09:53:39 -04001547/**
1548 * ftrace_location - return true if the ip giving is a traced location
1549 * @ip: the instruction pointer to check
1550 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001551 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001552 * That is, the instruction that is either a NOP or call to
1553 * the function tracer. It checks the ftrace internal tables to
1554 * determine if the address belongs or not.
1555 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001556unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001557{
Steven Rostedta650e022012-04-25 13:48:13 -04001558 return ftrace_location_range(ip, ip);
1559}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001560
Steven Rostedta650e022012-04-25 13:48:13 -04001561/**
1562 * ftrace_text_reserved - return true if range contains an ftrace location
1563 * @start: start of range to search
1564 * @end: end of range to search (inclusive). @end points to the last byte to check.
1565 *
1566 * Returns 1 if @start and @end contains a ftrace location.
1567 * That is, the instruction that is either a NOP or call to
1568 * the function tracer. It checks the ftrace internal tables to
1569 * determine if the address belongs or not.
1570 */
Sasha Levind88471c2013-01-09 18:09:20 -05001571int ftrace_text_reserved(const void *start, const void *end)
Steven Rostedta650e022012-04-25 13:48:13 -04001572{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001573 unsigned long ret;
1574
1575 ret = ftrace_location_range((unsigned long)start,
1576 (unsigned long)end);
1577
1578 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001579}
1580
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001581/* Test if ops registered to this rec needs regs */
1582static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1583{
1584 struct ftrace_ops *ops;
1585 bool keep_regs = false;
1586
1587 for (ops = ftrace_ops_list;
1588 ops != &ftrace_list_end; ops = ops->next) {
1589 /* pass rec in as regs to have non-NULL val */
1590 if (ftrace_ops_test(ops, rec->ip, rec)) {
1591 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1592 keep_regs = true;
1593 break;
1594 }
1595 }
1596 }
1597
1598 return keep_regs;
1599}
1600
Steven Rostedted926f92011-05-03 13:25:24 -04001601static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1602 int filter_hash,
1603 bool inc)
1604{
1605 struct ftrace_hash *hash;
1606 struct ftrace_hash *other_hash;
1607 struct ftrace_page *pg;
1608 struct dyn_ftrace *rec;
1609 int count = 0;
1610 int all = 0;
1611
1612 /* Only update if the ops has been registered */
1613 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1614 return;
1615
1616 /*
1617 * In the filter_hash case:
1618 * If the count is zero, we update all records.
1619 * Otherwise we just update the items in the hash.
1620 *
1621 * In the notrace_hash case:
1622 * We enable the update in the hash.
1623 * As disabling notrace means enabling the tracing,
1624 * and enabling notrace means disabling, the inc variable
1625 * gets inversed.
1626 */
1627 if (filter_hash) {
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001628 hash = ops->func_hash->filter_hash;
1629 other_hash = ops->func_hash->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001630 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001631 all = 1;
1632 } else {
1633 inc = !inc;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04001634 hash = ops->func_hash->notrace_hash;
1635 other_hash = ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04001636 /*
1637 * If the notrace hash has no items,
1638 * then there's nothing to do.
1639 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001640 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001641 return;
1642 }
1643
1644 do_for_each_ftrace_rec(pg, rec) {
1645 int in_other_hash = 0;
1646 int in_hash = 0;
1647 int match = 0;
1648
1649 if (all) {
1650 /*
1651 * Only the filter_hash affects all records.
1652 * Update if the record is not in the notrace hash.
1653 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001654 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001655 match = 1;
1656 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001657 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1658 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001659
1660 /*
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001661 * If filter_hash is set, we want to match all functions
1662 * that are in the hash but not in the other hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001663 *
Steven Rostedt (Red Hat)19eab4a2014-05-07 15:06:14 -04001664 * If filter_hash is not set, then we are decrementing.
1665 * That means we match anything that is in the hash
1666 * and also in the other_hash. That is, we need to turn
1667 * off functions in the other hash because they are disabled
1668 * by this hash.
Steven Rostedted926f92011-05-03 13:25:24 -04001669 */
1670 if (filter_hash && in_hash && !in_other_hash)
1671 match = 1;
1672 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001673 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001674 match = 1;
1675 }
1676 if (!match)
1677 continue;
1678
1679 if (inc) {
1680 rec->flags++;
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001681 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
Steven Rostedted926f92011-05-03 13:25:24 -04001682 return;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001683
1684 /*
1685 * If there's only a single callback registered to a
1686 * function, and the ops has a trampoline registered
1687 * for it, then we can call it directly.
1688 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001689 if (ftrace_rec_count(rec) == 1 && ops->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001690 rec->flags |= FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001691 else
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001692 /*
1693 * If we are adding another function callback
1694 * to this function, and the previous had a
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04001695 * custom trampoline in use, then we need to go
1696 * back to the default trampoline.
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001697 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001698 rec->flags &= ~FTRACE_FL_TRAMP;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001699
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001700 /*
1701 * If any ops wants regs saved for this function
1702 * then all ops will get saved regs.
1703 */
1704 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1705 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001706 } else {
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001707 if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
Steven Rostedted926f92011-05-03 13:25:24 -04001708 return;
1709 rec->flags--;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001710
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001711 /*
1712 * If the rec had REGS enabled and the ops that is
1713 * being removed had REGS set, then see if there is
1714 * still any ops for this record that wants regs.
1715 * If not, we can stop recording them.
1716 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001717 if (ftrace_rec_count(rec) > 0 &&
Steven Rostedt (Red Hat)4fbb48c2014-04-30 22:35:48 -04001718 rec->flags & FTRACE_FL_REGS &&
1719 ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1720 if (!test_rec_ops_needs_regs(rec))
1721 rec->flags &= ~FTRACE_FL_REGS;
1722 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001723
1724 /*
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04001725 * If the rec had TRAMP enabled, then it needs to
1726 * be cleared. As TRAMP can only be enabled iff
1727 * there is only a single ops attached to it.
1728 * In otherwords, always disable it on decrementing.
1729 * In the future, we may set it if rec count is
1730 * decremented to one, and the ops that is left
1731 * has a trampoline.
1732 */
1733 rec->flags &= ~FTRACE_FL_TRAMP;
1734
1735 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001736 * flags will be cleared in ftrace_check_record()
1737 * if rec count is zero.
1738 */
Steven Rostedted926f92011-05-03 13:25:24 -04001739 }
1740 count++;
1741 /* Shortcut, if we handled all records, we are done. */
1742 if (!all && count == hash->count)
1743 return;
1744 } while_for_each_ftrace_rec();
1745}
1746
1747static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1748 int filter_hash)
1749{
1750 __ftrace_hash_rec_update(ops, filter_hash, 0);
1751}
1752
1753static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1754 int filter_hash)
1755{
1756 __ftrace_hash_rec_update(ops, filter_hash, 1);
1757}
1758
Steven Rostedt (Red Hat)84261912014-08-18 13:21:08 -04001759static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1760 int filter_hash, int inc)
1761{
1762 struct ftrace_ops *op;
1763
1764 __ftrace_hash_rec_update(ops, filter_hash, inc);
1765
1766 if (ops->func_hash != &global_ops.local_hash)
1767 return;
1768
1769 /*
1770 * If the ops shares the global_ops hash, then we need to update
1771 * all ops that are enabled and use this hash.
1772 */
1773 do_for_each_ftrace_op(op, ftrace_ops_list) {
1774 /* Already done */
1775 if (op == ops)
1776 continue;
1777 if (op->func_hash == &global_ops.local_hash)
1778 __ftrace_hash_rec_update(op, filter_hash, inc);
1779 } while_for_each_ftrace_op(op);
1780}
1781
1782static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1783 int filter_hash)
1784{
1785 ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1786}
1787
1788static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1789 int filter_hash)
1790{
1791 ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1792}
1793
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05001794/*
1795 * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1796 * or no-needed to update, -EBUSY if it detects a conflict of the flag
1797 * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1798 * Note that old_hash and new_hash has below meanings
1799 * - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1800 * - If the hash is EMPTY_HASH, it hits nothing
1801 * - Anything else hits the recs which match the hash entries.
1802 */
1803static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1804 struct ftrace_hash *old_hash,
1805 struct ftrace_hash *new_hash)
1806{
1807 struct ftrace_page *pg;
1808 struct dyn_ftrace *rec, *end = NULL;
1809 int in_old, in_new;
1810
1811 /* Only update if the ops has been registered */
1812 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1813 return 0;
1814
1815 if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1816 return 0;
1817
1818 /*
1819 * Since the IPMODIFY is a very address sensitive action, we do not
1820 * allow ftrace_ops to set all functions to new hash.
1821 */
1822 if (!new_hash || !old_hash)
1823 return -EINVAL;
1824
1825 /* Update rec->flags */
1826 do_for_each_ftrace_rec(pg, rec) {
1827 /* We need to update only differences of filter_hash */
1828 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1829 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1830 if (in_old == in_new)
1831 continue;
1832
1833 if (in_new) {
1834 /* New entries must ensure no others are using it */
1835 if (rec->flags & FTRACE_FL_IPMODIFY)
1836 goto rollback;
1837 rec->flags |= FTRACE_FL_IPMODIFY;
1838 } else /* Removed entry */
1839 rec->flags &= ~FTRACE_FL_IPMODIFY;
1840 } while_for_each_ftrace_rec();
1841
1842 return 0;
1843
1844rollback:
1845 end = rec;
1846
1847 /* Roll back what we did above */
1848 do_for_each_ftrace_rec(pg, rec) {
1849 if (rec == end)
1850 goto err_out;
1851
1852 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1853 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1854 if (in_old == in_new)
1855 continue;
1856
1857 if (in_new)
1858 rec->flags &= ~FTRACE_FL_IPMODIFY;
1859 else
1860 rec->flags |= FTRACE_FL_IPMODIFY;
1861 } while_for_each_ftrace_rec();
1862
1863err_out:
1864 return -EBUSY;
1865}
1866
1867static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1868{
1869 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1870
1871 if (ftrace_hash_empty(hash))
1872 hash = NULL;
1873
1874 return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1875}
1876
1877/* Disabling always succeeds */
1878static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1879{
1880 struct ftrace_hash *hash = ops->func_hash->filter_hash;
1881
1882 if (ftrace_hash_empty(hash))
1883 hash = NULL;
1884
1885 __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1886}
1887
1888static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1889 struct ftrace_hash *new_hash)
1890{
1891 struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1892
1893 if (ftrace_hash_empty(old_hash))
1894 old_hash = NULL;
1895
1896 if (ftrace_hash_empty(new_hash))
1897 new_hash = NULL;
1898
1899 return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1900}
1901
Steven Rostedt05736a42008-09-22 14:55:47 -07001902static void print_ip_ins(const char *fmt, unsigned char *p)
1903{
1904 int i;
1905
1906 printk(KERN_CONT "%s", fmt);
1907
1908 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1909 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1910}
1911
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001912static struct ftrace_ops *
1913ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1914
Steven Rostedtc88fd862011-08-16 09:53:39 -04001915/**
1916 * ftrace_bug - report and shutdown function tracer
1917 * @failed: The failed type (EFAULT, EINVAL, EPERM)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001918 * @rec: The record that failed
Steven Rostedtc88fd862011-08-16 09:53:39 -04001919 *
1920 * The arch code that enables or disables the function tracing
1921 * can call ftrace_bug() when it has detected a problem in
1922 * modifying the code. @failed should be one of either:
1923 * EFAULT - if the problem happens on reading the @ip address
1924 * EINVAL - if what is read at @ip is not what was expected
1925 * EPERM - if the problem happens on writting to the @ip address
1926 */
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001927void ftrace_bug(int failed, struct dyn_ftrace *rec)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001928{
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001929 unsigned long ip = rec ? rec->ip : 0;
1930
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001931 switch (failed) {
1932 case -EFAULT:
1933 FTRACE_WARN_ON_ONCE(1);
1934 pr_info("ftrace faulted on modifying ");
1935 print_ip_sym(ip);
1936 break;
1937 case -EINVAL:
1938 FTRACE_WARN_ON_ONCE(1);
1939 pr_info("ftrace failed to modify ");
1940 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001941 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001942 pr_cont("\n");
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001943 break;
1944 case -EPERM:
1945 FTRACE_WARN_ON_ONCE(1);
1946 pr_info("ftrace faulted on writing ");
1947 print_ip_sym(ip);
1948 break;
1949 default:
1950 FTRACE_WARN_ON_ONCE(1);
1951 pr_info("ftrace faulted on unknown error ");
1952 print_ip_sym(ip);
1953 }
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04001954 if (rec) {
1955 struct ftrace_ops *ops = NULL;
1956
1957 pr_info("ftrace record flags: %lx\n", rec->flags);
1958 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
1959 rec->flags & FTRACE_FL_REGS ? " R" : " ");
1960 if (rec->flags & FTRACE_FL_TRAMP_EN) {
1961 ops = ftrace_find_tramp_ops_any(rec);
1962 if (ops)
1963 pr_cont("\ttramp: %pS",
1964 (void *)ops->trampoline);
1965 else
1966 pr_cont("\ttramp: ERROR!");
1967
1968 }
1969 ip = ftrace_get_addr_curr(rec);
1970 pr_cont(" expected tramp: %lx\n", ip);
1971 }
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001972}
1973
Steven Rostedtc88fd862011-08-16 09:53:39 -04001974static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001975{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001976 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001977
Steven Rostedt982c3502008-11-15 16:31:41 -05001978 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001979 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001980 *
Steven Rostedted926f92011-05-03 13:25:24 -04001981 * If the record has a ref count, then we need to enable it
1982 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001983 *
Steven Rostedted926f92011-05-03 13:25:24 -04001984 * Otherwise we make sure its disabled.
1985 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001986 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001987 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001988 */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04001989 if (enable && ftrace_rec_count(rec))
Steven Rostedted926f92011-05-03 13:25:24 -04001990 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001991
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001992 /*
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001993 * If enabling and the REGS flag does not match the REGS_EN, or
1994 * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
1995 * this record. Set flags to fail the compare against ENABLED.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001996 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04001997 if (flag) {
1998 if (!(rec->flags & FTRACE_FL_REGS) !=
1999 !(rec->flags & FTRACE_FL_REGS_EN))
2000 flag |= FTRACE_FL_REGS;
2001
2002 if (!(rec->flags & FTRACE_FL_TRAMP) !=
2003 !(rec->flags & FTRACE_FL_TRAMP_EN))
2004 flag |= FTRACE_FL_TRAMP;
2005 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002006
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002007 /* If the state of this record hasn't changed, then do nothing */
2008 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04002009 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002010
2011 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002012 /* Save off if rec is being enabled (for return value) */
2013 flag ^= rec->flags & FTRACE_FL_ENABLED;
2014
2015 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04002016 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002017 if (flag & FTRACE_FL_REGS) {
2018 if (rec->flags & FTRACE_FL_REGS)
2019 rec->flags |= FTRACE_FL_REGS_EN;
2020 else
2021 rec->flags &= ~FTRACE_FL_REGS_EN;
2022 }
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002023 if (flag & FTRACE_FL_TRAMP) {
2024 if (rec->flags & FTRACE_FL_TRAMP)
2025 rec->flags |= FTRACE_FL_TRAMP_EN;
2026 else
2027 rec->flags &= ~FTRACE_FL_TRAMP_EN;
2028 }
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002029 }
2030
2031 /*
2032 * If this record is being updated from a nop, then
2033 * return UPDATE_MAKE_CALL.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002034 * Otherwise,
2035 * return UPDATE_MODIFY_CALL to tell the caller to convert
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002036 * from the save regs, to a non-save regs function or
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002037 * vice versa, or from a trampoline call.
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002038 */
2039 if (flag & FTRACE_FL_ENABLED)
2040 return FTRACE_UPDATE_MAKE_CALL;
Steven Rostedt (Red Hat)f1b2f2b2014-05-07 16:09:49 -04002041
2042 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08002043 }
2044
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002045 if (update) {
2046 /* If there's no more users, clear all flags */
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04002047 if (!ftrace_rec_count(rec))
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002048 rec->flags = 0;
2049 else
Steven Rostedt (Red Hat)b24d4432015-03-04 23:10:28 -05002050 /*
2051 * Just disable the record, but keep the ops TRAMP
2052 * and REGS states. The _EN flags must be disabled though.
2053 */
2054 rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2055 FTRACE_FL_REGS_EN);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002056 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04002057
2058 return FTRACE_UPDATE_MAKE_NOP;
2059}
2060
2061/**
2062 * ftrace_update_record, set a record that now is tracing or not
2063 * @rec: the record to update
2064 * @enable: set to 1 if the record is tracing, zero to force disable
2065 *
2066 * The records that represent all functions that can be traced need
2067 * to be updated when tracing has been enabled.
2068 */
2069int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2070{
2071 return ftrace_check_record(rec, enable, 1);
2072}
2073
2074/**
2075 * ftrace_test_record, check if the record has been enabled or not
2076 * @rec: the record to test
2077 * @enable: set to 1 to check if enabled, 0 if it is disabled
2078 *
2079 * The arch code may need to test if a record is already set to
2080 * tracing to determine how to modify the function code that it
2081 * represents.
2082 */
2083int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2084{
2085 return ftrace_check_record(rec, enable, 0);
2086}
2087
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002088static struct ftrace_ops *
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002089ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2090{
2091 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002092 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002093
2094 do_for_each_ftrace_op(op, ftrace_ops_list) {
2095
2096 if (!op->trampoline)
2097 continue;
2098
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002099 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04002100 return op;
2101 } while_for_each_ftrace_op(op);
2102
2103 return NULL;
2104}
2105
2106static struct ftrace_ops *
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002107ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2108{
2109 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002110 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002111
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002112 /*
2113 * Need to check removed ops first.
2114 * If they are being removed, and this rec has a tramp,
2115 * and this rec is in the ops list, then it would be the
2116 * one with the tramp.
2117 */
2118 if (removed_ops) {
2119 if (hash_contains_ip(ip, &removed_ops->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002120 return removed_ops;
2121 }
2122
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002123 /*
2124 * Need to find the current trampoline for a rec.
2125 * Now, a trampoline is only attached to a rec if there
2126 * was a single 'ops' attached to it. But this can be called
2127 * when we are adding another op to the rec or removing the
2128 * current one. Thus, if the op is being added, we can
2129 * ignore it because it hasn't attached itself to the rec
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002130 * yet.
2131 *
2132 * If an ops is being modified (hooking to different functions)
2133 * then we don't care about the new functions that are being
2134 * added, just the old ones (that are probably being removed).
2135 *
2136 * If we are adding an ops to a function that already is using
2137 * a trampoline, it needs to be removed (trampolines are only
2138 * for single ops connected), then an ops that is not being
2139 * modified also needs to be checked.
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002140 */
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002141 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002142
2143 if (!op->trampoline)
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002144 continue;
2145
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002146 /*
2147 * If the ops is being added, it hasn't gotten to
2148 * the point to be removed from this tree yet.
2149 */
2150 if (op->flags & FTRACE_OPS_FL_ADDING)
2151 continue;
2152
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002153
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002154 /*
2155 * If the ops is being modified and is in the old
2156 * hash, then it is probably being removed from this
2157 * function.
2158 */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002159 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2160 hash_contains_ip(ip, &op->old_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002161 return op;
Steven Rostedt (Red Hat)4fc40902014-10-24 14:48:35 -04002162 /*
2163 * If the ops is not being added or modified, and it's
2164 * in its normal filter hash, then this must be the one
2165 * we want!
2166 */
2167 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2168 hash_contains_ip(ip, op->func_hash))
2169 return op;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002170
2171 } while_for_each_ftrace_op(op);
2172
2173 return NULL;
2174}
2175
2176static struct ftrace_ops *
2177ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2178{
2179 struct ftrace_ops *op;
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002180 unsigned long ip = rec->ip;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002181
2182 do_for_each_ftrace_op(op, ftrace_ops_list) {
2183 /* pass rec in as regs to have non-NULL val */
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002184 if (hash_contains_ip(ip, op->func_hash))
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002185 return op;
2186 } while_for_each_ftrace_op(op);
2187
2188 return NULL;
2189}
2190
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002191/**
2192 * ftrace_get_addr_new - Get the call address to set to
2193 * @rec: The ftrace record descriptor
2194 *
2195 * If the record has the FTRACE_FL_REGS set, that means that it
2196 * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2197 * is not not set, then it wants to convert to the normal callback.
2198 *
2199 * Returns the address of the trampoline to set to
2200 */
2201unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2202{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002203 struct ftrace_ops *ops;
2204
2205 /* Trampolines take precedence over regs */
2206 if (rec->flags & FTRACE_FL_TRAMP) {
2207 ops = ftrace_find_tramp_ops_new(rec);
2208 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
Steven Rostedt (Red Hat)bce0b6c2014-08-20 23:57:04 -04002209 pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2210 (void *)rec->ip, (void *)rec->ip, rec->flags);
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002211 /* Ftrace is shutting down, return anything */
2212 return (unsigned long)FTRACE_ADDR;
2213 }
2214 return ops->trampoline;
2215 }
2216
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002217 if (rec->flags & FTRACE_FL_REGS)
2218 return (unsigned long)FTRACE_REGS_ADDR;
2219 else
2220 return (unsigned long)FTRACE_ADDR;
2221}
2222
2223/**
2224 * ftrace_get_addr_curr - Get the call address that is already there
2225 * @rec: The ftrace record descriptor
2226 *
2227 * The FTRACE_FL_REGS_EN is set when the record already points to
2228 * a function that saves all the regs. Basically the '_EN' version
2229 * represents the current state of the function.
2230 *
2231 * Returns the address of the trampoline that is currently being called
2232 */
2233unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2234{
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002235 struct ftrace_ops *ops;
2236
2237 /* Trampolines take precedence over regs */
2238 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2239 ops = ftrace_find_tramp_ops_curr(rec);
2240 if (FTRACE_WARN_ON(!ops)) {
2241 pr_warning("Bad trampoline accounting at: %p (%pS)\n",
2242 (void *)rec->ip, (void *)rec->ip);
2243 /* Ftrace is shutting down, return anything */
2244 return (unsigned long)FTRACE_ADDR;
2245 }
2246 return ops->trampoline;
2247 }
2248
Steven Rostedt (Red Hat)7413af12014-05-06 21:34:14 -04002249 if (rec->flags & FTRACE_FL_REGS_EN)
2250 return (unsigned long)FTRACE_REGS_ADDR;
2251 else
2252 return (unsigned long)FTRACE_ADDR;
2253}
2254
Steven Rostedtc88fd862011-08-16 09:53:39 -04002255static int
2256__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2257{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002258 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002259 unsigned long ftrace_addr;
2260 int ret;
2261
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002262 ftrace_addr = ftrace_get_addr_new(rec);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002263
Steven Rostedt (Red Hat)7c0868e2014-05-08 07:01:21 -04002264 /* This needs to be done before we call ftrace_update_record */
2265 ftrace_old_addr = ftrace_get_addr_curr(rec);
2266
2267 ret = ftrace_update_record(rec, enable);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002268
Steven Rostedtc88fd862011-08-16 09:53:39 -04002269 switch (ret) {
2270 case FTRACE_UPDATE_IGNORE:
2271 return 0;
2272
2273 case FTRACE_UPDATE_MAKE_CALL:
2274 return ftrace_make_call(rec, ftrace_addr);
2275
2276 case FTRACE_UPDATE_MAKE_NOP:
Steven Rostedt (Red Hat)39b55522014-08-17 20:59:10 -04002277 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002278
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002279 case FTRACE_UPDATE_MODIFY_CALL:
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002280 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04002281 }
2282
2283 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02002284}
2285
Steven Rostedte4f5d542012-04-27 09:13:18 -04002286void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002287{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002288 struct dyn_ftrace *rec;
2289 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002290 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002291
Steven Rostedt45a4a232011-04-21 23:16:46 -04002292 if (unlikely(ftrace_disabled))
2293 return;
2294
Steven Rostedt265c8312009-02-13 12:43:56 -05002295 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04002296 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08002297 if (failed) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002298 ftrace_bug(failed, rec);
Steven Rostedt3279ba32009-10-07 16:57:56 -04002299 /* Stop processing */
2300 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05002301 }
2302 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002303}
2304
Steven Rostedtc88fd862011-08-16 09:53:39 -04002305struct ftrace_rec_iter {
2306 struct ftrace_page *pg;
2307 int index;
2308};
2309
2310/**
2311 * ftrace_rec_iter_start, start up iterating over traced functions
2312 *
2313 * Returns an iterator handle that is used to iterate over all
2314 * the records that represent address locations where functions
2315 * are traced.
2316 *
2317 * May return NULL if no records are available.
2318 */
2319struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2320{
2321 /*
2322 * We only use a single iterator.
2323 * Protected by the ftrace_lock mutex.
2324 */
2325 static struct ftrace_rec_iter ftrace_rec_iter;
2326 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2327
2328 iter->pg = ftrace_pages_start;
2329 iter->index = 0;
2330
2331 /* Could have empty pages */
2332 while (iter->pg && !iter->pg->index)
2333 iter->pg = iter->pg->next;
2334
2335 if (!iter->pg)
2336 return NULL;
2337
2338 return iter;
2339}
2340
2341/**
2342 * ftrace_rec_iter_next, get the next record to process.
2343 * @iter: The handle to the iterator.
2344 *
2345 * Returns the next iterator after the given iterator @iter.
2346 */
2347struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2348{
2349 iter->index++;
2350
2351 if (iter->index >= iter->pg->index) {
2352 iter->pg = iter->pg->next;
2353 iter->index = 0;
2354
2355 /* Could have empty pages */
2356 while (iter->pg && !iter->pg->index)
2357 iter->pg = iter->pg->next;
2358 }
2359
2360 if (!iter->pg)
2361 return NULL;
2362
2363 return iter;
2364}
2365
2366/**
2367 * ftrace_rec_iter_record, get the record at the iterator location
2368 * @iter: The current iterator location
2369 *
2370 * Returns the record that the current @iter is at.
2371 */
2372struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2373{
2374 return &iter->pg->records[iter->index];
2375}
2376
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302377static int
Steven Rostedt31e88902008-11-14 16:21:19 -08002378ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002379{
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002380 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002381
Steven Rostedt45a4a232011-04-21 23:16:46 -04002382 if (unlikely(ftrace_disabled))
2383 return 0;
2384
Shaohua Li25aac9d2009-01-09 11:29:40 +08002385 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04002386 if (ret) {
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002387 ftrace_bug(ret, rec);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302388 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02002389 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05302390 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002391}
2392
Steven Rostedt000ab692009-02-17 13:35:06 -05002393/*
2394 * archs can override this function if they must do something
2395 * before the modifying code is performed.
2396 */
2397int __weak ftrace_arch_code_modify_prepare(void)
2398{
2399 return 0;
2400}
2401
2402/*
2403 * archs can override this function if they must do something
2404 * after the modifying code is performed.
2405 */
2406int __weak ftrace_arch_code_modify_post_process(void)
2407{
2408 return 0;
2409}
2410
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002411void ftrace_modify_all_code(int command)
2412{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002413 int update = command & FTRACE_UPDATE_TRACE_FUNC;
Petr Mladekcd210672014-02-24 17:12:21 +01002414 int err = 0;
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002415
2416 /*
2417 * If the ftrace_caller calls a ftrace_ops func directly,
2418 * we need to make sure that it only traces functions it
2419 * expects to trace. When doing the switch of functions,
2420 * we need to update to the ftrace_ops_list_func first
2421 * before the transition between old and new calls are set,
2422 * as the ftrace_ops_list_func will check the ops hashes
2423 * to make sure the ops are having the right functions
2424 * traced.
2425 */
Petr Mladekcd210672014-02-24 17:12:21 +01002426 if (update) {
2427 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2428 if (FTRACE_WARN_ON(err))
2429 return;
2430 }
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04002431
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002432 if (command & FTRACE_UPDATE_CALLS)
2433 ftrace_replace_code(1);
2434 else if (command & FTRACE_DISABLE_CALLS)
2435 ftrace_replace_code(0);
2436
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002437 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2438 function_trace_op = set_function_trace_op;
2439 smp_wmb();
2440 /* If irqs are disabled, we are in stop machine */
2441 if (!irqs_disabled())
2442 smp_call_function(ftrace_sync_ipi, NULL, 1);
Petr Mladekcd210672014-02-24 17:12:21 +01002443 err = ftrace_update_ftrace_func(ftrace_trace_function);
2444 if (FTRACE_WARN_ON(err))
2445 return;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002446 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002447
2448 if (command & FTRACE_START_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002449 err = ftrace_enable_ftrace_graph_caller();
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002450 else if (command & FTRACE_STOP_FUNC_RET)
Petr Mladekcd210672014-02-24 17:12:21 +01002451 err = ftrace_disable_ftrace_graph_caller();
2452 FTRACE_WARN_ON(err);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002453}
2454
Ingo Molnare309b412008-05-12 21:20:51 +02002455static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002456{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002457 int *command = data;
2458
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002459 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002460
Steven Rostedtc88fd862011-08-16 09:53:39 -04002461 return 0;
2462}
2463
2464/**
2465 * ftrace_run_stop_machine, go back to the stop machine method
2466 * @command: The command to tell ftrace what to do
2467 *
2468 * If an arch needs to fall back to the stop machine method, the
2469 * it can call this function.
2470 */
2471void ftrace_run_stop_machine(int command)
2472{
2473 stop_machine(__ftrace_modify_code, &command, NULL);
2474}
2475
2476/**
2477 * arch_ftrace_update_code, modify the code to trace or not trace
2478 * @command: The command that needs to be done
2479 *
2480 * Archs can override this function if it does not need to
2481 * run stop_machine() to modify code.
2482 */
2483void __weak arch_ftrace_update_code(int command)
2484{
2485 ftrace_run_stop_machine(command);
2486}
2487
2488static void ftrace_run_update_code(int command)
2489{
2490 int ret;
2491
2492 ret = ftrace_arch_code_modify_prepare();
2493 FTRACE_WARN_ON(ret);
2494 if (ret)
2495 return;
Steven Rostedtc88fd862011-08-16 09:53:39 -04002496
2497 /*
2498 * By default we use stop_machine() to modify the code.
2499 * But archs can do what ever they want as long as it
2500 * is safe. The stop_machine() is the safest, but also
2501 * produces the most overhead.
2502 */
2503 arch_ftrace_update_code(command);
2504
Steven Rostedt000ab692009-02-17 13:35:06 -05002505 ret = ftrace_arch_code_modify_post_process();
2506 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002507}
2508
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002509static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002510 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002511{
2512 ops->flags |= FTRACE_OPS_FL_MODIFYING;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002513 ops->old_hash.filter_hash = old_hash->filter_hash;
2514 ops->old_hash.notrace_hash = old_hash->notrace_hash;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002515 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04002516 ops->old_hash.filter_hash = NULL;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05002517 ops->old_hash.notrace_hash = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002518 ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2519}
2520
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002521static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002522static int ftrace_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002523
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002524void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2525{
2526}
2527
Jiri Slabydb0fbad2014-03-10 21:42:11 +01002528static void control_ops_free(struct ftrace_ops *ops)
2529{
2530 free_percpu(ops->disabled);
2531}
2532
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002533static void ftrace_startup_enable(int command)
2534{
2535 if (saved_ftrace_func != ftrace_trace_function) {
2536 saved_ftrace_func = ftrace_trace_function;
2537 command |= FTRACE_UPDATE_TRACE_FUNC;
2538 }
2539
2540 if (!command || !ftrace_enabled)
2541 return;
2542
2543 ftrace_run_update_code(command);
2544}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002545
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002546static void ftrace_startup_all(int command)
2547{
2548 update_all_ops = true;
2549 ftrace_startup_enable(command);
2550 update_all_ops = false;
2551}
2552
Steven Rostedta1cd6172011-05-23 15:24:25 -04002553static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002554{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002555 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002556
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002557 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002558 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002559
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002560 ret = __register_ftrace_function(ops);
2561 if (ret)
2562 return ret;
2563
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002564 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002565 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002566
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002567 /*
2568 * Note that ftrace probes uses this to start up
2569 * and modify functions it will probe. But we still
2570 * set the ADDING flag for modification, as probes
2571 * do not have trampolines. If they add them in the
2572 * future, then the probes will need to distinguish
2573 * between adding and updating probes.
2574 */
2575 ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002576
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002577 ret = ftrace_hash_ipmodify_enable(ops);
2578 if (ret < 0) {
2579 /* Rollback registration process */
2580 __unregister_ftrace_function(ops);
2581 ftrace_start_up--;
2582 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2583 return ret;
2584 }
2585
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002586 ftrace_hash_rec_enable(ops, 1);
Steven Rostedted926f92011-05-03 13:25:24 -04002587
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002588 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002589
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002590 ops->flags &= ~FTRACE_OPS_FL_ADDING;
2591
Steven Rostedta1cd6172011-05-23 15:24:25 -04002592 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002593}
2594
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002595static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002596{
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002597 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002598
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002599 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002600 return -ENODEV;
2601
2602 ret = __unregister_ftrace_function(ops);
2603 if (ret)
2604 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002605
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002606 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002607 /*
2608 * Just warn in case of unbalance, no need to kill ftrace, it's not
2609 * critical but the ftrace_call callers may be never nopped again after
2610 * further ftrace uses.
2611 */
2612 WARN_ON_ONCE(ftrace_start_up < 0);
2613
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05002614 /* Disabling ipmodify never fails */
2615 ftrace_hash_ipmodify_disable(ops);
Steven Rostedt (Red Hat)66209a52014-05-06 21:57:49 -04002616 ftrace_hash_rec_disable(ops, 1);
Steven Rostedtb8489142011-05-04 09:27:52 -04002617
Namhyung Kima737e6d2014-06-12 23:56:12 +09002618 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002619
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002620 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002621
2622 if (saved_ftrace_func != ftrace_trace_function) {
2623 saved_ftrace_func = ftrace_trace_function;
2624 command |= FTRACE_UPDATE_TRACE_FUNC;
2625 }
2626
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002627 if (!command || !ftrace_enabled) {
2628 /*
2629 * If these are control ops, they still need their
2630 * per_cpu field freed. Since, function tracing is
2631 * not currently active, we can just free them
2632 * without synchronizing all CPUs.
2633 */
2634 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2635 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002636 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002637 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002638
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002639 /*
2640 * If the ops uses a trampoline, then it needs to be
2641 * tested first on update.
2642 */
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002643 ops->flags |= FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002644 removed_ops = ops;
2645
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002646 /* The trampoline logic checks the old hashes */
2647 ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2648 ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2649
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002650 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002651
Steven Rostedt (Red Hat)84bde622014-09-12 14:21:13 -04002652 /*
2653 * If there's no more ops registered with ftrace, run a
2654 * sanity check to make sure all rec flags are cleared.
2655 */
2656 if (ftrace_ops_list == &ftrace_list_end) {
2657 struct ftrace_page *pg;
2658 struct dyn_ftrace *rec;
2659
2660 do_for_each_ftrace_rec(pg, rec) {
2661 if (FTRACE_WARN_ON_ONCE(rec->flags))
2662 pr_warn(" %pS flags:%lx\n",
2663 (void *)rec->ip, rec->flags);
2664 } while_for_each_ftrace_rec();
2665 }
2666
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04002667 ops->old_hash.filter_hash = NULL;
2668 ops->old_hash.notrace_hash = NULL;
2669
2670 removed_ops = NULL;
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04002671 ops->flags &= ~FTRACE_OPS_FL_REMOVING;
Steven Rostedt (Red Hat)79922b82014-05-06 21:56:17 -04002672
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002673 /*
2674 * Dynamic ops may be freed, we must make sure that all
2675 * callers are done before leaving this function.
2676 * The same goes for freeing the per_cpu data of the control
2677 * ops.
2678 *
2679 * Again, normal synchronize_sched() is not good enough.
2680 * We need to do a hard force of sched synchronization.
2681 * This is because we use preempt_disable() to do RCU, but
2682 * the function tracers can be called where RCU is not watching
2683 * (like before user_exit()). We can not rely on the RCU
2684 * infrastructure to do the synchronization, thus we must do it
2685 * ourselves.
2686 */
2687 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2688 schedule_on_each_cpu(ftrace_sync);
2689
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04002690 arch_ftrace_trampoline_free(ops);
2691
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002692 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2693 control_ops_free(ops);
2694 }
2695
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002696 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002697}
2698
Ingo Molnare309b412008-05-12 21:20:51 +02002699static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002700{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302701 int command;
2702
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002703 if (unlikely(ftrace_disabled))
2704 return;
2705
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002706 /* Force update next time */
2707 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002708 /* ftrace_start_up is true if we want ftrace running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302709 if (ftrace_start_up) {
2710 command = FTRACE_UPDATE_CALLS;
2711 if (ftrace_graph_active)
2712 command |= FTRACE_START_FUNC_RET;
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05002713 ftrace_startup_enable(command);
Pratyush Anand1619dc32015-03-06 23:58:06 +05302714 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002715}
2716
Ingo Molnare309b412008-05-12 21:20:51 +02002717static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002718{
Pratyush Anand1619dc32015-03-06 23:58:06 +05302719 int command;
2720
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002721 if (unlikely(ftrace_disabled))
2722 return;
2723
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002724 /* ftrace_start_up is true if ftrace is running */
Pratyush Anand1619dc32015-03-06 23:58:06 +05302725 if (ftrace_start_up) {
2726 command = FTRACE_DISABLE_CALLS;
2727 if (ftrace_graph_active)
2728 command |= FTRACE_STOP_FUNC_RET;
2729 ftrace_run_update_code(command);
2730 }
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002731}
2732
Steven Rostedt3d083392008-05-12 21:20:42 +02002733static cycle_t ftrace_update_time;
Steven Rostedt3d083392008-05-12 21:20:42 +02002734unsigned long ftrace_update_tot_cnt;
2735
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002736static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002737{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002738 /*
2739 * Filter_hash being empty will default to trace module.
2740 * But notrace hash requires a test of individual module functions.
2741 */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002742 return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2743 ftrace_hash_empty(ops->func_hash->notrace_hash);
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002744}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002745
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002746/*
2747 * Check if the current ops references the record.
2748 *
2749 * If the ops traces all functions, then it was already accounted for.
2750 * If the ops does not trace the current record function, skip it.
2751 * If the ops ignores the function via notrace filter, skip it.
2752 */
2753static inline bool
2754ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2755{
2756 /* If ops isn't enabled, ignore it */
2757 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2758 return 0;
2759
2760 /* If ops traces all mods, we already accounted for it */
2761 if (ops_traces_mod(ops))
2762 return 0;
2763
2764 /* The function must be in the filter */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002765 if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2766 !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002767 return 0;
2768
2769 /* If in notrace hash, we ignore it too */
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04002770 if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002771 return 0;
2772
2773 return 1;
2774}
2775
2776static int referenced_filters(struct dyn_ftrace *rec)
2777{
2778 struct ftrace_ops *ops;
2779 int cnt = 0;
2780
2781 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2782 if (ops_references_rec(ops, rec))
2783 cnt++;
2784 }
2785
2786 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002787}
2788
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002789static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
Steven Rostedt3d083392008-05-12 21:20:42 +02002790{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002791 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002792 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302793 cycle_t start, stop;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002794 unsigned long update_cnt = 0;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002795 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002796 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002797 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002798
2799 /*
2800 * When adding a module, we need to check if tracers are
2801 * currently enabled and if they are set to trace all functions.
2802 * If they are, we need to enable the module functions as well
2803 * as update the reference counts for those function records.
2804 */
2805 if (mod) {
2806 struct ftrace_ops *ops;
2807
2808 for (ops = ftrace_ops_list;
2809 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002810 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2811 if (ops_traces_mod(ops))
2812 ref++;
2813 else
2814 test = true;
2815 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002816 }
2817 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002818
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002819 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002820
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002821 for (pg = new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302822
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002823 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002824 int cnt = ref;
2825
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002826 /* If something went wrong, bail without enabling anything */
2827 if (unlikely(ftrace_disabled))
2828 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002829
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002830 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002831 if (test)
2832 cnt += referenced_filters(p);
2833 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302834
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002835 /*
2836 * Do the initial record conversion from mcount jump
2837 * to the NOP instructions.
2838 */
2839 if (!ftrace_code_disable(mod, p))
2840 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002841
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002842 update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002843
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002844 /*
2845 * If the tracing is enabled, go ahead and enable the record.
2846 *
2847 * The reason not to enable the record immediatelly is the
2848 * inherent check of ftrace_make_nop/ftrace_make_call for
2849 * correct previous instructions. Making first the NOP
2850 * conversion puts the module to the correct state, thus
2851 * passing the ftrace_make_call check.
2852 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002853 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002854 int failed = __ftrace_replace_code(p, 1);
2855 if (failed)
Steven Rostedt (Red Hat)4fd32792014-10-24 17:56:04 -04002856 ftrace_bug(failed, p);
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002857 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002858 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002859 }
2860
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002861 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002862 ftrace_update_time = stop - start;
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01002863 ftrace_update_tot_cnt += update_cnt;
Steven Rostedt3d083392008-05-12 21:20:42 +02002864
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002865 return 0;
2866}
2867
Steven Rostedta7900872011-12-16 16:23:44 -05002868static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002869{
Steven Rostedta7900872011-12-16 16:23:44 -05002870 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002871 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002872
Steven Rostedta7900872011-12-16 16:23:44 -05002873 if (WARN_ON(!count))
2874 return -EINVAL;
2875
2876 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002877
2878 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002879 * We want to fill as much as possible. No more than a page
2880 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002881 */
Steven Rostedta7900872011-12-16 16:23:44 -05002882 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2883 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002884
Steven Rostedta7900872011-12-16 16:23:44 -05002885 again:
2886 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2887
2888 if (!pg->records) {
2889 /* if we can't allocate this size, try something smaller */
2890 if (!order)
2891 return -ENOMEM;
2892 order >>= 1;
2893 goto again;
2894 }
2895
2896 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2897 pg->size = cnt;
2898
2899 if (cnt > count)
2900 cnt = count;
2901
2902 return cnt;
2903}
2904
2905static struct ftrace_page *
2906ftrace_allocate_pages(unsigned long num_to_init)
2907{
2908 struct ftrace_page *start_pg;
2909 struct ftrace_page *pg;
2910 int order;
2911 int cnt;
2912
2913 if (!num_to_init)
2914 return 0;
2915
2916 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2917 if (!pg)
2918 return NULL;
2919
2920 /*
2921 * Try to allocate as much as possible in one continues
2922 * location that fills in all of the space. We want to
2923 * waste as little space as possible.
2924 */
2925 for (;;) {
2926 cnt = ftrace_allocate_records(pg, num_to_init);
2927 if (cnt < 0)
2928 goto free_pages;
2929
2930 num_to_init -= cnt;
2931 if (!num_to_init)
2932 break;
2933
2934 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2935 if (!pg->next)
2936 goto free_pages;
2937
2938 pg = pg->next;
2939 }
2940
2941 return start_pg;
2942
2943 free_pages:
Namhyung Kim1f61be002014-06-11 17:06:53 +09002944 pg = start_pg;
2945 while (pg) {
Steven Rostedta7900872011-12-16 16:23:44 -05002946 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2947 free_pages((unsigned long)pg->records, order);
2948 start_pg = pg->next;
2949 kfree(pg);
2950 pg = start_pg;
2951 }
2952 pr_info("ftrace: FAILED to allocate memory for functions\n");
2953 return NULL;
2954}
2955
Steven Rostedt5072c592008-05-12 21:20:43 +02002956#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2957
2958struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002959 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002960 loff_t func_pos;
2961 struct ftrace_page *pg;
2962 struct dyn_ftrace *func;
2963 struct ftrace_func_probe *probe;
2964 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002965 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002966 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002967 int hidx;
2968 int idx;
2969 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002970};
2971
Ingo Molnare309b412008-05-12 21:20:51 +02002972static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002973t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002974{
2975 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002976 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002977 struct hlist_head *hhd;
2978
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002979 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002980 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002981
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002982 if (iter->probe)
2983 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002984 retry:
2985 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2986 return NULL;
2987
2988 hhd = &ftrace_func_hash[iter->hidx];
2989
2990 if (hlist_empty(hhd)) {
2991 iter->hidx++;
2992 hnd = NULL;
2993 goto retry;
2994 }
2995
2996 if (!hnd)
2997 hnd = hhd->first;
2998 else {
2999 hnd = hnd->next;
3000 if (!hnd) {
3001 iter->hidx++;
3002 goto retry;
3003 }
3004 }
3005
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003006 if (WARN_ON_ONCE(!hnd))
3007 return NULL;
3008
3009 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3010
3011 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003012}
3013
3014static void *t_hash_start(struct seq_file *m, loff_t *pos)
3015{
3016 struct ftrace_iterator *iter = m->private;
3017 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08003018 loff_t l;
3019
Steven Rostedt69a30832011-12-19 15:21:16 -05003020 if (!(iter->flags & FTRACE_ITER_DO_HASH))
3021 return NULL;
3022
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003023 if (iter->func_pos > *pos)
3024 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003025
Li Zefand82d6242009-06-24 09:54:54 +08003026 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04003027 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003028 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08003029 if (!p)
3030 break;
3031 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003032 if (!p)
3033 return NULL;
3034
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003035 /* Only set this if we have an item */
3036 iter->flags |= FTRACE_ITER_HASH;
3037
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003038 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003039}
3040
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003041static int
3042t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003043{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003044 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003045
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003046 rec = iter->probe;
3047 if (WARN_ON_ONCE(!rec))
3048 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003049
Steven Rostedt809dcf22009-02-16 23:06:01 -05003050 if (rec->ops->print)
3051 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3052
Steven Rostedtb375a112009-09-17 00:05:58 -04003053 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003054
3055 if (rec->data)
3056 seq_printf(m, ":%p", rec->data);
3057 seq_putc(m, '\n');
3058
3059 return 0;
3060}
3061
3062static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02003063t_next(struct seq_file *m, void *v, loff_t *pos)
3064{
3065 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003066 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003067 struct dyn_ftrace *rec = NULL;
3068
Steven Rostedt45a4a232011-04-21 23:16:46 -04003069 if (unlikely(ftrace_disabled))
3070 return NULL;
3071
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003072 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003073 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003074
Steven Rostedt5072c592008-05-12 21:20:43 +02003075 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01003076 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02003077
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003078 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003079 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003080
Steven Rostedt5072c592008-05-12 21:20:43 +02003081 retry:
3082 if (iter->idx >= iter->pg->index) {
3083 if (iter->pg->next) {
3084 iter->pg = iter->pg->next;
3085 iter->idx = 0;
3086 goto retry;
3087 }
3088 } else {
3089 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05003090 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003091 !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05003092
Steven Rostedt41c52c02008-05-22 11:46:33 -04003093 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003094 !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
Steven Rostedt647bcd02011-05-03 14:39:21 -04003095
3096 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04003097 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04003098
Steven Rostedt5072c592008-05-12 21:20:43 +02003099 rec = NULL;
3100 goto retry;
3101 }
3102 }
3103
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003104 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04003105 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003106
3107 iter->func = rec;
3108
3109 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003110}
3111
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003112static void reset_iter_read(struct ftrace_iterator *iter)
3113{
3114 iter->pos = 0;
3115 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03003116 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02003117}
3118
3119static void *t_start(struct seq_file *m, loff_t *pos)
3120{
3121 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003122 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02003123 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08003124 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02003125
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003126 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003127
3128 if (unlikely(ftrace_disabled))
3129 return NULL;
3130
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003131 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003132 * If an lseek was done, then reset and start from beginning.
3133 */
3134 if (*pos < iter->pos)
3135 reset_iter_read(iter);
3136
3137 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003138 * For set_ftrace_filter reading, if we have the filter
3139 * off, we can short cut and just print out that all
3140 * functions are enabled.
3141 */
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003142 if ((iter->flags & FTRACE_ITER_FILTER &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003143 ftrace_hash_empty(ops->func_hash->filter_hash)) ||
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003144 (iter->flags & FTRACE_ITER_NOTRACE &&
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003145 ftrace_hash_empty(ops->func_hash->notrace_hash))) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003146 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003147 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003148 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07003149 /* reset in case of seek/pread */
3150 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003151 return iter;
3152 }
3153
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003154 if (iter->flags & FTRACE_ITER_HASH)
3155 return t_hash_start(m, pos);
3156
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003157 /*
3158 * Unfortunately, we need to restart at ftrace_pages_start
3159 * every time we let go of the ftrace_mutex. This is because
3160 * those pointers can change without the lock.
3161 */
Li Zefan694ce0a2009-06-24 09:54:19 +08003162 iter->pg = ftrace_pages_start;
3163 iter->idx = 0;
3164 for (l = 0; l <= *pos; ) {
3165 p = t_next(m, p, &l);
3166 if (!p)
3167 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08003168 }
walimis5821e1b2008-11-15 15:19:06 +08003169
Steven Rostedt69a30832011-12-19 15:21:16 -05003170 if (!p)
3171 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003172
3173 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003174}
3175
3176static void t_stop(struct seq_file *m, void *p)
3177{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003178 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003179}
3180
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003181void * __weak
3182arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3183{
3184 return NULL;
3185}
3186
3187static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3188 struct dyn_ftrace *rec)
3189{
3190 void *ptr;
3191
3192 ptr = arch_ftrace_trampoline_func(ops, rec);
3193 if (ptr)
3194 seq_printf(m, " ->%pS", ptr);
3195}
3196
Steven Rostedt5072c592008-05-12 21:20:43 +02003197static int t_show(struct seq_file *m, void *v)
3198{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003199 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003200 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02003201
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003202 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003203 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05003204
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003205 if (iter->flags & FTRACE_ITER_PRINTALL) {
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003206 if (iter->flags & FTRACE_ITER_NOTRACE)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003207 seq_puts(m, "#### no functions disabled ####\n");
Namhyung Kim8c006cf2014-06-13 16:24:06 +09003208 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003209 seq_puts(m, "#### all functions enabled ####\n");
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05003210 return 0;
3211 }
3212
Steven Rostedt4aeb6962010-09-09 10:00:28 -04003213 rec = iter->func;
3214
Steven Rostedt5072c592008-05-12 21:20:43 +02003215 if (!rec)
3216 return 0;
3217
Steven Rostedt647bcd02011-05-03 14:39:21 -04003218 seq_printf(m, "%ps", (void *)rec->ip);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003219 if (iter->flags & FTRACE_ITER_ENABLED) {
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003220 struct ftrace_ops *ops = NULL;
3221
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003222 seq_printf(m, " (%ld)%s%s",
Steven Rostedt (Red Hat)0376bde2014-05-07 13:46:45 -04003223 ftrace_rec_count(rec),
Masami Hiramatsuf8b8be82014-11-21 05:25:16 -05003224 rec->flags & FTRACE_FL_REGS ? " R" : " ",
3225 rec->flags & FTRACE_FL_IPMODIFY ? " I" : " ");
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003226 if (rec->flags & FTRACE_FL_TRAMP_EN) {
Steven Rostedt (Red Hat)5fecaa02014-07-24 16:00:31 -04003227 ops = ftrace_find_tramp_ops_any(rec);
Steven Rostedt (Red Hat)fef5aee2014-07-24 12:25:47 -04003228 if (ops)
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003229 seq_printf(m, "\ttramp: %pS",
3230 (void *)ops->trampoline);
3231 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003232 seq_puts(m, "\ttramp: ERROR!");
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003233
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003234 }
Steven Rostedt (Red Hat)15d5b022014-07-03 14:51:36 -04003235 add_trampoline_func(m, ops, rec);
Steven Rostedt (Red Hat)9674b2f2014-05-09 16:54:59 -04003236 }
3237
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01003238 seq_putc(m, '\n');
Steven Rostedt5072c592008-05-12 21:20:43 +02003239
3240 return 0;
3241}
3242
James Morris88e9d342009-09-22 16:43:43 -07003243static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003244 .start = t_start,
3245 .next = t_next,
3246 .stop = t_stop,
3247 .show = t_show,
3248};
3249
Ingo Molnare309b412008-05-12 21:20:51 +02003250static int
Steven Rostedt5072c592008-05-12 21:20:43 +02003251ftrace_avail_open(struct inode *inode, struct file *file)
3252{
3253 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02003254
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003255 if (unlikely(ftrace_disabled))
3256 return -ENODEV;
3257
Jiri Olsa50e18b92012-04-25 10:23:39 +02003258 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3259 if (iter) {
3260 iter->pg = ftrace_pages_start;
3261 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02003262 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003263
Jiri Olsa50e18b92012-04-25 10:23:39 +02003264 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02003265}
3266
Steven Rostedt647bcd02011-05-03 14:39:21 -04003267static int
3268ftrace_enabled_open(struct inode *inode, struct file *file)
3269{
3270 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003271
Jiri Olsa50e18b92012-04-25 10:23:39 +02003272 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3273 if (iter) {
3274 iter->pg = ftrace_pages_start;
3275 iter->flags = FTRACE_ITER_ENABLED;
3276 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003277 }
3278
Jiri Olsa50e18b92012-04-25 10:23:39 +02003279 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04003280}
3281
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003282/**
3283 * ftrace_regex_open - initialize function tracer filter files
3284 * @ops: The ftrace_ops that hold the hash filters
3285 * @flag: The type of filter to process
3286 * @inode: The inode, usually passed in to your open routine
3287 * @file: The file, usually passed in to your open routine
3288 *
3289 * ftrace_regex_open() initializes the filter files for the
3290 * @ops. Depending on @flag it may process the filter hash or
3291 * the notrace hash of @ops. With this called from the open
3292 * routine, you can use ftrace_filter_write() for the write
3293 * routine if @flag has FTRACE_ITER_FILTER set, or
3294 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003295 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003296 * release must call ftrace_regex_release().
3297 */
3298int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003299ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003300 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003301{
3302 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003303 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02003304 int ret = 0;
3305
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003306 ftrace_ops_init(ops);
3307
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003308 if (unlikely(ftrace_disabled))
3309 return -ENODEV;
3310
Steven Rostedt5072c592008-05-12 21:20:43 +02003311 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3312 if (!iter)
3313 return -ENOMEM;
3314
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003315 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3316 kfree(iter);
3317 return -ENOMEM;
3318 }
3319
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003320 iter->ops = ops;
3321 iter->flags = flag;
3322
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003323 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003324
Steven Rostedtf45948e2011-05-02 12:29:25 -04003325 if (flag & FTRACE_ITER_NOTRACE)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003326 hash = ops->func_hash->notrace_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003327 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003328 hash = ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003329
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003330 if (file->f_mode & FMODE_WRITE) {
Namhyung Kimef2fbe12014-06-11 17:06:54 +09003331 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3332
3333 if (file->f_flags & O_TRUNC)
3334 iter->hash = alloc_ftrace_hash(size_bits);
3335 else
3336 iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3337
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003338 if (!iter->hash) {
3339 trace_parser_put(&iter->parser);
3340 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003341 ret = -ENOMEM;
3342 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003343 }
3344 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003345
Steven Rostedt5072c592008-05-12 21:20:43 +02003346 if (file->f_mode & FMODE_READ) {
3347 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02003348
3349 ret = seq_open(file, &show_ftrace_seq_ops);
3350 if (!ret) {
3351 struct seq_file *m = file->private_data;
3352 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08003353 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003354 /* Failed */
3355 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08003356 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003357 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08003358 }
Steven Rostedt5072c592008-05-12 21:20:43 +02003359 } else
3360 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003361
3362 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003363 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003364
3365 return ret;
3366}
3367
Steven Rostedt41c52c02008-05-22 11:46:33 -04003368static int
3369ftrace_filter_open(struct inode *inode, struct file *file)
3370{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003371 struct ftrace_ops *ops = inode->i_private;
3372
3373 return ftrace_regex_open(ops,
Steven Rostedt69a30832011-12-19 15:21:16 -05003374 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3375 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003376}
3377
3378static int
3379ftrace_notrace_open(struct inode *inode, struct file *file)
3380{
Steven Rostedt (Red Hat)e3b3e2e2013-11-11 23:07:14 -05003381 struct ftrace_ops *ops = inode->i_private;
3382
3383 return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003384 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003385}
3386
Steven Rostedt64e7c442009-02-13 17:08:48 -05003387static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003388{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003389 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08003390 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003391
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003392 switch (type) {
3393 case MATCH_FULL:
3394 if (strcmp(str, regex) == 0)
3395 matched = 1;
3396 break;
3397 case MATCH_FRONT_ONLY:
3398 if (strncmp(str, regex, len) == 0)
3399 matched = 1;
3400 break;
3401 case MATCH_MIDDLE_ONLY:
3402 if (strstr(str, regex))
3403 matched = 1;
3404 break;
3405 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08003406 slen = strlen(str);
3407 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003408 matched = 1;
3409 break;
3410 }
3411
3412 return matched;
3413}
3414
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003415static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003416enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04003417{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003418 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003419 int ret = 0;
3420
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003421 entry = ftrace_lookup_ip(hash, rec->ip);
3422 if (not) {
3423 /* Do nothing if it doesn't exist */
3424 if (!entry)
3425 return 0;
3426
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003427 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003428 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003429 /* Do nothing if it exists */
3430 if (entry)
3431 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003432
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003433 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003434 }
3435 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04003436}
3437
Steven Rostedt64e7c442009-02-13 17:08:48 -05003438static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003439ftrace_match_record(struct dyn_ftrace *rec, char *mod,
3440 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003441{
3442 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003443 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003444
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003445 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3446
3447 if (mod) {
3448 /* module lookup requires matching the module */
3449 if (!modname || strcmp(modname, mod))
3450 return 0;
3451
3452 /* blank search means to match all funcs in the mod */
3453 if (!len)
3454 return 1;
3455 }
3456
Steven Rostedt64e7c442009-02-13 17:08:48 -05003457 return ftrace_match(str, regex, len, type);
3458}
3459
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003460static int
3461match_records(struct ftrace_hash *hash, char *buff,
3462 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003463{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003464 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003465 struct ftrace_page *pg;
3466 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003467 int type = MATCH_FULL;
3468 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08003469 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003470 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003471
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003472 if (len) {
3473 type = filter_parse_regex(buff, len, &search, &not);
3474 search_len = strlen(search);
3475 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05003476
Steven Rostedt52baf112009-02-14 01:15:39 -05003477 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003478
3479 if (unlikely(ftrace_disabled))
3480 goto out_unlock;
3481
Steven Rostedt265c8312009-02-13 12:43:56 -05003482 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003483 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003484 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003485 if (ret < 0) {
3486 found = ret;
3487 goto out_unlock;
3488 }
Li Zefan311d16d2009-12-08 11:15:11 +08003489 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05003490 }
3491 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003492 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05003493 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08003494
3495 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02003496}
3497
Steven Rostedt64e7c442009-02-13 17:08:48 -05003498static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003499ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003500{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003501 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003502}
3503
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003504static int
3505ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003506{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003507 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003508
Steven Rostedt64e7c442009-02-13 17:08:48 -05003509 /* blank or '*' mean the same */
3510 if (strcmp(buff, "*") == 0)
3511 buff[0] = 0;
3512
3513 /* handle the case of 'dont filter this module' */
3514 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3515 buff[0] = 0;
3516 not = 1;
3517 }
3518
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003519 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003520}
3521
Steven Rostedtf6180772009-02-14 00:40:25 -05003522/*
3523 * We register the module command as a template to show others how
3524 * to register the a command as well.
3525 */
3526
3527static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003528ftrace_mod_callback(struct ftrace_hash *hash,
3529 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003530{
3531 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003532 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003533
3534 /*
3535 * cmd == 'mod' because we only registered this func
3536 * for the 'mod' ftrace_func_command.
3537 * But if you register one func with multiple commands,
3538 * you can tell which command was used by the cmd
3539 * parameter.
3540 */
3541
3542 /* we must have a module name */
3543 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003544 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003545
3546 mod = strsep(&param, ":");
3547 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003548 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003549
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003550 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003551 if (!ret)
3552 ret = -EINVAL;
3553 if (ret < 0)
3554 return ret;
3555
3556 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003557}
3558
3559static struct ftrace_func_command ftrace_mod_cmd = {
3560 .name = "mod",
3561 .func = ftrace_mod_callback,
3562};
3563
3564static int __init ftrace_mod_cmd_init(void)
3565{
3566 return register_ftrace_command(&ftrace_mod_cmd);
3567}
Steven Rostedt6f415672012-10-05 12:13:07 -04003568core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003569
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003570static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003571 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003572{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003573 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003574 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003575 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003576
3577 key = hash_long(ip, FTRACE_HASH_BITS);
3578
3579 hhd = &ftrace_func_hash[key];
3580
3581 if (hlist_empty(hhd))
3582 return;
3583
3584 /*
3585 * Disable preemption for these calls to prevent a RCU grace
3586 * period. This syncs the hash iteration and freeing of items
3587 * on the hash. rcu_read_lock is too dangerous here.
3588 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003589 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003590 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003591 if (entry->ip == ip)
3592 entry->ops->func(ip, parent_ip, &entry->data);
3593 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003594 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003595}
3596
Steven Rostedtb6887d72009-02-17 12:32:04 -05003597static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003598{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003599 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003600 .flags = FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003601 INIT_OPS_HASH(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003602};
3603
Steven Rostedtb6887d72009-02-17 12:32:04 -05003604static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003605
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003606static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003607{
Steven Rostedtb8489142011-05-04 09:27:52 -04003608 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003609 int i;
3610
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003611 if (ftrace_probe_registered) {
3612 /* still need to update the function call sites */
3613 if (ftrace_enabled)
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003614 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3615 old_hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003616 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003617 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003618
3619 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3620 struct hlist_head *hhd = &ftrace_func_hash[i];
3621 if (hhd->first)
3622 break;
3623 }
3624 /* Nothing registered? */
3625 if (i == FTRACE_FUNC_HASHSIZE)
3626 return;
3627
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003628 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003629
Steven Rostedtb6887d72009-02-17 12:32:04 -05003630 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003631}
3632
Steven Rostedtb6887d72009-02-17 12:32:04 -05003633static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003634{
3635 int i;
3636
Steven Rostedtb6887d72009-02-17 12:32:04 -05003637 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003638 return;
3639
3640 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3641 struct hlist_head *hhd = &ftrace_func_hash[i];
3642 if (hhd->first)
3643 return;
3644 }
3645
3646 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003647 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003648
Steven Rostedtb6887d72009-02-17 12:32:04 -05003649 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003650}
3651
3652
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003653static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003654{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003655 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003656 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003657 kfree(entry);
3658}
3659
Steven Rostedt59df055f2009-02-14 15:29:06 -05003660int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003661register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003662 void *data)
3663{
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003664 struct ftrace_ops_hash old_hash_ops;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003665 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003666 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003667 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003668 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003669 struct ftrace_page *pg;
3670 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003671 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003672 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003673 int count = 0;
3674 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003675 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003676
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003677 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003678 len = strlen(search);
3679
Steven Rostedtb6887d72009-02-17 12:32:04 -05003680 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003681 if (WARN_ON(not))
3682 return -EINVAL;
3683
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003684 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003685
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003686 old_hash_ops.filter_hash = old_hash;
3687 /* Probes only have filters */
3688 old_hash_ops.notrace_hash = NULL;
3689
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003690 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003691 if (!hash) {
3692 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003693 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003694 }
3695
3696 if (unlikely(ftrace_disabled)) {
3697 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003698 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003699 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003700
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003701 mutex_lock(&ftrace_lock);
3702
Steven Rostedt59df055f2009-02-14 15:29:06 -05003703 do_for_each_ftrace_rec(pg, rec) {
3704
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003705 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003706 continue;
3707
3708 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3709 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003710 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003711 if (!count)
3712 count = -ENOMEM;
3713 goto out_unlock;
3714 }
3715
3716 count++;
3717
3718 entry->data = data;
3719
3720 /*
3721 * The caller might want to do something special
3722 * for each function we find. We call the callback
3723 * to give the caller an opportunity to do so.
3724 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003725 if (ops->init) {
3726 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003727 /* caller does not like this func */
3728 kfree(entry);
3729 continue;
3730 }
3731 }
3732
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003733 ret = enter_record(hash, rec, 0);
3734 if (ret < 0) {
3735 kfree(entry);
3736 count = ret;
3737 goto out_unlock;
3738 }
3739
Steven Rostedt59df055f2009-02-14 15:29:06 -05003740 entry->ops = ops;
3741 entry->ip = rec->ip;
3742
3743 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3744 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3745
3746 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003747
3748 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003749
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05003750 __enable_ftrace_function_probe(&old_hash_ops);
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04003751
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003752 if (!ret)
3753 free_ftrace_hash_rcu(old_hash);
3754 else
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003755 count = ret;
3756
Steven Rostedt59df055f2009-02-14 15:29:06 -05003757 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003758 mutex_unlock(&ftrace_lock);
3759 out:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003760 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003761 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003762
3763 return count;
3764}
3765
3766enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003767 PROBE_TEST_FUNC = 1,
3768 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003769};
3770
3771static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003772__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003773 void *data, int flags)
3774{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003775 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003776 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003777 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003778 struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003779 struct ftrace_hash *old_hash = *orig_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003780 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003781 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003782 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003783 char str[KSYM_SYMBOL_LEN];
3784 int type = MATCH_FULL;
3785 int i, len = 0;
3786 char *search;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003787 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003788
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003789 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003790 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003791 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003792 int not;
3793
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003794 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003795 len = strlen(search);
3796
Steven Rostedtb6887d72009-02-17 12:32:04 -05003797 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003798 if (WARN_ON(not))
3799 return;
3800 }
3801
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003802 mutex_lock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003803
3804 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3805 if (!hash)
3806 /* Hmm, should report this somehow */
3807 goto out_unlock;
3808
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003809 INIT_LIST_HEAD(&free_list);
3810
Steven Rostedt59df055f2009-02-14 15:29:06 -05003811 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3812 struct hlist_head *hhd = &ftrace_func_hash[i];
3813
Sasha Levinb67bfe02013-02-27 17:06:00 -08003814 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003815
3816 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003817 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003818 continue;
3819
Steven Rostedtb6887d72009-02-17 12:32:04 -05003820 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003821 continue;
3822
3823 /* do this last, since it is the most expensive */
3824 if (glob) {
3825 kallsyms_lookup(entry->ip, NULL, NULL,
3826 NULL, str);
3827 if (!ftrace_match(str, glob, len, type))
3828 continue;
3829 }
3830
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003831 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3832 /* It is possible more than one entry had this ip */
3833 if (rec_entry)
3834 free_hash_entry(hash, rec_entry);
3835
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003836 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003837 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003838 }
3839 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003840 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003841 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003842 /*
3843 * Remove after the disable is called. Otherwise, if the last
3844 * probe is removed, a null hash means *all enabled*.
3845 */
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003846 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003847 synchronize_sched();
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04003848 if (!ret)
3849 free_ftrace_hash_rcu(old_hash);
3850
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003851 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3852 list_del(&entry->free_list);
3853 ftrace_free_entry(entry);
3854 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003855 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003856
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003857 out_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04003858 mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003859 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003860}
3861
3862void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003863unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003864 void *data)
3865{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003866 __unregister_ftrace_function_probe(glob, ops, data,
3867 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003868}
3869
3870void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003871unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003872{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003873 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003874}
3875
Steven Rostedtb6887d72009-02-17 12:32:04 -05003876void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003877{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003878 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003879}
3880
Steven Rostedtf6180772009-02-14 00:40:25 -05003881static LIST_HEAD(ftrace_commands);
3882static DEFINE_MUTEX(ftrace_cmd_mutex);
3883
Tom Zanussi38de93a2013-10-24 08:34:18 -05003884/*
3885 * Currently we only register ftrace commands from __init, so mark this
3886 * __init too.
3887 */
3888__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003889{
3890 struct ftrace_func_command *p;
3891 int ret = 0;
3892
3893 mutex_lock(&ftrace_cmd_mutex);
3894 list_for_each_entry(p, &ftrace_commands, list) {
3895 if (strcmp(cmd->name, p->name) == 0) {
3896 ret = -EBUSY;
3897 goto out_unlock;
3898 }
3899 }
3900 list_add(&cmd->list, &ftrace_commands);
3901 out_unlock:
3902 mutex_unlock(&ftrace_cmd_mutex);
3903
3904 return ret;
3905}
3906
Tom Zanussi38de93a2013-10-24 08:34:18 -05003907/*
3908 * Currently we only unregister ftrace commands from __init, so mark
3909 * this __init too.
3910 */
3911__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003912{
3913 struct ftrace_func_command *p, *n;
3914 int ret = -ENODEV;
3915
3916 mutex_lock(&ftrace_cmd_mutex);
3917 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3918 if (strcmp(cmd->name, p->name) == 0) {
3919 ret = 0;
3920 list_del_init(&p->list);
3921 goto out_unlock;
3922 }
3923 }
3924 out_unlock:
3925 mutex_unlock(&ftrace_cmd_mutex);
3926
3927 return ret;
3928}
3929
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003930static int ftrace_process_regex(struct ftrace_hash *hash,
3931 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003932{
Steven Rostedtf6180772009-02-14 00:40:25 -05003933 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003934 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003935 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003936
3937 func = strsep(&next, ":");
3938
3939 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003940 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003941 if (!ret)
3942 ret = -EINVAL;
3943 if (ret < 0)
3944 return ret;
3945 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003946 }
3947
Steven Rostedtf6180772009-02-14 00:40:25 -05003948 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003949
3950 command = strsep(&next, ":");
3951
Steven Rostedtf6180772009-02-14 00:40:25 -05003952 mutex_lock(&ftrace_cmd_mutex);
3953 list_for_each_entry(p, &ftrace_commands, list) {
3954 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003955 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003956 goto out_unlock;
3957 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003958 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003959 out_unlock:
3960 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003961
Steven Rostedtf6180772009-02-14 00:40:25 -05003962 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003963}
3964
Ingo Molnare309b412008-05-12 21:20:51 +02003965static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003966ftrace_regex_write(struct file *file, const char __user *ubuf,
3967 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003968{
3969 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003970 struct trace_parser *parser;
3971 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003972
Li Zefan4ba79782009-09-22 13:52:20 +08003973 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003974 return 0;
3975
Steven Rostedt5072c592008-05-12 21:20:43 +02003976 if (file->f_mode & FMODE_READ) {
3977 struct seq_file *m = file->private_data;
3978 iter = m->private;
3979 } else
3980 iter = file->private_data;
3981
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003982 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003983 return -ENODEV;
3984
3985 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003986
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003987 parser = &iter->parser;
3988 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003989
Li Zefan4ba79782009-09-22 13:52:20 +08003990 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003991 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003992 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003993 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003994 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003995 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003996 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003997 }
3998
Steven Rostedt5072c592008-05-12 21:20:43 +02003999 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004000 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02004001 return ret;
4002}
4003
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004004ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004005ftrace_filter_write(struct file *file, const char __user *ubuf,
4006 size_t cnt, loff_t *ppos)
4007{
4008 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4009}
4010
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004011ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04004012ftrace_notrace_write(struct file *file, const char __user *ubuf,
4013 size_t cnt, loff_t *ppos)
4014{
4015 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4016}
4017
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004018static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004019ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4020{
4021 struct ftrace_func_entry *entry;
4022
4023 if (!ftrace_location(ip))
4024 return -EINVAL;
4025
4026 if (remove) {
4027 entry = ftrace_lookup_ip(hash, ip);
4028 if (!entry)
4029 return -ENOENT;
4030 free_hash_entry(hash, entry);
4031 return 0;
4032 }
4033
4034 return add_hash_entry(hash, ip);
4035}
4036
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004037static void ftrace_ops_update_code(struct ftrace_ops *ops,
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004038 struct ftrace_ops_hash *old_hash)
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004039{
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004040 struct ftrace_ops *op;
4041
4042 if (!ftrace_enabled)
4043 return;
4044
4045 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
Steven Rostedt (Red Hat)8252ecf2014-10-24 14:56:01 -04004046 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
Steven Rostedt (Red Hat)8f86f832015-01-13 11:20:43 -05004047 return;
4048 }
4049
4050 /*
4051 * If this is the shared global_ops filter, then we need to
4052 * check if there is another ops that shares it, is enabled.
4053 * If so, we still need to run the modify code.
4054 */
4055 if (ops->func_hash != &global_ops.local_hash)
4056 return;
4057
4058 do_for_each_ftrace_op(op, ftrace_ops_list) {
4059 if (op->func_hash == &global_ops.local_hash &&
4060 op->flags & FTRACE_OPS_FL_ENABLED) {
4061 ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4062 /* Only need to do this once */
4063 return;
4064 }
4065 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04004066}
4067
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004068static int
4069ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4070 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004071{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004072 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004073 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004074 struct ftrace_hash *old_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004075 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004076 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004077
Steven Rostedt41c52c02008-05-22 11:46:33 -04004078 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004079 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004080
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004081 mutex_lock(&ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004082
Steven Rostedtf45948e2011-05-02 12:29:25 -04004083 if (enable)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004084 orig_hash = &ops->func_hash->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04004085 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004086 orig_hash = &ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004087
Wang Nanb972cc52014-07-15 08:40:20 +08004088 if (reset)
4089 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4090 else
4091 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4092
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004093 if (!hash) {
4094 ret = -ENOMEM;
4095 goto out_regex_unlock;
4096 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04004097
Jiri Olsaac483c42012-01-02 10:04:14 +01004098 if (buf && !ftrace_match_records(hash, buf, len)) {
4099 ret = -EINVAL;
4100 goto out_regex_unlock;
4101 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004102 if (ip) {
4103 ret = ftrace_match_addr(hash, ip, remove);
4104 if (ret < 0)
4105 goto out_regex_unlock;
4106 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004107
4108 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004109 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004110 old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4111 old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004112 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004113 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004114 ftrace_ops_update_code(ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004115 free_ftrace_hash_rcu(old_hash);
4116 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004117 mutex_unlock(&ftrace_lock);
4118
Jiri Olsaac483c42012-01-02 10:04:14 +01004119 out_regex_unlock:
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004120 mutex_unlock(&ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004121
4122 free_ftrace_hash(hash);
4123 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04004124}
4125
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004126static int
4127ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4128 int reset, int enable)
4129{
4130 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4131}
4132
4133/**
4134 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4135 * @ops - the ops to set the filter with
4136 * @ip - the address to add to or remove from the filter.
4137 * @remove - non zero to remove the ip from the filter
4138 * @reset - non zero to reset all filters before applying this filter.
4139 *
4140 * Filters denote which functions should be enabled when tracing is enabled
4141 * If @ip is NULL, it failes to update filter.
4142 */
4143int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4144 int remove, int reset)
4145{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004146 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09004147 return ftrace_set_addr(ops, ip, remove, reset, 1);
4148}
4149EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4150
4151static int
4152ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4153 int reset, int enable)
4154{
4155 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4156}
4157
Steven Rostedt77a2b372008-05-12 21:20:45 +02004158/**
4159 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004160 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02004161 * @buf - the string that holds the function filter text.
4162 * @len - the length of the string.
4163 * @reset - non zero to reset all filters before applying this filter.
4164 *
4165 * Filters denote which functions should be enabled when tracing is enabled.
4166 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4167 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004168int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004169 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02004170{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004171 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004172 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004173}
Steven Rostedt936e0742011-05-05 22:54:01 -04004174EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004175
Steven Rostedt41c52c02008-05-22 11:46:33 -04004176/**
4177 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04004178 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04004179 * @buf - the string that holds the function notrace text.
4180 * @len - the length of the string.
4181 * @reset - non zero to reset all filters before applying this filter.
4182 *
4183 * Notrace Filters denote which functions should not be enabled when tracing
4184 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4185 * for tracing.
4186 */
Jiri Olsaac483c42012-01-02 10:04:14 +01004187int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04004188 int len, int reset)
4189{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004190 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01004191 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04004192}
4193EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4194/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004195 * ftrace_set_global_filter - set a function to filter on with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004196 * @buf - the string that holds the function filter text.
4197 * @len - the length of the string.
4198 * @reset - non zero to reset all filters before applying this filter.
4199 *
4200 * Filters denote which functions should be enabled when tracing is enabled.
4201 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4202 */
4203void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4204{
4205 ftrace_set_regex(&global_ops, buf, len, reset, 1);
4206}
4207EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4208
4209/**
Jiaxing Wang8d1b0652014-04-20 23:10:44 +08004210 * ftrace_set_global_notrace - set a function to not trace with global tracers
Steven Rostedt936e0742011-05-05 22:54:01 -04004211 * @buf - the string that holds the function notrace text.
4212 * @len - the length of the string.
4213 * @reset - non zero to reset all filters before applying this filter.
4214 *
4215 * Notrace Filters denote which functions should not be enabled when tracing
4216 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4217 * for tracing.
4218 */
4219void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04004220{
Steven Rostedtf45948e2011-05-02 12:29:25 -04004221 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004222}
Steven Rostedt936e0742011-05-05 22:54:01 -04004223EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02004224
Steven Rostedt2af15d62009-05-28 13:37:24 -04004225/*
4226 * command line interface to allow users to set filters on boot up.
4227 */
4228#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
4229static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4230static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4231
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004232/* Used by function selftest to not test if filter is set */
4233bool ftrace_filter_param __initdata;
4234
Steven Rostedt2af15d62009-05-28 13:37:24 -04004235static int __init set_ftrace_notrace(char *str)
4236{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004237 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004238 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004239 return 1;
4240}
4241__setup("ftrace_notrace=", set_ftrace_notrace);
4242
4243static int __init set_ftrace_filter(char *str)
4244{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04004245 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08004246 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004247 return 1;
4248}
4249__setup("ftrace_filter=", set_ftrace_filter);
4250
Stefan Assmann369bc182009-10-12 22:17:21 +02004251#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08004252static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004253static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004254static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05004255
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004256static unsigned long save_global_trampoline;
4257static unsigned long save_global_flags;
4258
Stefan Assmann369bc182009-10-12 22:17:21 +02004259static int __init set_graph_function(char *str)
4260{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02004261 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02004262 return 1;
4263}
4264__setup("ftrace_graph_filter=", set_graph_function);
4265
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004266static int __init set_graph_notrace_function(char *str)
4267{
4268 strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4269 return 1;
4270}
4271__setup("ftrace_graph_notrace=", set_graph_notrace_function);
4272
4273static void __init set_ftrace_early_graph(char *buf, int enable)
Stefan Assmann369bc182009-10-12 22:17:21 +02004274{
4275 int ret;
4276 char *func;
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004277 unsigned long *table = ftrace_graph_funcs;
4278 int *count = &ftrace_graph_count;
4279
4280 if (!enable) {
4281 table = ftrace_graph_notrace_funcs;
4282 count = &ftrace_graph_notrace_count;
4283 }
Stefan Assmann369bc182009-10-12 22:17:21 +02004284
4285 while (buf) {
4286 func = strsep(&buf, ",");
4287 /* we allow only one expression at a time */
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004288 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02004289 if (ret)
4290 printk(KERN_DEBUG "ftrace: function %s not "
4291 "traceable\n", func);
4292 }
4293}
4294#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4295
Steven Rostedt2a85a372011-12-19 21:57:44 -05004296void __init
4297ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04004298{
4299 char *func;
4300
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004301 ftrace_ops_init(ops);
4302
Steven Rostedt2af15d62009-05-28 13:37:24 -04004303 while (buf) {
4304 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04004305 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004306 }
4307}
4308
4309static void __init set_ftrace_early_filters(void)
4310{
4311 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004312 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04004313 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05004314 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004315#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4316 if (ftrace_graph_buf[0])
Namhyung Kim0d7d9a12014-06-13 01:23:50 +09004317 set_ftrace_early_graph(ftrace_graph_buf, 1);
4318 if (ftrace_graph_notrace_buf[0])
4319 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02004320#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04004321}
4322
Steven Rostedtfc13cb02011-12-19 14:41:25 -05004323int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02004324{
4325 struct seq_file *m = (struct seq_file *)file->private_data;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004326 struct ftrace_ops_hash old_hash_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02004327 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004328 struct ftrace_hash **orig_hash;
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004329 struct ftrace_hash *old_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004330 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04004331 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004332 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02004333
Steven Rostedt5072c592008-05-12 21:20:43 +02004334 if (file->f_mode & FMODE_READ) {
4335 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02004336 seq_release(inode, file);
4337 } else
4338 iter = file->private_data;
4339
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004340 parser = &iter->parser;
4341 if (trace_parser_loaded(parser)) {
4342 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004343 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02004344 }
4345
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004346 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004347
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004348 mutex_lock(&iter->ops->func_hash->regex_lock);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004349
Steven Rostedt058e2972011-04-29 22:35:33 -04004350 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04004351 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4352
4353 if (filter_hash)
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004354 orig_hash = &iter->ops->func_hash->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04004355 else
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004356 orig_hash = &iter->ops->func_hash->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004357
Steven Rostedt058e2972011-04-29 22:35:33 -04004358 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004359 old_hash = *orig_hash;
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004360 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4361 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04004362 ret = ftrace_hash_move(iter->ops, filter_hash,
4363 orig_hash, iter->hash);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004364 if (!ret) {
Steven Rostedt (Red Hat)7485058e2015-01-13 14:03:38 -05004365 ftrace_ops_update_code(iter->ops, &old_hash_ops);
Steven Rostedt (Red Hat)3296fc42014-07-24 15:33:41 -04004366 free_ftrace_hash_rcu(old_hash);
4367 }
Steven Rostedt058e2972011-04-29 22:35:33 -04004368 mutex_unlock(&ftrace_lock);
4369 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09004370
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04004371 mutex_unlock(&iter->ops->func_hash->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04004372 free_ftrace_hash(iter->hash);
4373 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04004374
Steven Rostedt5072c592008-05-12 21:20:43 +02004375 return 0;
4376}
4377
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004378static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004379 .open = ftrace_avail_open,
4380 .read = seq_read,
4381 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08004382 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02004383};
4384
Steven Rostedt647bcd02011-05-03 14:39:21 -04004385static const struct file_operations ftrace_enabled_fops = {
4386 .open = ftrace_enabled_open,
4387 .read = seq_read,
4388 .llseek = seq_lseek,
4389 .release = seq_release_private,
4390};
4391
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004392static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02004393 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004394 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02004395 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004396 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004397 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02004398};
4399
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004400static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04004401 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08004402 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004403 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004404 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04004405 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004406};
4407
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004408#ifdef CONFIG_FUNCTION_GRAPH_TRACER
4409
4410static DEFINE_MUTEX(graph_lock);
4411
4412int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004413int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004414unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004415unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004416
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004417struct ftrace_graph_data {
4418 unsigned long *table;
4419 size_t size;
4420 int *count;
4421 const struct seq_operations *seq_ops;
4422};
4423
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004424static void *
Li Zefan85951842009-06-24 09:54:00 +08004425__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004426{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004427 struct ftrace_graph_data *fgd = m->private;
4428
4429 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004430 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004431 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08004432}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004433
Li Zefan85951842009-06-24 09:54:00 +08004434static void *
4435g_next(struct seq_file *m, void *v, loff_t *pos)
4436{
4437 (*pos)++;
4438 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004439}
4440
4441static void *g_start(struct seq_file *m, loff_t *pos)
4442{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004443 struct ftrace_graph_data *fgd = m->private;
4444
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004445 mutex_lock(&graph_lock);
4446
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004447 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004448 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004449 return (void *)1;
4450
Li Zefan85951842009-06-24 09:54:00 +08004451 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004452}
4453
4454static void g_stop(struct seq_file *m, void *p)
4455{
4456 mutex_unlock(&graph_lock);
4457}
4458
4459static int g_show(struct seq_file *m, void *v)
4460{
4461 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004462
4463 if (!ptr)
4464 return 0;
4465
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004466 if (ptr == (unsigned long *)1) {
Namhyung Kim280d1422014-06-13 01:23:51 +09004467 struct ftrace_graph_data *fgd = m->private;
4468
4469 if (fgd->table == ftrace_graph_funcs)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004470 seq_puts(m, "#### all functions enabled ####\n");
Namhyung Kim280d1422014-06-13 01:23:51 +09004471 else
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01004472 seq_puts(m, "#### no functions disabled ####\n");
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004473 return 0;
4474 }
4475
Steven Rostedtb375a112009-09-17 00:05:58 -04004476 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004477
4478 return 0;
4479}
4480
James Morris88e9d342009-09-22 16:43:43 -07004481static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004482 .start = g_start,
4483 .next = g_next,
4484 .stop = g_stop,
4485 .show = g_show,
4486};
4487
4488static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004489__ftrace_graph_open(struct inode *inode, struct file *file,
4490 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004491{
4492 int ret = 0;
4493
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004494 mutex_lock(&graph_lock);
4495 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04004496 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004497 *fgd->count = 0;
4498 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004499 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004500 mutex_unlock(&graph_lock);
4501
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004502 if (file->f_mode & FMODE_READ) {
4503 ret = seq_open(file, fgd->seq_ops);
4504 if (!ret) {
4505 struct seq_file *m = file->private_data;
4506 m->private = fgd;
4507 }
4508 } else
4509 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08004510
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004511 return ret;
4512}
4513
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004514static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004515ftrace_graph_open(struct inode *inode, struct file *file)
4516{
4517 struct ftrace_graph_data *fgd;
4518
4519 if (unlikely(ftrace_disabled))
4520 return -ENODEV;
4521
4522 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4523 if (fgd == NULL)
4524 return -ENOMEM;
4525
4526 fgd->table = ftrace_graph_funcs;
4527 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4528 fgd->count = &ftrace_graph_count;
4529 fgd->seq_ops = &ftrace_graph_seq_ops;
4530
4531 return __ftrace_graph_open(inode, file, fgd);
4532}
4533
4534static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004535ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4536{
4537 struct ftrace_graph_data *fgd;
4538
4539 if (unlikely(ftrace_disabled))
4540 return -ENODEV;
4541
4542 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4543 if (fgd == NULL)
4544 return -ENOMEM;
4545
4546 fgd->table = ftrace_graph_notrace_funcs;
4547 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4548 fgd->count = &ftrace_graph_notrace_count;
4549 fgd->seq_ops = &ftrace_graph_seq_ops;
4550
4551 return __ftrace_graph_open(inode, file, fgd);
4552}
4553
4554static int
Li Zefan87827112009-07-23 11:29:11 +08004555ftrace_graph_release(struct inode *inode, struct file *file)
4556{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004557 if (file->f_mode & FMODE_READ) {
4558 struct seq_file *m = file->private_data;
4559
4560 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08004561 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004562 } else {
4563 kfree(file->private_data);
4564 }
4565
Li Zefan87827112009-07-23 11:29:11 +08004566 return 0;
4567}
4568
4569static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004570ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004571{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004572 struct dyn_ftrace *rec;
4573 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004574 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004575 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004576 int type, not;
4577 char *search;
4578 bool exists;
4579 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004580
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004581 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004582 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004583 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004584 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004585
4586 search_len = strlen(search);
4587
Steven Rostedt52baf112009-02-14 01:15:39 -05004588 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004589
4590 if (unlikely(ftrace_disabled)) {
4591 mutex_unlock(&ftrace_lock);
4592 return -ENODEV;
4593 }
4594
Steven Rostedt265c8312009-02-13 12:43:56 -05004595 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004596
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004597 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004598 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004599 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004600 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004601 if (array[i] == rec->ip) {
4602 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004603 break;
4604 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004605 }
4606
4607 if (!not) {
4608 fail = 0;
4609 if (!exists) {
4610 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004611 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004612 goto out;
4613 }
4614 } else {
4615 if (exists) {
4616 array[i] = array[--(*idx)];
4617 array[*idx] = 0;
4618 fail = 0;
4619 }
4620 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004621 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004622 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004623out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004624 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004625
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004626 if (fail)
4627 return -EINVAL;
4628
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004629 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004630}
4631
4632static ssize_t
4633ftrace_graph_write(struct file *file, const char __user *ubuf,
4634 size_t cnt, loff_t *ppos)
4635{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004636 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004637 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004638 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004639
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004640 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004641 return 0;
4642
Namhyung Kim6a101082013-10-14 17:24:25 +09004643 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4644 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004645
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004646 read = trace_get_user(&parser, ubuf, cnt, ppos);
4647
Li Zefan4ba79782009-09-22 13:52:20 +08004648 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004649 parser.buffer[parser.idx] = 0;
4650
Namhyung Kim6a101082013-10-14 17:24:25 +09004651 mutex_lock(&graph_lock);
4652
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004653 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004654 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4655 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004656
4657 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004658 }
4659
Namhyung Kim6a101082013-10-14 17:24:25 +09004660 if (!ret)
4661 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004662
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004663 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004664
4665 return ret;
4666}
4667
4668static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004669 .open = ftrace_graph_open,
4670 .read = seq_read,
4671 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004672 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004673 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004674};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004675
4676static const struct file_operations ftrace_graph_notrace_fops = {
4677 .open = ftrace_graph_notrace_open,
4678 .read = seq_read,
4679 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004680 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004681 .release = ftrace_graph_release,
4682};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004683#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4684
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004685void ftrace_create_filter_files(struct ftrace_ops *ops,
4686 struct dentry *parent)
4687{
4688
4689 trace_create_file("set_ftrace_filter", 0644, parent,
4690 ops, &ftrace_filter_fops);
4691
4692 trace_create_file("set_ftrace_notrace", 0644, parent,
4693 ops, &ftrace_notrace_fops);
4694}
4695
4696/*
4697 * The name "destroy_filter_files" is really a misnomer. Although
4698 * in the future, it may actualy delete the files, but this is
4699 * really intended to make sure the ops passed in are disabled
4700 * and that when this function returns, the caller is free to
4701 * free the ops.
4702 *
4703 * The "destroy" name is only to match the "create" name that this
4704 * should be paired with.
4705 */
4706void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4707{
4708 mutex_lock(&ftrace_lock);
4709 if (ops->flags & FTRACE_OPS_FL_ENABLED)
4710 ftrace_shutdown(ops, 0);
4711 ops->flags |= FTRACE_OPS_FL_DELETED;
4712 mutex_unlock(&ftrace_lock);
4713}
4714
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004715static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004716{
Steven Rostedt5072c592008-05-12 21:20:43 +02004717
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004718 trace_create_file("available_filter_functions", 0444,
4719 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004720
Steven Rostedt647bcd02011-05-03 14:39:21 -04004721 trace_create_file("enabled_functions", 0444,
4722 d_tracer, NULL, &ftrace_enabled_fops);
4723
Steven Rostedt (Red Hat)591dffd2014-01-10 16:17:45 -05004724 ftrace_create_filter_files(&global_ops, d_tracer);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004725
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004726#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004727 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004728 NULL,
4729 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004730 trace_create_file("set_graph_notrace", 0444, d_tracer,
4731 NULL,
4732 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004733#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4734
Steven Rostedt5072c592008-05-12 21:20:43 +02004735 return 0;
4736}
4737
Steven Rostedt9fd49322012-04-24 22:32:06 -04004738static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004739{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004740 const unsigned long *ipa = a;
4741 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004742
Steven Rostedt9fd49322012-04-24 22:32:06 -04004743 if (*ipa > *ipb)
4744 return 1;
4745 if (*ipa < *ipb)
4746 return -1;
4747 return 0;
4748}
4749
4750static void ftrace_swap_ips(void *a, void *b, int size)
4751{
4752 unsigned long *ipa = a;
4753 unsigned long *ipb = b;
4754 unsigned long t;
4755
4756 t = *ipa;
4757 *ipa = *ipb;
4758 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004759}
4760
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004761static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004762 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004763 unsigned long *end)
4764{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004765 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004766 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004767 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004768 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004769 unsigned long *p;
4770 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004771 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004772 int ret = -ENOMEM;
4773
4774 count = end - start;
4775
4776 if (!count)
4777 return 0;
4778
Steven Rostedt9fd49322012-04-24 22:32:06 -04004779 sort(start, count, sizeof(*start),
4780 ftrace_cmp_ips, ftrace_swap_ips);
4781
Steven Rostedt706c81f2012-04-24 23:45:26 -04004782 start_pg = ftrace_allocate_pages(count);
4783 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004784 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004785
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004786 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004787
Steven Rostedt32082302011-12-16 14:42:37 -05004788 /*
4789 * Core and each module needs their own pages, as
4790 * modules will free them when they are removed.
4791 * Force a new page to be allocated for modules.
4792 */
Steven Rostedta7900872011-12-16 16:23:44 -05004793 if (!mod) {
4794 WARN_ON(ftrace_pages || ftrace_pages_start);
4795 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004796 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004797 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004798 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004799 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004800
Steven Rostedta7900872011-12-16 16:23:44 -05004801 if (WARN_ON(ftrace_pages->next)) {
4802 /* Hmm, we have free pages? */
4803 while (ftrace_pages->next)
4804 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004805 }
Steven Rostedta7900872011-12-16 16:23:44 -05004806
Steven Rostedt706c81f2012-04-24 23:45:26 -04004807 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004808 }
4809
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004810 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004811 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004812 while (p < end) {
4813 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004814 /*
4815 * Some architecture linkers will pad between
4816 * the different mcount_loc sections of different
4817 * object files to satisfy alignments.
4818 * Skip any NULL pointers.
4819 */
4820 if (!addr)
4821 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004822
4823 if (pg->index == pg->size) {
4824 /* We should have allocated enough */
4825 if (WARN_ON(!pg->next))
4826 break;
4827 pg = pg->next;
4828 }
4829
4830 rec = &pg->records[pg->index++];
4831 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004832 }
4833
Steven Rostedt706c81f2012-04-24 23:45:26 -04004834 /* We should have used all pages */
4835 WARN_ON(pg->next);
4836
4837 /* Assign the last page to ftrace_pages */
4838 ftrace_pages = pg;
4839
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004840 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004841 * We only need to disable interrupts on start up
4842 * because we are modifying code that an interrupt
4843 * may execute, and the modification is not atomic.
4844 * But for modules, nothing runs the code we modify
4845 * until we are finished with it, and there's no
4846 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004847 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004848 if (!mod)
4849 local_irq_save(flags);
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004850 ftrace_update_code(mod, start_pg);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004851 if (!mod)
4852 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004853 ret = 0;
4854 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004855 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004856
Steven Rostedta7900872011-12-16 16:23:44 -05004857 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004858}
4859
Steven Rostedt93eb6772009-04-15 13:24:06 -04004860#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004861
4862#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4863
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004864void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004865{
4866 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004867 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004868 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004869 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004870
Steven Rostedt93eb6772009-04-15 13:24:06 -04004871 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004872
4873 if (ftrace_disabled)
4874 goto out_unlock;
4875
Steven Rostedt32082302011-12-16 14:42:37 -05004876 /*
4877 * Each module has its own ftrace_pages, remove
4878 * them from the list.
4879 */
4880 last_pg = &ftrace_pages_start;
4881 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4882 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004883 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004884 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004885 * As core pages are first, the first
4886 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004887 */
Steven Rostedt32082302011-12-16 14:42:37 -05004888 if (WARN_ON(pg == ftrace_pages_start))
4889 goto out_unlock;
4890
4891 /* Check if we are deleting the last page */
4892 if (pg == ftrace_pages)
4893 ftrace_pages = next_to_ftrace_page(last_pg);
4894
4895 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004896 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4897 free_pages((unsigned long)pg->records, order);
4898 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004899 } else
4900 last_pg = &pg->next;
4901 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004902 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004903 mutex_unlock(&ftrace_lock);
4904}
4905
4906static void ftrace_init_module(struct module *mod,
4907 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004908{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004909 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004910 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004911 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004912}
4913
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004914void ftrace_module_init(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004915{
Steven Rostedt (Red Hat)a949ae52014-04-24 10:40:12 -04004916 ftrace_init_module(mod, mod->ftrace_callsites,
4917 mod->ftrace_callsites +
4918 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004919}
4920
4921static int ftrace_module_notify_exit(struct notifier_block *self,
4922 unsigned long val, void *data)
4923{
4924 struct module *mod = data;
4925
4926 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004927 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004928
4929 return 0;
4930}
4931#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004932static int ftrace_module_notify_exit(struct notifier_block *self,
4933 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004934{
4935 return 0;
4936}
4937#endif /* CONFIG_MODULES */
4938
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004939struct notifier_block ftrace_module_exit_nb = {
4940 .notifier_call = ftrace_module_notify_exit,
4941 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4942};
4943
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004944void __init ftrace_init(void)
4945{
Jiri Slaby1dc43cf2014-02-24 19:59:56 +01004946 extern unsigned long __start_mcount_loc[];
4947 extern unsigned long __stop_mcount_loc[];
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004948 unsigned long count, flags;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004949 int ret;
4950
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004951 local_irq_save(flags);
Jiri Slaby3a36cb12014-02-24 19:59:59 +01004952 ret = ftrace_dyn_arch_init();
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004953 local_irq_restore(flags);
Jiri Slabyaf64a7c2014-02-24 19:59:58 +01004954 if (ret)
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004955 goto failed;
4956
4957 count = __stop_mcount_loc - __start_mcount_loc;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004958 if (!count) {
4959 pr_info("ftrace: No functions to be traced?\n");
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004960 goto failed;
Jiri Slabyc867ccd2014-02-24 19:59:57 +01004961 }
4962
4963 pr_info("ftrace: allocating %ld entries in %ld pages\n",
4964 count, count / ENTRIES_PER_PAGE + 1);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004965
4966 last_ftrace_enabled = ftrace_enabled = 1;
4967
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004968 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004969 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004970 __stop_mcount_loc);
4971
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004972 ret = register_module_notifier(&ftrace_module_exit_nb);
4973 if (ret)
4974 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004975
Steven Rostedt2af15d62009-05-28 13:37:24 -04004976 set_ftrace_early_filters();
4977
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004978 return;
4979 failed:
4980 ftrace_disabled = 1;
4981}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004982
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04004983/* Do nothing if arch does not support this */
4984void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
4985{
4986}
4987
4988static void ftrace_update_trampoline(struct ftrace_ops *ops)
4989{
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04004990
4991/*
4992 * Currently there's no safe way to free a trampoline when the kernel
4993 * is configured with PREEMPT. That is because a task could be preempted
4994 * when it jumped to the trampoline, it may be preempted for a long time
4995 * depending on the system load, and currently there's no way to know
4996 * when it will be off the trampoline. If the trampoline is freed
4997 * too early, when the task runs again, it will be executing on freed
4998 * memory and crash.
4999 */
5000#ifdef CONFIG_PREEMPT
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005001 /* Currently, only non dynamic ops can have a trampoline */
5002 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5003 return;
Steven Rostedt (Red Hat)12cce592014-07-03 15:48:16 -04005004#endif
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005005
5006 arch_ftrace_update_trampoline(ops);
5007}
5008
Steven Rostedt3d083392008-05-12 21:20:42 +02005009#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005010
Steven Rostedt2b499382011-05-03 22:49:52 -04005011static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04005012 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005013 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedtbd69c302011-05-03 21:55:54 -04005014};
5015
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005016static int __init ftrace_nodyn_init(void)
5017{
5018 ftrace_enabled = 1;
5019 return 0;
5020}
Steven Rostedt6f415672012-10-05 12:13:07 -04005021core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01005022
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005023static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
5024static inline void ftrace_startup_enable(int command) { }
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005025static inline void ftrace_startup_all(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05005026/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005027# define ftrace_startup(ops, command) \
5028 ({ \
5029 int ___ret = __register_ftrace_function(ops); \
5030 if (!___ret) \
5031 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
5032 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04005033 })
Steven Rostedt (Red Hat)1fcc1552014-02-19 15:12:18 -05005034# define ftrace_shutdown(ops, command) \
5035 ({ \
5036 int ___ret = __unregister_ftrace_function(ops); \
5037 if (!___ret) \
5038 (ops)->flags &= ~FTRACE_OPS_FL_ENABLED; \
5039 ___ret; \
5040 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005041
Ingo Molnarc7aafc52008-05-12 21:20:45 +02005042# define ftrace_startup_sysctl() do { } while (0)
5043# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04005044
5045static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005046ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005047{
5048 return 1;
5049}
5050
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005051static void ftrace_update_trampoline(struct ftrace_ops *ops)
5052{
5053}
5054
Steven Rostedt3d083392008-05-12 21:20:42 +02005055#endif /* CONFIG_DYNAMIC_FTRACE */
5056
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005057__init void ftrace_init_global_array_ops(struct trace_array *tr)
5058{
5059 tr->ops = &global_ops;
5060 tr->ops->private = tr;
5061}
5062
5063void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5064{
5065 /* If we filter on pids, update to use the pid function */
5066 if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5067 if (WARN_ON(tr->ops->func != ftrace_stub))
5068 printk("ftrace ops had %pS for function\n",
5069 tr->ops->func);
5070 /* Only the top level instance does pid tracing */
5071 if (!list_empty(&ftrace_pids)) {
5072 set_ftrace_pid_function(func);
5073 func = ftrace_pid_func;
5074 }
5075 }
5076 tr->ops->func = func;
5077 tr->ops->private = tr;
5078}
5079
5080void ftrace_reset_array_ops(struct trace_array *tr)
5081{
5082 tr->ops->func = ftrace_stub;
5083}
5084
Steven Rostedtb8489142011-05-04 09:27:52 -04005085static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005086ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005087 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01005088{
Jiri Olsae2484912012-02-15 15:51:48 +01005089 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
5090 return;
5091
5092 /*
5093 * Some of the ops may be dynamically allocated,
5094 * they must be freed after a synchronize_sched().
5095 */
5096 preempt_disable_notrace();
5097 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05005098
5099 /*
5100 * Control funcs (perf) uses RCU. Only trace if
5101 * RCU is currently active.
5102 */
5103 if (!rcu_is_watching())
5104 goto out;
5105
Steven Rostedt0a016402012-11-02 17:03:03 -04005106 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04005107 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
5108 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04005109 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04005110 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04005111 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05005112 out:
Jiri Olsae2484912012-02-15 15:51:48 +01005113 trace_recursion_clear(TRACE_CONTROL_BIT);
5114 preempt_enable_notrace();
5115}
5116
5117static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005118 .func = ftrace_ops_control_func,
5119 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
Steven Rostedt (Red Hat)33b7f992014-08-15 17:23:02 -04005120 INIT_OPS_HASH(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01005121};
5122
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005123static inline void
5124__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005125 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04005126{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005127 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005128 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04005129
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005130 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5131 if (bit < 0)
5132 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04005133
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005134 /*
5135 * Some of the ops may be dynamically allocated,
5136 * they must be freed after a synchronize_sched().
5137 */
5138 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04005139 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005140 if (ftrace_ops_test(op, ip, regs)) {
Steven Rostedt (Red Hat)1d48d592014-06-25 11:54:03 -04005141 if (FTRACE_WARN_ON(!op->func)) {
5142 pr_warn("op=%p %pS\n", op, op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005143 goto out;
5144 }
Steven Rostedta1e2e312011-08-09 12:50:46 -04005145 op->func(ip, parent_ip, op, regs);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005146 }
Steven Rostedt0a016402012-11-02 17:03:03 -04005147 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)4104d322014-01-10 17:01:58 -05005148out:
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005149 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04005150 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04005151}
5152
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005153/*
5154 * Some archs only support passing ip and parent_ip. Even though
5155 * the list function ignores the op parameter, we do not want any
5156 * C side effects, where a function is called without the caller
5157 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005158 * Archs are to support both the regs and ftrace_ops at the same time.
5159 * If they support ftrace_ops, it is assumed they support regs.
5160 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09005161 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5162 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04005163 * An architecture can pass partial regs with ftrace_ops and still
5164 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005165 */
5166#if ARCH_SUPPORTS_FTRACE_OPS
5167static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04005168 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005169{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005170 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005171}
5172#else
5173static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5174{
Steven Rostedta1e2e312011-08-09 12:50:46 -04005175 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04005176}
5177#endif
5178
Steven Rostedt (Red Hat)f1ff6342014-07-22 20:16:57 -04005179/*
5180 * If there's only one function registered but it does not support
5181 * recursion, this function will be called by the mcount trampoline.
5182 * This function will handle recursion protection.
5183 */
5184static void ftrace_ops_recurs_func(unsigned long ip, unsigned long parent_ip,
5185 struct ftrace_ops *op, struct pt_regs *regs)
5186{
5187 int bit;
5188
5189 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
5190 if (bit < 0)
5191 return;
5192
5193 op->func(ip, parent_ip, op, regs);
5194
5195 trace_clear_recursion(bit);
5196}
5197
Steven Rostedt (Red Hat)87354052014-07-22 20:41:42 -04005198/**
5199 * ftrace_ops_get_func - get the function a trampoline should call
5200 * @ops: the ops to get the function for
5201 *
5202 * Normally the mcount trampoline will call the ops->func, but there
5203 * are times that it should not. For example, if the ops does not
5204 * have its own recursion protection, then it should call the
5205 * ftrace_ops_recurs_func() instead.
5206 *
5207 * Returns the function that the trampoline should call for @ops.
5208 */
5209ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5210{
5211 /*
5212 * If this is a dynamic ops or we force list func,
5213 * then it needs to call the list anyway.
5214 */
5215 if (ops->flags & FTRACE_OPS_FL_DYNAMIC || FTRACE_FORCE_LIST_FUNC)
5216 return ftrace_ops_list_func;
5217
5218 /*
5219 * If the func handles its own recursion, call it directly.
5220 * Otherwise call the recursion protected function that
5221 * will call the ftrace ops function.
5222 */
5223 if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE))
5224 return ftrace_ops_recurs_func;
5225
5226 return ops->func;
5227}
5228
Steven Rostedte32d8952008-12-04 00:26:41 -05005229static void clear_ftrace_swapper(void)
5230{
5231 struct task_struct *p;
5232 int cpu;
5233
5234 get_online_cpus();
5235 for_each_online_cpu(cpu) {
5236 p = idle_task(cpu);
5237 clear_tsk_trace_trace(p);
5238 }
5239 put_online_cpus();
5240}
5241
5242static void set_ftrace_swapper(void)
5243{
5244 struct task_struct *p;
5245 int cpu;
5246
5247 get_online_cpus();
5248 for_each_online_cpu(cpu) {
5249 p = idle_task(cpu);
5250 set_tsk_trace_trace(p);
5251 }
5252 put_online_cpus();
5253}
5254
5255static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005256{
5257 struct task_struct *p;
5258
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005259 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05005260 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05005261 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05005262 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005263 rcu_read_unlock();
5264
Steven Rostedte32d8952008-12-04 00:26:41 -05005265 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005266}
5267
Steven Rostedte32d8952008-12-04 00:26:41 -05005268static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05005269{
5270 struct task_struct *p;
5271
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005272 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005273 do_each_pid_task(pid, PIDTYPE_PID, p) {
5274 set_tsk_trace_trace(p);
5275 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01005276 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05005277}
5278
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005279static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005280{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005281 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05005282 clear_ftrace_swapper();
5283 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005284 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05005285}
5286
5287static void set_ftrace_pid_task(struct pid *pid)
5288{
5289 if (pid == ftrace_swapper_pid)
5290 set_ftrace_swapper();
5291 else
5292 set_ftrace_pid(pid);
5293}
5294
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005295static int ftrace_pid_add(int p)
5296{
5297 struct pid *pid;
5298 struct ftrace_pid *fpid;
5299 int ret = -EINVAL;
5300
5301 mutex_lock(&ftrace_lock);
5302
5303 if (!p)
5304 pid = ftrace_swapper_pid;
5305 else
5306 pid = find_get_pid(p);
5307
5308 if (!pid)
5309 goto out;
5310
5311 ret = 0;
5312
5313 list_for_each_entry(fpid, &ftrace_pids, list)
5314 if (fpid->pid == pid)
5315 goto out_put;
5316
5317 ret = -ENOMEM;
5318
5319 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
5320 if (!fpid)
5321 goto out_put;
5322
5323 list_add(&fpid->list, &ftrace_pids);
5324 fpid->pid = pid;
5325
5326 set_ftrace_pid_task(pid);
5327
5328 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005329
5330 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005331
5332 mutex_unlock(&ftrace_lock);
5333 return 0;
5334
5335out_put:
5336 if (pid != ftrace_swapper_pid)
5337 put_pid(pid);
5338
5339out:
5340 mutex_unlock(&ftrace_lock);
5341 return ret;
5342}
5343
5344static void ftrace_pid_reset(void)
5345{
5346 struct ftrace_pid *fpid, *safe;
5347
5348 mutex_lock(&ftrace_lock);
5349 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
5350 struct pid *pid = fpid->pid;
5351
5352 clear_ftrace_pid_task(pid);
5353
5354 list_del(&fpid->list);
5355 kfree(fpid);
5356 }
5357
5358 ftrace_update_pid_func();
Steven Rostedt (Red Hat)e1effa02014-08-05 17:19:38 -04005359 ftrace_startup_all(0);
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005360
5361 mutex_unlock(&ftrace_lock);
5362}
5363
5364static void *fpid_start(struct seq_file *m, loff_t *pos)
5365{
5366 mutex_lock(&ftrace_lock);
5367
5368 if (list_empty(&ftrace_pids) && (!*pos))
5369 return (void *) 1;
5370
5371 return seq_list_start(&ftrace_pids, *pos);
5372}
5373
5374static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5375{
5376 if (v == (void *)1)
5377 return NULL;
5378
5379 return seq_list_next(v, &ftrace_pids, pos);
5380}
5381
5382static void fpid_stop(struct seq_file *m, void *p)
5383{
5384 mutex_unlock(&ftrace_lock);
5385}
5386
5387static int fpid_show(struct seq_file *m, void *v)
5388{
5389 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
5390
5391 if (v == (void *)1) {
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005392 seq_puts(m, "no pid\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005393 return 0;
5394 }
5395
5396 if (fpid->pid == ftrace_swapper_pid)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01005397 seq_puts(m, "swapper tasks\n");
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005398 else
5399 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
5400
5401 return 0;
5402}
5403
5404static const struct seq_operations ftrace_pid_sops = {
5405 .start = fpid_start,
5406 .next = fpid_next,
5407 .stop = fpid_stop,
5408 .show = fpid_show,
5409};
5410
5411static int
5412ftrace_pid_open(struct inode *inode, struct file *file)
5413{
5414 int ret = 0;
5415
5416 if ((file->f_mode & FMODE_WRITE) &&
5417 (file->f_flags & O_TRUNC))
5418 ftrace_pid_reset();
5419
5420 if (file->f_mode & FMODE_READ)
5421 ret = seq_open(file, &ftrace_pid_sops);
5422
5423 return ret;
5424}
5425
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005426static ssize_t
5427ftrace_pid_write(struct file *filp, const char __user *ubuf,
5428 size_t cnt, loff_t *ppos)
5429{
Ingo Molnar457dc922009-11-23 11:03:28 +01005430 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005431 long val;
5432 int ret;
5433
5434 if (cnt >= sizeof(buf))
5435 return -EINVAL;
5436
5437 if (copy_from_user(&buf, ubuf, cnt))
5438 return -EFAULT;
5439
5440 buf[cnt] = 0;
5441
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005442 /*
5443 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
5444 * to clean the filter quietly.
5445 */
Ingo Molnar457dc922009-11-23 11:03:28 +01005446 tmp = strstrip(buf);
5447 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005448 return 1;
5449
Daniel Walterbcd83ea2012-09-26 22:08:38 +02005450 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005451 if (ret < 0)
5452 return ret;
5453
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005454 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05005455
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005456 return ret ? ret : cnt;
5457}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005458
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005459static int
5460ftrace_pid_release(struct inode *inode, struct file *file)
5461{
5462 if (file->f_mode & FMODE_READ)
5463 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005464
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005465 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005466}
5467
Steven Rostedt5e2336a2009-03-05 21:44:55 -05005468static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005469 .open = ftrace_pid_open,
5470 .write = ftrace_pid_write,
5471 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05005472 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04005473 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005474};
5475
5476static __init int ftrace_init_debugfs(void)
5477{
5478 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005479
5480 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05005481 if (IS_ERR(d_tracer))
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005482 return 0;
5483
5484 ftrace_init_dyn_debugfs(d_tracer);
5485
Frederic Weisbecker5452af62009-03-27 00:25:38 +01005486 trace_create_file("set_ftrace_pid", 0644, d_tracer,
5487 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04005488
5489 ftrace_profile_debugfs(d_tracer);
5490
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005491 return 0;
5492}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05005493fs_initcall(ftrace_init_debugfs);
5494
Steven Rostedt3d083392008-05-12 21:20:42 +02005495/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005496 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005497 *
5498 * This function should be used by panic code. It stops ftrace
5499 * but in a not so nice way. If you need to simply kill ftrace
5500 * from a non-atomic section, use ftrace_kill.
5501 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04005502void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005503{
5504 ftrace_disabled = 1;
5505 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04005506 clear_ftrace_function();
5507}
5508
5509/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04005510 * Test if ftrace is dead or not.
5511 */
5512int ftrace_is_dead(void)
5513{
5514 return ftrace_disabled;
5515}
5516
5517/**
Steven Rostedt3d083392008-05-12 21:20:42 +02005518 * register_ftrace_function - register a function for profiling
5519 * @ops - ops structure that holds the function for profiling.
5520 *
5521 * Register a function to be called by all functions in the
5522 * kernel.
5523 *
5524 * Note: @ops->func and all the functions it calls must be labeled
5525 * with "notrace", otherwise it will go into a
5526 * recursive loop.
5527 */
5528int register_ftrace_function(struct ftrace_ops *ops)
5529{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005530 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005531
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09005532 ftrace_ops_init(ops);
5533
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005534 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005535
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005536 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04005537
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005538 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02005539
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005540 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02005541}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005542EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02005543
5544/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01005545 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02005546 * @ops - ops structure that holds the function to unregister
5547 *
5548 * Unregister a function that was added to be called by ftrace profiling.
5549 */
5550int unregister_ftrace_function(struct ftrace_ops *ops)
5551{
5552 int ret;
5553
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005554 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005555 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005556 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005557
5558 return ret;
5559}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04005560EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005561
Ingo Molnare309b412008-05-12 21:20:51 +02005562int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005563ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07005564 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005565 loff_t *ppos)
5566{
Steven Rostedt45a4a232011-04-21 23:16:46 -04005567 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02005568
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005569 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005570
Steven Rostedt45a4a232011-04-21 23:16:46 -04005571 if (unlikely(ftrace_disabled))
5572 goto out;
5573
5574 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005575
Li Zefana32c7762009-06-26 16:55:51 +08005576 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005577 goto out;
5578
Li Zefana32c7762009-06-26 16:55:51 +08005579 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005580
5581 if (ftrace_enabled) {
5582
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005583 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01005584 if (ftrace_ops_list != &ftrace_list_end)
5585 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005586
Steven Rostedt (Red Hat)524a3862015-03-06 19:55:13 -05005587 ftrace_startup_sysctl();
5588
Steven Rostedtb0fc4942008-05-12 21:20:43 +02005589 } else {
5590 /* stopping ftrace calls (just send to ftrace_stub) */
5591 ftrace_trace_function = ftrace_stub;
5592
5593 ftrace_shutdown_sysctl();
5594 }
5595
5596 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005597 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02005598 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02005599}
Ingo Molnarf17845e2008-10-24 12:47:10 +02005600
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005601#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005602
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005603static struct ftrace_ops graph_ops = {
5604 .func = ftrace_stub,
5605 .flags = FTRACE_OPS_FL_RECURSION_SAFE |
5606 FTRACE_OPS_FL_INITIALIZED |
5607 FTRACE_OPS_FL_STUB,
5608#ifdef FTRACE_GRAPH_TRAMP_ADDR
5609 .trampoline = FTRACE_GRAPH_TRAMP_ADDR,
Steven Rostedt (Red Hat)aec0be22014-11-18 21:14:11 -05005610 /* trampoline_size is only needed for dynamically allocated tramps */
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005611#endif
5612 ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5613};
5614
Steven Rostedte49dc192008-12-02 23:50:05 -05005615int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5616{
5617 return 0;
5618}
5619
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005620/* The callbacks that hook a function */
5621trace_func_graph_ret_t ftrace_graph_return =
5622 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005623trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005624static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005625
5626/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5627static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5628{
5629 int i;
5630 int ret = 0;
5631 unsigned long flags;
5632 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5633 struct task_struct *g, *t;
5634
5635 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5636 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5637 * sizeof(struct ftrace_ret_stack),
5638 GFP_KERNEL);
5639 if (!ret_stack_list[i]) {
5640 start = 0;
5641 end = i;
5642 ret = -ENOMEM;
5643 goto free;
5644 }
5645 }
5646
5647 read_lock_irqsave(&tasklist_lock, flags);
5648 do_each_thread(g, t) {
5649 if (start == end) {
5650 ret = -EAGAIN;
5651 goto unlock;
5652 }
5653
5654 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01005655 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005656 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04005657 t->curr_ret_stack = -1;
5658 /* Make sure the tasks see the -1 first: */
5659 smp_wmb();
5660 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005661 }
5662 } while_each_thread(g, t);
5663
5664unlock:
5665 read_unlock_irqrestore(&tasklist_lock, flags);
5666free:
5667 for (i = start; i < end; i++)
5668 kfree(ret_stack_list[i]);
5669 return ret;
5670}
5671
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005672static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04005673ftrace_graph_probe_sched_switch(void *ignore,
5674 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005675{
5676 unsigned long long timestamp;
5677 int index;
5678
Steven Rostedtbe6f1642009-03-24 11:06:24 -04005679 /*
5680 * Does the user want to count the time a function was asleep.
5681 * If so, do not update the time stamps.
5682 */
5683 if (trace_flags & TRACE_ITER_SLEEP_TIME)
5684 return;
5685
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005686 timestamp = trace_clock_local();
5687
5688 prev->ftrace_timestamp = timestamp;
5689
5690 /* only process tasks that we timestamped */
5691 if (!next->ftrace_timestamp)
5692 return;
5693
5694 /*
5695 * Update all the counters in next to make up for the
5696 * time next was sleeping.
5697 */
5698 timestamp -= next->ftrace_timestamp;
5699
5700 for (index = next->curr_ret_stack; index >= 0; index--)
5701 next->ret_stack[index].calltime += timestamp;
5702}
5703
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005704/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005705static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005706{
5707 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005708 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005709
5710 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5711 sizeof(struct ftrace_ret_stack *),
5712 GFP_KERNEL);
5713
5714 if (!ret_stack_list)
5715 return -ENOMEM;
5716
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005717 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005718 for_each_online_cpu(cpu) {
5719 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005720 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005721 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005722
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005723 do {
5724 ret = alloc_retstack_tasklist(ret_stack_list);
5725 } while (ret == -EAGAIN);
5726
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005727 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005728 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005729 if (ret)
5730 pr_info("ftrace_graph: Couldn't activate tracepoint"
5731 " probe to kernel_sched_switch\n");
5732 }
5733
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005734 kfree(ret_stack_list);
5735 return ret;
5736}
5737
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005738/*
5739 * Hibernation protection.
5740 * The state of the current task is too much unstable during
5741 * suspend/restore to disk. We want to protect against that.
5742 */
5743static int
5744ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5745 void *unused)
5746{
5747 switch (state) {
5748 case PM_HIBERNATION_PREPARE:
5749 pause_graph_tracing();
5750 break;
5751
5752 case PM_POST_HIBERNATION:
5753 unpause_graph_tracing();
5754 break;
5755 }
5756 return NOTIFY_DONE;
5757}
5758
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005759static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5760{
5761 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5762 return 0;
5763 return __ftrace_graph_entry(trace);
5764}
5765
5766/*
5767 * The function graph tracer should only trace the functions defined
5768 * by set_ftrace_filter and set_ftrace_notrace. If another function
5769 * tracer ops is registered, the graph tracer requires testing the
5770 * function against the global ops, and not just trace any function
5771 * that any ftrace_ops registered.
5772 */
5773static void update_function_graph_func(void)
5774{
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005775 struct ftrace_ops *op;
5776 bool do_test = false;
5777
5778 /*
5779 * The graph and global ops share the same set of functions
5780 * to test. If any other ops is on the list, then
5781 * the graph tracing needs to test if its the function
5782 * it should call.
5783 */
5784 do_for_each_ftrace_op(op, ftrace_ops_list) {
5785 if (op != &global_ops && op != &graph_ops &&
5786 op != &ftrace_list_end) {
5787 do_test = true;
5788 /* in double loop, break out with goto */
5789 goto out;
5790 }
5791 } while_for_each_ftrace_op(op);
5792 out:
5793 if (do_test)
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005794 ftrace_graph_entry = ftrace_graph_entry_test;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005795 else
5796 ftrace_graph_entry = __ftrace_graph_entry;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005797}
5798
Mathias Krause8275f692014-03-30 15:31:50 +02005799static struct notifier_block ftrace_suspend_notifier = {
5800 .notifier_call = ftrace_suspend_notifier_call,
5801};
5802
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005803int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5804 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005805{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005806 int ret = 0;
5807
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005808 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005809
Steven Rostedt05ce5812009-03-24 00:18:31 -04005810 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005811 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005812 ret = -EBUSY;
5813 goto out;
5814 }
5815
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005816 register_pm_notifier(&ftrace_suspend_notifier);
5817
Steven Rostedt597af812009-04-03 15:24:12 -04005818 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005819 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005820 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005821 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005822 goto out;
5823 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005824
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005825 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005826
5827 /*
5828 * Update the indirect function to the entryfunc, and the
5829 * function that gets called to the entry_test first. Then
5830 * call the update fgraph entry function to determine if
5831 * the entryfunc should be called directly or not.
5832 */
5833 __ftrace_graph_entry = entryfunc;
5834 ftrace_graph_entry = ftrace_graph_entry_test;
5835 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005836
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005837 ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005838out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005839 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005840 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005841}
5842
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005843void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005844{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005845 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005846
Steven Rostedt597af812009-04-03 15:24:12 -04005847 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005848 goto out;
5849
Steven Rostedt597af812009-04-03 15:24:12 -04005850 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005851 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005852 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005853 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)5f151b22014-08-15 17:18:46 -04005854 ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005855 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005856 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005857
Steven Rostedt (Red Hat)f3bea492014-07-02 23:23:31 -04005858#ifdef CONFIG_DYNAMIC_FTRACE
5859 /*
5860 * Function graph does not allocate the trampoline, but
5861 * other global_ops do. We need to reset the ALLOC_TRAMP flag
5862 * if one was used.
5863 */
5864 global_ops.trampoline = save_global_trampoline;
5865 if (save_global_flags & FTRACE_OPS_FL_ALLOC_TRAMP)
5866 global_ops.flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
5867#endif
5868
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005869 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005870 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005871}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005872
Steven Rostedt868baf02011-02-10 21:26:13 -05005873static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5874
5875static void
5876graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5877{
5878 atomic_set(&t->tracing_graph_pause, 0);
5879 atomic_set(&t->trace_overrun, 0);
5880 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005881 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005882 smp_wmb();
5883 t->ret_stack = ret_stack;
5884}
5885
5886/*
5887 * Allocate a return stack for the idle task. May be the first
5888 * time through, or it may be done by CPU hotplug online.
5889 */
5890void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5891{
5892 t->curr_ret_stack = -1;
5893 /*
5894 * The idle task has no parent, it either has its own
5895 * stack or no stack at all.
5896 */
5897 if (t->ret_stack)
5898 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5899
5900 if (ftrace_graph_active) {
5901 struct ftrace_ret_stack *ret_stack;
5902
5903 ret_stack = per_cpu(idle_ret_stack, cpu);
5904 if (!ret_stack) {
5905 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5906 * sizeof(struct ftrace_ret_stack),
5907 GFP_KERNEL);
5908 if (!ret_stack)
5909 return;
5910 per_cpu(idle_ret_stack, cpu) = ret_stack;
5911 }
5912 graph_init_task(t, ret_stack);
5913 }
5914}
5915
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005916/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005917void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005918{
Steven Rostedt84047e32009-06-02 16:51:55 -04005919 /* Make sure we do not use the parent ret_stack */
5920 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005921 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005922
Steven Rostedt597af812009-04-03 15:24:12 -04005923 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005924 struct ftrace_ret_stack *ret_stack;
5925
5926 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005927 * sizeof(struct ftrace_ret_stack),
5928 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005929 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005930 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005931 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005932 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005933}
5934
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005935void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005936{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005937 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5938
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005939 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005940 /* NULL must become visible to IRQs before we free it: */
5941 barrier();
5942
5943 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005944}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005945#endif